/** * Constructor: Generate LU decomposition of the matrix. * * @param matrix matrix to get the lu decomposition of * @return vector permutation */ public function __construct(&$matrix) { new Assertion($matrix instanceof Matrix, 'Given matrix not of class Matrix.'); new Assertion($matrix->isSquare(), 'Matrix is not quadratic.'); $this->_permutation = new Vector($matrix->rows()); $this->_matrix = $matrix->copy(); for ($j = 0; $j < $this->_matrix->rows(); $j++) { $pivot = $j; for ($i = $j + 1; $i < $this->_matrix->rows(); $i++) { if (abs($this->_matrix->get($i, $j)) > abs($this->_matrix->get($pivot, $j))) { $pivot = $i; } } $this->_permutation->set($j, $pivot); $this->_matrix->swapRows($j, $pivot); for ($i = $j + 1; $i < $this->_matrix->columns(); $i++) { $this->_matrix->set($i, $j, $this->_matrix->get($i, $j) / $this->_matrix->get($j, $j)); for ($k = $j + 1; $k < $this->_matrix->columns(); $k++) { $this->_matrix->set($i, $k, $this->_matrix->get($i, $k) - $this->_matrix->get($i, $j) * $this->_matrix->get($j, $k)); } } } }