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

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

Introduction

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

Prototype

void multiplyEntry(int row, int column, double factor) throws OutOfRangeException;

Source Link

Document

Multiplies (in place) the specified entry of this matrix by the specified value.

Usage

From source file:com.itemanalysis.psychometrics.factoranalysis.MatrixUtils.java

/**
 * Elementwise multiplication of two matrices.
 *
 * @param A a matrix that is multiplied by the elements of B
 * @param B another matrix/* w  w w.j a  v a  2  s.c  o m*/
 * @throws DimensionMismatchException
 */
public static void multiplyElementsBy(RealMatrix A, RealMatrix B) throws DimensionMismatchException {
    int nrow = A.getRowDimension();
    int ncol = A.getColumnDimension();
    if (nrow != B.getRowDimension()) {
        throw new DimensionMismatchException(nrow, B.getRowDimension());
    }
    if (ncol != B.getColumnDimension()) {
        throw new DimensionMismatchException(ncol, B.getColumnDimension());
    }

    RealMatrix M = new Array2DRowRealMatrix(nrow, ncol);
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            A.multiplyEntry(i, j, B.getEntry(i, j));
        }
    }
}

From source file:ellipsoidFit.FitPoints.java

/**
 * Find the center of the ellipsoid.//from   w  w  w  .j  a  v  a2s.  co m
 * 
 * @param a
 *            the algebraic from of the polynomial.
 * @return a vector containing the center of the ellipsoid.
 */
private RealVector findCenter(RealMatrix a) {
    RealMatrix subA = a.getSubMatrix(0, 2, 0, 2);

    for (int q = 0; q < subA.getRowDimension(); q++) {
        for (int s = 0; s < subA.getColumnDimension(); s++) {
            subA.multiplyEntry(q, s, -1.0);
        }
    }

    RealVector subV = a.getRowVector(3).getSubVector(0, 3);

    // inv (dtd)
    DecompositionSolver solver = new SingularValueDecomposition(subA).getSolver();
    RealMatrix subAi = solver.getInverse();

    return subAi.operate(subV);
}

From source file:MultivariateNormalDistribution.java

/**
 * Creates a multivariate normal distribution with the given mean vector and
 * covariance matrix./* ww w  . j  a  v  a 2 s  .co  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 MultivariateNormalDistribution(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);
    for (int v = 0; v < dim; v++) {
        final double[] evec = covMatDec.getEigenvector(v).toArray();
        covMatEigenvectors.setColumn(v, evec);
    }

    final RealMatrix tmpMatrix = covMatEigenvectors.transpose();

    // Scale each eigenvector by the square root of its eigenvalue.
    for (int row = 0; row < dim; row++) {
        final double factor = FastMath.sqrt(covMatEigenvalues[row]);
        for (int col = 0; col < dim; col++) {
            tmpMatrix.multiplyEntry(row, col, factor);
        }
    }

    samplingMatrix = covMatEigenvectors.multiply(tmpMatrix);
}

From source file:org.pmad.gmm.MyMND.java

/**
 * Creates a multivariate normal distribution with the given mean vector and
 * covariance matrix.// w  ww . ja v a  2  s.c om
 * <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 MyMND(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);
    double msum = 0;
    for (int i = 0; i < covariances.length; i++) {
        for (int j = 0; j < covariances.length; j++) {
            msum += covariances[i][j];
        }
    }
    msum /= covariances.length * covariances.length;
    //        System.out.print("in");
    MyEDC covMatDec = null;
    double a = -1;
    while (true) {
        try {

            covarianceMatrix = new Array2DRowRealMatrix(covariances);

            covMatDec = new MyEDC(covarianceMatrix);

            // Compute and store the inverse.
            covarianceMatrixInverse = covMatDec.getSolver().getInverse();
            a *= -1;
            break;
        } catch (NoDataException e) {
            e.printStackTrace();
        } catch (NullArgumentException e) {
            e.printStackTrace();
        } catch (MathArithmeticException e) {
            e.printStackTrace();
        } catch (SingularMatrixException e) {
            //            System.out.print("S");
            for (int i = 0; i < covariances.length; i++) {
                double add = covariances[i][i] == 0 ? msum : covariances[i][i];
                covariances[i][i] += new Random().nextDouble() * add * 0.01;
            }
        }
        //         catch (MaxCountExceededException e) {
        ////            e.printStackTrace();
        ////            System.out.print("M"+msum);
        //            for (int i = 0; i < covariances.length; i++) {
        //               for (int j = i; j < covariances.length; j++) {
        //                  double add = covariances[i][j] == 0?msum:covariances[i][j];
        //                  add = new Random().nextDouble()*add*0.1*a;
        //                  covariances[i][j] += add;
        //                  covariances[j][i] += add;
        //               }
        //            }
        ////            break;
        //         }
    }
    // 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);
    for (int v = 0; v < dim; v++) {
        final double[] evec = covMatDec.getEigenvector(v).toArray();
        covMatEigenvectors.setColumn(v, evec);
    }

    final RealMatrix tmpMatrix = covMatEigenvectors.transpose();

    // Scale each eigenvector by the square root of its eigenvalue.
    for (int row = 0; row < dim; row++) {
        final double factor = FastMath.sqrt(covMatEigenvalues[row]);
        for (int col = 0; col < dim; col++) {
            tmpMatrix.multiplyEntry(row, col, factor);
        }
    }

    samplingMatrix = covMatEigenvectors.multiply(tmpMatrix);
}