32 Commits

Author SHA1 Message Date
Christian Baltzer b28eb8debe Merge commit '934c8f9ab478cb0b45207860429d0c978c72d645' into HEAD 5 years ago
Lukas Reichwein 934c8f9ab4 merged featureMatrixCalculator to pullrequest 5 years ago
Lukas Reichwein 8339da416d correct some typos, and finishing up to get verion 1 of the Matrix Calculator ready to merge 5 years ago
Lukas Reichwein 2077778546 REFACTORING, extracted more form the event Heandler to get even more Readabillity 5 years ago
Lukas Reichwein a1ec4a577c Added try catch around the operations of addition, transponation and multiplication. 5 years ago
Lukas Reichwein 1e2f5969d9 REFACTORING, mainly extracting multiple try catch blocks to get the code more Readable. 5 years ago
Lukas Reichwein 0871928e84 Added IllegalArgumentException to checkInput instead of returning false, get tests and invoking methodes adapted 5 years ago
Lukas Reichwein 197721b332 Added try catch to DetA and subtraction inOrder to Display the reason why the input is Invalid 5 years ago
Lukas Reichwein 04bbd02fae Added try catch to DetB inOrder to Display the reason why the input is Invalid 5 years ago
Lukas Reichwein 95d3d1cb28 linked calculating the Determinants of the input Matrices to their Buttons 5 years ago
Lukas Reichwein e33c57aa2d Created Buttons for Determinat calculation of MatrixA and MatrixB 5 years ago
Lukas Reichwein 638ecdb125 connected Matrix Substraction to the substract Button 5 years ago
Lukas Reichwein 067e433763 connected matrix Addition to the add Button 5 years ago
Lukas Reichwein 2d2c08e674 connected Transponation to the tranpose Button 5 years ago
Lukas Reichwein dd72739d3c Tests 2D-Array to String funtionality, this functionallity was extracted and can now be tested. 5 years ago
Lukas Reichwein e7a4fe7a7d REFACTORING Moved some methodes in order to get a cleaner Struckture 5 years ago
Lukas Reichwein 4d92178abe Refactoring, mostly extracting code 5 years ago
Lukas Reichwein 9ad79c0f2e connected matrixMultiplication to the multiply Button 5 years ago
Lukas Reichwein 023b0dfe38 String to matrix function is finished. 5 years ago
Lukas Reichwein c6b1fae7a6 String to matrix Method functional to parse 2by2 matrices 5 years ago
Lukas Reichwein f7bd4010f7 Refactoring and renameing of some functions 5 years ago
Lukas Reichwein 05c259c0d1 Added and changed some Documentaion 5 years ago
Lukas Reichwein 5f8198e046 Adding CheckInput for a matrix with basic checks 5 years ago
Lukas Reichwein e57a53d6d0 Added and chnged tests in order to test Exceptions instead of if the funktion is returning null 5 years ago
Lukas Reichwein f5c91caa85 Added Claculateing Determinat for 2by2 and 3by3 Matrices 5 years ago
Lukas Reichwein 52b8a818a8 fixed project setup Junit Tests can now run automaticly and removed some unused imports. 5 years ago
Lukas Reichwein 0318b36f94 Orginazied some Imports and added Matrix Transponation 5 years ago
Lukas Reichwein e80a444fc7 Matrix substraction added, and renamed the test calss for it and the Matrix Addition 5 years ago
Lukas Reichwein 149f1c8cae Removed a bug in Matrix Multiplication and added Matrix Adition funktionality 5 years ago
Lukas Reichwein 28c0a349e6 Matrix Multiplication and linked check implemented 5 years ago
Lukas Reichwein f9891c392a The funktionality Linked Matricies Check 5 years ago
Lukas Reichwein 7cba186f82 Added MatrixCalcGui and some dummy Methodes 5 years ago
  1. 8
      src/main/java/com/ugsbo/gui/BasicGuiController.java
  2. 12
      src/main/java/com/ugsbo/gui/MainApp.java
  3. 273
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java
  4. 97
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java
  5. 228
      src/main/java/com/ugsbo/matrixcalc/MatrixCalcMath.java
  6. 4
      src/main/resources/com/ugsbo/gui/BasicGui.fxml
  7. 37
      src/main/resources/com/ugsbo/gui/matrixCalcGui.fxml
  8. 156
      src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java
  9. 61
      src/test/java/com/ugsbo/matrixcalc/MatrixCalcDeterminatTest.java
  10. 170
      src/test/java/com/ugsbo/matrixcalc/MatrixIOUtilsTest.java
  11. 109
      src/test/java/com/ugsbo/matrixcalc/MatrixMultiplicationTest.java
  12. 57
      src/test/java/com/ugsbo/matrixcalc/MatrixTransposeTest.java

8
src/main/java/com/ugsbo/gui/BasicGuiController.java

