Search in sources :

Example 1 with BlockRealMatrix

use of org.apache.commons.math3.linear.BlockRealMatrix in project gatk-protected by broadinstitute.

the class Nd4jApacheAdapterUtilsUnitTest method assertINDArrayToApacheMatrixCorrectness.

public void assertINDArrayToApacheMatrixCorrectness(final INDArray arr) {
    /* brute force result */
    final RealMatrix expected = new BlockRealMatrix(arr.rows(), arr.columns());
    for (int i = 0; i < arr.rows(); i++) {
        for (int j = 0; j < arr.columns(); j++) {
            expected.setEntry(i, j, arr.getDouble(i, j));
        }
    }
    final RealMatrix result = Nd4jApacheAdapterUtils.convertINDArrayToApacheMatrix(arr);
    Assert.assertEquals(result.getColumnDimension(), expected.getColumnDimension());
    Assert.assertEquals(result.getRowDimension(), expected.getRowDimension());
    for (int i = 0; i < expected.getRowDimension(); i++) {
        ArrayAsserts.assertArrayEquals(result.getData()[i], expected.getData()[i], EPS);
    }
}
Also used : BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix)

Example 2 with BlockRealMatrix

use of org.apache.commons.math3.linear.BlockRealMatrix in project gatk by broadinstitute.

the class Nd4jApacheAdapterUtils method convertINDArrayToApacheMatrix.

/**
     * INDArray to Apache
     *
     * @param matrix rank-2 INDArray
     * @return Apache matrix
     */
public static RealMatrix convertINDArrayToApacheMatrix(@Nonnull final INDArray matrix) {
    Utils.validateArg(matrix.rank() == 2, "Input rank is not 2 (not matrix)");
    final int[] shape = matrix.shape();
    final INDArray concreteMatrix = matrix.isView() ? matrix.dup() : matrix;
    final double[] data = concreteMatrix.data().asDouble();
    final char ordering = concreteMatrix.ordering();
    if (ordering == 'c') {
        return new BlockRealMatrix(monoToBiDiArrayRowMajor(data, shape[0], shape[1]));
    } else {
        /* ordering == 'f' */
        return new BlockRealMatrix(monoToBiDiArrayColumnMajor(data, shape[0], shape[1]));
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix)

Example 3 with BlockRealMatrix

use of org.apache.commons.math3.linear.BlockRealMatrix in project gatk by broadinstitute.

the class Nd4jApacheAdapterUtilsUnitTest method assertINDArrayToApacheMatrixCorrectness.

public void assertINDArrayToApacheMatrixCorrectness(final INDArray arr) {
    /* brute force result */
    final RealMatrix expected = new BlockRealMatrix(arr.rows(), arr.columns());
    for (int i = 0; i < arr.rows(); i++) {
        for (int j = 0; j < arr.columns(); j++) {
            expected.setEntry(i, j, arr.getDouble(i, j));
        }
    }
    final RealMatrix result = Nd4jApacheAdapterUtils.convertINDArrayToApacheMatrix(arr);
    Assert.assertEquals(result.getColumnDimension(), expected.getColumnDimension());
    Assert.assertEquals(result.getRowDimension(), expected.getRowDimension());
    for (int i = 0; i < expected.getRowDimension(); i++) {
        ArrayAsserts.assertArrayEquals(result.getData()[i], expected.getData()[i], EPS);
    }
}
Also used : BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix)

Example 4 with BlockRealMatrix

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

the class DefaultInertiaTensor3DMesh method calculate.

@Override
public RealMatrix calculate(final Mesh input) {
    final RealLocalizable o = centroid.calculate(input);
    BlockRealMatrix tensor = new BlockRealMatrix(3, 3);
    final Iterator<Facet> c = input.getFacets().iterator();
    while (c.hasNext()) {
        final TriangularFacet tf = (TriangularFacet) c.next();
        tensor = tensor.add(tetrahedronInertiaTensor(tf.getVertex(0), tf.getVertex(1), tf.getVertex(2), o));
    }
    return tensor;
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) TriangularFacet(net.imagej.ops.geom.geom3d.mesh.TriangularFacet) BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix) Facet(net.imagej.ops.geom.geom3d.mesh.Facet) TriangularFacet(net.imagej.ops.geom.geom3d.mesh.TriangularFacet)

