Java API By Example, From Geeks To Geeks.

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

 1 /* AUTO-GENERATED */2 package JSci.maths.matrices;3 4 import JSci.maths.ArrayMath;5 import JSci.maths.ExtraMath;6 import JSci.maths.LinearMath;7 import JSci.maths.Mapping;8 import JSci.maths.DimensionException;9 import JSci.maths.MaximumIterationsExceededException;10 import JSci.maths.vectors.AbstractDoubleVector;11 import JSci.maths.vectors.DoubleVector;12 import JSci.maths.groups.AbelianGroup;13 import JSci.maths.algebras.*;14 import JSci.maths.fields.*;15 16 /**17 * The DoubleSquareMatrix class provides an object for encapsulating double square matrices.18 * @version 2.219 * @author Mark Hale20 */21 public class DoubleSquareMatrix extends AbstractDoubleSquareMatrix {22         /**23         * Array containing the elements of the matrix.24         */25         protected final double matrix[][];26         /**27         * Constructs a matrix by wrapping an array.28         * @param array an assigned value29         */30         public DoubleSquareMatrix(final double array[][]) {31                 super(array.length);32                 if(!ArrayMath.isSquare(array))33                         throw new MatrixDimensionException("Array is not square.");34                 matrix=array;35         }36         /**37         * Constructs an empty matrix.38         */39         public DoubleSquareMatrix(final int size) {40                 this(new double[size][size]);41         }42         /**43         * Constructs a matrix from an array of vectors (columns).44         * @param array an assigned value45         */46         public DoubleSquareMatrix(final AbstractDoubleVector array[]) {47                 this(array.length);48                 for(int i=0;i=0 && i=0 && j=0 && i=0 && j-norm.137         * @author Taber Smith138         */139         public double infNorm() {140                 double result=0;141                 for(int i=0;iresult)146                                 result=tmpResult;147                 }148                 return result;149         }150         /**151         * Returns the Frobenius or Hilbert-Schmidt (l2) norm.152         * @jsci.planetmath FrobeniusMatrixNorm153         */154         public double frobeniusNorm() {155                 double result=0.0;156                 for(int j,i=0;irows()+1505         * to hold the pivot information (null if not interested).506         * The last array element will contain the parity.507         * @return an array with [0] containing the L-matrix508         * and [1] containing the U-matrix.509         * @jsci.planetmath LUDecomposition510         */511         public final AbstractDoubleSquareMatrix[] luDecompose(int pivot[]) {512                 if(LU!=null) {513                         if(pivot!=null)514                                 System.arraycopy(LUpivot,0,pivot,0,pivot.length);515                         return LU;516                 }517                 int pivotrow;518                 final int N=numRows;519                 final double arrayL[][]=new double[N][N];520                 final double arrayU[][]=new double[N][N];521                 double tmp;522                 if(pivot==null)523                         pivot=new int[N+1];524                 for(int i=0;imax) {545                                         max=tmp;546                                         pivotrow=i;547                                 }548                         }549                 // swap row j with pivotrow550 if(pivotrow!=j) {551                                 double[] tmprow = arrayU[j];552                                 arrayU[j] = arrayU[pivotrow];553                                 arrayU[pivotrow] = tmprow;554                                 int k=pivot[j];555                                 pivot[j]=pivot[pivotrow];556                                 pivot[pivotrow]=k;557                                 // update parity558 pivot[N]=-pivot[N];559                         }560                 // divide by pivot561 tmp=arrayU[j][j];562                         for(int i=j+1;iJAMA (public domain).664         * @return an array with [0] containing the Q-matrix and [1] containing the R-matrix.665         * @jsci.planetmath QRDecomposition666         */667         public AbstractDoubleSquareMatrix[] qrDecompose() {668                 final int N=numRows;669                 final double array[][]=new double[N][N];670                 final double arrayQ[][]=new double[N][N];671                 final double arrayR[][]=new double[N][N];672                 // copy matrix673 for(int i=0;i=0; k--) {703                         arrayQ[k][k] = 1.0;704                         for(int j=k; jJAMA (public domain).730         * @return an array with [0] containing the U-matrix, [1] containing the S-matrix and [2] containing the V-matrix.731         * @jsci.planetmath SingularValueDecomposition732         */733         public AbstractDoubleSquareMatrix[] singularValueDecompose() {734                 final int N=numRows;735                 final int Nm1=N-1;736                 final double array[][]=new double[N][N];737                 final double arrayU[][]=new double[N][N];738                 final double arrayS[]=new double[N];739                 final double arrayV[][]=new double[N][N];740                 final double e[]=new double[N];741                 final double work[]=new double[N];742                 // copy matrix743 for(int i=0;i=0;k--) {815                         if(arrayS[k]!=0.0) {816                                 for(int j=k+1;j=0;k--) {836                         if(k0) {853                         int k, action;854                         // action = 1 if arrayS[p] and e[k-1] are negligible and k=-1;k--) {859                                 if(k==-1)860                                         break;861                                 if(Math.abs(e[k])<=eps*(Math.abs(arrayS[k])+Math.abs(arrayS[k+1]))) {862                                         e[k]=0.0;863                                         break;864                                 }865                         }866                         if(k==p-2) {867                                 action=4;868                         } else {869                                 int ks;870                                 for(ks=p-1;ks>=k;ks--) {871                                         if(ks==k)872                                                 break;873                                         double t=(ks!=p ? Math.abs(e[ks]) : 0.0)+(ks!=k+1 ? Math.abs(e[ks-1]) : 0.0);874                                         if(Math.abs(arrayS[ks])<=eps*t) {875                                                 arrayS[ks]=0.0;876                                                 break;877                                         }878                                 }879                                 if(ks==k) {880                                         action=3;881                                 } else if(ks==p-1) {882                                         action=1;883                                 } else {884                                         action=2;885                                         k=ks;886                                 }887                         }888                         k++;889                         switch(action) {890                                 // deflate negligible arrayS[p]891 case 1: {892                                         double f=e[p-2];893                                         e[p-2]=0.0;894                                         for(int j=p-2;j>=k;j--) {895                                                 double t=ExtraMath.hypot(arrayS[j],f);896                                                 final double cs=arrayS[j]/t;897                                                 final double sn=f/t;898                                                 arrayS[j]=t;899                                                 if(j!=k) {900                                                         f=-sn*e[j-1];901                                                         e[j-1]*=cs;902                                                 }903                                                 for(int i=0;i=arrayS[k+1])996                                                         break;997                                                 double tmp=arrayS[k];998                                                 arrayS[k]=arrayS[k+1];999                                                 arrayS[k+1]=tmp;1000                                                if(k