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

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

Introduction

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

Prototype

int getColumnDimension();

Source Link

Document

Returns the number of columns in the matrix.

Usage

From source file:edu.cudenver.bios.power.glmm.GLMMTest.java

/**
 * Create a statistical test for to compute power
 * @param params GLMM input parameters/*from   w w  w  .ja  va 2  s . c om*/
 */
public GLMMTest(FApproximation fMethod, RealMatrix Xessence, RealMatrix XtXInverse, int perGroupN, int rank,
        FixedRandomMatrix C, RealMatrix U, RealMatrix thetaNull, RealMatrix beta, RealMatrix sigmaError) {
    this.fMethod = fMethod;
    this.Xessence = Xessence;
    this.XtXInverse = XtXInverse;
    int xrd = Xessence.getRowDimension();
    if (xrd != 0 && perGroupN > Integer.MAX_VALUE / xrd) {
        throw new GLMMTestException(OVERFLOW_ERROR_MESSAGE);
    }
    this.totalN = (double) xrd * perGroupN;
    this.rank = rank;
    this.CFixed = C.getFixedMatrix();
    this.C = C.getCombinedMatrix();
    this.U = U;
    this.thetaNull = thetaNull;
    this.beta = beta;
    this.sigmaError = sigmaError;

    // check if uni/multivariate design
    multivariate = (beta.getColumnDimension() > 1);

    // cache the value of M
    RealMatrix CFixed = C.getFixedMatrix();
    RealMatrix cxxcEssence = CFixed.multiply((XtXInverse).multiply(CFixed.transpose()));
    RealMatrix cxxcEssenceInverse = new LUDecomposition(cxxcEssence).getSolver().getInverse();
    this.M = cxxcEssenceInverse.scalarMultiply(perGroupN);
}

From source file:edu.cmu.tetrad.data.DataUtils.java

public static void simpleTest() {
    double[][] d = new double[][] { { 1, 2 }, { 3, 4 }, { 5, 6 }, };

    RealMatrix m = new BlockRealMatrix(d);

    System.out.println(m);//from w ww  . ja  v  a  2s.c o  m

    System.out.println(times(m.transpose(), m));

    System.out.println(MatrixUtils.transposeWithoutCopy(m).multiply(m));

    TetradMatrix n = new TetradMatrix(m, m.getRowDimension(), m.getColumnDimension());

    System.out.println(n);

    RealMatrix q = n.getRealMatrix();

    RealMatrix q1 = MatrixUtils.transposeWithoutCopy(q);
    RealMatrix q2 = times(q1, q);
    System.out.println(new TetradMatrix(q2, q.getColumnDimension(), q.getColumnDimension()));
}

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

/**
 * Tests the presolving of a netlib problem.
 * If checkExpectedSolution, the presolving is checked step by step against 
 * a (beforehand) known solution of the problem. 
 * NOTE: this known solution might differ from the solution given by the presolver 
 * (e.g. in the presence of a weakly dominated column @see {@link LPPresolver#removeDominatedColumns}, 
 * or if it is calculated with simplex method 
 * or if bounds are not exactly the same), so sometimes it is not a good test. 
 *///from   w  w  w  . j  a  v  a2  s .c om
