List of usage examples for org.apache.commons.math3.linear RealMatrix transpose
RealMatrix transpose();
From source file:io.github.malapert.jwcs.coordsystem.Utility.java
/** * Creates a rotation matrix for a precession based on IAU 2000/2006 * expressions, see `IAU2006precangles`. * //from w w w . jav a 2 s . com * Reference: * ---------- * Capitaine N. et al.: IAU 2000 precession A and A 412, 567-586 (2003) * * Notes: * ------ * Note that we apply this precession only to equatorial * coordinates in the system of dynamical J2000 coordinates. * When converting from ICRS coordinates this means applying * a frame bias. * Therefore the angles differ from the precession * Fukushima-Williams angles (IAU 2006) * The precession matrix is: M = rotZ(-z).rotY(+theta).rotZ(-zeta) * * @param epoch1 Julian start epoch * @param epoch2 Julian epoch to precess to * @return Matrix to transform equatorial coordinates from epoch1 to * epoch2 as in XYZepoch2 = M * XYZepoch1 */ public final static RealMatrix IAU2006MatrixEpoch12Epoch2(float epoch1, float epoch2) { RealMatrix result; if (epoch1 == epoch2) { result = MatrixUtils.createRealIdentityMatrix(3); } else if (epoch1 == 2000.0) { double[] precessionAngles = IAU2006PrecAngles(epoch2); result = precessionMatrix(precessionAngles[0], precessionAngles[1], precessionAngles[2]); } else { // If both epochs are not J2000.0 double[] precessionAngles = IAU2006PrecAngles(epoch1); RealMatrix m1 = precessionMatrix(precessionAngles[0], precessionAngles[1], precessionAngles[2]); m1 = m1.transpose(); precessionAngles = IAU2006PrecAngles(epoch2); RealMatrix m2 = precessionMatrix(precessionAngles[0], precessionAngles[1], precessionAngles[2]); result = m1.multiply(m2); } return result; }
From source file:edu.cudenver.bios.matrix.GramSchmidtOrthonormalization.java
/** * Perform Gram Schmidt Orthonormalization on the specified * matrix. The matrix A (mxn) is decomposed into two matrices * Q (mxn), R (nxn) such that// w w w . j av a 2s.c o m * <ul> * <li>A = QR * <li>Q'Q = Identity * <li>R is upper triangular * </ul> * The resulting Q, R matrices can be retrieved with the getQ() * and getR() functions. * * @param matrix */ public GramSchmidtOrthonormalization(RealMatrix matrix) { if (matrix == null) throw new IllegalArgumentException("Null matrix"); // create the Q, R matrices int m = matrix.getRowDimension(); int n = matrix.getColumnDimension(); Q = MatrixUtils.createRealMatrix(m, n); R = MatrixUtils.createRealMatrix(n, n); // perform Gram Schmidt process using the following algorithm // let w<n> be the resulting orthonormal column vectors // let v<n> be the columns of the incoming matrix // w1 = (1/norm(v1))*v1 // ... // wj = 1/norm(vj - projectionVj-1Vj)*[vj - projectionVj-1Vj] // where projectionVj-1Vj = (w1 * vj) * w1 + (w2 * vj) * w2 + ... + (wj-1 * vj) * wj-1 // for (int i = 0; i < n; i++) { RealMatrix v = matrix.getColumnMatrix(i); for (int j = 0; j < i; j++) { RealMatrix Qj = Q.getColumnMatrix(j); double value = Qj.transpose().multiply(v).getEntry(0, 0); R.setEntry(j, i, value); v = v.subtract(Qj.scalarMultiply(value)); } double norm = v.getFrobeniusNorm(); R.setEntry(i, i, norm); Q.setColumnMatrix(i, v.scalarMultiply(1 / norm)); } }
From source file:edu.cudenver.bios.matrix.test.TestMatrixOrthonormalization.java
/** * Verify that the Q'Q = I for the Q matrix produced by the * orthonormalization/*from w ww .j av a 2 s . co m*/ */ public void testQQisIdentity() { RealMatrix Q = norm.getQ(); // verify that Q'Q = identity RealMatrix shouldBeIdentityMatrix = Q.transpose().multiply(Q); // make sure the matrix is sqaure if (!shouldBeIdentityMatrix.isSquare()) { fail(); } // make sure the diagonal elements are one (within tolerance), and off diagonals // are zero (within tolerance) for (int r = 0; r < shouldBeIdentityMatrix.getRowDimension(); r++) { for (int c = 0; c < shouldBeIdentityMatrix.getColumnDimension(); c++) { double shouldBeValue = (r == c) ? 1 : 0; if (Precision.compareTo(shouldBeIdentityMatrix.getEntry(r, c), shouldBeValue, TOLERANCE) != 0) fail(); } } assertTrue(true); }
From source file:edu.cmu.tetrad.search.EstimateRank.java
public double[] CanCor(double[][] A, double[][] B) { this.A = A;/* ww w. j av a 2s . c o m*/ this.B = B; RealMatrix Ua = new SingularValueDecomposition(new BlockRealMatrix(A)).getU(); RealMatrix UTa = Ua.transpose(); RealMatrix Ub = new SingularValueDecomposition(new BlockRealMatrix(B)).getU(); return new SingularValueDecomposition(UTa.multiply(Ub)).getSingularValues(); }
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 w ww . ja v a 2 s .c o 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:edu.ucdenver.bios.designcalculator.DesignCalculator.java
/** * Calculate the sum of squares error matrix (the E matrix) * /*from w ww .j a v a2s . c om*/ * @param params matrices input by the user * @return error sum of squares */ public RealMatrix getErrorSumOfSquares(RealMatrix U, RealMatrix sigmaError, double nuError) { return U.transpose().multiply(sigmaError.multiply(U)).scalarMultiply(nuError); }
From source file:com.itemanalysis.psychometrics.factoranalysis.FactorAnalysisTest.java
@Test public void eigenValues() { RealMatrix R = new Array2DRowRealMatrix(readHarman74Data()); int nFactors = 2; int nVariables = 24; double[] x = new double[nVariables]; for (int i = 0; i < nVariables; i++) { x[i] = .5;/*from ww w . j av a 2s . c om*/ } for (int i = 0; i < nVariables; i++) { R.setEntry(i, i, 1.0 - x[i]); } EigenDecomposition eigen = new EigenDecomposition(R); RealMatrix eigenVectors = eigen.getV().getSubMatrix(0, nVariables - 1, 0, nFactors - 1); double[] ev = new double[nFactors]; for (int i = 0; i < nFactors; i++) { ev[i] = Math.sqrt(eigen.getRealEigenvalue(i)); } DiagonalMatrix evMatrix = new DiagonalMatrix(ev);// USE Apache version // of Diagonal // matrix when // upgrade to // version 3.2 RealMatrix LAMBDA = eigenVectors.multiply(evMatrix); RealMatrix SIGMA = (LAMBDA.multiply(LAMBDA.transpose())); RealMatrix RESID = R.subtract(SIGMA); double sum = 0.0; double squared = 0.0; for (int i = 0; i < RESID.getRowDimension(); i++) { for (int j = 0; j < RESID.getColumnDimension(); j++) { if (i != j) { sum += Math.pow(RESID.getEntry(i, j), 2); } } } System.out.println(sum); // RealMatrix SIGMA = (LAMBDA.multiply(LAMBDA.transpose())); // // System.out.println("SIGMA"); // for(int i=0;i<SIGMA.getRowDimension();i++){ // for(int j=0;j<SIGMA.getColumnDimension();j++){ // System.out.print(SIGMA.getEntry(i, j) + " "); // } // System.out.println(); // } }
From source file:com.joptimizer.algebra.CholeskyRCFactorizationTest.java
public void testInvert1() throws Exception { log.debug("testInvert1"); double[][] QData = new double[][] { { 1, .12, .13, .14, .15 }, { .12, 2, .23, .24, .25 }, { .13, .23, 3, 0, 0 }, { .14, .24, 0, 4, 0 }, { .15, .25, 0, 0, 5 } }; RealMatrix Q = MatrixUtils.createRealMatrix(QData); CholeskyRCFactorization myc = new CholeskyRCFactorization(DoubleFactory2D.dense.make(QData)); myc.factorize();/*www . j a va 2s.c om*/ RealMatrix L = new Array2DRowRealMatrix(myc.getL().toArray()); RealMatrix LT = new Array2DRowRealMatrix(myc.getLT().toArray()); log.debug("L: " + ArrayUtils.toString(L.getData())); log.debug("LT: " + ArrayUtils.toString(LT.getData())); log.debug("L.LT: " + ArrayUtils.toString(L.multiply(LT).getData())); log.debug("LT.L: " + ArrayUtils.toString(LT.multiply(L).getData())); // check Q = L.LT double norm = L.multiply(LT).subtract(Q).getNorm(); log.debug("norm: " + norm); assertTrue(norm < 1.E-15); RealMatrix LInv = new SingularValueDecomposition(L).getSolver().getInverse(); log.debug("LInv: " + ArrayUtils.toString(LInv.getData())); RealMatrix LInvT = LInv.transpose(); log.debug("LInvT: " + ArrayUtils.toString(LInvT.getData())); RealMatrix LTInv = new SingularValueDecomposition(LT).getSolver().getInverse(); log.debug("LTInv: " + ArrayUtils.toString(LTInv.getData())); RealMatrix LTInvT = LTInv.transpose(); log.debug("LTInvT: " + ArrayUtils.toString(LTInvT.getData())); log.debug("LInv.LInvT: " + ArrayUtils.toString(LInv.multiply(LInvT).getData())); log.debug("LTInv.LTInvT: " + ArrayUtils.toString(LTInv.multiply(LTInvT).getData())); RealMatrix Id = MatrixUtils.createRealIdentityMatrix(Q.getRowDimension()); //check Q.(LTInv * LInv) = 1 norm = Q.multiply(LTInv.multiply(LInv)).subtract(Id).getNorm(); log.debug("norm: " + norm); assertTrue(norm < 5.E-15); // check Q.QInv = 1 RealMatrix QInv = MatrixUtils.createRealMatrix(myc.getInverse().toArray()); norm = Q.multiply(QInv).subtract(Id).getNorm(); log.debug("norm: " + norm); assertTrue(norm < 1.E-15); }
From source file:com.joptimizer.algebra.CholeskyRCTFactorizationTest.java
public void testInvert1() throws Exception { log.debug("testInvert1"); double[][] QData = new double[][] { { 1, .12, .13, .14, .15 }, { .12, 2, .23, .24, .25 }, { .13, .23, 3, 0, 0 }, { .14, .24, 0, 4, 0 }, { .15, .25, 0, 0, 5 } }; RealMatrix Q = MatrixUtils.createRealMatrix(QData); CholeskyRCTFactorization myc = new CholeskyRCTFactorization(DoubleFactory2D.dense.make(QData)); myc.factorize();//from w ww .ja v a2s . co m RealMatrix L = new Array2DRowRealMatrix(myc.getL().toArray()); RealMatrix LT = new Array2DRowRealMatrix(myc.getLT().toArray()); log.debug("L: " + ArrayUtils.toString(L.getData())); log.debug("LT: " + ArrayUtils.toString(LT.getData())); log.debug("L.LT: " + ArrayUtils.toString(L.multiply(LT).getData())); log.debug("LT.L: " + ArrayUtils.toString(LT.multiply(L).getData())); // check Q = L.LT double norm = L.multiply(LT).subtract(Q).getNorm(); log.debug("norm: " + norm); assertTrue(norm < 1.E-15); RealMatrix LInv = new SingularValueDecomposition(L).getSolver().getInverse(); log.debug("LInv: " + ArrayUtils.toString(LInv.getData())); RealMatrix LInvT = LInv.transpose(); log.debug("LInvT: " + ArrayUtils.toString(LInvT.getData())); RealMatrix LTInv = new SingularValueDecomposition(LT).getSolver().getInverse(); log.debug("LTInv: " + ArrayUtils.toString(LTInv.getData())); RealMatrix LTInvT = LTInv.transpose(); log.debug("LTInvT: " + ArrayUtils.toString(LTInvT.getData())); log.debug("LInv.LInvT: " + ArrayUtils.toString(LInv.multiply(LInvT).getData())); log.debug("LTInv.LTInvT: " + ArrayUtils.toString(LTInv.multiply(LTInvT).getData())); RealMatrix Id = MatrixUtils.createRealIdentityMatrix(Q.getRowDimension()); //check Q.(LTInv * LInv) = 1 norm = Q.multiply(LTInv.multiply(LInv)).subtract(Id).getNorm(); log.debug("norm: " + norm); assertTrue(norm < 5.E-15); // check Q.QInv = 1 RealMatrix QInv = MatrixUtils.createRealMatrix(myc.getInverse().toArray()); norm = Q.multiply(QInv).subtract(Id).getNorm(); log.debug("norm: " + norm); assertTrue(norm < 1.E-15); }
From source file:com.joptimizer.util.CholeskyFactorizationTest.java
public void testInvert1() throws Exception { log.debug("testInvert1"); double[][] QData = new double[][] { { 1, .12, .13, .14, .15 }, { .12, 2, .23, .24, .25 }, { .13, .23, 3, 0, 0 }, { .14, .24, 0, 4, 0 }, { .15, .25, 0, 0, 5 } }; RealMatrix Q = MatrixUtils.createRealMatrix(QData); CholeskyFactorization myc = new CholeskyFactorization(QData); RealMatrix L = new Array2DRowRealMatrix(myc.getL()); RealMatrix LT = new Array2DRowRealMatrix(myc.getLT()); log.debug("L: " + L); log.debug("LT: " + LT); log.debug("L.LT: " + L.multiply(LT)); log.debug("LT.L: " + LT.multiply(L)); // check Q = L.LT double norm = L.multiply(LT).subtract(Q).getNorm(); log.debug("norm: " + norm); assertTrue(norm < 1.E-15);// www. ja va 2s.co m RealMatrix LInv = new SingularValueDecomposition(L).getSolver().getInverse(); log.debug("LInv: " + ArrayUtils.toString(LInv.getData())); RealMatrix LInvT = LInv.transpose(); log.debug("LInvT: " + ArrayUtils.toString(LInvT.getData())); RealMatrix LTInv = new SingularValueDecomposition(LT).getSolver().getInverse(); log.debug("LTInv: " + ArrayUtils.toString(LTInv.getData())); RealMatrix LTInvT = LTInv.transpose(); log.debug("LTInvT: " + ArrayUtils.toString(LTInvT.getData())); log.debug("LInv.LInvT: " + ArrayUtils.toString(LInv.multiply(LInvT).getData())); log.debug("LTInv.LTInvT: " + ArrayUtils.toString(LTInv.multiply(LTInvT).getData())); RealMatrix Id = MatrixUtils.createRealIdentityMatrix(Q.getRowDimension()); //check Q.(LTInv * LInv) = 1 norm = Q.multiply(LTInv.multiply(LInv)).subtract(Id).getNorm(); log.debug("norm: " + norm); assertTrue(norm < 5.E-15); // check Q.QInv = 1 RealMatrix QInv = MatrixUtils.createRealMatrix(myc.getInverse()); norm = Q.multiply(QInv).subtract(Id).getNorm(); log.debug("norm: " + norm); assertTrue(norm < 1.E-15); //check eigenvalues double det1 = Utils.calculateDeterminant(QData, QData.length); double det2 = 1; List<Double> eigenvalues = myc.getEigenvalues(); for (double ev : eigenvalues) { det2 = det2 * ev; } log.debug("det1: " + det1); log.debug("det2: " + det2); assertEquals(det1, det2, 1.E-13); }