MatrixSolveTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Matrix\Numeric;
  3. use MathPHP\LinearAlgebra\MatrixFactory;
  4. use MathPHP\LinearAlgebra\NumericMatrix;
  5. use MathPHP\LinearAlgebra\Vector;
  6. use MathPHP\Exception;
  7. class MatrixSolveTest extends \PHPUnit\Framework\TestCase
  8. {
  9. use \MathPHP\Tests\LinearAlgebra\Fixture\MatrixDataProvider;
  10. /**
  11. * @test Solve array
  12. * @dataProvider dataProviderForSolve
  13. * @param array $A
  14. * @param array $b
  15. * @param array $expected
  16. * @throws \Exception
  17. */
  18. public function testSolveArray(array $A, array $b, array $expected)
  19. {
  20. // Given
  21. $A = MatrixFactory::create($A);
  22. $expected = new Vector($expected);
  23. // When
  24. $x = $A->solve($b);
  25. // Then
  26. $this->assertEqualsWithDelta($expected, $x, 0.00001);
  27. }
  28. /**
  29. * @test Solve vector
  30. * @dataProvider dataProviderForSolve
  31. * @param array $A
  32. * @param array $b
  33. * @param array $expected
  34. * @throws \Exception
  35. */
  36. public function testSolveVector(array $A, array $b, array $expected)
  37. {
  38. // Given
  39. $A = MatrixFactory::create($A);
  40. $b = new Vector($b);
  41. $expected = new Vector($expected);
  42. // When
  43. $x = $A->solve($b);
  44. // Then
  45. $this->assertEqualsWithDelta($expected, $x, 0.00001);
  46. }
  47. /**
  48. * @test Compute the inverse before trying to solve
  49. * @dataProvider dataProviderForSolve
  50. * @param array $A
  51. * @param array $b
  52. * @param array $expected
  53. * @throws \Exception
  54. */
  55. public function testSolveInverse(array $A, array $b, array $expected)
  56. {
  57. // Given
  58. $A = MatrixFactory::create($A);
  59. $b = new Vector($b);
  60. $expected = new Vector($expected);
  61. // When
  62. $A->inverse();
  63. $x = $A->solve($b);
  64. // Then
  65. $this->assertEqualsWithDelta($expected, $x, 0.00001);
  66. }
  67. /**
  68. * @test Compute the RREF before trying to solve.
  69. * @dataProvider dataProviderForSolve
  70. * @param array $A
  71. * @param array $b
  72. * @param array $expected
  73. * @throws \Exception
  74. */
  75. public function testSolveRref(array $A, array $b, array $expected)
  76. {
  77. // Given
  78. $A = MatrixFactory::create($A);
  79. $b = new Vector($b);
  80. $expected = new Vector($expected);
  81. // When
  82. $A->rref();
  83. $x = $A->solve($b);
  84. // Then
  85. $this->assertEqualsWithDelta($expected, $x, 0.00001);
  86. }
  87. /**
  88. * @test Solve forcing LU method
  89. * @dataProvider dataProviderForSolve
  90. * @param array $A
  91. * @param array $b
  92. * @param array $expected
  93. * @throws \Exception
  94. */
  95. public function testSolveForcingLuMethod(array $A, array $b, array $expected)
  96. {
  97. // Given
  98. $A = MatrixFactory::create($A);
  99. $b = new Vector($b);
  100. $expected = new Vector($expected);
  101. // When
  102. $x = $A->solve($b, NumericMatrix::LU);
  103. // Then
  104. $this->assertEqualsWithDelta($expected, $x, 0.00001);
  105. }
  106. /**
  107. * @test Solve forcing QR method
  108. * @dataProvider dataProviderForSolve
  109. * @param array $A
  110. * @param array $b
  111. * @param array $expected
  112. * @throws \Exception
  113. */
  114. public function testSolveForcingQrMethod(array $A, array $b, array $expected)
  115. {
  116. // Given
  117. $A = MatrixFactory::create($A);
  118. $b = new Vector($b);
  119. $expected = new Vector($expected);
  120. // When
  121. $x = $A->solve($b, NumericMatrix::QR);
  122. // Then
  123. $this->assertEqualsWithDelta($expected, $x, 0.00001);
  124. }
  125. /**
  126. * @test Solve forcing Inverse method
  127. * @dataProvider dataProviderForSolve
  128. * @param array $A
  129. * @param array $b
  130. * @param array $expected
  131. * @throws \Exception
  132. */
  133. public function testSolveForcingInverseMethod(array $A, array $b, array $expected)
  134. {
  135. // Given
  136. $A = MatrixFactory::create($A);
  137. $b = new Vector($b);
  138. $expected = new Vector($expected);
  139. // When
  140. $x = $A->solve($b, NumericMatrix::INVERSE);
  141. // Then
  142. $this->assertEqualsWithDelta($expected, $x, 0.00001);
  143. }
  144. /**
  145. * @test Solve forcing RREF method
  146. * @dataProvider dataProviderForSolve
  147. * @param array $A
  148. * @param array $b
  149. * @param array $expected
  150. * @throws \Exception
  151. */
  152. public function testSolveForcingRrefMethod(array $A, array $b, array $expected)
  153. {
  154. // Given
  155. $A = MatrixFactory::create($A);
  156. $b = new Vector($b);
  157. $expected = new Vector($expected);
  158. // When
  159. $x = $A->solve($b, NumericMatrix::RREF);
  160. // Then
  161. $this->assertEqualsWithDelta($expected, $x, 0.00001);
  162. }
  163. /**
  164. * @test solve exception
  165. * @dataProvider dataProviderForSolveExceptionNotVectorOrArray
  166. * @throws \Exception
  167. */
  168. public function testSolveExceptionNotVectorOrArray($b)
  169. {
  170. // Given
  171. $A = new NumericMatrix([
  172. [1, 2, 3],
  173. [2, 3, 4],
  174. [3, 4, 5],
  175. ]);
  176. // Then
  177. $this->expectException(Exception\IncorrectTypeException::class);
  178. // When
  179. $A->solve($b);
  180. }
  181. /**
  182. * @return array
  183. * @throws \Exception
  184. */
  185. public function dataProviderForSolveExceptionNotVectorOrArray(): array
  186. {
  187. return [
  188. [new NumericMatrix([[1], [2], [3]])],
  189. [25],
  190. ];
  191. }
  192. /**
  193. * @test Test ref by solving the system of linear equations.
  194. * There is no single row echelon form for a matrix (as opposed to reduced row echelon form).
  195. * Therefore, instead of directly testing the REF obtained,
  196. * use the REF to then solve for x using back substitution.
  197. * The result should be the expected solution to the system of linear equations.
  198. * @dataProvider dataProviderForSolve
  199. * @param array $A
  200. * @param array $b
  201. * @param array $expected_x
  202. * @throws \Exception
  203. */
  204. public function testRefUsingSolve(array $A, array $b, array $expected_x)
  205. {
  206. // Given
  207. $m = count($b);
  208. $A = MatrixFactory::create($A);
  209. $b_matrix = MatrixFactory::createFromVectors([new Vector($b)]);
  210. $Ab = $A->augment($b_matrix);
  211. $ref = $Ab->ref();
  212. // When solve for x using back substitution on the REF matrix
  213. $x = [];
  214. for ($i = $m - 1; $i >= 0; $i--) {
  215. $x[$i] = $ref[$i][$m];
  216. for ($j = $i + 1; $j < $m; $j++) {
  217. $x[$i] -= $ref[$i][$j] * $x[$j];
  218. }
  219. $x[$i] /= $ref[$i][$i];
  220. }
  221. // Then
  222. $this->assertEqualsWithDelta($expected_x, $x, 0.00001);
  223. // And as an extra check, solve the original matrix and compare the result.
  224. $solved_x = $A->solve($b);
  225. $this->assertEqualsWithDelta($x, $solved_x->getVector(), 0.00001);
  226. }
  227. /**
  228. * @test After solving, multiplying Ax = b
  229. * In Python you could do numpy.dot(A, x) == b for this verification
  230. * @dataProvider dataProviderForSolve
  231. * @param array $A
  232. * @param array $b
  233. * @throws \Exception
  234. */
  235. public function testAxEqualsBAfterSolving(array $A, array $b)
  236. {
  237. // Given
  238. $A = MatrixFactory::create($A);
  239. // And
  240. $x = $A->solve($b);
  241. // When Ax
  242. $Ax = $A->multiply($x);
  243. // Then Ax = b
  244. $this->assertEqualsWithDelta($b, $Ax->asVectors()[0]->getVector(), 0.00001);
  245. }
  246. /**
  247. * @test Issue 413 solving a singular matrix with RREF - Solve with RREF
  248. *
  249. * For the matrix
  250. * [1, 0, 0, 0, 0, 0]
  251. * [0, 1, 0, 0, 1, 0]
  252. * [0, 0, 1, 0, 0, 1]
  253. * [0, 0, 0, 0, 0, 0]
  254. * [0, 0, -180.92, 0, 0, -854.14]
  255. * [0, 180.92, 0, 0, 854.14, 0]
  256. * The RREF ends up being
  257. * [1, 0, 0, 0, 0, 0, 1.457]
  258. * [0, 1, 0, 0, 0, 0, -1.2294375984077]
  259. * [0, 0, 1, 0, 0, 0, -4.7787483437806]
  260. * [0, 0, 0, 0, 1, 0, -0.22756240159235]
  261. * [0, 0, 0, 0, 0, 1, -0.88525165621936]
  262. * [0, 0, 0, 0, 0, 0, 0]
  263. *
  264. * If we solve by just taking the augmented column on the right, the values are in the wrong order.
  265. * This is because the ones are not on the diagonal because the zero row at the bottom.
  266. * Expect x to be [1.4577, -1.230246179417, -4.778633012290, 0, -0.22745382058, -0.88526698770]
  267. * But intead ends up being [1.4577, -1.230246179417, -4.778633012290, -0.22745382058, -0.88526698770, 0]
  268. *
  269. * The fix checks if the matrix is singular, and if so, rearranges the x vector using the ones to order the values.
  270. */
  271. public function testSingularMatrixIssue413WhenSpecifyingSolveByRref()
  272. {
  273. // Given
  274. $data = [
  275. [1, 0, 0, 0, 0, 0],
  276. [0, 1, 0, 0, 1, 0],
  277. [0, 0, 1, 0, 0, 1],
  278. [0, 0, 0, 0, 0, 0],
  279. [0, 0, -180.92, 0, 0, -854.14],
  280. [0, 180.92, 0, 0, 854.14, 0],
  281. ];
  282. // And
  283. $A = MatrixFactory::create($data);
  284. $b = [1.457, -1.457, -5.664, 0, 1620.7, -416.8];
  285. // When
  286. $x = $A->solve($b, NumericMatrix::RREF);
  287. // Then x has expected values
  288. $expected = [1.4577, -1.230246179417, -4.778633012290, 0, -0.22745382058, -0.88526698770];
  289. $this->assertEqualsWithDelta($expected, $x->getVector(), 0.001);
  290. // And when Ax
  291. $Ax = $A->multiply($x);
  292. // Then Ax = b
  293. $this->assertEqualsWithDelta($b, $Ax->getColumn(0), 0.00001);
  294. }
  295. /**
  296. * @test Issue 413 solving a singular matrix with RREF - Solve without specifying method
  297. * @link https://github.com/markrogoyski/math-php/issues/413
  298. *
  299. * For the matrix
  300. * [1, 0, 0, 0, 0, 0]
  301. * [0, 1, 0, 0, 1, 0]
  302. * [0, 0, 1, 0, 0, 1]
  303. * [0, 0, 0, 0, 0, 0]
  304. * [0, 0, -180.92, 0, 0, -854.14]
  305. * [0, 180.92, 0, 0, 854.14, 0]
  306. * The RREF ends up being
  307. * [1, 0, 0, 0, 0, 0, 1.457]
  308. * [0, 1, 0, 0, 0, 0, -1.2294375984077]
  309. * [0, 0, 1, 0, 0, 0, -4.7787483437806]
  310. * [0, 0, 0, 0, 1, 0, -0.22756240159235]
  311. * [0, 0, 0, 0, 0, 1, -0.88525165621936]
  312. * [0, 0, 0, 0, 0, 0, 0]
  313. *
  314. * If we solve by just taking the augmented column on the right, the values are in the wrong order.
  315. * This is because the ones are not on the diagonal because the zero row at the bottom.
  316. * Expect x to be [1.4577, -1.230246179417, -4.778633012290, 0, -0.22745382058, -0.88526698770]
  317. * But instead ends up being [1.4577, -1.230246179417, -4.778633012290, -0.22745382058, -0.88526698770, 0]
  318. *
  319. * The fix checks if the matrix is singular, and if so, rearranges the x vector using the ones to order the values.
  320. */
  321. public function testSingularMatrixIssue413WhenSpecifyingSolveWithoutSpecifyingMethod()
  322. {
  323. // Given
  324. $data = [
  325. [1, 0, 0, 0, 0, 0],
  326. [0, 1, 0, 0, 1, 0],
  327. [0, 0, 1, 0, 0, 1],
  328. [0, 0, 0, 0, 0, 0],
  329. [0, 0, -180.92, 0, 0, -854.14],
  330. [0, 180.92, 0, 0, 854.14, 0],
  331. ];
  332. // And
  333. $A = MatrixFactory::create($data);
  334. $b = [1.457, -1.457, -5.664, 0, 1620.7, -416.8];
  335. // When
  336. $x = $A->solve($b);
  337. // Then x has expected values
  338. $expected = [1.4577, -1.230246179417, -4.778633012290, 0, -0.22745382058, -0.88526698770];
  339. $this->assertEqualsWithDelta($expected, $x->getVector(), 0.001);
  340. // And when Ax
  341. $Ax = $A->multiply($x);
  342. // Then Ax = b
  343. $this->assertEqualsWithDelta($b, $Ax->getColumn(0), 0.00001);
  344. }
  345. }