Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. public class Matrix {
  2.  
  3. private int rows; // number of rows in Matrix Object
  4. private int cols; // number of cols in Matrix Object
  5. private int intitialValue; // I have no fucking clue why I need this field
  6. private int[][] array; // array for matrix's values
  7. /**
  8. * Constructor to create a matrix of size rows by cols and to initialise
  9. * each element with the value of initialValue
  10. *
  11. * @param rows:
  12. * an integer value indicating the number of rows
  13. * @param cols:
  14. * an integer value indicating the number of columns
  15. * @param initialValue:
  16. * each element of the matrix is initialised to this value
  17. */
  18. public Matrix(int rows, int cols, int initialValue) {
  19. this.rows=rows;
  20. this.cols=cols;
  21. this.intitialValue=initialValue;
  22. }
  23.  
  24. /**
  25. * Constructor to create a matrix of size equivalent to the size of int[][]
  26. * initialData and to initialise each element with the values in
  27. * initialData.
  28. *
  29. * @param initialData:
  30. * An array of array of integers (int[][]) whose size determines
  31. * the size of the matrix. The array is cloned into the Matrix
  32. * object.
  33. */
  34. public Matrix(int[][] initialData) {
  35. this.array=initialData;
  36. }
  37.  
  38. /**
  39. * @return Returns the number of rows of the Matrix object.
  40. */
  41. public int getRows() {
  42. return array.length;
  43. }
  44.  
  45. /**
  46. * @return Returns the number of columns of the Matrix object.
  47. */
  48. public int getCols() {
  49. return array[0].length;
  50. }
  51.  
  52. /**
  53. * @param i:
  54. * data element row index starting from 0
  55. * @param j:
  56. * data element column index starting from 0
  57. * @return: Returns the element at row i and column j of the Matrix object
  58. */
  59. public int getData(int i, int j) {
  60. return array[i][j];
  61.  
  62. }
  63.  
  64. /**
  65. * @return Returns a 2-dimensional array of integers (int[][]) where the
  66. * matrix elements are stored
  67. */
  68. public int[][] getData() {
  69. return array;
  70.  
  71.  
  72. }
  73.  
  74. /**
  75. * @param matrix:
  76. * Matrix object to be added to the caller matrix
  77. * @return Returns a new matrix object which is the sum of self and the
  78. * parameter matrix. Returns null if dimensions do not match.
  79. */
  80. public Matrix add(Matrix matrix) {
  81. int[][] c = new int[array.length][array[0].length];
  82.  
  83. for(int i=0;i<getRows ();i++){
  84. for(int j=0;j<getCols();j++){
  85. c[i][j] = array[i][j] + matrix.getData (i,j);
  86.  
  87. }
  88. }
  89. return new Matrix (c);
  90. }
  91.  
  92. /**
  93. * @param matrix:
  94. * Matrix object to be subtracted from the caller matrix
  95. * @return Returns a new Matrix object which is the difference of self and
  96. * the parameter matrix. Returns null if dimensions do not match.
  97. */
  98. public Matrix sub(Matrix matrix) {
  99. int[][] c = new int[array.length][array[0].length];
  100.  
  101. for(int i=0;i<getRows ();i++){
  102. for(int j=0;j<getCols();j++){
  103. c[i][j] = array[i][j] - matrix.getData (i,j);
  104.  
  105. }
  106. }
  107. return new Matrix (c);
  108.  
  109. }
  110.  
  111. /**
  112. * @return Returns the transpose of the matrix as a new Matrix object
  113. */
  114. public Matrix transpose() {
  115. int[][] c = new int[array[0].length][array.length];
  116.  
  117. for(int i=0;i<getRows ();i++){
  118. for(int j=0;j<getCols();j++){
  119. c[j][i] = array[i][j];
  120.  
  121. }
  122. }
  123. return new Matrix (c);
  124. }
  125.  
  126. /**
  127. * @param matrix:
  128. * Matrix object to be concatenated to the caller matrix.
  129. * @return: Returns the concatenation of the two matrices as a new Matrix
  130. * object. Returns null if the number of columns does not match.
  131. */
  132. public Matrix concat(Matrix matrix) {
  133. int[][] c = new int[array.length+matrix.getRows ()][array[0].length];
  134.  
  135. System.arraycopy(array, 0, c, 0, array.length);
  136. System.arraycopy(matrix.getData (), 0, c, array.length, matrix.getData ().length);
  137.  
  138. return new Matrix (c);
  139. }
  140.  
  141.  
  142.  
  143. /**
  144. * @param row:
  145. * row index starting from 1
  146. * @return: Returns an array (int[]) containing the requested row. Returns null if the row does not exist.
  147. */
  148. public int[] getRow(int row) {
  149. return null;
  150.  
  151. }
  152.  
  153. /**
  154. * @param col:
  155. * column index starting from 1
  156. * @return : Returns an array (int[]) containing the requested column. Returns null if the row does not exist.
  157. */
  158. public int[] getCol(int col) {
  159. return null;
  160.  
  161. }
  162.  
  163. /**
  164. * @return : Returns a String representation of the Matrix.
  165. */
  166. public String toString(){
  167. String result ="";
  168. String ls = System.getProperty("line.separator");
  169.  
  170. for (int i = 0; i < this.getRows(); i++){
  171. for (int j = 0; j < this.getCols(); j++)
  172. result = result.concat(Integer.toString(this.getData(i,j)) + " ");
  173. result = result.concat(ls);
  174. }
  175. return result;
  176. }
  177.  
  178. public static void main(String[] args){
  179. int[][] dataA = {{2, 2},{3, 3}};
  180. int[][] dataB = {{1, 1},{1, 1}};
  181.  
  182. Matrix matrixA = new Matrix(dataA);
  183. Matrix matrixB = new Matrix(dataB);
  184.  
  185. System.out.println("A+B");
  186. System.out.println(matrixA.add(matrixB).toString());
  187. System.out.println("A-B");
  188. System.out.println(matrixA.sub(matrixB).toString());
  189. System.out.println("transpose(A)");
  190. System.out.println(matrixA.transpose().toString());
  191. System.out.println("A concat B");
  192. System.out.println(matrixA.concat(matrixB).toString());
  193. System.out.println("B concat A");
  194. System.out.println(matrixB.concat(matrixA).toString());
  195. System.out.println("(A concat B) + (B concat A)");
  196. System.out.println(matrixA.concat(matrixB).add(matrixB.concat(matrixA)).toString());
  197. System.out.println("A+B-B");
  198. System.out.println(matrixA.add(matrixB).sub(matrixB).toString());
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement