Example usage for org.apache.commons.math3.linear SingularValueDecomposition getUT

List of usage examples for org.apache.commons.math3.linear SingularValueDecomposition getUT

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear SingularValueDecomposition getUT.

Prototype

public RealMatrix getUT() 

Source Link

Document

Returns the transpose of the matrix U of the decomposition.

Usage

From source file:edu.oregonstate.eecs.mcplan.ml.MatrixAlgorithms.java

/**
 * Computes the inverse of a matrix using the singular value decomposition.
 * /*  www  .j a  v a 2 s .c o m*/
 * The input matrix M is assumed to be positive definite up to numerical
 * precision 'eps'. That is, for all eigenvalues lambda of M, it must be
 * the case that lambda + eps > 0. For eigenvalues with |lambda| < eps, the
 * eigenvalue is set to 'eps' before inverting. Throws an exception if
 * any lambda < -eps.
 * @param M
 * @param eps
 * @return
 */
public static RealMatrix robustInversePSD(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, 1.0 / eps);
        } else if (lambda < 0.0) {
            throw new IllegalArgumentException("Negative eigenvalue " + lambda);
        } else {
            Sigma.setEntry(i, i, 1.0 / lambda);
        }
    }
    return svd.getV().multiply(Sigma.transpose()).multiply(svd.getUT());
}

From source file:hivemall.anomaly.SingularSpectrumTransform.java

/**
 * Singular Value Decomposition (SVD) based naive scoring.
 *//*from  w w  w . j av  a2s .com*/
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:com.opengamma.strata.math.impl.linearalgebra.SVDecompositionCommonsResult.java

/**
 * @param svd The result of the SV decomposition, not null
 *///from ww  w .j a v  a2 s . co  m
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();
}

From source file:com.analog.lyric.dimple.solvers.sumproduct.customFactors.MutlivariateGaussianMatrixProduct.java

public MutlivariateGaussianMatrixProduct(double[][] A) {
    int i; //,m;
    M = A.length;/*from  w  ww .j ava  2s.  c  o  m*/
    N = A[0].length;
    /* Here we precompute and store matrices for future message computations.
     * First, compute an SVD of the matrix A using EigenDecompositions of A*A^T and A^T*A
     * This way, we get nullspaces for free along with regularized inverse.
     */

    RealMatrix Amat = wrapRealMatrix(A);

    SingularValueDecomposition svd = new SingularValueDecomposition(Amat);

    RealMatrix tmp = svd.getVT();
    tmp = svd.getS().multiply(tmp);
    tmp = svd.getU().multiply(tmp);

    A_clean = matrixGetDataRef(tmp);

    RealMatrix ST = svd.getS().transpose();

    int numS = Math.min(ST.getColumnDimension(), ST.getRowDimension());
    for (i = 0; i < numS; i++) {
        double d = ST.getEntry(i, i);
        if (d < eps)
            d = eps;
        else if (d > 1 / eps)
            d = 1 / eps;
        ST.setEntry(i, i, 1.0 / d);
    }

    A_pinv = matrixGetDataRef(svd.getV().multiply(ST.multiply(svd.getUT())));
}