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:org.meteoinfo.math.stats.StatsUtil.java

/**
 * Computes covariances for pairs of arrays or columns of a matrix.
 *
 * @param x X data//ww w. ja  v a2  s.co m
 * @param y Y data
 * @param bias If true, returned value will be bias-corrected
 * @return The covariance matrix
 */
public static Array cov(Array x, Array y, boolean bias) {
    int m = x.getShape()[0];
    int n = 1;
    if (x.getRank() == 2)
        n = x.getShape()[1];
    double[][] aa = new double[m][n * 2];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n * 2; j++) {
            if (j < n) {
                aa[i][j] = x.getDouble(i * n + j);
            } else {
                aa[i][j] = y.getDouble(i * n + j - n);
            }
        }
    }
    RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
    Covariance cov = new Covariance(matrix, bias);
    RealMatrix mcov = cov.getCovarianceMatrix();
    m = mcov.getColumnDimension();
    n = mcov.getRowDimension();
    Array r = Array.factory(DataType.DOUBLE, new int[] { m, n });
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            r.setDouble(i * n + j, mcov.getEntry(i, j));
        }
    }

    return r;
}

From source file:org.meteoinfo.math.stats.StatsUtil.java

/**
 * Computes covariances for columns of a matrix.
 * @param a Matrix data//from  w w w  .  j a  v a2 s .  c  om
 * @param bias If true, returned value will be bias-corrected
 * @return Covariant matrix or value
 */
public static Object cov(Array a, boolean bias) {
    if (a.getRank() == 1) {
        double[] ad = (double[]) ArrayUtil.copyToNDJavaArray(a);
        Covariance cov = new Covariance();
        return cov.covariance(ad, ad);
    } else {
        double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a);
        RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
        Covariance cov = new Covariance(matrix, bias);
        RealMatrix mcov = cov.getCovarianceMatrix();
        int m = mcov.getColumnDimension();
        int n = mcov.getRowDimension();
        Array r = Array.factory(DataType.DOUBLE, new int[] { m, n });
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                r.setDouble(i * n + j, mcov.getEntry(i, j));
            }
        }

        return r;
    }
}

From source file:org.meteoinfo.math.stats.StatsUtil.java

/**
 * Computes Spearman's rank correlation for pairs of arrays or columns of a matrix.
 *
 * @param x X data/*from  ww  w  .  jav a2 s  .  co m*/
 * @param y Y data
 * @return Spearman's rank correlation
 */
public static Array spearmanr(Array x, Array y) {
    int m = x.getShape()[0];
    int n = 1;
    if (x.getRank() == 2)
        n = x.getShape()[1];
    double[][] aa = new double[m][n * 2];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n * 2; j++) {
            if (j < n) {
                aa[i][j] = x.getDouble(i * n + j);
            } else {
                aa[i][j] = y.getDouble(i * n + j - n);
            }
        }
    }
    RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
    SpearmansCorrelation cov = new SpearmansCorrelation(matrix);
    RealMatrix mcov = cov.getCorrelationMatrix();
    m = mcov.getColumnDimension();
    n = mcov.getRowDimension();
    Array r = Array.factory(DataType.DOUBLE, new int[] { m, n });
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            r.setDouble(i * n + j, mcov.getEntry(i, j));
        }
    }

    return r;
}

From source file:org.meteoinfo.math.stats.StatsUtil.java

/**
 * Computes Spearman's rank correlation for columns of a matrix.
 * @param a Matrix data//  w  w  w.  j  av a 2 s.  c  o m
 * @return Spearman's rank correlation
 */
public static Object spearmanr(Array a) {
    if (a.getRank() == 1) {
        double[] ad = (double[]) ArrayUtil.copyToNDJavaArray(a);
        Covariance cov = new Covariance();
        return cov.covariance(ad, ad);
    } else {
        double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a);
        RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
        SpearmansCorrelation cov = new SpearmansCorrelation(matrix);
        RealMatrix mcov = cov.getCorrelationMatrix();
        int m = mcov.getColumnDimension();
        int n = mcov.getRowDimension();
        Array r = Array.factory(DataType.DOUBLE, new int[] { m, n });
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                r.setDouble(i * n + j, mcov.getEntry(i, j));
            }
        }

        return r;
    }
}

From source file:org.moeaframework.TestUtils.java

/**
 * Asserts that the two matrices are equal.
 * /*from  w  w w  .  j av  a2 s  .c  o m*/
 * @param rm1 the first matrix
 * @param rm2 the second matrix
 * @param error the equality comparison used to assert pairwise values 
 *        are equal
 */
public static void assertEquals(RealMatrix rm1, RealMatrix rm2, FloatingPointError error) {
    Assert.assertEquals(rm1.getRowDimension(), rm2.getRowDimension());
    Assert.assertEquals(rm1.getColumnDimension(), rm2.getColumnDimension());

    for (int i = 0; i < rm1.getRowDimension(); i++) {
        for (int j = 0; j < rm2.getColumnDimension(); j++) {
            error.assertEquals(rm1.getEntry(i, j), rm2.getEntry(i, j));
        }
    }
}

