@@ -1665,11 +1665,11 @@ public function dotVector() : void
16651665 public function convolve () : void
16661666 {
16671667 $ a = Matrix::fromArray ([
1668- [3.0 , 27.0 , 66.0 , 29.0 , 42.0 , 5.0 ],
1669- [5.0 , 9.0 , 15.0 , 42.0 , 45.0 , 16.0 ],
1670- [1.0 , 5.0 , 10.0 , 22.0 , 66.0 , 5.0 ],
1668+ [3.0 , 27.0 , 66.0 , 29.0 , 42.0 , 5.0 , 0.0 ],
1669+ [5.0 , 9.0 , 15.0 , 42.0 , 45.0 , 16.0 , 0.0 ],
1670+ [1.0 , 5.0 , 10.0 , 22.0 , 66.0 , 5.0 , 0.0 ],
16711671 [0.0 , 1.0 , 4.0 , 9.0 , 10.0 , 22.0 , 2.0 ],
1672- [0.0 , 0.0 , 3.0 , 19.0 , 21.0 , 25.0 ],
1672+ [0.0 , 0.0 , 3.0 , 19.0 , 21.0 , 25.0 , 0.0 ],
16731673 [0.0 , 0.0 , 0.0 , 5.0 , 2.0 , 33.0 , 35.0 ],
16741674 ], false );
16751675
@@ -1682,17 +1682,86 @@ public function convolve() : void
16821682 $ c = $ a ->convolve ($ b , 1 );
16831683
16841684 $ expected = Matrix::fromArray ([
1685- [3.0 , 32.0 , 75.0 , 44.0 , 84.0 , 50.0 ],
1686- [32.0 , 76.0 , 49.0 , 94.0 , 72.0 , 82.0 ],
1687- [10.0 , 20.0 , 53.0 , 71.0 , 91.0 , 15.0 ],
1688- [5.0 , 11.0 , 26.0 , 78.0 , 34.0 , 43.0 ],
1689- [1.0 , 4.0 , 12.0 , 29.0 , 48.0 , 27 .0 ],
1690- [0.0 , 3.0 , 19.0 , 26.0 , 27.0 , 33.0 ],
1685+ [3.0 , 32.0 , 75.0 , 44.0 , 84.0 , 50.0 , 16.0 ],
1686+ [32.0 , 76.0 , 49.0 , 94.0 , 72.0 , 82.0 , 5.0 ],
1687+ [10.0 , 20.0 , 53.0 , 71.0 , 91.0 , 15.0 , 22.0 ],
1688+ [5.0 , 11.0 , 26.0 , 78.0 , 34.0 , 43.0 , 27.0 ],
1689+ [1.0 , 4.0 , 12.0 , 29.0 , 48.0 , 29.0 , 33 .0 ],
1690+ [0.0 , 3.0 , 19.0 , 26.0 , 27.0 , 33.0 , 35.0 ],
16911691 ], false );
16921692
16931693 $ this ->assertEqualsWithDelta ($ expected , $ c , self ::MAX_DELTA );
16941694 }
16951695
1696+ #[Test]
1697+ public function convolveStride () : void
1698+ {
1699+ $ a = Matrix::fromArray ([
1700+ [1.0 , 2.0 , 3.0 ],
1701+ [4.0 , 5.0 , 6.0 ],
1702+ [7.0 , 8.0 , 9.0 ],
1703+ ], false );
1704+
1705+ $ b = Matrix::fromArray ([
1706+ [1.0 , 0.0 ],
1707+ [0.0 , 1.0 ],
1708+ ], false );
1709+
1710+ $ c = $ a ->convolve ($ b , 2 );
1711+
1712+ $ expected = Matrix::fromArray ([
1713+ [6.0 , 3.0 ],
1714+ [7.0 , 9.0 ],
1715+ ], false );
1716+
1717+ $ this ->assertEqualsWithDelta ($ expected , $ c , self ::MAX_DELTA );
1718+ }
1719+
1720+ #[Test]
1721+ public function convolveWithEvenKernel () : void
1722+ {
1723+ $ a = Matrix::fromArray ([
1724+ [1.0 , 2.0 , 3.0 , 4.0 ],
1725+ ], false );
1726+
1727+ $ b = Matrix::fromArray ([
1728+ [1.0 , 2.0 , 3.0 ],
1729+ ], false );
1730+
1731+ $ c = $ a ->convolve ($ b , 1 );
1732+
1733+ $ expected = Matrix::fromArray ([
1734+ [4.0 , 10.0 , 16.0 , 17.0 ],
1735+ ], false );
1736+
1737+ $ this ->assertEqualsWithDelta ($ expected , $ c , self ::MAX_DELTA );
1738+ }
1739+
1740+ #[Test]
1741+ public function convolveStrideLessThanOneThrows () : void
1742+ {
1743+ $ this ->expectException (InvalidArgumentException::class);
1744+
1745+ Matrix::fromArray ([[1.0 , 2.0 , 3.0 ]], false )
1746+ ->convolve (Matrix::fromArray ([[1.0 , 1.0 ]], false ), 0 );
1747+ }
1748+
1749+ #[Test]
1750+ public function convolveKernelLargerThanMatrixThrows () : void
1751+ {
1752+ $ this ->expectException (InvalidArgumentException::class);
1753+
1754+ Matrix::fromArray ([
1755+ [1.0 , 2.0 ],
1756+ [3.0 , 4.0 ],
1757+ ], false )
1758+ ->convolve (Matrix::fromArray ([
1759+ [1.0 , 2.0 , 3.0 ],
1760+ [4.0 , 5.0 , 6.0 ],
1761+ [7.0 , 8.0 , 9.0 ],
1762+ ], false ));
1763+ }
1764+
16961765 /**
16971766 * @param Matrix $a
16981767 * @param Tensor|float $b
0 commit comments