Search in sources :

Example 11 with BlockRealMatrix

use of org.apache.commons.math3.linear.BlockRealMatrix in project activityinfo by bedatadriven.

the class ScoreMatrix method computeScoreMatrix.

public BlockRealMatrix computeScoreMatrix() {
    // First compute the scores of all possible matches between the source and targets
    int numPairs = getRowCount() * getColumnCount();
    int numDimensions = getDimensionCount();
    BlockRealMatrix matrix = new BlockRealMatrix(numPairs, numDimensions);
    int rowIndex = 0;
    for (int i = 0; i < getRowCount(); ++i) {
        for (int j = 0; j < getColumnCount(); ++j) {
            matrix.setRow(rowIndex++, score(i, j));
        }
    }
    return matrix;
}
Also used : BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix)

Example 12 with BlockRealMatrix

use of org.apache.commons.math3.linear.BlockRealMatrix in project MindsEye by SimiaCryptus.

the class PCAUtil method getCovariance.

/**
 * Forked from Apache Commons Math
 *
 * @param stream the stream
 * @return covariance covariance
 */
@Nonnull
public static RealMatrix getCovariance(@Nonnull final Supplier<Stream<double[]>> stream) {
    final int dimension = stream.get().findAny().get().length;
    final List<DoubleStatistics> statList = IntStream.range(0, dimension * dimension).mapToObj(i -> new DoubleStatistics()).collect(Collectors.toList());
    stream.get().forEach(array -> {
        for (int i = 0; i < dimension; i++) {
            for (int j = 0; j <= i; j++) {
                statList.get(i * dimension + j).accept(array[i] * array[j]);
            }
        }
        RecycleBin.DOUBLES.recycle(array, array.length);
    });
    @Nonnull final RealMatrix covariance = new BlockRealMatrix(dimension, dimension);
    for (int i = 0; i < dimension; i++) {
        for (int j = 0; j <= i; j++) {
            final double v = statList.get(i + dimension * j).getAverage();
            covariance.setEntry(i, j, v);
            covariance.setEntry(j, i, v);
        }
    }
    return covariance;
}
Also used : IntStream(java.util.stream.IntStream) BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix) DoubleStatistics(com.simiacryptus.util.data.DoubleStatistics) Tensor(com.simiacryptus.mindseye.lang.Tensor) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) RecycleBin(com.simiacryptus.mindseye.lang.RecycleBin) List(java.util.List) Stream(java.util.stream.Stream) EigenDecomposition(org.apache.commons.math3.linear.EigenDecomposition) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Comparator(java.util.Comparator) Nonnull(javax.annotation.Nonnull) BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Nonnull(javax.annotation.Nonnull) DoubleStatistics(com.simiacryptus.util.data.DoubleStatistics) BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix) Nonnull(javax.annotation.Nonnull)

Example 13 with BlockRealMatrix

use of org.apache.commons.math3.linear.BlockRealMatrix in project tetrad by cmu-phil.

the class DataUtils method simpleTest.

public static void simpleTest() {
    double[][] d = new double[][] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
    RealMatrix m = new BlockRealMatrix(d);
    System.out.println(m);
    System.out.println(times(m.transpose(), m));
    System.out.println(MatrixUtils.transposeWithoutCopy(m).multiply(m));
    TetradMatrix n = new TetradMatrix(m, m.getRowDimension(), m.getColumnDimension());
    System.out.println(n);
    RealMatrix q = n.getRealMatrix();
    RealMatrix q1 = MatrixUtils.transposeWithoutCopy(q);
    RealMatrix q2 = times(q1, q);
    System.out.println(new TetradMatrix(q2, q.getColumnDimension(), q.getColumnDimension()));
}
Also used : BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix)

Example 14 with BlockRealMatrix

use of org.apache.commons.math3.linear.BlockRealMatrix in project tetrad by cmu-phil.

the class DataUtils method times.

