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

The following examples show how to use org.apache.commons.math.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: CorrelatedRandomVectorGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Simple constructor.
 * <p>Build a correlated random vector generator from its mean
 * vector and covariance matrix.</p>
 * @param mean expected mean values for all components
 * @param covariance covariance matrix
 * @param small diagonal elements threshold under which  column are
 * considered to be dependent on previous ones and are discarded
 * @param generator underlying generator for uncorrelated normalized
 * components
 * @exception IllegalArgumentException if there is a dimension
 * mismatch between the mean vector and the covariance matrix
 * @exception NotPositiveDefiniteMatrixException if the
 * covariance matrix is not strictly positive definite
 * @exception DimensionMismatchException if the mean and covariance
 * arrays dimensions don't match
 */
public CorrelatedRandomVectorGenerator(double[] mean,
                                       RealMatrix covariance, double small,
                                       NormalizedRandomGenerator generator)
throws NotPositiveDefiniteMatrixException, DimensionMismatchException {

    int order = covariance.getRowDimension();
    if (mean.length != order) {
        throw new DimensionMismatchException(mean.length, order);
    }
    this.mean = mean.clone();

    decompose(covariance, small);

    this.generator = generator;
    normalized = new double[rank];

}
 
Example 2
Source File: CorrelatedRandomVectorGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a correlated random vector generator from its mean
 * vector and covariance matrix.
 *
 * @param mean Expected mean values for all components.
 * @param covariance Covariance matrix.
 * @param small Diagonal elements threshold under which  column are
 * considered to be dependent on previous ones and are discarded
 * @param generator underlying generator for uncorrelated normalized
 * components.
 * @throws org.apache.commons.math.linear.NonPositiveDefiniteMatrixException
 * if the covariance matrix is not strictly positive definite.
 * @throws DimensionMismatchException if the mean and covariance
 * arrays dimensions do not match.
 */
public CorrelatedRandomVectorGenerator(double[] mean,
                                       RealMatrix covariance, double small,
                                       NormalizedRandomGenerator generator) {
    int order = covariance.getRowDimension();
    if (mean.length != order) {
        throw new DimensionMismatchException(mean.length, order);
    }
    this.mean = mean.clone();

    final RectangularCholeskyDecomposition decomposition =
        new RectangularCholeskyDecompositionImpl(covariance, small);
    root = decomposition.getRootMatrix();

    this.generator = generator;
    normalized = new double[decomposition.getRank()];

}
 
Example 3
Source File: BiDiagonalTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void checkBiDiagonal(RealMatrix m) {
    final int rows = m.getRowDimension();
    final int cols = m.getColumnDimension();
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            if (rows < cols) {
                if ((i < j) || (i > j + 1)) {
                    Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16);
                }
            } else {
                if ((i < j - 1) || (i > j)) {
                    Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16);
                }
            }
        }
    }
}
 
Example 4
Source File: MatrixTools.java    From JuiceboxLegacy with MIT License 6 votes vote down vote up
/**
 * Return sign of values in matrix:
 * val > 0 : 1
 * val = 0 : 0
 * val < 0 : -1
 */
public static RealMatrix sign(RealMatrix matrix) {
    int r = matrix.getRowDimension();
    int c = matrix.getColumnDimension();
    RealMatrix signMatrix = cleanArray2DMatrix(r, c);
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            double val = matrix.getEntry(i, j);
            if (val > 0) {
                signMatrix.setEntry(i, j, 1);
            } else if (val < 0) {
                signMatrix.setEntry(i, j, -1);
            }
        }
    }
    return signMatrix;
}
 
Example 5
Source File: BiDiagonalTransformerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void checkBiDiagonal(RealMatrix m) {
    final int rows = m.getRowDimension();
    final int cols = m.getColumnDimension();
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            if (rows < cols) {
                if ((i < j) || (i > j + 1)) {
                    Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16);
                }
            } else {
                if ((i < j - 1) || (i > j)) {
                    Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16);
                }
            }
        }
    }
}
 
Example 6
Source File: VectorialCovarianceTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testBasicStats() throws DimensionMismatchException {

        VectorialCovariance stat = new VectorialCovariance(points[0].length, true);
        for (int i = 0; i < points.length; ++i) {
            stat.increment(points[i]);
        }

        assertEquals(points.length, stat.getN());

        RealMatrix c = stat.getResult();
        double[][] refC    = new double[][] {
                { 8.0470, -1.9195, -3.4445},
                {-1.9195,  1.0470,  3.2795},
                {-3.4445,  3.2795, 12.2070}
        };

        for (int i = 0; i < c.getRowDimension(); ++i) {
            for (int j = 0; j <= i; ++j) {
                assertEquals(refC[i][j], c.getEntry(i, j), 1.0e-12);
            }
        }

    }
 
