List of usage examples for org.apache.commons.math3.linear SingularValueDecomposition getU
public RealMatrix getU()
From source file:cooccurrence.Omer_Levy.java
public static void main(String args[]) { String path = ""; String writePath = ""; BufferedReader br = null;/*w ww. jav a 2s . c o m*/ ArrayList<String> files = new ArrayList<>(); //reading all the files in the directory //each file is PPMI matrix for an year listFilesForFolder(new File(path), files); for (String filePath : files) { System.out.println(filePath); String fileName = new File(filePath).getName(); //data structure to store the PPMI matrix in the file HashMap<String, HashMap<String, Double>> cooccur = new HashMap<>(); readFileContents(filePath, cooccur); //reading the file and storing the content in the hashmap //Because Matrices are identified by row and col id, the following //lists maps id to corresponding string. Note that matrix is symmetric. ArrayList<String> rowStrings = new ArrayList<>(cooccur.keySet()); ArrayList<String> colStrings = new ArrayList<>(cooccur.keySet()); //creating matrix with given dimensions and initializing it to 0 RealMatrix matrixR = MatrixUtils.createRealMatrix(rowStrings.size(), colStrings.size()); //creating the matrices for storing top rank-d matrices of SVD RealMatrix matrixUd = MatrixUtils.createRealMatrix(D, D); RealMatrix matrixVd = MatrixUtils.createRealMatrix(D, D); RealMatrix coVarD = MatrixUtils.createRealMatrix(D, D); //populating the matrices based on the co-occur hashmap populateMatrixR(matrixR, cooccur, rowStrings, colStrings); //computing the svd SingularValueDecomposition svd = new SingularValueDecomposition(matrixR); //extracting the components of SVD factorization RealMatrix U = svd.getU(); RealMatrix V = svd.getV(); RealMatrix coVariance = svd.getCovariance(-1); //list to store indices of top-D singular values of coVar. //Use this with rowsString (colStrings) to get the corresponding word and context ArrayList<Integer> indicesD = new ArrayList<>(); //Extract topD singular value from covariance to store in coVarD and //extract corresponding columns from U and V to store in Ud and Vd getTopD(U, V, coVariance, matrixUd, matrixVd, coVarD, indicesD); //calulate the squareRoot of coVarD RealMatrix squareRootCoVarD = squareRoot(coVarD); RealMatrix W_svd = matrixUd.multiply(squareRootCoVarD); RealMatrix C_svd = matrixVd.multiply(squareRootCoVarD); } }
From source file:com.github.tteofili.p2h.Par2HierUtils.java
static double[][] getTruncatedUT(INDArray matrix, int k) { double[][] data = getDoubles(matrix); SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(data)); double[][] truncatedU = new double[svd.getU().getRowDimension()][k]; svd.getU().copySubMatrix(0, truncatedU.length - 1, 0, k - 1, truncatedU); return truncatedU; }
From source file:com.github.tteofili.p2h.Par2HierUtils.java
/** * truncated SVD as taken from http://stackoverflow.com/questions/19957076/best-way-to-compute-a-truncated-singular-value-decomposition-in-java *//* ww w . ja va 2 s . c om*/ static double[][] getTruncatedSVD(double[][] matrix, final int k) { SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(matrix)); double[][] truncatedU = new double[svd.getU().getRowDimension()][k]; svd.getU().copySubMatrix(0, truncatedU.length - 1, 0, k - 1, truncatedU); double[][] truncatedS = new double[k][k]; svd.getS().copySubMatrix(0, k - 1, 0, k - 1, truncatedS); double[][] truncatedVT = new double[k][svd.getVT().getColumnDimension()]; svd.getVT().copySubMatrix(0, k - 1, 0, truncatedVT[0].length - 1, truncatedVT); RealMatrix approximatedSvdMatrix = (MatrixUtils.createRealMatrix(truncatedU)) .multiply(MatrixUtils.createRealMatrix(truncatedS)) .multiply(MatrixUtils.createRealMatrix(truncatedVT)); return approximatedSvdMatrix.getData(); }
From source file:edu.oregonstate.eecs.mcplan.ml.MatrixAlgorithms.java
public static RealMatrix makePositiveDefinite(final RealMatrix M, final double eps) { assert (eps > 0.0); final SingularValueDecomposition svd = new SingularValueDecomposition(M); final RealMatrix Sigma = svd.getS().copy(); final int N = Math.min(Sigma.getColumnDimension(), Sigma.getRowDimension()); for (int i = 0; i < N; ++i) { final double lambda = Sigma.getEntry(i, i); System.out.println("lambda_" + i + " = " + lambda); if (Math.abs(lambda) < eps) { System.out.println("Corrected " + i); Sigma.setEntry(i, i, eps);/*from www .jav a 2s . c om*/ } else if (lambda < 0.0) { throw new NonPositiveDefiniteMatrixException(lambda, i, eps); } else { Sigma.setEntry(i, i, lambda); } } return svd.getU().multiply(Sigma).multiply(svd.getVT()); }
From source file:edu.cmu.tetrad.util.MatrixUtils.java
public static double[][] pseudoInverse(double[][] x) { SingularValueDecomposition svd = new SingularValueDecomposition(new Matrix(x)); Matrix U = svd.getU(); Matrix V = svd.getV();//from www.j av a 2 s.co m Matrix S = svd.getS(); for (int i = 0; i < S.getRowDimension(); i++) { for (int j = 0; j < S.getColumnDimension(); j++) { double v = S.get(i, j); S.set(i, j, v == 0 ? 0.0 : 1.0 / v); } } return V.times(S.times(U.transpose())).getArray(); }
From source file:edu.cmu.tetrad.util.MatrixUtils1.java
public static double[][] pseudoInverse(double[][] x) { SingularValueDecomposition svd = new SingularValueDecomposition(new BlockRealMatrix(x)); RealMatrix U = svd.getU(); RealMatrix V = svd.getV();// w ww . ja v a 2 s.co m RealMatrix S = svd.getS(); for (int i = 0; i < S.getRowDimension(); i++) { for (int j = 0; j < S.getColumnDimension(); j++) { double v = S.getEntry(i, j); S.setEntry(i, j, v == 0 ? 0.0 : 1.0 / v); } } return V.multiply(S.multiply(U.transpose())).getData(); }
From source file:dataminning2.SVDDecomposition.java
public double[][] DecompositionU(double[][] Dataarraytest) { Array2DRowRealMatrix rmA = new Array2DRowRealMatrix(Dataarraytest); SingularValueDecomposition svdObj = new SingularValueDecomposition(rmA); double[][] DataU = svdObj.getU().getData(); return DataU; }
From source file:de.andreasschoknecht.LS3.LSSMCalculator.java
/** * Calculation of the singular value decomposition and initialization of corresponding variables. * * @param tdMatrix The weighted Term-Document Matrix for decomposition *//*w w w . j ava 2 s. co m*/ void calculateSVD(double[][] tdMatrix) { weightedTDMatrix = new Array2DRowRealMatrix(tdMatrix); // execute actual SVD SingularValueDecomposition svd = new SingularValueDecomposition(weightedTDMatrix); U = svd.getU(); S = svd.getS(); Vt = svd.getVT(); // set rank of SVD rank = svd.getRank(); }
From source file:hivemall.anomaly.SingularSpectrumTransform.java
/** * Singular Value Decomposition (SVD) based naive scoring. *//*from www . ja v a 2 s . c o m*/ private double computeScoreSVD(@Nonnull final RealMatrix H, @Nonnull final RealMatrix G) { SingularValueDecomposition svdH = new SingularValueDecomposition(H); RealMatrix UT = svdH.getUT(); SingularValueDecomposition svdG = new SingularValueDecomposition(G); RealMatrix Q = svdG.getU(); // find the largest singular value for the r principal components RealMatrix UTQ = UT.getSubMatrix(0, r - 1, 0, window - 1).multiply(Q.getSubMatrix(0, window - 1, 0, r - 1)); SingularValueDecomposition svdUTQ = new SingularValueDecomposition(UTQ); double[] s = svdUTQ.getSingularValues(); return 1.d - s[0]; }
From source file:hivemall.utils.math.MatrixUtilsTest.java
@Test public void testPower1() { RealMatrix A = new Array2DRowRealMatrix( new double[][] { new double[] { 1, 2, 3 }, new double[] { 4, 5, 6 } }); double[] x = new double[3]; x[0] = Math.random();//w ww. j ava2s . c o m x[1] = Math.random(); x[2] = Math.random(); double[] u = new double[2]; double[] v = new double[3]; double s = MatrixUtils.power1(A, x, 2, u, v); SingularValueDecomposition svdA = new SingularValueDecomposition(A); Assert.assertArrayEquals(svdA.getU().getColumn(0), u, 0.001d); Assert.assertArrayEquals(svdA.getV().getColumn(0), v, 0.001d); Assert.assertEquals(svdA.getSingularValues()[0], s, 0.001d); }