18 $this->qrMatrix = $matrix->
toArray();
19 $this->rows = $matrix->
rows;
20 $this->columns = $matrix->
columns;
27 $householdVectors = [];
30 if (
$row >= $column) {
31 $householdVectors[
$row][$column] = $this->qrMatrix[
$row][$column];
33 $householdVectors[
$row][$column] = 0.0;
38 return new Matrix($householdVectors);
46 for ($k = $this->columns - 1; $k >= 0; --$k) {
51 if ($this->columns > $this->rows) {
52 $qGrid = array_slice($qGrid, 0, $this->rows);
56 if (isset($this->qrMatrix[$k], $this->qrMatrix[$k][$k]) && $this->qrMatrix[$k][$k] != 0.0) {
59 $s += $this->qrMatrix[
$i][$k] * $qGrid[
$i][$j];
61 $s = -
$s / $this->qrMatrix[$k][$k];
63 $qGrid[
$i][$j] +=
$s * $this->qrMatrix[
$i][$k];
71 function (&
$row) use ($rowCount) {
73 $row = array_slice(
$row, 0, $rowCount);
87 $rGrid[
$row][$column] = $this->qrMatrix[
$row][$column] ?? 0.0;
88 } elseif (
$row === $column) {
89 $rGrid[
$row][$column] = $this->rDiagonal[
$row] ?? 0.0;
91 $rGrid[
$row][$column] = 0.0;
96 if ($this->columns > $this->rows) {
97 $rGrid = array_slice($rGrid, 0, $this->rows);
100 return new Matrix($rGrid);
103 private function hypo($a, $b): float
105 if (abs($a) > abs($b)) {
107 $r = abs($a) * sqrt(1 +
$r *
$r);
108 } elseif ($b != 0.0) {
110 $r = abs($b) * sqrt(1 +
$r *
$r);
127 $norm = $this->
hypo($norm, $this->qrMatrix[
$i][$k]);
131 if ($this->qrMatrix[$k][$k] < 0.0) {
135 $this->qrMatrix[
$i][$k] /= $norm;
137 $this->qrMatrix[$k][$k] += 1.0;
142 $s += $this->qrMatrix[
$i][$k] * $this->qrMatrix[
$i][$j];
144 $s = -
$s / $this->qrMatrix[$k][$k];
146 $this->qrMatrix[
$i][$j] +=
$s * $this->qrMatrix[
$i][$k];
150 $this->rDiagonal[$k] = -$norm;
157 if ($this->rDiagonal[$j] == 0.0) {
176 if ($B->
rows !== $this->rows) {
177 throw new Exception(
'Matrix row dimensions are not equal');
181 throw new Exception(
'Can only perform this operation on a full-rank matrix');
185 $Y = $this->
getQ()->transpose()
188 return $this->
getR()->inverse()
decompose()
QR Decomposition computed by Householder reflections.
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
solve(Matrix $B)
Least squares solution of A*X = B.
columns()
Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value...
toArray()
Return the matrix as a 2-dimensional array.
__construct(Matrix $matrix)
Class for the creating "special" Matrices.