Example usage for org.apache.commons.math3.linear Array2DRowRealMatrix Array2DRowRealMatrix

List of usage examples for org.apache.commons.math3.linear Array2DRowRealMatrix Array2DRowRealMatrix

Introduction

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

Prototype

public Array2DRowRealMatrix(final double[][] d, final boolean copyArray)
        throws DimensionMismatchException, NoDataException, NullArgumentException 

Source Link

Document

Create a new RealMatrix using the input array as the underlying data array.

Usage

From source file:msi.gama.util.matrix.GamaFloatMatrix.java

RealMatrix getRealMatrix() {
    final RealMatrix realMatrix = new Array2DRowRealMatrix(this.numRows, this.numCols);
    for (int i = 0; i < this.numRows; i++) {
        for (int j = 0; j < this.numCols; j++) {
            realMatrix.setEntry(i, j, this.get(null, j, i));
        }//  w w w  . jav  a  2 s  .  c om
    }
    return realMatrix;
}

From source file:com.clust4j.data.DataSet.java

public void sortAscInPlace(int colIdx) {
    if (colIdx < 0 || colIdx >= data.getColumnDimension())
        throw new IllegalArgumentException("col out of bounds");

    double[][] dataRef = data.getDataRef();
    data = new Array2DRowRealMatrix(MatUtils.sortAscByCol(dataRef, colIdx), false);
}

From source file:edu.stanford.cfuller.colocalization3d.correction.Correction.java

/**
 * Applies an existing correction to a single x-y position in the Image plane.
 *
 * @param x     The x-position at which to apply the correction.
 * @param y     The y-position at which to apply the correction.
 * @return      A RealVector containing 3 elements-- the magnitude of the correction in the x, y, and z dimensions, in that order.
 *//*from w w w  . jav a 2 s  .  c  o m*/
public RealVector correctPosition(double x, double y) throws UnableToCorrectException {

    RealVector corrections = new ArrayRealVector(3, 0.0);

    RealVector distsToCentroids = this.getPositionsForCorrection().getColumnVector(0).mapSubtract(x)
            .mapToSelf(new Power(2));
    distsToCentroids = distsToCentroids
            .add(this.getPositionsForCorrection().getColumnVector(1).mapSubtract(y).mapToSelf(new Power(2)));
    distsToCentroids.mapToSelf(new Sqrt());

    RealVector distRatio = distsToCentroids.ebeDivide(this.getDistanceCutoffs());

    RealVector distRatioBin = new ArrayRealVector(distRatio.getDimension(), 0.0);

    for (int i = 0; i < distRatio.getDimension(); i++) {
        if (distRatio.getEntry(i) <= 1)
            distRatioBin.setEntry(i, 1.0);
    }

    RealVector weights = distRatio.map(new Power(2.0)).mapMultiplyToSelf(-3).mapAddToSelf(1)
            .add(distRatio.map(new Power(3.0)).mapMultiplyToSelf(2));

    weights = weights.ebeMultiply(distRatioBin);

    double sumWeights = 0;

    int countWeights = 0;

    for (int i = 0; i < weights.getDimension(); i++) {
        if (weights.getEntry(i) > 0) {
            sumWeights += weights.getEntry(i);
            countWeights++;
        }
    }

    if (countWeights == 0) { // this means there were no points in the correction dataset near the position being corrected.
        throw (new UnableToCorrectException(
                "Incomplete coverage in correction dataset at (x,y) = (" + x + ", " + y + ")."));
    }

    RealMatrix cX = new Array2DRowRealMatrix(countWeights, this.getCorrectionX().getColumnDimension());
    RealMatrix cY = new Array2DRowRealMatrix(countWeights, this.getCorrectionX().getColumnDimension());
    RealMatrix cZ = new Array2DRowRealMatrix(countWeights, this.getCorrectionX().getColumnDimension());

    RealVector xVec = new ArrayRealVector(countWeights, 0.0);
    RealVector yVec = new ArrayRealVector(countWeights, 0.0);

    RealVector keptWeights = new ArrayRealVector(countWeights, 0.0);

    int keptCounter = 0;

    for (int i = 0; i < weights.getDimension(); i++) {
        if (weights.getEntry(i) > 0) {

            cX.setRowVector(keptCounter, this.getCorrectionX().getRowVector(i));
            cY.setRowVector(keptCounter, this.getCorrectionY().getRowVector(i));
            cZ.setRowVector(keptCounter, this.getCorrectionZ().getRowVector(i));

            xVec.setEntry(keptCounter, x - this.getPositionsForCorrection().getEntry(i, 0));
            yVec.setEntry(keptCounter, y - this.getPositionsForCorrection().getEntry(i, 1));

            keptWeights.setEntry(keptCounter, weights.getEntry(i));

            keptCounter++;
        }
    }

    double xCorr = 0;
    double yCorr = 0;
    double zCorr = 0;

    RealMatrix allCorrectionParameters = new Array2DRowRealMatrix(countWeights, numberOfCorrectionParameters);

    RealVector ones = new ArrayRealVector(countWeights, 1.0);

    allCorrectionParameters.setColumnVector(0, ones);
    allCorrectionParameters.setColumnVector(1, xVec);
    allCorrectionParameters.setColumnVector(2, yVec);
    allCorrectionParameters.setColumnVector(3, xVec.map(new Power(2)));
    allCorrectionParameters.setColumnVector(4, yVec.map(new Power(2)));
    allCorrectionParameters.setColumnVector(5, xVec.ebeMultiply(yVec));

    for (int i = 0; i < countWeights; i++) {

        xCorr += allCorrectionParameters.getRowVector(i).dotProduct(cX.getRowVector(i))
                * keptWeights.getEntry(i);
        yCorr += allCorrectionParameters.getRowVector(i).dotProduct(cY.getRowVector(i))
                * keptWeights.getEntry(i);
        zCorr += allCorrectionParameters.getRowVector(i).dotProduct(cZ.getRowVector(i))
                * keptWeights.getEntry(i);

    }

    xCorr /= sumWeights;
    yCorr /= sumWeights;
    zCorr /= sumWeights;

    corrections.setEntry(0, xCorr);
    corrections.setEntry(1, yCorr);
    corrections.setEntry(2, zCorr);

    return corrections;
}

