|
| 1 | +// This file contains helper functions for the matrix operations necessary for the graphics to be generated. |
| 2 | + |
| 3 | +package trick.matrixOps; |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +public class MatrixOps { // Defines the matrix operation class. |
| 8 | + |
| 9 | + public static void printMatrix(double M[][]) { // Prints out the matrix. |
| 10 | + int M_rows = M.length; |
| 11 | + int M_cols = M[0].length; |
| 12 | + for (int i = 0; i < M_rows; i++) { |
| 13 | + for (int j = 0; j < M_cols; j++) |
| 14 | + System.out.print(" " + M[i][j]); |
| 15 | + System.out.println(); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + public static void printDVector(double V[]) { // Prints out a vector passed as a double. |
| 20 | + System.out.print("("); |
| 21 | + for (int i = 0; i < V.length; i++) { |
| 22 | + System.out.print(" " + V[i]); |
| 23 | + } |
| 24 | + System.out.println(")"); |
| 25 | + } |
| 26 | + |
| 27 | + public static void printIVector(int V[]) { // Prints out a vector passed as an integer. |
| 28 | + System.out.print("("); |
| 29 | + for (int i = 0; i < V.length; i++) { |
| 30 | + System.out.print(" " + V[i]); |
| 31 | + } |
| 32 | + System.out.println(")"); |
| 33 | + } |
| 34 | + |
| 35 | + public static void MtimesM( double R[][], double A[][], double B[][]) { // Multiplies two matrices together. |
| 36 | + int A_rows = A.length; |
| 37 | + int A_cols = A[0].length; |
| 38 | + int B_rows = B.length; |
| 39 | + int B_cols = B[0].length; |
| 40 | + int R_rows = R.length; |
| 41 | + int R_cols = R[0].length; |
| 42 | + |
| 43 | + if (A_cols != B_rows) { // Checks if the matrices can be multiplied. |
| 44 | + System.out.println( "\nNot possible to multiply matrixes,"); |
| 45 | + System.out.println("where the first has " + A_cols + " columns,"); |
| 46 | + System.out.println("and the second has " + B_rows + "rows."); |
| 47 | + return; |
| 48 | + } |
| 49 | + if ((R_rows != A_rows) || (R_cols != B_cols)) { // Checks if the defined result matrix is the wrong size. |
| 50 | + System.out.println( "\n Result matrix is wrong size."); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + for (int i = 0; i < A_rows; i++) { // Multiplies the two matrices together. |
| 55 | + for (int j = 0; j < B_cols; j++) { |
| 56 | + R[i][j] = 0.0; |
| 57 | + for (int k = 0; k < B_rows; k++) |
| 58 | + R[i][j] += A[i][k] * B[k][j]; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static void MtimesV( double R[], double M[][], double V[]) { // Multiplies a matrix with a vector. |
| 64 | + int M_rows = M.length; |
| 65 | + int M_cols = M[0].length; |
| 66 | + int V_rows = V.length; |
| 67 | + |
| 68 | + if (M_cols != V_rows) { // Checks if the matrix and the vector can be multiplied together. |
| 69 | + System.out.println( "\nNot possible to multiply matrix and vector,"); |
| 70 | + System.out.println( "where the matrix has " + M_cols + " columns,"); |
| 71 | + System.out.println("and the vector has " + V_rows + " elements."); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (R.length != M.length) { // Checks if the defined result vector is the wrong size. |
| 76 | + System.out.println( "\n Result vector is wrong size."); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + for (int i =0; i < M_rows ; i++) { // Multiplies the vector with the matrix. |
| 81 | + R[i] = 0.0; |
| 82 | + for (int j =0; j < M_cols ; j++) { |
| 83 | + R[i] += M[i][j] * V[j]; |
| 84 | + } |
| 85 | + } |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + public static void VplusV(double R[], double A[], double B[]) { // Adds two matrices together. |
| 90 | + if ((A.length != B.length) || (A.length != R.length)) { |
| 91 | + System.out.println( "\n MatrixOps::VplusV : Vectors are not the same size."); |
| 92 | + } |
| 93 | + for (int i=0; i<A.length ; i++) { |
| 94 | + R[i] = A[i] + B[i]; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public static void VminusV(double R[], double A[], double B[]) { // Subtracts two matrices together. |
| 99 | + if ((A.length != B.length) || (A.length != R.length)) { |
| 100 | + System.out.println( "\n MatrixOps::VminusV : Vectors are not the same size."); |
| 101 | + return; |
| 102 | + } |
| 103 | + for (int i=0; i<A.length ; i++) { |
| 104 | + R[i] = A[i] - B[i]; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public static void VcrossV(double R[], double A[], double B[]) { // Finds the cross product of two matrices. |
| 109 | + if ((R.length != 3) || (A.length != 3) || (B.length != 3)) { |
| 110 | + System.out.println( "\n MatrixOps::VcrossV : All vector args must be length 3."); |
| 111 | + return; |
| 112 | + } |
| 113 | + R[0] = A[1] * B[2] - A[2] * B[1]; |
| 114 | + R[1] = A[2] * B[0] - A[0] * B[2]; |
| 115 | + R[2] = A[0] * B[1] - A[1] * B[0]; |
| 116 | + } |
| 117 | + |
| 118 | + public static double VdotV(double A[], double B[]) { // Finds the dot product of two matrices. |
| 119 | + if (A.length != B.length) { |
| 120 | + System.out.println( "\n MatrixOps::VdotV : Vectors are not the same size."); |
| 121 | + return 0.0; |
| 122 | + } |
| 123 | + double R = 0.0; |
| 124 | + for (int i=0; i<A.length ; i++) { |
| 125 | + R += A[i] * B[i]; |
| 126 | + } |
| 127 | + return R; |
| 128 | + } |
| 129 | + |
| 130 | + public static void Vscale(double R[], double A[], double S) { // Scales a vector (Multiplies by a given number). |
| 131 | + if (A.length != R.length) { |
| 132 | + System.out.println( "\n MatrixOps::Vscale : Input and output vectors are not the same size."); |
| 133 | + return; |
| 134 | + } |
| 135 | + for (int i=0; i<A.length ; i++) { |
| 136 | + R[i] = A[i] * S; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public static double Vmagnitude (double V[]) { // Returns the magnitude of a vector (Length of a vector). |
| 141 | + double S = 0; |
| 142 | + for (int i =0; i < V.length ; i++) { |
| 143 | + S += V[i]*V[i]; |
| 144 | + } |
| 145 | + return Math.sqrt(S); |
| 146 | + } |
| 147 | + |
| 148 | +} |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
0 commit comments