List of usage examples for org.apache.commons.math3.linear RealMatrix getEntry
double getEntry(int row, int column) throws OutOfRangeException;
From source file:io.github.malapert.jwcs.coordsystem.Utility.java
/** * Given Cartesian x,y,z return corresponding longitude and latitude in * degrees./*w w w .ja va2 s .co m*/ * * Notes: * ------ * Note that one can expect strange behavior for the values of the * longitudes very close to the pole. In fact, at the poles itself, * the longitudes are meaningless. * * @param xyz Vector with values for x,y,z * @return The same number of positions (longitude, latitude and in the * same order as the input. */ public final static double[] xyz2longlat(final RealMatrix xyz) { double x = xyz.getEntry(0, 0); double y = xyz.getEntry(1, 0); double z = xyz.getEntry(2, 0); double longitude = Math.toDegrees(Math.atan2(y, x)); longitude = (longitude < 0) ? longitude + 360.0d : longitude; double latitude = Math.toDegrees(Math.asin(z)); double coord[] = { longitude, latitude }; return coord; }
From source file:edu.cmu.tetrad.util.MatrixUtils1.java
public static double[][] pseudoInverse(double[][] x) { SingularValueDecomposition svd = new SingularValueDecomposition(new BlockRealMatrix(x)); RealMatrix U = svd.getU();//from w ww. ja v a 2 s. c o m RealMatrix V = svd.getV(); RealMatrix S = svd.getS(); for (int i = 0; i < S.getRowDimension(); i++) { for (int j = 0; j < S.getColumnDimension(); j++) { double v = S.getEntry(i, j); S.setEntry(i, j, v == 0 ? 0.0 : 1.0 / v); } } return V.multiply(S.multiply(U.transpose())).getData(); }
From source file:io.yields.math.concepts.operator.Smoothness.java
private boolean isConstant(RealMatrix matrix) { double refElement = matrix.getEntry(0, 0); return !IntStream.range(0, matrix.getRowDimension()).asDoubleStream() .filter(element -> Math.abs(refElement - element) > EPS).findAny().isPresent(); }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * Calculate the Kronecker product of two matrices * * @param matrix1 first matrix// w ww. j a v a2 s. c o m * @param matrix2 second matrix * @return Kronecker product of matrix 1 and matrix 2 */ public static RealMatrix getKroneckerProduct(RealMatrix matrix1, RealMatrix matrix2) { if (matrix1 == null || matrix2 == null) throw new IllegalArgumentException("null input matrix"); int m1Rows = matrix1.getRowDimension(); int m1Cols = matrix1.getColumnDimension(); int m2Rows = matrix2.getRowDimension(); int m2Cols = matrix2.getColumnDimension(); double[][] productData = new double[m1Rows * m2Rows][m1Cols * m2Cols]; RealMatrix productMatrix = new Array2DRowRealMatrix(productData); for (int col = 0; col < m1Cols; col++) { for (int row = 0; row < m1Rows; row++) { productMatrix.setSubMatrix((matrix2.scalarMultiply(matrix1.getEntry(row, col))).getData(), row * m2Rows, col * m2Cols); } } return productMatrix; }
From source file:com.cloudera.oryx.common.math.MatrixUtilsTest.java
@Test public void testMultiplyXYT() { LongObjectMap<float[]> X = new LongObjectMap<float[]>(); X.put(0L, new float[] { 2.0f, 3.5f }); X.put(1L, new float[] { -1.0f, 0.0f }); LongObjectMap<float[]> Y = new LongObjectMap<float[]>(); Y.put(0L, new float[] { 0.0f, -3.0f }); Y.put(1L, new float[] { -0.5f, 1.5f }); RealMatrix product = multiplyXYT(X, Y); assertEquals(-10.5, product.getEntry(0, 0)); assertEquals(4.25, product.getEntry(0, 1)); assertEquals(0.0, product.getEntry(1, 0)); assertEquals(0.5, product.getEntry(1, 1)); }
From source file:com.caseystella.analytics.outlier.batch.rpca.RidgeRegression.java
private double[] getDiagonal(RealMatrix X) { double[] diag = new double[X.getColumnDimension()]; for (int i = 0; i < diag.length; i++) { diag[i] = X.getEntry(i, i); }//from w w w . j a v a2 s .c o m return diag; }
From source file:com.itemanalysis.psychometrics.mixture.MvNormalComponentDistribution.java
/** * * @param x a matrix of dimension 1 x k, where k is the number of variables * @return/*from ww w.j ava2 s . co m*/ */ public double density(RealMatrix x) throws SingularMatrixException { double prob = 0.0; RealMatrix xTran = x.transpose(); int d = xTran.getRowDimension(); double det = new LUDecomposition(sigma).getDeterminant(); double nconst = 1.0 / Math.sqrt(det * Math.pow(2.0 * Math.PI, d)); RealMatrix Sinv = new LUDecomposition(sigma).getSolver().getInverse(); RealMatrix delta = xTran.subtract(mu); RealMatrix dist = (delta.transpose().multiply(Sinv).multiply(delta)); prob = nconst * Math.exp(-0.5 * dist.getEntry(0, 0)); return prob; }
From source file:io.github.malapert.jwcs.coordsystem.Utility.java
/** * Remove the elliptic component of annual aberration when this is included * in a catalogue fk4 position.//from w w w .j av a 2s.c om * * Notes: * ------ * Return a new position where the elliptic terms of aberration * are removed i.e. convert a apparent position from a catalog to * a mean place. * The effects of ecliptic aberration were included in the * catalog positions to facilitate telescope pointing. * See also notes at 'addEterms'. * * @param xyz vector xyz * @param eterm E-terms vector (as returned by getEterms()). If input a is * omitted (== *null*), the e-terms for 1950 will be substituted. * @return **Mean place** */ public final static RealMatrix removeEterms(final RealMatrix xyz, RealMatrix eterm) { RealMatrix xyzeterm = xyz.copy(); if (eterm == null) { eterm = FK4.getEterms(1950); } double x = xyz.getEntry(0, 0); double y = xyz.getEntry(1, 0); double z = xyz.getEntry(2, 0); x -= eterm.getEntry(0, 0); y -= eterm.getEntry(0, 1); z -= eterm.getEntry(0, 2); xyzeterm.setEntry(0, 0, x); xyzeterm.setEntry(1, 0, y); xyzeterm.setEntry(2, 0, z); return xyzeterm; }
From source file:io.github.malapert.jwcs.coordsystem.Utility.java
/** * Add the elliptic component of annual aberration when the rsult must be a * catalogue fk4 position./*from ww w. j ava2 s.c o m*/ * * Reference: * ---------- * * Seidelman, P.K., 1992. Explanatory Supplement to the Astronomical * Almanac. University Science Books, Mill Valley. * * Yallop et al, Transformation of mean star places, * AJ, 1989, vol 97, page 274 * * Stumpff, On the relation between Classical and Relativistic * Theory of Stellar Aberration, Astron, Astrophys, 84, 257-259 (1980) * * Notes: * ------ * There is a so called ecliptic component in the stellar aberration. * This vector depends on the epoch at which we want to process * these terms. It corresponds to the component of the earth's velocity * perpendicular to the major axis of the ellipse in the ecliptic. * The E-term corrections are as follows. A catalog FK4 position * include corrections for elliptic terms of aberration. * These positions are apparent places. For precession and/or * rotations to other sky systems, one processes only mean places. * So to get a mean place, one has to remove the E-terms vector. * The ES suggests for the removal to use a decompositions of the * E-term vector along the unit circle to get the approximate * new vector, which has almost the correct angle and has almost * length 1. The advantage is that when we add the E-term vector * to this new vector, we obtain a new vector with the original * angle, but with a length unequal to 1, which makes it suitable * for closure tests. * However, the procedure can be made more rigorous: * For the subtraction we subtract the E-term vector from the * start vector and normalize it afterwards. Then we have an * exact new angle (opposed to the approximation in the ES). * The procedure to go from a vector in the mean place system to * a vector in the system of apparent places is a bit more * complicated: * Find a value for lambda so that the current vector is * adjusted in length so that adding the e-term vector gives a new * vector with length 1. This is by definition the new vector * with the right angle. * * @param xyz Cartesian position(s) converted from lonlat * @param eterm E-terms vector (as returned by getEterms()). If input *a* * is omitted (i.e. *a == null*), the e-terms for 1950 will be substituted. * @return **Apparent place**, */ public final static RealMatrix addEterms(final RealMatrix xyz, RealMatrix eterm) { RealMatrix xyzeterm = xyz.copy(); if (eterm == null) { eterm = FK4.getEterms(1950); } double x = xyz.getEntry(0, 0); double y = xyz.getEntry(1, 0); double z = xyz.getEntry(2, 0); // Normalize to get a vector of length 1. Our algorithm is based on that fact. double d = Math.sqrt(x * x + y * y + z * z); x /= d; y /= d; z /= d; // Find the lambda to stretch the vector double w = 2.0d * (eterm.getEntry(0, 0) * x + eterm.getEntry(0, 1) * y + eterm.getEntry(0, 2) * z); double p = eterm.getEntry(0, 0) * eterm.getEntry(0, 0) + eterm.getEntry(0, 1) * eterm.getEntry(0, 1) + eterm.getEntry(0, 2) * eterm.getEntry(0, 2) - 1.0d; double lambda1 = (-1 * w + Math.sqrt(w * w - 4.0d * p)) / 2.0d; //Vector a is small. We want only the positive lambda x = lambda1 * x + eterm.getEntry(0, 0); y = lambda1 * y + eterm.getEntry(0, 1); z = lambda1 * z + eterm.getEntry(0, 2); xyzeterm.setEntry(0, 0, x); xyzeterm.setEntry(1, 0, y); xyzeterm.setEntry(2, 0, z); return xyzeterm; }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * This method will return the element-wise product of matrixA * and matrixB.//ww w . jav a 2 s.c o m * In order to perform element-wise multiplication, the incoming * matrices must have the same dimensions. * @param matrixA * @param matrixB * @return RealMatrix which is the element-wise product of A and B. */ public static RealMatrix getElementWiseProduct(RealMatrix matrixA, RealMatrix matrixB) throws IllegalArgumentException { if (matrixA == null || matrixB == null || !areDimensionsEqual(matrixA, matrixB)) { throw new IllegalArgumentException("Both matrices must be non-null " + "and matrix dimensions must be equal" + " for element-wise multiplication."); } int numRows = matrixA.getRowDimension(); int numCols = matrixA.getColumnDimension(); RealMatrix product = new Array2DRowRealMatrix(numRows, numCols); double aVal, bVal; //loop through each row for (int r = 0; r < numRows; r++) { //multiply each element of A by same element of B for (int c = 0; c < numCols; c++) { aVal = matrixA.getEntry(r, c); bVal = matrixB.getEntry(r, c); product.setEntry(r, c, aVal * bVal); } } return product; }