From source file:com.clust4j.data.DataSet.java

public void sortDescInPlace(int colIdx) {
    if (colIdx < 0 || colIdx >= data.getColumnDimension())
        throw new IllegalArgumentException("col out of bounds");

    double[][] dataRef = data.getDataRef();
    data = new Array2DRowRealMatrix(MatUtils.sortDescByCol(dataRef, colIdx), false);
}

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

/**
 * Assert that when all of the matrix entries are exactly the same,
 * the algorithm will still converge, yet produce one label: 0
 *///from  www.  jav  a  2  s  .c  o  m
@Override
@Test
public void testAllSame() {
    final double[][] x = MatUtils.rep(-1, 6, 6);
    final Array2DRowRealMatrix X = new Array2DRowRealMatrix(x, false);

    Neighborhood neighb = new NearestNeighbors(X, new NearestNeighborsParameters(3).setVerbose(true)).fit()
            .getNeighbors();
    assertTrue(new MatUtils.MatSeries(neighb.getDistances(), Inequality.EQUAL_TO, 0).all());
    System.out.println();

    /*
     * Test def constructor
     */
    neighb = new NearestNeighbors(X, new NearestNeighborsParameters().setVerbose(true)).fit().getNeighbors();
    assertTrue(new MatUtils.MatSeries(neighb.getDistances(), Inequality.EQUAL_TO, 0).all());
    System.out.println();

    /*
     * Test BallTree
     */
    neighb = new NearestNeighbors(X,
            new NearestNeighborsParameters().setVerbose(true).setAlgorithm(NeighborsAlgorithm.BALL_TREE)).fit()
                    .getNeighbors();
    assertTrue(new MatUtils.MatSeries(neighb.getDistances(), Inequality.EQUAL_TO, 0).all());
    System.out.println();
}

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

/**
 * Assert that when all of the matrix entries are exactly the same,
 * the algorithm will still converge, yet produce one label: 0
 *///from ww  w. j av a  2  s . c om
