List of usage examples for org.apache.commons.math.linear SingularValueDecomposition getUT
RealMatrix getUT();
From source file:dr.math.Procrustes.java
public Procrustes(RealMatrix X, RealMatrix Xstar, boolean allowTranslation, boolean allowDilation) { rowDimension = X.getRowDimension();// w w w . j a va 2 s . c o m 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.linearalgebra.SVDecompositionCommonsResult.java
/** * @param svd The result of the SV decomposition, not null *//*from w w w . ja v a 2 s. co m*/ public SVDecompositionCommonsResult(final SingularValueDecomposition svd) { Validate.notNull(svd); _condition = svd.getConditionNumber(); _norm = svd.getNorm(); _rank = svd.getRank(); _s = CommonsMathWrapper.unwrap(svd.getS()); _singularValues = svd.getSingularValues(); _u = CommonsMathWrapper.unwrap(svd.getU()); _uTranspose = CommonsMathWrapper.unwrap(svd.getUT()); _v = CommonsMathWrapper.unwrap(svd.getV()); _vTranspose = CommonsMathWrapper.unwrap(svd.getVT()); _solver = svd.getSolver(); }