CholeskyDecomposition.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @package JAMA
  4. *
  5. * Cholesky decomposition class
  6. *
  7. * For a symmetric, positive definite matrix A, the Cholesky decomposition
  8. * is an lower triangular matrix L so that A = L*L'.
  9. *
  10. * If the matrix is not symmetric or positive definite, the constructor
  11. * returns a partial decomposition and sets an internal flag that may
  12. * be queried by the isSPD() method.
  13. *
  14. * @author Paul Meagher
  15. * @author Michael Bommarito
  16. * @version 1.2
  17. */
  18. class CholeskyDecomposition {
  19. /**
  20. * Decomposition storage
  21. * @var array
  22. * @access private
  23. */
  24. private $L = array();
  25. /**
  26. * Matrix row and column dimension
  27. * @var int
  28. * @access private
  29. */
  30. private $m;
  31. /**
  32. * Symmetric positive definite flag
  33. * @var boolean
  34. * @access private
  35. */
  36. private $isspd = true;
  37. /**
  38. * CholeskyDecomposition
  39. *
  40. * Class constructor - decomposes symmetric positive definite matrix
  41. * @param mixed Matrix square symmetric positive definite matrix
  42. */
  43. public function __construct($A = null) {
  44. if ($A instanceof Matrix) {
  45. $this->L = $A->getArray();
  46. $this->m = $A->getRowDimension();
  47. for($i = 0; $i < $this->m; ++$i) {
  48. for($j = $i; $j < $this->m; ++$j) {
  49. for($sum = $this->L[$i][$j], $k = $i - 1; $k >= 0; --$k) {
  50. $sum -= $this->L[$i][$k] * $this->L[$j][$k];
  51. }
  52. if ($i == $j) {
  53. if ($sum >= 0) {
  54. $this->L[$i][$i] = sqrt($sum);
  55. } else {
  56. $this->isspd = false;
  57. }
  58. } else {
  59. if ($this->L[$i][$i] != 0) {
  60. $this->L[$j][$i] = $sum / $this->L[$i][$i];
  61. }
  62. }
  63. }
  64. for ($k = $i+1; $k < $this->m; ++$k) {
  65. $this->L[$i][$k] = 0.0;
  66. }
  67. }
  68. } else {
  69. throw new PHPExcel_Calculation_Exception(JAMAError(ArgumentTypeException));
  70. }
  71. } // function __construct()
  72. /**
  73. * Is the matrix symmetric and positive definite?
  74. *
  75. * @return boolean
  76. */
  77. public function isSPD() {
  78. return $this->isspd;
  79. } // function isSPD()
  80. /**
  81. * getL
  82. *
  83. * Return triangular factor.
  84. * @return Matrix Lower triangular matrix
  85. */
  86. public function getL() {
  87. return new Matrix($this->L);
  88. } // function getL()
  89. /**
  90. * Solve A*X = B
  91. *
  92. * @param $B Row-equal matrix
  93. * @return Matrix L * L' * X = B
  94. */
  95. public function solve($B = null) {
  96. if ($B instanceof Matrix) {
  97. if ($B->getRowDimension() == $this->m) {
  98. if ($this->isspd) {
  99. $X = $B->getArrayCopy();
  100. $nx = $B->getColumnDimension();
  101. for ($k = 0; $k < $this->m; ++$k) {
  102. for ($i = $k + 1; $i < $this->m; ++$i) {
  103. for ($j = 0; $j < $nx; ++$j) {
  104. $X[$i][$j] -= $X[$k][$j] * $this->L[$i][$k];
  105. }
  106. }
  107. for ($j = 0; $j < $nx; ++$j) {
  108. $X[$k][$j] /= $this->L[$k][$k];
  109. }
  110. }
  111. for ($k = $this->m - 1; $k >= 0; --$k) {
  112. for ($j = 0; $j < $nx; ++$j) {
  113. $X[$k][$j] /= $this->L[$k][$k];
  114. }
  115. for ($i = 0; $i < $k; ++$i) {
  116. for ($j = 0; $j < $nx; ++$j) {
  117. $X[$i][$j] -= $X[$k][$j] * $this->L[$k][$i];
  118. }
  119. }
  120. }
  121. return new Matrix($X, $this->m, $nx);
  122. } else {
  123. throw new PHPExcel_Calculation_Exception(JAMAError(MatrixSPDException));
  124. }
  125. } else {
  126. throw new PHPExcel_Calculation_Exception(JAMAError(MatrixDimensionException));
  127. }
  128. } else {
  129. throw new PHPExcel_Calculation_Exception(JAMAError(ArgumentTypeException));
  130. }
  131. } // function solve()
  132. } // class CholeskyDecomposition