Browse Source

Matrix substraction added, and renamed the test calss for it and the Matrix Addition

featureMatrixCalculator
Lukas Reichwein 5 years ago
parent
commit
e80a444fc7
  1. 27
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java
  2. 43
      src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java

27
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");
}
}
}

43
src/test/java/com/ugsbo/matrixcalc/MatrixAdditionTest.java → 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);
}
}
Loading…
Cancel
Save