fork download
  1.  
  2. class Matrix {
  3.  
  4. private int a[][];
  5.  
  6. public Matrix(int n, int m) {
  7. a = new int[n][m];
  8. }
  9.  
  10. public Matrix(int a[][]) {
  11. this.a = a;
  12. }
  13.  
  14. public int getVerticalSize() {
  15. return a.length;
  16. }
  17.  
  18. public int getHorizontalSize() {
  19. return a[0].length;
  20. }
  21.  
  22. public int getElement(int i, int j) {
  23. return a[i][j];
  24. }
  25.  
  26. public void setElement(int i, int j, int value) {
  27. a[i][j] = value;
  28. }
  29.  
  30. @Override
  31. public String toString() {
  32. String s = "\n";
  33. for (int[] vector : a) {
  34. for (int value : vector)
  35. s += value + " ";
  36. s += "\n";
  37. }
  38. return s;
  39. }
  40.  
  41. public static Matrix add(Matrix a, Matrix b) {
  42. int v = a.getVerticalSize();
  43. int h = a.getHorizontalSize();
  44. Matrix result = new Matrix(v, h);
  45.  
  46. for (int i = 0; i < h; i++) {
  47. for (int j = 0; j < v; j++) {
  48. int value = 0;
  49. value = a.getElement(i, j) + b.getElement(i, j);
  50. result.setElement(i, j, value);
  51. }
  52. }
  53. return result;
  54. }
  55.  
  56. public static Matrix subtract(Matrix a, Matrix b) {
  57. int v = a.getVerticalSize();
  58. int h = a.getHorizontalSize();
  59. Matrix result = new Matrix(v, h);
  60.  
  61. for (int i = 0; i < h; i++) {
  62. for (int j = 0; j < v; j++) {
  63. int value = 0;
  64. value = a.getElement(i, j) - b.getElement(i, j);
  65. result.setElement(i, j, value);
  66. }
  67. }
  68. return result;
  69. }
  70.  
  71. public static Matrix multiply(Matrix a, Matrix b) {
  72. int v = a.getVerticalSize();
  73. int h = b.getHorizontalSize();
  74. int temp = a.getHorizontalSize();
  75.  
  76. Matrix result = new Matrix(v, h);
  77.  
  78. for (int i = 0; i < v; i++)
  79. for (int j = 0; j < h; j++) {
  80. int value = 0;
  81. for (int k = 0; k < temp; k++) {
  82. value += a.getElement(i, k) * b.getElement(k, j);
  83. }
  84. result.setElement(i, j, value);
  85. }
  86. return result;
  87. }
  88.  
  89. public void transMatrix() {
  90. int x = a.length;
  91. int retA[][] = new int[x][x];
  92. for (int i = 0; i < x; i++) {
  93. for (int j = 0; j < x; j++) {
  94. retA[j][i] = a[i][j];
  95. }
  96. }
  97. for (int i = 0; i < a.length; i++) {
  98. System.arraycopy(retA[i], 0, a[i], 0, a.length);
  99. }
  100. }
  101.  
  102. public static void main(String[] args) {
  103.  
  104. int[][] a = {{-9, 1, 0}, {4, 1, 1}, {-2, 2, -1}};
  105. int[][] b = {{0, 0, 0}, {0, 2, 0}, {0, 0, 1}};
  106.  
  107. Matrix A = new Matrix(a);
  108. System.out.println("A" + A);
  109. System.out.println("Element [0,0] : " + A.getElement(0, 0));
  110.  
  111. Matrix B = new Matrix(b);
  112. B.setElement(0, 0, 1);
  113. System.out.println("Size of matrix B : " + B.getHorizontalSize() + " " + B.getVerticalSize() + B);
  114.  
  115. Matrix C;
  116. C = add(A, B);
  117. System.out.println("add " + C);
  118.  
  119. C = subtract(A, B);
  120. System.out.println("subtract " + C);
  121.  
  122. C = multiply(A, B);
  123. System.out.println("multiply " + C);
  124.  
  125. A.transMatrix();
  126. System.out.println("Transposed A : " + A);
  127.  
  128. }
  129. }
  130.  
Success #stdin #stdout 0.03s 320576KB
stdin
Standard input is empty
stdout
A
-9 1 0 
4 1 1 
-2 2 -1 

Element [0,0] : -9
Size of matrix B : 3 3
1 0 0 
0 2 0 
0 0 1 

add 
-8 1 0 
4 3 1 
-2 2 0 

subtract 
-10 1 0 
4 -1 1 
-2 2 -2 

multiply 
-9 2 0 
4 2 1 
-2 4 -1 

Transposed A : 
-9 4 -2 
1 1 2 
0 1 -1