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:com.analog.lyric.math.MoreMatrixUtils.java

/**
 * Return row-real matrix that uses specified underlying representation.
 * <p>//from   w w  w .  ja v a 2 s . c  o m
 * Simply shorthand for invoking {@link Array2DRowRealMatrix#Array2DRowRealMatrix(double[][], boolean)}
 * constructor with {@code copyArray} argument set to false.
 * @since 0.08
 */
public static Array2DRowRealMatrix wrapRealMatrix(double[][] data) {
    return new Array2DRowRealMatrix(data, false);
}

From source file:com.cloudera.oryx.common.math.VectorMath.java

/**
 * @param M tall, skinny matrix//ww w  .  jav  a 2s . co m
 * @return MT * M as a dense matrix
 */
public static RealMatrix transposeTimesSelf(Collection<float[]> M) {
    if (M == null || M.isEmpty()) {
        return null;
    }
    int features = 0;
    RealMatrix result = null;
    for (float[] vector : M) {
        if (result == null) {
            features = vector.length;
            result = new Array2DRowRealMatrix(features, features);
        }
        for (int row = 0; row < features; row++) {
            float rowValue = vector[row];
            for (int col = 0; col < features; col++) {
                result.addToEntry(row, col, rowValue * vector[col]);
            }
        }
    }
    return result;
}

From source file:com.itemanalysis.psychometrics.factoranalysis.MatrixUtils.java

/**
 * Elementwise multiplication of two matrices.
 *
 * @param A a matrix that is multiplied by the elements of B
 * @param B another matrix//w  ww. j a v a 2s  .  co m
 * @throws DimensionMismatchException
 */
public static void multiplyElementsBy(RealMatrix A, RealMatrix B) throws DimensionMismatchException {
    int nrow = A.getRowDimension();
    int ncol = A.getColumnDimension();
    if (nrow != B.getRowDimension()) {
        throw new DimensionMismatchException(nrow, B.getRowDimension());
    }
    if (ncol != B.getColumnDimension()) {
        throw new DimensionMismatchException(ncol, B.getColumnDimension());
    }

    RealMatrix M = new Array2DRowRealMatrix(nrow, ncol);
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            A.multiplyEntry(i, j, B.getEntry(i, j));
        }
    }
}

From source file:com.clust4j.metrics.pairwise.HaversineTest.java

@Test
public void test1() {

    final Array2DRowRealMatrix mat = new Array2DRowRealMatrix(coordinates, false);
    StandardScaler scaler = new StandardScaler().fit(mat);

    AbstractCentroidClusterer km;//from   w  w w . jav a  2  s. c om
    CentroidClustererParameters<? extends AbstractCentroidClusterer> planner;

    planner = new KMeansParameters(2).setVerbose(true).setMetric(Distance.HAVERSINE.MI).setVerbose(true);
    km = planner.fitNewModel(scaler.transform(mat));

    int[] kmlabels = km.getLabels();
    assertTrue(kmlabels[0] == kmlabels[1] && kmlabels[1] == kmlabels[2]);
    assertTrue(kmlabels[1] != kmlabels[3] && kmlabels[3] == kmlabels[4]);
}

From source file:com.itemanalysis.psychometrics.factoranalysis.ObliminCriteria.java

public void computeValues(RealMatrix L) {
    final int k = L.getColumnDimension();
    final int p = L.getRowDimension();
    RealMatrix I = new IdentityMatrix(p);
    RealMatrix L2 = MatrixUtils.multiplyElements(L, L);

    RealMatrix N = new Array2DRowRealMatrix(k, k);
    N.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override// w  w w .j  ava 2  s. c  o m
        public double visit(int row, int column, double value) {
            if (row == column)
                return 0.0;
            return 1.0;
        }
    });

    RealMatrix C = new Array2DRowRealMatrix(p, p);
    C.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            return gam / (double) p;
        }
    });

    RealMatrix X = I.subtract(C).multiply(L2).multiply(N);
    double sum = MatrixUtils.sumMatrix(MatrixUtils.multiplyElements(L2, X));
    functionValue = sum / 4.0;
    gradient = MatrixUtils.multiplyElements(L, X);

}

From source file:com.github.thorbenlindhauer.math.MathUtil.java

public RealMatrix invert() {
    if (!matrix.isSquare()) {
        throw new FactorOperationException("Cannot invert non-square matrix");
    }/*w  ww  . jav a 2 s .c  om*/
    ensureLUDecompositionInitialized();

    int matrixDimension = matrix.getRowDimension();

    RealMatrix inverseMatrix = new Array2DRowRealMatrix(matrixDimension, matrixDimension);
    RealMatrix identityMatrix = MatrixUtils.createRealIdentityMatrix(matrixDimension);

    DecompositionSolver solver = luDecomposition.getSolver();

    for (int i = 0; i < matrixDimension; i++) {
        RealVector identityColumn = identityMatrix.getColumnVector(i);
        RealVector inverseColumn = solver.solve(identityColumn);

        inverseMatrix.setColumnVector(i, inverseColumn);
    }

    return inverseMatrix;

}

