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.util.TetradMatrix.java

public TetradMatrix(RealMatrix matrix) {
    if (matrix == null) {
        throw new IllegalArgumentException("Null matrix.");
    }//from   w  w  w  .  ja  v a  2  s. co m

    this.apacheData = matrix;
    this.m = matrix.getRowDimension();
    this.n = matrix.getColumnDimension();
}

From source file:eagle.security.userprofile.model.eigen.UserProfileEigenModeler.java

private void computeCovarianceAndSVD(RealMatrix inputMat, int containsLowVariantCol) {

    int finalMatrixRow = 0;
    int finalMatrixCol = 0;

    LOG.info("containsLowVariantCol size: " + containsLowVariantCol);
    int colDimension = (inputMat.getColumnDimension() - containsLowVariantCol);
    try {/*from w w  w .  j a  v a 2s . co m*/
        finalMatrixWithoutLowVariantCmds = new Array2DRowRealMatrix(inputMat.getRowDimension(), colDimension);
    } catch (NotStrictlyPositiveException e) {
        LOG.error(String.format("Failed to build matrix [rowDimension:%s, columnDimension: %s]",
                inputMat.getRowDimension(), colDimension), e);
        throw e;
    }

    for (int i = 0; i < inputMat.getRowDimension(); i++) {
        for (int j = 0; j < inputMat.getColumnDimension(); j++) {
            if (!statistics[j].isLowVariant()) {
                finalMatrixWithoutLowVariantCmds.setEntry(finalMatrixRow, finalMatrixCol,
                        inputMat.getEntry(i, j));
                finalMatrixCol++;
            }
        }
        finalMatrixCol = 0;
        finalMatrixRow++;
    }

    Covariance cov;
    try {
        cov = new Covariance(finalMatrixWithoutLowVariantCmds.getData());
    } catch (Exception ex) {
        throw new IllegalArgumentException(String.format("Failed to create covariance from matrix [ %s x %s ]",
                finalMatrixWithoutLowVariantCmds.getRowDimension(),
                finalMatrixWithoutLowVariantCmds.getColumnDimension()), ex);
    }
    covarianceMatrix = cov.getCovarianceMatrix();
    SingularValueDecomposition svd = new SingularValueDecomposition(covarianceMatrix);
    diagonalMatrix = svd.getS();
    uMatrix = svd.getU();
    vMatrix = svd.getV();
}

From source file:gamlss.utilities.WLSMultipleLinearRegression.java

/**
 * /*from w w  w. j  av a 2  s.c  om*/
 * @param y
 * @param x
 * @param w
 */
private void newSampleDataCopyOriginal(ArrayRealVector y, RealMatrix x, ArrayRealVector w) {
    this.y = y.copy(); //we need this for the fitted values and residuals.
    if (this.isNoIntercept()) {
        this.X = x.copy();

    } else {
        setDesignMatrix(x);//add 1 for the Intercept;
    }
    for (int row = 0; row < x.getRowDimension(); row++) {
        x.setRowVector(row, x.getRowVector(row).mapMultiplyToSelf(w.getEntry(row)));
    }
    //double[][] xw=x.getData();
    //double[] yw= y.ebeMultiply(w).getDataRef();
    //validateSampleData(xw, yw); //we have already checked this in the gamlss algorithm. 
    newYSampleData(y.ebeMultiply(w));
    newXSampleData(x.getData(), w);
}

From source file:com.joptimizer.optimizers.LPPresolverTest.java

/**
 * This problem has a deterministic solution.
 *//*from   w  w  w  .j  a  va  2s.  co  m*/
