Java Code Examples for org.apache.commons.math3.linear.RealMatrix#getRowDimension()

The following examples show how to use org.apache.commons.math3.linear.RealMatrix#getRowDimension() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PoNTestUtils.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test whether two matrices are equal (within 1e-4)
 * @param left never {@code null}
 * @param right never {@code null}
 * @param isAllowNegatedValues whether values that are just negated are still considered equal.  True is useful for
 *                       the outputs of some matrix operations, such as SVD.
 */
public static void assertEqualsMatrix(final RealMatrix left, final RealMatrix right, final boolean isAllowNegatedValues) {
    Assert.assertEquals(left.getRowDimension(), right.getRowDimension());
    Assert.assertEquals(left.getColumnDimension(), right.getColumnDimension());
    for (int i = 0; i < left.getRowDimension(); i++) {
        final double[] leftRow = left.getRow(i);
        final double[] rightRow = right.getRow(i);
        for (int j = 0; j < leftRow.length; j++) {
            if (isAllowNegatedValues) {
                Assert.assertEquals(Math.abs(leftRow[j]), Math.abs(rightRow[j]), DOUBLE_MATRIX_TOLERANCE);
            } else {
                Assert.assertEquals(leftRow[j], rightRow[j], DOUBLE_MATRIX_TOLERANCE);
            }
        }
    }
}
 
Example 2
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Throws IllegalArgumentException of the matrix does not have at least
 * two columns and two rows
 *
 * @param matrix matrix to check for sufficiency
 * @throws MathIllegalArgumentException if there is insufficient data
 */
private void checkSufficientData(final RealMatrix matrix) {
    int nRows = matrix.getRowDimension();
    int nCols = matrix.getColumnDimension();
    if (nRows < 2 || nCols < 2) {
        throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_ROWS_AND_COLUMNS,
                                               nRows, nCols);
    }
}
 
Example 3
Source File: CMAESOptimizer_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m
 *            Input matrix
 * @return Matrix representing the element-wise square root of m.
 */
