/** * @param Matrix $source * @throws MatrixException */ private function decompose(Matrix $source) { $decompositionLiteral = $source->toArray(); if (!$source->isSquare()) { throw new MatrixException('Operation can only be called on square matrix: ' . print_r($decompositionLiteral, true)); } $size = $source->getRowCount(); for ($k = 0; $k < $size; $k++) { for ($i = $k + 1; $i < $size; $i++) { $decompositionLiteral[$i][$k] = $decompositionLiteral[$i][$k] / $decompositionLiteral[$k][$k]; } for ($i = $k + 1; $i < $size; $i++) { for ($j = $k + 1; $j < $size; $j++) { $decompositionLiteral[$i][$j] = $decompositionLiteral[$i][$j] - $decompositionLiteral[$i][$k] * $decompositionLiteral[$k][$j]; } } } $this->decomposition = new Matrix($decompositionLiteral); }
/** * @param Matrix $source * @throws MatrixException */ private function decompose(Matrix $source) { $sourceLiteral = $source->toArray(); $decompositionLiteral = $sourceLiteral; if (!$source->isSquare()) { throw new MatrixException('Operation can only be called on square matrix: ' . print_r($decompositionLiteral, true)); } $size = $source->getRowCount(); $this->permutation = range(0, $size - 1); for ($k = 0; $k < $size; $k++) { $p = 0.0; $kPrime = $k; for ($i = $k; $i < $size; $i++) { $absolute = abs($decompositionLiteral[$i][$k]); if ($absolute > $p) { $p = $absolute; $kPrime = $i; } } if ($p === 0.0) { throw new MatrixException('Cannot take the LUP decomposition of a singular matrix: ' . print_r($sourceLiteral, true)); } if ($k !== $kPrime) { list($this->permutation[$k], $this->permutation[$kPrime]) = [$this->permutation[$kPrime], $this->permutation[$k]]; $this->parity++; } for ($i = 0; $i < $size; $i++) { list($decompositionLiteral[$k][$i], $decompositionLiteral[$kPrime][$i]) = [$decompositionLiteral[$kPrime][$i], $decompositionLiteral[$k][$i]]; } for ($i = $k + 1; $i < $size; $i++) { $decompositionLiteral[$i][$k] = $decompositionLiteral[$i][$k] / $decompositionLiteral[$k][$k]; for ($j = $k + 1; $j < $size; $j++) { $decompositionLiteral[$i][$j] = $decompositionLiteral[$i][$j] - $decompositionLiteral[$i][$k] * $decompositionLiteral[$k][$j]; } } } $this->decomposition = new Matrix($decompositionLiteral); }
/** * @param self $other * @return self * @throws MatrixException */ public function concatenateRight(self $other) : self { return $this->spliceColumns($this->getColumnCount(), 0, $other->toArray()); }
/** * @param Matrix $matrix * @return boolean */ public function equals(Matrix $matrix) : bool { return $this->toArray() === $matrix->toArray(); }