Search in sources :

Example 46 with Array2DRowRealMatrix

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

the class GATKProtectedMathUtilsTest method testRowSum.

@Test
public void testRowSum() {
    final double[][] array = { { 1, 2, 3 }, { 5, 5, 5 }, { 7, 8, 9 }, { -15, 2, 12 } };
    final double[] guess = GATKProtectedMathUtils.rowSums(new Array2DRowRealMatrix(array));
    final double[] gt = { 6, 15, 24, -1 };
    Assert.assertEquals(guess.length, 4);
    Assert.assertEquals(guess, gt);
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) Test(org.testng.annotations.Test)

Example 47 with Array2DRowRealMatrix

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

the class MatrixSummaryUtilsUnitTest method testGetRowVariances.

@Test(dataProvider = "rowVarianceTestData")
public void testGetRowVariances(double[][] testdata, double[] gtRowVariances) {
    final RealMatrix tmp = new Array2DRowRealMatrix(testdata);
    PoNTestUtils.assertEqualsDoubleArrays(MatrixSummaryUtils.getRowVariances(tmp), gtRowVariances);
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 48 with Array2DRowRealMatrix

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

the class GATKProtectedMathUtilsTest method testRowStdDevs.

@Test
public void testRowStdDevs() {
    final double[][] array = { { 1, 2, 3 }, { 5, 5, 5 }, { 7, 8, 9 }, { -15, 2, 12 } };
    final double[] rowStdDevs = GATKProtectedMathUtils.rowStdDevs(new Array2DRowRealMatrix(array));
    Assert.assertEquals(rowStdDevs[0], 1, 1e-8);
    Assert.assertEquals(rowStdDevs[1], 0, 1e-8);
    Assert.assertEquals(rowStdDevs[2], 1, 1e-8);
    Assert.assertEquals(rowStdDevs[3], 13.65039682, 1e-8);
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) Test(org.testng.annotations.Test)

Example 49 with Array2DRowRealMatrix

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

the class GATKProtectedMathUtilsTest method testColumnInf.

@Test
public void testColumnInf() {
    final double[][] array = { { 1, 2, Double.POSITIVE_INFINITY }, { 5, 5, 5 }, { 7, 8, 9 }, { -15, 2, 12 } };
    final double[] guess = GATKProtectedMathUtils.columnSums(new Array2DRowRealMatrix(array));
    final double[] gt = { -2, 17, Double.POSITIVE_INFINITY };
    Assert.assertEquals(guess.length, 3);
    Assert.assertEquals(guess[0], gt[0]);
    Assert.assertEquals(guess[1], gt[1]);
    Assert.assertTrue(Double.isInfinite(guess[2]));
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) Test(org.testng.annotations.Test)

Example 50 with Array2DRowRealMatrix

use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project incubator-systemml by apache.

the class LibCommonsMath method computeEigen.

/**
 * Function to perform Eigen decomposition on a given matrix.
 * Input must be a symmetric matrix.
 *
 * @param in matrix object
 * @return array of matrix blocks
 */
private static MatrixBlock[] computeEigen(MatrixObject in) {
    if (in.getNumRows() != in.getNumColumns()) {
        throw new DMLRuntimeException("Eigen Decomposition can only be done on a square matrix. Input matrix is rectangular (rows=" + in.getNumRows() + ", cols=" + in.getNumColumns() + ")");
    }
    Array2DRowRealMatrix matrixInput = DataConverter.convertToArray2DRowRealMatrix(in);
    EigenDecomposition eigendecompose = new EigenDecomposition(matrixInput);
    RealMatrix eVectorsMatrix = eigendecompose.getV();
    double[][] eVectors = eVectorsMatrix.getData();
    double[] eValues = eigendecompose.getRealEigenvalues();
    // Sort the eigen values (and vectors) in increasing order (to be compatible w/ LAPACK.DSYEVR())
    int n = eValues.length;
    for (int i = 0; i < n; i++) {
        int k = i;
        double p = eValues[i];
        for (int j = i + 1; j < n; j++) {
            if (eValues[j] < p) {
                k = j;
                p = eValues[j];
            }
        }
        if (k != i) {
            eValues[k] = eValues[i];
            eValues[i] = p;
            for (int j = 0; j < n; j++) {
                p = eVectors[j][i];
                eVectors[j][i] = eVectors[j][k];
                eVectors[j][k] = p;
            }
        }
    }
    MatrixBlock mbValues = DataConverter.convertToMatrixBlock(eValues, true);
    MatrixBlock mbVectors = DataConverter.convertToMatrixBlock(eVectors);
    return new MatrixBlock[] { mbValues, mbVectors };
}
Also used : EigenDecomposition(org.apache.commons.math3.linear.EigenDecomposition) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)141 RealMatrix (org.apache.commons.math3.linear.RealMatrix)101 Test (org.testng.annotations.Test)60 IntStream (java.util.stream.IntStream)31 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)28 File (java.io.File)27 Collectors (java.util.stream.Collectors)25 ArrayList (java.util.ArrayList)24 Assert (org.testng.Assert)24 List (java.util.List)22 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)22 Target (org.broadinstitute.hellbender.tools.exome.Target)18 java.util (java.util)15 Random (java.util.Random)14 ReadCountCollection (org.broadinstitute.hellbender.tools.exome.ReadCountCollection)14 ParamUtils (org.broadinstitute.hellbender.utils.param.ParamUtils)14 DataProvider (org.testng.annotations.DataProvider)14 Stream (java.util.stream.Stream)13 Arrays (java.util.Arrays)12 DoubleStream (java.util.stream.DoubleStream)12