/* Example showing how to apply some of the matrix decomposition methods. The dataset consists of Bear Measurements (why not, right?). the matrix analyzed is the covariance matrix which is both square and symmetric. Feel free to switch things up with a non-square matrix, or an asymmetric one. To load the file into R, use something along the lines of garbage<-read.csv("/Users/adilapapaya/Desktop/Downloads/TrioloDatasets/BEARS.csv",header=TRUE,sep=",") Adila Faruk, 2012. */ // import the library import papaya.*; int numDecimals =4; float[][] A = new float[][]{{1,5,9},{2,4,6},{1,6,3}}; float[][] iA = Mat.inverse(A); // if HHSize[i]==0, weights[i] = 0; println("\nA"); Mat.print(A,numDecimals); println("\ninverse A"); Mat.print(iA,numDecimals); // Using LU: Best for solving square matrices so we take just the upper 2x2 part of x //Mat.print(Mat.multiply(A,X),numDecimals); // should equal B LU lu = new LU(A); // covMatrix * pivotArray = L*U float det = (float)lu.det(); println("\nLU Decomposition:\nDeterminant is "+det +" (=the product of singular values or eigenvalues)" ); float[][] L = Cast.doubleToFloat(lu.getL()); println("\nL"); Mat.print(L,numDecimals); float[][] U = Cast.doubleToFloat(lu.getU()); println("\nU"); Mat.print(U,numDecimals); float[] B = new float[]{2,9,-1}; // if you want to try the case where B is an array float[] X = Cast.doubleToFloat( new LU(A).solve(B) ); println("\nLU solution X, to A*X = B"); Mat.print(X,numDecimals); Eigenvalue ev = new Eigenvalue(A); float[][] V = Cast.doubleToFloat(ev.getV()); println("\n\nV"); Mat.print(V,numDecimals); float[][] D = Cast.doubleToFloat(ev.getD()); println("\nD"); Mat.print(D,numDecimals);