Skip to content

Instantly share code, notes, and snippets.

@rossGardiner
Created March 10, 2015 19:47
Show Gist options
  • Save rossGardiner/75dd1f4cd55281fc487e to your computer and use it in GitHub Desktop.
Save rossGardiner/75dd1f4cd55281fc487e to your computer and use it in GitHub Desktop.
public class TwoByTwoMatrix extends SquareMatrix
{
private static final double[][] IDENTITY_MATRIX = {{1,0}, {0,1}};
/**
* constructor for the class TwoByTwoMatrix
*/
public TwoByTwoMatrix()
{
super(2);
}
/**
* will return the inverse of the matrix
* if the inverse exists
* @return
*/
public double[][] findInverse()
{
if (this.doesInverseExist() == false)
{
System.out.println("there is no inverse for this system of equations(the matrix is singular)");
}
if (this.doesInverseExist() == true)
{
double[][] inverseMatrix = new double[2][2];
inverseMatrix = this.multiplyMatrix((1 / this.determinant()), this.adjoint());
return inverseMatrix;
}
return null;
}
/**
* returns true if an inverse of the matrix exists
* returns false if an inverse of the matrix does not exist
* @return
*/
public boolean doesInverseExist()
{
if (this.determinant() == 0)
{
return false;
}
if (!(this.determinant() == 0))
{
return true;
}
return false;
}
/**
* returns true if the matrix is orthogonal
* returns false if the matrix is not orthogonal
* @param trans
* @param mat
* @return
*/
public boolean isOrthogonal(double[][] trans, double[][] mat)
{
trans = this.matrixTranspose(mat);
if (this.multiplyMatrices(trans, mat) == this.IDENTITY_MATRIX)
return true;
if (this.multiplyMatrices(trans, mat)!= this.IDENTITY_MATRIX)
return false;
return false;
}
/**
* returns the determinant of the matrix
* @return
*/
public double determinant()
{
double determinant;
determinant = (this.getAMatrixValue(0, 0) * this.getAMatrixValue(1, 1)) - (this.getAMatrixValue(0, 1) * this.getAMatrixValue(1, 0));
return determinant;
}
/**
* returns the adjoint of the matrix
* @return
*/
public double[][] adjoint()
{
double[][] adj = new double[2][2];
adj[0][0] = this.getAMatrixValue(1, 1);
adj[0][1] = -(this.getAMatrixValue(0, 1));
adj[1][0] = -(this.getAMatrixValue(1, 0));
adj[1][1] = this.getAMatrixValue(0, 0);
return adj;
}
/**
* multiplies a matrix by a factor
* overloads multiplyMatrix(double)
* in Matrix class
* @param factor
* @param mat
* @return
*/
public double[][] multiplyMatrix(double factor, double[][] mat)
{
for(int i = 0; i < 2; i++)
{
for (int j =0; j < 2; j++)
mat[i][j] = (mat[i][j] * factor);
}
return mat;
}
/**
* multiplies together
* two TwoByTwo matrices
* @param mat1
* @param mat2
* @return
*/
public double[][] multiplyMatrices(double[][] mat1, double[][] mat2)
{
double[][] retMat = new double[2][2];
retMat[0][0] = ((mat1[0][0] * mat2[0][0]) + (mat1[0][1] * mat2[1][0]));
retMat[0][1] = ((mat1[0][0] * mat2[0][1]) + (mat1[0][1] * mat2[1][1]));
retMat[1][0] = ((mat1[1][0] * mat2[0][0]) + (mat1[1][1] * mat2[1][0]));
retMat[1][1] = ((mat1[1][0] * mat2[0][1]) + (mat1[1][1] * mat2[1][1]));
return retMat;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment