Ultra Geile Studenten Benutzer Oberfläche (UGSBO)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.5 KiB

  1. package com.ugsbo.matrixcalc;
  2. //import java.io.IOException;
  3. /**
  4. * Contains all basic matrix math calculations.
  5. */
  6. public class MatrixCalcMath {
  7. /**
  8. * Mutliplys matrixA and matrixB.
  9. *
  10. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  11. * @param matrixB The Inputmatrix B (left TextArea in the GUI)
  12. * @return The Matrixproduct of the matricies A and B
  13. */
  14. public double[][] matrixMultiplication(double[][] matrixA, double[][] matrixB) {
  15. if (checkIfMatriciesAreLinked(matrixA, matrixB)) {
  16. int rowOfResultMatrix = matrixA.length;
  17. int columOfResultMatrix = matrixB[0].length;
  18. int ColumsOfMatA = matrixA[0].length;
  19. double[][] result = new double[rowOfResultMatrix][columOfResultMatrix];
  20. for (int rowResult = 0; rowResult < rowOfResultMatrix; rowResult++) {
  21. for (int columResult = 0; columResult < columOfResultMatrix; columResult++) {
  22. for (int columOfA = 0; columOfA < ColumsOfMatA; columOfA++) {
  23. result[rowResult][columResult] += matrixA[rowResult][columOfA] * matrixB[columOfA][columResult];
  24. }
  25. }
  26. }
  27. return result;
  28. } else {
  29. return null;
  30. // throw new IllegalArgumentException("Matricies must be linked");
  31. }
  32. }
  33. /**
  34. * checks if matrixA and matrixB are linked to know if it is possible to
  35. * multiply them. If they are linked it is possible.
  36. *
  37. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  38. * @param matrixB The Inputmatrix B (left TextArea in the GUI)
  39. * @return true if you can Muliply A with B false if not.
  40. */
  41. public boolean checkIfMatriciesAreLinked(double[][] matrixA, double[][] matrixB) {
  42. if (matrixA == null) {
  43. return false;
  44. }
  45. if (matrixA.length == 0) {
  46. return false;
  47. }
  48. if (matrixA[0].length == matrixB.length) {
  49. return true;
  50. }
  51. return false;
  52. }
  53. /**
  54. * Adds two matroices A and B. Adding matrix A to matrix B is the same as adding
  55. * B to A.
  56. *
  57. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  58. * @param matrixB The Inputmatrix B (left TextArea in the GUI)
  59. * @return The Matrixsum of matrix A and matrix B
  60. */
  61. public double[][] matrixAddition(double[][] matrixA, double[][] matrixB) {
  62. if (checkIfMatriciesAreTheSameDimension(matrixA, matrixB)) {
  63. double[][] result = new double[matrixA.length][matrixA[0].length];
  64. for (int rows = 0; rows < matrixA.length; rows++) {
  65. for (int colums = 0; colums < matrixA[0].length; colums++) {
  66. result[rows][colums] = matrixA[rows][colums] + matrixB[rows][colums];
  67. }
  68. }
  69. return result;
  70. } else {
  71. return null;
  72. // TODO Fragen wie man eine Exception testen kann.
  73. // throw new IllegalArgumentException("Matricies need to have the same
  74. // Dimensions");
  75. }
  76. }
  77. /**
  78. * In order to adding two Matricies they must have the same Dimensions. This
  79. * Methode checks if this is the case.
  80. *
  81. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  82. * @param matrixB The Inputmatrix B (left TextArea in the GUI)
  83. * @return true if the Dimensions of Matrix A equals the Dimensions Matrix B
  84. */
  85. public boolean checkIfMatriciesAreTheSameDimension(double[][] matrixA, double[][] matrixB) {
  86. if (matrixA.length == matrixB.length && matrixA[0].length == matrixB[0].length) {
  87. return true;
  88. } else {
  89. return false;
  90. }
  91. }
  92. /**
  93. * Substracts matrix A by the matrix B. Substaction for Matrices is just the
  94. * substraction of each component with thier coorsponding component.
  95. *
  96. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  97. * @param matrixB The Inputmatrix B (left TextArea in the GUI
  98. * @return matrix A substracted by matrix B
  99. */
  100. public double[][] matrixSubstraction(double[][] matrixA, double[][] matrixB) {
  101. if (checkIfMatriciesAreTheSameDimension(matrixA, matrixB)) {
  102. double[][] result = new double[matrixA.length][matrixA[0].length];
  103. for (int rows = 0; rows < matrixA.length; rows++) {
  104. for (int colums = 0; colums < matrixA[0].length; colums++) {
  105. result[rows][colums] = matrixA[rows][colums] - matrixB[rows][colums];
  106. }
  107. }
  108. return result;
  109. } else {
  110. return null;
  111. // TODO Fragen wie man eine Exception testen kann.
  112. // throw new IllegalArgumentException("Matricies need to have the same
  113. // Dimensions");
  114. }
  115. }
  116. /**
  117. * Transposes the Input Matrix. Swaps rows with colums.
  118. *
  119. * @param matrixA The Inputmatrix A wich will be Transposed
  120. * @return The Transposed matrix of matrix A
  121. */
  122. public double[][] matrixTransponation(double[][] matrixA) {
  123. if(matrixA == null) {
  124. // TODO hier auch die exception.
  125. return null;
  126. }
  127. int columCountResult = matrixA.length;
  128. int rowCountResult = matrixA[0].length;
  129. double[][] result = new double[rowCountResult][columCountResult];
  130. for (int row = 0; row < rowCountResult; row++) {
  131. for (int colum = 0; colum < columCountResult; colum++) {
  132. result[row][colum] = matrixA[colum][row];
  133. }
  134. }
  135. return result;
  136. }
  137. }