QR.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace Matrix\Decomposition;
  3. use Matrix\Exception;
  4. use Matrix\Matrix;
  5. class QR
  6. {
  7. private $qrMatrix;
  8. private $rows;
  9. private $columns;
  10. private $rDiagonal = [];
  11. public function __construct(Matrix $matrix)
  12. {
  13. $this->qrMatrix = $matrix->toArray();
  14. $this->rows = $matrix->rows;
  15. $this->columns = $matrix->columns;
  16. $this->decompose();
  17. }
  18. public function getHouseholdVectors(): Matrix
  19. {
  20. $householdVectors = [];
  21. for ($row = 0; $row < $this->rows; ++$row) {
  22. for ($column = 0; $column < $this->columns; ++$column) {
  23. if ($row >= $column) {
  24. $householdVectors[$row][$column] = $this->qrMatrix[$row][$column];
  25. } else {
  26. $householdVectors[$row][$column] = 0.0;
  27. }
  28. }
  29. }
  30. return new Matrix($householdVectors);
  31. }
  32. public function getQ(): Matrix
  33. {
  34. $qGrid = [];
  35. $rowCount = $this->rows;
  36. for ($k = $this->columns - 1; $k >= 0; --$k) {
  37. for ($i = 0; $i < $this->rows; ++$i) {
  38. $qGrid[$i][$k] = 0.0;
  39. }
  40. $qGrid[$k][$k] = 1.0;
  41. if ($this->columns > $this->rows) {
  42. $qGrid = array_slice($qGrid, 0, $this->rows);
  43. }
  44. for ($j = $k; $j < $this->columns; ++$j) {
  45. if (isset($this->qrMatrix[$k], $this->qrMatrix[$k][$k]) && $this->qrMatrix[$k][$k] != 0.0) {
  46. $s = 0.0;
  47. for ($i = $k; $i < $this->rows; ++$i) {
  48. $s += $this->qrMatrix[$i][$k] * $qGrid[$i][$j];
  49. }
  50. $s = -$s / $this->qrMatrix[$k][$k];
  51. for ($i = $k; $i < $this->rows; ++$i) {
  52. $qGrid[$i][$j] += $s * $this->qrMatrix[$i][$k];
  53. }
  54. }
  55. }
  56. }
  57. array_walk(
  58. $qGrid,
  59. function (&$row) use ($rowCount) {
  60. $row = array_reverse($row);
  61. $row = array_slice($row, 0, $rowCount);
  62. }
  63. );
  64. return new Matrix($qGrid);
  65. }
  66. public function getR(): Matrix
  67. {
  68. $rGrid = [];
  69. for ($row = 0; $row < $this->columns; ++$row) {
  70. for ($column = 0; $column < $this->columns; ++$column) {
  71. if ($row < $column) {
  72. $rGrid[$row][$column] = $this->qrMatrix[$row][$column] ?? 0.0;
  73. } elseif ($row === $column) {
  74. $rGrid[$row][$column] = $this->rDiagonal[$row] ?? 0.0;
  75. } else {
  76. $rGrid[$row][$column] = 0.0;
  77. }
  78. }
  79. }
  80. if ($this->columns > $this->rows) {
  81. $rGrid = array_slice($rGrid, 0, $this->rows);
  82. }
  83. return new Matrix($rGrid);
  84. }
  85. private function hypo($a, $b): float
  86. {
  87. if (abs($a) > abs($b)) {
  88. $r = $b / $a;
  89. $r = abs($a) * sqrt(1 + $r * $r);
  90. } elseif ($b != 0.0) {
  91. $r = $a / $b;
  92. $r = abs($b) * sqrt(1 + $r * $r);
  93. } else {
  94. $r = 0.0;
  95. }
  96. return $r;
  97. }
  98. /**
  99. * QR Decomposition computed by Householder reflections.
  100. */
  101. private function decompose(): void
  102. {
  103. for ($k = 0; $k < $this->columns; ++$k) {
  104. // Compute 2-norm of k-th column without under/overflow.
  105. $norm = 0.0;
  106. for ($i = $k; $i < $this->rows; ++$i) {
  107. $norm = $this->hypo($norm, $this->qrMatrix[$i][$k]);
  108. }
  109. if ($norm != 0.0) {
  110. // Form k-th Householder vector.
  111. if ($this->qrMatrix[$k][$k] < 0.0) {
  112. $norm = -$norm;
  113. }
  114. for ($i = $k; $i < $this->rows; ++$i) {
  115. $this->qrMatrix[$i][$k] /= $norm;
  116. }
  117. $this->qrMatrix[$k][$k] += 1.0;
  118. // Apply transformation to remaining columns.
  119. for ($j = $k + 1; $j < $this->columns; ++$j) {
  120. $s = 0.0;
  121. for ($i = $k; $i < $this->rows; ++$i) {
  122. $s += $this->qrMatrix[$i][$k] * $this->qrMatrix[$i][$j];
  123. }
  124. $s = -$s / $this->qrMatrix[$k][$k];
  125. for ($i = $k; $i < $this->rows; ++$i) {
  126. $this->qrMatrix[$i][$j] += $s * $this->qrMatrix[$i][$k];
  127. }
  128. }
  129. }
  130. $this->rDiagonal[$k] = -$norm;
  131. }
  132. }
  133. public function isFullRank(): bool
  134. {
  135. for ($j = 0; $j < $this->columns; ++$j) {
  136. if ($this->rDiagonal[$j] == 0.0) {
  137. return false;
  138. }
  139. }
  140. return true;
  141. }
  142. /**
  143. * Least squares solution of A*X = B.
  144. *
  145. * @param Matrix $B a Matrix with as many rows as A and any number of columns
  146. *
  147. * @throws Exception
  148. *
  149. * @return Matrix matrix that minimizes the two norm of Q*R*X-B
  150. */
  151. public function solve(Matrix $B): Matrix
  152. {
  153. if ($B->rows !== $this->rows) {
  154. throw new Exception('Matrix row dimensions are not equal');
  155. }
  156. if (!$this->isFullRank()) {
  157. throw new Exception('Can only perform this operation on a full-rank matrix');
  158. }
  159. // Compute Y = transpose(Q)*B
  160. $Y = $this->getQ()->transpose()
  161. ->multiply($B);
  162. // Solve R*X = Y;
  163. return $this->getR()->inverse()
  164. ->multiply($Y);
  165. }
  166. }