LUDecomposition.php 7.6 KB

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