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);
}
}
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]));
}
}
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);
}
}
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;
}
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;
}
Aggregations