@Override
@Test
public void testAllSame() {
    final double[][] x = MatUtils.rep(-1, 3, 3);
    final Array2DRowRealMatrix X = new Array2DRowRealMatrix(x, false);

    int[] labels = new KMeans(X, new KMeansParameters(3).setVerbose(true)).fit().getLabels();
    assertTrue(new VecUtils.IntSeries(labels, Inequality.EQUAL_TO, 0).all());
    System.out.println();
}

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

@Test
public void testAutoEstimationWithScale() {
    Array2DRowRealMatrix iris = (Array2DRowRealMatrix) new StandardScaler().fit(data_).transform(data_);
    final double[][] X = iris.getData();

    // MS estimates bw at 1.6041295821313855
    final double bandwidth = 1.6041295821313855;

    assertTrue(Precision.equals(/*  w  w  w .  j ava2  s.c o m*/
            MeanShift.autoEstimateBW(iris, 0.3, Distance.EUCLIDEAN, GlobalState.DEFAULT_RANDOM_STATE, false),
            bandwidth, 1e-9));

    assertTrue(Precision.equals(
            MeanShift.autoEstimateBW(iris, 0.3, Distance.EUCLIDEAN, GlobalState.DEFAULT_RANDOM_STATE, true),
            bandwidth, 1e-9));

    // Asserting fit works without breaking things...
    RadiusNeighbors r = new RadiusNeighbors(iris, new RadiusNeighborsParameters(bandwidth)).fit();

    TreeSet<MeanShiftSeed> centers = new TreeSet<>();
    for (double[] seed : X)
        centers.add(MeanShift.singleSeed(seed, r, X, 300));

    assertTrue(centers.size() == 4);

    double[][] expected_dists = new double[][] {
            new double[] { 0.50161528154395962, -0.31685274298813487, 0.65388162422893481,
                    0.65270450741975761 },
            new double[] { 0.52001211065400177, -0.29561728795619946, 0.67106269515983397,
                    0.67390853215763813 },
            new double[] { 0.54861244890482475, -0.25718786696105495, 0.68964559485632182,
                    0.69326664641211422 },
            new double[] { -1.0595457115461515, 0.74408909010240054, -1.2995708885010491,
                    -1.2545442961404225 } };

    int[] expected_centers = new int[] { 82, 80, 77, 45 };

    int idx = 0;
    for (MeanShiftSeed seed : centers) {
        assertTrue(VecUtils.equalsWithTolerance(seed.dists, expected_dists[idx], 1e-1));
        assertTrue(seed.count == expected_centers[idx]);
        idx++;
    }

    ArrayList<EntryPair<double[], Integer>> center_intensity = new ArrayList<>();
    for (MeanShiftSeed seed : centers) {
        if (null != seed) {
            center_intensity.add(seed.getPair());
        }
    }

    final ArrayList<EntryPair<double[], Integer>> sorted_by_intensity = center_intensity;

    // test getting the unique vals
    idx = 0;
    final int m_prime = sorted_by_intensity.size();
    final Array2DRowRealMatrix sorted_centers = new Array2DRowRealMatrix(m_prime, iris.getColumnDimension());
    for (Map.Entry<double[], Integer> e : sorted_by_intensity)
        sorted_centers.setRow(idx++, e.getKey());

    // Create a boolean mask, init true
    final boolean[] unique = new boolean[m_prime];
    for (int i = 0; i < unique.length; i++)
        unique[i] = true;

    // Fit the new neighbors model
    RadiusNeighbors nbrs = new RadiusNeighbors(sorted_centers,
            new RadiusNeighborsParameters(bandwidth).setVerbose(false)).fit();

    // Iterate over sorted centers and query radii
    int[] indcs;
    double[] center;
    for (int i = 0; i < m_prime; i++) {
        if (unique[i]) {
            center = sorted_centers.getRow(i);
            indcs = nbrs.getNeighbors(new double[][] { center }, bandwidth, false).getIndices()[0];

            for (int id : indcs) {
                unique[id] = false;
            }

            unique[i] = true; // Keep this as true
        }
    }

    // Now assign the centroids...
    int redundant_ct = 0;
    final ArrayList<double[]> centroids = new ArrayList<>();
    for (int i = 0; i < unique.length; i++) {
        if (unique[i]) {
            centroids.add(sorted_centers.getRow(i));
        }
    }

    redundant_ct = unique.length - centroids.size();

    assertTrue(redundant_ct == 2);
    assertTrue(centroids.size() == 2);
    assertTrue(VecUtils.equalsWithTolerance(centroids.get(0),
            new double[] { 0.4999404345258691, -0.3157948009929614, 0.6516983739795399, 0.6505251874544873 },
            1e-6));

    assertTrue(VecUtils.equalsExactly(centroids.get(1),
            new double[] { -1.0560079864392702, 0.7416046454700266, -1.295231741534238, -1.2503554887998656 }));

    // also put the centroids into a matrix. We have to
    // wait to perform this op, because we have to know
    // the size of centroids first...
    Array2DRowRealMatrix clust_centers = new Array2DRowRealMatrix(centroids.size(), iris.getColumnDimension());
    for (int i = 0; i < clust_centers.getRowDimension(); i++)
        clust_centers.setRow(i, centroids.get(i));

    // The final nearest neighbors model -- if this works, we are in the clear...
    new NearestNeighbors(clust_centers, new NearestNeighborsParameters(1)).fit();
}