private static RealMatrix sqrt(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.sqrt(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 4
Source File: Arja_00166_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return the matrix where the elements of m and n are element-wise multiplied.
 */
private static RealMatrix times(final RealMatrix m, final RealMatrix n) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) * n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 5
Source File: Cardumen_00107_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise square (^2) of m.
 */
private static RealMatrix square(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            double e = m.getEntry(r, c);
            d[r][c] = e * e;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 6
Source File: 1_CMAESOptimizer.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param mat Input matrix.
 * @param n Number of row replicates.
 * @param m Number of column replicates.
 * @return a matrix which replicates the input matrix in both directions.
 */
private static RealMatrix repmat(final RealMatrix mat, int n, int m) {
    int rd = mat.getRowDimension();
    int cd = mat.getColumnDimension();
    double[][] d = new double[n * rd][m * cd];
    for (int r = 0; r < n * rd; r++) {
        for (int c = 0; c < m * cd; c++) {
            d[r][c] = mat.getEntry(r % rd, c % cd);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 7
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m
 *            Input matrix
 * @return Matrix representing the element-wise square root of m.
 */
private static RealMatrix sqrt(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.sqrt(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 8
Source File: JGenProg2017_0089_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param k Diagonal position.
 * @return Upper triangular part of matrix.
 */
private static RealMatrix triu(final RealMatrix m, int k) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = r <= c - k ? m.getEntry(r, c) : 0;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 9
Source File: NormalizeSomaticReadCountsIntegrationTest.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * This code reconstructs the expected pre-tangent normalization counts given the input,
 * and then compares against the actual pre-tangent output.
 * <p>
 * This method does not use any of the components that does the actual computation
 * in production, so that the calculation is independent.
 * </p>
 * <p>
 * This code also use an alternative way to calculate it to reduce overlap even further.
 * </p>
 * @param preTangentNormalized actual output.
 * @param factorNormalized input.
 */
private void assertPreTangentNormalizedValues(final ReadCountCollection factorNormalized, final ReadCountCollection preTangentNormalized) {
    final double epsilon = PCATangentNormalizationUtils.EPSILON;
    final RealMatrix outCounts = preTangentNormalized.counts();
    final RealMatrix inCounts = factorNormalized.counts();
    final double[] columnMeans = GATKProtectedMathUtils.columnMeans(inCounts);
    Assert.assertTrue(DoubleStream.of(columnMeans).allMatch(d -> d < 0.5));
    final double[][] expected = new double[inCounts.getRowDimension()][inCounts.getColumnDimension()];
    final double[] columnValues = new double[inCounts.getRowDimension()];
    for (int i = 0; i < columnMeans.length; i++) {
        for (int j = 0; j < inCounts.getRowDimension(); j++) {
            final double inValue = inCounts.getEntry(j,i);
            final double lowBoundedInValue = Math.max(epsilon, inValue / columnMeans[i]);
            final double outValue = Math.log(lowBoundedInValue) / Math.log(2);
            expected[j][i] = outValue;
            columnValues[j] = outValue;
        }
        Arrays.sort(columnValues);
        final int midIndex = columnValues.length >> 1;
        final double median = columnValues.length % 2 == 1 ?
                columnValues[midIndex] : (columnValues[midIndex] + columnValues[1 + midIndex]) * .5;
        for (int j = 0; j < inCounts.getRowDimension(); j++) {
            expected[j][i] -= median;
            Assert.assertEquals(outCounts.getEntry(j,i),expected[j][i],0.000001," Row " + j + " col " + i);
        }
    }
}
 
Example 10
Source File: JGenProg2017_00111_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param cols Columns to select.
 * @return Matrix representing the selected columns.
 */
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
    double[][] d = new double[m.getRowDimension()][cols.length];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < cols.length; c++) {
            d[r][c] = m.getEntry(r, cols[c]);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 11
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param cols Columns to select.
 * @return Matrix representing the selected columns.
 */
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
    final double[][] d = new double[m.getRowDimension()][cols.length];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < cols.length; c++) {
            d[r][c] = m.getEntry(r, cols[c]);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 12
Source File: Cardumen_00105_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param cols Columns to select.
 * @return Matrix representing the selected columns.
 */
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
    double[][] d = new double[m.getRowDimension()][cols.length];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < cols.length; c++) {
            d[r][c] = m.getEntry(r, cols[c]);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 13
Source File: Cardumen_00107_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param k Diagonal position.
 * @return Upper triangular part of matrix.
 */
private static RealMatrix triu(final RealMatrix m, int k) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = r <= c - k ? m.getEntry(r, c) : 0;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 14
Source File: Cardumen_00211_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param k Diagonal position.
 * @return Upper triangular part of matrix.
 */
private static RealMatrix triu(final RealMatrix m, int k) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = r <= c - k ? m.getEntry(r, c) : 0;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 15
Source File: jKali_0024_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise logarithm of m.
 */
private static RealMatrix log(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.log(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 16
Source File: Cardumen_00257_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise logarithm of m.
 */
private static RealMatrix log(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.log(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 17
Source File: Arja_00181_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m
 *            Input matrix
 * @return Matrix representing the element-wise square root of m.
 */
private static RealMatrix sqrt(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = Math.sqrt(m.getEntry(r, c));
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 18
Source File: JGenProg2017_0020_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Copies a column from m1 to m2.
 *
 * @param m1 Source matrix 1.
 * @param col1 Source column.
 * @param m2 Target matrix.
 * @param col2 Target column.
 */
private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) {
    for (int i = 0; i < m1.getRowDimension(); i++) {
        m2.setEntry(i, col2, m1.getEntry(i, col1));
    }
}
 
Example 19
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a PearsonsCorrelation from a RealMatrix whose columns
 * represent variables to be correlated.
 *
 * @param matrix matrix with columns representing variables to correlate
 */
public PearsonsCorrelation(RealMatrix matrix) {
    checkSufficientData(matrix);
    nObs = matrix.getRowDimension();
    correlationMatrix = computeCorrelationMatrix(matrix);
}
 
Example 20
Source File: Arja_0079_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Copies a column from m1 to m2.
 *
 * @param m1 Source matrix 1.
 * @param col1 Source column.
 * @param m2 Target matrix.
 * @param col2 Target column.
 */
private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) {
    for (int i = 0; i < m1.getRowDimension(); i++) {
        m2.setEntry(i, col2, m1.getEntry(i, col1));
    }
}