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

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

Introduction

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

Prototype

double[][] getData();

Source Link

Document

Returns matrix entries as a two-dimensional array.

Usage

From source file:edu.ucdenver.bios.powersvc.resource.PowerResourceHelper.java

/**
 * This method takes a Real Matrix and converts it into a Named Matrix and
 * returns that Named Matrix.//  www .  j a  v  a  2 s .c  o m
 * 
 * @param matrix
 *            The matrix is a input matrix of type RealMatrix and is to be
 *            converted to a NamedMatrix.
 * @param name
 *            the name is a String, which is to be assigned to named matrix.
 * @return namedMatrix Returns a NamedMatrix which is obtained by converting
 *         the input matrix to NamedMatrix
 */
public static NamedMatrix toNamedMatrix(final RealMatrix matrix, final String name) {
    if (matrix == null || name == null || name.isEmpty()) {
        logger.error("failed to create NamedMatrix object name=[" + (name != null ? name : "NULL") + "]");
        return null;
    }
    NamedMatrix namedMatrix = new NamedMatrix();
    namedMatrix.setDataFromArray(matrix.getData());
    namedMatrix.setName(name);
    namedMatrix.setColumns(matrix.getColumnDimension());
    namedMatrix.setRows(matrix.getRowDimension());
    return namedMatrix;
}

From source file:com.mothsoft.alexis.engine.numeric.CorrelationCalculatorImpl.java

@Override
public double[][] correlate(List<DataSet> dataSets, Timestamp startDate, Timestamp endDate,
        TimeUnits granularity) {// w w  w.jav  a 2 s .c  o  m
    if (dataSets == null || dataSets.size() < 2) {
        throw new IllegalArgumentException("At least 2 data sets are required for correlation");
    }

    final Map<Long, List<Double>> orderedPoints = new TreeMap<Long, List<Double>>();

    for (int i = 0; i < dataSets.size(); i++) {
        final DataSet dataSet = dataSets.get(i);
        final List<DataSetPoint> points = this.dao.findAndAggregatePointsGroupedByUnit(dataSet, startDate,
                endDate, granularity);
        for (final DataSetPoint point : points) {
            final Long millis = point.getX().getTime();
            if (!orderedPoints.containsKey(millis)) {
                orderedPoints.put(millis, newDoubleList(dataSets.size()));
            }
            orderedPoints.get(millis).set(i, point.getY());
        }
    }

    if (orderedPoints.size() <= 1) {
        throw new IllegalArgumentException("Needed at least 2 points, found: " + orderedPoints.size());
    }

    final double[][] points = new double[orderedPoints.size()][dataSets.size()];

    int i = 0;
    for (final Map.Entry<Long, List<Double>> entry : orderedPoints.entrySet()) {
        final List<Double> values = entry.getValue();
        for (int j = 0; j < values.size(); j++) {
            points[i][j] = values.get(j);
        }
        i++;
    }

    final PearsonsCorrelation correlation = new PearsonsCorrelation();
    final RealMatrix matrix = correlation.computeCorrelationMatrix(points);
    return matrix.getData();
}

From source file:com.opengamma.strata.math.impl.util.CommonsMathWrapperTest.java

@Test
public void testVectorAsMatrix() {
    RealMatrix commons = CommonsMathWrapper.wrapAsMatrix(OG_VECTOR);
    double[][] data = commons.getData();
    assertEquals(data.length, OG_VECTOR.size());
    assertEquals(data[0].length, 1);//from   w w  w .j av a  2s.c  om
}

From source file:com.clust4j.algo.preprocess.MedianCenterer.java

@Override
public RealMatrix inverseTransform(RealMatrix X) {
    checkFit();/*from ww w.j  a  va2s .  c om*/

    // This effectively copies, so no need to do a copy later
    double[][] data = X.getData();
    final int m = data.length;
    final int n = data[0].length;

    if (n != medians.length)
        throw new DimensionMismatchException(n, medians.length);

    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            data[i][j] += medians[j];
        }
    }

    return new Array2DRowRealMatrix(data, false);
}

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  . ja v  a2 s  .com*/

    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);
}

From source file:com.clust4j.algo.preprocess.MeanCenterer.java

@Override
public RealMatrix inverseTransform(RealMatrix X) {
    checkFit();//  w  w  w .  j a  v a  2 s .c om

    // This effectively copies, so no need to do a copy later
    double[][] data = X.getData();
    final int m = data.length;
    final int n = data[0].length;

    if (n != means.length)
        throw new DimensionMismatchException(n, means.length);

    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            data[i][j] += means[j];
        }
    }

    return new Array2DRowRealMatrix(data, false);
}

From source file:com.clust4j.algo.preprocess.impute.BootstrapImputation.java

@Override
public RealMatrix transform(final RealMatrix dat) {
    return new Array2DRowRealMatrix(transform(dat.getData()), false);
}

From source file:com.itemanalysis.psychometrics.polycor.PolychoricMaximumLikelihood.java

private void computeStandardError(double[][] hessian) {
    RealMatrix m = new Array2DRowRealMatrix(hessian);
    LUDecomposition SLUD = new LUDecomposition(m);
    RealMatrix inv = SLUD.getSolver().getInverse();
    variance = inv.getData();
}

From source file:com.joptimizer.algebra.CholeskyRCFactorizationTest.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);

    CholeskyRCFactorization myc = new CholeskyRCFactorization(DoubleFactory2D.dense.make(QData));
    myc.factorize();/*  w  w w.  j ava2 s  .  c  o m*/
    RealMatrix L = new Array2DRowRealMatrix(myc.getL().toArray());
    RealMatrix LT = new Array2DRowRealMatrix(myc.getLT().toArray());
    log.debug("L: " + ArrayUtils.toString(L.getData()));
    log.debug("LT: " + ArrayUtils.toString(LT.getData()));
    log.debug("L.LT: " + ArrayUtils.toString(L.multiply(LT).getData()));
    log.debug("LT.L: " + ArrayUtils.toString(LT.multiply(L).getData()));

    // check Q = L.LT
    double norm = L.multiply(LT).subtract(Q).getNorm();
    log.debug("norm: " + norm);
    assertTrue(norm < 1.E-15);

    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().toArray());
    norm = Q.multiply(QInv).subtract(Id).getNorm();
    log.debug("norm: " + norm);
    assertTrue(norm < 1.E-15);
}

From source file:com.joptimizer.algebra.CholeskyRCTFactorizationTest.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);

    CholeskyRCTFactorization myc = new CholeskyRCTFactorization(DoubleFactory2D.dense.make(QData));
    myc.factorize();//from   w  w w  .j  a v  a  2s  .  c o m
    RealMatrix L = new Array2DRowRealMatrix(myc.getL().toArray());
    RealMatrix LT = new Array2DRowRealMatrix(myc.getLT().toArray());
    log.debug("L: " + ArrayUtils.toString(L.getData()));
    log.debug("LT: " + ArrayUtils.toString(LT.getData()));
    log.debug("L.LT: " + ArrayUtils.toString(L.multiply(LT).getData()));
    log.debug("LT.L: " + ArrayUtils.toString(LT.multiply(L).getData()));

    // check Q = L.LT
    double norm = L.multiply(LT).subtract(Q).getNorm();
    log.debug("norm: " + norm);
    assertTrue(norm < 1.E-15);

    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().toArray());
    norm = Q.multiply(QInv).subtract(Id).getNorm();
    log.debug("norm: " + norm);
    assertTrue(norm < 1.E-15);
}