Example 7
Source File: QRDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void checkDimension(RealMatrix m) {
    int rows = m.getRowDimension();
    int columns = m.getColumnDimension();
    QRDecomposition qr = new QRDecompositionImpl(m);
    assertEquals(rows,    qr.getQ().getRowDimension());
    assertEquals(rows,    qr.getQ().getColumnDimension());
    assertEquals(rows,    qr.getR().getRowDimension());
    assertEquals(columns, qr.getR().getColumnDimension());        
}
 
Example 8
Source File: CMAESOptimizer.java    From astor 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 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 9
Source File: OLSMultipleLinearRegression.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Check if a matrix is upper-triangular.</p>
 * 
 * <p>Makes sure all below-diagonal elements are within epsilon of 0.</p>
 * 
 * @param m matrix to check
 * @param epsilon maximum allowable absolute value for elements below
 * the main diagonal
 * 
 * @throws IllegalArgumentException if m is not upper-triangular
 */
private static void checkUpperTriangular(RealMatrix m, double epsilon) {
    int nCols = m.getColumnDimension();
    int nRows = m.getRowDimension();
    for (int r = 0; r < nRows; r++) {
        int bound = Math.min(r, nCols);
        for (int c = 0; c < bound; c++) {
            if (Math.abs(m.getEntry(r, c)) > epsilon) {
                throw MathRuntimeException.createIllegalArgumentException(
                      "matrix is not upper-triangular, entry ({0}, {1}) = {2} is too large",
                      r, c, m.getEntry(r, c));
            }
        }
    }
}
 
Example 10
Source File: CorrelatedRandomVectorGeneratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public CorrelatedRandomVectorGeneratorTest() {
    mean = new double[] { 0.0, 1.0, -3.0, 2.3 };

    RealMatrix b = MatrixUtils.createRealMatrix(4, 3);
    int counter = 0;
    for (int i = 0; i < b.getRowDimension(); ++i) {
        for (int j = 0; j < b.getColumnDimension(); ++j) {
            b.setEntry(i, j, 1.0 + 0.1 * ++counter);
        }
    }
    RealMatrix bbt = b.multiply(b.transpose());
    covariance = MatrixUtils.createRealMatrix(mean.length, mean.length);
    for (int i = 0; i < covariance.getRowDimension(); ++i) {
        covariance.setEntry(i, i, bbt.getEntry(i, i));
        for (int j = 0; j < covariance.getColumnDimension(); ++j) {
            double s = bbt.getEntry(i, j);
            covariance.setEntry(i, j, s);
            covariance.setEntry(j, i, s);
        }
    }

    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(17399225432l);
    GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg);
    generator = new CorrelatedRandomVectorGenerator(mean,
                                                    covariance,
                                                    1.0e-12 * covariance.getNorm(),
                                                    rawGenerator);
}
 
Example 11
Source File: BiDiagonalTransformerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void checkdimensions(RealMatrix matrix) {
    final int m = matrix.getRowDimension();
    final int n = matrix.getColumnDimension();
    BiDiagonalTransformer transformer = new BiDiagonalTransformer(matrix);
    assertEquals(m, transformer.getU().getRowDimension());
    assertEquals(m, transformer.getU().getColumnDimension());
    assertEquals(m, transformer.getB().getRowDimension());
    assertEquals(n, transformer.getB().getColumnDimension());
    assertEquals(n, transformer.getV().getRowDimension());
    assertEquals(n, transformer.getV().getColumnDimension());

}
 
Example 12
Source File: LUDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test that L is lower triangular with unit diagonal */
public void testLLowerTriangular() {
    RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
    RealMatrix l = new LUDecompositionImpl(matrix).getL();
    for (int i = 0; i < l.getRowDimension(); i++) {
        assertEquals(l.getEntry(i, i), 1, entryTolerance);
        for (int j = i + 1; j < l.getColumnDimension(); j++) {
            assertEquals(l.getEntry(i, j), 0, entryTolerance);
        }
    }
}
 
Example 13
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
 */
private void checkSufficientData(final RealMatrix matrix) {
    int nRows = matrix.getRowDimension();
    int nCols = matrix.getColumnDimension();
    if (nRows < 2 || nCols < 2) {
        throw MathRuntimeException.createIllegalArgumentException(
                "insufficient data: only {0} rows and {1} columns.",
                nRows, nCols);
    }
}
 
