Advertisement
Shabbyshab

Untitled

Feb 18th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. /**
  2. * Matrix Project : General Operations of Matrices
  3. *
  4. * @Rishabh Sud
  5. * @2/18/2019 */
  6. import java.io.*;
  7. import java.util.*;
  8. public class Matrix
  9. {
  10. double [][] mat;
  11. public int row;
  12. public int column;
  13. /**
  14. * Constructor w/ Paramters
  15. * @ param int a = # of Rows
  16. * @ param int b = # of Columns
  17. */
  18. public Matrix(int a, int b) {
  19. mat = new double [a][b];
  20. row = a;
  21. column = b;
  22. }
  23.  
  24. /**
  25. * Method assigns a particular value to the indicated index
  26. * @param int a: Row index
  27. * @param int b: Column index
  28. * @param double c : Value assigned to index
  29. */
  30. public void set(int a, int b, double c) throws MatrixException {
  31. if( a >= row || b >= column ) {
  32. throw new MatrixException ("God, you numbered the indices wrong");
  33. }
  34. if( a < 0 || b < 0 ) {
  35. throw new MatrixException ("N E G A T I V E I N D I C E S ARE FAKE NEWS");
  36. }
  37. mat[a][b] = c;
  38. }
  39.  
  40. /**
  41. * Method returns value of index
  42. * @param int a : Row index
  43. * @param int b : Column Index
  44. */
  45. public double get(int a, int b) throws MatrixException {
  46. if( a >= row || b >= column ) {
  47. throw new MatrixException ("God, you numbered the indices wrong");
  48. }
  49. if( a < 0 || b < 0 ) {
  50. throw new MatrixException ("N E G A T I V E I N D I C E S ARE FAKE NEWS");
  51. }
  52. double c = mat[a][b];
  53. return c;
  54. }
  55.  
  56. /**
  57. * Method adds two matrices (both have to be the same size)
  58. * @param Matrix a: Matrix 1
  59. * @param Matrix b: Matrix 2
  60. */
  61. public static Matrix add(Matrix a, Matrix b) throws MatrixException {
  62. if(a.row != b.row || a.column != b.column) {
  63. throw new MatrixException("Matrix Size Error");
  64. }
  65. if (a == null || b == null) {
  66. throw new MatrixException("Matrix Null Error");
  67. }
  68. Matrix sum = new Matrix (a.row, b.row);
  69. for (int i = 0; i < a.row; i++) {
  70. for (int j = 0; j < a.column; j++) {
  71. double z = a.get(i,j) + b.get(i,j);
  72. sum.set(i,j,z);
  73. }
  74. }
  75. return sum;
  76. }
  77.  
  78. /**
  79. * Method subtracts two matrices (both have to be the same size)
  80. * @param Matrix a: Matrix 1
  81. * @param Matrix b: Matrix 2
  82. */
  83. public static Matrix sub(Matrix a, Matrix b) throws MatrixException {
  84. if(a.row != b.row || a.column != b.column) {
  85. throw new MatrixException("Matrix Size Error");
  86. }
  87. if (a == null || b == null) {
  88. throw new MatrixException("Matrix Null Error");
  89. }
  90. Matrix diff = new Matrix (a.row, b.row);
  91. for (int i = 0; i < a.row; i++) {
  92. for (int j = 0; j <a.column; j++) {
  93. double z = a.get(i,j) + b.get(i,j);
  94. diff.set(i,j,z);
  95. }
  96. }
  97. return diff;
  98. }
  99.  
  100. /**
  101. * Method basically multiplies the entire matrix by a scalar value
  102. * @param Matrix a : Matrix 1
  103. * @param double b : Scalar Factor
  104. */
  105. public static Matrix mult (Matrix a, double b) throws MatrixException {
  106. if (a == null) {
  107. throw new MatrixException("Matrix Null Error");
  108. }
  109. Matrix result = new Matrix (a.row, a.column);
  110. for (int i = 0; i < a.row; i++) {
  111. for (int j = 0; j <a.column; j++) {
  112. double z = a.get(i,j)*b;
  113. result.set(i,j,z);
  114. }
  115. }
  116. return result;
  117. }
  118.  
  119. /**
  120. * Method multiplies a matrix with another matrix
  121. * @param Matrix a: Matrix 1
  122. * @param Matrix b: Matrix 2
  123. */
  124. public static Matrix mult(Matrix a, Matrix b) throws MatrixException {
  125. if (a == null || b == null) {
  126. throw new MatrixException("Matrix Null Error");
  127. }
  128. if (a.row != b.column) {
  129. throw new MatrixException("Cannot Multiply the Matrices");
  130. }
  131. Matrix result = new Matrix (a.row, b.column);
  132. double DotProd =0;
  133. double temp;
  134. for(int i = 0; i <a.row; i++) {
  135. for(int j = 0; j < b.column; j++){
  136. for(int k = 0; k < a.column; k++) {
  137. temp = a.get(i,k) * b.get(k,j);
  138. DotProd += temp;
  139. }
  140. result.set(i,j, DotProd);
  141. DotProd = 0;
  142. }
  143. }
  144. return result;
  145. }
  146. /**
  147. * Method Transposes, or inverses the matrix
  148. * @param Matrix a : Matrix 1
  149. */
  150. public static Matrix transpose (Matrix a) throws MatrixException {
  151. if(a == null) {
  152. throw new MatrixException("Matrix Null Error");
  153. }
  154. Matrix T = new Matrix(a.column,a.row);
  155. for (int i = 0; i<a.row; i++) {
  156. for (int j = 0; j<a.column; j++) {
  157. T.set(j,i,a.get(i,j));
  158. }
  159. }
  160. return T;
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement