Java API By Example, From Geeks To Geeks.

# Java > Open Source Codes > JSci > maths > LinearMath

 `1 package JSci.maths;2 3 import JSci.maths.matrices.*;4 import JSci.maths.vectors.*;5 import JSci.maths.polynomials.RealPolynomial;6 7 /**8 * The linear math library.9 * This class cannot be subclassed or instantiated because all methods are static.10 * @version 2.211 * @author Mark Hale12 */13 public final class LinearMath extends AbstractMath {14         private LinearMath() {}15 16 // LINEAR SYSTEM17 18         /**19         * Solves the linear system Mx=v.20         * @param M a double square matrix.21         * @param v a double vector.22         * @return the double vector x.23         */24         public static AbstractDoubleVector solve(final AbstractDoubleSquareMatrix M,final AbstractDoubleVector v) {25                 final int n=v.dimension();26                 final double array[]=new double[n];27                 final int pivot[]=new int[n+1];28                 final AbstractDoubleSquareMatrix lu[]=M.luDecompose(pivot);29                 int i,j;30                 double sum;31         // forward substitution32 for(i=0;i=0;i--) {40                         sum=array[i];41                         for(j=i+1;j=0;i--) {65                         sum=array[i];66                         for(j=i+1;j=0;i--) {90                         sum=array[i];91                         for(j=i+1;j Math.abs(dx)) {191                 temp = dx / dy;192                 cs[1] = 1.0 / Math.sqrt( 1.0 + temp*temp );193                 cs[0] = temp * cs[1];194                 } else {195                 temp = dy / dx;196                 cs[0] = 1.0 / Math.sqrt( 1.0 + temp*temp );197                 cs[1] = temp * cs[0];198                 }199             return (cs);200     }201     private static double[] applyPlaneRotation(double dx, double dy, double cs, double sn) {202         double [] dxy = new double[2];203         dxy[0] = cs * dx + sn * dy;204         dxy[1] = -sn * dx + cs * dy;205             return (dxy);206     }207     private static AbstractDoubleVector update(AbstractDoubleVector x, int k, double[][] H, double [] s, AbstractDoubleVector [] v) {208         /************************************************209             * Backsolve of a triangular + sub diagonal matrix:210             ************************************************/211         for (int i = k; i >= 0; i--) {212                 s[i]=s[i]/H[i][i];213                 for (int j = i - 1; j >= 0; j--)214                         s[j]=s[j]-H[j][i]*s[i];215                 }216         for (int j = 0; j <= k; j++){217                 x = x.add(v[j].scalarMultiply(s[j]));218                 }219             return(x);220     }221 222 // LEAST SQUARES223 224         /**225         * Fits an nth degree polynomial to data using the method of least squares.226         * @param n the degree of the polynomial (>= 0).227         * @param data228         * [0][] contains the x-series,229         * [1][] contains the y-series.230         */231         public static RealPolynomial leastSquaresFit(int n, final double data[][]) {232                 int i,j,nm1=n++;233                 double xsum,xysum,tmp;234                 final double mArray[][]=new double[n][n];235                 final double vArray[]=new double[n];236                 for(i=0;i=l;i--) {532                                                 f=s*offdiag[i];533                                                 b=c*offdiag[i];534                                                 if(Math.abs(f)>=Math.abs(g)) {535                                                         c=g/f;536                                                         r=Math.sqrt(c*c+1.0);537                                                         offdiag[i+1]=f*r;538                                                         s=1/r;539                                                         c*=s;540                                                 } else {541                                                         s=f/g;542                                                         r=Math.sqrt(s*s+1.0);543                                                         offdiag[i+1]=g*r;544                                                         c=1/r;545                                                         s*=c;546                                                 }547                                                 g=diag[i+1]-p;548                                                 r=(diag[i]-g)*s+2.0*c*b;549                                                 p=s*r;550                                                 diag[i+1]=g+p;551                                                 g=c*r-b;552                                         }553                                         diag[l]=diag[l]-p;554                                         offdiag[l]=g;555                                         offdiag[m]=0.0;556                                 }557                         } while(m!=l);558                 }559         }560         /**561         * Internal NETLIB tred1 routine.562         * @author Richard Cannings563         */564         private static void reduceSymmetric1_SquareToTridiagonal(final double matrix[][],final double diag[],final double offdiag[]) {565                 final int n=diag.length;566                 int i,j,k,l;567                 double f,g,h,hh,scale;568                 for(i=n-1;i>0;i--) {569                         l=i-1;570                         h=scale=0.0;571                         if(l>0) {572                                 for(k=0;k<=l;k++)573                                         scale+=Math.abs(matrix[i][k]);574                                 if(scale==0.0)575                                         offdiag[i]=matrix[i][l];576                                 else {577                                         for(k=0;k<=l;k++) {578                                                 matrix[i][k]/=scale;579                                                 h+=matrix[i][k]*matrix[i][k];580                                         }581                                         f=matrix[i][l];582                                         g=(f>=0.0?-Math.sqrt(h):Math.sqrt(h));583                                         offdiag[i]=scale*g;584                                         h-=f*g;585                                         matrix[i][l]=f-g;586                                         f=0.0;587                                         for(j=0;j<=l;j++) {588                                                 g=0.0;589                                                 for(k=0;k<=j;k++)590                                                         g+=matrix[j][k]*matrix[i][k];591                                                 for(k=j+1;k<=l;k++)592                                                         g+=matrix[k][j]*matrix[i][k];593                                                 offdiag[j]=g/h;594                                                 f+=offdiag[j]*matrix[i][j];595                                         }596                                         hh=f/(h+h);597                                         for(j=0;j<=l;j++) {598                                                 f=matrix[i][j];599                                                 offdiag[j]=g=offdiag[j]-hh*f;600                                                 for(k=0;k<=j;k++)601                                                         matrix[j][k]-=f*offdiag[k]+g*matrix[i][k];602                                         }603                                 }604                         } else605                                 offdiag[i]=matrix[i][l];606                         diag[i]=h;607                 }608                 offdiag[0]=0.0;609                 for(i=0;i=l;i--) {640                                                 f=s*offdiag[i];641                                                 b=c*offdiag[i];642                                                 if(Math.abs(f)>=Math.abs(g)) {643                                                         c=g/f;644                                                         r=Math.sqrt(c*c+1.0);645                                                         offdiag[i+1]=f*r;646                                                         s=1/r;647                                                         c*=s;648                                                 } else {649                                                         s=f/g;650                                                         r=Math.sqrt(s*s+1.0);651                                                         offdiag[i+1]=g*r;652                                                         c=1/r;653                                                         s*=c;654                                                 }655                                                 g=diag[i+1]-p;656                                                 r=(diag[i]-g)*s+2.0*c*b;657                                                 p=s*r;658                                                 diag[i+1]=g+p;659                                                 g=c*r-b;660                                                 for(k=0;k0;i--) {683                         l=i-1;684                         h=scale=0.0;685                         if(l>0) {686                                 for(k=0;k<=l;k++)687                                         scale+=Math.abs(matrix[i][k]);688                                 if(scale==0.0)689                                         offdiag[i]=matrix[i][l];690                                 else {691                                         for(k=0;k<=l;k++) {692                                                 matrix[i][k]/=scale;693                                                 h+=matrix[i][k]*matrix[i][k];694                                         }695                                         f=matrix[i][l];696                                         g=(f>=0.0?-Math.sqrt(h):Math.sqrt(h));697                                         offdiag[i]=scale*g;698                                         h-=f*g;699                                         matrix[i][l]=f-g;700                                         f=0.0;701                                         for(j=0;j<=l;j++) {702                                                 matrix[j][i]=matrix[i][j]/h;703                                                 g=0.0;704                                                 for(k=0;k<=j;k++)705                                                         g+=matrix[j][k]*matrix[i][k];706                                                 for(k=j+1;k<=l;k++)707                                                         g+=matrix[k][j]*matrix[i][k];708                                                 offdiag[j]=g/h;709                                                 f+=offdiag[j]*matrix[i][j];710                                         }711                                         hh=f/(h+h);712                                         for(j=0;j<=l;j++) {713                                                 f=matrix[i][j];714                                                 offdiag[j]=g=offdiag[j]-hh*f;715                                                 for(k=0;k<=j;k++)716                                                         matrix[j][k]-=f*offdiag[k]+g*matrix[i][k];717                                         }718                                 }719                         } else720                                 offdiag[i]=matrix[i][l];721                         diag[i]=h;722                 }723                 diag[0]=offdiag[0]=0.0;724                 for(i=0;i