1 import java.util.Random;
2 import java.text.NumberFormat;
6 /** Class representing matrix with methods for matrix algebra
7 * Copylefted by: Harvie 2oo9 ( http://blog.harvie.cz/ )
8 * @author Thomas Harvie Mudrunka (mudruto1)
12 class Matrix implements Serializable {
13 public float[][] matrix;
14 public final int x, y;
15 private Random rnd = new Random();
17 /** Construct new zero matrix described by (rows,cols) */
18 Matrix(int i, int j) {
21 matrix = new float[x][y];
22 for(i = 0;i < x;i++) for(j = 0;j < y;j++) matrix[i][j] = 0;
25 /** Construct new matrix from (2d_array) */
32 /** Return matrix as multiline String ready to output */
33 public String toString() {
34 String out = new String("");
35 for(int i = 0;i < x;i++) {
37 for(int j = 0;j < y;j++) out += (NumberFormat.getInstance().format(matrix[i][j])+"\t");
43 /** Print matrix to console */
45 System.out.println(this.toString());
48 /** Randomize matrix with numbers x, where: 0 <= x < max */
49 public void randomize(int max) {
50 for(int i = 0;i < x;i++) for(int j = 0;j < y;j++) matrix[i][j] = rnd.nextInt(max);
53 /** Compare size of this and another matrix */
54 public boolean compatible(Matrix m) {
55 if(m.x == this.x && m.y == this.y) return true;
56 System.err.println("Cannot add/subtract/multiply two matrices with different sizes!");
60 /** Add another matrix to this and return result */
61 public Matrix add(Matrix m) {
62 if(!compatible(m)) return null;
63 Matrix o = new Matrix(x,y);
64 for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j];
65 for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += m.matrix[i][j];
69 /** Subtract another matrix from this and return result */
70 public Matrix subtract(Matrix m) {
71 if(!compatible(m)) return null;
72 Matrix o = new Matrix(x,y);
73 for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j];
74 for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] -= m.matrix[i][j];
78 /** Scalar-multiply this matrix by another one and return result */
79 public Matrix multiply(Matrix m) {
80 if(!compatible(m)) return null;
81 Matrix o = new Matrix(x,y);
82 for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j];
83 for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) o.matrix[i][j] *= m.matrix[i][j];
87 /** Matrix-multiply this matrix by another one and return result */
88 public Matrix mmultiply(Matrix m) {
90 System.err.println("Cannot multiply those two matrices!");
93 Matrix o = new Matrix(this.x,m.y);
94 for(int i = 0;i < o.x;i++) for(int j = 0;j < o.y;j++) {
95 for(int z = 0;z < this.y;z++) o.matrix[i][j] += this.matrix[i][z] * m.matrix[z][j];
100 /** Return matrix representing this matrix with swapped rows a and b */
101 public Matrix swap_rows(int a, int b) {
102 Matrix o = new Matrix(x,y);
104 for(i = 0;i < o.x;i++) for(j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j];
105 float tmp[] = o.matrix[a];
106 o.matrix[a] = o.matrix[b];
111 /** Return determinant of this matrix */
112 public double determinant() {
113 System.err.println("TODO: Determinant!");
117 /*public float SIM_MIN(float a, float b) {
118 return (a < b ? a : b);
121 public double fabs(double a) {
125 /** Return matrix representing upper triangle format of this matrix */
126 public Matrix echelon() {
127 System.err.println("Reducing to echelon row form is not working properly!");
129 Matrix o = new Matrix(x,y);
131 for(i = 0;i < o.x;i++) for(j = 0;j < o.y;j++) o.matrix[i][j] += this.matrix[i][j];
133 for(int row = x; row >= 0; row--) {
136 for(j=row+1; j < y; j++) {
137 if(o.matrix[row][j] != 0) {
138 multiplier = -o.matrix[row][j];
139 //addRow(j, row, multiplier);
140 //(int fromRow, int toRow, double mult)
142 o.matrix[row][i] += o.matrix[j][i]*multiplier;
149 for(int r = 0; r < x; r++) {
154 while(o.matrix[i][lead] == 0) {
164 o = o.swap_rows(i, r);
165 for(j = 0;j < y; j++) o.matrix[r][j] /= o.matrix[r][lead];
166 for(int row = 0; row < x; row++)
170 for(int l = 0; l < y; l++)
171 o.matrix[row][l] -= o.matrix[i][lead] * o.matrix[r][l];
181 /** Serialize this object to file specified by its name (and path) */
182 public boolean save(String file) {
184 ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
185 os.writeObject(this);
187 } catch (Exception e) {
194 /** Deserialize and return Matrix object from file specified by its name (and path) */
195 public static Matrix load(String file) {
198 ObjectInputStream is = new ObjectInputStream(new FileInputStream(file));
199 m = (Matrix) is.readObject();
201 } catch (Exception e) {
210 /** Class demonstrating usage of Matrix class */
211 public class matice {
212 public static void main(String[] argv) {
213 System.out.println("You has matrix! Follow the black habit ;o)\n");
215 String file = "f.matrix";
217 System.out.println("Created matrix F and saved to file "+file+" =");
218 Matrix f = new Matrix(3,3); f.randomize(2); f.print();
221 System.out.println("Loaded matrix G from file "+file+" =");
222 Matrix g = Matrix.load(file); g.print();
226 System.out.println("Created matrix A =");
227 Matrix a = new Matrix(3,3); a.randomize(2); a.print();
229 System.out.println("Created matrix B =");
230 Matrix b = new Matrix(new float[][] {
237 /*System.out.println("Row Echelon form of B =");
241 System.out.println("A + B =");
244 System.out.println("A - B =");
245 a.subtract(b).print();
247 System.out.println("A * B =");
248 a.multiply(b).print();
250 System.out.println("Swap rows 0 and 1 of matrix B =");
251 b.swap_rows(0,1).print();
253 System.out.println("Created matrix C =");
254 Matrix c = new Matrix(3,4); c.randomize(20); c.print();
256 System.out.println("Created matrix D =");
257 Matrix d = new Matrix(4,2); d.randomize(10); d.print();
259 System.out.println("C . D =");
260 c.mmultiply(d).print();
278 **********************************/
283 0 ;) harvie@harvie-ntb prg $ javac matice.java; java matice
284 You has matrix! Follow the black habit ;o)