/* A simple class to implement 2-dimensional matricies because I'll be damned if I can find a standard class online to use instead. Good thing I only need basic multiplication for this project... eh? **reinvents wheel** Things to note: -height and width are set by constructor based on the int[][] we're passed -we can make a new Matrix from a Point, and there is a toPoint() method -matrix multiplication is integer only at the moment, probably not correct but easy enough to fix later without damaging anything that depends on it... Nathan Merritt - April 16, 2008 */ import java.awt.*; public class Matrix { int[][] matrix; int height, width; public Matrix (int[][] matrix) { this.matrix = matrix; height = matrix.length; width = matrix[0].length; } // makes a matrix from a Point x,y so we can do easy multiplication public Matrix (Point p) { int x = (int)p.getX(); int y = (int)p.getY(); matrix = new int[2][1]; matrix[0][0] = (int)p.getX(); matrix[1][0] = (int)p.getY(); height = 2; width = 1; } // creates a matrix full of zeroes given a height and width public Matrix (int height, int width) { matrix = new int[height][width]; this.height = height; this.width = width; zeroMatrix(); } // 2-dimensional matrix multiplication: [this][other] = [product] public Matrix multiply(Matrix other_matrix) { assert (width == other_matrix.getHeight()); // multiply: row by column and add up the sum int[][] other = other_matrix.getMatrix(); int[][] mul_product = new int[matrix.length][other[0].length]; for (int r= 0; r < matrix.length; r++) { for (int c= 0; c < other[0].length; c++) { mul_product[r][c]= dot(matrix[r], other, c); } } Matrix product = new Matrix(mul_product); return product; } /* returns the dot product of a row times a matrix m. I had an almost working version of my own but his is more elegantly expressed and is generally a better way to break down the problem. */ /* Credit to: http://forum.java.sun.com/thread.jspa?threadID=5145325 &messageID=9547719 */ int dot(int[] v, int[][] m, int c) { int dot= 0; for (int i= 0; i < v.length; i++) dot+= v[i]*m[i][c]; return dot; } /* prints the Matrix out, one row per column to stdout */ public String toString() { String string = ""; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { string += matrix[i][j] + " "; } string += "\n"; } return string; } // converts 2x1 matrix to a point // useful for my particular applications... public Point toPoint() { assert (height == 2 && width == 1); return new Point(matrix[0][0], matrix[1][0]); } // value getters & setters public int getValue(int row, int col) { assert (row > 0 && row < matrix.length - 1); assert (col > 0 && col < matrix[0].length - 1); return matrix[row][col]; } public void setValue(int row, int col, int value) { assert (row > 0 && row < matrix.length - 1); assert (col > 0 && col < matrix[0].length - 1); matrix[row][col] = value; } public int getHeight() { return height; } public int getWidth() { return width; } public int[][] getMatrix() { return matrix; } // simple function to set all values of a matrix to 0 public void zeroMatrix() { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { matrix[i][j] = 0; } } } }