Added cofactor function

This commit is contained in:
2022-02-03 23:10:52 +00:00
parent 125dd365a2
commit 442f69d4ec

View File

@@ -656,6 +656,32 @@ public class IntegerMatrix{
//Return the determinant
return det;
}
public IntegerMatrix cofactor(){
//Make sure the matrix is square
if(!isSquare()){
throw new InvalidGeometryException("A matrix must be square to find the cofactor matrix");
}
//Create a new grid
int[][] newGrid = new int[getNumRows()][getNumCols()];
//If the grid is 1x1 return the grid
if(getNumRows() == 1){
newGrid[0][0] = grid[0][0];
}
//Use the formula to find the cofactor matrix
else{
for(int row = 0;row < getNumRows();++row){
int multiplier = ((row % 2) == 0) ? 1 : -1;
for(int col = 0;col < getNumCols();++col){
newGrid[row][col] = multiplier * laplaceExpansionHelper(row, col).determinant();
multiplier = -multiplier;
}
}
}
//Return the new matrix
return new IntegerMatrix(newGrid);
}
//Object funtions
@Override