Browse Source

Removed a bug in Matrix Multiplication and added Matrix Adition funktionality

featureMatrixCalculator
Lukas Reichwein 5 years ago
parent
commit
149f1c8cae
  1. 36
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java
  2. 61
      src/test/java/com/ugsbo/matrixcalc/MatrixAdditionTest.java
  3. 68
      src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java

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

@ -1,5 +1,7 @@
package com.ugsbo.matrixcalc;
import java.io.IOException;
/**
* Contains all basic matrix math calculations.
*/
@ -28,7 +30,8 @@ public class MatrixCalcMath {
}
return result;
} else {
throw new IllegalArgumentException("array must be linked");
return null;
//throw new IllegalArgumentException("Matricies must be linked");
}
}
@ -41,15 +44,16 @@ public class MatrixCalcMath {
* @return true if you can Muliply A with B false if not.
*/
public boolean checkIfMatriciesAreLinked(double[][] matrixA, double[][] matrixB) {
if (matrixA != null) {
if (matrixA[0].length == matrixB.length) {
return true;
} else {
if (matrixA == null) {
return false;
}
} else {
if (matrixA.length == 0) {
return false;
}
if (matrixA[0].length == matrixB.length) {
return true;
}
return false;
}
/**
@ -61,8 +65,20 @@ public class MatrixCalcMath {
* @return The Matrixsum of matrix A and matrix B
*/
public double[][] matrixAddition(double[][] matrixA, double[][] matrixB) {
// TODO Sum each Element of matrix A to the corrosponding elem in B
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");
}
}
/**
@ -74,7 +90,11 @@ public class MatrixCalcMath {
* @return true if the Dimensions of Matrix A equals the Dimensions Matrix B
*/
public boolean checkIfMatriciesAreTheSameDimension(double[][] matrixA, double[][] matrixB) {
// TODO Dimension check.
if (matrixA.length == matrixB.length && matrixA[0].length == matrixB[0].length) {
return true;
} else {
return false;
}
}
}

61
src/test/java/com/ugsbo/matrixcalc/MatrixAdditionTest.java

@ -0,0 +1,61 @@
package com.ugsbo.matrixcalc;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Tests the funktionality to add two matricies
*/
public class MatrixAdditionTest {
@Test
public void twoMatriciesHaveTheSameDimensions() {
MatrixCalcMath math = new MatrixCalcMath();
double[][] matrixA = new double[1][1];
double[][] matrixB = new double[1][1];
boolean result = math.checkIfMatriciesAreTheSameDimension(matrixA, matrixB);
assertTrue("Two Matricies with the same Dimension were not detected as that", result);
}
@Test
public void twoMatriciesDONOTHaveTheSameDimensions() {
MatrixCalcMath math = new MatrixCalcMath();
double[][] matrixA = new double[2][1];
double[][] matrixB = new double[1][1];
boolean result = math.checkIfMatriciesAreTheSameDimension(matrixA, matrixB);
assertFalse("Two Matricies without the same Dimension were detected as that", result);
}
@Test
public void addTwoMatriciesWithSameContent() {
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 = { { 2.0, 2.0 }, { 2.0, 2.0 } };
double[][] result = math.matrixAddition(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 addTwoMatriciesWithDiffrentContent() {
MatrixCalcMath math = new MatrixCalcMath();
double[][] matrixA = { { 7.0, 3.0 }, { 2.0, 9.0 } };
double[][] matrixB = { { 6.0, 3.0 }, { 7.0, 11.0 } };
double[][] matrixC = { { 13.0, 6.0 }, { 9.0, 20.0 } };
double[][] result = math.matrixAddition(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);
}
}

68
src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java

@ -1,10 +1,12 @@
package com.ugsbo.matrixcalc;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.internal.matchers.ThrowableCauseMatcher;
/**
* Testet die Matrix Multiplication funkionalität
@ -16,8 +18,10 @@ public class MatrixMultiplicationTest {
MatrixCalcMath math = new MatrixCalcMath();
double[][] matrixA = new double[1][2];
double[][] matrixB = new double[2][1];
assertTrue("Matrix A is Linked to B but it is not detected that way",
math.checkIfMatriciesAreLinked(matrixA, matrixB));
boolean result = math.checkIfMatriciesAreLinked(matrixA, matrixB);
assertTrue("Matrix A is Linked to B but it is not detected that way", result);
}
@Test
@ -25,7 +29,10 @@ public class MatrixMultiplicationTest {
MatrixCalcMath math = new MatrixCalcMath();
double[][] matrixA = null;
double[][] matrixB = null;
assertFalse("Matrix A and B are null but detected as Linked", math.checkIfMatriciesAreLinked(matrixA, matrixB));
boolean result = math.checkIfMatriciesAreLinked(matrixA, matrixB);
assertFalse("Matrix A and B are null but detected as Linked", result);
}
@Test
@ -33,8 +40,10 @@ public class MatrixMultiplicationTest {
MatrixCalcMath math = new MatrixCalcMath();
double[][] matrixA = new double[1][1];
double[][] matrixB = new double[2][1];
assertFalse("Matrix A and B are not Linked but detected as Linked",
math.checkIfMatriciesAreLinked(matrixA, matrixB));
boolean result = math.checkIfMatriciesAreLinked(matrixA, matrixB);
assertFalse("Matrix A and B are not Linked but detected as Linked", result);
}
@Test
@ -43,53 +52,62 @@ public class MatrixMultiplicationTest {
double[][] matrixA = { { 1.0, 1.0 }, { 1.0, 1.0 } };
double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 } };
double[][] matrixC = { { 2.0, 2.0 }, { 2.0, 2.0 } };
assertArrayEquals("The first row is not correct", matrixC[0], math.matrixMultiplication(matrixA, matrixB)[0],
0.1);
assertArrayEquals("The seound row is not correct", matrixC[1], math.matrixMultiplication(matrixA, matrixB)[1],
0.1);
double[][] result = math.matrixMultiplication(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 multiplyTowMatriciesWithDiffrentDimensions() {
MatrixCalcMath math = new MatrixCalcMath();
//A(2,3) B(3,2) => C(2,2)
double[][] matrixA = { { 1.0, 1.0, 1.0}, { 1.0, 1.0, 1.0 } };
// A(2,3) B(3,2) => C(2,2)
double[][] matrixA = { { 1.0, 1.0, 1.0 }, { 1.0, 1.0, 1.0 } };
double[][] matrixB = { { 1.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0 } };
double[][] matrixC = { { 3.0, 3.0 }, { 3.0, 3.0 } };
assertArrayEquals("The first row is not correct", matrixC[0], math.matrixMultiplication(matrixA, matrixB)[0],
0.1);
assertArrayEquals("The seound row is not correct", matrixC[1], math.matrixMultiplication(matrixA, matrixB)[1],
0.1);
double[][] result = math.matrixMultiplication(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 multiplyTowMatriciesWithDiffrentDimensionsAndDiffentContent() {
MatrixCalcMath math = new MatrixCalcMath();
//A(2,3) B(3,2) => C(2,2)
double[][] matrixA = { { 1.0, 2.0, 3.0}, { 4.0, 5.0, 6.0 } };
// A(2,3) B(3,2) => C(2,2)
double[][] matrixA = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } };
double[][] matrixB = { { 7.0, 8.0 }, { 9.0, 10.0 }, { 11.0, 12.0 } };
double[][] matrixC = { { 58.0, 64.0 }, { 139.0, 154.0 } };
assertArrayEquals("The first row is not correct", matrixC[0], math.matrixMultiplication(matrixA, matrixB)[0],
0.1);
assertArrayEquals("The seound row is not correct", matrixC[1], math.matrixMultiplication(matrixA, matrixB)[1],
0.1);
double[][] result = math.matrixMultiplication(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 tryToMultiplyTowEmptyMatricies() {
MatrixCalcMath math = new MatrixCalcMath();
//A(0,0) B(0,0) => null
// A(0,0) B(0,0) => null
double[][] matrixA = new double[0][0];
double[][] matrixB = new double[0][0];
assertArrayEquals(null, math.matrixAddition(matrixA, matrixB));
double[][] result = math.matrixMultiplication(matrixA, matrixB);
assertArrayEquals(null, result);
}
@Test
public void tryToMultiplyTowNullObjects() {
MatrixCalcMath math = new MatrixCalcMath();
//null null => null
// null null => null
double[][] matrixA = null;
double[][] matrixB = null;
assertArrayEquals(null, math.matrixAddition(matrixA, matrixB));
double[][] result = math.matrixMultiplication(matrixA, matrixB);
assertArrayEquals(null, result);
}
}
Loading…
Cancel
Save