Browse Source

Orginazied some Imports and added Matrix Transponation

featureMatrixCalculator
Lukas Reichwein 5 years ago
parent
commit
0318b36f94
  1. 22
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java
  2. 2
      src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java
  3. 52
      src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java

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

@ -122,4 +122,26 @@ public class MatrixCalcMath {
// Dimensions"); // Dimensions");
} }
} }
/**
* Transposes the Input Matrix. Swaps rows with colums.
*
* @param matrixA The Inputmatrix A wich will be Transposed
* @return The Transposed matrix of matrix A
*/
public double[][] matrixTransponation(double[][] matrixA) {
if(matrixA == null) {
// TODO hier auch die exception.
return null;
}
int columCountResult = matrixA.length;
int rowCountResult = matrixA[0].length;
double[][] result = new double[rowCountResult][columCountResult];
for (int row = 0; row < rowCountResult; row++) {
for (int colum = 0; colum < columCountResult; colum++) {
result[row][colum] = matrixA[colum][row];
}
}
return result;
}
} }

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

@ -1,12 +1,10 @@
package com.ugsbo.matrixcalc; package com.ugsbo.matrixcalc;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
import org.junit.internal.matchers.ThrowableCauseMatcher;
/** /**
* Testet die Matrix Multiplication funkionalität * Testet die Matrix Multiplication funkionalität

52
src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java

@ -0,0 +1,52 @@
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 Transpose a Matix
*/
public class MatrixTransposeTest {
@Test
public void TransformQuadraticMatrixAResultsInQuadraticMatrixC() {
MatrixCalcMath math = new MatrixCalcMath();
// A(2,2) => C(2,2)
double[][] matrixA = { { 1.0, 2.0 }, { 3.0, 4.0 } };
double[][] matrixC = { { 1.0, 3.0 }, { 2.0, 4.0 } };
double[][] result = math.matrixTransponation(matrixA);
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 Transform2by3MatrixAResultsIn3by2MatrixC() {
MatrixCalcMath math = new MatrixCalcMath();
// A(2,3) => C(3,2)
double[][] matrixA = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } };
double[][] matrixC = { { 1.0, 4.0 }, { 2.0, 5.0 }, { 3.0, 6.0 } };
double[][] result = math.matrixTransponation(matrixA);
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 Transform1by3MatrixAResultsIn3by1MatrixC() {
MatrixCalcMath math = new MatrixCalcMath();
// A(1,3) => C(1,3)
double[][] matrixA = { { 1.0, 2.0, 3.0 } };
double[][] matrixC = { { 1.0 }, { 2.0 }, { 3.0 } };
double[][] result = math.matrixTransponation(matrixA);
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