From source file:edu.oregonstate.eecs.mcplan.ml.WekaGlue.java

public static SequentialProjectionHashLearner createSequentialProjectionHashLearner(final RandomGenerator rng,
        final Instances labeled, final Instances unlabeled, final int K, final double eta, final double alpha) {
    assert (labeled.classIndex() >= 0);
    final int Nfeatures = labeled.numAttributes() - 1;

    final RealMatrix X = new Array2DRowRealMatrix(Nfeatures, labeled.size() + unlabeled.size());
    final RealMatrix XL = new Array2DRowRealMatrix(Nfeatures, labeled.size() * 2);
    final RealMatrix S = new Array2DRowRealMatrix(XL.getColumnDimension(), XL.getColumnDimension());

    for (int j = 0; j < labeled.size(); ++j) {
        final Instance inst = labeled.get(j);
        for (int i = 0; i < XL.getRowDimension(); ++i) {
            X.setEntry(i, j, inst.value(i));
            XL.setEntry(i, j, inst.value(i));
        }/* ww w . j ava  2  s.  c o m*/

        int sj = -1;
        Instance s = null;
        do {
            sj = rng.nextInt(labeled.size());
            s = labeled.get(sj);
        } while (s == inst || s.classValue() != inst.classValue());
        S.setEntry(j, sj, 1);

        int dj = -1;
        Instance d = null;
        do {
            dj = rng.nextInt(labeled.size());
            d = labeled.get(dj);
        } while (d == inst || d.classValue() == inst.classValue());
        S.setEntry(j, dj, -1);
    }

    for (int j = 0; j < unlabeled.size(); ++j) {
        final Instance inst = unlabeled.get(j);
        for (int i = 0; i < X.getRowDimension(); ++i) {
            X.setEntry(i, labeled.size() + j, inst.value(i));
        }
    }

    return new SequentialProjectionHashLearner(X, XL, S, K, eta, alpha);
}

From source file:eagle.security.userprofile.impl.UserProfileAnomalyEigenEvaluator.java

private RealMatrix normalizeData(RealMatrix matrix, UserProfileEigenModel model) {
    RealMatrix normalizedData = new Array2DRowRealMatrix(matrix.getRowDimension(), matrix.getColumnDimension());
    if (LOG.isDebugEnabled())
        LOG.debug("model statistics size: " + model.statistics().length);
    for (int i = 0; i < matrix.getRowDimension(); i++) {
        for (int j = 0; j < matrix.getColumnDimension(); j++) {
            double value = (matrix.getEntry(i, j) - model.statistics()[j].getMean())
                    / model.statistics()[j].getStddev();
            normalizedData.setEntry(i, j, value);
        }//from www.ja va  2 s.  c om
    }
    return normalizedData;
}

From source file:de.biomedical_imaging.traj.math.RadiusGyrationTensor2D.java

/**
 * Calculates the radius of gyration tensor according to formula (6.3) in
 * /*from  w  w  w  .j a  v a 2  s. c  o m*/
 * ELEMENTS OF THE RANDOM WALK by Rudnick and Gaspari
 * 
 * @return Radius of gyration tensor
 */
public static Array2DRowRealMatrix getRadiusOfGyrationTensor(Trajectory t) {
    double meanx = 0;
    double meany = 0;
    for (int i = 0; i < t.size(); i++) {
        meanx += t.get(i).x;
        meany += t.get(i).y;
    }
    meanx = meanx / t.size();
    meany = meany / t.size();

    double e11 = 0;
    double e12 = 0;
    double e21 = 0;
    double e22 = 0;

    for (int i = 0; i < t.size(); i++) {
        e11 += Math.pow(t.get(i).x - meanx, 2);
        e12 += (t.get(i).x - meanx) * (t.get(i).y - meany);
        e22 += Math.pow(t.get(i).y - meany, 2);
    }
    e11 = e11 / t.size();
    e12 = e12 / t.size();
    e21 = e12;
    e22 = e22 / t.size();
    int rows = 2;
    int columns = 2;
    Array2DRowRealMatrix gyr = new Array2DRowRealMatrix(rows, columns);

    gyr.addToEntry(0, 0, e11);
    gyr.addToEntry(0, 1, e12);
    gyr.addToEntry(1, 0, e21);
    gyr.addToEntry(1, 1, e22);

    return gyr;
}

From source file:com.joptimizer.solvers.KKTSolver.java

public void setHMatrix(double[][] HMatrix) {
    this.H = new Array2DRowRealMatrix(HMatrix, false);
}