Skip to content

Commit 255da29

Browse files
committed
Update LUDecomp constructor to properly inherit from Matrix.
1 parent f9fcd2f commit 255da29

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

src/LUDecomposition.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,12 @@ final class LUDecomposition extends Matrix
2121
protected $permutations = []; // Stores a vector representation of the row permutations performed on this matrix.
2222

2323
/**
24-
* @param Matrix $matrix The matrix to decompose.
24+
* @param array $literal The matrix to decompose.
2525
* @throws MatrixException
2626
*/
27-
public function __construct(Matrix $matrix)
27+
public function __construct(array $literal)
2828
{
29-
// Copy the matrix
30-
$matrix->map(function ($element, $i, $j) {
31-
$this->internal[$i][$j] = $element;
32-
});
33-
34-
$this->rowCount = $matrix->rows;
35-
$this->columnCount = $matrix->columns;
29+
parent::__construct($literal);
3630

3731
if (!$this->isSquare()) {
3832
throw new MatrixException("Matrix is not square.");

src/Matrix.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ protected function choleskyInverse()
652652
protected function getLUDecomp()
653653
{
654654
if (!$this->LU) {
655-
$this->LU = new LUDecomposition($this);
655+
$this->LU = new LUDecomposition($this->internal);
656656
}
657657

658658
return $this->LU;

tests/LUDecompTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ public function testNonSquare()
2222
{
2323
static::setExpectedException(MatrixException::class);
2424

25-
new LUDecomposition(new Matrix([[1, 2, 3]]));
25+
new LUDecomposition([[1, 2, 3]]);
2626
}
2727

2828
public function testSingularByZeroes()
2929
{
3030
static::setExpectedException(MatrixException::class);
3131

32-
new LUDecomposition(new Matrix([
32+
new LUDecomposition([
3333
[0, 0],
3434
[0, 0],
35-
]));
35+
]);
3636
}
3737

3838
public function testWrongSizeKnowns()
3939
{
4040
$matrix = $this->buildMatrix();
41-
$decomp = new LUDecomposition($matrix);
41+
$decomp = new LUDecomposition($matrix->toArray());
4242

4343
static::setExpectedException(MatrixException::class);
4444

@@ -49,18 +49,18 @@ public function testSingularByDiagonal()
4949
{
5050
static::setExpectedException(MatrixException::class);
5151

52-
new LUDecomposition(new Matrix([
52+
new LUDecomposition([
5353
[1, -1, 2],
5454
[1, 0, 1],
5555
[2, 3, -1],
56-
]));
56+
]);
5757
}
5858

5959
public function testLUDecompConstructor()
6060
{
6161
$matrix = $this->buildMatrix();
6262

63-
$LU = new LUDecomposition($matrix);
63+
$LU = new LUDecomposition($matrix->toArray());
6464

6565
$this->assertEquals(1, $LU->get(0, 0));
6666
$this->assertEquals(-1, $LU->get(0, 1));
@@ -77,7 +77,7 @@ public function testDeterminant()
7777
{
7878
$matrix = $this->buildMatrix();
7979

80-
$LU = new LUDecomposition($matrix);
80+
$LU = new LUDecomposition($matrix->toArray());
8181

8282
$this->assertEquals(5, $LU->determinant());
8383
}
@@ -90,7 +90,7 @@ public function testSolve()
9090
[1, -1, 4],
9191
]);
9292

93-
$LU = new LUDecomposition($matrix);
93+
$LU = new LUDecomposition($matrix->toArray());
9494

9595
$b = [1, 9, 8];
9696

@@ -107,7 +107,7 @@ public function testSolve()
107107
[-2, -3, 4, 4],
108108
]);
109109

110-
$LU = new LUDecomposition($matrix);
110+
$LU = new LUDecomposition($matrix->toArray());
111111

112112
$b = [-1, 7, -24, 3];
113113

@@ -125,7 +125,7 @@ public function testInverse()
125125
{
126126
$matrix = $this->buildMatrix();
127127

128-
$LU = new LUDecomposition($matrix);
128+
$LU = new LUDecomposition($matrix->toArray());
129129

130130
$inverse = $LU->inverse();
131131

0 commit comments

Comments
 (0)