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:com.clust4j.algo.preprocess.RobustScaler.java

@Override
public RealMatrix inverseTransform(RealMatrix X) {
    checkFit();/*from w w  w . j  a v a 2  s  .co m*/

    // 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 != this.centerer.medians.length)
        throw new DimensionMismatchException(n, this.centerer.medians.length);

    // First, multiply back by scales
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            data[i][j] *= scale[j];

            // To avoid a second pass of O(M*N), we
            // won't call the inverseTransform in the centerer,
            // we will just explicitly add the median back here.
            data[i][j] += centerer.medians[j];
        }
    }

    return new Array2DRowRealMatrix(data, false);
}

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

@Override
public RealMatrix inverseTransform(RealMatrix X) {
    checkFit();//w w w.j av  a  2 s.c o  m

    // 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] *= stdevs[j]; // first re-scale
            data[i][j] += means[j]; // then add means
        }
    }

    return new Array2DRowRealMatrix(data, false);
}

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

@Override
public RealMatrix inverseTransform(RealMatrix X) {
    checkFit();//from w  ww.  java 2 s  .co m

    // 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 != mins.length)
        throw new DimensionMismatchException(n, mins.length);

    double rng, mn;
    for (int j = 0; j < n; j++) {
        mn = mins[j];
        rng = maxes[j] - mn;

        for (int i = 0; i < m; i++) {
            data[i][j] -= min; // First subtract the min
            data[i][j] /= (max - min); // then divide over max - min
            data[i][j] *= rng; // multiply back by the range
            data[i][j] += mn; // finally add the mn back
        }
    }

    return new Array2DRowRealMatrix(data, false);
}

From source file:com.clust4j.algo.RadiusNeighbors.java

public Neighborhood getNeighbors(RealMatrix x, double rad) {
    return getNeighbors(x.getData(), rad, parallel);
}

From source file:com.analog.lyric.dimple.test.solvers.sumproduct.TestSampledFactors.java

/**
 * Adapted from MATLAB test4 in tests/algoGaussian/testSampledFactors.m
 *//* w  w  w  . j  a  v  a 2  s . c  o  m*/
@Test
public void sampledComplexProduct() {
    // NOTE: test may fail if seed is changed! We keep the number of samples down so that the test doesn't
    // take too long. Increasing the samples produces better results.

    testRand.setSeed(42);

    try (CurrentModel cur = using(new FactorGraph())) {
        final Complex a = complex("a");
        final Complex b = complex("b");
        final Complex c = product(a, b);

        double[] aMean = new double[] { 10, 10 };
        RealMatrix aCovariance = randCovariance(2);
        a.setPrior(new MultivariateNormal(aMean, aCovariance.getData()));

        double[] bMean = new double[] { -20, 20 };
        RealMatrix bCovariance = randCovariance(2);
        b.setPrior(new MultivariateNormalParameters(bMean, bCovariance.getData()));

        GaussianRandomGenerator normalGenerator = new GaussianRandomGenerator(testRand);
        CorrelatedRandomVectorGenerator aGenerator = new CorrelatedRandomVectorGenerator(aMean, aCovariance,
                1e-12, normalGenerator);
        CorrelatedRandomVectorGenerator bGenerator = new CorrelatedRandomVectorGenerator(bMean, bCovariance,
                1e-12, normalGenerator);

        StorelessCovariance expectedCov = new StorelessCovariance(2);

        final int nSamples = 10000;

        RealVector expectedMean = MatrixUtils.createRealVector(new double[2]);
        double[] cSample = new double[2];

        for (int i = 0; i < nSamples; ++i) {
            double[] aSample = aGenerator.nextVector();
            double[] bSample = bGenerator.nextVector();

            // Compute complex product
            cSample[0] = aSample[0] * bSample[0] - aSample[1] * bSample[1];
            cSample[1] = aSample[0] * bSample[1] + aSample[1] * bSample[0];

            expectedMean.addToEntry(0, cSample[0]);
            expectedMean.addToEntry(1, cSample[1]);

            expectedCov.increment(cSample);
        }

        expectedMean.mapDivideToSelf(nSamples); // normalize

        SumProductSolverGraph sfg = requireNonNull(cur.graph.setSolverFactory(new SumProductSolver()));
        sfg.setOption(GibbsOptions.numSamples, nSamples);

        sfg.solve();

        MultivariateNormalParameters cBelief = requireNonNull(c.getBelief());

        RealVector observedMean = MatrixUtils.createRealVector(cBelief.getMean());
        double scaledMeanDistance = expectedMean.getDistance(observedMean) / expectedMean.getNorm();

        //         System.out.format("expectedMean = %s\n", expectedMean);
        //         System.out.format("observedMean = %s\n", observedMean);
        //         System.out.println(scaledMeanDistance);

        assertEquals(0.0, scaledMeanDistance, .02);

        RealMatrix expectedCovariance = expectedCov.getCovarianceMatrix();
        RealMatrix observedCovariance = MatrixUtils.createRealMatrix(cBelief.getCovariance());
        RealMatrix diffCovariance = expectedCovariance.subtract(observedCovariance);

        double scaledCovarianceDistance = diffCovariance.getNorm() / expectedCovariance.getNorm();

        //         System.out.println(expectedCovariance);
        //         System.out.println(expectedCovariance.getNorm());
        //         System.out.println(diffCovariance);
        //         System.out.println(diffCovariance.getNorm());
        //         System.out.println(diffCovariance.getNorm() / expectedCovariance.getNorm());

        assertEquals(0.0, scaledCovarianceDistance, .2);
    }
}

