public static NMatrix Minors(NMatrix matrix) { NMatrix minors = new NMatrix(new decimal[matrix.Rows, matrix.Columns]); for (int i = 0; i < matrix.Rows; i++) { for (int j = 0; j < matrix.Columns; j++) { if (matrix.Rows - 1 > 0 && matrix.Columns - 1 > 0) { NMatrix minorCalc = new NMatrix(new decimal[matrix.Rows - 1, matrix.Columns - 1]); int x0 = 0; for (int x = 0; x < matrix.Rows; x++) { if (x == i) { x++; } if (x == matrix.Rows) { break; } int y0 = 0; for (int y = 0; y < matrix.Columns; y++) { if (y == j) { y++; } if (y == matrix.Columns) { break; } minorCalc.Data[x0, y0] = matrix.Data[x, y]; y0++; } x0++; } minors.Data[i, j] = NMatrix.Determinant(minorCalc); } else if (matrix.Rows == 1 && matrix.Columns == 1) { minors.Data[i, j] = matrix.Data[i, j]; } else { throw new ArgumentException("A minor can only be calculated for a square matrix."); } } } return(minors); }
public decimal Determinant() { return(NMatrix.Determinant(this)); }