List of usage examples for org.apache.commons.math3.linear Array2DRowRealMatrix transpose
public RealMatrix transpose()
From source file:fi.smaa.jsmaa.model.MultivariateGaussianCriterionMeasurement.java
@Override public void deleteAlternative(Alternative alt) { final int n = alternatives.size(); final int index = alternatives.indexOf(alt); alternatives.remove(alt);//from w w w. j av a 2 s .c o m double select[][] = new double[n - 1][n]; for (int i = 0; i < n; ++i) { if (i < index) { select[i][i] = 1; } else if (i > index) { select[i - 1][i] = 1; } } Array2DRowRealMatrix m = new Array2DRowRealMatrix(select); final RealVector newMeanVector = m.operate(meanVector); final RealMatrix newCovarianceMatrix = covarianceMatrix.multiply(m.transpose()).preMultiply(m); fireEvents(setMeanVectorInternal(newMeanVector), setCovarianceMatrixInternal(newCovarianceMatrix)); }
From source file:fi.smaa.jsmaa.model.MultivariateGaussianCriterionMeasurement.java
@Override public void reorderAlternatives(List<Alternative> alts) { final int n = alternatives.size(); double permute[][] = new double[n][n]; for (int i = 0; i < n; ++i) { int j = alternatives.indexOf(alts.get(i)); permute[i][j] = 1;/* w w w . ja va2 s . c om*/ } Array2DRowRealMatrix m = new Array2DRowRealMatrix(permute); alternatives.clear(); alternatives.addAll(alts); final RealVector newMeanVector = m.operate(meanVector); final RealMatrix newCovarianceMatrix = covarianceMatrix.multiply(m.transpose()).preMultiply(m); fireEvents(setMeanVectorInternal(newMeanVector), setCovarianceMatrixInternal(newCovarianceMatrix)); }
From source file:MultivariateNormalDistribution.java
/** * Creates a multivariate normal distribution with the given mean vector and * covariance matrix./*from w ww . j a v a2s. c o m*/ * <br/> * The number of dimensions is equal to the length of the mean vector * and to the number of rows and columns of the covariance matrix. * It is frequently written as "p" in formulae. * * @param rng Random Number Generator. * @param means Vector of means. * @param covariances Covariance matrix. * @throws DimensionMismatchException if the arrays length are * inconsistent. * @throws SingularMatrixException if the eigenvalue decomposition cannot * be performed on the provided covariance matrix. * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is * negative. */ public MultivariateNormalDistribution(RandomGenerator rng, final double[] means, final double[][] covariances) throws SingularMatrixException, DimensionMismatchException, NonPositiveDefiniteMatrixException { super(rng, means.length); final int dim = means.length; if (covariances.length != dim) { throw new DimensionMismatchException(covariances.length, dim); } for (int i = 0; i < dim; i++) { if (dim != covariances[i].length) { throw new DimensionMismatchException(covariances[i].length, dim); } } this.means = MathArrays.copyOf(means); covarianceMatrix = new Array2DRowRealMatrix(covariances); // Covariance matrix eigen decomposition. final EigenDecomposition covMatDec = new EigenDecomposition(covarianceMatrix); // Compute and store the inverse. covarianceMatrixInverse = covMatDec.getSolver().getInverse(); // Compute and store the determinant. covarianceMatrixDeterminant = covMatDec.getDeterminant(); // Eigenvalues of the covariance matrix. final double[] covMatEigenvalues = covMatDec.getRealEigenvalues(); for (int i = 0; i < covMatEigenvalues.length; i++) { if (covMatEigenvalues[i] < 0) { throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0); } } // Matrix where each column is an eigenvector of the covariance matrix. final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim); for (int v = 0; v < dim; v++) { final double[] evec = covMatDec.getEigenvector(v).toArray(); covMatEigenvectors.setColumn(v, evec); } final RealMatrix tmpMatrix = covMatEigenvectors.transpose(); // Scale each eigenvector by the square root of its eigenvalue. for (int row = 0; row < dim; row++) { final double factor = FastMath.sqrt(covMatEigenvalues[row]); for (int col = 0; col < dim; col++) { tmpMatrix.multiplyEntry(row, col, factor); } } samplingMatrix = covMatEigenvectors.multiply(tmpMatrix); }
From source file:com.clust4j.utils.MatTests.java
@Test public void testTrans() { final double[][] mat = new double[][] { new double[] { -1.000, -1.000, -1.000 }, new double[] { 10.000, 10.000, 10.000 }, new double[] { 90.000, 90.000, 90.000 } }; Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(mat); assertTrue(MatUtils.equalsExactly(MatUtils.transpose(mat), matrix.transpose().getData())); }
From source file:org.apache.solr.client.solrj.io.eval.TransposeEvaluator.java
@Override public Object doWork(Object value) throws IOException { if (null == value) { return null; } else if (value instanceof Matrix) { Matrix matrix = (Matrix) value;/*ww w . j a va2 s.com*/ double[][] data = matrix.getData(); Array2DRowRealMatrix amatrix = new Array2DRowRealMatrix(data); Array2DRowRealMatrix tmatrix = (Array2DRowRealMatrix) amatrix.transpose(); return new Matrix(tmatrix.getData()); } else { throw new IOException("matrix parameter expected for transpose function"); } }
From source file:org.drugis.mtc.yadas.MultivariateGaussian.java
public double compute(double[] x, double[] mu, double[][] sigma) { int d = x.length; if (d != mu.length || d != sigma.length) { throw new IllegalArgumentException("All arguments need to be of equal length"); }/*w w w. j ava 2 s .c o m*/ Array2DRowRealMatrix sigmaM = new Array2DRowRealMatrix(sigma); Array2DRowRealMatrix xM = new Array2DRowRealMatrix(x); Array2DRowRealMatrix muM = new Array2DRowRealMatrix(mu); // Return the log of: // 1/sqrt(2pi^d * det(sigma)) * e^(-.5 (x - mu)' inv(sigma) (x - mu)) // Which is: // -log(sqrt(2pi^d * det(sigma))) + -.5 (x - mu)' inv(sigma) (x - mu) Array2DRowRealMatrix dM = xM.subtract(muM); LUDecomposition sigmaD = new LUDecomposition(sigmaM, Precision.SAFE_MIN); try { RealMatrix sigmaInv = sigmaD.getSolver().getInverse(); return -0.5 * (Math.log(2 * Math.PI) * d + Math.log(sigmaD.getDeterminant()) + dM.transpose().multiply(sigmaInv).multiply(dM).getEntry(0, 0)); } catch (RuntimeException e) { System.out.println(sigmaM); throw e; } /* RealMatrix sigmaInv = sigmaM.inverse(); return -0.5 * ( Math.log(2 * Math.PI) * d + Math.log(sigmaM.getDeterminant()) + dM.transpose().multiply(sigmaInv).multiply(dM).getEntry(0, 0)); */ }
From source file:org.interpss.opf.dc.impl.EqIneqMatrixBuilder.java
public Array2DRowRealMatrix formCeq() { int numOfBus = opfNet.getNoActiveBus(); int numOfGen = opfNet.getNoOfGen(); Array2DRowRealMatrix genBusAdjacent = new Array2DRowRealMatrix(numOfBus, numOfGen); int genCount = 0; for (Bus b : opfNet.getBusList()) { // set the elements in genBusAdjacent matrix to ONE if there is a // generator ; /*/* ww w . j a v a 2s .c o m*/ * in the case of multi-gen ,all are treated as a union; * not suitable for a case with multi-generators connected to one * bus; */ if (opfNet.isOpfGenBus(b)) { genBusAdjacent.setEntry(b.getSortNumber(), genCount, 1); genCount++; } } Array2DRowRealMatrix BrTranspose = (Array2DRowRealMatrix) this.getReducedBusAdmittance().transpose(); Array2DRowRealMatrix CeqTranspose = new Array2DRowRealMatrix(numOfBus, numOfBus + numOfGen - 1); CeqTranspose.setSubMatrix(genBusAdjacent.getData(), 0, 0); // Ceq'={genBusAdjacent,BrTranspose} CeqTranspose.setSubMatrix(BrTranspose.scalarMultiply(-1).getData(), 0, numOfGen); return (Array2DRowRealMatrix) CeqTranspose.transpose(); }
From source file:org.interpss.opf.dc.impl.EqIneqMatrixBuilder.java
public Array2DRowRealMatrix formCiq() { int numOfBranch = opfNet.getNoActiveBranch(); int numOfBus = opfNet.getNoActiveBus(); int numOfGen = opfNet.getNoOfGen(); double bij = DEFAULT_BIJ; int braIndex = 0; Array2DRowRealMatrix braAdmDiag = new Array2DRowRealMatrix(numOfBranch, numOfBranch); Array2DRowRealMatrix braBusAdjacent = new Array2DRowRealMatrix(numOfBranch, numOfBus - 1); int swingIndex = this.getSwingBusIndex(); for (Branch bra : opfNet.getBranchList()) { //if (bra.isAclfBranch()) { DclfOpfBranch aclfBra = (DclfOpfBranch) bra; // create branch admittance matrix bij = (aclfBra.getZ().getImaginary() > 0.00001) ? 1 / aclfBra.getZ().getImaginary() : DEFAULT_BIJ; // in case x=0; braAdmDiag.setEntry(braIndex, braIndex, bij); // create branch-bus connected or adjacent matrix; if (!((DclfOpfBus) bra.getFromBus()).isSwing()) { int fromBusIndex = bra.getFromBus().getSortNumber(); fromBusIndex = (fromBusIndex < swingIndex) ? fromBusIndex : fromBusIndex - 1; // insure nonswing bus; braBusAdjacent.setEntry(braIndex, fromBusIndex, 1); }/* w w w. j av a 2 s . c o m*/ if (!((DclfOpfBus) bra.getToBus()).isSwing()) { int toBusIndex = bra.getToBus().getSortNumber(); toBusIndex = (toBusIndex < swingIndex) ? toBusIndex : toBusIndex - 1; braBusAdjacent.setEntry(braIndex, toBusIndex, -1); } braIndex++; //} } int genIndex = 0; Array2DRowRealMatrix eyeGenP = new Array2DRowRealMatrix(numOfGen, numOfGen); for (Bus bus : opfNet.getBusList()) { if (opfNet.isOpfGenBus(bus)) { eyeGenP.setEntry(genIndex, genIndex, 1); genIndex++; } } Array2DRowRealMatrix DAr = braAdmDiag.multiply(braBusAdjacent); // Ciq'={Ot,-braAdmDiag*braBusAdjacent; // -Ot,braAdmDiag*braBusAdjacent; // eyeGenP,Op; // -eyeGenP,-Op} Array2DRowRealMatrix CiqTranspose = new Array2DRowRealMatrix(numOfBranch * 2 + numOfGen * 2, numOfGen + numOfBus - 1); CiqTranspose.setSubMatrix(DAr.scalarMultiply(-1).getData(), 0, numOfGen); CiqTranspose.setSubMatrix(DAr.getData(), numOfBranch, numOfGen); CiqTranspose.setSubMatrix(eyeGenP.getData(), 2 * numOfBranch, 0); CiqTranspose.setSubMatrix(eyeGenP.scalarMultiply(-1).getData(), 2 * numOfBranch + numOfGen, 0); // Ciq=CiqTranspose' return (Array2DRowRealMatrix) CiqTranspose.transpose(); }
From source file:org.pmad.gmm.MyMND.java
/** * Creates a multivariate normal distribution with the given mean vector and * covariance matrix.//from ww w . ja va 2 s. c o m * <br/> * The number of dimensions is equal to the length of the mean vector * and to the number of rows and columns of the covariance matrix. * It is frequently written as "p" in formulae. * * @param rng Random Number Generator. * @param means Vector of means. * @param covariances Covariance matrix. * @throws DimensionMismatchException if the arrays length are * inconsistent. * @throws SingularMatrixException if the eigenvalue decomposition cannot * be performed on the provided covariance matrix. * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is * negative. */ public MyMND(RandomGenerator rng, final double[] means, final double[][] covariances) throws SingularMatrixException, DimensionMismatchException, NonPositiveDefiniteMatrixException { super(rng, means.length); final int dim = means.length; if (covariances.length != dim) { throw new DimensionMismatchException(covariances.length, dim); } for (int i = 0; i < dim; i++) { if (dim != covariances[i].length) { throw new DimensionMismatchException(covariances[i].length, dim); } } this.means = MathArrays.copyOf(means); double msum = 0; for (int i = 0; i < covariances.length; i++) { for (int j = 0; j < covariances.length; j++) { msum += covariances[i][j]; } } msum /= covariances.length * covariances.length; // System.out.print("in"); MyEDC covMatDec = null; double a = -1; while (true) { try { covarianceMatrix = new Array2DRowRealMatrix(covariances); covMatDec = new MyEDC(covarianceMatrix); // Compute and store the inverse. covarianceMatrixInverse = covMatDec.getSolver().getInverse(); a *= -1; break; } catch (NoDataException e) { e.printStackTrace(); } catch (NullArgumentException e) { e.printStackTrace(); } catch (MathArithmeticException e) { e.printStackTrace(); } catch (SingularMatrixException e) { // System.out.print("S"); for (int i = 0; i < covariances.length; i++) { double add = covariances[i][i] == 0 ? msum : covariances[i][i]; covariances[i][i] += new Random().nextDouble() * add * 0.01; } } // catch (MaxCountExceededException e) { //// e.printStackTrace(); //// System.out.print("M"+msum); // for (int i = 0; i < covariances.length; i++) { // for (int j = i; j < covariances.length; j++) { // double add = covariances[i][j] == 0?msum:covariances[i][j]; // add = new Random().nextDouble()*add*0.1*a; // covariances[i][j] += add; // covariances[j][i] += add; // } // } //// break; // } } // Compute and store the determinant. covarianceMatrixDeterminant = covMatDec.getDeterminant(); // Eigenvalues of the covariance matrix. final double[] covMatEigenvalues = covMatDec.getRealEigenvalues(); for (int i = 0; i < covMatEigenvalues.length; i++) { if (covMatEigenvalues[i] < 0) { throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0); } } // Matrix where each column is an eigenvector of the covariance matrix. final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim); for (int v = 0; v < dim; v++) { final double[] evec = covMatDec.getEigenvector(v).toArray(); covMatEigenvectors.setColumn(v, evec); } final RealMatrix tmpMatrix = covMatEigenvectors.transpose(); // Scale each eigenvector by the square root of its eigenvalue. for (int row = 0; row < dim; row++) { final double factor = FastMath.sqrt(covMatEigenvalues[row]); for (int col = 0; col < dim; col++) { tmpMatrix.multiplyEntry(row, col, factor); } } samplingMatrix = covMatEigenvectors.multiply(tmpMatrix); }
From source file:outlineDescriptor.ODUtils.java
/** * Generates a 2D covariance matrix for a distribution function * * @param vX X component/*from w w w. ja v a 2s .c o m*/ * @param vY Y component * @param eV1 First eigenvalue * @param eV2 Second eigenvalue * * @return Resulting covariance matrix */ public static Array2DRowRealMatrix getDistributionCovMatrix(double vX, double vY, double eV1, double eV2) { double multiplier = 1 / (vX * vX + vY * vY); Array2DRowRealMatrix vectMat = new Array2DRowRealMatrix(2, 2); Array2DRowRealMatrix valMat = new Array2DRowRealMatrix(2, 2); valMat.setEntry(0, 0, eV1); valMat.setEntry(1, 1, eV2); vectMat.setEntry(0, 0, vX); vectMat.setEntry(0, 1, vY); vectMat.setEntry(1, 0, -vY); vectMat.setEntry(1, 1, vX); Array2DRowRealMatrix vE = vectMat.multiply(valMat); Array2DRowRealMatrix tempCov = vE.multiply((Array2DRowRealMatrix) vectMat.transpose()); return (Array2DRowRealMatrix) tempCov.scalarMultiply(multiplier); }