Example usage for org.apache.commons.math3.linear MatrixUtils createRealMatrix

List of usage examples for org.apache.commons.math3.linear MatrixUtils createRealMatrix

Introduction

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

Prototype

public static RealMatrix createRealMatrix(double[][] data)
        throws NullArgumentException, DimensionMismatchException, NoDataException 

Source Link

Document

Returns a RealMatrix whose entries are the the values in the the input array.

Usage

From source file:Matrix_Operations.java

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    //Allow user to enter number of columns
    int rows = getRowsNumberFromUser();
    int columns = getColumnNumberFromUser();
    double matrixA[][] = new double[rows][columns];

    //Enter values for matrix
    System.out.println("Enter values for each position in the matrix below. ");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            matrixA[i][j] = keyboard.nextDouble();

        }/*from  w  w w .j a v  a 2  s .  co m*/
        System.out.println("");
    }

    showMatrix(matrixA);
    System.out.println("");
    RealMatrix A = MatrixUtils.createRealMatrix(matrixA);

    LUDecomposition lu = new LUDecomposition(A);

    showMatrix(matrixA);

    System.out.println(lu.getDeterminant());

}

From source file:com.yahoo.egads.utilities.TestSpectralMethods.java

@Test
public void f() {
    double[][] mat = { { 1, 2 }, { 3, 2 }, { 2, 5 }, { 7, 8 }, { 3, 4 }, { 8, 9 }, { 3, 3 } };
    RealMatrix data = MatrixUtils.createRealMatrix(mat);

    RealMatrix res = SpectralMethods.createHankelMatrix(data, 3);
    for (int i = 0; i < res.getRowDimension(); ++i) {
        System.out.println(Arrays.toString(res.getRow(i)));
    }/* w  w w  .j  a  v  a2  s.  c o m*/

    RealMatrix data2 = SpectralMethods.averageHankelMatrix(res, 3);
    for (int i = 0; i < data2.getRowDimension(); ++i) {
        System.out.println(Arrays.toString(data2.getRow(i)));
    }
}

From source file:br.unicamp.ic.recod.gpsi.measures.gpsiWilcoxonRankSumTestScore.java

@Override
public double score(double[][][] samples) {
    double p_value;

    RealMatrix m0 = MatrixUtils.createRealMatrix(samples[0]);
    RealMatrix m1 = MatrixUtils.createRealMatrix(samples[1]);

    MannWhitneyUTest t = new MannWhitneyUTest();
    p_value = t.mannWhitneyUTest(m0.getColumn(0), m1.getColumn(0));

    return p_value;
}

From source file:br.unicamp.ic.recod.gpsi.measures.gpsiDistanceOfMediansScore.java

@Override
public double score(double[][][] input) {

    Median median = new Median();

    RealMatrix matrix0 = MatrixUtils.createRealMatrix(input[0]);
    RealMatrix matrix1 = MatrixUtils.createRealMatrix(input[1]);

    return Math.abs(median.evaluate(matrix0.getColumn(0)) - median.evaluate(matrix1.getColumn(0)));

}

From source file:br.unicamp.ic.recod.gpsi.measures.gpsiHellingerDistanceScore.java

@Override
public double score(double[][][] input) {

    double dist[][] = new double[2][];

    int bins = 1000;

    dist[0] = MatrixUtils.createRealMatrix(input[0]).getColumn(0);
    dist[1] = MatrixUtils.createRealMatrix(input[1]).getColumn(0);

    gpsiHistogram hist = new gpsiHistogram();
    double globalMin = (new Min()).evaluate(ArrayUtils.addAll(dist[0], dist[1]));
    double globalMax = (new Max()).evaluate(ArrayUtils.addAll(dist[0], dist[1]));

    double[] h0 = hist.distribution(dist[0], bins, globalMin, globalMax);
    double[] h1 = hist.distribution(dist[1], bins, globalMin, globalMax);

    double BC = 0.0;

    for (int i = 0; i < bins; i++)
        BC += Math.sqrt(h0[i] * h1[i]);

    return Math.sqrt(1 - BC);

}

