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:io.warp10.script.functions.MATTO.java

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object o = stack.pop();//from   ww  w  .  j  a v  a2 s  .  co m

    if (!(o instanceof RealMatrix)) {
        throw new WarpScriptException(getName() + " expects a matrix on top of the stack.");
    }

    RealMatrix matrix = (RealMatrix) o;

    List<Object> rows = new ArrayList<Object>(matrix.getRowDimension());

    double[][] data = matrix.getData();

    for (int i = 0; i < matrix.getRowDimension(); i++) {
        List<Object> cols = new ArrayList<Object>(matrix.getColumnDimension());
        for (int j = 0; j < matrix.getColumnDimension(); j++) {
            cols.add(data[i][j]);
        }
        rows.add(cols);
    }

    stack.push(rows);

    return stack;
}

From source file:edu.cudenver.bios.matrix.test.TestMatrixUtils.java

/**
 * Determines if dimensions and all cells are equal between the
 * two matrices//from  w  w w  . j a va2s  .c  o  m
 * 
 * @param A matrix A
 * @param B matrix B
 * @return true if equal, false if not equal
 */
private boolean compareMatrices(RealMatrix A, RealMatrix B) {
    if (A.getRowDimension() == B.getRowDimension() && A.getColumnDimension() == B.getColumnDimension()) {
        for (int r = 0; r < A.getRowDimension(); r++) {
            for (int c = 0; c < A.getColumnDimension(); c++) {
                if (Double.compare(A.getEntry(r, c), B.getEntry(r, c)) != 0) {
                    System.err.println("Value mismatch at row=" + r + ", column=" + c);
                    return false;
                }
            }
        }
    } else {
        System.err.println("Dimensions do not match: A (" + A.getRowDimension() + " x " + A.getColumnDimension()
                + "), B (" + B.getRowDimension() + " x " + B.getColumnDimension() + ")");
        return false;
    }
    return true;
}

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

/**
 * Computes the first centroid factor loadings to be used
 * as initial values for the confirmatory factor anlaysis.
 *
 * This only works for a one-factor model. Need to
 * generalize to multiple factors./*from  w  ww .  j a va 2 s.  com*/
 * 
 * @param varcov observed covariance matrix
 * @return
 */
public double[] computeInitialFactorLoadings(RealMatrix varcov) {
    int r = varcov.getRowDimension();
    int c = varcov.getColumnDimension();
    double totalVariance = 0.0;
    double[] rowSum = new double[c];
    double[] inits = new double[c];

    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            rowSum[i] += varcov.getEntry(i, j);
            totalVariance += varcov.getEntry(i, j);
        }
    }

    double denom = Math.sqrt(totalVariance);

    for (int i = 0; i < r; i++) {
        inits[i] = rowSum[i] / denom;
    }

    return inits;
}

From source file:com.itemanalysis.psychometrics.mixture.MvNormalComponentDistribution.java

/**
 *
 * @param x a matrix of dimension 1 x k, where k is the number of variables
 * @return/* w  w w. j a va 2 s .co  m*/
 */
public double density(RealMatrix x) throws SingularMatrixException {
    double prob = 0.0;
    RealMatrix xTran = x.transpose();
    int d = xTran.getRowDimension();
    double det = new LUDecomposition(sigma).getDeterminant();
    double nconst = 1.0 / Math.sqrt(det * Math.pow(2.0 * Math.PI, d));
    RealMatrix Sinv = new LUDecomposition(sigma).getSolver().getInverse();
    RealMatrix delta = xTran.subtract(mu);
    RealMatrix dist = (delta.transpose().multiply(Sinv).multiply(delta));
    prob = nconst * Math.exp(-0.5 * dist.getEntry(0, 0));
    return prob;
}

From source file:edu.oregonstate.eecs.mcplan.ml.SequentialProjectionHashLearner.java

private void T(final RealMatrix S_tilde, final RealMatrix S) {
    for (int i = 0; i < S_tilde.getRowDimension(); ++i) {
        for (int j = 0; j < S_tilde.getColumnDimension(); ++j) {
            final double st = S_tilde.getEntry(i, j);
            S_tilde.setEntry(i, j, st * S.getEntry(i, j) < 0 ? st : 0);
        }/*from  w w  w  .j a  v  a  2s  .com*/
    }
}

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

public void computeValues(RealMatrix L) {
    final int k = L.getColumnDimension();
    final int p = L.getRowDimension();
    RealMatrix I = new IdentityMatrix(p);
    RealMatrix L2 = MatrixUtils.multiplyElements(L, L);

    RealMatrix N = new Array2DRowRealMatrix(k, k);
    N.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override//from  ww  w. j a v a2s  .  co  m
        public double visit(int row, int column, double value) {
            if (row == column)
                return 0.0;
            return 1.0;
        }
    });

    RealMatrix C = new Array2DRowRealMatrix(p, p);
    C.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            return gam / (double) p;
        }
    });

    RealMatrix X = I.subtract(C).multiply(L2).multiply(N);
    double sum = MatrixUtils.sumMatrix(MatrixUtils.multiplyElements(L2, X));
    functionValue = sum / 4.0;
    gradient = MatrixUtils.multiplyElements(L, X);

}

From source file:com.clust4j.algo.preprocess.WeightTransformer.java

/**
 * Inverse transform the incoming data. If the corresponding weight is 0.0,
 * will coerce the column to positive infinity rather than NaN.
 *//*from   ww  w.  j  a  v a  2 s .  c om*/
@Override
public RealMatrix inverseTransform(RealMatrix data) {
    checkFit();

    final int m = data.getRowDimension();
    if (data.getColumnDimension() != n)
        throw new DimensionMismatchException(n, data.getColumnDimension());

    double[][] X = data.getData();
    double weight, val;
    for (int j = 0; j < n; j++) {
        weight = weights[j];

        for (int i = 0; i < m; i++) {
            // sometimes, weight can be 0.0 if the user is masochistic...
            val = X[i][j] / weight;
            X[i][j] = Double.isNaN(val) ? Inf : val;
        }
    }

    // assign -- already copied in getData()
    return new Array2DRowRealMatrix(X, false);
}

From source file:com.clust4j.algo.preprocess.YeoJohnsonTransformer.java

/**
 * Inverse transform your matrix.//from   w  ww.  j  av a  2  s  .com
 */
@Override
public RealMatrix inverseTransform(RealMatrix X) {
    checkFit();

    final int m = X.getRowDimension();
    final int n = X.getColumnDimension();

    if (n != shift.length)
        throw new DimensionMismatchException(n, shift.length);

    double[][] x = X.getData();
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            x[i][j] = yjInvTransSingle(x[i][j], this.lambdas[j]);
        }
    }

    // Implicit copy in the getData()
    return new Array2DRowRealMatrix(x, false);
}

From source file:io.yields.math.concepts.operator.Smoothness.java

private boolean isConstant(RealMatrix matrix) {
    double refElement = matrix.getEntry(0, 0);
    return !IntStream.range(0, matrix.getRowDimension()).asDoubleStream()
            .filter(element -> Math.abs(refElement - element) > EPS).findAny().isPresent();
}

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

/**
 * Computes the function value for varimax rotation.
 *
 * @param L matrix of factor loadings./*from w w  w .j  a va 2  s .  c  o m*/
 */
public void computeValues(RealMatrix L) {
    //initialize dimensions and column mean array
    int nrow = L.getRowDimension();
    int ncol = L.getColumnDimension();
    Mean[] colMean = new Mean[ncol];
    for (int i = 0; i < ncol; i++) {
        colMean[i] = new Mean();
    }

    //square each element in matrix
    RealMatrix L2 = L.copy();
    double value = 0.0;
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            value = L.getEntry(i, j);
            value *= value;
            L2.setEntry(i, j, value);
            colMean[j].increment(value);
        }
    }

    double dif = 0.0;
    RealMatrix QL = new Array2DRowRealMatrix(nrow, ncol);
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            dif = L2.getEntry(i, j) - colMean[j].getResult();
            QL.setEntry(i, j, dif);
        }
    }

    //compute gradientAt
    gradient = new Array2DRowRealMatrix(nrow, ncol);
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            value = -L.getEntry(i, j) * QL.getEntry(i, j);
            gradient.setEntry(i, j, value);
        }
    }

    //compute function value
    RealMatrix B = QL.transpose().multiply(QL);
    double sum = B.getTrace();
    functionValue = -sum / 4.0;

}