CroutTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace MathPHP\Tests\LinearAlgebra\Decomposition;
  3. use MathPHP\LinearAlgebra\MatrixFactory;
  4. use MathPHP\Exception;
  5. class CroutTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test croutDecomposition returns the expected array of L and U factorized matrices
  9. * @dataProvider dataProviderForCroutDecomposition
  10. * @param array $A
  11. * @param array $expected
  12. * @throws \Exception
  13. */
  14. public function testCroutDecomposition(array $A, array $expected)
  15. {
  16. // Given
  17. $A = MatrixFactory::create($A);
  18. $L = MatrixFactory::create($expected['L']);
  19. $U = MatrixFactory::create($expected['U']);
  20. // When
  21. $lu = $A->croutDecomposition();
  22. // Then
  23. $this->assertEqualsWithDelta($L->getMatrix(), $lu->L->getMatrix(), 0.00001);
  24. $this->assertEqualsWithDelta($U->getMatrix(), $lu->U->getMatrix(), 0.00001);
  25. }
  26. /**
  27. * @return array
  28. */
  29. public function dataProviderForCroutDecomposition(): array
  30. {
  31. return [
  32. [
  33. [
  34. [4, 0, 1],
  35. [2, 1, 0],
  36. [2, 2, 3],
  37. ],
  38. [
  39. 'L' => [
  40. [4, 0, 0],
  41. [2, 1, 0],
  42. [2, 2, 7 / 2],
  43. ],
  44. 'U' => [
  45. [1, 0, 1 / 4],
  46. [0, 1, -1 / 2],
  47. [0, 0, 1],
  48. ],
  49. ],
  50. ],
  51. [
  52. [
  53. [5, 4, 1],
  54. [10, 9, 4],
  55. [10, 13, 15],
  56. ],
  57. [
  58. 'L' => [
  59. [5, 0, 0],
  60. [10, 1, 0],
  61. [10, 5, 3],
  62. ],
  63. 'U' => [
  64. [1, 4 / 5, 1 / 5],
  65. [0, 1, 2],
  66. [0, 0, 1],
  67. ],
  68. ],
  69. ],
  70. [
  71. [
  72. [2, -4, 1],
  73. [6, 2, -1],
  74. [-2, 6, -2],
  75. ],
  76. [
  77. 'L' => [
  78. [2, 0, 0],
  79. [6, 14, 0],
  80. [-2, 2, -0.428571],
  81. ],
  82. 'U' => [
  83. [1, -2, 0.5],
  84. [0, 1, -0.285714],
  85. [0, 0, 1],
  86. ],
  87. ],
  88. ],
  89. [
  90. [
  91. [1, 2, 3],
  92. [2, 20, 26],
  93. [3, 26, 70],
  94. ],
  95. [
  96. 'L' => [
  97. [1, 0, 0],
  98. [2, 16, 0],
  99. [3, 20, 36],
  100. ],
  101. 'U' => [
  102. [1, 2, 3],
  103. [0, 1, 5 / 4],
  104. [0, 0, 1],
  105. ],
  106. ],
  107. ],
  108. [
  109. [
  110. [2, -1, 3],
  111. [1, 3, -1],
  112. [2, -2, 5],
  113. ],
  114. [
  115. 'L' => [
  116. [2, 0, 0],
  117. [1, 7 / 2, 0],
  118. [2, -1, 9 / 7],
  119. ],
  120. 'U' => [
  121. [1, -1 / 2, 3 / 2],
  122. [0, 1, -5 / 7],
  123. [0, 0, 1],
  124. ],
  125. ],
  126. ],
  127. ];
  128. }
  129. /**
  130. * @test croutDecomposition throws a MatrixException if the det(L) is close to zero
  131. * @throws \Exception
  132. */
  133. public function testCroutDecompositionException()
  134. {
  135. // Given
  136. $A = MatrixFactory::create([
  137. [3, 4],
  138. [6, 8],
  139. ]);
  140. // Then
  141. $this->expectException(Exception\MatrixException::class);
  142. // When
  143. $lu = $A->croutDecomposition();
  144. }
  145. /**
  146. * @test Crout Decomposition invalid property
  147. * @throws \Exception
  148. */
  149. public function testCountDecompositionInvalidProperty()
  150. {
  151. // Given
  152. $A = MatrixFactory::create([
  153. [4, 1, -1],
  154. [1, 2, 1],
  155. [-1, 1, 2],
  156. ]);
  157. $crout = $A->croutDecomposition();
  158. // Then
  159. $this->expectException(Exception\MathException::class);
  160. // When
  161. $doesNotExist = $crout->doesNotExist;
  162. }
  163. }