public void doTesting(String problemName, boolean checkExpectedSolution, double myExpectedTolerance)
        throws Exception {
    log.debug("doTesting: " + problemName);

    int s = (int) Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator + problemName
            + File.separator + "standardS.txt")[0];
    double[] c = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardC.txt");
    double[][] A = Utils.loadDoubleMatrixFromFile(
            "lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardA.csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardB.txt");
    double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator + problemName
            + File.separator + "standardLB.txt");
    double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator + problemName
            + File.separator + "standardUB.txt");
    double[] expectedSolution = Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator
            + problemName + File.separator + "standardSolution.txt");
    double expectedValue = Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator
            + problemName + File.separator + "standardValue.txt")[0];
    double expectedTolerance = Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator
            + problemName + File.separator + "standardTolerance.txt")[0];

    //in order to compare with tha Math results, we must have the same bounds
    for (int i = 0; i < lb.length; i++) {
        if (Double.isNaN(lb[i])) {
            lb[i] = -9999999d; //the same as the notebook value
        }
    }
    for (int i = 0; i < ub.length; i++) {
        if (Double.isNaN(ub[i])) {
            ub[i] = +9999999d; //the same as the notebook value
        }
    }

    RealMatrix AMatrix = MatrixUtils.createRealMatrix(A);
    RealVector bVector = MatrixUtils.createRealVector(b);
    //expectedTolerance = Math.max(expectedTolerance,   AMatrix.operate(MatrixUtils.createRealVector(expectedSolution)).subtract(bVector).getNorm());
    expectedTolerance = Math.max(1.e-9, expectedTolerance);

    // must be: A pXn with rank(A)=p < n
    QRSparseFactorization qr = new QRSparseFactorization(new SparseDoubleMatrix2D(A));
    qr.factorize();
    log.debug("p        : " + AMatrix.getRowDimension());
    log.debug("n        : " + AMatrix.getColumnDimension());
    log.debug("full rank: " + qr.hasFullRank());

    LPPresolver lpPresolver = new LPPresolver();
    lpPresolver.setNOfSlackVariables((short) s);
    if (checkExpectedSolution) {
        lpPresolver.setExpectedSolution(expectedSolution);// this is just for test!
        lpPresolver.setExpectedTolerance(myExpectedTolerance);// this is just for test!
    }
    // lpPresolver.setAvoidFillIn(true);
    // lpPresolver.setZeroTolerance(1.e-13);
    lpPresolver.presolve(c, A, b, lb, ub);
    int n = lpPresolver.getPresolvedN();
    DoubleMatrix1D presolvedC = lpPresolver.getPresolvedC();
    DoubleMatrix2D presolvedA = lpPresolver.getPresolvedA();
    DoubleMatrix1D presolvedB = lpPresolver.getPresolvedB();
    DoubleMatrix1D presolvedLb = lpPresolver.getPresolvedLB();
    DoubleMatrix1D presolvedUb = lpPresolver.getPresolvedUB();
    DoubleMatrix1D presolvedYlb = lpPresolver.getPresolvedYlb();
    DoubleMatrix1D presolvedYub = lpPresolver.getPresolvedYub();
    DoubleMatrix1D presolvedZlb = lpPresolver.getPresolvedZlb();
    DoubleMatrix1D presolvedZub = lpPresolver.getPresolvedZub();
    log.debug("n  : " + n);
    if (log.isDebugEnabled() && n > 0) {
        log.debug("presolvedC  : " + ArrayUtils.toString(presolvedC.toArray()));
        log.debug("presolvedA  : " + ArrayUtils.toString(presolvedA.toArray()));
        log.debug("presolvedB  : " + ArrayUtils.toString(presolvedB.toArray()));
        log.debug("presolvedLb : " + ArrayUtils.toString(presolvedLb.toArray()));
        log.debug("presolvedUb : " + ArrayUtils.toString(presolvedUb.toArray()));
        log.debug("presolvedYlb: " + ArrayUtils.toString(presolvedYlb.toArray()));
        log.debug("presolvedYub: " + ArrayUtils.toString(presolvedYub.toArray()));
        log.debug("presolvedZlb: " + ArrayUtils.toString(presolvedZlb.toArray()));
        log.debug("presolvedZub: " + ArrayUtils.toString(presolvedZub.toArray()));
    }

    if (n == 0) {
        // deterministic problem
        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);
        }
    } else {

        Utils.writeDoubleArrayToFile(presolvedC.toArray(),
                "target" + File.separator + "presolvedC_" + problemName + ".txt");
        Utils.writeDoubleMatrixToFile(presolvedA.toArray(),
                "target" + File.separator + "presolvedA_" + problemName + ".csv");
        Utils.writeDoubleArrayToFile(presolvedB.toArray(),
                "target" + File.separator + "presolvedB_" + problemName + ".txt");
        Utils.writeDoubleArrayToFile(presolvedLb.toArray(),
                "target" + File.separator + "presolvedLB_" + problemName + ".txt");
        Utils.writeDoubleArrayToFile(presolvedUb.toArray(),
                "target" + File.separator + "presolvedUB_" + problemName + ".txt");

        // check objective function
        double delta = expectedTolerance;
        RealVector presolvedES = MatrixUtils.createRealVector(lpPresolver.presolve(expectedSolution));
        double presolvedEV = MatrixUtils.createRealVector(presolvedC.toArray()).dotProduct(presolvedES);// in general it is different from the optimal value
        log.debug("presolved expected value: " + presolvedEV);
        RealVector postsolvedES = MatrixUtils.createRealVector(lpPresolver.postsolve(presolvedES.toArray()));
        double postsolvedEV = MatrixUtils.createRealVector(c).dotProduct(postsolvedES);
        //assertEquals(expectedValue, postsolvedEV, delta);
        assertTrue(Math.abs((expectedValue - postsolvedEV) / expectedValue) < 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 <= postsolvedES.getEntry(i) + delta);
        }
        for (int i = 0; i < ub.length; i++) {
            double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i];
            assertTrue(di + delta >= postsolvedES.getEntry(i));
        }
        RealVector Axmb = AMatrix.operate(postsolvedES).subtract(bVector);
        assertEquals(0., Axmb.getNorm(), 1.5 * expectedTolerance);

        // check presolved constraints
        assertEquals(presolvedLb.size(), presolvedES.getDimension());
        assertEquals(presolvedUb.size(), presolvedES.getDimension());
        AMatrix = MatrixUtils.createRealMatrix(presolvedA.toArray());//reassigned to avoid memory consumption
        bVector = MatrixUtils.createRealVector(presolvedB.toArray());
        for (int i = 0; i < presolvedLb.size(); i++) {
            double di = Double.isNaN(presolvedLb.getQuick(i)) ? -Double.MAX_VALUE : presolvedLb.getQuick(i);
            assertTrue(di <= presolvedES.getEntry(i) + delta);
        }
        for (int i = 0; i < presolvedUb.size(); i++) {
            double di = Double.isNaN(presolvedUb.getQuick(i)) ? Double.MAX_VALUE : presolvedUb.getQuick(i);
            assertTrue(di + delta >= presolvedES.getEntry(i));
        }
        Axmb = AMatrix.operate(presolvedES).subtract(bVector);
        assertEquals(0., Axmb.getNorm(), 1.5 * expectedTolerance);

        //check for 0-rows
        List<Integer> zeroRows = new ArrayList<Integer>();
        for (int i = 0; i < presolvedA.rows(); i++) {
            boolean isNotZero = false;
            for (int j = 0; !isNotZero && j < presolvedA.columns(); j++) {
                isNotZero = Double.compare(0., presolvedA.getQuick(i, j)) != 0;
            }
            if (!isNotZero) {
                zeroRows.add(zeroRows.size(), i);
            }
        }
        if (!zeroRows.isEmpty()) {
            log.debug("All 0 entries in rows " + ArrayUtils.toString(zeroRows));
            fail();
        }

        //check for 0-columns
        List<Integer> zeroCols = new ArrayList<Integer>();
        for (int j = 0; j < presolvedA.columns(); j++) {
            boolean isNotZero = false;
            for (int i = 0; !isNotZero && i < presolvedA.rows(); i++) {
                isNotZero = Double.compare(0., presolvedA.getQuick(i, j)) != 0;
            }
            if (!isNotZero) {
                zeroCols.add(zeroCols.size(), j);
            }
        }
        if (!zeroCols.isEmpty()) {
            log.debug("All 0 entries in columns " + ArrayUtils.toString(zeroCols));
            fail();
        }

        // check rank(A): must be A pXn with rank(A)=p < n
        qr = new QRSparseFactorization(new SparseDoubleMatrix2D(presolvedA.toArray()));
        qr.factorize();
        boolean isFullRank = qr.hasFullRank();
        log.debug("p        : " + AMatrix.getRowDimension());
        log.debug("n        : " + AMatrix.getColumnDimension());
        log.debug("full rank: " + isFullRank);
        assertTrue(AMatrix.getRowDimension() < AMatrix.getColumnDimension());
        assertTrue(isFullRank);
    }
}

