JSci.maths.matrices.DoubleSquareMatrix Maven / Gradle / Ivy
Show all versions of jsci Show documentation
/* AUTO-GENERATED */ package JSci.maths.matrices; import JSci.maths.ArrayMath; import JSci.maths.ExtraMath; import JSci.maths.LinearMath; import JSci.maths.Mapping; import JSci.maths.DimensionException; import JSci.maths.MaximumIterationsExceededException; import JSci.maths.vectors.AbstractDoubleVector; import JSci.maths.vectors.DoubleVector; import JSci.maths.groups.AbelianGroup; import JSci.maths.algebras.*; import JSci.maths.fields.*; /** * The DoubleSquareMatrix class provides an object for encapsulating double square matrices. * @version 2.3 * @author Mark Hale */ public class DoubleSquareMatrix extends AbstractDoubleSquareMatrix { /** * Array containing the elements of the matrix. */ protected final double matrix[][]; /** * Constructs a matrix by wrapping an array. * @param array an assigned value */ public DoubleSquareMatrix(final double array[][]) { super(array.length); if(!ArrayMath.isSquare(array)) throw new MatrixDimensionException("Array is not square."); matrix=array; } /** * Constructs an empty matrix. */ public DoubleSquareMatrix(final int size) { this(new double[size][size]); } /** * Constructs a matrix from an array of vectors (columns). * @param array an assigned value */ public DoubleSquareMatrix(final AbstractDoubleVector array[]) { this(array.length); for(int i=0;i
* to hold the pivot information (null if not interested). * The last array element will contain the parity. * @return an array with [0] containing the L-matrix * and [1] containing the U-matrix. * @jsci.planetmath LUDecomposition */ public final AbstractDoubleSquareMatrix[] luDecompose(int pivot[]) { AbstractDoubleSquareMatrix[] LU = luDecompose_cache(pivot); if(LU != null) return LU; int pivotrow; final int N=numRows; final double arrayL[][]=new double[N][N]; final double arrayU[][]=new double[N][N]; double tmp; if(pivot==null) pivot=new int[N+1]; for(int i=0;i=0 && i =0 && j =0 && i =0 && j -norm. * @author Taber Smith */ public double infNorm() { double result=0; for(int i=0;i result) result=tmpResult; } return result; } /** * Returns the Frobenius or Hilbert-Schmidt (l2) norm. * @jsci.planetmath FrobeniusMatrixNorm */ public double frobeniusNorm() { double result=0.0; for(int j,i=0;i rows()+1 max) { max=tmp; pivotrow=i; } } // swap row j with pivotrow if(pivotrow!=j) { double[] tmprow = arrayU[j]; arrayU[j] = arrayU[pivotrow]; arrayU[pivotrow] = tmprow; int k=pivot[j]; pivot[j]=pivot[pivotrow]; pivot[pivotrow]=k; // update parity pivot[N]=-pivot[N]; } // divide by pivot tmp=arrayU[j][j]; for(int i=j+1;i JAMA (public domain). * @return an array with [0] containing the Q-matrix and [1] containing the R-matrix. * @jsci.planetmath QRDecomposition */ public AbstractDoubleSquareMatrix[] qrDecompose() { final int N=numRows; final double array[][]=new double[N][N]; final double arrayQ[][]=new double[N][N]; final double arrayR[][]=new double[N][N]; // copy matrix for(int i=0;i =0; k--) { arrayQ[k][k] = 1.0; for(int j=k; j JAMA (public domain). * @return an array with [0] containing the U-matrix, [1] containing the S-matrix and [2] containing the V-matrix. * @jsci.planetmath SingularValueDecomposition */ public AbstractDoubleSquareMatrix[] singularValueDecompose() { final int N=numRows; final int Nm1=N-1; final double array[][]=new double[N][N]; final double arrayU[][]=new double[N][N]; final double arrayS[]=new double[N]; final double arrayV[][]=new double[N][N]; final double e[]=new double[N]; final double work[]=new double[N]; // copy matrix for(int i=0;i =0;k--) { if(arrayS[k]!=0.0) { for(int j=k+1;j =0;k--) { if(k 0) { int k, action; // action = 1 if arrayS[p] and e[k-1] are negligible and k =-1;k--) { if(k==-1) break; if(Math.abs(e[k])<=eps*(Math.abs(arrayS[k])+Math.abs(arrayS[k+1]))) { e[k]=0.0; break; } } if(k==p-2) { action=4; } else { int ks; for(ks=p-1;ks>=k;ks--) { if(ks==k) break; double t=(ks!=p ? Math.abs(e[ks]) : 0.0)+(ks!=k+1 ? Math.abs(e[ks-1]) : 0.0); if(Math.abs(arrayS[ks])<=eps*t) { arrayS[ks]=0.0; break; } } if(ks==k) { action=3; } else if(ks==p-1) { action=1; } else { action=2; k=ks; } } k++; switch(action) { // deflate negligible arrayS[p] case 1: { double f=e[p-2]; e[p-2]=0.0; for(int j=p-2;j>=k;j--) { double t=ExtraMath.hypot(arrayS[j],f); final double cs=arrayS[j]/t; final double sn=f/t; arrayS[j]=t; if(j!=k) { f=-sn*e[j-1]; e[j-1]*=cs; } for(int i=0;i
=arrayS[k+1]) break; double tmp=arrayS[k]; arrayS[k]=arrayS[k+1]; arrayS[k+1]=tmp; if(k