Example usage for org.apache.commons.math3.linear Array2DRowRealMatrix multiplyEntry

List of usage examples for org.apache.commons.math3.linear Array2DRowRealMatrix multiplyEntry

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear Array2DRowRealMatrix multiplyEntry.

Prototype

@Override
public void multiplyEntry(final int row, final int column, final double factor) throws OutOfRangeException 

Source Link

Usage

From source file:outlineDescriptor.Transform.java

private Array2DRowRealMatrix createSVDmatrix(Array2DRowRealMatrix angleOffset, int aSteps, double step) {

    Array2DRowRealMatrix output = new Array2DRowRealMatrix(2 * aSteps, 2);

    for (int i = 0; i < aSteps; i++) {

        double angle = Math.PI - i * step;

        for (int row = 0; row < angleOffset.getRowDimension(); row++) {

            angleOffset.multiplyEntry(row, i, angleOffset.getEntry(row, i));
        }//from  w  w  w  .  j a  v  a 2s  . c o m

        double stdDev = getDeviance(angleOffset.getColumn(i));
        // new StandardDeviation().evaluate(angleOffset.getColumn(i));

        double outX = Math.cos(angle) * stdDev;
        double outY = Math.sin(angle) * stdDev;

        output.setEntry(i, 0, outX);
        output.setEntry(i, 1, outY);

        output.setEntry(i + aSteps, 0, -outX);
        output.setEntry(i + aSteps, 1, -outY);

    }

    return output;

}

From source file:xyz.lejon.sampling.FastMultivariateNormalDistribution.java

/**
 * Creates a multivariate normal distribution with the given mean vector and
 * covariance matrix.//from ww w.  ja v a  2s  .c o  m
 * <br/>
 * The number of dimensions is equal to the length of the mean vector
 * and to the number of rows and columns of the covariance matrix.
 * It is frequently written as "p" in formulae.
 *
 * @param rng Random Number Generator.
 * @param means Vector of means.
 * @param covariances Covariance matrix.
 * @throws DimensionMismatchException if the arrays length are
 * inconsistent.
 * @throws SingularMatrixException if the eigenvalue decomposition cannot
 * be performed on the provided covariance matrix.
 * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is
 * negative.
 */
public FastMultivariateNormalDistribution(RandomGenerator rng, final double[] means,
        final double[][] covariances)
        throws SingularMatrixException, DimensionMismatchException, NonPositiveDefiniteMatrixException {
    super(rng, means.length);

    final int dim = means.length;

    if (covariances.length != dim) {
        throw new DimensionMismatchException(covariances.length, dim);
    }

    for (int i = 0; i < dim; i++) {
        if (dim != covariances[i].length) {
            throw new DimensionMismatchException(covariances[i].length, dim);
        }
    }

    this.means = MathArrays.copyOf(means);

    covarianceMatrix = new Array2DRowRealMatrix(covariances);

    // Covariance matrix eigen decomposition.
    final EigenDecomposition covMatDec = new EigenDecomposition(covarianceMatrix);

    // Compute and store the inverse.
    covarianceMatrixInverse = covMatDec.getSolver().getInverse();
    // Compute and store the determinant.
    covarianceMatrixDeterminant = covMatDec.getDeterminant();

    // Eigenvalues of the covariance matrix.
    final double[] covMatEigenvalues = covMatDec.getRealEigenvalues();

    for (int i = 0; i < covMatEigenvalues.length; i++) {
        if (covMatEigenvalues[i] < 0) {
            throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0);
        }
    }

    // Matrix where each column is an eigenvector of the covariance matrix.
    final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim);
    final Array2DRowRealMatrix tmpMatrix = new Array2DRowRealMatrix(dim, dim);
    for (int v = 0; v < dim; v++) {
        final double factor = FastMath.sqrt(covMatEigenvalues[v]);
        final double[] evec = covMatDec.getEigenvector(v).toArray();
        covMatEigenvectors.setColumn(v, evec);
        tmpMatrix.setRow(v, evec);
        for (int col = 0; col < dim; col++) {
            tmpMatrix.multiplyEntry(v, col, factor);
        }
    }

    samplingMatrix = covMatEigenvectors.multiply(tmpMatrix);
}