public Matrix GetCofactor() { // https://www.mathwords.com/c/cofactor_matrix.htm decimal[,] cofactor = new decimal[Height, Width]; for (int rowIndex = 0; rowIndex < Height; rowIndex++) { for (int columnIndex = 0; columnIndex < Width; columnIndex++) { cofactor[rowIndex, columnIndex] = new Cofactor( this, rowIndex, columnIndex).GetDeterminant(); } } return(new Matrix(cofactor)); }
private static decimal GetDeterminant( Matrix matrix) { // you can only get a determinant from a square matrix // using expansion by cofactors // https://www.mathwords.com/e/expansion_by_cofactors.htm // look for 2x2 matrix if (matrix.Height == 2) { return(GetDeterminant( matrix[0, 0], matrix[1, 0], matrix[0, 1], matrix[1, 1])); } else { decimal determinant = 0; for (int columnIndex = 0; columnIndex < matrix.Width; columnIndex++) { Cofactor cofactor = new Cofactor( matrix, 0, columnIndex); determinant += matrix[0, columnIndex] * cofactor.GetDeterminant(); } return(determinant); } }