List of usage examples for org.apache.commons.math3.linear RealMatrix getEntry
double getEntry(int row, int column) throws OutOfRangeException;
From source file:org.meteoinfo.math.linalg.LinalgUtil.java
/** * Calculates the Cholesky decomposition of a matrix. * The Cholesky decomposition of a real symmetric positive-definite matrix A consists of a * lower triangular matrix L with same size such that: A = LLT. In a sense, this is * the square root of A./*w w w .ja v a 2s . c om*/ * @param a The given matrix. * @return Result array. */ public static Array cholesky(Array a) { Array r = Array.factory(DataType.DOUBLE, a.getShape()); double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a); RealMatrix matrix = new Array2DRowRealMatrix(aa, false); CholeskyDecomposition decomposition = new CholeskyDecomposition(matrix); RealMatrix L = decomposition.getL(); int n = L.getColumnDimension(); int m = L.getRowDimension(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { r.setDouble(i * n + j, L.getEntry(i, j)); } } return r; }
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 w w w . j a v a 2 s . c o m*/ * @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.linalg.LinalgUtil.java
/** * Calculates the QR-decomposition of a matrix. * The QR-decomposition of a matrix A consists of two matrices Q and R that satisfy: A = QR, Q * is orthogonal (QTQ = I), and R is upper triangular. If A is mn, Q is mm and R mn. * @param a Given matrix./*from www .j a v a 2 s. co m*/ * @return Result Q/R arrays. */ public static Array[] qr(Array a) { int m = a.getShape()[0]; int n = a.getShape()[1]; Array Qa = Array.factory(DataType.DOUBLE, new int[] { m, m }); Array Ra = Array.factory(DataType.DOUBLE, a.getShape()); double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a); RealMatrix matrix = new Array2DRowRealMatrix(aa, false); QRDecomposition decomposition = new QRDecomposition(matrix); RealMatrix Q = decomposition.getQ(); RealMatrix R = decomposition.getR(); for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { Qa.setDouble(i * m + j, Q.getEntry(i, j)); } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { Ra.setDouble(i * n + j, R.getEntry(i, j)); } } return new Array[] { Qa, Ra }; }
From source file:org.meteoinfo.math.linalg.LinalgUtil.java
/** * Calculates the compact Singular Value Decomposition of a matrix. * The Singular Value Decomposition of matrix A is a set of three matrices: U, and V * such that A = U VT. Let A be a m n matrix, then U is a m p orthogonal * matrix, is a p p diagonal matrix with positive or null elements, V is a p n * orthogonal matrix (hence VT is also orthogonal) where p=min(m,n). * @param a Given matrix.//from w w w . j a v a 2 s.c om * @return Result U/S/V arrays. */ public static Array[] svd(Array a) { int m = a.getShape()[0]; int n = a.getShape()[1]; int k = Math.min(m, n); Array Ua = Array.factory(DataType.DOUBLE, new int[] { m, k }); Array Va = Array.factory(DataType.DOUBLE, new int[] { k, n }); Array Sa = Array.factory(DataType.DOUBLE, new int[] { k }); double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a); RealMatrix matrix = new Array2DRowRealMatrix(aa, false); SingularValueDecomposition decomposition = new SingularValueDecomposition(matrix); RealMatrix U = decomposition.getU(); RealMatrix V = decomposition.getVT(); double[] sv = decomposition.getSingularValues(); for (int i = 0; i < m; i++) { for (int j = 0; j < k; j++) { Ua.setDouble(i * k + j, U.getEntry(i, j)); } } for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) { Va.setDouble(i * n + j, V.getEntry(i, j)); } } for (int i = 0; i < k; i++) { Sa.setDouble(i, sv[i]); } return new Array[] { Ua, Sa, Va }; }
From source file:org.meteoinfo.math.stats.StatsUtil.java
/** * Computes covariances for pairs of arrays or columns of a matrix. * * @param x X data/*www . j ava 2 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 w w w.ja va 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 www . j ava 2 s .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. j a v a 2s . 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.micromanager.plugins.magellan.coordinates.AffineCalibrator.java
private AffineTransform computeAffine(Point2D.Double pix1, Point2D.Double pix2, Point2D.Double pix3, Point2D.Double stage1, Point2D.Double stage2, Point2D.Double stage3) { //solve system x' = xA for A, x is stage points and x' is pixel points //6x6 matrix/*from ww w .jav a2 s.c o m*/ RealMatrix pixPoints = new Array2DRowRealMatrix(new double[][] { { pix1.x, pix1.y, 1, 0, 0, 0 }, { 0, 0, 0, pix1.x, pix1.y, 1 }, { pix2.x, pix2.y, 1, 0, 0, 0 }, { 0, 0, 0, pix2.x, pix2.y, 1 }, { pix3.x, pix3.y, 1, 0, 0, 0 }, { 0, 0, 0, pix3.x, pix3.y, 1 } }); //6x1 matrix RealMatrix stagePoints = new Array2DRowRealMatrix( new double[] { stage1.x, stage1.y, stage2.x, stage2.y, stage3.x, stage3.y }); //invert stagePoints matrix RealMatrix stagePointsInv = new LUDecomposition(pixPoints).getSolver().getInverse(); RealMatrix A = stagePointsInv.multiply(stagePoints); AffineTransform transform = new AffineTransform( new double[] { A.getEntry(0, 0), A.getEntry(3, 0), A.getEntry(1, 0), A.getEntry(4, 0) }); return transform; }
From source file:org.moeaframework.TestUtils.java
/** * Asserts that the two matrices are equal. * /*from w ww. j a v a2 s . c om*/ * @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)); } } }