No special classes or libraries are used with this application. The complete source resides in the one file above. After downloading, the following should work in any JDK 1.2 compatible compiler:
javac MatrixCalculator.java java MatrixCalculator
Matrix Calculator Tips/Help
All Matrices must be symmetric (n x n) Enter Matrix Elements Row by Row seperated by spaces. Ex. (2x2) 2 4
3 1
Results will be placed in the C matrix.
The calculation of the determinant, by definition, is based upon a factorial number of calculations with respect to the size of the matrix. ie. a 3x3 matrix would have 6 calculations (3!) to make, whereas a 20x20 matrix would have 2.43 x 10^18 calculations (20!). So instead of brute forcing the calculations, I first do some operations on the matrix, which converts it to a upper triangular matrix, and then calculate the determinant by multipling down the diagonal, since everything below is 0, this will give the determinant.
Floating Points and Accuracies
For some reason computers aren't as accurate as I think they are, probably my calculation techniques. The accuracy of the numbers are probably only to 3 maybe 2 decimal places. If you keep applying operations to matrices and then use the resultant matrix a couple of times, the decimals get out of whack. Calculating an inverse and then multplying the matrix by it, is a good example of this.
Test Some Mathematical Theories
The determinant of A-inverse equals 1 over the determinant of A.
If two rows of matrix A are equal, the determinant of A equals 0.
det(A*B)=det(A)det(B)
A*B does not necessarily equal B*A
The determinant of A-transpose equals the determinant of A.
If the matrix B is constructed by interchanging two rows (columns) in matrix A, then the determinant of B equals the negative determinant of A
You can test, adj(A) = det(A) * inv(A), but this is the theorem I use to calculate the inverse, so it better work.
Mathematics and Linear Algebra Calculating the Determinant
The calculation of the determinant, by definition, is based upon a factorial number of calculations with respect to the size of the matrix. ie. a 3x3 matrix would have 6 calculations (3!), whereas a 20x20 matrix would have 2.43 x 10^18 calculations (20!).
So instead of brute forcing the calculations, I first do some operations on the matrix, which converts it to a upper triangular matrix, and then calculate the determinant by multipling down the diagonal, since everything below is 0, this will give the determinant.
See The Mathematics Behind Them for more information and mathematical explanations on the definitions and calculation techniques.
Click the spoiler below for displaying source-code
if (DEBUG) System.out.println("Baris: " + ts.countTokens());
float matrix[][] = new float[ts.countTokens()][];
StringTokenizer st2; int row = 0; int col = 0; //making sure rows are same length int last = -5; int curr = -5; while (ts.hasMoreTokens()) { st2 = new StringTokenizer(ts.nextToken(), " "); last = curr; curr = st2.countTokens(); if(last != -5 && curr!= last) throw new Exception("Baris != length"); if (DEBUG) System.out.println("Kolom: " + st2.countTokens()); matrix[row] = new float[st2.countTokens()]; while (st2.hasMoreElements()) { matrix[row][col++] = Float.parseFloat(st2.nextToken()); } row++; col = 0; } System.out.println(); return matrix; }
// -------------------------------------------------------------- // Display Matrix in TextArea // -------------------------------------------------------------- public void DisplayMatrix(float[][] matrix, JTextArea ta) {
if (DEBUG) { System.out.println("Displaying Matrix"); }
String rstr = ""; String dv = "";
for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { dv = nf.format(matrix[i][j]); rstr = rstr.concat(dv + " "); }
rstr = rstr.concat("\n"); }
ta.setText(rstr); }
public float[][] AddMatrix(float[][] a, float[][] b) throws Exception { int tms = a.length; int tmsB = b.length; if (tms != tmsB) { statusBar.setText("Matrix Size Mismatch"); }
float matrix[][] = new float[tms][tms];
for (int i = 0; i < tms; i++) for (int j = 0; j < tms; j++) { matrix[i][j] = a[i][j] + b[i][j]; }
return matrix; }
// -------------------------------------------------------------- public float[][] MultiplyMatrix(float[][] a, float[][] b) throws Exception {
if(a[0].length != b.length) throw new Exception("Matrices incompatible for multiplication"); float matrix[][] = new float[a.length][b[0].length];
for (int i = 0; i < a.length; i++) for (int j = 0; j < b[i].length; j++) matrix[i][j] = 0;
//cycle through answer matrix for(int i = 0; i < matrix.length; i++){ for(int j = 0; j < matrix[i].length; j++){ matrix[i][j] = calculateRowColumnProduct(a,i,b,j); } } return matrix; }
public float calculateRowColumnProduct(float[][] A, int row, float[][] B, int col){ float product = 0; for(int i = 0; i < A[row].length; i++) product +=A[row][i]*B[i][col]; return product; } // --------------------------------------------------------------
public float[][] Transpose(float[][] a) { if (INFO) { System.out.println("Performing Transpose..."); }
float m[][] = new float[a[0].length][a.length];
for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) m[j][i] = a[i][j]; return m; }
public float[][] Inverse(float[][] a) throws Exception { // Formula used to Calculate Inverse: // inv(A) = 1/det(A) * adj(A) if (INFO) { System.out.println("Performing Inverse..."); } int tms = a.length;
float m[][] = new float[tms][tms]; float mm[][] = Adjoint(a);
float det = Determinant(a); float dd = 0;
if (det == 0) { statusBar.setText("Determinant = 0, Not Invertible."); if (INFO) { System.out.println("Determinant = 0, Not Invertible."); } } else { dd = 1 / det; }
for (int i = 0; i < tms; i++) for (int j = 0; j < tms; j++) { m[i][j] = dd * mm[i][j]; }