From source file:ch.epfl.leb.sass.models.psfs.internal.GibsonLanniPSF.java

/**
 * Computes a digital representation of the PSF.
 * //www  . ja v  a 2 s  .co m
 * @param z The stage displacement.
 * @return An image stack of the PSF.
 **/
private void computeDigitalPSF(double z) {

    // Has a PSF has already been computed for this emitter's z-plane?
    Long zDiscrete = getNearestZPlane(this.eZ);
    if (interpolators.containsKey(zDiscrete)) {
        // PSF already computed for this z-plane, so don't do anything.
        return;
    }

    // Otherwise, compute the PSF and store the result in the hash map
    double x0 = (this.sizeX - 1) / 2.0D;
    double y0 = (this.sizeY - 1) / 2.0D;

    double xp = x0;
    double yp = y0;

    //ImageStack stack = new ImageStack(this.sizeX, this.sizeY);

    int maxRadius = (int) Math
            .round(Math.sqrt((this.sizeX - x0) * (this.sizeX - x0) + (this.sizeY - y0) * (this.sizeY - y0)))
            + 1;
    double[] r = new double[maxRadius * this.oversampling];
    double[] h = new double[r.length];

    double a = 0.0D;
    double b = Math.min(1.0D, this.ns / this.NA);

    double k0 = 2 * Math.PI / this.wavelength;
    double factor1 = this.MINWAVELENGTH / this.wavelength;
    double factor = factor1 * this.NA / 1.4;
    double deltaRho = (b - a) / (this.numSamples - 1);

    // basis construction
    double rho = 0.0D;
    double am = 0.0;
    double[][] Basis = new double[this.numSamples][this.numBasis];

    BesselJ bj0 = new BesselJ(0);
    BesselJ bj1 = new BesselJ(1);

    for (int m = 0; m < this.numBasis; m++) {
        am = (3 * m + 1) * factor;
        //                am = (3 * m + 1);
        for (int rhoi = 0; rhoi < this.numSamples; rhoi++) {
            rho = rhoi * deltaRho;
            Basis[rhoi][m] = bj0.value(am * rho);
        }
    }

    // compute the function to be approximated

    double ti = 0.0D;
    double OPD = 0;
    double W = 0;

    double[][] Coef = new double[1][this.numBasis * 2];
    double[][] Ffun = new double[this.numSamples][2];

    // Oil thickness.
    ti = (this.ti0 + z);
    double sqNA = this.NA * this.NA;
    double rhoNA2;
    for (int rhoi = 0; rhoi < this.numSamples; rhoi++) {
        rho = rhoi * deltaRho;
        rhoNA2 = rho * rho * sqNA;

        // OPD in the sample
        OPD = this.eZ * Math.sqrt(this.ns * this.ns - rhoNA2);

        // OPD in the immersion medium
        OPD += ti * Math.sqrt(this.ni * this.ni - rhoNA2) - this.ti0 * Math.sqrt(this.ni0 * this.ni0 - rhoNA2);

        // OPD in the coverslip
        OPD += this.tg * Math.sqrt(this.ng * this.ng - rhoNA2)
                - this.tg0 * Math.sqrt(this.ng0 * this.ng0 - rhoNA2);

        W = k0 * OPD;

        Ffun[rhoi][0] = Math.cos(W);
        Ffun[rhoi][1] = Math.sin(W);
    }

    // solve the linear system
    // begin....... (Using Common Math)
    RealMatrix coefficients = new Array2DRowRealMatrix(Basis, false);
    RealMatrix rhsFun = new Array2DRowRealMatrix(Ffun, false);

    DecompositionSolver solver = null;
    if (this.solverName.equals("svd")) {
        // slower but more accurate
        solver = new SingularValueDecomposition(coefficients).getSolver();
    } else {
        // faster, less accurate
        solver = new QRDecomposition(coefficients).getSolver();
    }

    RealMatrix solution = solver.solve(rhsFun);
    Coef = solution.getData();

    // end.......

    double[][] RM = new double[this.numBasis][r.length];
    double beta = 0.0D;

    double rm = 0.0D;
    for (int n = 0; n < r.length; n++) {
        r[n] = (n * 1.0 / this.oversampling);
        beta = k0 * this.NA * r[n] * this.resPSF;

        for (int m = 0; m < this.numBasis; m++) {
            am = (3 * m + 1) * factor;
            rm = am * bj1.value(am * b) * bj0.value(beta * b) * b;
            rm = rm - beta * b * bj0.value(am * b) * bj1.value(beta * b);
            RM[m][n] = rm / (am * am - beta * beta);

        }
    }

    // obtain one component
    double maxValue = 0.0D;

    for (int n = 0; n < r.length; n++) {
        double realh = 0.0D;
        double imgh = 0.0D;
        for (int m = 0; m < this.numBasis; m++) {
            realh = realh + RM[m][n] * Coef[m][0];
            imgh = imgh + RM[m][n] * Coef[m][1];

        }
        h[n] = realh * realh + imgh * imgh;
    }

    // Interpolate the PSF onto a 2D grid of physical coordinates
    double[] pixel = new double[this.sizeX * this.sizeY];

    for (int x = 0; x < this.sizeX; x++) {
        for (int y = 0; y < this.sizeY; y++) {
            // Distance in pixels from the center of the array
            double rPixel = Math.sqrt((x - xp) * (x - xp) + (y - yp) * (y - yp));

            // Find the index of the PSF h array that matches this distance
            int index = (int) Math.floor(rPixel * this.oversampling);

            // Perform a linear interpolation from h onto the current point.
            // (1 / this.oversampling) is the distance between samples in
            // the h array in units of pixels in the final output array.
            double value = h[index] + (h[(index + 1)] - h[index]) * (rPixel - r[index]) * this.oversampling;
            pixel[(x + this.sizeX * y)] = value;
        }
    }

    // Compute the constant that normalizes the PSF to its area
    double normConst = 0;
    for (int x = 0; x < this.sizeX; x++) {
        for (int y = 0; y < this.sizeY; y++) {
            normConst += pixel[x + this.sizeX * y] * this.resPSF * this.resPSF;
        }
    }

    // Compute the (discrete) cumulative distribution function.
    // First, compute the sums in the y-direction.
    double[] CDF = new double[this.sizeX * this.sizeY];
    double sum = 0;
    for (int x = 0; x < this.sizeX; x++) {
        sum = 0;
        for (int y = 0; y < this.sizeY; y++) {
            // Normalize to the integrated area
            pixel[x + this.sizeX * y] /= normConst;

            // Increment the sum in the y-direction and the CDF
            sum += pixel[x + this.sizeX * y] * this.resPSF;
            CDF[x + this.sizeX * y] = sum;
        }
    }

    // Next, compute the sums in the x-direction.
    for (int y = 0; y < this.sizeY; y++) {
        sum = 0;
        for (int x = 0; x < this.sizeX; x++) {
            // Increment the sum in the x-direction
            sum += CDF[x + y * this.sizeX] * this.resPSF;
            CDF[x + y * this.sizeX] = sum;
        }
    }

    // Construct the x-coordinates
    double[] mgridX = new double[this.sizeX];
    for (int x = 0; x < this.sizeX; x++) {
        mgridX[x] = (x - 0.5 * (this.sizeX - 1)) * this.resPSF;
    }

    // Construct the y-coordinates
    double[] mgridY = new double[this.sizeY];
    for (int y = 0; y < this.sizeY; y++) {
        mgridY[y] = (y - 0.5 * (this.sizeY - 1)) * this.resPSF;
    }

    //stack.addSlice(new FloatProcessor(this.sizeX, this.sizeY, pixel));
    //stack.addSlice(new FloatProcessor(this.sizeX, this.sizeY, CDF));

    // Reshape CDF for interpolation
    double[][] rCDF = new double[this.sizeY][this.sizeX];
    for (int x = 0; x < this.sizeX; x++) {
        for (int y = 0; y < this.sizeY; y++) {
            rCDF[y][x] = CDF[x + y * this.sizeX];
        }
    }

    // Compute the interpolating spline for this PSF.
    this.interpCDF = new PiecewiseBicubicSplineInterpolatingFunction(mgridX, mgridY, rCDF);

    interpolators.put(zDiscrete, new PiecewiseBicubicSplineInterpolatingFunction(mgridX, mgridY, rCDF));
}

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

