List of usage examples for org.apache.commons.math.linear SingularValueDecompositionImpl SingularValueDecompositionImpl
public SingularValueDecompositionImpl(final RealMatrix matrix) throws InvalidMatrixException
From source file:com.opengamma.analytics.math.linearalgebra.SVDecompositionCommons.java
/** * {@inheritDoc}//ww w . j a va 2s.c o m */ @Override public SVDecompositionResult evaluate(final DoubleMatrix2D x) { Validate.notNull(x); MatrixValidate.notNaNOrInfinite(x); final RealMatrix commonsMatrix = CommonsMathWrapper.wrap(x); final SingularValueDecomposition svd = new SingularValueDecompositionImpl(commonsMatrix); return new SVDecompositionCommonsResult(svd); }
From source file:com.opengamma.analytics.math.matrix.CommonsMatrixAlgebra.java
/** * {@inheritDoc}/* w w w . jav a 2 s. c o m*/ */ @Override public double getCondition(final Matrix<?> m) { Validate.notNull(m, "m"); if (m instanceof DoubleMatrix2D) { final RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix2D) m); final SingularValueDecomposition svd = new SingularValueDecompositionImpl(temp); return svd.getConditionNumber(); } throw new IllegalArgumentException( "Can only find condition number of DoubleMatrix2D; have " + m.getClass()); }
From source file:edu.cudenver.bios.matrixsvc.resource.MatrixRankResource.java
/** * Handle POST request to calculate the rank of a matrix * @param entity XMl formatted matrix list * @return XML representation of the matrix rank * @throws ResourceException/*from w ww . j a va 2 s. co m*/ */ @Post public Representation matrixRank(Representation entity) throws ResourceException { if (entity == null) { throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No Input- Calculation of Matrix Rank not possible"); } DomRepresentation rep = new DomRepresentation(entity); NamedRealMatrix reqMatrix = null; try { // parse the parameters from the entity body reqMatrix = MatrixParamParser.getMatrixRankParamsFromDomNode(rep.getDocument().getDocumentElement()); //perform rank operation and set value in our parameter object SingularValueDecompositionImpl impl = new SingularValueDecompositionImpl(reqMatrix); //create our response representation RankXmlRepresentation response = new RankXmlRepresentation(impl.getRank()); /* getResponse().setEntity(response); getResponse().setStatus(Status.SUCCESS_CREATED);*/ return response; } catch (IllegalArgumentException iae) { MatrixLogger.getInstance().error(iae.getMessage()); throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, iae.getMessage()); } catch (IOException ioe) { MatrixLogger.getInstance().error(ioe.getMessage()); throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, ioe.getMessage()); } }
From source file:dr.math.Procrustes.java
public Procrustes(RealMatrix X, RealMatrix Xstar, boolean allowTranslation, boolean allowDilation) { rowDimension = X.getRowDimension();//w w w . ja v a 2 s.com columnDimension = X.getColumnDimension(); if (Xstar.getRowDimension() != rowDimension) { throw new IllegalArgumentException("X and Xstar do not have the same number of rows"); } if (Xstar.getColumnDimension() != columnDimension) { throw new IllegalArgumentException("X and Xstar do not have the same number of columns"); } RealMatrix J = new Array2DRowRealMatrix(rowDimension, rowDimension); if (allowTranslation) { // J <- diag(n) - 1/n * matrix(1, n, n) // for n = 3, J = {{1, -2/3, -2/3}, {-2/3, 1, -2/3}, {-2/3, -2/3, 1}} for (int i = 0; i < rowDimension; i++) { J.setEntry(i, i, 1.0 - (1.0 / rowDimension)); for (int j = i + 1; j < rowDimension; j++) { J.setEntry(i, j, -1.0 / rowDimension); J.setEntry(j, i, -1.0 / rowDimension); } } } else { // J <- diag(n) for (int i = 0; i < rowDimension; i++) { J.setEntry(i, i, 1); } } // C <- t(Xstar) %*% J %*% X RealMatrix C = Xstar.transpose().multiply(J.multiply(X)); // svd.out <- svd(C) // R <- svd.out$v %*% t(svd.out$u) // NB: Apache math does a different SVD from R. TODO Should use Colt library SingularValueDecomposition SVD = new SingularValueDecompositionImpl(C); R = SVD.getV().multiply(SVD.getUT()); // s <- 1 double s = 1.0; // scale = 1 unless dilation is being used if (allowDilation) { // mat1 <- t(Xstar) %*% J %*% X %*% R RealMatrix mat1 = Xstar.transpose().multiply(J.multiply(X.multiply(R))); // mat2 <- t(X) %*% J %*% X RealMatrix mat2 = X.transpose().multiply(J.multiply(X)); // s.numer <- 0 // s.denom <- 0 double numer = 0.0; double denom = 0.0; // for (i in 1:m) { // s.numer <- s.numer + mat1[i, i] // s.denom <- s.denom + mat2[i, i] // } for (int i = 0; i < columnDimension; i++) { numer = numer + mat1.getEntry(i, i); denom = denom + mat2.getEntry(i, i); } // s <- s.numer/s.denom s = numer / denom; } this.s = s; // tt <- matrix(0, m, 1) RealMatrix tmpT = new Array2DRowRealMatrix(columnDimension, 1); // a translation vector of zero unless translation is being used if (allowTranslation) { // tt <- 1/n * t(Xstar - s * X %*% R) %*% matrix(1, n, 1) RealMatrix tmp = new Array2DRowRealMatrix(rowDimension, 1); for (int i = 0; i < rowDimension; i++) { tmp.setEntry(i, 0, 1); } tmpT = Xstar.subtract(X.multiply(R).scalarMultiply(s)).transpose().scalarMultiply(1.0 / rowDimension) .multiply(tmp); } T = tmpT; }
From source file:com.opengamma.analytics.math.matrix.CommonsMatrixAlgebra.java
/** * {@inheritDoc}/* w w w . j a v a2 s . c om*/ */ @Override public DoubleMatrix2D getInverse(final Matrix<?> m) { Validate.notNull(m, "matrix was null"); if (m instanceof DoubleMatrix2D) { final RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix2D) m); final SingularValueDecomposition sv = new SingularValueDecompositionImpl(temp); final RealMatrix inv = sv.getSolver().getInverse(); return CommonsMathWrapper.unwrap(inv); } throw new IllegalArgumentException("Can only find inverse of DoubleMatrix2D; have " + m.getClass()); }
From source file:geogebra.common.kernel.statistics.AlgoFitImplicit.java
@Override public final void compute() { int order = (int) orderGeo.evaluateDouble(); datasize = pointlist.size(); // rows in M and Y if (!pointlist.isDefined() || datasize < order * (order + 3) / 2) { fitfunction.setUndefined();//from w w w. ja va2 s . c om return; } if (!pointlist.getElementType().equals(GeoClass.POINT)) { fitfunction.setUndefined(); return; } try { // Get functions, x and y from lists if (!makeMatrixes()) { fitfunction.setUndefined(); return; } // App.debug("datasize = " + datasize); // App.debug("order = " + order); // App.debug("M cols = "+M.getColumnDimension()); // App.debug("M rows = "+M.getRowDimension()); // App.debug("M = "+M.toString()); SingularValueDecomposition svd = new SingularValueDecompositionImpl(M); V = svd.getV(); // App.debug("V = "+V.toString()); // App.debug("size of M = "+M.getColumnDimension()+" "+M.getRowDimension()); // App.debug("size of V = "+V.getColumnDimension()+" "+V.getRowDimension()); makeFunction(); } catch (Throwable t) { fitfunction.setUndefined(); t.printStackTrace(); } }
From source file:com.opengamma.analytics.math.matrix.CommonsMatrixAlgebra.java
/** * {@inheritDoc}//w w w .j a va2s .c o m */ @Override public double getNorm2(final Matrix<?> m) { Validate.notNull(m, "m"); if (m instanceof DoubleMatrix1D) { final RealVector temp = CommonsMathWrapper.wrap((DoubleMatrix1D) m); return temp.getNorm(); } else if (m instanceof DoubleMatrix2D) { final RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix2D) m); final SingularValueDecomposition svd = new SingularValueDecompositionImpl(temp); return svd.getNorm(); } throw new IllegalArgumentException("Can only find norm2 of DoubleMatrix2D; have " + m.getClass()); }
From source file:org.mitre.math.linear.RealMatrixUtils.java
/** * Two norm returns the <a href="http://mathworld.wolfram.com/L2-Norm.html"> L2-Norm </a> * @return maximum singular value./* w ww. jav a 2s . c o m*/ */ public double norm2(RealMatrix matrix) { return (new SingularValueDecompositionImpl(matrix).getNorm()); }
From source file:playground.mmoyo.taste_variations.MyLeastSquareSolutionCalculator.java
private double[] getSVDsolution(final RealMatrix matrixA, final double[] arrayB) { SingularValueDecomposition svd = new SingularValueDecompositionImpl(matrixA); DecompositionSolver svdSolver = svd.getSolver(); double[] arrayX = svdSolver.solve(arrayB); return arrayX; }
From source file:trainableSegmentation.utils.PrincipalComponentAnalysis.java
public static ImagePlus getPrincipalComponents(final ImagePlus inputImage, final int patchSize, final int step) { final int maxX = (inputImage.getWidth() - patchSize); final int maxY = (inputImage.getHeight() - patchSize); final int matrixHeight = patchSize * patchSize; final int matrixWidth = (maxX / step + 1) * (maxY / step + 1) * inputImage.getImageStackSize(); final double[][] matrix = new double[matrixWidth][matrixHeight]; int n = 0;//w ww .ja v a 2s . co m for (int i = 1; i <= inputImage.getImageStack().getSize(); i++) { final ImageProcessor ip = inputImage.getImageStack().getProcessor(i).convertToFloat(); for (int j = 0; j < maxX; j += step) for (int k = 0; k < maxY; k += step) { final Roi roi = new Roi(j, k, patchSize, patchSize); ip.setRoi(roi); final ImageProcessor patch = ip.crop(); float[] pixels = (float[]) patch.getPixels(); for (int l = 0; l < matrixHeight; l++) matrix[n][l] = pixels[l]; n++; } } final Array2DRowRealMatrix m = new Array2DRowRealMatrix(matrix); // Calculate SVD and get V final long start = System.currentTimeMillis(); final SingularValueDecompositionImpl svd = new SingularValueDecompositionImpl(m); final RealMatrix v = svd.getV(); final long end = System.currentTimeMillis(); IJ.log("SVD took: " + (end - start) + "ms"); final ImageStack result = new ImageStack(patchSize, patchSize); for (int i = 0; i < v.getRowDimension(); i++) { final double[] column = new double[v.getColumnDimension()]; for (int j = 0; j < v.getColumnDimension(); j++) column[j] = v.getEntry(j, i); result.addSlice("PCA " + i, new FloatProcessor(patchSize, patchSize, column)); } return new ImagePlus("PCA", result); }