From source file:edu.cudenver.bios.power.glmm.GLMMTest.java

/**
 * Create a test to perform data analysis
 * @param fMethod/*from   w  w w . j ava 2s . co m*/
 * @param cdfMethod
 * @param X
 * @param XtXInverse
 * @param rank
 * @param Y
 * @param C
 * @param U
 * @param thetaNull
 */
public GLMMTest(FApproximation fMethod, RealMatrix X, RealMatrix XtXInverse, int rank, RealMatrix Y,
        RealMatrix C, RealMatrix U, RealMatrix thetaNull) {
    this.fMethod = fMethod;
    this.Xessence = null;
    this.XtXInverse = XtXInverse;
    if (this.XtXInverse == null) {
        this.XtXInverse = new LUDecomposition(X.transpose().multiply(X)).getSolver().getInverse();
    }
    this.totalN = X.getRowDimension();
    this.rank = rank;
    this.C = C;
    this.U = U;
    this.thetaNull = thetaNull;
    this.beta = this.XtXInverse.multiply(X.transpose()).multiply(Y);
    RealMatrix YHat = X.multiply(this.beta);
    RealMatrix Ydiff = Y.subtract(YHat);
    this.sigmaError = (Ydiff.transpose().multiply(Ydiff))
            .scalarMultiply(((double) 1 / (double) (totalN - rank)));

    // check if uni/multivariate design
    multivariate = (Y.getColumnDimension() > 1);

    // cache the value of M
    RealMatrix cxxcEssence = C.multiply((this.XtXInverse).multiply(C.transpose()));
    M = new LUDecomposition(cxxcEssence).getSolver().getInverse();
}