@Test
public void compareToPckg() {
    Array2DRowRealMatrix X = new Array2DRowRealMatrix(MatUtils.reshape(new double[] { 0.58459246, 0.2411591,
            0.54266953, 0.80298748, 0.7108317, 0.77419375, 0.19460038, 0.51769224, 0.80581355, 0.06109043,
            0.57755264, 0.48690635, 0.4698578, 0.68256655, 0.35583625, 0.33956817, 0.46084149, 0.2266772,
            0.78013553, 0.84169725, 0.45929076, 0.5763663, 0.85034392, 0.42344478, 0.08823549 }, 5, 5), false);

    HDBSCAN h = new HDBSCAN(X).fit();
    assertTrue(VecUtils.equalsExactly(h.getLabels(), VecUtils.repInt(-1, 5)));

    h = new HDBSCAN(X, new HDBSCANParameters().setAlgo(HDBSCAN_Algorithm.PRIMS_KDTREE)).fit();
    assertTrue(VecUtils.equalsExactly(h.getLabels(), VecUtils.repInt(-1, 5)));

    h = new HDBSCAN(X, new HDBSCANParameters().setAlgo(HDBSCAN_Algorithm.PRIMS_BALLTREE)).fit();
    assertTrue(VecUtils.equalsExactly(h.getLabels(), VecUtils.repInt(-1, 5)));

    // Test on IRIS
    h = new HDBSCAN(iris, new HDBSCANParameters().setAlgo(HDBSCAN_Algorithm.GENERIC)).fit();

    int[] expectedLabels = new NoiseyLabelEncoder(expected_iris_labs).fit().getEncodedLabels();

    assertTrue(VecUtils.equalsExactly(expectedLabels, h.getLabels()));

    h = new HDBSCAN(X, new HDBSCANParameters().setAlgo(HDBSCAN_Algorithm.PRIMS_KDTREE)).fit();
    System.out.println(Arrays.toString(h.getLabels()));
}

From source file:com.ibm.bi.dml.runtime.util.DataConverter.java

/**
 * Helper method that converts SystemML matrix variable (<code>varname</code>) into a Array2DRowRealMatrix format,
 * which is useful in invoking Apache CommonsMath.
 * /*  w w  w. j  a va 2s. c o m*/
 * @param ec
 * @param varname
 * @return
 * @throws DMLRuntimeException
 */
public static Array2DRowRealMatrix convertToArray2DRowRealMatrix(MatrixObject mo) throws DMLRuntimeException {
    Matrix.ValueType vt = (mo.getValueType() == ValueType.DOUBLE ? Matrix.ValueType.Double
            : Matrix.ValueType.Integer);
    Matrix mathInput = new Matrix(mo.getFileName(), mo.getNumRows(), mo.getNumColumns(), vt);
    mathInput.setMatrixObject(mo);
    double[][] data = mathInput.getMatrixAsDoubleArray();
    Array2DRowRealMatrix matrixInput = new Array2DRowRealMatrix(data, false);

    return matrixInput;
}