From source file:org.moeaframework.util.RotationMatrixBuilderTest.java

/**
 * Tests if the matrix is the identity matrix.
 * /*  ww  w .  j  a  v  a2 s.  c  o  m*/
 * @param rm the matrix to test
 */
public void testIdentityMatrix(RealMatrix rm) {
    for (int i = 0; i < rm.getRowDimension(); i++) {
        for (int j = 0; j < rm.getColumnDimension(); j++) {
            if (i == j) {
                Assert.assertEquals(1.0, rm.getEntry(i, j), Settings.EPS);
            } else {
                Assert.assertEquals(0.0, rm.getEntry(i, j), Settings.EPS);
            }
        }
    }
}

From source file:org.nd4j.linalg.Nd4jTestsComparisonFortran.java

@Test
public void testGemvApacheCommons() {

    int[] rowsArr = new int[] { 4, 4, 4, 8, 8, 8 };
    int[] colsArr = new int[] { 2, 1, 10, 2, 1, 10 };

    for (int x = 0; x < rowsArr.length; x++) {
        int rows = rowsArr[x];
        int cols = colsArr[x];

        List<Pair<INDArray, String>> matrices = NDArrayCreationUtil.getAllTestMatricesWithShape(rows, cols,
                12345);// w ww.  j  a v a  2 s  .  c o  m
        List<Pair<INDArray, String>> vectors = NDArrayCreationUtil.getAllTestMatricesWithShape(cols, 1, 12345);

        for (int i = 0; i < matrices.size(); i++) {
            for (int j = 0; j < vectors.size(); j++) {

                Pair<INDArray, String> p1 = matrices.get(i);
                Pair<INDArray, String> p2 = vectors.get(j);
                String errorMsg = getTestWithOpsErrorMsg(i, j, "mmul", p1, p2);

                INDArray m = p1.getFirst();
                INDArray v = p2.getFirst();

                RealMatrix rm = new BlockRealMatrix(m.rows(), m.columns());
                for (int r = 0; r < m.rows(); r++) {
                    for (int c = 0; c < m.columns(); c++) {
                        double d = m.getDouble(r, c);
                        rm.setEntry(r, c, d);
                    }
                }

                RealMatrix rv = new BlockRealMatrix(cols, 1);
                for (int r = 0; r < v.rows(); r++) {
                    double d = v.getDouble(r, 0);
                    rv.setEntry(r, 0, d);
                }

                INDArray gemv = m.mmul(v);
                RealMatrix gemv2 = rm.multiply(rv);

                assertArrayEquals(new int[] { rows, 1 }, gemv.shape());
                assertArrayEquals(new int[] { rows, 1 },
                        new int[] { gemv2.getRowDimension(), gemv2.getColumnDimension() });

                //Check entries:
                for (int r = 0; r < rows; r++) {
                    double exp = gemv2.getEntry(r, 0);
                    double act = gemv.getDouble(r, 0);
                    assertEquals(errorMsg, exp, act, 1e-5);
                }
            }
        }
    }
}

From source file:org.opentestsystem.airose.linear.Matrices.java

public static void copyTo(Matrix matrix, Matrix output) {
    RealMatrix source = matrix._matrix;
    RealMatrix destination = output._matrix;
    for (int row = 0; row < source.getRowDimension(); ++row) {
        for (int column = 0; column < source.getColumnDimension(); ++column) {
            destination.setEntry(row, column, source.getEntry(row, column));
        }// w w w  .java 2  s  .c  o m
    }
}

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

/**
 * Build the transformation to Hessenberg form of a general matrix.
 *
 * @param matrix matrix to transform//from   w w  w  . j a  v a 2  s . c  om
 * @throws NonSquareMatrixException if the matrix is not square
 */
public HessenbergTransformer(final RealMatrix matrix) {
    if (!matrix.isSquare()) {
        throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension());
    }

    final int m = matrix.getRowDimension();
    householderVectors = matrix.getData();
    ort = new double[m];
    cachedP = null;
    cachedPt = null;
    cachedH = null;

    // transform matrix
    transform();
}

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

/**
 * Calculates the eigen decomposition of the given real matrix.
 * <p>/*from ww  w . j  a v  a  2 s  . co m*/
 * Supports decomposition of a general matrix since 3.1.
 *
 * @param matrix Matrix to decompose.
 * @throws MaxCountExceededException if the algorithm fails to converge.
 * @throws MathArithmeticException if the decomposition of a general matrix
 * results in a matrix with zero norm
 * @since 3.1
 */
public MyEDC(final RealMatrix matrix) throws MathArithmeticException {
    final double symTol = 10 * matrix.getRowDimension() * matrix.getColumnDimension() * Precision.EPSILON;
    isSymmetric = MatrixUtils.isSymmetric(matrix, symTol);
    if (isSymmetric) {
        transformToTridiagonal(matrix);
        findEigenVectors(transformer.getQ().getData());
    } else {
        final SchurTransformer t = transformToSchur(matrix);
        findEigenVectorsFromSchur(t);
    }
}