diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java index 2c2acf0..f8e1cdb 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java +++ b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java @@ -28,7 +28,7 @@ public class MatrixCalcController { @FXML public void initialize() { multiplyButton.setOnMouseClicked((event) -> { - // TODO matrixATextArea and matrixBTextArea need to be parsed to Double[][] do + // TODO matrixATextArea and matrixBTextArea need to be parsed to double[][] do // this in an extern Methode maybe an extern class. // MatrixCalcMath math = new MatrixCalcMath(); // math.matrixMultiplication(matrixATextArea, matrixATextArea); diff --git a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java index d18d9cd..2e29ba4 100644 --- a/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java +++ b/src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java @@ -12,9 +12,24 @@ public class MatrixCalcMath { * @param matrixB The Inputmatrix B (left TextArea in the GUI) * @return The Matrixproduct of the matricies A and B */ - public Double[][] matrixMultiplication(Double[][] matrixA, Double[][] matrixB) { - // TODO Matrix Multiplication - return null; + public double[][] matrixMultiplication(double[][] matrixA, double[][] matrixB) { + if (checkIfMatriciesAreLinked(matrixA, matrixB)) { + int rowOfResultMatrix = matrixA.length; + int columOfResultMatrix = matrixB[0].length; + int ColumsOfMatA = matrixA[0].length; + double[][] result = new double[rowOfResultMatrix][columOfResultMatrix]; + + for (int rowResult = 0; rowResult < rowOfResultMatrix; rowResult++) { + for (int columResult = 0; columResult < columOfResultMatrix; columResult++) { + for (int columOfA = 0; columOfA < ColumsOfMatA; columOfA++) { + result[rowResult][columResult] += matrixA[rowResult][columOfA] * matrixB[columOfA][columResult]; + } + } + } + return result; + } else { + throw new IllegalArgumentException("array must be linked"); + } } /** @@ -25,8 +40,7 @@ public class MatrixCalcMath { * @param matrixB The Inputmatrix B (left TextArea in the GUI) * @return true if you can Muliply A with B false if not. */ - public boolean checkIfMatriciesAreLinked(Double[][] matrixA, Double[][] matrixB) { - // TODO Check if the number of Rows of Matrix A equal to the coulums of Matrix B + public boolean checkIfMatriciesAreLinked(double[][] matrixA, double[][] matrixB) { if (matrixA != null) { if (matrixA[0].length == matrixB.length) { return true; @@ -46,7 +60,7 @@ public class MatrixCalcMath { * @param matrixB The Inputmatrix B (left TextArea in the GUI) * @return The Matrixsum of matrix A and matrix B */ - public Double[][] matrixAddition(Double[][] matrixA, Double[][] matrixB) { + public double[][] matrixAddition(double[][] matrixA, double[][] matrixB) { // TODO Sum each Element of matrix A to the corrosponding elem in B return null; } @@ -59,7 +73,7 @@ public class MatrixCalcMath { * @param matrixB The Inputmatrix B (left TextArea in the GUI) * @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) { // TODO Dimension check. return false; } diff --git a/src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java b/src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java index 913227d..8dc006f 100644 --- a/src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java +++ b/src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java @@ -1,5 +1,6 @@ package com.ugsbo.matrixcalc; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -11,28 +12,84 @@ import org.junit.Test; public class MatrixMultiplicationTest { @Test - public void MatrixAIsLinkedToMatrixBSouldBeTrue() { + public void matrixAIsLinkedToMatrixBSouldBeTrue() { MatrixCalcMath math = new MatrixCalcMath(); - Double[][] matrixA = new Double[1][2]; - Double[][] matrixB = new Double[2][1]; + 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)); } @Test - public void MatrixAAndMatrixBAreNullSouldReturnFalse() { + public void matrixAAndMatrixBAreNullSouldReturnFalse() { MatrixCalcMath math = new MatrixCalcMath(); - Double[][] matrixA = null; - Double[][] matrixB = null; + double[][] matrixA = null; + double[][] matrixB = null; assertFalse("Matrix A and B are null but detected as Linked", math.checkIfMatriciesAreLinked(matrixA, matrixB)); } @Test - public void MatrixAAndMatrixBAreNotLinkedSouldReturnFalse() { + public void matrixAAndMatrixBAreNotLinkedSouldReturnFalse() { MatrixCalcMath math = new MatrixCalcMath(); - Double[][] matrixA = new Double[1][1]; - Double[][] matrixB = new Double[2][1]; + 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)); } + + @Test + public void multiplyTwoMatriciesWithSameDimensionsAndSameContent() { + 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 } }; + 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); + } + + @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 } }; + 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); + } + + @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 } }; + 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); + } + + @Test + public void tryToMultiplyTowEmptyMatricies() { + MatrixCalcMath math = new MatrixCalcMath(); + //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)); + } + + @Test + public void tryToMultiplyTowNullObjects() { + MatrixCalcMath math = new MatrixCalcMath(); + //null null => null + double[][] matrixA = null; + double[][] matrixB = null; + assertArrayEquals(null, math.matrixAddition(matrixA, matrixB)); + } }