Example usage for org.apache.commons.math3.linear RealMatrix multiply

List of usage examples for org.apache.commons.math3.linear RealMatrix multiply

Introduction

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

Prototype

RealMatrix multiply(RealMatrix m) throws DimensionMismatchException;

Source Link

Document

Returns the result of postmultiplying this by m .

Usage

From source file:com.itemanalysis.psychometrics.cfa.TauEquivalentModel.java

public RealMatrix getImpliedCovariance(double[] argument) {
    setParameters(argument);/* www.j a  va 2s. co  m*/
    RealMatrix B2 = Ivec.scalarMultiply(Math.pow(argument[0], 2));
    RealMatrix THETA = new DiagonalMatrix(errorVariance);//param
    SIGMA = B2.multiply(Ivec.transpose()).add(THETA);

    //        System.out.println("TAU: " );
    //        for(int i=0;i<SIGMA.getRowDimension();i++){
    //            for(int j=0;j<SIGMA.getColumnDimension();j++){
    //                System.out.print(" " + SIGMA.getEntry(i, j));
    //            }
    //            System.out.println();
    //        }
    //        System.out.println();

    return SIGMA;
}

From source file:edu.cmu.tetrad.search.EstimateRank.java

public double[] CanCor(double[][] A, double[][] B) {
    this.A = A;/*from  ww  w  . ja  v a2  s. c  om*/
    this.B = B;
    RealMatrix Ua = new SingularValueDecomposition(new BlockRealMatrix(A)).getU();
    RealMatrix UTa = Ua.transpose();
    RealMatrix Ub = new SingularValueDecomposition(new BlockRealMatrix(B)).getU();
    return new SingularValueDecomposition(UTa.multiply(Ub)).getSingularValues();
}

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();/*from  w  w w.  ja v a 2  s  .  c  o  m*/
    RealMatrix V = svd.getV();
    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:io.github.malapert.jwcs.coordsystem.Galactic.java

@Override
protected RealMatrix getRotationMatrix(final SkySystem refFrame) {
    RealMatrix m;//from  ww w .j  a  v a2 s. com
    if (refFrame instanceof Equatorial) {
        RealMatrix m1 = Utility.MatrixEqB19502Gal().transpose();
        RealMatrix m2 = Utility.MatrixEpoch12Epoch2(1950.0f, refFrame.getEquinox(),
                ReferenceSystemInterface.Type.FK4, ((Equatorial) refFrame).getReferenceSystemType(), null);
        m = m2.multiply(m1);
    } else if (refFrame instanceof Galactic) {
        m = MatrixUtils.createRealIdentityMatrix(3);
    } else if (refFrame instanceof SuperGalactic) {
        m = Utility.MatrixGal2Sgal();
    } else if (refFrame instanceof Ecliptic) {
        RealMatrix m1 = Utility.MatrixEqB19502Gal().transpose();
        RealMatrix m2 = Utility.MatrixEpoch12Epoch2(1950.0f, refFrame.getEquinox(),
                ReferenceSystemInterface.Type.FK4, ReferenceSystemInterface.Type.FK5, null);
        RealMatrix m3 = Utility.MatrixEq2Ecl(refFrame.getEquinox(), ReferenceSystemInterface.Type.FK5);
        m = m3.multiply(m2).multiply(m1);
    } else {
        throw new JWcsError(String.format("Unknown output sky system: %s", refFrame.getSkySystemName()));
    }
    return m;
}

From source file:com.joptimizer.functions.SDPLogarithmicBarrier.java

/**
 * @see "S.Boyd and L.Vandenberghe, Convex Optimization, p. 618"
 *///ww  w.jav a 2  s  . c o  m
public double[] gradient(double[] X) {
    double[] ret = new double[dim];
    RealMatrix S = buildS(X);
    CholeskyDecomposition cFact = new CholeskyDecomposition(S);
    RealMatrix SInv = cFact.getSolver().getInverse();
    for (int i = 0; i < dim; i++) {
        ret[i] = SInv.multiply(this.Fi[i]).getTrace();
    }
    return ret;
}

From source file:de.andreasschoknecht.LS3.Query.java

/**
 * Calculate the LSSM values with respect to a document collection.
 *
 * @param Sk The matrix Sk of singular values
 * @param Vtk The matrix Vtk of the singular value decomposition
 *///from w ww.  java2  s .c om
