CholeskyDecomposition.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Shared\JAMA;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
  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. *
  17. * @version 1.2
  18. */
  19. class CholeskyDecomposition
  20. {
  21. /**
  22. * Decomposition storage.
  23. *
  24. * @var array
  25. */
  26. private $L = [];
  27. /**
  28. * Matrix row and column dimension.
  29. *
  30. * @var int
  31. */
  32. private $m;
  33. /**
  34. * Symmetric positive definite flag.
  35. *
  36. * @var bool
  37. */
  38. private $isspd = true;
  39. /**
  40. * CholeskyDecomposition.
  41. *
  42. * Class constructor - decomposes symmetric positive definite matrix
  43. *
  44. * @param Matrix $A Matrix square symmetric positive definite matrix
  45. */
  46. public function __construct(Matrix $A)
  47. {
  48. $this->L = $A->getArray();
  49. $this->m = $A->getRowDimension();
  50. for ($i = 0; $i < $this->m; ++$i) {
  51. for ($j = $i; $j < $this->m; ++$j) {
  52. for ($sum = $this->L[$i][$j], $k = $i - 1; $k >= 0; --$k) {
  53. $sum -= $this->L[$i][$k] * $this->L[$j][$k];
  54. }
  55. if ($i == $j) {
  56. if ($sum >= 0) {
  57. $this->L[$i][$i] = sqrt($sum);
  58. } else {
  59. $this->isspd = false;
  60. }
  61. } else {
  62. if ($this->L[$i][$i] != 0) {
  63. $this->L[$j][$i] = $sum / $this->L[$i][$i];
  64. }
  65. }
  66. }
  67. for ($k = $i + 1; $k < $this->m; ++$k) {
  68. $this->L[$i][$k] = 0.0;
  69. }
  70. }
  71. }
  72. /**
  73. * Is the matrix symmetric and positive definite?
  74. *
  75. * @return bool
  76. */
  77. public function isSPD()
  78. {
  79. return $this->isspd;
  80. }
  81. /**
  82. * getL.
  83. *
  84. * Return triangular factor.
  85. *
  86. * @return Matrix Lower triangular matrix
  87. */
  88. public function getL()
  89. {
  90. return new Matrix($this->L);
  91. }
  92. /**
  93. * Solve A*X = B.
  94. *
  95. * @param $B Row-equal matrix
  96. *
  97. * @return Matrix L * L' * X = B
  98. */
  99. public function solve(Matrix $B)
  100. {
  101. if ($B->getRowDimension() == $this->m) {
  102. if ($this->isspd) {
  103. $X = $B->getArrayCopy();
  104. $nx = $B->getColumnDimension();
  105. for ($k = 0; $k < $this->m; ++$k) {
  106. for ($i = $k + 1; $i < $this->m; ++$i) {
  107. for ($j = 0; $j < $nx; ++$j) {
  108. $X[$i][$j] -= $X[$k][$j] * $this->L[$i][$k];
  109. }
  110. }
  111. for ($j = 0; $j < $nx; ++$j) {
  112. $X[$k][$j] /= $this->L[$k][$k];
  113. }
  114. }
  115. for ($k = $this->m - 1; $k >= 0; --$k) {
  116. for ($j = 0; $j < $nx; ++$j) {
  117. $X[$k][$j] /= $this->L[$k][$k];
  118. }
  119. for ($i = 0; $i < $k; ++$i) {
  120. for ($j = 0; $j < $nx; ++$j) {
  121. $X[$i][$j] -= $X[$k][$j] * $this->L[$k][$i];
  122. }
  123. }
  124. }
  125. return new Matrix($X, $this->m, $nx);
  126. }
  127. throw new CalculationException(Matrix::MATRIX_SPD_EXCEPTION);
  128. }
  129. throw new CalculationException(Matrix::MATRIX_DIMENSION_EXCEPTION);
  130. }
  131. }