/** * Solve A*X = B. * * @param * b * A Matrix with as many rows as A and any number of columns. * @return X so that L*L'*X = b. */ public function solve(Matrix $b) { if ($b->getRows() != $this->n) { throw new MatrixError("Matrix row dimensions must agree."); } if (!$this->isspd) { throw new RuntimeException("Matrix is not symmetric positive definite."); } // Copy right hand side. $x = $b->getArrayCopy(); $nx = $b->getCols(); // Solve L*Y = B; for ($k = 0; $k < $this->n; ++$k) { for ($j = 0; $j < $nx; ++$j) { for ($i = 0; $i < $k; ++$i) { $x[$k][$j] -= $x[$i][$j] * $this->l[$k][$i]; } $x[$k][$j] /= $this->l[$k][$k]; } } // Solve L'*X = Y; for ($k = $this->n - 1; $k >= 0; $k--) { for ($j = 0; $j < $nx; ++$j) { for ($i = $k + 1; $i < $this->n; ++$i) { $x[$k][$j] -= $x[$i][$j] * $this->l[$i][$k]; } $x[$k][$j] /= $this->l[$k][$k]; } } return Matrix::matrixFromDoubles($x); }