Skip to content

Commit a6b6910

Browse files
committed
Fix tests
1 parent 8c4dc0e commit a6b6910

3 files changed

Lines changed: 94 additions & 11 deletions

File tree

‎src/Matrix.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ public function convolve(Matrix $b, int $stride = 1) : self
817817
$rowC = [];
818818

819819
for ($j = 0; $j < $this->n; $j += $stride) {
820-
$sigma = 0;
820+
$sigma = 0.0;
821821

822822
foreach ($b as $k => $rowB) {
823823
$x = $i + $p - $k;

‎tests/MatrixTest.php‎

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎tests/VectorTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,20 @@ public function convolve() : void
912912
$this->assertEqualsWithDelta($expected, $c, self::MAX_DELTA);
913913
}
914914

915+
#[Test]
916+
public function convolveStride() : void
917+
{
918+
$a = Vector::fromArray([1.0, 2.0, 3.0, 4.0], false);
919+
920+
$b = Vector::fromArray([1.0, 2.0], false);
921+
922+
$c = $a->convolve($b, 2);
923+
924+
$expected = Vector::fromArray([1.0, 7.0, 8.0], false);
925+
926+
$this->assertEqualsWithDelta($expected, $c, self::MAX_DELTA);
927+
}
928+
915929
/**
916930
* @param Vector $a
917931
* @param Tensor|float $b

0 commit comments

Comments
 (0)