void calculateLSSMValues(RealMatrix Sk, RealMatrix Vtk) {
    // scale Vtk with singular value matrix Sk
    RealMatrix scaledVtk = Sk.multiply(Vtk);

    // the query model as vector
    ArrayRealVector queryVector = new ArrayRealVector(pseudoDocument);

    int docsNumber = scaledVtk.getColumnDimension();
    lssmValues = new double[docsNumber];

    for (int i = 0; i < docsNumber; i++) {
        RealVector columnVector = scaledVtk.getColumnVector(i);
        lssmValues[i] = (queryVector.cosine(columnVector) + 1) / 2;
    }
}

From source file:com.opengamma.strata.math.impl.regression.WeightedLeastSquaresRegression.java

public LeastSquaresRegressionResult regress(double[][] x, double[] weights, double[] y, boolean useIntercept) {
    if (weights == null) {
        throw new IllegalArgumentException("Cannot perform WLS regression without an array of weights");
    }/*from  w w w.  java2s  . c  o m*/
    checkData(x, weights, y);
    double[][] dep = addInterceptVariable(x, useIntercept);
    double[] w = new double[weights.length];
    for (int i = 0; i < y.length; i++) {
        w[i] = weights[i];
    }
    DoubleMatrix matrix = DoubleMatrix.copyOf(dep);
    DoubleArray vector = DoubleArray.copyOf(y);
    RealMatrix wDiag = new DiagonalMatrix(w);
    DoubleMatrix transpose = s_algebra.getTranspose(matrix);

    DoubleMatrix wDiagTimesMatrix = DoubleMatrix
            .ofUnsafe(wDiag.multiply(new Array2DRowRealMatrix(matrix.toArrayUnsafe())).getData());
    DoubleMatrix tmp = (DoubleMatrix) s_algebra
            .multiply(s_algebra.getInverse(s_algebra.multiply(transpose, wDiagTimesMatrix)), transpose);

    DoubleMatrix wTmpTimesDiag = DoubleMatrix
            .copyOf(wDiag.preMultiply(new Array2DRowRealMatrix(tmp.toArrayUnsafe())).getData());
    DoubleMatrix betasVector = (DoubleMatrix) s_algebra.multiply(wTmpTimesDiag, vector);
    double[] yModel = super.writeArrayAsVector(
            ((DoubleMatrix) s_algebra.multiply(matrix, betasVector)).toArray());
    double[] betas = super.writeArrayAsVector(betasVector.toArray());
    return getResultWithStatistics(x, convertArray(wDiag.getData()), y, betas, yModel, transpose, matrix,
            useIntercept);
}

From source file:io.github.malapert.jwcs.coordsystem.SuperGalactic.java

@Override
protected RealMatrix getRotationMatrix(final SkySystem refFrame) {
    RealMatrix m;//  w ww .j a  v  a 2s .  co m
    if (refFrame instanceof Equatorial) {
        RealMatrix m1 = Utility.MatrixGal2Sgal().transpose();
        RealMatrix m2 = Utility.MatrixEqB19502Gal().transpose();
        RealMatrix m3 = Utility.MatrixEpoch12Epoch2(1950.0f, (float) ((Equatorial) refFrame).getEquinox(),
                ReferenceSystemInterface.Type.FK4, ((Equatorial) refFrame).getReferenceSystemType(), null);
        m = m3.multiply(m2).multiply(m1);
    } else if (refFrame instanceof Galactic) {
        m = Utility.MatrixGal2Sgal().transpose();
    } else if (refFrame instanceof SuperGalactic) {
        m = MatrixUtils.createRealIdentityMatrix(3);
    } else if (refFrame instanceof Ecliptic) {
        RealMatrix m1 = Utility.MatrixGal2Sgal().transpose();
        RealMatrix m2 = Utility.MatrixEqB19502Gal().transpose();
        //TODO : Revrifier aprs implmentation
        RealMatrix m3 = Utility.MatrixEpoch12Epoch2(1950.0f, refFrame.getEquinox(),
                ReferenceSystemInterface.Type.FK4, ReferenceSystemInterface.Type.FK5, null);
        RealMatrix m4 = Utility.MatrixEq2Ecl(refFrame.getEquinox(), ReferenceSystemInterface.Type.FK5);
        ///
        m = m4.multiply(m3).multiply(m2).multiply(m1);
    } else {
        throw new JWcsError(String.format("Unknown output sky system: %s", refFrame.getSkySystemName()));
    }
    return m;
}