Example 14
Source File: MatrixTools.java    From JuiceboxLegacy with MIT License 5 votes vote down vote up
public static void thresholdValuesDouble(RealMatrix matrix, double lowVal, double highVal) {
    for (int i = 0; i < matrix.getRowDimension(); i++) {
        for (int j = 0; j < matrix.getColumnDimension(); j++) {
            if (matrix.getEntry(i, j) > highVal) {
                matrix.setEntry(i, j, highVal);
            }
            if (matrix.getEntry(i, j) < lowVal) {
                matrix.setEntry(i, j, lowVal);
            }
        }
    }
}
 
Example 15
Source File: BiDiagonalTransformerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void checkdimensions(RealMatrix matrix) {
    final int m = matrix.getRowDimension();
    final int n = matrix.getColumnDimension();
    BiDiagonalTransformer transformer = new BiDiagonalTransformer(matrix);
    assertEquals(m, transformer.getU().getRowDimension());
    assertEquals(m, transformer.getU().getColumnDimension());
    assertEquals(m, transformer.getB().getRowDimension());
    assertEquals(n, transformer.getB().getColumnDimension());
    assertEquals(n, transformer.getV().getRowDimension());
    assertEquals(n, transformer.getV().getColumnDimension());

}
 
Example 16
Source File: Covariance.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
 */
private void checkSufficientData(final RealMatrix matrix) {
    int nRows = matrix.getRowDimension();
    int nCols = matrix.getColumnDimension();
    if (nRows < 2 || nCols < 2) {
        throw MathRuntimeException.createIllegalArgumentException(
                "insufficient data: only {0} rows and {1} columns.",
                nRows, nCols);
    }
}
 
Example 17
Source File: CholeskyDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test that L is lower triangular */
@Test
public void testLLowerTriangular() throws MathException {
    RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
    RealMatrix l = new CholeskyDecompositionImpl(matrix).getL();
    for (int i = 0; i < l.getRowDimension(); i++) {
        for (int j = i + 1; j < l.getColumnDimension(); j++) {
            assertEquals(0.0, l.getEntry(i, j), 0.0);
        }
    }
}
 
Example 18
Source File: OLSMultipleLinearRegression.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Check if a matrix is upper-triangular.</p>
 * 
 * <p>Makes sure all below-diagonal elements are within epsilon of 0.</p>
 * 
 * @param m matrix to check
 * @param epsilon maximum allowable absolute value for elements below
 * the main diagonal
 * 
 * @throws IllegalArgumentException if m is not upper-triangular
 */
private static void checkUpperTriangular(RealMatrix m, double epsilon) {
    int nCols = m.getColumnDimension();
    int nRows = m.getRowDimension();
    for (int r = 0; r < nRows; r++) {
        int bound = Math.min(r, nCols);
        for (int c = 0; c < bound; c++) {
            if (Math.abs(m.getEntry(r, c)) > epsilon) {
                throw MathRuntimeException.createIllegalArgumentException(
                      "matrix is not upper-triangular, entry ({0}, {1}) = {2} is too large",
                      r, c, m.getEntry(r, c));
            }
        }
    }
}
 
Example 19
Source File: Covariance.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a covariance matrix from a matrix whose columns
 * represent covariates.
 * 
 * <p>The <code>biasCorrected</code> parameter determines whether or not
 * covariance estimates are bias-corrected.</p>
 * 
 * <p>The matrix must have at least two columns and two rows</p>
 * 
 * @param matrix matrix with columns representing covariates
 * @param biasCorrected true means covariances are bias-corrected
 * @throws IllegalArgumentException if the input matrix does not have
 * at least two rows and two columns
 */
public Covariance(RealMatrix matrix, boolean biasCorrected) {
   checkSufficientData(matrix);
   n = matrix.getRowDimension();
   covarianceMatrix = computeCovarianceMatrix(matrix, biasCorrected);
}
 
Example 20
Source File: Covariance.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a covariance matrix from a matrix whose columns
 * represent covariates.
 *
 * <p>The <code>biasCorrected</code> parameter determines whether or not
 * covariance estimates are bias-corrected.</p>
 *
 * <p>The matrix must have at least two columns and two rows</p>
 *
 * @param matrix matrix with columns representing covariates
 * @param biasCorrected true means covariances are bias-corrected
 * @throws IllegalArgumentException if the input matrix does not have
 * at least two rows and two columns
 */
public Covariance(RealMatrix matrix, boolean biasCorrected) {
   checkSufficientData(matrix);
   n = matrix.getRowDimension();
   covarianceMatrix = computeCovarianceMatrix(matrix, biasCorrected);
}