List of usage examples for org.apache.commons.math3.linear EigenDecomposition EigenDecomposition
public EigenDecomposition(final RealMatrix matrix) throws MathArithmeticException
From source file:lanchester.TestEigen.java
public static void main(String[] args) { mat = getMat(4);/* w w w .j av a 2 s . c o m*/ // fillMat(mat); eigen = new EigenDecomposition(mat); double det = eigen.getDeterminant(); double[] eVals = eigen.getRealEigenvalues(); if (eigen.hasComplexEigenvalues()) { System.out.println("Complex eigenvalues"); } Array2DRowRealMatrix eVectors = (Array2DRowRealMatrix) eigen.getV(); EigenDecomposition eigen2 = new EigenDecomposition(eVectors); double det2 = eigen2.getDeterminant(); System.out.println("Det 1 = " + det + " and Det 2 = " + det2); }
From source file:bigdataproject.PCA.java
public double[][] reduceDimensions() { BlockRealMatrix matrix = new BlockRealMatrix(dataSet); Covariance cov = new Covariance(matrix, false); RealMatrix covarianceMatrix = cov.getCovarianceMatrix(); EigenDecomposition dec = new EigenDecomposition(covarianceMatrix); RealVector principalEigenVector = dec.getEigenvector(0); RealVector secondEigenVector = dec.getEigenvector(1); BlockRealMatrix pca = new BlockRealMatrix(principalEigenVector.getDimension(), 2); pca.setColumnVector(0, principalEigenVector); pca.setColumnVector(1, secondEigenVector); BlockRealMatrix pcaTranspose = pca.transpose(); BlockRealMatrix columnVectorMatrix = matrix.transpose(); BlockRealMatrix matrix2D = pcaTranspose.multiply(columnVectorMatrix); return matrix2D.getData(); }
From source file:net.sf.dsp4j.octave_3_2_4.Eig.java
public static Complex[] eig(RealMatrix d) { EigenDecomposition eig = new EigenDecomposition(d); double[] realEigenvalues = eig.getRealEigenvalues(); double[] imagEigenvalues = eig.getImagEigenvalues(); final Complex[] result = new Complex[realEigenvalues.length]; for (int i = 0; i < realEigenvalues.length; i++) { result[i] = new Complex(realEigenvalues[i], imagEigenvalues[i]); }//from w w w . ja va 2 s . com return result; }
From source file:com.itemanalysis.psychometrics.factoranalysis.PrincipalComponentsMethod.java
public double estimateParameters() { 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)); }/*ww w . j av a2 s . c o m*/ DiagonalMatrix evMatrix = new DiagonalMatrix(ev);//USE Apache version of Diagonal matrix when upgrade to version 3.2 RealMatrix LOAD = eigenVectors.multiply(evMatrix); //rotate factor loadings if (rotationMethod != RotationMethod.NONE) { GPArotation gpa = new GPArotation(); RotationResults results = gpa.rotate(LOAD, rotationMethod); LOAD = results.getFactorLoadings(); } Sum[] colSums = new Sum[nFactors]; Sum[] colSumsSquares = new Sum[nFactors]; for (int j = 0; j < nFactors; j++) { colSums[j] = new Sum(); colSumsSquares[j] = new Sum(); } factorLoading = new double[nVariables][nFactors]; communality = new double[nVariables]; uniqueness = new double[nVariables]; for (int i = 0; i < nVariables; i++) { for (int j = 0; j < nFactors; j++) { factorLoading[i][j] = LOAD.getEntry(i, j); colSums[j].increment(factorLoading[i][j]); colSumsSquares[j].increment(Math.pow(factorLoading[i][j], 2)); communality[i] += Math.pow(factorLoading[i][j], 2); } } //check sign of factor double sign = 1.0; for (int i = 0; i < nVariables; i++) { for (int j = 0; j < nFactors; j++) { if (colSums[j].getResult() < 0) { sign = -1.0; } else { sign = 1.0; } factorLoading[i][j] = factorLoading[i][j] * sign; } uniqueness[i] = 1.0 - communality[i]; } double totSumOfSquares = 0.0; sumsOfSquares = new double[nFactors]; proportionOfExplainedVariance = new double[nFactors]; proportionOfVariance = new double[nFactors]; for (int j = 0; j < nFactors; j++) { sumsOfSquares[j] = colSumsSquares[j].getResult(); totSumOfSquares += sumsOfSquares[j]; } for (int j = 0; j < nFactors; j++) { proportionOfExplainedVariance[j] = sumsOfSquares[j] / totSumOfSquares; proportionOfVariance[j] = sumsOfSquares[j] / nVariables; } return 0.0; }
From source file:com.simiacryptus.mindseye.test.PCAUtil.java
/** * Pca features inv tensor [ ].// w w w . j av a 2s . c om * * @param covariance the covariance * @param components the components * @param featureDimensions the feature dimensions * @param power the power * @return the tensor [ ] */ public static Tensor[] pcaFeatures(final RealMatrix covariance, final int components, final int[] featureDimensions, final double power) { @Nonnull final EigenDecomposition decomposition = new EigenDecomposition(covariance); final int[] orderedVectors = IntStream.range(0, components).mapToObj(x -> x) .sorted(Comparator.comparing(x -> -decomposition.getRealEigenvalue(x))).mapToInt(x -> x).toArray(); return IntStream.range(0, orderedVectors.length).mapToObj(i -> { @Nonnull final Tensor src = new Tensor(decomposition.getEigenvector(orderedVectors[i]).toArray(), featureDimensions).copy(); return src.scale(1.0 / src.rms()).scale((Math.pow(decomposition.getRealEigenvalue(orderedVectors[i]) / decomposition.getRealEigenvalue(orderedVectors[0]), power))); }).toArray(i -> new Tensor[i]); }
From source file:com.opengamma.strata.math.impl.rootfinding.EigenvaluePolynomialRootFinder.java
@Override public Double[] getRoots(RealPolynomialFunction1D function) { ArgChecker.notNull(function, "function"); double[] coeffs = function.getCoefficients(); int l = coeffs.length - 1; double[][] hessianDeref = new double[l][l]; for (int i = 0; i < l; i++) { hessianDeref[0][i] = -coeffs[l - i - 1] / coeffs[l]; for (int j = 1; j < l; j++) { hessianDeref[j][i] = 0;/*from www . j a v a2s . com*/ if (i != l - 1) { hessianDeref[i + 1][i] = 1; } } } RealMatrix hessian = new Array2DRowRealMatrix(hessianDeref); double[] d = new EigenDecomposition(hessian).getRealEigenvalues(); Double[] result = new Double[d.length]; for (int i = 0; i < d.length; i++) { result[i] = d[i]; } return result; }
From source file:de.biomedical_imaging.traJ.features.Asymmetry3Feature.java
/** * @return Returns an double array with the following elements [0]=Asymmetry *///from www. ja va 2s.c o m @Override public double[] evaluate() { Array2DRowRealMatrix gyr = RadiusGyrationTensor2D.getRadiusOfGyrationTensor(t); EigenDecomposition eigdec = new EigenDecomposition(gyr); double e1 = eigdec.getRealEigenvalue(0); double e2 = eigdec.getRealEigenvalue(1); double asym = -1 * Math.log(1 - Math.pow(e1 - e2, 2) / (2 * Math.pow(e1 + e2, 2))); result = new double[] { asym }; return result; }
From source file:de.biomedical_imaging.traJ.features.Asymmetry2Feature.java
/** * @return Returns an double array with the following elements [0]=asymmetry *//*from w ww . j ava 2s . com*/ @Override public double[] evaluate() { Array2DRowRealMatrix gyr = RadiusGyrationTensor2D.getRadiusOfGyrationTensor(t); EigenDecomposition eigdec = new EigenDecomposition(gyr); double e1 = eigdec.getRealEigenvalue(0); double e2 = eigdec.getRealEigenvalue(1); double asym = e2 / e1; //-1*Math.log(1-Math.pow(e1-e2, 2)/(2*Math.pow(e1+e2, 2))); result = new double[] { asym }; return result; }
From source file:de.biomedical_imaging.traJ.features.AsymmetryFeature.java
/** * @return Returns an double array with the following elements [0]=Asymmetry *//*from ww w . j a v a 2 s.co m*/ @Override public double[] evaluate() { Array2DRowRealMatrix gyr = RadiusGyrationTensor2D.getRadiusOfGyrationTensor(t); EigenDecomposition eigdec = new EigenDecomposition(gyr); double e1 = eigdec.getRealEigenvalue(0); double e2 = eigdec.getRealEigenvalue(1); double asym = Math.pow(e1 * e1 - e2 * e2, 2) / (Math.pow(e1 * e1 + e2 * e2, 2)); //-1*Math.log(1-Math.pow(e1-e2, 2)/(2*Math.pow(e1+e2, 2))); result = new double[] { asym }; return result; }
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 w w w . j av a 2 s . c o m } 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(); // } }