public function cofactorMatrix() : SquareMatrix
{
if (!$this->isSquare()) {
throw new Exception\MatrixException('Matrix is not square; cannot get cofactor Matrix of a non-square matrix');
}
$m = $this->m;
$n = $this->n;
$R = [];
for ($i = 0; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) {
$R[$i][$j] = $this->cofactor($i, $j);
}
}
return MatrixFactory::create($R);
}