Example usage for org.apache.commons.math.linear SingularValueDecomposition getV

List of usage examples for org.apache.commons.math.linear SingularValueDecomposition getV

Introduction

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

Prototype

RealMatrix getV();

Source Link

Document

Returns the matrix V of the decomposition.

Usage

From source file:geogebra.common.kernel.statistics.AlgoFitImplicit.java

@Override
public final void compute() {

    int order = (int) orderGeo.evaluateDouble();

    datasize = pointlist.size(); // rows in M and Y

    if (!pointlist.isDefined() || datasize < order * (order + 3) / 2) {
        fitfunction.setUndefined();/*w ww  .  ja v  a  2s . com*/
        return;
    }

    if (!pointlist.getElementType().equals(GeoClass.POINT)) {
        fitfunction.setUndefined();
        return;
    }
    try {

        // Get functions, x and y from lists
        if (!makeMatrixes()) {
            fitfunction.setUndefined();
            return;
        }

        // App.debug("datasize = " + datasize);
        // App.debug("order = " + order);
        // App.debug("M cols = "+M.getColumnDimension());
        // App.debug("M rows = "+M.getRowDimension());
        // App.debug("M = "+M.toString());

        SingularValueDecomposition svd = new SingularValueDecompositionImpl(M);

        V = svd.getV();

        // App.debug("V = "+V.toString());

        // App.debug("size of M = "+M.getColumnDimension()+" "+M.getRowDimension());
        // App.debug("size of V = "+V.getColumnDimension()+" "+V.getRowDimension());

        makeFunction();

    } catch (Throwable t) {
        fitfunction.setUndefined();
        t.printStackTrace();
    }

}

From source file:dr.math.Procrustes.java

public Procrustes(RealMatrix X, RealMatrix Xstar, boolean allowTranslation, boolean allowDilation) {
    rowDimension = X.getRowDimension();/*from   w ww  . j ava 2 s  . c  om*/
    columnDimension = X.getColumnDimension();

    if (Xstar.getRowDimension() != rowDimension) {
        throw new IllegalArgumentException("X and Xstar do not have the same number of rows");
    }
    if (Xstar.getColumnDimension() != columnDimension) {
        throw new IllegalArgumentException("X and Xstar do not have the same number of columns");
    }

    RealMatrix J = new Array2DRowRealMatrix(rowDimension, rowDimension);

    if (allowTranslation) {
        //           J <- diag(n) - 1/n * matrix(1, n, n)
        //           for n = 3, J = {{1, -2/3, -2/3}, {-2/3, 1, -2/3}, {-2/3, -2/3, 1}}

        for (int i = 0; i < rowDimension; i++) {
            J.setEntry(i, i, 1.0 - (1.0 / rowDimension));

            for (int j = i + 1; j < rowDimension; j++) {
                J.setEntry(i, j, -1.0 / rowDimension);
                J.setEntry(j, i, -1.0 / rowDimension);
            }
        }
    } else {
        //           J <- diag(n)

        for (int i = 0; i < rowDimension; i++) {
            J.setEntry(i, i, 1);
        }

    }

    //       C <- t(Xstar) %*% J %*% X

    RealMatrix C = Xstar.transpose().multiply(J.multiply(X));

    //       svd.out <- svd(C)
    //       R <- svd.out$v %*% t(svd.out$u)
    //      NB: Apache math does a different SVD from R.  TODO Should use Colt library
    SingularValueDecomposition SVD = new SingularValueDecompositionImpl(C);
    R = SVD.getV().multiply(SVD.getUT());

    //       s <- 1
    double s = 1.0; // scale = 1 unless dilation is being used

    if (allowDilation) {
        //           mat1 <- t(Xstar) %*% J %*% X %*% R
        RealMatrix mat1 = Xstar.transpose().multiply(J.multiply(X.multiply(R)));

        //           mat2 <- t(X) %*% J %*% X
        RealMatrix mat2 = X.transpose().multiply(J.multiply(X));

        //           s.numer <- 0
        //           s.denom <- 0
        double numer = 0.0;
        double denom = 0.0;

        //           for (i in 1:m) {
        //               s.numer <- s.numer + mat1[i, i]
        //               s.denom <- s.denom + mat2[i, i]
        //           }
        for (int i = 0; i < columnDimension; i++) {
            numer = numer + mat1.getEntry(i, i);
            denom = denom + mat2.getEntry(i, i);
        }
        //           s <- s.numer/s.denom
        s = numer / denom;
    }
    this.s = s;

    //       tt <- matrix(0, m, 1)
    RealMatrix tmpT = new Array2DRowRealMatrix(columnDimension, 1); // a translation vector of zero unless translation is being used

    if (allowTranslation) {
        //           tt <- 1/n * t(Xstar - s * X %*% R) %*% matrix(1, n, 1)
        RealMatrix tmp = new Array2DRowRealMatrix(rowDimension, 1);
        for (int i = 0; i < rowDimension; i++) {
            tmp.setEntry(i, 0, 1);
        }
        tmpT = Xstar.subtract(X.multiply(R).scalarMultiply(s)).transpose().scalarMultiply(1.0 / rowDimension)
                .multiply(tmp);
    }

    T = tmpT;
}

From source file:com.opengamma.analytics.math.linearalgebra.SVDecompositionCommonsResult.java

/**
 * @param svd The result of the SV decomposition, not null
 *///  w w w  .j a v a 2s.  c o  m
public SVDecompositionCommonsResult(final SingularValueDecomposition svd) {
    Validate.notNull(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();
}