LUDecomposition.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * @package JAMA
  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. * @version 1.1
  19. * @license PHP v3.0
  20. */
  21. class PHPExcel_Shared_JAMA_LUDecomposition {
  22. const MatrixSingularException = "Can only perform operation on singular matrix.";
  23. const MatrixSquareException = "Mismatched Row dimension";
  24. /**
  25. * Decomposition storage
  26. * @var array
  27. */
  28. private $LU = array();
  29. /**
  30. * Row dimension.
  31. * @var int
  32. */
  33. private $m;
  34. /**
  35. * Column dimension.
  36. * @var int
  37. */
  38. private $n;
  39. /**
  40. * Pivot sign.
  41. * @var int
  42. */
  43. private $pivsign;
  44. /**
  45. * Internal storage of pivot vector.
  46. * @var array
  47. */
  48. private $piv = array();
  49. /**
  50. * LU Decomposition constructor.
  51. *
  52. * @param $A Rectangular matrix
  53. * @return Structure to access L, U and piv.
  54. */
  55. public function __construct($A) {
  56. if ($A instanceof PHPExcel_Shared_JAMA_Matrix) {
  57. // Use a "left-looking", dot-product, Crout/Doolittle algorithm.
  58. $this->LU = $A->getArray();
  59. $this->m = $A->getRowDimension();
  60. $this->n = $A->getColumnDimension();
  61. for ($i = 0; $i < $this->m; ++$i) {
  62. $this->piv[$i] = $i;
  63. }
  64. $this->pivsign = 1;
  65. $LUrowi = $LUcolj = array();
  66. // Outer loop.
  67. for ($j = 0; $j < $this->n; ++$j) {
  68. // Make a copy of the j-th column to localize references.
  69. for ($i = 0; $i < $this->m; ++$i) {
  70. $LUcolj[$i] = &$this->LU[$i][$j];
  71. }
  72. // Apply previous transformations.
  73. for ($i = 0; $i < $this->m; ++$i) {
  74. $LUrowi = $this->LU[$i];
  75. // Most of the time is spent in the following dot product.
  76. $kmax = min($i,$j);
  77. $s = 0.0;
  78. for ($k = 0; $k < $kmax; ++$k) {
  79. $s += $LUrowi[$k] * $LUcolj[$k];
  80. }
  81. $LUrowi[$j] = $LUcolj[$i] -= $s;
  82. }
  83. // Find pivot and exchange if necessary.
  84. $p = $j;
  85. for ($i = $j+1; $i < $this->m; ++$i) {
  86. if (abs($LUcolj[$i]) > abs($LUcolj[$p])) {
  87. $p = $i;
  88. }
  89. }
  90. if ($p != $j) {
  91. for ($k = 0; $k < $this->n; ++$k) {
  92. $t = $this->LU[$p][$k];
  93. $this->LU[$p][$k] = $this->LU[$j][$k];
  94. $this->LU[$j][$k] = $t;
  95. }
  96. $k = $this->piv[$p];
  97. $this->piv[$p] = $this->piv[$j];
  98. $this->piv[$j] = $k;
  99. $this->pivsign = $this->pivsign * -1;
  100. }
  101. // Compute multipliers.
  102. if (($j < $this->m) && ($this->LU[$j][$j] != 0.0)) {
  103. for ($i = $j+1; $i < $this->m; ++$i) {
  104. $this->LU[$i][$j] /= $this->LU[$j][$j];
  105. }
  106. }
  107. }
  108. } else {
  109. throw new PHPExcel_Calculation_Exception(PHPExcel_Shared_JAMA_Matrix::ArgumentTypeException);
  110. }
  111. } // function __construct()
  112. /**
  113. * Get lower triangular factor.
  114. *
  115. * @return array Lower triangular factor
  116. */
  117. public function getL() {
  118. for ($i = 0; $i < $this->m; ++$i) {
  119. for ($j = 0; $j < $this->n; ++$j) {
  120. if ($i > $j) {
  121. $L[$i][$j] = $this->LU[$i][$j];
  122. } elseif ($i == $j) {
  123. $L[$i][$j] = 1.0;
  124. } else {
  125. $L[$i][$j] = 0.0;
  126. }
  127. }
  128. }
  129. return new PHPExcel_Shared_JAMA_Matrix($L);
  130. } // function getL()
  131. /**
  132. * Get upper triangular factor.
  133. *
  134. * @return array Upper triangular factor
  135. */
  136. public function getU() {
  137. for ($i = 0; $i < $this->n; ++$i) {
  138. for ($j = 0; $j < $this->n; ++$j) {
  139. if ($i <= $j) {
  140. $U[$i][$j] = $this->LU[$i][$j];
  141. } else {
  142. $U[$i][$j] = 0.0;
  143. }
  144. }
  145. }
  146. return new PHPExcel_Shared_JAMA_Matrix($U);
  147. } // function getU()
  148. /**
  149. * Return pivot permutation vector.
  150. *
  151. * @return array Pivot vector
  152. */
  153. public function getPivot() {
  154. return $this->piv;
  155. } // function getPivot()
  156. /**
  157. * Alias for getPivot
  158. *
  159. * @see getPivot
  160. */
  161. public function getDoublePivot() {
  162. return $this->getPivot();
  163. } // function getDoublePivot()
  164. /**
  165. * Is the matrix nonsingular?
  166. *
  167. * @return true if U, and hence A, is nonsingular.
  168. */
  169. public function isNonsingular() {
  170. for ($j = 0; $j < $this->n; ++$j) {
  171. if ($this->LU[$j][$j] == 0) {
  172. return false;
  173. }
  174. }
  175. return true;
  176. } // function isNonsingular()
  177. /**
  178. * Count determinants
  179. *
  180. * @return array d matrix deterninat
  181. */
  182. public function det() {
  183. if ($this->m == $this->n) {
  184. $d = $this->pivsign;
  185. for ($j = 0; $j < $this->n; ++$j) {
  186. $d *= $this->LU[$j][$j];
  187. }
  188. return $d;
  189. } else {
  190. throw new PHPExcel_Calculation_Exception(PHPExcel_Shared_JAMA_Matrix::MatrixDimensionException);
  191. }
  192. } // function det()
  193. /**
  194. * Solve A*X = B
  195. *
  196. * @param $B A Matrix with as many rows as A and any number of columns.
  197. * @return X so that L*U*X = B(piv,:)
  198. * @PHPExcel_Calculation_Exception IllegalArgumentException Matrix row dimensions must agree.
  199. * @PHPExcel_Calculation_Exception RuntimeException Matrix is singular.
  200. */
  201. public function solve($B) {
  202. if ($B->getRowDimension() == $this->m) {
  203. if ($this->isNonsingular()) {
  204. // Copy right hand side with pivoting
  205. $nx = $B->getColumnDimension();
  206. $X = $B->getMatrix($this->piv, 0, $nx-1);
  207. // Solve L*Y = B(piv,:)
  208. for ($k = 0; $k < $this->n; ++$k) {
  209. for ($i = $k+1; $i < $this->n; ++$i) {
  210. for ($j = 0; $j < $nx; ++$j) {
  211. $X->A[$i][$j] -= $X->A[$k][$j] * $this->LU[$i][$k];
  212. }
  213. }
  214. }
  215. // Solve U*X = Y;
  216. for ($k = $this->n-1; $k >= 0; --$k) {
  217. for ($j = 0; $j < $nx; ++$j) {
  218. $X->A[$k][$j] /= $this->LU[$k][$k];
  219. }
  220. for ($i = 0; $i < $k; ++$i) {
  221. for ($j = 0; $j < $nx; ++$j) {
  222. $X->A[$i][$j] -= $X->A[$k][$j] * $this->LU[$i][$k];
  223. }
  224. }
  225. }
  226. return $X;
  227. } else {
  228. throw new PHPExcel_Calculation_Exception(self::MatrixSingularException);
  229. }
  230. } else {
  231. throw new PHPExcel_Calculation_Exception(self::MatrixSquareException);
  232. }
  233. } // function solve()
  234. } // class PHPExcel_Shared_JAMA_LUDecomposition