22
33namespace mcordingley \LinearAlgebra ;
44
5- class LUDecompTest extends \PHPUnit_Framework_TestCase {
6-
5+ class LUDecompTest extends \PHPUnit_Framework_TestCase
6+ {
77 /**
88 * Builds a nonsingular matrix
9- *
10- * @return \mcordingley\LinearAlgebra\ Matrix
9+ *
10+ * @return Matrix
1111 */
12- private function buildMatrix () {
12+ private function buildMatrix ()
13+ {
1314 return new Matrix ([
1415 [1 , -1 , 2 ],
1516 [1 , -1 , 1 ],
16- [2 , 3 , -1 ]
17+ [2 , 3 , -1 ],
1718 ]);
1819 }
1920
20- public function testLUDecompConstructor () {
21+ public function testLUDecompConstructor ()
22+ {
2123 $ matrix = $ this ->buildMatrix ();
22-
24+
2325 $ LU = new LUDecomposition ($ matrix );
24-
25- $ this ->assertEquals (1 , $ LU ->get (0 ,0 ));
26- $ this ->assertEquals (-1 , $ LU ->get (0 ,1 ));
27- $ this ->assertEquals (1 , $ LU ->get (0 ,2 ));
28- $ this ->assertEquals (2 , $ LU ->get (1 ,0 ));
29- $ this ->assertEquals (5 , $ LU ->get (1 ,1 ));
30- $ this ->assertEquals (-3 , $ LU ->get (1 ,2 ));
31- $ this ->assertEquals (1 , $ LU ->get (2 ,0 ));
32- $ this ->assertEquals (0 , $ LU ->get (2 ,1 ));
33- $ this ->assertEquals (1 , $ LU ->get (2 ,2 ));
26+
27+ $ this ->assertEquals (1 , $ LU ->get (0 , 0 ));
28+ $ this ->assertEquals (-1 , $ LU ->get (0 , 1 ));
29+ $ this ->assertEquals (1 , $ LU ->get (0 , 2 ));
30+ $ this ->assertEquals (2 , $ LU ->get (1 , 0 ));
31+ $ this ->assertEquals (5 , $ LU ->get (1 , 1 ));
32+ $ this ->assertEquals (-3 , $ LU ->get (1 , 2 ));
33+ $ this ->assertEquals (1 , $ LU ->get (2 , 0 ));
34+ $ this ->assertEquals (0 , $ LU ->get (2 , 1 ));
35+ $ this ->assertEquals (1 , $ LU ->get (2 , 2 ));
3436 }
3537
3638 public function testDeterminant ()
@@ -45,26 +47,26 @@ public function testDeterminant()
4547 public function testSolve ()
4648 {
4749 $ matrix = new Matrix ([
48- [1 , 1 , -1 ],
49- [3 , 1 , 1 ],
50- [1 , -1 , 4 ]
50+ [1 , 1 , -1 ],
51+ [3 , 1 , 1 ],
52+ [1 , -1 , 4 ],
5153 ]);
5254
5355 $ LU = new LUDecomposition ($ matrix );
5456
55- $ b =[1 ,9 , 8 ];
57+ $ b = [1 , 9 , 8 ];
5658
5759 $ x = $ LU ->solve ($ b );
5860
5961 $ expected_x = [3 , -1 , 1 ];
6062 $ this ->assertEquals ($ expected_x , $ x );
6163
6264 // Try another one
63- $ matrix = new Matrix ([
65+ $ matrix = new Matrix ([
6466 [1 , 2 , 0 , -4 ],
6567 [-1 , 0 , 6 , 2 ],
6668 [3 , -2 , -25 , 0 ],
67- [-2 , -3 , 4 , 4 ]
69+ [-2 , -3 , 4 , 4 ],
6870 ]);
6971
7072 $ LU = new LUDecomposition ($ matrix );
@@ -84,11 +86,11 @@ public function testSolve()
8486 public function testInverse ()
8587 {
8688 $ matrix = $ this ->buildMatrix ();
87-
89+
8890 $ LU = new LUDecomposition ($ matrix );
89-
91+
9092 $ inverse = $ LU ->inverse ();
91-
93+
9294 $ message = 'Not within tolerance ' ;
9395 $ delta = 0.00000001 ;
9496 $ this ->assertEquals (-0.4 , $ inverse ->get (0 , 0 ), $ message , $ delta );
0 commit comments