public MatrixNxM GetInverse() { if (Rows != Columns) { throw new MatrixDimensionExceptionNonSquare(this); } double det = this.GetDeterminant(); MatrixNxM inv = new MatrixNxM(Rows, Columns); for (int x = 0; x < Columns; x++) { for (int y = 0; y < Rows; y++) { MatrixNxM sub = this.GetSubmatrix(x, y); int f1 = (x % 2 == 0) ? 1 : -1; int f2 = (y % 2 == 0) ? 1 : -1; inv[y, x] = f1 * f2 * sub.GetDeterminant() / det; } } return(inv); }
public double GetDeterminant() { if (Rows != Columns) { throw new MatrixDimensionExceptionNonSquare(this); } if (Rows == 0 && Columns == 0) { return(0); } if (Rows == 1 && Columns == 1) { return(this[0, 0]); } if (Rows == 2 && Columns == 2) { return(this[0, 0] * this[1, 1] - this[0, 1] * this[1, 0]); } double sum = 0; for (int i = 0; i < Columns; i++) { if (this[0, i] == 0) { continue; } MatrixNxM sub = this.GetSubmatrix(0, i); int factor = (i % 2 == 0) ? 1 : -1; sum += factor * this[0, i] * sub.GetDeterminant(); } return(sum); }