coFactors method

Matrix coFactors()

Calculates the Cofactors of the matrix.

Throws MatrixInvalidDimensions if matrix is not square and at least a 3x3

Implementation

Matrix coFactors() {
  if (this.m != this.n) {
    throw MatrixInvalidDimensions();
  }

  if (this.m < 3) {
    throw MatrixInvalidDimensions();
  }

  Matrix cf = Matrix.fill(m, n, 0.0);

  for (int r = 0; r < this.m; r++) {
    for (int c = 0; c < this.n; c++) {
      Matrix sub = cut(r, c);
      double v = pow(-1, r + c) * sub.det();
      cf[r][c] = v;
    }
  }

  return cf;
}