From source file:edu.cudenver.bios.power.glmm.NonCentralityDistribution.java

/**
 * Calculate the inverse of the sigma star matrix
 * @param params GLMM input parameters//from  www  . j a  v a2s . c om
 * @return sigma star inverse
 */
private RealMatrix getSigmaStarInverse(RealMatrix U, RealMatrix sigmaError, Test test) {
    // sigma* = U'*sigmaE*U
    RealMatrix sigmaStar = forceSymmetric(U.transpose().multiply(sigmaError).multiply(U));
    debug("U", U);
    debug("sigmaError", sigmaError);
    debug("sigmaStar = U transpose * sigmaError * U", sigmaStar);

    if (!MatrixUtils.isPositiveDefinite(sigmaStar)) {
        throw new IllegalArgumentException(NOT_POSITIVE_DEFINITE);
    }

    if (test == Test.HOTELLING_LAWLEY_TRACE) {
        return new LUDecomposition(sigmaStar).getSolver().getInverse();
    } else {
        // stat should only be UNIREP (uncorrected, box, GG, or HF) at this point
        // (exception is thrown by valdiateParams otherwise)
        int b = sigmaStar.getColumnDimension();
        // get discrepancy from sphericity for unirep test
        double sigmaStarTrace = sigmaStar.getTrace();
        double sigmaStarSquaredTrace = sigmaStar.multiply(sigmaStar).getTrace();
        double epsilon = (sigmaStarTrace * sigmaStarTrace) / ((double) b * sigmaStarSquaredTrace);
        RealMatrix identity = org.apache.commons.math3.linear.MatrixUtils.createRealIdentityMatrix(b);
        return identity.scalarMultiply((double) b * epsilon / sigmaStarTrace);
    }
}

From source file:com.github.tteofili.looseen.yay.SGM.java

/**
 * predict network output given an input
 *
 * @param input the input/*from   w w w .  j  av a 2s .c  o  m*/
 * @return the output
 * @throws Exception
 */
private double[] predictOutput(double[] input) throws Exception {

    RealMatrix hidden = rectifierFunction.applyMatrix(
            MatrixUtils.createRowRealMatrix(input).multiply(weights[0].transpose()).add(biases[0]));
    RealMatrix scores = hidden.multiply(weights[1].transpose()).add(biases[1]);

    RealMatrix probs = scores.copy();
    int len = scores.getColumnDimension() - 1;
    for (int d = 0; d < configuration.window - 1; d++) {
        int startColumn = d * len / (configuration.window - 1);
        RealMatrix subMatrix = scores.getSubMatrix(0, scores.getRowDimension() - 1, startColumn,
                startColumn + input.length);
        for (int sm = 0; sm < subMatrix.getRowDimension(); sm++) {
            probs.setSubMatrix(softmaxActivationFunction.applyMatrix(subMatrix.getRowMatrix(sm)).getData(), sm,
                    startColumn);
        }
    }

    RealVector d = probs.getRowVector(0);
    return d.toArray();
}

From source file:lirmm.inria.fr.math.BigSparseRealMatrixTest.java

/**
 * test examples in user guide/*from  ww w.jav a 2  s. com*/
 */
