fork(3) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Matrix {
  6.  
  7. int n, m;
  8. int[][] mainMatrix;
  9.  
  10. public Matrix(int n, int m)
  11. {
  12. this.n = n;
  13. this.m = m;
  14. this.mainMatrix = new int[this.n][this.m];
  15. }
  16.  
  17. public Matrix(int [][] paramMatrix)
  18. {
  19. this.n = paramMatrix.length;
  20. this.m = paramMatrix[0].length;
  21. this.mainMatrix = paramMatrix;
  22. }
  23.  
  24. public int getElement(int n, int m)
  25. {
  26. return this.mainMatrix[n][m];
  27. }
  28.  
  29. public void setElement(int n, int m, int value)
  30. {
  31. this.mainMatrix[n][m] = value;
  32. }
  33.  
  34. public int getVerticalLength()
  35. {
  36. return this.mainMatrix.length;
  37. }
  38.  
  39. public int getHorizontalLength()
  40. {
  41. return this.mainMatrix[0].length;
  42. }
  43.  
  44. public void fillRandomValues()
  45. {
  46. Random rand = new Random();
  47.  
  48. for(int i = 0; i < this.n; i++)
  49. {
  50. for(int j = 0; j < this.m; j++)
  51. {
  52. this.mainMatrix[i][j] = rand.nextInt(100);
  53. }
  54. }
  55. }
  56.  
  57. public void displayMatrix()
  58. {
  59. for(int i = 0; i < this.n; i++)
  60. {
  61. for(int j = 0; j < this.m; j++)
  62. {
  63. System.out.print(this.mainMatrix[i][j] + " ");
  64. }
  65. System.out.println();
  66. }
  67. }
  68.  
  69. public static int[][] transpone(int[][] paramMatrix)
  70. {
  71. int[][] tmpMatrix = new int[paramMatrix[0].length][paramMatrix.length];
  72. for(int i = 0; i < paramMatrix[0].length; i++)
  73. {
  74. for(int j = 0; j < paramMatrix.length; j++)
  75. {
  76. tmpMatrix[i][j] = paramMatrix[j][i];
  77. }
  78. }
  79. return tmpMatrix;
  80. }
  81.  
  82. public static Matrix transpone(Matrix paramMatrix)
  83. {
  84. Matrix tmpMatrix = new Matrix(paramMatrix.m, paramMatrix.n);
  85. for(int i = 0; i < paramMatrix.m; i++)
  86. {
  87. for(int j = 0; j < paramMatrix.n; j++)
  88. {
  89. tmpMatrix.setElement(i, j, paramMatrix.getElement(j, i));
  90. }
  91. }
  92. return tmpMatrix;
  93. }
  94.  
  95. public static Matrix add(Matrix first, Matrix second) throws NotEqualLengthsOfMatrixException {
  96. if(first.getVerticalLength() != second.getVerticalLength() ||
  97. first.getHorizontalLength() != second.getHorizontalLength())
  98. {
  99. throw new NotEqualLengthsOfMatrixException();
  100. }
  101. else {
  102. Matrix tmpMatrix = new Matrix(first.getVerticalLength(), second.getHorizontalLength());
  103. for (int i = 0; i < tmpMatrix.getHorizontalLength(); i++) {
  104. for(int j = 0; j < tmpMatrix.getVerticalLength(); j++){
  105. tmpMatrix.setElement(i, j, first.getElement(i, j) + second.getElement(i, j));
  106. }
  107. }
  108. return tmpMatrix;
  109. }
  110. }
  111.  
  112. public static Matrix subtract (Matrix first, Matrix second) throws NotEqualLengthsOfMatrixException {
  113. if(first.getVerticalLength() != second.getVerticalLength() ||
  114. first.getHorizontalLength() != second.getHorizontalLength())
  115. throw new NotEqualLengthsOfMatrixException();
  116. else {
  117. Matrix tmpMatrix = new Matrix(first.getVerticalLength(), second.getHorizontalLength());
  118. for (int i = 0; i < tmpMatrix.getHorizontalLength(); i++) {
  119. for(int j = 0; j < tmpMatrix.getVerticalLength(); j++){
  120. tmpMatrix.setElement(i, j, first.getElement(i, j) - second.getElement(i, j));
  121. }
  122. }
  123. return tmpMatrix;
  124. }
  125. }
  126. public static Matrix multiply (Matrix first, Matrix second) throws NotEqualLengthsOfMatrixException {
  127. if(first.getHorizontalLength() != second.getVerticalLength())
  128. throw new NotEqualLengthsOfMatrixException();
  129. else {
  130. Matrix tmpMatrix;
  131. int n = first.getVerticalLength();
  132. int m = second.getHorizontalLength();
  133. int o = second.getVerticalLength();
  134. int[][] tmpArr = new int[n][m];
  135. for(int i = 0; i < n; i++){
  136. for(int j = 0; j < m; j++){
  137. for (int k = 0; k < o; k++) {
  138. tmpArr[i][j] += first.getElement(i, k) * second.getElement(k, j);
  139. }
  140. }
  141. }
  142. tmpMatrix = new Matrix(tmpArr);
  143. return tmpMatrix;
  144. }
  145. }
  146. }
  147.  
  148.  
  149. class NotEqualLengthsOfMatrixException extends Exception {}
  150.  
  151.  
  152. class MatrixMain {
  153.  
  154. public static void main(String[] args) throws java.lang.Exception
  155. {
  156. //int[][] A = {{-1, 1, -1}, {1, -1, 1}, {-1, 1, -1}};
  157. //int[][] B = {{1, -1, 1}, {-1, 1, -1}, {1, -1, 1}};
  158.  
  159. int[][] A = {{33,34,12},
  160. {33,19,10},
  161. {12,14,17},
  162. {84,24,51},
  163. {43,71,21}};
  164.  
  165. int[][] B = {{10,11,34,55},
  166. {33,45,17,81},
  167. {45,63,12,16}};
  168.  
  169.  
  170.  
  171.  
  172. Matrix x = new Matrix(A);
  173. Matrix y = new Matrix(B);
  174. x.displayMatrix();
  175. y.displayMatrix();
  176.  
  177. Matrix mM = Matrix.multiply(x, y);
  178.  
  179. mM.displayMatrix();
  180.  
  181. }
  182. }
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
33 34 12 
33 19 10 
12 14 17 
84 24 51 
43 71 21 
10 11 34 55 
33 45 17 81 
45 63 12 16 
1992 2649 1844 4761 
1407 1848 1565 3514 
1347 1833 850 2066 
3927 5217 3876 7380 
3718 4991 2921 8452