Advertisement
Guest User

matrix

a guest
Oct 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. public class Matrix {
  2. double[]data;
  3. int rows;
  4. int cols;
  5.  
  6. Matrix(int rows, int cols){
  7. this.rows = rows;
  8. this.cols = cols;
  9. data = new double[rows*cols];
  10. }
  11.  
  12. Matrix(double[][] d) {
  13. this.rows = d.length;
  14. int max = 0;
  15. for (int i = 0; i < rows; i++) {
  16. int len = d[i].length;
  17. if (len > max) max = len;
  18. }
  19. this.cols = max;
  20. data = new double[rows*cols];
  21.  
  22. for (int i = 0; i < rows; i++) {
  23. for (int j = 0; j < cols; j++) {
  24. if (j < d[i].length) {
  25. data[i*cols + j] = d[i][j];
  26. }
  27. else {
  28. data[i*cols + j] = 0;
  29. }
  30. }
  31. }
  32. }
  33.  
  34. double[][] asArray() {
  35. double[][] returnArray = new double[rows][cols];
  36. for (int i = 0; i < rows; i++) {
  37. for (int j = 0; j < cols; j++) {
  38. returnArray[i][j] = data[i*cols + j];
  39. }
  40. }
  41. return returnArray;
  42. }
  43.  
  44. double get(int r, int c) {
  45. return data[r*cols + c];
  46. }
  47.  
  48. void set(int r, int c, double value) {
  49. data[r*cols + c] = value;
  50. }
  51.  
  52. public String toString(){
  53. StringBuilder buf = new StringBuilder();
  54. buf.append("[");
  55. for(int i=0; i<rows; i++) {
  56. buf.append("[");
  57. for (int j = 0; j < cols; j++) {
  58. buf.append(Double.toString(data[i * cols + j]));
  59.  
  60. if (j + 1 < cols) buf.append(", ");
  61. else buf.append("]");
  62. }
  63. if (i + 1 < rows) buf.append(", ");
  64. }
  65. buf.append("]");
  66. return buf.toString();
  67. }
  68.  
  69. void reshape(int newRows,int newCols){
  70. if(rows*cols != newRows*newCols)
  71. throw new RuntimeException(String.format("%d x %d matrix can't be reshaped to %d x %d",rows,cols,newRows,newCols));
  72. else {
  73. this.rows = newRows;
  74. this.cols = newCols;
  75. }
  76. }
  77.  
  78. int[] shape(){
  79. int [] tab = new int[2];
  80. tab[0] = rows;
  81. tab[1] = cols;
  82. return tab;
  83. }
  84.  
  85. Matrix add(Matrix m) {
  86. if(rows != m.rows && cols != m.cols)
  87. throw new RuntimeException(String.format("Matrixes have different shapes"));
  88. else {
  89. Matrix temp = new Matrix(rows, cols);
  90. for (int i = 0; i < rows; i++) {
  91. for (int j = 0; j < cols; j++) {
  92. temp.data[i * cols + j] = this.get(i, j) + m.get(i, j);
  93. }
  94. }
  95. return temp;
  96. }
  97. }
  98.  
  99. Matrix sub(Matrix m) {
  100. if(rows != m.rows && cols != m.cols)
  101. throw new RuntimeException(String.format("Matrixes have different shapes"));
  102. else {
  103. Matrix temp = new Matrix(rows, cols);
  104. for (int i = 0; i < rows; i++) {
  105. for (int j = 0; j < cols; j++) {
  106. temp.data[i * cols + j] = this.get(i, j) - m.get(i, j);
  107. }
  108. }
  109. return temp;
  110. }
  111. }
  112.  
  113. Matrix mul(Matrix m) {
  114. if(rows != m.rows && cols != m.cols)
  115. throw new RuntimeException(String.format("Matrixes have different shapes"));
  116. else {
  117. Matrix temp = new Matrix(rows, cols);
  118. for (int i = 0; i < rows; i++) {
  119. for (int j = 0; j < cols; j++) {
  120. temp.data[i * cols + j] = this.get(i, j) * m.get(i, j);
  121. }
  122. }
  123. return temp;
  124. }
  125. }
  126.  
  127. Matrix div(Matrix m) {
  128. if(rows != m.rows && cols != m.cols)
  129. throw new RuntimeException(String.format("Matrixes have different shapes"));
  130. else {
  131. Matrix temp = new Matrix(rows, cols);
  132. for (int i = 0; i < rows; i++) {
  133. for (int j = 0; j < cols; j++) {
  134. temp.data[i * cols + j] = this.get(i, j) / m.get(i, j);
  135. }
  136. }
  137. return temp;
  138. }
  139. }
  140.  
  141. Matrix add(double w) {
  142. for (int i = 0; i < rows*cols; i++) this.data[i] += w;
  143. return this;
  144. }
  145.  
  146. Matrix sub(double w) {
  147. for (int i = 0; i < rows*cols; i++) this.data[i] -= w;
  148. return this;
  149. }
  150.  
  151. Matrix mul(double w) {
  152. for (int i = 0; i < rows*cols; i++) this.data[i] *= w;
  153. return this;
  154. }
  155.  
  156. Matrix div(double w) {
  157. for (int i = 0; i < rows*cols; i++) this.data[i] /= w;
  158. return this;
  159. }
  160.  
  161.  
  162. public static void main(String[] args) {
  163. double[][] d = {
  164. {1,2,3},
  165. {4,5},
  166. {6},
  167. {40,50,60},
  168. };
  169.  
  170. Matrix m = new Matrix(d);
  171. //double[][] array = m.asArray();
  172.  
  173. // TESTY
  174. System.out.println(m.get(2,2));
  175. m.set(2,2, 15.5);
  176. System.out.println(m.get(2,2));
  177.  
  178. String s = m.toString();
  179. System.out.println(s);
  180.  
  181. int [] shape = m.shape();
  182. System.out.printf("Macierz ma wymiary %d x %d\n", shape[0], shape[1]);
  183.  
  184. m.reshape(2,6);
  185. String s2 = m.toString();
  186. System.out.println(s2);
  187.  
  188. shape = m.shape();
  189. System.out.printf("Macierz ma wymiary %d x %d\n", shape[0], shape[1]);
  190.  
  191. double[][] t = {
  192. {1,2},
  193. {3,4},
  194. };
  195.  
  196. Matrix d1 = new Matrix(t);
  197. Matrix d2 = new Matrix(t);
  198.  
  199. int skalar = 5;
  200.  
  201. System.out.println("\nDodawanie macierzy:");
  202. Matrix result1 = d1.add(d2);
  203. s = result1.toString();
  204. System.out.println(s);
  205.  
  206. System.out.println("\nOdejmowanie macierzy:");
  207. Matrix result2 = d1.sub(d2);
  208. s = result2.toString();
  209. System.out.println(s);
  210.  
  211. System.out.println("\nMnożenie elementów macierzy:");
  212. Matrix result3 = d1.mul(d2);
  213. s = result3.toString();
  214. System.out.println(s);
  215.  
  216. System.out.println("\nDzielenie elementów macierzy:");
  217. Matrix result4 = d1.div(d2);
  218. s = result4.toString();
  219. System.out.println(s);
  220.  
  221. System.out.printf("\nDodawanie skalara %d do macierzy:\n", skalar);
  222. Matrix m5 = new Matrix(t);
  223. Matrix result5 = m5.add(skalar);
  224. s = result5.toString();
  225. System.out.println(s);
  226.  
  227. System.out.printf("\nOdejmowanie skalara %d od macierzy:\n", skalar);
  228. Matrix m6 = new Matrix(t);
  229. Matrix result6 = m6.sub(skalar);
  230. s = result6.toString();
  231. System.out.println(s);
  232.  
  233. System.out.printf("\nMnożenie elementów macierzy przez skalar %d:\n", skalar);
  234. Matrix m7 = new Matrix(t);
  235. Matrix result7 = m7.mul(skalar);
  236. s = result7.toString();
  237. System.out.println(s);
  238.  
  239. System.out.printf("\nDzielenie elementów macierzy przez skalar %d:\n", skalar);
  240. Matrix m8 = new Matrix(t);
  241. Matrix result8 = m8.div(skalar);
  242. s = result8.toString();
  243. System.out.println(s);
  244.  
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement