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

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

Introduction

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

Prototype

int getRowDimension();

Source Link

Document

Returns the number of rows in the matrix.

Usage

From source file:edu.cmu.tetrad.search.SemLikelihood2.java

private double logdet(TetradMatrix m) {
    if (m.rows() == 0)
        return 0;
    RealMatrix M = m.getRealMatrix();//from   w w w  . j  av  a2 s.  com
    final double tol = 1e-9;
    RealMatrix LT = new org.apache.commons.math3.linear.CholeskyDecomposition(M, tol, tol).getLT();

    double sum = 0.0;

    for (int i = 0; i < LT.getRowDimension(); i++) {
        sum += FastMath.log(LT.getEntry(i, i));
    }

    return 2.0 * sum;
}

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

@Test
public void testM255GeominOblique() {
    System.out.println("Oblique Geomin rotation test: m255 data");

    /**/*from  www . jav  a  2  s  .c  o m*/
     * True result obtained form R using GPArotation package
     */
    double[][] true_Geomin_oblique = { { 0.814401032, -0.07246248, -0.02746737 },
            { 0.831474480, -0.01326628, 0.02443654 }, { 0.646042722, 0.11864079, -0.05620512 },
            { 0.604841472, 0.16035608, 0.04246413 }, { 0.429130847, 0.32919710, -0.11951339 },
            { 0.068651498, 0.74475428, -0.04555539 }, { -0.026759588, 0.84183379, 0.08633271 },
            { 0.111079049, 0.44919958, -0.05420552 }, { 0.130427210, 0.37116164, -0.31191986 },
            { -0.051932818, 0.47010788, -0.26735188 }, { 0.036623600, 0.01140911, -0.87276177 },
            { 0.002120624, 0.01879531, -0.73637231 }, };

    double[][] true_Phi = { { 1.0000000, 0.5565973, -0.7280542 }, { 0.5565973, 1.0000000, -0.6808553 },
            { -0.7280542, -0.6808553, 1.0000000 } };

    RealMatrix L = new Array2DRowRealMatrix(m255MINRESLoadings);
    GPArotation gpa = new GPArotation();
    RotationResults R = gpa.rotate(L, RotationMethod.GEOMIN_Q, false, 500, 1e-5);
    RealMatrix Lr = R.getFactorLoadings();
    //        System.out.println(R.toString());

    for (int i = 0; i < Lr.getRowDimension(); i++) {
        for (int j = 0; j < Lr.getColumnDimension(); j++) {
            assertEquals("  loading: ", Precision.round(true_Geomin_oblique[i][j], 4),
                    Precision.round(Lr.getEntry(i, j), 5), 1e-4);
        }
    }

    RealMatrix Phi = R.getPhi();
    for (int i = 0; i < Phi.getRowDimension(); i++) {
        for (int j = 0; j < Phi.getColumnDimension(); j++) {
            assertEquals("  factor correlation: ", Precision.round(true_Phi[i][j], 4),
                    Precision.round(Phi.getEntry(i, j), 5), 1e-4);
        }
    }

}

From source file:com.itemanalysis.psychometrics.cfa.ParallelModel.java

public double[] getGradient(RealMatrix factorLoadingFirstDerivative, RealMatrix errorVarianceFirstDerivative) {
    double[] gradient = new double[getNumberOfParameters()];
    double sumL = 0.0, sumE = 0.0;

    for (int i = 0; i < factorLoadingFirstDerivative.getRowDimension(); i++) {
        sumL += factorLoadingFirstDerivative.getEntry(i, 0);
    }//from  www.j a va 2s .c om
    gradient[0] = sumL;

    for (int i = 0; i < errorVariance.length; i++) {
        sumE += errorVarianceFirstDerivative.getEntry(i, i);
    }
    gradient[1] = sumE;
    return gradient;
}

From source file:com.itemanalysis.psychometrics.cfa.AbstractConfirmatoryFactorAnalysisEstimator.java

public double sumSquaredElements(RealMatrix matrix) {
    double sum = 0.0;
    double v = 0.0;
    for (int i = 0; i < matrix.getRowDimension(); i++) {
        for (int j = 0; j < matrix.getColumnDimension(); j++) {
            v = matrix.getEntry(i, j);//from w  w  w  .  j  av a 2  s .  c  o m
            sum += (v * v);
        }
    }
    return sum;
}

From source file:ellipsoidFit.FitPoints.java

/**
 * Find the center of the ellipsoid.//from  w w  w  .j  av  a2  s .  c  o  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:edu.cudenver.bios.matrix.FixedRandomMatrix.java

/**
 * Update the values in the random submatrix.  The matrix of new values
 * must match the dimensions of the random submatrix.
 *
 * @param updatedMatrix new values for the random submatrix
 *///from  w w w . j ava2s.  c  om
public void updateRandomMatrix(RealMatrix updatedMatrix) {
    if (randomMatrix != null && updatedMatrix.getColumnDimension() == randomMatrix.getColumnDimension()
            && updatedMatrix.getRowDimension() == randomMatrix.getRowDimension()) {
        double[][] data = updatedMatrix.getData();
        randomMatrix.setSubMatrix(data, 0, 0);
        int startRow = 0;
        int startCol = 0;
        if (fixedMatrix != null) {
            if (combineHorizontal) {
                startRow = 0;
                startCol = fixedMatrix.getColumnDimension();
            } else {
                startRow = fixedMatrix.getRowDimension();
                startCol = 0;
            }
        }
        combinedMatrix.setSubMatrix(data, startRow, startCol);
    }
}

