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.

205 lines
7.6 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. throw new IllegalArgumentException("Matricies must be linked");
  30. }
  31. }
  32. /**
  33. * checks if matrixA and matrixB are linked to know if it is possible to
  34. * multiply them. If they are linked it is possible.
  35. *
  36. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  37. * @param matrixB The Inputmatrix B (left TextArea in the GUI)
  38. * @return true if you can Muliply A with B false if not.
  39. */
  40. public boolean checkIfMatriciesAreLinked(double[][] matrixA, double[][] matrixB) {
  41. if (matrixA == null) {
  42. return false;
  43. }
  44. if (matrixA.length == 0) {
  45. return false;
  46. }
  47. if (matrixA[0].length == matrixB.length) {
  48. return true;
  49. }
  50. return false;
  51. }
  52. /**
  53. * Adds two matroices A and B. Adding matrix A to matrix B is the same as adding
  54. * B to A.
  55. *
  56. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  57. * @param matrixB The Inputmatrix B (left TextArea in the GUI)
  58. * @return The Matrixsum of matrix A and matrix B
  59. */
  60. public double[][] matrixAddition(double[][] matrixA, double[][] matrixB) {
  61. if (checkIfMatriciesAreTheSameDimension(matrixA, matrixB)) {
  62. double[][] result = new double[matrixA.length][matrixA[0].length];
  63. for (int rows = 0; rows < matrixA.length; rows++) {
  64. for (int colums = 0; colums < matrixA[0].length; colums++) {
  65. result[rows][colums] = matrixA[rows][colums] + matrixB[rows][colums];
  66. }
  67. }
  68. return result;
  69. } else {
  70. throw new IllegalArgumentException("Matricies need to have the same Dimensions");
  71. }
  72. }
  73. /**
  74. * In order to adding two Matricies they must have the same Dimensions. This
  75. * Methode checks if this is the case.
  76. *
  77. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  78. * @param matrixB The Inputmatrix B (left TextArea in the GUI)
  79. * @return true if the Dimensions of Matrix A equals the Dimensions Matrix B
  80. */
  81. public boolean checkIfMatriciesAreTheSameDimension(double[][] matrixA, double[][] matrixB) {
  82. if (matrixA == null || matrixA.length == 0) {
  83. return false;
  84. }
  85. if (matrixA[0] == null) {
  86. return false;
  87. }
  88. if (matrixA.length == matrixB.length && matrixA[0].length == matrixB[0].length) {
  89. return true;
  90. } else {
  91. return false;
  92. }
  93. }
  94. /**
  95. * Substracts matrix A by the matrix B. Substaction for Matrices is just the
  96. * substraction of each component with thier coorsponding component.
  97. *
  98. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  99. * @param matrixB The Inputmatrix B (left TextArea in the GUI
  100. * @return matrix A substracted by matrix B
  101. */
  102. public double[][] matrixSubstraction(double[][] matrixA, double[][] matrixB) {
  103. if (checkIfMatriciesAreTheSameDimension(matrixA, matrixB)) {
  104. double[][] result = new double[matrixA.length][matrixA[0].length];
  105. for (int rows = 0; rows < matrixA.length; rows++) {
  106. for (int colums = 0; colums < matrixA[0].length; colums++) {
  107. result[rows][colums] = matrixA[rows][colums] - matrixB[rows][colums];
  108. }
  109. }
  110. return result;
  111. } else {
  112. throw new IllegalArgumentException("Matricies need to have the same Dimensions");
  113. }
  114. }
  115. /**
  116. * Transposes the Input Matrix. Swaps rows with colums.
  117. *
  118. * @param matrixA The Inputmatrix A wich will be Transposed
  119. * @return The Transposed matrix of matrix A
  120. */
  121. public double[][] matrixTransponation(double[][] matrixA) {
  122. if (matrixA == null) {
  123. throw new IllegalArgumentException("Matricies need to have the same Dimensions");
  124. }
  125. if (matrixA.length == 0){
  126. throw new IllegalArgumentException("Matricies need to have the same Dimensions");
  127. }
  128. if(matrixA[0] == null){
  129. throw new IllegalArgumentException("Matricies need to have the same Dimensions");
  130. }
  131. int columCountResult = matrixA.length;
  132. int rowCountResult = matrixA[0].length;
  133. double[][] result = new double[rowCountResult][columCountResult];
  134. for (int row = 0; row < rowCountResult; row++) {
  135. for (int colum = 0; colum < columCountResult; colum++) {
  136. result[row][colum] = matrixA[colum][row];
  137. }
  138. }
  139. return result;
  140. }
  141. /**
  142. * Calculates the Determinant of a Matrix.
  143. *
  144. * @param matrixA The Inputmatrix
  145. * @return The Determinant of the Matrix A
  146. */
  147. public double calcDeterminat(double[][] matrixA) {
  148. // checking if a Determinant can be calculated.
  149. double result = 0.0;
  150. if (checkIfMatrixIsQuadradtic(matrixA)) {
  151. if (getMatrixRowCount(matrixA) == 2) {
  152. result = calc2by2Determinant(matrixA);
  153. } else if (getMatrixRowCount(matrixA) == 3) {
  154. result = calc3by3Determinant(matrixA);
  155. } else {
  156. throw new IllegalArgumentException("Matrix is not 2 by 2 or 3 by 3");
  157. }
  158. } else {
  159. throw new IllegalArgumentException("Matrix must be Quadratic");
  160. }
  161. return result;
  162. }
  163. private double calc3by3Determinant(double[][] matrixA) {
  164. double result = matrixA[0][0] * matrixA[1][1] * matrixA[2][2] + matrixA[0][1] * matrixA[1][2] * matrixA[2][0]
  165. + matrixA[0][2] * matrixA[1][0] * matrixA[2][1] - matrixA[0][0] * matrixA[1][2] * matrixA[2][1]
  166. - matrixA[0][1] * matrixA[1][0] * matrixA[2][2] - matrixA[0][2] * matrixA[1][1] * matrixA[2][0];
  167. return result;
  168. }
  169. private double calc2by2Determinant(double[][] matrixA) {
  170. double result = matrixA[0][0] * matrixA[1][1] - matrixA[0][1] * matrixA[1][0];
  171. return result;
  172. }
  173. private int getMatrixRowCount(double[][] matrixA) {
  174. return matrixA.length;
  175. }
  176. private boolean checkIfMatrixIsQuadradtic(double[][] matrixA) {
  177. if (matrixA == null) {
  178. return false;
  179. }
  180. if (matrixA[0] == null) {
  181. return false;
  182. }
  183. if (matrixA.length == matrixA[0].length) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. }