Browse Source

Added Claculateing Determinat for 2by2 and 3by3 Matrices

featureMatrixCalculator
Lukas Reichwein 5 years ago
parent
commit
f5c91caa85
  1. 83
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java
  2. 45
      src/test/java/com/ugsbo/matrixcalc/MatrixCalcDeterminatTest.java

83
src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java

@ -30,8 +30,7 @@ public class MatrixCalcMath {
} }
return result; return result;
} else { } else {
return null;
// throw new IllegalArgumentException("Matricies must be linked");
throw new IllegalArgumentException("Matricies must be linked");
} }
} }
@ -74,10 +73,7 @@ public class MatrixCalcMath {
} }
return result; return result;
} else { } else {
return null;
// TODO Fragen wie man eine Exception testen kann.
// throw new IllegalArgumentException("Matricies need to have the same
// Dimensions");
throw new IllegalArgumentException("Matricies need to have the same Dimensions");
} }
} }
@ -90,6 +86,12 @@ public class MatrixCalcMath {
* @return true if the Dimensions of Matrix A equals the Dimensions Matrix B * @return true if the Dimensions of Matrix A equals the Dimensions Matrix B
*/ */
public boolean checkIfMatriciesAreTheSameDimension(double[][] matrixA, double[][] matrixB) { public boolean checkIfMatriciesAreTheSameDimension(double[][] matrixA, double[][] matrixB) {
if (matrixA == null || matrixA.length == 0) {
return false;
}
if (matrixA[0] == null) {
return false;
}
if (matrixA.length == matrixB.length && matrixA[0].length == matrixB[0].length) { if (matrixA.length == matrixB.length && matrixA[0].length == matrixB[0].length) {
return true; return true;
} else { } else {
@ -116,10 +118,7 @@ public class MatrixCalcMath {
} }
return result; return result;
} else { } else {
return null;
// TODO Fragen wie man eine Exception testen kann.
// throw new IllegalArgumentException("Matricies need to have the same
// Dimensions");
throw new IllegalArgumentException("Matricies need to have the same Dimensions");
} }
} }
@ -131,8 +130,13 @@ public class MatrixCalcMath {
*/ */
public double[][] matrixTransponation(double[][] matrixA) { public double[][] matrixTransponation(double[][] matrixA) {
if (matrixA == null) { if (matrixA == null) {
// TODO hier auch die exception.
return null;
throw new IllegalArgumentException("Matricies need to have the same Dimensions");
}
if (matrixA.length == 0){
throw new IllegalArgumentException("Matricies need to have the same Dimensions");
}
if(matrixA[0] == null){
throw new IllegalArgumentException("Matricies need to have the same Dimensions");
} }
int columCountResult = matrixA.length; int columCountResult = matrixA.length;
int rowCountResult = matrixA[0].length; int rowCountResult = matrixA[0].length;
@ -144,4 +148,59 @@ public class MatrixCalcMath {
} }
return result; return result;
} }
/**
* Calculates the Determinant of a Matrix.
*
* @param matrixA The Inputmatrix
* @return The Determinant of the Matrix A
*/
public double calcDeterminat(double[][] matrixA) {
// checking if a Determinant can be calculated.
double result = 0.0;
if (checkIfMatrixIsQuadradtic(matrixA)) {
if (getMatrixRowCount(matrixA) == 2) {
result = calc2by2Determinant(matrixA);
} else if (getMatrixRowCount(matrixA) == 3) {
result = calc3by3Determinant(matrixA);
} else {
throw new IllegalArgumentException("Matrix is not 2 by 2 or 3 by 3");
}
} else {
throw new IllegalArgumentException("Matrix must be Quadratic");
}
return result;
}
private double calc3by3Determinant(double[][] matrixA) {
double result = matrixA[0][0] * matrixA[1][1] * matrixA[2][2] + matrixA[0][1] * matrixA[1][2] * matrixA[2][0]
+ matrixA[0][2] * matrixA[1][0] * matrixA[2][1] - matrixA[0][0] * matrixA[1][2] * matrixA[2][1]
- matrixA[0][1] * matrixA[1][0] * matrixA[2][2] - matrixA[0][2] * matrixA[1][1] * matrixA[2][0];
return result;
}
private double calc2by2Determinant(double[][] matrixA) {
double result = matrixA[0][0] * matrixA[1][1] - matrixA[0][1] * matrixA[1][0];
return result;
}
private int getMatrixRowCount(double[][] matrixA) {
return matrixA.length;
}
private boolean checkIfMatrixIsQuadradtic(double[][] matrixA) {
if (matrixA == null) {
return false;
}
if (matrixA[0] == null) {
return false;
}
if (matrixA.length == matrixA[0].length) {
return true;
}
return false;
}
} }

45
src/test/java/com/ugsbo/matrixcalc/MatrixCalcDeterminatTest.java

@ -0,0 +1,45 @@
package com.ugsbo.matrixcalc;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* Tests the funktionality to Calculate the Determinant of a Matrix.
*/
public class MatrixCalcDeterminatTest {
@Test
public void CalculatesTheDeterminanteOfA2by2Matrix() {
MatrixCalcMath math = new MatrixCalcMath();
// A(2,2)
double[][] matrixA = { { 1.0, 2.0 }, { 3.0, 4.0 } };
double determinat = -2.0;
double result = math.calcDeterminat(matrixA);
assertEquals("The Determinant is not as it should be", determinat, result, 0.01);
}
@Test
public void CalculatesTheDeterminanteOfA3by3Matrix() {
MatrixCalcMath math = new MatrixCalcMath();
// A(3,3)
double[][] matrixA = { { 1.0, 2.0, 1.0 }, { 3.0, 4.0, 0.0 }, {5.0, 6.0, 0.0} };
double determinat = -2.0;
double result = math.calcDeterminat(matrixA);
assertEquals("The Determinant is not as it should be", determinat, result, 0.01);
}
@Test(expected = IllegalArgumentException.class)
public void tryToCalculateA4by4MatrixSouldResulInIllegalArgumentException() {
MatrixCalcMath math = new MatrixCalcMath();
// A(4,4)
double[][] matrixA = { { 1.0, 2.0, 1.0, 0.0 }, { 3.0, 4.0, 0.0, 0.0 }, {5.0, 6.0, 0.0, 0.0}, {5.0, 6.0, 0.0, 0.0} };
math.calcDeterminat(matrixA);
}
}
Loading…
Cancel
Save