inverse method

Matrix inverse()

Calculates the matrix inverse.

Throws MatrixNoInverseException if there is no inverse of this matrix. Throws MatrixInvalidDimensions if the matrix was not square.

Implementation

Matrix inverse() {
  double d = det();
  if (_isClose(d, 0.0)) {
    throw new MatrixNoInverseException();
  }

  if (m == 2) {
    Matrix i = Matrix.fill(m, n);
    i[0][0] = this[1][1];
    i[1][1] = this[0][0];
    i[0][1] = -1.0 * this[0][1];
    i[1][0] = -1.0 * this[1][0];
    return i * (1 / d);
  } else {
    Matrix i = coFactors();
    i = i.transpose();
    return i * (1 / d);
  }
}