public void testFromFile2() throws Exception {
    log.debug("testFromFile2");

    String problemId = "2";

    double[] c = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "presolving" + File.separator + "c" + problemId + ".txt");
    double[][] A = Utils.loadDoubleMatrixFromFile(
            "lp" + File.separator + "presolving" + File.separator + "A" + problemId + ".csv", ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "presolving" + File.separator + "b" + problemId + ".txt");
    double[] lb = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "presolving" + File.separator + "lb" + problemId + ".txt");
    double[] ub = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "presolving" + File.separator + "ub" + problemId + ".txt");
    double s = 0;
    try {
        s = Utils.loadDoubleArrayFromFile(
                "lp" + File.separator + "presolving" + File.separator + "s" + problemId + ".txt")[0];
    } catch (Exception e) {
    }
    double[] expectedSolution = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "presolving" + File.separator + "sol" + problemId + ".txt");
    double expectedValue = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "presolving" + File.separator + "value" + problemId + ".txt")[0];
    double expectedTolerance = MatrixUtils.createRealMatrix(A)
            .operate(MatrixUtils.createRealVector(expectedSolution)).subtract(MatrixUtils.createRealVector(b))
            .getNorm();

    //must be: A pXn with rank(A)=p < n
    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A);
    SingularValueDecomposition dec = new SingularValueDecomposition(AMatrix);
    int rankA = dec.getRank();
    log.debug("p: " + AMatrix.getRowDimension());
    log.debug("n: " + AMatrix.getColumnDimension());
    log.debug("rank: " + rankA);

    LPPresolver lpPresolver = new LPPresolver();
    lpPresolver.setNOfSlackVariables((short) s);
    lpPresolver.setExpectedSolution(expectedSolution);//this is just for test!
    lpPresolver.presolve(c, A, b, lb, ub);
    int n = lpPresolver.getPresolvedN();

    //deterministic solution
    assertEquals(0, n);
    assertTrue(lpPresolver.getPresolvedC() == null);
    assertTrue(lpPresolver.getPresolvedA() == null);
    assertTrue(lpPresolver.getPresolvedB() == null);
    assertTrue(lpPresolver.getPresolvedLB() == null);
    assertTrue(lpPresolver.getPresolvedUB() == null);
    assertTrue(lpPresolver.getPresolvedYlb() == null);
    assertTrue(lpPresolver.getPresolvedYub() == null);
    assertTrue(lpPresolver.getPresolvedZlb() == null);
    assertTrue(lpPresolver.getPresolvedZub() == null);
    double[] sol = lpPresolver.postsolve(new double[] {});
    assertEquals(expectedSolution.length, sol.length);
    for (int i = 0; i < sol.length; i++) {
        //log.debug("i: " + i);
        assertEquals(expectedSolution[i], sol[i], 1.e-9);
    }
}

From source file:com.cloudera.oryx.common.math.LinearSystemSolver.java

public Solver getSolver(RealMatrix M) {
    if (M == null) {
        return null;
    }// w  w  w.j  a v a  2s . c  o m
    RRQRDecomposition decomposition = new RRQRDecomposition(M, SINGULARITY_THRESHOLD);
    DecompositionSolver solver = decomposition.getSolver();
    if (solver.isNonSingular()) {
        return new Solver(solver);
    }
    // Otherwise try to report apparent rank
    int apparentRank = decomposition.getRank(0.01); // Better value?
    log.warn(
            "{} x {} matrix is near-singular (threshold {}). Add more data or decrease the "
                    + "value of als.hyperparams.features, to <= about {}",
            M.getRowDimension(), M.getColumnDimension(), SINGULARITY_THRESHOLD, apparentRank);
    throw new SingularMatrixSolverException(apparentRank, "Apparent rank: " + apparentRank);
}

From source file:com.cloudera.oryx.common.math.CommonsMathLinearSystemSolver.java

@Override
public Solver getSolver(RealMatrix M) {
    if (M == null) {
        return null;
    }//from www  .  j  av a  2s  .  c o m
    RRQRDecomposition decomposition = new RRQRDecomposition(M, SINGULARITY_THRESHOLD);
    DecompositionSolver solver = decomposition.getSolver();
    if (solver.isNonSingular()) {
        return new CommonsMathSolver(solver);
    }
    // Otherwise try to report apparent rank
    int apparentRank = decomposition.getRank(0.01); // Better value?
    log.warn(
            "{} x {} matrix is near-singular (threshold {}). Add more data or decrease the value of model.features, "
                    + "to <= about {}",
            M.getRowDimension(), M.getColumnDimension(), SINGULARITY_THRESHOLD, apparentRank);
    throw new SingularMatrixSolverException(apparentRank, "Apparent rank: " + apparentRank);
}

From source file:com.joptimizer.optimizers.LPPresolverTest.java