From source file:com.itemanalysis.psychometrics.irt.estimation.ItemLogLikelihood.java

/**
 * Computes the standard errors of the item parameter estimates.
 *
 * @param x array of item parameter estimates
 * @return array of item parameter standard errors
 *///from  ww w .  j av a2  s  .  c  om
public double[] stdError(double[] x, double[][] hessian) {
    double[] se = new double[x.length];
    try {
        //            RealMatrix m = new Array2DRowRealMatrix(numericHessian(x));
        RealMatrix m = new Array2DRowRealMatrix(hessian);
        RealMatrix info = new LUDecomposition(m).getSolver().getInverse();

        for (int i = 0; i < info.getRowDimension(); i++) {
            se[i] = Math.sqrt(info.getEntry(i, i));
        }
    } catch (SingularMatrixException ex) {
        for (int i = 0; i < se.length; i++) {
            se[i] = Double.NaN;
        }
        ex.printStackTrace();
    }

    return se;
}

From source file:edu.ucdenver.bios.powersvc.resource.PowerResourceHelper.java

/**
 * This method takes a Real Matrix and converts it into a Named Matrix and
 * returns that Named Matrix./*from   www  . j a  v a 2s .co  m*/
 * 
 * @param matrix
 *            The matrix is a input matrix of type RealMatrix and is to be
 *            converted to a NamedMatrix.
 * @param name
 *            the name is a String, which is to be assigned to named matrix.
 * @return namedMatrix Returns a NamedMatrix which is obtained by converting
 *         the input matrix to NamedMatrix
 */
public static NamedMatrix toNamedMatrix(final RealMatrix matrix, final String name) {
    if (matrix == null || name == null || name.isEmpty()) {
        logger.error("failed to create NamedMatrix object name=[" + (name != null ? name : "NULL") + "]");
        return null;
    }
    NamedMatrix namedMatrix = new NamedMatrix();
    namedMatrix.setDataFromArray(matrix.getData());
    namedMatrix.setName(name);
    namedMatrix.setColumns(matrix.getColumnDimension());
    namedMatrix.setRows(matrix.getRowDimension());
    return namedMatrix;
}

From source file:com.analog.lyric.dimple.solvers.sumproduct.customFactors.MutlivariateGaussianMatrixProduct.java

public MutlivariateGaussianMatrixProduct(double[][] A) {
    int i; //,m;
    M = A.length;//from  www.  j  av  a  2 s . c o m
    N = A[0].length;
    /* Here we precompute and store matrices for future message computations.
     * First, compute an SVD of the matrix A using EigenDecompositions of A*A^T and A^T*A
     * This way, we get nullspaces for free along with regularized inverse.
     */

    RealMatrix Amat = wrapRealMatrix(A);

    SingularValueDecomposition svd = new SingularValueDecomposition(Amat);

    RealMatrix tmp = svd.getVT();
    tmp = svd.getS().multiply(tmp);
    tmp = svd.getU().multiply(tmp);

    A_clean = matrixGetDataRef(tmp);

    RealMatrix ST = svd.getS().transpose();

    int numS = Math.min(ST.getColumnDimension(), ST.getRowDimension());
    for (i = 0; i < numS; i++) {
        double d = ST.getEntry(i, i);
        if (d < eps)
            d = eps;
        else if (d > 1 / eps)
            d = 1 / eps;
        ST.setEntry(i, i, 1.0 / d);
    }

    A_pinv = matrixGetDataRef(svd.getV().multiply(ST.multiply(svd.getUT())));
}

From source file:com.joptimizer.algebra.CholeskySparseFactorizationTest.java

/**
 * This test shows that the correct check of the inversion accuracy must be done with
 * the scaled residual, not with the simple norm ||A.x-b||
 *///www . j av a2 s.co m
public void testScaledResidual() throws Exception {
    log.debug("testScaledResidual");

    String matrixId = "1";
    double[][] A = Utils
            .loadDoubleMatrixFromFile("factorization" + File.separator + "matrix" + matrixId + ".csv");
    RealMatrix Q = MatrixUtils.createRealMatrix(A);
    int dim = Q.getRowDimension();

    RealVector b = new ArrayRealVector(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });

    CholeskySparseFactorization cs = new CholeskySparseFactorization(new SparseDoubleMatrix2D(Q.getData()));
    cs.factorize();
    RealVector x = new ArrayRealVector(cs.solve(F1.make(b.toArray())).toArray());

    //scaledResidual = ||Ax-b||_oo/( ||A||_oo . ||x||_oo + ||b||_oo )
    // with ||x||_oo = max(x[i])
    double residual = Utils.calculateScaledResidual(A, x.toArray(), b.toArray());
    log.debug("residual: " + residual);
    assertTrue(residual < Utils.getDoubleMachineEpsilon());

    //b - A.x
    //checking the simple norm, this will fail
    double n1 = b.subtract(Q.operate(x)).getNorm();
    log.debug("||b - A.x||: " + n1);
    //assertTrue(n1 < 1.E-8);
}