@Test
public void testExamples() {
    // Create a real matrix with two rows and three columns
    double[][] matrixData = { { 1d, 2d, 3d }, { 2d, 5d, 3d } };
    RealMatrix m = new BigSparseRealMatrix(matrixData);
    // One more with three rows, two columns
    double[][] matrixData2 = { { 1d, 2d }, { 2d, 5d }, { 1d, 7d } };
    RealMatrix n = new BigSparseRealMatrix(matrixData2);
    // Now specialOperation m by n
    RealMatrix p = m.multiply(n);
    Assert.assertEquals(2, p.getRowDimension());
    Assert.assertEquals(2, p.getColumnDimension());
    // Invert p
    RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse();
    Assert.assertEquals(2, pInverse.getRowDimension());
    Assert.assertEquals(2, pInverse.getColumnDimension());

    // Solve example
    double[][] coefficientsData = { { 2, 3, -2 }, { -1, 7, 6 }, { 4, -3, -5 } };
    RealMatrix coefficients = new BigSparseRealMatrix(coefficientsData);
    RealVector constants = new ArrayRealVector(new double[] { 1, -2, 1 }, false);
    RealVector solution = new LUDecomposition(coefficients).getSolver().solve(constants);
    final double cst0 = constants.getEntry(0);
    final double cst1 = constants.getEntry(1);
    final double cst2 = constants.getEntry(2);
    final double sol0 = solution.getEntry(0);
    final double sol1 = solution.getEntry(1);
    final double sol2 = solution.getEntry(2);
    Assert.assertEquals(2 * sol0 + 3 * sol1 - 2 * sol2, cst0, 1E-12);
    Assert.assertEquals(-1 * sol0 + 7 * sol1 + 6 * sol2, cst1, 1E-12);
    Assert.assertEquals(4 * sol0 - 3 * sol1 - 5 * sol2, cst2, 1E-12);
}

From source file:lirmm.inria.fr.math.BigSparseRealMatrixTest.java

/**
 * Returns the result of applying the given row permutation to the matrix
 *///w  ww. j  a  va2  s  .c o  m
protected RealMatrix permuteRows(RealMatrix matrix, int[] permutation) {
    if (!matrix.isSquare()) {
        throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension());
    }
    if (matrix.getRowDimension() != permutation.length) {
        throw new DimensionMismatchException(matrix.getRowDimension(), permutation.length);
    }

    int n = matrix.getRowDimension();
    int m = matrix.getColumnDimension();
    double out[][] = new double[m][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            out[i][j] = matrix.getEntry(permutation[i], j);
        }
    }
    return new BigSparseRealMatrix(out);
}

From source file:lirmm.inria.fr.math.BigSparseRealMatrixTest.java

/**
 * extracts the l and u matrices from compact lu representation
 *//*from   w  w  w .  j  av  a 2  s  . c om*/
protected void splitLU(RealMatrix lu, double[][] lowerData, double[][] upperData) {
    if (!lu.isSquare()) {
        throw new NonSquareMatrixException(lu.getRowDimension(), lu.getColumnDimension());
    }
    if (lowerData.length != lowerData[0].length) {
        throw new DimensionMismatchException(lowerData.length, lowerData[0].length);
    }
    if (upperData.length != upperData[0].length) {
        throw new DimensionMismatchException(upperData.length, upperData[0].length);
    }
    if (lowerData.length != upperData.length) {
        throw new DimensionMismatchException(lowerData.length, upperData.length);
    }
    if (lowerData.length != lu.getRowDimension()) {
        throw new DimensionMismatchException(lowerData.length, lu.getRowDimension());
    }

    int n = lu.getRowDimension();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (j < i) {
                lowerData[i][j] = lu.getEntry(i, j);
                upperData[i][j] = 0d;
            } else if (i == j) {
                lowerData[i][j] = 1d;
                upperData[i][j] = lu.getEntry(i, j);
            } else {
                lowerData[i][j] = 0d;
                upperData[i][j] = lu.getEntry(i, j);
            }
        }
    }
}

From source file:com.github.tteofili.looseen.yay.SGM.java

private RealMatrix[] initWeights() {
    int[] conf = new int[] { configuration.inputs, configuration.vectorSize, configuration.outputs };
    int[] layers = new int[conf.length];
    System.arraycopy(conf, 0, layers, 0, layers.length);
    int weightsCount = layers.length - 1;

    RealMatrix[] initialWeights = new RealMatrix[weightsCount];

    for (int i = 0; i < weightsCount; i++) {
        RealMatrix matrix = MatrixUtils.createRealMatrix(layers[i + 1], layers[i]);

        UniformRealDistribution uniformRealDistribution = new UniformRealDistribution();
        double[] vs = uniformRealDistribution.sample(matrix.getRowDimension() * matrix.getColumnDimension());
        int r = 0;
        int c = 0;
        for (double v : vs) {
            matrix.setEntry(r % matrix.getRowDimension(), c % matrix.getColumnDimension(), v);
            r++;//from www  .j ava 2s . c o  m
            c++;
        }

        initialWeights[i] = matrix;
    }
    return initialWeights;

}