private void doPresolving(double[] c, double[][] A, double[] b, double[] lb, double[] ub, double s,
        double[] expectedSolution, double expectedValue, double expectedTolerance) throws Exception {

    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A);
    SingularValueDecomposition dec = new SingularValueDecomposition(AMatrix);
    int rankA = dec.getRank();
    log.debug("p: " + AMatrix.getRowDimension());
    log.debug("n: " + AMatrix.getColumnDimension());
    log.debug("rank: " + rankA);

    LPPresolver lpPresolver = new LPPresolver();
    lpPresolver.setNOfSlackVariables((short) s);
    lpPresolver.setExpectedSolution(expectedSolution);//this is just for test!
    //lpPresolver.setExpectedTolerance(expectedTolerance);//this is just for test!
    lpPresolver.presolve(c, A, b, lb, ub);
    int n = lpPresolver.getPresolvedN();
    double[] presolvedC = lpPresolver.getPresolvedC().toArray();
    double[][] presolvedA = lpPresolver.getPresolvedA().toArray();
    double[] presolvedB = lpPresolver.getPresolvedB().toArray();
    double[] presolvedLb = lpPresolver.getPresolvedLB().toArray();
    double[] presolvedUb = lpPresolver.getPresolvedUB().toArray();
    double[] presolvedYlb = lpPresolver.getPresolvedYlb().toArray();
    double[] presolvedYub = lpPresolver.getPresolvedYub().toArray();
    double[] presolvedZlb = lpPresolver.getPresolvedZlb().toArray();
    double[] presolvedZub = lpPresolver.getPresolvedZub().toArray();
    log.debug("n  : " + n);
    log.debug("presolvedC  : " + ArrayUtils.toString(presolvedC));
    log.debug("presolvedA  : " + ArrayUtils.toString(presolvedA));
    log.debug("presolvedB  : " + ArrayUtils.toString(presolvedB));
    log.debug("presolvedLb : " + ArrayUtils.toString(presolvedLb));
    log.debug("presolvedUb : " + ArrayUtils.toString(presolvedUb));
    log.debug("presolvedYlb: " + ArrayUtils.toString(presolvedYlb));
    log.debug("presolvedYub: " + ArrayUtils.toString(presolvedYub));
    log.debug("presolvedZlb: " + ArrayUtils.toString(presolvedZlb));
    log.debug("presolvedZub: " + ArrayUtils.toString(presolvedZub));

    //check objective function
    double delta = expectedTolerance;
    RealVector presolvedX = MatrixUtils.createRealVector(lpPresolver.presolve(expectedSolution));
    log.debug("presolved value: " + MatrixUtils.createRealVector(presolvedC).dotProduct(presolvedX));
    RealVector postsolvedX = MatrixUtils.createRealVector(lpPresolver.postsolve(presolvedX.toArray()));
    double value = MatrixUtils.createRealVector(c).dotProduct(postsolvedX);
    assertEquals(expectedValue, value, delta);

    //check postsolved constraints
    for (int i = 0; i < lb.length; i++) {
        double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i];
        assertTrue(di <= postsolvedX.getEntry(i) + delta);
    }/*from   ww w .ja v  a  2 s . com*/
    for (int i = 0; i < ub.length; i++) {
        double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i];
        assertTrue(di + delta >= postsolvedX.getEntry(i));
    }
    RealVector Axmb = AMatrix.operate(postsolvedX).subtract(MatrixUtils.createRealVector(b));
    assertEquals(0., Axmb.getNorm(), expectedTolerance);

    //check presolved constraints
    assertEquals(presolvedLb.length, presolvedX.getDimension());
    assertEquals(presolvedUb.length, presolvedX.getDimension());
    AMatrix = MatrixUtils.createRealMatrix(presolvedA);
    RealVector bvector = MatrixUtils.createRealVector(presolvedB);
    for (int i = 0; i < presolvedLb.length; i++) {
        double di = Double.isNaN(presolvedLb[i]) ? -Double.MAX_VALUE : presolvedLb[i];
        assertTrue(di <= presolvedX.getEntry(i) + delta);
    }
    for (int i = 0; i < presolvedUb.length; i++) {
        double di = Double.isNaN(presolvedUb[i]) ? Double.MAX_VALUE : presolvedUb[i];
        assertTrue(di + delta >= presolvedX.getEntry(i));
    }
    Axmb = AMatrix.operate(presolvedX).subtract(bvector);
    assertEquals(0., Axmb.getNorm(), expectedTolerance);

    //check rank(A): must be A pXn with rank(A)=p < n
    AMatrix = MatrixUtils.createRealMatrix(presolvedA);
    dec = new SingularValueDecomposition(AMatrix);
    rankA = dec.getRank();
    log.debug("p: " + AMatrix.getRowDimension());
    log.debug("n: " + AMatrix.getColumnDimension());
    log.debug("rank: " + rankA);
    assertEquals(AMatrix.getRowDimension(), rankA);
    assertTrue(rankA < AMatrix.getColumnDimension());
}

From source file:edu.cmu.tetrad.util.TetradMatrix1.java

public TetradMatrix1(RealMatrix matrix) {
    if (matrix == null) {
        throw new IllegalArgumentException("Null matrix.");
    }//from  www .  ja va 2  s.  c  o m

    this.apacheData = matrix;
    this.m = matrix.getRowDimension();
    this.n = matrix.getColumnDimension();
}

From source file:msi.gama.util.matrix.GamaFloatMatrix.java

public GamaFloatMatrix(final RealMatrix rm) {
    super(rm.getColumnDimension(), rm.getRowDimension(), Types.FLOAT);
    matrix = new double[rm.getColumnDimension() * rm.getRowDimension()];
    updateMatrix(rm);/*from  w w w . j  av  a2 s  .c o  m*/
}

From source file:edu.cmu.tetrad.util.TetradMatrix.java

public TetradMatrix(RealMatrix matrix, int rows, int columns) {
    if (matrix == null) {
        throw new IllegalArgumentException("Null matrix.");
    }/*from  ww w. jav a2 s.  c o m*/

    this.apacheData = matrix;
    this.m = rows;
    this.n = columns;

    int _rows = matrix.getRowDimension();
    int _cols = matrix.getColumnDimension();
    if (_rows != 0 && _rows != rows)
        throw new IllegalArgumentException();
    if (_cols != 0 && _cols != columns)
        throw new IllegalArgumentException();
}