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.

227 lines
8.6 KiB

  1. package com.ugsbo.matrixcalc;
  2. /**
  3. * Contains all basic matrix math calculations.
  4. */
  5. public class MatrixCalcMath {
  6. /**
  7. * Mutliplys matrixA and matrixB.
  8. *
  9. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  10. * @param matrixB The Inputmatrix B (left TextArea in the GUI)
  11. * @return The Matrixproduct of the matricies A and B
  12. */
  13. public double[][] matrixMultiplication(double[][] matrixA, double[][] matrixB) {
  14. if (checkIfMatriciesAreLinked(matrixA, matrixB)) {
  15. int rowOfResultMatrix = matrixA.length;
  16. int columOfResultMatrix = matrixB[0].length;
  17. int ColumsOfMatA = matrixA[0].length;
  18. double[][] result = new double[rowOfResultMatrix][columOfResultMatrix];
  19. for (int rowResult = 0; rowResult < rowOfResultMatrix; rowResult++) {
  20. for (int columResult = 0; columResult < columOfResultMatrix; columResult++) {
  21. for (int columOfA = 0; columOfA < ColumsOfMatA; columOfA++) {
  22. result[rowResult][columResult] += matrixA[rowResult][columOfA] * matrixB[columOfA][columResult];
  23. }
  24. }
  25. }
  26. return result;
  27. } else {
  28. throw new IllegalArgumentException("Matricies must be linked");
  29. }
  30. }
  31. /**
  32. * checks if matrixA and matrixB are linked to know if it is possible to
  33. * multiply them. If they are linked it is possible.
  34. *
  35. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  36. * @param matrixB The Inputmatrix B (left TextArea in the GUI)
  37. * @return true if you can Muliply A with B false if not.
  38. */
  39. public boolean checkIfMatriciesAreLinked(double[][] matrixA, double[][] matrixB) {
  40. if (matrixA == null) {
  41. return false;
  42. }
  43. if (matrixA.length == 0) {
  44. return false;
  45. }
  46. if (matrixA[0].length == matrixB.length) {
  47. return true;
  48. }
  49. return false;
  50. }
  51. /**
  52. * Adds two matroices A and B. Adding matrix A to matrix B is the same as adding
  53. * B to A.
  54. *
  55. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  56. * @param matrixB The Inputmatrix B (left TextArea in the GUI)
  57. * @return The Matrixsum of matrix A and matrix B
  58. */
  59. public double[][] matrixAddition(double[][] matrixA, double[][] matrixB) {
  60. if (checkIfMatriciesAreTheSameDimension(matrixA, matrixB)) {
  61. double[][] result = new double[matrixA.length][matrixA[0].length];
  62. for (int rows = 0; rows < matrixA.length; rows++) {
  63. for (int colums = 0; colums < matrixA[0].length; colums++) {
  64. result[rows][colums] = matrixA[rows][colums] + matrixB[rows][colums];
  65. }
  66. }
  67. return result;
  68. } else {
  69. throw new IllegalArgumentException("Matricies need to have the same Dimensions");
  70. }
  71. }
  72. /**
  73. * In order to adding two Matricies they must have the same Dimensions. This
  74. * Methode checks if this is the case.
  75. *
  76. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  77. * @param matrixB The Inputmatrix B (left TextArea in the GUI)
  78. * @return true if the Dimensions of Matrix A equals the Dimensions Matrix B
  79. */
  80. public boolean checkIfMatriciesAreTheSameDimension(double[][] matrixA, double[][] matrixB) {
  81. if (matrixA == null || matrixA.length == 0) {
  82. return false;
  83. }
  84. if (matrixA[0] == null) {
  85. return false;
  86. }
  87. if (matrixA.length == matrixB.length && matrixA[0].length == matrixB[0].length) {
  88. return true;
  89. } else {
  90. return false;
  91. }
  92. }
  93. /**
  94. * Substracts matrix A by the matrix B. Substaction for Matrices is just the
  95. * substraction of each component with thier coorsponding component.
  96. *
  97. * @param matrixA The Inputmatrix A (right TextArea in the GUI)
  98. * @param matrixB The Inputmatrix B (left TextArea in the GUI
  99. * @return matrix A substracted by matrix B
  100. */
  101. public double[][] matrixSubstraction(double[][] matrixA, double[][] matrixB) {
  102. if (checkIfMatriciesAreTheSameDimension(matrixA, matrixB)) {
  103. double[][] result = new double[matrixA.length][matrixA[0].length];
  104. for (int rows = 0; rows < matrixA.length; rows++) {
  105. for (int colums = 0; colums < matrixA[0].length; colums++) {
  106. result[rows][colums] = matrixA[rows][colums] - matrixB[rows][colums];
  107. }
  108. }
  109. return result;
  110. } else {
  111. throw new IllegalArgumentException("Matricies need to have the same Dimensions");
  112. }
  113. }
  114. /**
  115. * Transposes the Input Matrix. Swaps rows with colums.
  116. *
  117. * @param matrixA The Inputmatrix A wich will be Transposed
  118. * @return The Transposed matrix of matrix A
  119. */
  120. public double[][] matrixTransponation(double[][] matrixA) {
  121. if (matrixA == null) {
  122. throw new IllegalArgumentException("Matricies need to have the same Dimensions");
  123. }
  124. if (matrixA.length == 0) {
  125. throw new IllegalArgumentException("Matricies need to have the same Dimensions");
  126. }
  127. if (matrixA[0] == null) {
  128. throw new IllegalArgumentException("Matricies need to have the same Dimensions");
  129. }
  130. int columCountResult = matrixA.length;
  131. int rowCountResult = matrixA[0].length;
  132. double[][] result = new double[rowCountResult][columCountResult];
  133. for (int row = 0; row < rowCountResult; row++) {
  134. for (int colum = 0; colum < columCountResult; colum++) {
  135. result[row][colum] = matrixA[colum][row];
  136. }
  137. }
  138. return result;
  139. }
  140. /**
  141. * Calculates the Determinant of a Matrix.
  142. *
  143. * @param matrixA The Inputmatrix
  144. * @return The Determinant of the Matrix A
  145. */
  146. public double calcDeterminat(double[][] matrixA) throws IllegalArgumentException{
  147. // checking if a Determinant can be calculated.
  148. double result = 0.0;
  149. if (checkIfMatrixIsQuadradtic(matrixA)) {
  150. if (getMatrixRowCount(matrixA) == 2) {
  151. result = calc2by2Determinant(matrixA);
  152. } else if (getMatrixRowCount(matrixA) == 3) {
  153. result = calc3by3Determinant(matrixA);
  154. } else {
  155. throw new IllegalArgumentException("Matrix is not 2 by 2 or 3 by 3");
  156. }
  157. } else {
  158. throw new IllegalArgumentException("Matrix must be Quadratic");
  159. }
  160. return result;
  161. }
  162. /**
  163. * Calculates the Determinat of an three by three Matrix.
  164. *
  165. * @param matrixA The Inputmatrix form wich the Determinat will be calculated
  166. * @return the Determinant of the Matrix
  167. */
  168. private double calc3by3Determinant(double[][] matrixA) {
  169. double result = matrixA[0][0] * matrixA[1][1] * matrixA[2][2] + matrixA[0][1] * matrixA[1][2] * matrixA[2][0]
  170. + matrixA[0][2] * matrixA[1][0] * matrixA[2][1] - matrixA[0][0] * matrixA[1][2] * matrixA[2][1]
  171. - matrixA[0][1] * matrixA[1][0] * matrixA[2][2] - matrixA[0][2] * matrixA[1][1] * matrixA[2][0];
  172. return result;
  173. }
  174. /**
  175. * Calculates the Determinat of an two by two Matrix.
  176. *
  177. * @param matrixA The Inputmatrix form wich the Determinat will be calculated
  178. * @return the Determinant of the Matrix
  179. */
  180. private double calc2by2Determinant(double[][] matrixA) {
  181. double result = matrixA[0][0] * matrixA[1][1] - matrixA[0][1] * matrixA[1][0];
  182. return result;
  183. }
  184. /**
  185. * Returns the Number of rows of a Matrix.
  186. *
  187. * @param matrixA the Inputmatrix form wich the rows will be counted
  188. * @return Number of rows
  189. */
  190. private int getMatrixRowCount(double[][] matrixA) {
  191. return matrixA.length;
  192. }
  193. /**
  194. * Checks if the rows and colums of an Matrix are equal. If they are equal the
  195. * Matrix is Quadratic and the function will return true.
  196. *
  197. * @param matrixA the Inputmatrix for wich the rows and colums will be compared
  198. * @return isQuadratic
  199. */
  200. private boolean checkIfMatrixIsQuadradtic(double[][] matrixA) {
  201. if (matrixA == null) {
  202. return false;
  203. }
  204. if (matrixA[0] == null) {
  205. return false;
  206. }
  207. if (matrixA.length == matrixA[0].length) {
  208. return true;
  209. }
  210. return false;
  211. }
  212. }