Example 5 with BlockRealMatrix

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

the class DefaultInertiaTensor3DMesh method tetrahedronInertiaTensor.

/**
 * The computations are based on this paper:
 * http://docsdrive.com/pdfs/sciencepublications/jmssp/2005/8-11.pdf
 *
 * Note: In the paper b' and c' are swapped.
 *
 * @param p1
 *            triangular facet point
 * @param p2
 *            triangular facet point
 * @param p3
 *            triangular facet point
 * @param cent
 *            of the mesh
 * @return inertia tensor of this tetrahedron
 */
private BlockRealMatrix tetrahedronInertiaTensor(final RealLocalizable p1, final RealLocalizable p2, final RealLocalizable p3, final RealLocalizable cent) {
    final double originX = cent.getDoublePosition(0);
    final double originY = cent.getDoublePosition(1);
    final double originZ = cent.getDoublePosition(2);
    final double x1 = p1.getDoublePosition(0) - originX;
    final double y1 = p1.getDoublePosition(1) - originY;
    final double z1 = p1.getDoublePosition(2) - originZ;
    final double x2 = p2.getDoublePosition(0) - originX;
    final double y2 = p2.getDoublePosition(1) - originY;
    final double z2 = p2.getDoublePosition(2) - originZ;
    final double x3 = p3.getDoublePosition(0) - originX;
    final double y3 = p3.getDoublePosition(1) - originY;
    final double z3 = p3.getDoublePosition(2) - originZ;
    final double volume = tetrahedronVolume(new Vector3D(x1, y1, z1), new Vector3D(x2, y2, z2), new Vector3D(x3, y3, z3));
    final double a = 6 * volume * (y1 * y1 + y1 * y2 + y2 * y2 + y1 * y3 + y2 * y3 + y3 * y3 + z1 * z1 + z1 * z2 + z2 * z2 + z1 * z3 + z2 * z3 + z3 * z3) / 60.0;
    final double b = 6 * volume * (x1 * x1 + x1 * x2 + x2 * x2 + x1 * x3 + x2 * x3 + x3 * x3 + z1 * z1 + z1 * z2 + z2 * z2 + z1 * z3 + z2 * z3 + z3 * z3) / 60.0;
    final double c = 6 * volume * (x1 * x1 + x1 * x2 + x2 * x2 + x1 * x3 + x2 * x3 + x3 * x3 + y1 * y1 + y1 * y2 + y2 * y2 + y1 * y3 + y2 * y3 + y3 * y3) / 60.0;
    final double aa = 6 * volume * (2 * y1 * z1 + y2 * z1 + y3 * z1 + y1 * z2 + 2 * y2 * z2 + y3 * z2 + y1 * z3 + y2 * z3 + 2 * y3 * z3) / 120.0;
    final double bb = 6 * volume * (2 * x1 * y1 + x2 * y1 + x3 * y1 + x1 * y2 + 2 * x2 * y2 + x3 * y2 + x1 * y3 + x2 * y3 + 2 * x3 * y3) / 120.0;
    final double cc = 6 * volume * (2 * x1 * z1 + x2 * z1 + x3 * z1 + x1 * z2 + 2 * x2 * z2 + x3 * z2 + x1 * z3 + x2 * z3 + 2 * x3 * z3) / 120.0;
    final BlockRealMatrix t = new BlockRealMatrix(3, 3);
    t.setRow(0, new double[] { a, -bb, -cc });
    t.setRow(1, new double[] { -bb, b, -aa });
    t.setRow(2, new double[] { -cc, -aa, c });
    return t;
}
Also used : Vector3D(org.apache.commons.math3.geometry.euclidean.threed.Vector3D) 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