From source file:br.unicamp.ic.recod.gpsi.measures.gpsiNormalBhattacharyyaDistanceScore.java

@Override
public double score(double[][][] input) {

    Mean mean = new Mean();
    Variance var = new Variance();

    double mu0, sigs0, mu1, sigs1;
    double dist[][] = new double[2][];

    dist[0] = MatrixUtils.createRealMatrix(input[0]).getColumn(0);
    dist[1] = MatrixUtils.createRealMatrix(input[1]).getColumn(0);

    mu0 = mean.evaluate(dist[0]);/*from   www.  ja  v  a 2s.  co m*/
    sigs0 = var.evaluate(dist[0]) + Double.MIN_VALUE;
    mu1 = mean.evaluate(dist[1]);
    sigs1 = var.evaluate(dist[1]) + Double.MIN_VALUE;

    double distance = (Math.log((sigs0 / sigs1 + sigs1 / sigs0 + 2) / 4)
            + (Math.pow(mu1 - mu0, 2.0) / (sigs0 + sigs1))) / 4;

    return distance == Double.POSITIVE_INFINITY ? 0 : distance;

}

From source file:br.unicamp.ic.recod.gpsi.measures.gpsiDualScore.java

@Override
public double score(double[][][] input) {

    int least;// w w w  .  j  a  v a 2  s .  c o  m

    Min min = new Min();
    Max max = new Max();

    double dist[][] = new double[2][];
    double limits[][] = new double[2][2];

    for (int i = 0; i <= 1; i++) {
        dist[i] = MatrixUtils.createRealMatrix(input[i]).getColumn(0);
        limits[i] = new double[] { min.evaluate(dist[i]), max.evaluate(dist[i]) };
    }

    least = limits[0][0] <= limits[1][0] ? 0 : 1;
    double sep = limits[1 - least][0] - limits[least][1];

    if (sep >= 0)
        return sep / (max.evaluate(new double[] { limits[0][1], limits[1][1] })
                - min.evaluate(new double[] { limits[0][0], limits[1][0] }));

    double n = 0;

    for (int j = 0; j <= 1; j++)
        for (int i = 0; i < dist[j].length; i++) {
            if (dist[j][i] >= limits[1 - least][0] && dist[j][i] <= limits[least][1])
                n++;
        }

    return -n / (dist[0].length + dist[1].length);

}

From source file:com.caseystella.analytics.outlier.batch.rpca.RidgeRegression.java

public RidgeRegression(double[][] x, double[] y) {
    this.X = MatrixUtils.createRealMatrix(x);
    this.X_svd = null;
    this.Y = y;/*from  w  ww  .  j  ava  2s. c o  m*/
    this.l2penalty = 0;
    this.coefficients = null;

    this.fitted = new double[y.length];
    this.residuals = new double[y.length];
}

From source file:br.unicamp.ic.recod.gpsi.ml.gpsi1NNToMomentScalarClassificationAlgorithm.java

@Override
public void fit(HashMap<Byte, ArrayList<double[]>> x) {

    int i;//from   w w  w. j a va  2 s  .  c o m
    double[] centroid;
    RealMatrix entities;

    this.centroids = new HashMap<>();
    this.nClasses = x.size();
    this.dimensionality = 0;
    for (byte label : x.keySet()) {
        if (this.dimensionality <= 0)
            this.dimensionality = x.get(label).get(0).length;
        entities = MatrixUtils.createRealMatrix(x.get(label).toArray(new double[0][]));
        centroid = new double[this.dimensionality];
        for (i = 0; i < this.dimensionality; i++)
            centroid[i] = this.moment.evaluate(entities.getColumn(i));
        this.centroids.put(label, centroid);
    }

}

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();/*from   www  .jav  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);
}