List of usage examples for org.apache.commons.math3.linear RealMatrix getRowDimension
int getRowDimension();
From source file:org.meteoinfo.math.linalg.LinalgUtil.java
/** * Calculates the LUP-decomposition of a square matrix. * The LUP-decomposition of a matrix A consists of three matrices L, U and P that satisfy: * PA = LU. L is lower triangular (with unit diagonal terms), U is upper triangular and P is * a permutation matrix. All matrices are mm. * @param a Given matrix./*from ww w .j a va 2 s . c om*/ * @return Result P/L/U arrays. */ public static Array[] lu(Array a) { Array Pa = Array.factory(DataType.DOUBLE, a.getShape()); Array La = Array.factory(DataType.DOUBLE, a.getShape()); Array Ua = Array.factory(DataType.DOUBLE, a.getShape()); double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a); RealMatrix matrix = new Array2DRowRealMatrix(aa, false); LUDecomposition decomposition = new LUDecomposition(matrix); RealMatrix P = decomposition.getP(); RealMatrix L = decomposition.getL(); RealMatrix U = decomposition.getU(); int n = L.getColumnDimension(); int m = L.getRowDimension(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { Pa.setDouble(i * n + j, P.getEntry(i, j)); La.setDouble(i * n + j, L.getEntry(i, j)); Ua.setDouble(i * n + j, U.getEntry(i, j)); } } return new Array[] { Pa, La, Ua }; }
From source file:org.meteoinfo.math.stats.StatsUtil.java
/** * Computes covariances for pairs of arrays or columns of a matrix. * * @param x X data//from w w w . ja v a2 s .c o 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 www.j a v a 2 s . c o m*/ * @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 a2s . c om * @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//from w w w .ja v a2 s . c om * @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. * // w w w . j av a 2 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. * //from www. jav a2s.co 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);// ww w .j a v a2s . c om 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.ojalgo.benchmark.lab.library.ACM.java
@Override protected Array2DRowRealMatrix copy(final RealMatrix source, final Array2DRowRealMatrix destination) { for (int i = 0, rlim = source.getRowDimension(); i < rlim; i++) { for (int j = 0, clim = destination.getColumnDimension(); j < clim; j++) { destination.setEntry(i, j, source.getEntry(i, j)); }//from w w w . j a v a 2 s . co m } return destination; }
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)); }/*from w w w . j a va 2 s . com*/ } }