From source file:net.semanticmetadata.lire.filter.LsaFilter.java

/**
 * @param results/*ww  w.  j a va 2 s.c  o m*/
 * @param query
 * @return the filtered results or null if error occurs.
 */
public ImageSearchHits filter(ImageSearchHits results, Document query) {
    // create a double[items][histogram]
    tempFeature = null;
    LinkedList<double[]> features = new LinkedList<double[]>();
    try {
        tempFeature = (LireFeature) featureClass.newInstance();
    } catch (Exception e) {
        logger.severe("Could not create feature " + featureClass.getName() + " (" + e.getMessage() + ").");
        return null;
    }
    // get all features from the result set, take care of those that do not have the respective field.
    for (int i = 0; i < results.length(); i++) {
        Document d = results.doc(i);
        if (d.getField(fieldName) != null) {
            tempFeature.setByteArrayRepresentation(d.getField(fieldName).binaryValue().bytes,
                    d.getField(fieldName).binaryValue().offset, d.getField(fieldName).binaryValue().length);
            features.add(tempFeature.getDoubleHistogram());
        }
    }
    // now go for the query
    if (query.getField(fieldName) != null) {
        tempFeature.setByteArrayRepresentation(query.getField(fieldName).binaryValue().bytes,
                query.getField(fieldName).binaryValue().offset, query.getField(fieldName).binaryValue().length);
    } else {
        logger.severe("Query document is missing the given feature " + featureClass.getName() + ".");
        return null;
    }
    double[][] matrixData = new double[features.size() + 1][tempFeature.getDoubleHistogram().length];
    System.arraycopy(tempFeature.getDoubleHistogram(), 0, matrixData[0], 0,
            tempFeature.getDoubleHistogram().length);
    int count = 1;
    for (Iterator<double[]> iterator = features.iterator(); iterator.hasNext();) {
        double[] next = iterator.next();
        System.arraycopy(next, 0, matrixData[count], 0, next.length);
        count++;
    }
    for (int i = 0; i < matrixData.length; i++) {
        double[] doubles = matrixData[i];
        for (int j = 0; j < doubles.length; j++) {
            if (Double.isNaN(doubles[j]))
                System.err.println("Value is NaN");
            ;
        }
    }
    // create a matrix object and do the magic
    Array2DRowRealMatrix m = new Array2DRowRealMatrix(matrixData);
    long ms = System.currentTimeMillis();
    SingularValueDecomposition svd = new SingularValueDecomposition(m);
    ms = System.currentTimeMillis() - ms;
    double[] singularValues = svd.getSingularValues();
    RealMatrix s = svd.getS();
    // if no number of dimensions is given reduce to a tenth.
    if (numberOfDimensions < 1)
        numberOfDimensions = singularValues.length / 10;
    for (int i = numberOfDimensions; i < singularValues.length; i++) {
        s.setEntry(i, i, 0);
    }
    RealMatrix mNew = svd.getU().multiply(s).multiply(svd.getVT());
    double[][] data = mNew.getData();

    // create the new result set
    TreeSet<SimpleResult> result = new TreeSet<SimpleResult>();
    double maxDistance = 0;
    double[] queryData = data[0];
    for (int i = 1; i < data.length; i++) {
        double[] doubles = data[i];
        double distance = MetricsUtils.distL1(doubles, queryData);
        result.add(new SimpleResult((float) distance, results.doc(i - 1), i - 1));
        maxDistance = Math.max(maxDistance, distance);
    }
    ImageSearchHits hits;
    hits = new SimpleImageSearchHits(result, (float) maxDistance);
    return hits;
}

From source file:com.clust4j.algo.NearestNeighbors.java

public Neighborhood getNeighbors(RealMatrix x, int k) {
    return getNeighbors(x.getData(), k, parallel);
}

From source file:net.semanticmetadata.lire.filters.LsaFilter.java

/**
 * @param results/*from w  ww.  ja va 2  s  .  c o  m*/
 * @param query
 * @return the filtered results or null if error occurs.
 */
