|
| 1 | +/** |
| 2 | +* Matrix data-type. |
| 3 | +* |
| 4 | +* @author Kyler Smith, 2017 |
| 5 | +*/ |
| 6 | + |
| 7 | + |
| 8 | +public class Matrix { |
| 9 | + |
| 10 | + public static void main(String[] args) { |
| 11 | + |
| 12 | + int[][] data1 = new int[0][0]; |
| 13 | + int[][] data2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
| 14 | + int[][] data3 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}; |
| 15 | + |
| 16 | + Matrix m1 = new Matrix(data1); |
| 17 | + Matrix m2 = new Matrix(data2); |
| 18 | + Matrix m3 = new Matrix(data3); |
| 19 | + |
| 20 | + System.out.println("m1 --> Rows: " + m1.getRows() + " Columns: " + m1.getColumns()); |
| 21 | + System.out.println("m2 --> Rows: " + m2.getRows() + " Columns: " + m2.getColumns()); |
| 22 | + System.out.println("m3 --> Rows: " + m3.getRows() + " Columns: " + m3.getColumns()); |
| 23 | + |
| 24 | + //check for reference issues |
| 25 | + System.out.println("m2 -->\n" + m2); |
| 26 | + data2[1][1] = 101; |
| 27 | + System.out.println("m2 -->\n" + m2); |
| 28 | + |
| 29 | + //test equals |
| 30 | + System.out.println("m2==null: " + m2.equals(null)); //false |
| 31 | + System.out.println("m3==\"MATRIX\": " + m2.equals("MATRIX")); //false |
| 32 | + System.out.println("m2==m1: " + m2.equals(m1)); //false |
| 33 | + System.out.println("m2==m2: " + m2.equals(m2)); //true |
| 34 | + System.out.println("m2==m3: " + m2.equals(m3)); //false |
| 35 | + |
| 36 | + //test operations (valid) |
| 37 | + System.out.println("2 * m2:\n" + m2.scale(2)); |
| 38 | + System.out.println("m2 + m3:\n" + m2.plus(m3)); |
| 39 | + System.out.println("m2 - m3:\n" + m2.minus(m3)); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * Data needs to be a deep copy as not to change the original state. |
| 45 | + */ |
| 46 | + private int[][] data; |
| 47 | + |
| 48 | + /** |
| 49 | + * Constructor for the matrix takes in a 2D array |
| 50 | + * |
| 51 | + * @param pData |
| 52 | + */ |
| 53 | + public Matrix(int[][] pData) { |
| 54 | + |
| 55 | + /** Make a deep copy of the data */ |
| 56 | + if(pData.length != 0) { |
| 57 | + int[][] newData = new int[pData.length][pData[0].length]; |
| 58 | + |
| 59 | + for(int i = 0; i < pData.length; i++) |
| 60 | + for(int j = 0; j < pData[0].length; j++) |
| 61 | + newData[i][j] = pData[i][j]; |
| 62 | + |
| 63 | + this.data = newData; |
| 64 | + } else { |
| 65 | + this.data = null; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns the element specified by the given location |
| 71 | + * |
| 72 | + * @param x : x cooridinate |
| 73 | + * @param y : y cooridinate |
| 74 | + * @return int : value at location |
| 75 | + */ |
| 76 | + public int getElement(int x, int y) { |
| 77 | + return data[x][y]; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the number of rows in the Matrix |
| 82 | + * |
| 83 | + * @return rows |
| 84 | + */ |
| 85 | + public int getRows() { |
| 86 | + if(this.data == null) |
| 87 | + return 0; |
| 88 | + |
| 89 | + return data.length; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns the number of rows in the Matrix |
| 94 | + * |
| 95 | + * @return columns |
| 96 | + */ |
| 97 | + public int getColumns() { |
| 98 | + if(this.data == null) |
| 99 | + return 0; |
| 100 | + return data[0].length; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns this matrix scaled by a factor. That is, computes sA where s is a |
| 105 | + * constant and A is a matrix (this object). |
| 106 | + * |
| 107 | + * @param scalar : value to scale by |
| 108 | + * @return A new matrix scaled by the scalar value |
| 109 | + */ |
| 110 | + public Matrix scale(int scalar) { |
| 111 | + |
| 112 | + int[][] newData = new int[this.data.length][this.data[0].length]; |
| 113 | + |
| 114 | + for (int i = 0; i < this.getRows(); ++i) |
| 115 | + for(int j = 0; j < this.getColumns(); ++j) |
| 116 | + newData[i][j] = this.data[i][j] * scalar; |
| 117 | + |
| 118 | + return new Matrix(newData); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Adds this matrix to another matrix. |
| 123 | + * |
| 124 | + * @param other : Matrix to be added |
| 125 | + * @return addend |
| 126 | + */ |
| 127 | + public Matrix plus(Matrix other) throws RuntimeException { |
| 128 | + |
| 129 | + int[][] newData = new int[this.data.length][this.data[0].length]; |
| 130 | + |
| 131 | + if(this.getRows() != other.getRows() || this.getColumns() != other.getColumns()) |
| 132 | + throw new RuntimeException("Not the same size matrix."); |
| 133 | + |
| 134 | + for (int i = 0; i < this.getRows(); ++i) |
| 135 | + for(int j = 0; j < this.getColumns(); ++j) |
| 136 | + newData[i][j] = this.data[i][j] + other.getElement(i, j); |
| 137 | + |
| 138 | + return new Matrix(newData); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Subtracts this matrix from another matrix. |
| 143 | + * |
| 144 | + * @param other : Matrix to be subtracted |
| 145 | + * @return difference |
| 146 | + */ |
| 147 | + public Matrix minus(Matrix other) throws RuntimeException { |
| 148 | + |
| 149 | + int[][] newData = new int[this.data.length][this.data[0].length]; |
| 150 | + |
| 151 | + if(this.getRows() != other.getRows() || this.getColumns() != other.getColumns()) |
| 152 | + throw new RuntimeException("Not the same size matrix."); |
| 153 | + |
| 154 | + for (int i = 0; i < this.getRows(); ++i) |
| 155 | + for(int j = 0; j < this.getColumns(); ++j) |
| 156 | + newData[i][j] = this.data[i][j] - other.getElement(i, j); |
| 157 | + |
| 158 | + return new Matrix(newData); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Checks if the matrix passed is equal to this matrix |
| 163 | + * |
| 164 | + * @param other : the other matrix |
| 165 | + * @return boolean |
| 166 | + */ |
| 167 | + public boolean equals(Matrix other) { |
| 168 | + return this == other; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Returns the Matrix as a String in the following format |
| 173 | + * |
| 174 | + * [ a b c ] ... |
| 175 | + * [ x y z ] ... |
| 176 | + * [ i j k ] ... |
| 177 | + * ... |
| 178 | + * |
| 179 | + * @return Matrix as String |
| 180 | + * TODO: Work formatting for different digit sizes |
| 181 | + */ |
| 182 | + public String toString() { |
| 183 | + String str = ""; |
| 184 | + |
| 185 | + for(int i = 0; i < this.data.length; i++) { |
| 186 | + str += "[ "; |
| 187 | + for(int j = 0; j < this.data[0].length; j++) { |
| 188 | + str += data[i][j]; |
| 189 | + str += " "; |
| 190 | + } |
| 191 | + str += "]"; |
| 192 | + str += "\n"; |
| 193 | + } |
| 194 | + |
| 195 | + return str; |
| 196 | + } |
| 197 | +} |
0 commit comments