Advertisement
Guest User

Matrix.java

a guest
Feb 13th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. //
  2. // Matrix.java
  3. // This program is intended for a debugging exercise and was expressly
  4. // designed not to work. If you're my student you have permission to
  5. // use this code for the assignment in which it was given. Don't expect
  6. // it to work out of the box.
  7. // Copyright (c) 2013-2016 cmagnus. All rights reserved.
  8. //
  9.  
  10. //package Debugging;
  11.  
  12. public class Matrix {
  13. private static int MAXSIZE = 99;
  14. private Vect[] m = new Vect[99];
  15. private int rows;
  16. private int columns;
  17.  
  18. // create default array of size 1
  19. public Matrix()
  20. {
  21. m[0] = new Vect();
  22. rows = 1;
  23. columns = 1;
  24. }
  25.  
  26. // create an empty rxc matrix
  27. public Matrix(int r, int c)
  28. {
  29. rows = r;
  30. columns = c;
  31. int j = 0;
  32. for (int i = 0; i < c; i++)
  33. {
  34. // initialize matrix row
  35. m[i] = new Vect(c);
  36. }
  37. }
  38.  
  39. // create array with dimensions r c
  40. // use r and c to parse args into an rxc matrix
  41. public Matrix(int r, int c, float[] args)
  42. {
  43. rows = r;
  44. columns = c;
  45.  
  46. for (int i = 0; i < r; i++)
  47. {
  48.  
  49. // loop through row, setting column values
  50. for (int j = 0; j < c; j++)
  51. {
  52. m[i].setValue(i, args[i*c + j]);
  53. }
  54. }
  55. }
  56.  
  57. // returns number of rows in Matrix
  58. public int getRows()
  59. {
  60. return(rows);
  61. }
  62.  
  63. // returns number of Columns in Matrix
  64. public int getColumns()
  65. {
  66. return(columns);
  67. }
  68.  
  69. // sets value at (r, c) to value
  70. public void setValue(int r, int c, float value)
  71. {
  72. m[r].setValue(c, value);
  73. }
  74.  
  75. // returns row i as a Vect
  76. public Vect getRow(int r)
  77. {
  78. return(m[r]);
  79. }
  80.  
  81. // returns column i as Vect
  82. public Vect getColumn(int c)
  83. {
  84. Vect columnVect = new Vect(rows);
  85. for (int i = 0; i < rows; i++)
  86. columnVect.setValue(i, m[i].getVect(c));
  87. return(columnVect);
  88. }
  89.  
  90. // returns product of Matrix multiplication
  91. public Matrix matrixMult(Matrix m2)
  92. {
  93. Matrix product = new Matrix(rows, rows);
  94. for (int i = 0; i < rows; i++)
  95. {
  96. for (int j = 0; j < columns; j++)
  97. {
  98. product.setValue(j, i, m[i].dotProduct(m2.getColumn(j)));
  99. }
  100. }
  101. return(product);
  102. }
  103.  
  104. // print matrix
  105. public void printMatrix()
  106. {
  107. for (int i = 0; i < rows; i++)
  108. m[i].printVect();
  109. System.out.println("");
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement