// Matrix.java // solve, invert, etc. last argument is output // void solve(double A[][], double Y[], double X[]); X=A^-1*Y // void invert(double A[][]); A=A^-1 // double determinant(double A[]); d=det A // void eigenvalues(double A[][], double V[][], double Y[]); V,Y=eigen A // void checkeigen(double A[][], V[][], double Y[]); printout // void multiply(double A[][], double B[][], double C[][]); C=A*B // void add(double A[][], double B[][], double C[][]); C=A+B // void subtract(double A[][], double B[][], double C[][]); C=A-B // void transpose(double A[][], double C[][]); C=A^t // double norm1(double A[][]); d=norm1 A // double norm2(double A[][]); sqrt largest eigenvalue A^T*A d=norm2 A // double normFro(double A[][]); Frobenius d=normFro A // double normInf(double A[][]); d=normInf A // void identity(double A[][]); A=I // void zero(double A[][]); A=0 // void copy(double A[][], double B[][]); B=A // void boolean equals(double A[][], double B[][]); B==A // void print(double A[][]); A // void multiply(double A[][], double X[], double Y[]); Y=A*X // void add(double X[], double Y[], double Z[]); Z=X+Y // void subtract(double X[], double Y[], double Z[]); Z=X-Y // double norm1(double X[]); d=norm1 X // double norm2(double X[]); d=norm2 X // double normInf(double X[]); d=normInf X // void unitVector(double X[], int i); X[i]=1 else 0 // void zero(double X[]); X=0 // void copy(double X[], double Y[]); Y=X // void boolean equals(double X[], doubleY[]); X==Y // void print(double X[]); X // void print(int K[][]); K // void print(int K[]); K public strictfp class Matrix { public static void solve(final double A[][], final double Y[], double X[]) { // solve real linear equations for X where Y = A * X // method: Gauss-Jordan elimination using maximum pivot // usage: Matrix.solve(A,Y,X); // Translated to java by : Jon Squire , 26 March 2003 // First written by Jon Squire December 1959 for IBM 650, translated to // other languages e.g. Fortran converted to Ada converted to C // converted to java int n=A.length; int m=n+1; double B[][]=new double[n][m]; // working matrix int row[]=new int[n]; // row interchange indicies int hold , I_pivot; // pivot indicies double pivot; // pivot element value double abs_pivot; if(A[0].length!=n || Y.length!=n || X.length!=n) { System.out.println("Error in Matrix.solve, inconsistent array sizes."); } // build working data structure for(int i=0; i abs_pivot) { I_pivot = i; pivot = B[row[i]][k]; abs_pivot = Math.abs(pivot); } } // have pivot, interchange row indicies hold = row[k]; row[k] = row[I_pivot]; row[I_pivot] = hold; // check for near singular if(abs_pivot < 1.0E-10) { for(int j=k+1; j abs_pivot) { I_pivot = i ; J_pivot = j ; pivot = A[row[i]][col[j]] ; } } } if(Math.abs(pivot) < 1.0E-10) { System.out.println("Matrix is singular !"); return; } hold = row[k]; row[k]= row[I_pivot]; row[I_pivot] = hold ; hold = col[k]; col[k]= col[J_pivot]; col[J_pivot] = hold ; // reduce about pivot A[row[k]][col[k]] = 1.0 / pivot ; for(int j=0; j abs_pivot ) { I_pivot = i; pivot = B[row[i]][k]; abs_pivot = Math.abs(pivot); } } // have pivot, interchange row indicies if(I_pivot != k) { hold = row[k]; row[k] = row[I_pivot]; row[I_pivot] = hold; D = - D; } // check for near singular if(abs_pivot < 1.0E-20) { return abs_pivot; } else { D = D * pivot; // reduce about pivot for(int j=k+1; j=0.0) t=1.0/(tau+Math.sqrt(1.0+tau*tau)); else t=-1.0/((-tau)+Math.sqrt(1.0+tau*tau)); c[0]=1.0/Math.sqrt(1.0+t*t); s[0]=t * c[0]; } else { c[0]=1.0; s[0]=0.0; } } // end schur2 static void mat22(final double c[], final double s[], final double A[][], double B[][]) { if(A.length!=2 || A[0].length!=2 || B.length!=2 || B[0].length!=2) { System.out.println("Error in mat22 of Jacobi, not both 2 by 2"); } double T[][] = new double[2][2]; T[0][0] = c[0] * A[0][0] - s[0] * A[0][1] ; T[0][1] = s[0] * A[0][0] + c[0] * A[0][1] ; T[1][0] = c[0] * A[1][0] - s[0] * A[1][1] ; T[1][1] = s[0] * A[1][0] + c[0] * A[1][1] ; B[0][0] = c[0] * T[0][0] - s[0] * T[1][0] ; B[0][1] = c[0] * T[0][1] - s[0] * T[1][1] ; B[1][0] = s[0] * T[0][0] + c[0] * T[1][0] ; B[1][1] = s[0] * T[0][1] + c[0] * T[1][1] ; } // end mat2 static void mat44(final int p, final int q, final double c[], final double s[], final double A[][], double V[][]) { int n = A.length; double B[][] = new double[n][n]; double J[][] = new double[n][n]; if(s.length!=1 || c.length!=1) { System.out.println("Error in mat44 of Jacobi, s or c not length 1"); } if(A[0].length!=n || V.length!=n || V[0].length!=n) { System.out.println("Error in mat44 of Jacobi, A or V not same and square"); } for(int i=0; i