/** * Compute inverse using determinants method * We are expecting a non singular, square matrix (complete, n=m, n>1) * * @param \Chippyash\Matrix\Matrix $mA * @return Matrix */ public function invert(NumericMatrix $mA) { $rows = $mA->rows(); $cols = $mA->columns(); $work = array(); $fDet = new Det(); $fCof = new Cofactor(); for ($row = 0; $row < $rows; $row++) { for ($col = 0; $col < $cols; $col++) { $t = $fDet($fCof($mA, [$row + 1, $col + 1])); if (fmod($row + $col, 2) == 0) { $work[$row][$col] = $t; } else { $work[$row][$col] = $t->negate(); } $r = $row + 1; $c = $col + 1; } } $fTr = new Transpose(); $fDiv = new Scalar(); return $fTr($fDiv($this->createCorrectMatrixType($mA, $work), $fDet($mA))); }
/** * * @param \Chippyash\Math\Matrix\NumericMatrix $mA * @param \Chippyash\Math\Matrix\NumericMatrix $mB * @throws ComputationException */ protected function checkRectangleMatrixCompatibility(NumericMatrix $mA, NumericMatrix $mB) { if ($mB->is('rectangle') && $mA->columns() != $mB->rows()) { throw new ComputationException('Two matrices cannot be multiplied: mA->columns != mB->rows'); } }
/** * Set lower triangular factor. */ protected function setLowerProduct(NumericMatrix $mA) { $m = $this->LU->rows(); $n = $this->LU->columns(); $LU = $this->LU->toArray(); $rcFactor = $this->rows - $this->cols; //set Lower $this->set('L', function () use($m, $n, $LU, $rcFactor, $mA) { $L = []; for ($i = 0; $i < $m; $i++) { for ($j = 0; $j < $n; $j++) { if ($i > $j) { $L[$i][$j] = $LU[$i][$j]; } elseif ($i == $j) { $L[$i][$j] = $this->createCorrectScalarType($mA, 1); } else { $L[$i][$j] = $this->createCorrectScalarType($mA, 0); } } } //remove extra cols for non square matrices if ($rcFactor < 0) { $mLL = new NumericMatrix($L); return $this->createCorrectMatrixType($mA, $mLL('Colslice', array(1, $mLL->columns() + $rcFactor))->toArray()); } else { return $this->createCorrectMatrixType($mA, $L); } }); }