private static RealMatrix times(final RealMatrix m, final RealMatrix n) {
    if (m.getColumnDimension() != n.getRowDimension())
        throw new IllegalArgumentException("Incompatible matrices.");
    final int rowDimension = m.getRowDimension();
    final int columnDimension = n.getColumnDimension();
    final RealMatrix out = new BlockRealMatrix(rowDimension, columnDimension);
    final int NTHREADS = Runtime.getRuntime().availableProcessors();
    ForkJoinPool pool = ForkJoinPoolInstance.getInstance().getPool();
    for (int t = 0; t < NTHREADS; t++) {
        final int _t = t;
        Runnable worker = new Runnable() {

            @Override
            public void run() {
                int chunk = rowDimension / NTHREADS + 1;
                for (int row = _t * chunk; row < Math.min((_t + 1) * chunk, rowDimension); row++) {
                    if ((row + 1) % 100 == 0)
                        System.out.println(row + 1);
                    for (int col = 0; col < columnDimension; ++col) {
                        double sum = 0.0D;
                        int commonDimension = m.getColumnDimension();
                        for (int i = 0; i < commonDimension; ++i) {
                            sum += m.getEntry(row, i) * n.getEntry(i, col);
                        }
                        // double sum = m.getRowVector(row).dotProduct(n.getColumnVector(col));
                        out.setEntry(row, col, sum);
                    }
                }
            }
        };
        pool.submit(worker);
    }
    while (!pool.isQuiescent()) {
    }
    return out;
}
Also used : BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 15 with BlockRealMatrix

use of org.apache.commons.math3.linear.BlockRealMatrix in project imagej-ops by imagej.

the class DefaultInertiaTensor3D method calculate.

@Override
public RealMatrix calculate(final IterableRegion<B> input) {
    final BlockRealMatrix output = new BlockRealMatrix(3, 3);
    Cursor<Void> c = input.localizingCursor();
    double[] pos = new double[3];
    double[] computedCentroid = new double[3];
    centroid.calculate(input).localize(computedCentroid);
    final double mX = computedCentroid[0];
    final double mY = computedCentroid[1];
    final double mZ = computedCentroid[2];
    while (c.hasNext()) {
        c.fwd();
        c.localize(pos);
        output.setEntry(0, 0, output.getEntry(0, 0) + (pos[0] - mX) * (pos[0] - mX));
        output.setEntry(1, 1, output.getEntry(1, 1) + (pos[1] - mX) * (pos[1] - mY));
        output.setEntry(2, 2, output.getEntry(2, 2) + (pos[2] - mX) * (pos[2] - mZ));
        output.setEntry(0, 1, output.getEntry(0, 1) + (pos[0] - mY) * (pos[1] - mY));
        output.setEntry(1, 0, output.getEntry(0, 1));
        output.setEntry(0, 2, output.getEntry(0, 2) + (pos[0] - mY) * (pos[2] - mZ));
        output.setEntry(2, 0, output.getEntry(0, 2));
        output.setEntry(1, 2, output.getEntry(1, 2) + (pos[1] - mZ) * (pos[2] - mZ));
        output.setEntry(2, 1, output.getEntry(1, 2));
    }
    final double size = input.size();
    output.setEntry(0, 0, output.getEntry(0, 0) / size);
    output.setEntry(0, 1, output.getEntry(0, 1) / size);
    output.setEntry(0, 2, output.getEntry(0, 2) / size);
    output.setEntry(1, 0, output.getEntry(1, 0) / size);
    output.setEntry(1, 1, output.getEntry(1, 1) / size);
    output.setEntry(1, 2, output.getEntry(1, 2) / size);
    output.setEntry(2, 0, output.getEntry(2, 0) / size);
    output.setEntry(2, 1, output.getEntry(2, 1) / size);
    output.setEntry(2, 2, output.getEntry(2, 2) / size);
    return output;
}
Also used : BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix)

Aggregations

BlockRealMatrix (org.apache.commons.math3.linear.BlockRealMatrix)15 RealMatrix (org.apache.commons.math3.linear.RealMatrix)6 INDArray (org.nd4j.linalg.api.ndarray.INDArray)3 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)2 Test (org.testng.annotations.Test)2 RecycleBin (com.simiacryptus.mindseye.lang.RecycleBin)1 Tensor (com.simiacryptus.mindseye.lang.Tensor)1 DoubleStatistics (com.simiacryptus.util.data.DoubleStatistics)1 Comparator (java.util.Comparator)1 List (java.util.List)1 ForkJoinPool (java.util.concurrent.ForkJoinPool)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1 Nonnull (javax.annotation.Nonnull)1 Facet (net.imagej.ops.geom.geom3d.mesh.Facet)1 TriangularFacet (net.imagej.ops.geom.geom3d.mesh.TriangularFacet)1 RealLocalizable (net.imglib2.RealLocalizable)1 Vector3D (org.apache.commons.math3.geometry.euclidean.threed.Vector3D)1