Added function to calculate determinants
This commit is contained in:
@@ -58,6 +58,57 @@ public class IntegerMatrix{
|
|||||||
//Return the new grid
|
//Return the new grid
|
||||||
return newGrid;
|
return newGrid;
|
||||||
}
|
}
|
||||||
|
private boolean isSquare(){
|
||||||
|
if(getNumRows() == 0){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return getNumRows() == getNumCols();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Returns a matrix with the supplied row and column removed
|
||||||
|
private IntegerMatrix laplaceExpansionHelper(int row, int col){
|
||||||
|
//Make sure the matrix is square
|
||||||
|
if(!isSquare()){
|
||||||
|
throw new InvalidGeometryException("A matrix must be square for you to perform Laplace Expansion");
|
||||||
|
}
|
||||||
|
//Make sure the matrix is large enough to have this operation performed
|
||||||
|
if((getNumRows() <= 1) || (getNumCols() <= 1)){
|
||||||
|
throw new InvalidGeometryException("A matrix must be at least 2x2 for you to perform Laplace Expansion: " + getNumRows() + "x" + getNumCols());
|
||||||
|
}
|
||||||
|
//Make sure the row is valid
|
||||||
|
if((row < 0) || (row >= getNumRows())){
|
||||||
|
throw new InvalidCoordinatesException("An invalid row number was supplied: " + row + ". The matrix contains " + getNumRows() + " rows");
|
||||||
|
}
|
||||||
|
//Make sure the col is valid
|
||||||
|
if((col < 0) || (col >= getNumCols())){
|
||||||
|
throw new InvalidCoordinatesException("An invalid column number was supplied: " + col + ". The matrix contains " + getNumCols() + " columns");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Create a new matrix that is one row and column smaller than the existing row
|
||||||
|
int[][] newGrid = new int[getNumRows() - 1][getNumCols() - 1];
|
||||||
|
//Traverse the matrix and set the values in the new matrix, skipping elements in the given row and col
|
||||||
|
for(int workingRow = 0, newRow = 0;workingRow < getNumRows();++workingRow){
|
||||||
|
//Skip the row we are expanding
|
||||||
|
if(workingRow == row){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for(int workingCol = 0, newCol = 0;workingCol < getNumCols();++workingCol){
|
||||||
|
//Skip the column we are expanding
|
||||||
|
if(workingCol == col){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
newGrid[newRow][newCol] = grid[workingRow][workingCol];
|
||||||
|
++newCol;
|
||||||
|
}
|
||||||
|
++newRow;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Return the new matrix
|
||||||
|
return new IntegerMatrix(newGrid);
|
||||||
|
}
|
||||||
|
|
||||||
//Constructors
|
//Constructors
|
||||||
public IntegerMatrix(){
|
public IntegerMatrix(){
|
||||||
@@ -492,6 +543,103 @@ public class IntegerMatrix{
|
|||||||
//Return the new matrix
|
//Return the new matrix
|
||||||
return new IntegerMatrix(newGrid);
|
return new IntegerMatrix(newGrid);
|
||||||
}
|
}
|
||||||
|
public int det(){
|
||||||
|
return determinant();
|
||||||
|
}
|
||||||
|
public int determinant(){
|
||||||
|
//Make sure the matrix is square
|
||||||
|
if(!isSquare()){
|
||||||
|
throw new InvalidGeometryException("A matrix must be square for it to have a determinant");
|
||||||
|
}
|
||||||
|
//?Don't have to worry about a matrix existing. isSquare takes care of 0x0 matrix
|
||||||
|
|
||||||
|
//Determine the formula do use for the determinant
|
||||||
|
int det = 0;
|
||||||
|
switch(getNumRows()){
|
||||||
|
//If the matrix is 1x1 return the number
|
||||||
|
case 1 : det = grid[0][0]; break;
|
||||||
|
//If the matrix is 2x2 use the formula
|
||||||
|
case 2 : {
|
||||||
|
det = (grid[0][0] * grid[1][1]) - (grid[0][1] * grid[1][0]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
//If the matrix is 3x3 use the formula
|
||||||
|
case 3 : {
|
||||||
|
det = (grid[0][0] * grid[1][1] * grid[2][2]) + (grid[0][1] * grid[1][2] * grid[2][0]) + (grid[0][2] * grid[1][0] * grid[2][1]) -
|
||||||
|
(grid[2][0] * grid[1][1] * grid[0][2]) - (grid[2][1] * grid[1][2] * grid[0][0]) - (grid[2][2] * grid[1][0] * grid[0][1]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
//If the matrix is larger break it down and try again
|
||||||
|
default : {
|
||||||
|
//Find the row/column with the largest number of 0's
|
||||||
|
int zerosLocation = 0;
|
||||||
|
int maxNumZeros = 0;
|
||||||
|
boolean zerosRow = true;
|
||||||
|
//Check the rows
|
||||||
|
for(int row = 0;row < getNumRows();++row){
|
||||||
|
int numZeros = 0;
|
||||||
|
for(int col = 0;col < getNumCols();++col){
|
||||||
|
if(grid[row][col] == 0){
|
||||||
|
++numZeros;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(numZeros > maxNumZeros){
|
||||||
|
maxNumZeros = numZeros;
|
||||||
|
zerosLocation = row;
|
||||||
|
zerosRow = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Check the columns
|
||||||
|
for(int col = 0;col < getNumCols();++col){
|
||||||
|
int numZeros = 0;
|
||||||
|
for(int row = 0;row < getNumRows();++row){
|
||||||
|
if(grid[row][col] == 0){
|
||||||
|
++numZeros;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(numZeros > maxNumZeros){
|
||||||
|
maxNumZeros = numZeros;
|
||||||
|
zerosLocation = col;
|
||||||
|
zerosRow = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//If the largest number of zeros were found in a row
|
||||||
|
if(zerosRow){
|
||||||
|
//Set a variable to make sure the appropriate + or - is applied to the scalar
|
||||||
|
int multiplier = 1;
|
||||||
|
if((zerosLocation % 2) == 1){
|
||||||
|
multiplier = -1;
|
||||||
|
}
|
||||||
|
//Go through every column in the optimal row using the formula
|
||||||
|
for(int col = 0;col < getNumCols();++col){
|
||||||
|
if(grid[zerosLocation][col] != 0){
|
||||||
|
det += (multiplier * grid[zerosLocation][col] * laplaceExpansionHelper(zerosLocation, col).determinant());
|
||||||
|
}
|
||||||
|
multiplier = -multiplier;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//If the largest number of zeros were found in a column
|
||||||
|
else{
|
||||||
|
//Set a variable to make sure the appropriate + or - is applied to the scalar
|
||||||
|
int multiplier = 1;
|
||||||
|
if((zerosLocation % 2) == 1){
|
||||||
|
multiplier = -1;
|
||||||
|
}
|
||||||
|
//Go through every row in the coptimal column using the formula
|
||||||
|
for(int row = 0;row < getNumRows();++row){
|
||||||
|
if(grid[row][zerosLocation] != 0){
|
||||||
|
det += (multiplier * grid[row][zerosLocation] * laplaceExpansionHelper(row, zerosLocation).determinant());
|
||||||
|
}
|
||||||
|
multiplier = -multiplier;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Return the determinant
|
||||||
|
return det;
|
||||||
|
}
|
||||||
|
|
||||||
//Object funtions
|
//Object funtions
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user