LUDecomposition.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Shared\JAMA;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
  4. /**
  5. * For an m-by-n matrix A with m >= n, the LU decomposition is an m-by-n
  6. * unit lower triangular matrix L, an n-by-n upper triangular matrix U,
  7. * and a permutation vector piv of length m so that A(piv,:) = L*U.
  8. * If m < n, then L is m-by-m and U is m-by-n.
  9. *
  10. * The LU decompostion with pivoting always exists, even if the matrix is
  11. * singular, so the constructor will never fail. The primary use of the
  12. * LU decomposition is in the solution of square systems of simultaneous
  13. * linear equations. This will fail if isNonsingular() returns false.
  14. *
  15. * @author Paul Meagher
  16. * @author Bartosz Matosiuk
  17. * @author Michael Bommarito
  18. *
  19. * @version 1.1
  20. */
  21. class LUDecomposition
  22. {
  23. const MATRIX_SINGULAR_EXCEPTION = 'Can only perform operation on singular matrix.';
  24. const MATRIX_SQUARE_EXCEPTION = 'Mismatched Row dimension';
  25. /**
  26. * Decomposition storage.
  27. *
  28. * @var array
  29. */
  30. private $LU = [];
  31. /**
  32. * Row dimension.
  33. *
  34. * @var int
  35. */
  36. private $m;
  37. /**
  38. * Column dimension.
  39. *
  40. * @var int
  41. */
  42. private $n;
  43. /**
  44. * Pivot sign.
  45. *
  46. * @var int
  47. */
  48. private $pivsign;
  49. /**
  50. * Internal storage of pivot vector.
  51. *
  52. * @var array
  53. */
  54. private $piv = [];
  55. /**
  56. * LU Decomposition constructor.
  57. *
  58. * @param Matrix $A Rectangular matrix
  59. */
  60. public function __construct($A)
  61. {
  62. if ($A instanceof Matrix) {
  63. // Use a "left-looking", dot-product, Crout/Doolittle algorithm.
  64. $this->LU = $A->getArray();
  65. $this->m = $A->getRowDimension();
  66. $this->n = $A->getColumnDimension();
  67. for ($i = 0; $i < $this->m; ++$i) {
  68. $this->piv[$i] = $i;
  69. }
  70. $this->pivsign = 1;
  71. $LUrowi = $LUcolj = [];
  72. // Outer loop.
  73. for ($j = 0; $j < $this->n; ++$j) {
  74. // Make a copy of the j-th column to localize references.
  75. for ($i = 0; $i < $this->m; ++$i) {
  76. $LUcolj[$i] = &$this->LU[$i][$j];
  77. }
  78. // Apply previous transformations.
  79. for ($i = 0; $i < $this->m; ++$i) {
  80. $LUrowi = $this->LU[$i];
  81. // Most of the time is spent in the following dot product.
  82. $kmax = min($i, $j);
  83. $s = 0.0;
  84. for ($k = 0; $k < $kmax; ++$k) {
  85. $s += $LUrowi[$k] * $LUcolj[$k];
  86. }
  87. $LUrowi[$j] = $LUcolj[$i] -= $s;
  88. }
  89. // Find pivot and exchange if necessary.
  90. $p = $j;
  91. for ($i = $j + 1; $i < $this->m; ++$i) {
  92. if (abs($LUcolj[$i]) > abs($LUcolj[$p])) {
  93. $p = $i;
  94. }
  95. }
  96. if ($p != $j) {
  97. for ($k = 0; $k < $this->n; ++$k) {
  98. $t = $this->LU[$p][$k];
  99. $this->LU[$p][$k] = $this->LU[$j][$k];
  100. $this->LU[$j][$k] = $t;
  101. }
  102. $k = $this->piv[$p];
  103. $this->piv[$p] = $this->piv[$j];
  104. $this->piv[$j] = $k;
  105. $this->pivsign = $this->pivsign * -1;
  106. }
  107. // Compute multipliers.
  108. if (($j < $this->m) && ($this->LU[$j][$j] != 0.0)) {
  109. for ($i = $j + 1; $i < $this->m; ++$i) {
  110. $this->LU[$i][$j] /= $this->LU[$j][$j];
  111. }
  112. }
  113. }
  114. } else {
  115. throw new CalculationException(Matrix::ARGUMENT_TYPE_EXCEPTION);
  116. }
  117. }
  118. // function __construct()
  119. /**
  120. * Get lower triangular factor.
  121. *
  122. * @return Matrix Lower triangular factor
  123. */
  124. public function getL()
  125. {
  126. for ($i = 0; $i < $this->m; ++$i) {
  127. for ($j = 0; $j < $this->n; ++$j) {
  128. if ($i > $j) {
  129. $L[$i][$j] = $this->LU[$i][$j];
  130. } elseif ($i == $j) {
  131. $L[$i][$j] = 1.0;
  132. } else {
  133. $L[$i][$j] = 0.0;
  134. }
  135. }
  136. }
  137. return new Matrix($L);
  138. }
  139. // function getL()
  140. /**
  141. * Get upper triangular factor.
  142. *
  143. * @return Matrix Upper triangular factor
  144. */
  145. public function getU()
  146. {
  147. for ($i = 0; $i < $this->n; ++$i) {
  148. for ($j = 0; $j < $this->n; ++$j) {
  149. if ($i <= $j) {
  150. $U[$i][$j] = $this->LU[$i][$j];
  151. } else {
  152. $U[$i][$j] = 0.0;
  153. }
  154. }
  155. }
  156. return new Matrix($U);
  157. }
  158. // function getU()
  159. /**
  160. * Return pivot permutation vector.
  161. *
  162. * @return array Pivot vector
  163. */
  164. public function getPivot()
  165. {
  166. return $this->piv;
  167. }
  168. // function getPivot()
  169. /**
  170. * Alias for getPivot.
  171. *
  172. * @see getPivot
  173. */
  174. public function getDoublePivot()
  175. {
  176. return $this->getPivot();
  177. }
  178. // function getDoublePivot()
  179. /**
  180. * Is the matrix nonsingular?
  181. *
  182. * @return bool true if U, and hence A, is nonsingular
  183. */
  184. public function isNonsingular()
  185. {
  186. for ($j = 0; $j < $this->n; ++$j) {
  187. if ($this->LU[$j][$j] == 0) {
  188. return false;
  189. }
  190. }
  191. return true;
  192. }
  193. // function isNonsingular()
  194. /**
  195. * Count determinants.
  196. *
  197. * @return array d matrix deterninat
  198. */
  199. public function det()
  200. {
  201. if ($this->m == $this->n) {
  202. $d = $this->pivsign;
  203. for ($j = 0; $j < $this->n; ++$j) {
  204. $d *= $this->LU[$j][$j];
  205. }
  206. return $d;
  207. }
  208. throw new CalculationException(Matrix::MATRIX_DIMENSION_EXCEPTION);
  209. }
  210. // function det()
  211. /**
  212. * Solve A*X = B.
  213. *
  214. * @param mixed $B a Matrix with as many rows as A and any number of columns
  215. *
  216. * @throws CalculationException illegalArgumentException Matrix row dimensions must agree
  217. * @throws CalculationException runtimeException Matrix is singular
  218. *
  219. * @return Matrix X so that L*U*X = B(piv,:)
  220. */
  221. public function solve($B)
  222. {
  223. if ($B->getRowDimension() == $this->m) {
  224. if ($this->isNonsingular()) {
  225. // Copy right hand side with pivoting
  226. $nx = $B->getColumnDimension();
  227. $X = $B->getMatrix($this->piv, 0, $nx - 1);
  228. // Solve L*Y = B(piv,:)
  229. for ($k = 0; $k < $this->n; ++$k) {
  230. for ($i = $k + 1; $i < $this->n; ++$i) {
  231. for ($j = 0; $j < $nx; ++$j) {
  232. $X->A[$i][$j] -= $X->A[$k][$j] * $this->LU[$i][$k];
  233. }
  234. }
  235. }
  236. // Solve U*X = Y;
  237. for ($k = $this->n - 1; $k >= 0; --$k) {
  238. for ($j = 0; $j < $nx; ++$j) {
  239. $X->A[$k][$j] /= $this->LU[$k][$k];
  240. }
  241. for ($i = 0; $i < $k; ++$i) {
  242. for ($j = 0; $j < $nx; ++$j) {
  243. $X->A[$i][$j] -= $X->A[$k][$j] * $this->LU[$i][$k];
  244. }
  245. }
  246. }
  247. return $X;
  248. }
  249. throw new CalculationException(self::MATRIX_SINGULAR_EXCEPTION);
  250. }
  251. throw new CalculationException(self::MATRIX_SQUARE_EXCEPTION);
  252. }
  253. }