Skip to content

Instantly share code, notes, and snippets.

@rossGardiner
Created March 10, 2015 19:48
Show Gist options
  • Save rossGardiner/8b02d99c9955dfda4ec5 to your computer and use it in GitHub Desktop.
Save rossGardiner/8b02d99c9955dfda4ec5 to your computer and use it in GitHub Desktop.
public class ThreeByThreeMatrix extends SquareMatrix
{
private static final double[][] IDENTITY_MATRIX = {{1,0,0},{0,1,0},{0,0,1}};
/**
* constructor for class ThreeByThreeMatrix
*/
public ThreeByThreeMatrix()
{
super(3);
}
public void inverseMatrix()
{
}
/**
* returns determinant of the matrix
* @return
*/
public double determinant()
{
//creating and initialising sub-matrices
TwoByTwoMatrix efhi = new TwoByTwoMatrix();
efhi.setAMatrixValue(0, 0, this.getAMatrixValue(1, 1));
efhi.setAMatrixValue(0, 1, this.getAMatrixValue(1, 2));
efhi.setAMatrixValue(1, 0, this.getAMatrixValue(2, 1));
efhi.setAMatrixValue(1, 1, this.getAMatrixValue(2, 2));
TwoByTwoMatrix dfgi = new TwoByTwoMatrix();
dfgi.setAMatrixValue(0, 0, this.getAMatrixValue(1, 0));
dfgi.setAMatrixValue(0, 1, this.getAMatrixValue(1, 2));
dfgi.setAMatrixValue(1, 0, this.getAMatrixValue(2, 0));
dfgi.setAMatrixValue(1, 1, this.getAMatrixValue(2, 2));
TwoByTwoMatrix degh = new TwoByTwoMatrix();
degh.setAMatrixValue(0, 0, this.getAMatrixValue(1, 0));
degh.setAMatrixValue(0, 1, this.getAMatrixValue(1, 1));
degh.setAMatrixValue(1, 0, this.getAMatrixValue(2, 0));
degh.setAMatrixValue(1, 1, this.getAMatrixValue(2, 1));
// find determinant of each sub-matrix
double detEfhi = efhi.determinant();
double detDfgi = dfgi.determinant();
double detDegh = degh.determinant();
//use formula to find determinant of matrix
return (this.getAMatrixValue(0, 0) * detEfhi) - (this.getAMatrixValue(0, 1) * detDfgi) + (this.getAMatrixValue(0, 2) * detDegh);
}
public double[][] findInverse()
{
double[][] inverseMatrix = new double[3][3];
if (this.doesInverseExist() == false)
{
System.out.println("there is no inverse for this system of equations(the matrix is singular)");
}
if (this.doesInverseExist() == true)
{
inverseMatrix = this.multiplyMatrix((1 / this.determinant()), this.adjoint());
return inverseMatrix;
}
return null;
}
public boolean doesInverseExist()
{
if (this.determinant() == 0)
{
return false;
}
if (!(this.determinant() == 0))
{
return true;
}
return false;
}
public double[][] adjoint()
{
int[][] flipMat = {{1, -1, 1}, {-1, 1, -1}, {1, -1, 1}};
double[][] trans = new double[3][3];
double[][] ans = new double[3][3];
//find transpose of the matrix
trans = this.matrixTranspose(this.getMatrix());
//creating sub-matrices and initializing
TwoByTwoMatrix efhi = new TwoByTwoMatrix();
efhi.setAMatrixValue(0, 0, trans[1][1]);
efhi.setAMatrixValue(0, 1, trans[1][2]);
efhi.setAMatrixValue(1, 0, trans[2][1]);
efhi.setAMatrixValue(1, 1, trans[2][2]);
TwoByTwoMatrix dfgi = new TwoByTwoMatrix();
dfgi.setAMatrixValue(0, 0, trans[1][0]);
dfgi.setAMatrixValue(0, 1, trans[1][2]);
dfgi.setAMatrixValue(1, 0, trans[2][0]);
dfgi.setAMatrixValue(1, 1, trans[2][2]);
TwoByTwoMatrix degh = new TwoByTwoMatrix();
degh.setAMatrixValue(0, 0, trans[1][0]);
degh.setAMatrixValue(0, 1, trans[1][1]);
degh.setAMatrixValue(1, 0, trans[2][0]);
degh.setAMatrixValue(1, 1, trans[2][1]);
TwoByTwoMatrix bchi = new TwoByTwoMatrix();
bchi.setAMatrixValue(0, 0, trans[0][1]);
bchi.setAMatrixValue(0, 1, trans[0][2]);
bchi.setAMatrixValue(1, 0, trans[2][1]);
bchi.setAMatrixValue(1, 1, trans[2][2]);
TwoByTwoMatrix acgi = new TwoByTwoMatrix();
acgi.setAMatrixValue(0, 0, trans[0][0]);
acgi.setAMatrixValue(0, 1, trans[0][2]);
acgi.setAMatrixValue(1, 0, trans[2][0]);
acgi.setAMatrixValue(1, 1, trans[2][2]);
TwoByTwoMatrix abgh = new TwoByTwoMatrix();
abgh.setAMatrixValue(0, 0, trans[0][0]);
abgh.setAMatrixValue(0, 1, trans[0][1]);
abgh.setAMatrixValue(1, 0, trans[2][0]);
abgh.setAMatrixValue(1, 1, trans[2][1]);
TwoByTwoMatrix bcef = new TwoByTwoMatrix();
bcef.setAMatrixValue(0, 0, trans[0][1]);
bcef.setAMatrixValue(0, 1, trans[0][2]);
bcef.setAMatrixValue(1, 0, trans[1][1]);
bcef.setAMatrixValue(1, 1, trans[1][2]);
TwoByTwoMatrix acdf = new TwoByTwoMatrix();
acdf.setAMatrixValue(0, 0, trans[0][0]);
acdf.setAMatrixValue(0, 1, trans[0][2]);
acdf.setAMatrixValue(1, 0, trans[1][0]);
acdf.setAMatrixValue(1, 1, trans[1][2]);
TwoByTwoMatrix abde = new TwoByTwoMatrix();
abde.setAMatrixValue(0, 0, trans[0][0]);
abde.setAMatrixValue(0, 1, trans[0][1]);
abde.setAMatrixValue(1, 0, trans[1][0]);
abde.setAMatrixValue(1, 1, trans[1][1]);
//creating matrix of determinants
double[][] detMat = {{efhi.determinant(), dfgi.determinant(), degh.determinant()},
{bchi.determinant(), acgi.determinant(), abgh.determinant()},
{bcef.determinant(), acdf.determinant(), abde.determinant()}};
//finding adjugate
for (int i = 0; i < this.getRows(); i++)
{
for (int j = 0; j < this.getColumns(); j++)
{
ans[i][j] = detMat[i][j] * flipMat[i][j];
}
}
return ans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment