import java.util.Random; import java.text.NumberFormat; import java.lang.Math; import java.io.*; /** Class representing matrix with methods for matrix algebra * Copylefted by: Harvie 2oo9 ( http://blog.harvie.cz/ ) * @author Thomas Harvie Mudrunka (mudruto1) * @version 1.0 */ class Matrix implements Serializable { public float[][] matrix; public final int x, y; private Random rnd = new Random(); /** Construct new zero matrix described by (rows,cols) */ Matrix(int i, int j) { x = i; y = j; matrix = new float[x][y]; for(i = 0;i < x;i++) for(j = 0;j < y;j++) matrix[i][j] = 0; } /** Construct new matrix from (2d_array) */ Matrix(float[][] m) { x = m.length; y = m[0].length; matrix = m; } /** Return matrix as multiline String ready to output */ public String toString() { String out = new String(""); for(int i = 0;i < x;i++) { out += "|\t"; for(int j = 0;j < y;j++) out += (NumberFormat.getInstance().format(matrix[i][j])+"\t"); out += "|\n"; } return out; } /** Print matrix to console */ public void print() { System.out.println(this.toString()); } /** Randomize matrix with numbers x, where: 0 <= x < max */ public void randomize(int max) { for(int i = 0;i < x;i++) for(int j = 0;j < y;j++) matrix[i][j] = rnd.nextInt(max); } /** Compare size of this and another matrix */ public boolean compatible(Matrix m) { if(m.x == this.x && m.y == this.y) return true; System.err.println("Cannot add/subtract/multiply two matrices with different sizes!"); return false; } /** Add another matrix to this and return result */ public Matrix add(Matrix m) { if(!compatible(m)) return null; Matrix o = new Matrix(x,y); for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j]; for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += m.matrix[i][j]; return o; } /** Subtract another matrix from this and return result */ public Matrix subtract(Matrix m) { if(!compatible(m)) return null; Matrix o = new Matrix(x,y); for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j]; for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] -= m.matrix[i][j]; return o; } /** Scalar-multiply this matrix by another one and return result */ public Matrix multiply(Matrix m) { if(!compatible(m)) return null; Matrix o = new Matrix(x,y); for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j]; for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] *= m.matrix[i][j]; return o; } /** Matrix-multiply this matrix by another one and return result */ public Matrix mmultiply(Matrix m) { if(this.y != m.x) { System.err.println("Cannot multiply those two matrices!"); return null; } Matrix o = new Matrix(this.x,m.y); for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) { for(int z = 0;z < this.y;z++) o.matrix[i][j] += this.matrix[i][z] * m.matrix[z][j]; } return o; } /** Return matrix representing this matrix with swapped rows a and b */ public Matrix swap_rows(int a, int b) { Matrix o = new Matrix(x,y); int i, j; for(i = 0;i < o.x;i++) for(j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j]; float tmp[] = o.matrix[a]; o.matrix[a] = o.matrix[b]; o.matrix[b] = tmp; return o; } /** Return determinant of this matrix */ public double determinant() { System.err.println("TODO: Determinant!"); return 0; } /*public float SIM_MIN(float a, float b) { return (a < b ? a : b); } public double fabs(double a) { return Math.abs(a); }*/ /** Return matrix representing upper triangle format of this matrix */ public Matrix echelon() { System.err.println("Reducing to echelon row form is not working properly!"); //return null; Matrix o = new Matrix(x,y); int i, j; for(i = 0;i < o.x;i++) for(j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j]; for(int row = x; row >= 0; row--) { //reduceRow(row); double multiplier; for(j=row+1; j < y; j++) { if(o.matrix[row][j] != 0) { multiplier = -o.matrix[row][j]; //addRow(j, row, multiplier); //(int fromRow, int toRow, double mult) for(i=0; i