Java API By Example, From Geeks To Geeks.

# Java > Open Source Codes > JSci > maths > matrices > AbstractDoubleSquareMatrix

 `1 /* AUTO-GENERATED */2 package JSci.maths.matrices;3 4 import JSci.GlobalSettings;5 import JSci.maths.ArrayMath;6 import JSci.maths.ExtraMath;7 import JSci.maths.LinearMath;8 import JSci.maths.MaximumIterationsExceededException;9 import JSci.maths.Mapping;10 import JSci.maths.vectors.AbstractDoubleVector;11 import JSci.maths.groups.AbelianGroup;12 import JSci.maths.algebras.*;13 import JSci.maths.fields.*;14 15 /**16 * The AbstractDoubleSquareMatrix class provides an object for encapsulating double square matrices.17 * @version 2.218 * @author Mark Hale19 */20 public abstract class AbstractDoubleSquareMatrix extends AbstractDoubleMatrix implements SquareMatrix {21         protected transient AbstractDoubleSquareMatrix[] LU;22         protected transient int[] LUpivot;23         /**24         * Constructs a matrix.25         */26         protected AbstractDoubleSquareMatrix(final int size) {27                 super(size, size);28         }29         /**30         * Converts this matrix to an integer matrix.31         * @return an integer square matrix32         */33         public AbstractIntegerMatrix toIntegerMatrix() {34                 final int ans[][]=new int[numRows][numCols];35                 for(int i=0;irows()+1373         * to hold the pivot information (null if not interested).374         * The last array element will contain the parity.375         * @return an array with [0] containing the L-matrix376         * and [1] containing the U-matrix.377         * @jsci.planetmath LUDecomposition378         */379         public AbstractDoubleSquareMatrix[] luDecompose(int pivot[]) {380                 if(LU!=null) {381                         if(pivot!=null)382                                 System.arraycopy(LUpivot,0,pivot,0,pivot.length);383                         return LU;384                 }385                 int pivotrow;386                 final int N=numRows;387                 final double arrayL[][]=new double[N][N];388                 final double arrayU[][]=new double[N][N];389                 if(pivot==null)390                         pivot=new int[N+1];391                 for(int i=0;imax) {412                                         max=tmp;413                                         pivotrow=i;414                                 }415                         }416                 // swap row j with pivotrow417 if(pivotrow!=j) {418                                 double[] tmprow = arrayU[j];419                                 arrayU[j] = arrayU[pivotrow];420                                 arrayU[pivotrow] = tmprow;421                                 int k=pivot[j];422                                 pivot[j]=pivot[pivotrow];423                                 pivot[pivotrow]=k;424                                 // update parity425 pivot[N]=-pivot[N];426                         }427                 // divide by pivot428 double tmp=arrayU[j][j];429                         for(int i=j+1;iJAMA (public domain).530         * @return an array with [0] containing the Q-matrix and [1] containing the R-matrix.531         * @jsci.planetmath QRDecomposition532         */533         public AbstractDoubleSquareMatrix[] qrDecompose() {534                 final int N=numRows;535                 final double array[][]=new double[N][N];536                 final double arrayQ[][]=new double[N][N];537                 final double arrayR[][]=new double[N][N];538                 // copy matrix539 for(int i=0;i=0; k--) {569                         arrayQ[k][k] = 1.0;570                         for(int j=k; jJAMA (public domain).596         * @return an array with [0] containing the U-matrix, [1] containing the S-matrix and [2] containing the V-matrix.597         * @jsci.planetmath SingularValueDecomposition598         */599         public AbstractDoubleSquareMatrix[] singularValueDecompose() {600                 final int N=numRows;601                 final int Nm1=N-1;602                 final double array[][]=new double[N][N];603                 final double arrayU[][]=new double[N][N];604                 final double arrayS[]=new double[N];605                 final double arrayV[][]=new double[N][N];606                 final double e[]=new double[N];607                 final double work[]=new double[N];608                 // copy matrix609 for(int i=0;i=0;k--) {681                         if(arrayS[k]!=0.0) {682                                 for(int j=k+1;j=0;k--) {702                         if(k0) {719                         int k,action;720                         // action = 1 if arrayS[p] and e[k-1] are negligible and k=-1;k--) {725                                 if(k==-1)726                                         break;727                                 if(Math.abs(e[k])<=eps*(Math.abs(arrayS[k])+Math.abs(arrayS[k+1]))) {728                                         e[k]=0.0;729                                         break;730                                 }731                         }732                         if(k==p-2) {733                                 action=4;734                         } else {735                                 int ks;736                                 for(ks=p-1;ks>=k;ks--) {737                                         if(ks==k)738                                                 break;739                                         double t=(ks!=p ? Math.abs(e[ks]) : 0.0)+(ks!=k+1 ? Math.abs(e[ks-1]) : 0.0);740                                         if(Math.abs(arrayS[ks])<=eps*t) {741                                                 arrayS[ks]=0.0;742                                                 break;743                                         }744                                 }745                                 if(ks==k) {746                                         action=3;747                                 } else if(ks==p-1) {748                                         action=1;749                                 } else {750                                         action=2;751                                         k=ks;752                                 }753                         }754                         k++;755                         switch(action) {756                                 // deflate negligible arrayS[p]757 case 1: {758                                         double f=e[p-2];759                                         e[p-2]=0.0;760                                         for(int j=p-2;j>=k;j--) {761                                                 double t=ExtraMath.hypot(arrayS[j],f);762                                                 final double cs=arrayS[j]/t;763                                                 final double sn=f/t;764                                                 arrayS[j]=t;765                                                 if(j!=k) {766                                                         f=-sn*e[j-1];767                                                         e[j-1]*=cs;768                                                 }769                                                 for(int i=0;i=arrayS[k+1])862                                                         break;863                                                 double tmp=arrayS[k];864                                                 arrayS[k]=arrayS[k+1];865                                                 arrayS[k+1]=tmp;866                                                 if(k