public ImageSearchHits filter(ImageSearchHits results, IndexReader reader, Document query) {
    // create a double[items][histogram]
    tempFeature = null;
    LinkedList<double[]> features = new LinkedList<double[]>();
    try {
        tempFeature = (LireFeature) featureClass.newInstance();
    } catch (Exception e) {
        logger.severe("Could not create feature " + featureClass.getName() + " (" + e.getMessage() + ").");
        return null;
    }
    // get all features from the result set, take care of those that do not have the respective field.
    for (int i = 0; i < results.length(); i++) {
        Document d = null;
        try {
            d = reader.document(results.documentID(i));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (d.getField(fieldName) != null) {
            tempFeature.setByteArrayRepresentation(d.getField(fieldName).binaryValue().bytes,
                    d.getField(fieldName).binaryValue().offset, d.getField(fieldName).binaryValue().length);
            features.add(tempFeature.getFeatureVector());
        }
    }
    // now go for the query
    if (query.getField(fieldName) != null) {
        tempFeature.setByteArrayRepresentation(query.getField(fieldName).binaryValue().bytes,
                query.getField(fieldName).binaryValue().offset, query.getField(fieldName).binaryValue().length);
    } else {
        logger.severe("Query document is missing the given feature " + featureClass.getName() + ".");
        return null;
    }
    double[][] matrixData = new double[features.size() + 1][tempFeature.getFeatureVector().length];
    System.arraycopy(tempFeature.getFeatureVector(), 0, matrixData[0], 0,
            tempFeature.getFeatureVector().length);
    int count = 1;
    for (Iterator<double[]> iterator = features.iterator(); iterator.hasNext();) {
        double[] next = iterator.next();
        System.arraycopy(next, 0, matrixData[count], 0, next.length);
        count++;
    }
    for (int i = 0; i < matrixData.length; i++) {
        double[] doubles = matrixData[i];
        for (int j = 0; j < doubles.length; j++) {
            if (Double.isNaN(doubles[j]))
                System.err.println("Value is NaN");
            ;
        }
    }
    // create a matrix object and do the magic
    Array2DRowRealMatrix m = new Array2DRowRealMatrix(matrixData);
    long ms = System.currentTimeMillis();
    SingularValueDecomposition svd = new SingularValueDecomposition(m);
    ms = System.currentTimeMillis() - ms;
    double[] singularValues = svd.getSingularValues();
    RealMatrix s = svd.getS();
    // if no number of dimensions is given reduce to a tenth.
    if (numberOfDimensions < 1)
        numberOfDimensions = singularValues.length / 10;
    for (int i = numberOfDimensions; i < singularValues.length; i++) {
        s.setEntry(i, i, 0);
    }
    RealMatrix mNew = svd.getU().multiply(s).multiply(svd.getVT());
    double[][] data = mNew.getData();

    // create the new result set
    TreeSet<SimpleResult> result = new TreeSet<SimpleResult>();
    double maxDistance = 0;
    double[] queryData = data[0];
    for (int i = 1; i < data.length; i++) {
        double[] doubles = data[i];
        double distance = MetricsUtils.distL1(doubles, queryData);
        result.add(new SimpleResult((float) distance, results.documentID(i - 1)));
        maxDistance = Math.max(maxDistance, distance);
    }
    ImageSearchHits hits;
    hits = new SimpleImageSearchHits(result, (float) maxDistance);
    return hits;
}

From source file:edu.cudenver.bios.matrix.FixedRandomMatrix.java

/**
 * Perform scalar multiplication on a fixed / random matrix and
 * return the resulting scaled matrix.  The scale factor can be applied
 * to the entire matrix, or just the fixed submatrix.
 *
 * @param scale scale factor//from  w  w  w . ja v  a  2s.  c  o m
 * @param fixedOnly if true, only the fixed submatrix will be scaled
 * @return scaled combined matrix
 */
public RealMatrix scalarMultiply(double scale, boolean fixedOnly) {
    if (fixedOnly) {
        RealMatrix fixedScaled = fixedMatrix.scalarMultiply(scale);
        // update the combined matrix and return it
        combinedMatrix.setSubMatrix(fixedScaled.getData(), 0, 0);
        return combinedMatrix;
    } else {
        return combinedMatrix.scalarMultiply(scale);
    }
}

From source file:hivemall.utils.math.MatrixUtilsTest.java

@Test
public void testCombineMatrices1D() {
    RealMatrix[] m1 = new RealMatrix[] { new Array2DRowRealMatrix(new double[] { 0, 1 }),
            new Array2DRowRealMatrix(new double[] { 2, 3 }), new Array2DRowRealMatrix(new double[] { 4, 5 }) };
    RealMatrix flatten1 = MatrixUtils.combinedMatrices(m1);
    double[][] data = flatten1.getData();
    Assert.assertEquals(6, data.length);
    Assert.assertArrayEquals(new double[] { 0 }, data[0], 0.d);
    Assert.assertArrayEquals(new double[] { 1 }, data[1], 0.d);
    Assert.assertArrayEquals(new double[] { 2 }, data[2], 0.d);
    Assert.assertArrayEquals(new double[] { 3 }, data[3], 0.d);
    Assert.assertArrayEquals(new double[] { 4 }, data[4], 0.d);
    Assert.assertArrayEquals(new double[] { 5 }, data[5], 0.d);
}