diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java index 76f1c5c..6ca296d 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java +++ b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java @@ -31,7 +31,7 @@ public class MatrixCalcMath { return result; } else { return null; - //throw new IllegalArgumentException("Matricies must be linked"); + // throw new IllegalArgumentException("Matricies must be linked"); } } @@ -97,4 +97,29 @@ public class MatrixCalcMath { } } + + /** + * Substracts matrix A by the matrix B. Substaction for Matrices is just the + * substraction of each component with thier coorsponding component. + * + * @param matrixA The Inputmatrix A (right TextArea in the GUI) + * @param matrixB The Inputmatrix B (left TextArea in the GUI + * @return matrix A substracted by matrix B + */ + public double[][] matrixSubstraction(double[][] matrixA, double[][] matrixB) { + if (checkIfMatriciesAreTheSameDimension(matrixA, matrixB)) { + double[][] result = new double[matrixA.length][matrixA[0].length]; + for (int rows = 0; rows < matrixA.length; rows++) { + for (int colums = 0; colums < matrixA[0].length; colums++) { + result[rows][colums] = matrixA[rows][colums] - matrixB[rows][colums]; + } + } + return result; + } else { + return null; + // TODO Fragen wie man eine Exception testen kann. + // throw new IllegalArgumentException("Matricies need to have the same + // Dimensions"); + } + } } \ No newline at end of file diff --git a/src/test/java/com/ugsbo/matrixcalc/MatrixAdditionTest.java b/src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java similarity index 53% rename from src/test/java/com/ugsbo/matrixcalc/MatrixAdditionTest.java rename to src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java index 4ca53bb..44ebfa8 100644 --- a/src/test/java/com/ugsbo/matrixcalc/MatrixAdditionTest.java +++ b/src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java @@ -7,9 +7,9 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; /** - * Tests the funktionality to add two matricies + * Tests the funktionality to add and substract two matricies */ -public class MatrixAdditionTest { +public class MatrixAdditionAndSubstractionTest { @Test public void twoMatriciesHaveTheSameDimensions() { @@ -58,4 +58,43 @@ public class MatrixAdditionTest { assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); } + + @Test + public void substractTwoMatriciesWithSameContent() { + MatrixCalcMath math = new MatrixCalcMath(); + double[][] matrixA = { { 1.0, 1.0 }, { 1.0, 1.0 } }; + double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 } }; + double[][] matrixC = { { 0.0, 0.0 }, { 0.0, 0.0 } }; + + double[][] result = math.matrixSubstraction(matrixA, matrixB); + + assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); + assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + } + + @Test + public void substractTwoMatriciesWithDiffrentContent() { + MatrixCalcMath math = new MatrixCalcMath(); + double[][] matrixA = { { 1.0, 2.0 }, { 3.0, 4.0 } }; + double[][] matrixB = { { 5.0, 6.0 }, { 7.0, 8.0 } }; + double[][] matrixC = { { -4.0, -4.0 }, { -4.0, -4.0 } }; + + double[][] result = math.matrixSubstraction(matrixA, matrixB); + + assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); + assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + } + + @Test + public void substractTwoMatriciesWithNegativeContent() { + MatrixCalcMath math = new MatrixCalcMath(); + double[][] matrixA = { { -1.0, -2.0 }, { -3.0, -4.0 } }; + double[][] matrixB = { { 5.0, 6.0 }, { 7.0, 8.0 } }; + double[][] matrixC = { { -6.0, -8.0 }, { -10.0, -12.0 } }; + + double[][] result = math.matrixSubstraction(matrixA, matrixB); + + assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1); + assertArrayEquals("The seound row is not correct", matrixC[1], result[1], 0.1); + } } \ No newline at end of file