use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.
the class SingularValueDecomposerUnitTest method assertSVD.
public void assertSVD(final SVD svd, final RealMatrix m) {
final RealMatrix U = svd.getU();
final RealMatrix V = svd.getV();
final double[] s = svd.getSingularValues();
final RealMatrix S = new Array2DRowRealMatrix(U.getColumnDimension(), V.getColumnDimension());
IntStream.range(0, s.length).forEach(n -> S.setEntry(n, n, s[n]));
final RealMatrix SPrime = new Array2DRowRealMatrix(V.getColumnDimension(), U.getColumnDimension());
IntStream.range(0, s.length).forEach(n -> SPrime.setEntry(n, n, 1 / s[n]));
Assert.assertEquals(U.multiply(S).multiply(V.transpose()), m);
Assert.assertEquals(V.transpose().multiply(V), MatrixUtils.createRealIdentityMatrix(2), "V is unitary");
Assert.assertEquals(U.transpose().multiply(U), MatrixUtils.createRealIdentityMatrix(2), "U is unitary");
Assert.assertTrue(isUnitaryMatrix(U), "matrix U");
Assert.assertTrue(isUnitaryMatrix(V), "matrix V");
Assert.assertEquals(U.multiply(S).multiply(V.transpose()), m);
Assert.assertEquals(V.multiply(SPrime).multiply(U.transpose()), svd.getPinv());
assertPseudoinverse(m, svd.getPinv());
}
use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.
the class CaseToPoNTargetMapper method fromCaseToPoNCounts.
/**
* Re-arrange case read counts counts based on PoN target order.
* @param caseCounts the input counts to rearrange.
* @return never {@code null}, a new matrix with row sorted according to the PoN target order.
*/
public RealMatrix fromCaseToPoNCounts(final RealMatrix caseCounts) {
Utils.nonNull(caseCounts, "Case-sample counts cannot be null.");
final RealMatrix result = new Array2DRowRealMatrix(ponTargetNames.size(), caseCounts.getColumnDimension());
for (int i = 0; i < ponTargetNames.size(); i++) {
final String targetName = ponTargetNames.get(i);
final Integer inputIndex = caseTargetIndexes.get(targetName);
if (inputIndex == null) {
throw new UserException.BadInput(String.format("missing PoN target name %s in input read counts", targetName));
}
result.setRow(i, caseCounts.getRow(inputIndex));
}
return result;
}
use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.
the class CaseToPoNTargetMapper method fromPoNToCaseCounts.
/**
* Re-arrange the input rows from the PoN to the case data target order.
* @param ponCounts count matrix with row organized using the PoN target order.
* @return never {@code null} a new matrix with the row order changed according to the case read count target order.
*/
public RealMatrix fromPoNToCaseCounts(final RealMatrix ponCounts) {
final Map<String, Integer> ponTargetsIndexes = IntStream.range(0, ponTargetNames.size()).boxed().collect(Collectors.toMap(ponTargetNames::get, Function.identity()));
final RealMatrix result = new Array2DRowRealMatrix(ponCounts.getRowDimension(), ponCounts.getColumnDimension());
for (int i = 0; i < outputTargets.size(); i++) {
final Target target = outputTargets.get(i);
result.setRow(i, ponCounts.getRow(ponTargetsIndexes.get(target.getName())));
}
return result;
}
use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.
the class SparkConverter method convertSparkRowMatrixToRealMatrix.
/**
* Create an Apache Commons RealMatrix from a Spark RowMatrix.
*
* @param r Never {@code null}
* @param cachedNumRows Checking the number of rows in {@code r} can be time-consuming. Provide the value here, if it is already known.
* Use {@code -1} if unknown.
* @return Never {@code null}
*/
public static RealMatrix convertSparkRowMatrixToRealMatrix(final RowMatrix r, final int cachedNumRows) {
Utils.nonNull(r, "Input row matrix cannot be null");
int numRows;
if (cachedNumRows == -1) {
// This takes a while in Spark
numRows = (int) r.numRows();
} else {
numRows = cachedNumRows;
}
final int numCols = (int) r.numCols();
// This cast is required, even though it would not seem necessary, at first. Exact reason why is unknown.
// Will fail compilation if the cast is removed.
final Vector[] rowVectors = (Vector[]) r.rows().collect();
final RealMatrix result = new Array2DRowRealMatrix(numRows, numCols);
for (int i = 0; i < numRows; i++) {
result.setRow(i, rowVectors[i].toArray());
}
return result;
}
use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.
the class IntegerCopyNumberTransitionMatrixUnitTest method testUnnormalizedProbability.
@Test
public void testUnnormalizedProbability() {
/* it should normalize unnormalized transition matrices and give a warning */
final IntegerCopyNumberTransitionMatrix transitionMatrix = new IntegerCopyNumberTransitionMatrix(new Array2DRowRealMatrix(new double[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }), 0);
for (int i = 0; i < 3; i++) {
final double[] col = transitionMatrix.getTransitionMatrix().getColumn(i);
Assert.assertEquals(Arrays.stream(col).sum(), 1.0, 1e-12);
}
}
Aggregations