Java Code Examples for org.apache.commons.math3.linear.SingularValueDecomposition#getSingularValues()
The following examples show how to use
org.apache.commons.math3.linear.SingularValueDecomposition#getSingularValues() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SingularSpectrumTransform.java From incubator-hivemall with Apache License 2.0 | 6 votes |
/** * Singular Value Decomposition (SVD) based naive scoring. */ 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]; }
Example 2
Source File: LinalgUtil.java From MeteoInfo with GNU Lesser General Public License v3.0 | 5 votes |
/** * Calculates the compact Singular Value Decomposition of a matrix. The * Singular Value Decomposition of matrix A is a set of three matrices: U, Σ * and V such that A = U × Σ × VT. Let A be a m × n matrix, then U is a m × * p orthogonal matrix, Σ is a p × p diagonal matrix with positive or null * elements, V is a p × n orthogonal matrix (hence VT is also orthogonal) * where p=min(m,n). * * @param a Given matrix. * @return Result U/S/V arrays. */ public static Array[] svd(Array a) { int m = a.getShape()[0]; int n = a.getShape()[1]; int k = Math.min(m, n); Array Ua = Array.factory(DataType.DOUBLE, new int[]{m, k}); Array Va = Array.factory(DataType.DOUBLE, new int[]{k, n}); Array Sa = Array.factory(DataType.DOUBLE, new int[]{k}); double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray_Double(a); RealMatrix matrix = new Array2DRowRealMatrix(aa, false); SingularValueDecomposition decomposition = new SingularValueDecomposition(matrix); RealMatrix U = decomposition.getU(); RealMatrix V = decomposition.getVT(); double[] sv = decomposition.getSingularValues(); for (int i = 0; i < m; i++) { for (int j = 0; j < k; j++) { Ua.setDouble(i * k + j, U.getEntry(i, j)); } } for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) { Va.setDouble(i * n + j, V.getEntry(i, j)); } } for (int i = 0; i < k; i++) { Sa.setDouble(i, sv[i]); } return new Array[]{Ua, Sa, Va}; }
Example 3
Source File: LibCommonsMath.java From systemds with Apache License 2.0 | 5 votes |
/** * Performs Singular Value Decomposition. Calls Apache Commons Math SVD. * X = U * Sigma * Vt, where X is the input matrix, * U is the left singular matrix, Sigma is the singular values matrix returned as a * column matrix and Vt is the transpose of the right singular matrix V. * However, the returned array has { U, Sigma, V} * * @param in Input matrix * @return An array containing U, Sigma & V */ private static MatrixBlock[] computeSvd(MatrixBlock in) { Array2DRowRealMatrix matrixInput = DataConverter.convertToArray2DRowRealMatrix(in); SingularValueDecomposition svd = new SingularValueDecomposition(matrixInput); double[] sigma = svd.getSingularValues(); RealMatrix u = svd.getU(); RealMatrix v = svd.getV(); MatrixBlock U = DataConverter.convertToMatrixBlock(u.getData()); MatrixBlock Sigma = DataConverter.convertToMatrixBlock(sigma, true); Sigma = LibMatrixReorg.diag(Sigma, new MatrixBlock(Sigma.rlen, Sigma.rlen, true)); MatrixBlock V = DataConverter.convertToMatrixBlock(v.getData()); return new MatrixBlock[] { U, Sigma, V }; }
Example 4
Source File: LibCommonsMath.java From systemds with Apache License 2.0 | 5 votes |
/** * Performs Singular Value Decomposition. Calls Apache Commons Math SVD. * X = U * Sigma * Vt, where X is the input matrix, * U is the left singular matrix, Sigma is the singular values matrix returned as a * column matrix and Vt is the transpose of the right singular matrix V. * However, the returned array has { U, Sigma, V} * * @param in Input matrix * @return An array containing U, Sigma & V */ private static MatrixBlock[] computeSvd(MatrixBlock in) { Array2DRowRealMatrix matrixInput = DataConverter.convertToArray2DRowRealMatrix(in); SingularValueDecomposition svd = new SingularValueDecomposition(matrixInput); double[] sigma = svd.getSingularValues(); RealMatrix u = svd.getU(); RealMatrix v = svd.getV(); MatrixBlock U = DataConverter.convertToMatrixBlock(u.getData()); MatrixBlock Sigma = DataConverter.convertToMatrixBlock(sigma, true); Sigma = LibMatrixReorg.diag(Sigma, new MatrixBlock(Sigma.rlen, Sigma.rlen, true)); MatrixBlock V = DataConverter.convertToMatrixBlock(v.getData()); return new MatrixBlock[] { U, Sigma, V }; }
Example 5
Source File: ApacheSingularValueDecomposer.java From gatk with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** Create a SVD instance using Apache Commons Math. * * @param m matrix that is not {@code null} * @return SVD instance that is never {@code null} */ @Override public SVD createSVD(final RealMatrix m) { Utils.nonNull(m, "Cannot create SVD on a null matrix."); final SingularValueDecomposition svd = new SingularValueDecomposition(m); final RealMatrix pinv = svd.getSolver().getInverse(); return new SimpleSVD(svd.getU(), svd.getSingularValues(), svd.getV(), pinv); }
Example 6
Source File: SVDecompositionCommonsResult.java From Strata with Apache License 2.0 | 5 votes |
/** * Creates an instance. * * @param svd The result of the SV decomposition, not null */ public SVDecompositionCommonsResult(SingularValueDecomposition svd) { ArgChecker.notNull(svd, "svd"); _condition = svd.getConditionNumber(); _norm = svd.getNorm(); _rank = svd.getRank(); _s = CommonsMathWrapper.unwrap(svd.getS()); _singularValues = svd.getSingularValues(); _u = CommonsMathWrapper.unwrap(svd.getU()); _uTranspose = CommonsMathWrapper.unwrap(svd.getUT()); _v = CommonsMathWrapper.unwrap(svd.getV()); _vTranspose = CommonsMathWrapper.unwrap(svd.getVT()); _solver = svd.getSolver(); }
Example 7
Source File: LinearAlgebra.java From january with Eclipse Public License 1.0 | 4 votes |
/** * @param a * @return array of singular values */ public static double[] calcSingularValues(Dataset a) { SingularValueDecomposition svd = new SingularValueDecomposition(createRealMatrix(a)); return svd.getSingularValues(); }