@ -7,7 +7,7 @@ public class BasicGuiController {
// Hier werden die fx:id Attribute verknuepft. // Hier werden die fx:id Attribute verknuepft.
@FXML @FXML
private Button app1; //Fuer ToDoManager.
private Button matrixCalc;
@FXML @FXML
private Button app2; //Fuer VokabelKartenSchreiber private Button app2; //Fuer VokabelKartenSchreiber
@FXML @FXML
@ -28,9 +28,9 @@ public class BasicGuiController {
*/ */
@FXML @FXML
public void initialize() { public void initialize() {
app1.setOnMouseClicked((event) -> {
//MainApp.startToDoMainGui();
System.out.println(event);
matrixCalc.setOnMouseClicked((event) -> {
MainApp.startMatrixCalcGUI();
// System.out.println(event);
}); });
app2.setOnMouseClicked((event) -> { app2.setOnMouseClicked((event) -> {
//System.out.println(event); //System.out.println(event);

12
src/main/java/com/ugsbo/gui/MainApp.java

@ -35,7 +35,7 @@ public class MainApp extends Application {
/** /**
* Loades the FXML file and the Default CSS. * Loades the FXML file and the Default CSS.
* *
* @param stage The Stage will be passed over from fxml
* @param stage The Stage will be passed over from JavaFx
* @param fxmlFileName Only the Filename of the fxml file wich sould be loaded * @param fxmlFileName Only the Filename of the fxml file wich sould be loaded
*/ */
private void createStageFromFXML(Stage stage, String fxmlFileName) { private void createStageFromFXML(Stage stage, String fxmlFileName) {
@ -64,4 +64,14 @@ public class MainApp extends Application {
MainApp main = new MainApp(); MainApp main = new MainApp();
main.createStageFromFXML(stage, "Voabelkartenschreiber"); main.createStageFromFXML(stage, "Voabelkartenschreiber");
} }
/**
* Startet eine Instanz der MatrixCalcGui.
*/
public static void startMatrixCalcGUI() {
Stage stage = new Stage();
MainApp app = new MainApp();
app.createStageFromFXML(stage, "matrixCalcGui");
}
} }

273
src/main/java/com/ugsbo/matrixcalc/MatrixCalcController.java

@ -0,0 +1,273 @@
package com.ugsbo.matrixcalc;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import java.util.ArrayList;
public class MatrixCalcController {
private static final String MULTIPLICATION_STRING = "multiplication";
private static final String ADDITION_STRING = "addition";
private static final String SUBSTRACTION_STRING = "substract";
private static final String TRANPOSE_STRING = "transpose";
private static final String CALCDETERMINAT_STRING = "calcDeterminate";
// The fx:id Attributes will be bind here.
@FXML
private Button multiplyButton, addButton, DetAButton, DetBButton, substractButton, transposeButton;
@FXML
private Text errorText, outputText;
@FXML
private TextArea matrixATextArea, matrixBTextArea;
private MatrixCalcMath math = new MatrixCalcMath();
private MatrixCalcIOUtils util = new MatrixCalcIOUtils();
/**
* Konstructor is called before initialize()
*/
public MatrixCalcController() {
// Setup of some Fields could be defined here.
}
/**
* Initializes the controller class. This method is automatically called after
* the fxml file has been loaded.
*/
@FXML
public void initialize() {
/**
* Convert Strings to matricies, multiply them and output the result.
*/
multiplyButton.setOnMouseClicked((event) -> {
String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText();
String[] stringMatrix = { stringMatrixA, stringMatrixB };
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
matricies.add(util.stringToMatrix(stringMatrixB));
invokeOperation(matricies, MULTIPLICATION_STRING);
});
/**
* Convert a String to a matrix, transpose it and output the result.
*/
transposeButton.setOnMouseClicked((event) -> {
String stringMatrixA = matrixATextArea.getText();
String[] stringMatrix = { stringMatrixA, "" };
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
invokeOperation(matricies, TRANPOSE_STRING);
});
/**
* Convert Strings to matricies, add them and output the result.
*/
addButton.setOnMouseClicked((event) -> {
String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText();
String[] stringMatrix = { stringMatrixA, stringMatrixB };
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
matricies.add(util.stringToMatrix(stringMatrixB));
invokeOperation(matricies, ADDITION_STRING);
});
/**
* Convert Strings to matricies, substract them and output the result.
*/
substractButton.setOnMouseClicked((event) -> {
String stringMatrixA = matrixATextArea.getText();
String stringMatrixB = matrixBTextArea.getText();
String[] stringMatrix = { stringMatrixA, stringMatrixB };
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 2);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
matricies.add(util.stringToMatrix(stringMatrixB));
invokeOperation(matricies, SUBSTRACTION_STRING);
});
/**
* Convert the String of the left inputField to a matrix, calculate the Determinate and output it.
*/
DetAButton.setOnMouseClicked((event) -> {
String stringMatrixA = matrixATextArea.getText();
String[] stringMatrix = { stringMatrixA, "" };
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixA));
invokeOperation(matricies, CALCDETERMINAT_STRING);
});
/**
* Convert the String of the right inputField to a matrix, calculate the Determinate and output it.
*/
DetBButton.setOnMouseClicked((event) -> {
String stringMatrixB = matrixBTextArea.getText();
String[] stringMatrix = { "", stringMatrixB };
checkInputAndDisplayIfInputIsNotValid(stringMatrix, 1);
ArrayList<double[][]> matricies = new ArrayList<double[][]>();
matricies.add(util.stringToMatrix(stringMatrixB));
invokeOperation(matricies, CALCDETERMINAT_STRING);
});
}
/**
* Invokes the needed operations form the MatrixCalcMath class
* @param matricies Contains both Matricies or onely one Matrix
* @param operation One of the global Constats to select wich Operation is needed.
*/
private void invokeOperation(ArrayList<double[][]> matricies, String operation) {
if (matricies.size() == 2) {
if (operation.equals(MULTIPLICATION_STRING)) {
try {
double[][] result = math.matrixMultiplication(matricies.get(0), matricies.get(1));
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
} else if (operation.equals(ADDITION_STRING)) {
try {
double[][] result = math.matrixAddition(matricies.get(0), matricies.get(1));
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
} else if (operation.equals(SUBSTRACTION_STRING)) {
try {
double[][] result = math.matrixSubstraction(matricies.get(0), matricies.get(1));
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
}
}else if (matricies.size() == 1) {
if (operation.equals(TRANPOSE_STRING)) {
try {
double[][] result = math.matrixTransponation(matricies.get(0));
String DisplayableString = util.outputMatrixToOutputText(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
}
if (operation.equals(CALCDETERMINAT_STRING)) {
try {
double result = math.calcDeterminat(matricies.get(0));
String DisplayableString = Double.toString(result);
outputText.setText(DisplayableString);
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (IllegalArgumentException e) {
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
} catch (Exception e){
outputText.setText(e.getMessage());
outputText.setTextAlignment(TextAlignment.CENTER);
}
}
}
}
/**
* Checks the Input and Displays it if the Input is valid.
*
* @param stringMatrix Contains both input matrices if
* numberOfMarriciesToMatch is 2. If the number
* is 1 than one of them has to be a empty String
* @param numberOfMatricesToMatch If the number is 1 onely one Marix will be
* verifyed and the other one needs to be an empty
* String in the stringMatrix
*/
private void checkInputAndDisplayIfInputIsNotValid(String[] stringMatrix, int numberOfMatricesToMatch) {
if (numberOfMatricesToMatch == 1 && !stringMatrix[0].equals("")) {
try {
util.checkInput(stringMatrix[0]);
} catch (Exception e) {
outputText.setText(e.getMessage() + "A");
outputText.setTextAlignment(TextAlignment.CENTER);
}
} else if (numberOfMatricesToMatch == 1 && !stringMatrix[1].equals("")) {
try {
util.checkInput(stringMatrix[1]);
} catch (Exception e) {
outputText.setText(e.getMessage() + "B");
outputText.setTextAlignment(TextAlignment.CENTER);
}
} else if (numberOfMatricesToMatch == 2 && !stringMatrix[0].equals("") && !stringMatrix[1].equals("")) {
try {
util.checkInput(stringMatrix[0]);
} catch (Exception e) {
outputText.setText(e.getMessage() + "A");
outputText.setTextAlignment(TextAlignment.CENTER);
}
try {
util.checkInput(stringMatrix[1]);
} catch (Exception e) {
outputText.setText(e.getMessage() + "B");
outputText.setTextAlignment(TextAlignment.CENTER);
}
} else if (stringMatrix[0].equals("")) {
outputText.setText("Pease insert MatrixA");
outputText.setTextAlignment(TextAlignment.CENTER);
} else if (stringMatrix[1].equals("")) {
outputText.setText("Pease insert MatrixB");
outputText.setTextAlignment(TextAlignment.CENTER);
}
}
}

97
src/main/java/com/ugsbo/matrixcalc/MatrixCalcIOUtils.java

@ -0,0 +1,97 @@
package com.ugsbo.matrixcalc;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatrixCalcIOUtils {
/**
* Prints a given 2D-Array to the output Textfield.
*
* @param output2DArray The Array that gets Displayed
*/
protected String outputMatrixToOutputText(double[][] output2DArray) {
// convert array to String
String DisplayableString = convertsArrayToStringInOrderToDisplayIt(output2DArray);
// Display output
return DisplayableString;
}
/**
* Converts 2D-Array to String in order to Display it.
*
* @param array2D The array wich will be converted to an Displayable String
* @return The Displayable String
*/
protected String convertsArrayToStringInOrderToDisplayIt(double[][] array2D) {
String displayableString = "";
for (int i = 0; i < array2D.length; i++) {
for (int j = 0; j < array2D[0].length; j++) {
displayableString += array2D[i][j] + " ";
}
displayableString += "\n\n";
}
return displayableString;
}
/**
* Checks if the Input is Valid, with Regex. Returns true if the Matrix can be
* matched by the regular Expression.
*
* @param matrix It is the InputMatrix
* @return True if the Matrix is valid Input.
*/
protected void checkInput(String matrix) throws IllegalArgumentException {
if (matrix.length() == 0) {
throw new IllegalArgumentException("Please insert a Matrix");
}
// Matches digits witch following spaces 1 to 3 times
String row1 = "(\\d*\\u0020*){1,3}";
// Matches newlineCurrierReturn followed by digits witch following spaces 1 to
// 3 times
String row2 = "(\\n){0,3}(\\d*\\u0020*){0,3}";
String row3 = "(\\n){0,3}(\\d*\\u0020*){0,3}";
// TODO for verion 2 get the input check more stricktly missing matrix slots are allowed.
Pattern p = Pattern.compile(row1 + row2 + row3);
Matcher m = p.matcher(matrix);
if(!m.matches()){
throw new IllegalArgumentException("A Matrix is not in the right format, Matrix ");
}
}
/**
* Converts a String form the Inputfield to an 2D-Array aka Matrix.
*
* @param stringMatrix The String form the Inputfield
* @return Matrix as a 2D-Array
*/
public double[][] stringToMatrix(String stringMatrix) {
ArrayList<String[]> singleNumbersArr = new ArrayList<String[]>();
// Splitting the strings into their rows
String[] singleNumbers = null;
String[] rows = stringMatrix.split("\n");
for (int i = 0; i < rows.length; i++) {
// Splitting rows into their Numbers
singleNumbers = rows[i].split("\\s");
singleNumbersArr.add(singleNumbers);
}
int rowlength = singleNumbersArr.get(0).length;
int columCount = singleNumbersArr.size();
double[][] matrix = new double[columCount][rowlength];
for (int columIndex = 0; columIndex < columCount; columIndex++) {
for (int rowIndex = 0; rowIndex < singleNumbers.length; rowIndex++) {
matrix[columIndex][rowIndex] = Double.parseDouble(singleNumbersArr.get(columIndex)[rowIndex]);
}
}
return matrix;
}
}

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

@ -0,0 +1,228 @@
package com.ugsbo.matrixcalc;
/**
* Contains all basic matrix math calculations.
*/
public class MatrixCalcMath {
/**
* Mutliplys matrixA and matrixB.
*
* @param matrixA The Inputmatrix A (right TextArea in the GUI)
* @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) {
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("Matricies must be linked");
}
}
/**
* checks if matrixA and matrixB are linked to know if it is possible to
* multiply them. If they are linked it is possible.
*
* @param matrixA The Inputmatrix A (right TextArea in the GUI)
* @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) {
if (matrixA == null) {
return false;
}
if (matrixA.length == 0) {
return false;
}
if (matrixA[0].length == matrixB.length) {
return true;
}
return false;
}
/**
* Adds two matroices A and B. Adding matrix A to matrix B is the same as adding
* B to A.
*
* @param matrixA The Inputmatrix A (right TextArea in the GUI)
* @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) {
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 {
throw new IllegalArgumentException("Matricies need to have the same Dimensions");
}
}
/**
* In order to adding two Matricies they must have the same Dimensions. This
* Methode checks if this is the case.
*
* @param matrixA The Inputmatrix A (right TextArea in the GUI)
* @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) {
if (matrixA == null || matrixA.length == 0) {
return false;
}
if (matrixA[0] == null) {
return false;
}
if (matrixA.length == matrixB.length && matrixA[0].length == matrixB[0].length) {
return true;
} else {
return false;
}
}
/**
* 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 {
throw new IllegalArgumentException("Matricies need to have the same 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) {
throw new IllegalArgumentException("Matricies need to have the same Dimensions");
}
if (matrixA.length == 0) {
throw new IllegalArgumentException("Matricies need to have the same Dimensions");
}
if (matrixA[0] == null) {
throw new IllegalArgumentException("Matricies need to have the same Dimensions");
}
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;
}
/**
* Calculates the Determinant of a Matrix.
*
* @param matrixA The Inputmatrix
* @return The Determinant of the Matrix A
*/
public double calcDeterminat(double[][] matrixA) throws IllegalArgumentException{
// checking if a Determinant can be calculated.
double result = 0.0;
if (checkIfMatrixIsQuadradtic(matrixA)) {
if (getMatrixRowCount(matrixA) == 2) {
result = calc2by2Determinant(matrixA);
} else if (getMatrixRowCount(matrixA) == 3) {
result = calc3by3Determinant(matrixA);
} else {
throw new IllegalArgumentException("Matrix is not 2 by 2 or 3 by 3");
}
} else {
throw new IllegalArgumentException("Matrix must be Quadratic");
}
return result;
}
/**
* Calculates the Determinat of an three by three Matrix.
*
* @param matrixA The Inputmatrix form wich the Determinat will be calculated
* @return the Determinant of the Matrix
*/
private double calc3by3Determinant(double[][] matrixA) {
double result = matrixA[0][0] * matrixA[1][1] * matrixA[2][2] + matrixA[0][1] * matrixA[1][2] * matrixA[2][0]
+ matrixA[0][2] * matrixA[1][0] * matrixA[2][1] - matrixA[0][0] * matrixA[1][2] * matrixA[2][1]
- matrixA[0][1] * matrixA[1][0] * matrixA[2][2] - matrixA[0][2] * matrixA[1][1] * matrixA[2][0];
return result;
}
/**
* Calculates the Determinat of an two by two Matrix.
*
* @param matrixA The Inputmatrix form wich the Determinat will be calculated
* @return the Determinant of the Matrix
*/
private double calc2by2Determinant(double[][] matrixA) {
double result = matrixA[0][0] * matrixA[1][1] - matrixA[0][1] * matrixA[1][0];
return result;
}
/**
* Returns the Number of rows of a Matrix.
*
* @param matrixA the Inputmatrix form wich the rows will be counted
* @return Number of rows
*/
private int getMatrixRowCount(double[][] matrixA) {
return matrixA.length;
}
/**
* Checks if the rows and colums of an Matrix are equal. If they are equal the
* Matrix is Quadratic and the function will return true.
*
* @param matrixA the Inputmatrix for wich the rows and colums will be compared
* @return isQuadratic
*/
private boolean checkIfMatrixIsQuadradtic(double[][] matrixA) {
if (matrixA == null) {
return false;
}
if (matrixA[0] == null) {
return false;
}
if (matrixA.length == matrixA[0].length) {
return true;
}
return false;
}
}

4
src/main/resources/com/ugsbo/gui/BasicGui.fxml

@ -27,8 +27,8 @@
</AnchorPane> </AnchorPane>
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="150.0" AnchorPane.rightAnchor="150.0" AnchorPane.topAnchor="50.0"> <AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="150.0" AnchorPane.rightAnchor="150.0" AnchorPane.topAnchor="50.0">
<children> <children>
<Button fx:id="app1" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="260.0" text="ToDoListe" textAlignment="CENTER" AnchorPane.leftAnchor="19.0" AnchorPane.rightAnchor="19.0" AnchorPane.topAnchor="15.0" />
<Button fx:id="app2" maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="260.0" text="VokabelKartenSchreiber" textAlignment="CENTER" AnchorPane.leftAnchor="19.0" AnchorPane.rightAnchor="19.0" AnchorPane.topAnchor="60.0" />
<Button fx:id="matrixCalc" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="260.0" text="Matrix Calculator" textAlignment="CENTER" AnchorPane.leftAnchor="19.0" AnchorPane.rightAnchor="19.0" AnchorPane.topAnchor="15.0" />
<Button fx:id="app2" maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="260.0" text="App2" textAlignment="CENTER" AnchorPane.leftAnchor="19.0" AnchorPane.rightAnchor="19.0" AnchorPane.topAnchor="60.0" />
<Button fx:id="app3" maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="260.0" text="App3" textAlignment="CENTER" AnchorPane.leftAnchor="19.0" AnchorPane.rightAnchor="19.0" AnchorPane.topAnchor="105.0" /> <Button fx:id="app3" maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="260.0" text="App3" textAlignment="CENTER" AnchorPane.leftAnchor="19.0" AnchorPane.rightAnchor="19.0" AnchorPane.topAnchor="105.0" />
<Button fx:id="app4" maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="260.0" text="App4" textAlignment="CENTER" AnchorPane.leftAnchor="19.0" AnchorPane.rightAnchor="19.0" AnchorPane.topAnchor="150.0" /> <Button fx:id="app4" maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="30.0" prefWidth="260.0" text="App4" textAlignment="CENTER" AnchorPane.leftAnchor="19.0" AnchorPane.rightAnchor="19.0" AnchorPane.topAnchor="150.0" />
</children> </children>

37
src/main/resources/com/ugsbo/gui/matrixCalcGui.fxml

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ugsbo.matrixcalc.MatrixCalcController">
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TextArea fx:id="matrixATextArea" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0" />
</children>
</AnchorPane>
<AnchorPane layoutX="200.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="200.0" AnchorPane.leftAnchor="200.0" AnchorPane.rightAnchor="200.0" AnchorPane.topAnchor="0.0">
<children>
<Button fx:id="multiplyButton" layoutX="14.0" layoutY="27.0" mnemonicParsing="false" text="multiply" AnchorPane.leftAnchor="15.0" AnchorPane.topAnchor="27.0" />
<Button fx:id="addButton" layoutX="14.0" layoutY="62.0" mnemonicParsing="false" text="add" AnchorPane.leftAnchor="15.0" AnchorPane.topAnchor="67.0" />
<Button fx:id="substractButton" layoutX="99.0" layoutY="62.0" mnemonicParsing="false" text="Substract" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="67.0" />
<Button fx:id="transposeButton" layoutX="97.0" layoutY="27.0" mnemonicParsing="false" text="Transpose" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="27.0" />
<Button fx:id="DetAButton" layoutX="14.0" layoutY="95.0" mnemonicParsing="false" text="DetA" AnchorPane.leftAnchor="15.0" AnchorPane.topAnchor="107.0" />
<Button fx:id="DetBButton" layoutX="120.0" layoutY="107.0" mnemonicParsing="false" text="DetB" />
<Text fx:id="errorText" layoutX="14.0" layoutY="113.0" strokeType="OUTSIDE" strokeWidth="0.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="100.0" />
</children>
</AnchorPane>
<AnchorPane layoutX="400.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TextArea fx:id="matrixBTextArea" layoutX="-15.0" layoutY="30.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0" />
</children>
</AnchorPane>
<Text fx:id="outputText" layoutX="14.0" layoutY="242.0" strokeType="OUTSIDE" strokeWidth="0.0" textAlignment="JUSTIFY" wrappingWidth="554.7294921875" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="210.0" />
</children>
</AnchorPane>

156
src/test/java/com/ugsbo/matrixcalc/MatrixAdditionAndSubstractionTest.java

@ -0,0 +1,156 @@
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.Before;
import org.junit.Test;
/**
* Tests the funktionality to add and substract two matricies
*/
public class MatrixAdditionAndSubstractionTest {
private MatrixCalcMath matrixMath;
@Before
public void setup() {
matrixMath = new MatrixCalcMath();
}
@Test
public void twoMatriciesHaveTheSameDimensions() {
double[][] matrixA = new double[1][1];
double[][] matrixB = new double[1][1];
boolean result = matrixMath.checkIfMatriciesAreTheSameDimension(matrixA, matrixB);
assertTrue("Two Matricies with the same Dimension were not detected as that", result);
}
@Test
public void twoMatriciesDONOTHaveTheSameDimensions() {
double[][] matrixA = new double[2][1];
double[][] matrixB = new double[1][1];
boolean result = matrixMath.checkIfMatriciesAreTheSameDimension(matrixA, matrixB);
assertFalse("Two Matricies without the same Dimension were detected as that", result);
}
@Test
public void addTwoMatriciesWithSameContent() {
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 = matrixMath.matrixAddition(matrixA, matrixB);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
}
@Test
public void addTwoMatriciesWithDiffrentContent() {
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 = matrixMath.matrixAddition(matrixA, matrixB);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
}
@Test(expected = IllegalArgumentException.class)
public void tryToAddTowEmptyMatricies() {
// A(0,0) B(0,0) => IllegalArgumentException
double[][] matrixA = new double[0][0];
double[][] matrixB = new double[0][0];
matrixMath.matrixAddition(matrixA, matrixB);
}
@Test(expected = IllegalArgumentException.class)
public void tryToAddTowNullMatrices() {
// A(0,0) B(0,0) => IllegalArgumentException
double[][] matrixA = null;
double[][] matrixB = null;
matrixMath.matrixAddition(matrixA, matrixB);
}
@Test(expected = IllegalArgumentException.class)
public void tryToAddTowMatricesWithDifferentDimensions() {
// A(0,0) B(0,0) => IllegalArgumentException
double[][] matrixA = { { 1.0, 2.0 } };
double[][] matrixB = { { 1.0 }, { 2.0 } };
matrixMath.matrixAddition(matrixA, matrixB);
}
@Test
public void substractTwoMatriciesWithSameContent() {
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 = matrixMath.matrixSubstraction(matrixA, matrixB);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
}
@Test
public void substractTwoMatriciesWithDiffrentContent() {
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 = matrixMath.matrixSubstraction(matrixA, matrixB);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
}
@Test
public void substractTwoMatriciesWithNegativeContent() {
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 = matrixMath.matrixSubstraction(matrixA, matrixB);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
}
@Test(expected = IllegalArgumentException.class)
public void tryToSubstractTowEmptyMatricies() {
// A(0,0) B(0,0) => IllegalArgumentException
double[][] matrixA = new double[0][0];
double[][] matrixB = new double[0][0];
matrixMath.matrixSubstraction(matrixA, matrixB);
}
@Test(expected = IllegalArgumentException.class)
public void tryToSubstractTowNullMatrices() {
// A(0,0) B(0,0) => IllegalArgumentException
double[][] matrixA = null;
double[][] matrixB = null;
matrixMath.matrixSubstraction(matrixA, matrixB);
}
@Test(expected = IllegalArgumentException.class)
public void tryToSubstractTowMatricesWithDifferentDimensions() {
// A(0,0) B(0,0) => IllegalArgumentException
double[][] matrixA = { { 1.0, 2.0 } };
double[][] matrixB = { { 1.0 }, { 2.0 } };
matrixMath.matrixSubstraction(matrixA, matrixB);
}
}

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

@ -0,0 +1,61 @@
package com.ugsbo.matrixcalc;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
/**
* Tests the funktionality to Calculate the Determinant of a Matrix.
*/
public class MatrixCalcDeterminatTest {
private MatrixCalcMath matrixMath;
@Before
public void setup() {
matrixMath = new MatrixCalcMath();
}
@Test
public void CalculatesTheDeterminanteOfA2by2Matrix() {
// A(2,2)
double[][] matrixA = { { 1.0, 2.0 }, { 3.0, 4.0 } };
double determinat = -2.0;
double delta = 0.01;
double result = matrixMath.calcDeterminat(matrixA);
assertEquals("The Determinant is not as it should be", determinat, result, delta);
}
@Test
public void CalculatesTheDeterminanteOfA3by3Matrix() {
// A(3,3)
double[][] matrixA = { { 1.0, 2.0, 1.0 }, { 3.0, 4.0, 0.0 }, { 5.0, 6.0, 0.0 } };
double determinat = -2.0;
double delta = 0.01;
double result = matrixMath.calcDeterminat(matrixA);
assertEquals("The Determinant is not as it should be", determinat, result, delta);
}
@Test(expected = IllegalArgumentException.class)
public void tryToCalculateA4by4Matrix_ShouldResulInIllegalArgumentException() {
// A(4,4)
double[][] matrixA = { { 1.0, 2.0, 1.0, 0.0 }, { 3.0, 4.0, 0.0, 0.0 }, { 5.0, 6.0, 0.0, 0.0 },
{ 5.0, 6.0, 0.0, 0.0 } };
matrixMath.calcDeterminat(matrixA);
}
@Test(expected = IllegalArgumentException.class)
public void tryToCalculateANonQuadraticMatrix_SouldResulInIllegalArgumentException() {
// A(2,4)
double[][] matrixA = { { 5.0, 6.0, 0.0, 0.0 }, { 5.0, 6.0, 0.0, 0.0 } };
matrixMath.calcDeterminat(matrixA);
}
}

170
src/test/java/com/ugsbo/matrixcalc/MatrixIOUtilsTest.java

@ -0,0 +1,170 @@
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.Before;
import org.junit.Test;
/**
* Tests how well worng input can be determent.
*/
public class MatrixIOUtilsTest {
private MatrixCalcIOUtils util;
@Before
public void setup() {
util = new MatrixCalcIOUtils();
}
@Test(expected = IllegalArgumentException.class)
public void inputEmptySouldThrowAIllegalArgumentException() {
String input = "";
util.checkInput(input);
}
@Test
public void checkIfA1by3MatrixIsValidInput() {
String input = "1 2 3";
try {
// act
util.checkInput(input);
// assert
assertTrue("The 1 by 3 Matrix should be Matched as valid input.", true);
} catch (IllegalArgumentException e) {
assertFalse("The 1 by 3 Matrix should be Matched as valid input.", true);
}
}
@Test
public void checkIfA2by3MatrixIsValidInput() {
// arrange
String input = "1 2 3\n1 2 3";
try {
// act
util.checkInput(input);
// assert
assertTrue("The 2 by 3 Matrix should be Matched as valid input.", true);
} catch (IllegalArgumentException e) {
assertFalse("The 2 by 3 Matrix should be Matched as valid input.", true);
}
}
@Test
public void checkIfA3by3MatrixIsValidInput() {
String input = "1 2 3\n1 2 3\n1 2 3";
try {
util.checkInput(input);
assertTrue("The 3 by 3 Matrix should be Matched as valid input.", true);
} catch (IllegalArgumentException e) {
assertFalse("The 3 by 3 Matrix should be Matched as valid input.", true);
}
}
@Test
public void StringWithSingleDigitNumbersToMatrix_ReturnsEquivalentMatrix() {
String inputString = "1";
double[][] expected = { { 1.0 } };
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
}
@Test
public void StringWithfourDigitNumbersToMatrix_ReturnsEquivalentMatrix() {
String inputString = "1 2\n3 4";
double[][] expected = { { 1.0, 2.0 }, { 3.0, 4.0 } };
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
}
@Test
public void StringWithSixDigitNumbersToMatrix_Returns1by6Matrix() {
String inputString = "1 2 3 4 5 6";
double[][] expected = { { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 } };
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
}
@Test
public void StringWithSixDigitNumbersToMatrix_Returns2by3Matrix() {
String inputString = "1 2 3\n4 5 6";
double[][] expected = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } };
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
}
@Test
public void StringWithSixDigitNumbersToMatrix_Returns3by2Matrix() {
String inputString = "1 2\n3 4\n5 6";
double[][] expected = { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 } };
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
assertArrayEquals("The therd row is not correct", expected[2], result[2], 0.1);
}
@Test
public void StringWithNineDigitNumbersToMatrix_Returns3by3Matrix() {
String inputString = "1 2 3\n4 5 6\n7 8 9";
double[][] expected = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 } };
double[][] result = util.stringToMatrix(inputString);
assertArrayEquals("The first row is not correct", expected[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", expected[1], result[1], 0.1);
assertArrayEquals("The thierd row is not correct", expected[2], result[2], 0.1);
}
@Test
public void convertsArrayToString_SingleNumber() {
double[][] matrix = { { 1.0 } };
String expected = "1.0 \n\n";
String result = util.convertsArrayToStringInOrderToDisplayIt(matrix);
assertEquals("The Strings do not Match", expected, result);
}
@Test
public void convertsArrayToString_FourNumbersInARow() {
double[][] matrix = { { 1.0, 2.0, 3.0, 4.0 } };
String expected = "1.0 2.0 3.0 4.0 \n\n";
String result = util.convertsArrayToStringInOrderToDisplayIt(matrix);
assertEquals("The Strings do not Match", expected, result);
}
@Test
public void convertsArrayToString_FourNumbersInTwoRows() {
double[][] matrix = { { 1.0, 2.0 }, { 3.0, 4.0 } };
String expected = "1.0 2.0 \n\n3.0 4.0 \n\n";
String result = util.convertsArrayToStringInOrderToDisplayIt(matrix);
assertEquals("The Strings do not Match", expected, result);
}
}

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

@ -0,0 +1,109 @@
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.Before;
import org.junit.Test;
/**
* Testet die Matrix Multiplication funkionalität
*/
public class MatrixMultiplicationTest {
private MatrixCalcMath matrixMath;
@Before
public void setup() {
matrixMath = new MatrixCalcMath();
}
@Test
public void matrixAIsLinkedToMatrixBSouldBeTrue() {
double[][] matrixA = new double[1][2];
double[][] matrixB = new double[2][1];
boolean result = matrixMath.checkIfMatriciesAreLinked(matrixA, matrixB);
assertTrue("Matrix A is Linked to B but it is not detected that way", result);
}
@Test
public void matrixAAndMatrixBAreNullSouldReturnFalse() {
double[][] matrixA = null;
double[][] matrixB = null;
boolean result = matrixMath.checkIfMatriciesAreLinked(matrixA, matrixB);
assertFalse("Matrix A and B are null but detected as Linked", result);
}
@Test
public void matrixAAndMatrixBAreNotLinkedSouldReturnFalse() {
double[][] matrixA = new double[1][1];
double[][] matrixB = new double[2][1];
boolean result = matrixMath.checkIfMatriciesAreLinked(matrixA, matrixB);
assertFalse("Matrix A and B are not Linked but detected as Linked", result);
}
@Test
public void multiplyTwoMatriciesWithSameDimensionsAndSameContent() {
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 = matrixMath.matrixMultiplication(matrixA, matrixB);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
}
@Test
public void multiplyTowMatriciesWithDiffrentDimensions() {
// 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 } };
double[][] result = matrixMath.matrixMultiplication(matrixA, matrixB);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
}
@Test
public void multiplyTowMatriciesWithDiffrentDimensionsAndDiffentContent() {
// 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 } };
double[][] result = matrixMath.matrixMultiplication(matrixA, matrixB);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
}
@Test(expected = IllegalArgumentException.class)
public void tryToMultiplyTowEmptyMatricies() {
// A(0,0) B(0,0) => IllegalArgumentException
double[][] matrixA = new double[0][0];
double[][] matrixB = new double[0][0];
matrixMath.matrixMultiplication(matrixA, matrixB);
}
@Test(expected = IllegalArgumentException.class)
public void tryToMultiplyTowNullObjects() {
// null null => IllegalArgumentException
double[][] matrixA = null;
double[][] matrixB = null;
matrixMath.matrixMultiplication(matrixA, matrixB);
}
}

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

@ -0,0 +1,57 @@
package com.ugsbo.matrixcalc;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Before;
import org.junit.Test;
/**
* Tests the funktionality to Transpose a Matix
*/
public class MatrixTransposeTest {
private MatrixCalcMath matrixMath;
@Before
public void setup() {
matrixMath = new MatrixCalcMath();
}
@Test
public void TransformQuadraticMatrixA_ResultsInQuadraticMatrixC() {
// 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 = matrixMath.matrixTransponation(matrixA);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
}
@Test
public void Transform2by3MatrixA_ResultsIn3by2MatrixC() {
// 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 = matrixMath.matrixTransponation(matrixA);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
assertArrayEquals("The third row is not correct", matrixC[2], result[2], 0.1);
}
@Test
public void Transform1by3MatrixA_ResultsIn3by1MatrixC() {
// A(1,3) => C(1,3)
double[][] matrixA = { { 1.0, 2.0, 3.0 } };
double[][] matrixC = { { 1.0 }, { 2.0 }, { 3.0 } };
double[][] result = matrixMath.matrixTransponation(matrixA);
assertArrayEquals("The first row is not correct", matrixC[0], result[0], 0.1);
assertArrayEquals("The second row is not correct", matrixC[1], result[1], 0.1);
assertArrayEquals("The third row is not correct", matrixC[2], result[2], 0.1);
}
}
Loading…
Cancel
Save