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