From source file:edu.ucdenver.bios.designcalculator.DesignCalculator.java

/**
 * Calculate the sum of squares hypothesis matrix (the H matrix)
 * @param params matrices input by user/*from   w w  w .  j  a  v  a2 s .  c om*/
 * @return H matrix
 */
public RealMatrix getHypothesisSumOfSquares(RealMatrix C, RealMatrix beta, RealMatrix U, RealMatrix thetaNull,
        RealMatrix XtXInverse) {
    // M = C(X'X)^-1C'
    RealMatrix M = C.multiply(XtXInverse.multiply(C.transpose()));
    // thetaHat = C * Beta * U
    RealMatrix thetaHat = C.multiply(beta.multiply(U));
    // thetaHat - thetaNull.  Multiple by negative one to do subtraction
    RealMatrix thetaDiff = thetaHat.subtract(thetaNull);

    // calculate the hypothesis sum of squares: (thetaHat - thetaNull)'[C(X'X)-1C'](thetaHat - thetaNull)
    RealMatrix hss = thetaDiff.transpose().multiply(M.multiply(thetaDiff));

    return hss;

}

From source file:com.joptimizer.util.CholeskyFactorizationTest.java

public void testInvert1() throws Exception {
    log.debug("testInvert1");
    double[][] QData = new double[][] { { 1, .12, .13, .14, .15 }, { .12, 2, .23, .24, .25 },
            { .13, .23, 3, 0, 0 }, { .14, .24, 0, 4, 0 }, { .15, .25, 0, 0, 5 } };
    RealMatrix Q = MatrixUtils.createRealMatrix(QData);

    CholeskyFactorization myc = new CholeskyFactorization(QData);
    RealMatrix L = new Array2DRowRealMatrix(myc.getL());
    RealMatrix LT = new Array2DRowRealMatrix(myc.getLT());
    log.debug("L: " + L);
    log.debug("LT: " + LT);
    log.debug("L.LT: " + L.multiply(LT));
    log.debug("LT.L: " + LT.multiply(L));

    // check Q = L.LT
    double norm = L.multiply(LT).subtract(Q).getNorm();
    log.debug("norm: " + norm);
    assertTrue(norm < 1.E-15);//from   ww  w. j a  v a  2s.c o  m

    RealMatrix LInv = new SingularValueDecomposition(L).getSolver().getInverse();
    log.debug("LInv: " + ArrayUtils.toString(LInv.getData()));
    RealMatrix LInvT = LInv.transpose();
    log.debug("LInvT: " + ArrayUtils.toString(LInvT.getData()));
    RealMatrix LTInv = new SingularValueDecomposition(LT).getSolver().getInverse();
    log.debug("LTInv: " + ArrayUtils.toString(LTInv.getData()));
    RealMatrix LTInvT = LTInv.transpose();
    log.debug("LTInvT: " + ArrayUtils.toString(LTInvT.getData()));
    log.debug("LInv.LInvT: " + ArrayUtils.toString(LInv.multiply(LInvT).getData()));
    log.debug("LTInv.LTInvT: " + ArrayUtils.toString(LTInv.multiply(LTInvT).getData()));

    RealMatrix Id = MatrixUtils.createRealIdentityMatrix(Q.getRowDimension());
    //check Q.(LTInv * LInv) = 1
    norm = Q.multiply(LTInv.multiply(LInv)).subtract(Id).getNorm();
    log.debug("norm: " + norm);
    assertTrue(norm < 5.E-15);

    // check Q.QInv = 1
    RealMatrix QInv = MatrixUtils.createRealMatrix(myc.getInverse());
    norm = Q.multiply(QInv).subtract(Id).getNorm();
    log.debug("norm: " + norm);
    assertTrue(norm < 1.E-15);

    //check eigenvalues
    double det1 = Utils.calculateDeterminant(QData, QData.length);
    double det2 = 1;
    List<Double> eigenvalues = myc.getEigenvalues();
    for (double ev : eigenvalues) {
        det2 = det2 * ev;
    }
    log.debug("det1: " + det1);
    log.debug("det2: " + det2);
    assertEquals(det1, det2, 1.E-13);
}