本文整理汇总了Java中org.apache.commons.math3.linear.SingularValueDecomposition类的典型用法代码示例。如果您正苦于以下问题:Java SingularValueDecomposition类的具体用法?Java SingularValueDecomposition怎么用?Java SingularValueDecomposition使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SingularValueDecomposition类属于org.apache.commons.math3.linear包,在下文中一共展示了SingularValueDecomposition类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTruncatedSVD
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
/**
* truncated SVD as taken from http://stackoverflow.com/questions/19957076/best-way-to-compute-a-truncated-singular-value-decomposition-in-java
*/
static double[][] getTruncatedSVD(double[][] matrix, final int k) {
SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(matrix));
double[][] truncatedU = new double[svd.getU().getRowDimension()][k];
svd.getU().copySubMatrix(0, truncatedU.length - 1, 0, k - 1, truncatedU);
double[][] truncatedS = new double[k][k];
svd.getS().copySubMatrix(0, k - 1, 0, k - 1, truncatedS);
double[][] truncatedVT = new double[k][svd.getVT().getColumnDimension()];
svd.getVT().copySubMatrix(0, k - 1, 0, truncatedVT[0].length - 1, truncatedVT);
RealMatrix approximatedSvdMatrix = (MatrixUtils.createRealMatrix(truncatedU)).multiply(
MatrixUtils.createRealMatrix(truncatedS)).multiply(MatrixUtils.createRealMatrix(truncatedVT));
return approximatedSvdMatrix.getData();
}
示例2: computeScoreSVD
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
/**
* Singular Value Decomposition (SVD) based naive scoring.
*/
private double computeScoreSVD(@Nonnull final RealMatrix H, @Nonnull final RealMatrix G) {
SingularValueDecomposition svdH = new SingularValueDecomposition(H);
RealMatrix UT = svdH.getUT();
SingularValueDecomposition svdG = new SingularValueDecomposition(G);
RealMatrix Q = svdG.getU();
// find the largest singular value for the r principal components
RealMatrix UTQ = UT.getSubMatrix(0, r - 1, 0, window - 1).multiply(
Q.getSubMatrix(0, window - 1, 0, r - 1));
SingularValueDecomposition svdUTQ = new SingularValueDecomposition(UTQ);
double[] s = svdUTQ.getSingularValues();
return 1.d - s[0];
}
示例3: testPower1
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
@Test
public void testPower1() {
RealMatrix A = new Array2DRowRealMatrix(new double[][] {new double[] {1, 2, 3},
new double[] {4, 5, 6}});
double[] x = new double[3];
x[0] = Math.random();
x[1] = Math.random();
x[2] = Math.random();
double[] u = new double[2];
double[] v = new double[3];
double s = MatrixUtils.power1(A, x, 2, u, v);
SingularValueDecomposition svdA = new SingularValueDecomposition(A);
Assert.assertArrayEquals(svdA.getU().getColumn(0), u, 0.001d);
Assert.assertArrayEquals(svdA.getV().getColumn(0), v, 0.001d);
Assert.assertEquals(svdA.getSingularValues()[0], s, 0.001d);
}
示例4: findCenter
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
/**
* Find the center of the ellipsoid.
*
* @param a the algebraic from of the polynomial.
* @return a vector containing the center of the ellipsoid.
*/
private RealVector findCenter(RealMatrix a) {
RealMatrix subA = a.getSubMatrix(0, 2, 0, 2);
for (int q = 0; q < subA.getRowDimension(); q++) {
for (int s = 0; s < subA.getColumnDimension(); s++) {
subA.multiplyEntry(q, s, -1.0);
}
}
RealVector subV = a.getRowVector(3).getSubVector(0, 3);
// inv (dtd)
DecompositionSolver solver = new SingularValueDecomposition(subA)
.getSolver();
RealMatrix subAi = solver.getInverse();
return subAi.operate(subV);
}
示例5: findCenter
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
/**
* Find the center of the ellipsoid.
*
* @param a
* the algebraic from of the polynomial.
* @return a vector containing the center of the ellipsoid.
*/
private RealVector findCenter(RealMatrix a)
{
RealMatrix subA = a.getSubMatrix(0, 2, 0, 2);
for (int q = 0; q < subA.getRowDimension(); q++)
{
for (int s = 0; s < subA.getColumnDimension(); s++)
{
subA.multiplyEntry(q, s, -1.0);
}
}
RealVector subV = a.getRowVector(3).getSubVector(0, 3);
// inv (dtd)
DecompositionSolver solver = new SingularValueDecomposition(subA)
.getSolver();
RealMatrix subAi = solver.getInverse();
return subAi.operate(subV);
}
示例6: pInvMon
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
/**
*
* @param aif
* low-triangular shifted matrix
* @return the aif's pseudo-inverse by SVD
*/
public static double[][] pInvMon(double[][] aif) {
RealMatrix trunUTrans, trunS, trunV;
SingularValueDecomposition svd = new SingularValueDecomposition(
new Array2DRowRealMatrix(aif, false));
int rank = svd.getRank();
/*
* double [] sinV = svd.getSingularValues(); double [] cag = new
* double[rank]; for (int i = 0; i < rank; i++) cag[i] = sinV[i];
*/
trunS = svd.getS().getSubMatrix(0, rank - 1, 0, rank - 1);
trunV = svd.getV().getSubMatrix(0, aif[0].length - 1, 0, rank - 1);
trunUTrans = svd.getUT()
.getSubMatrix(0, rank - 1, 0, aif[0].length - 1);
// IJ.showMessage(" " +interBad(cag) /
// interBad(svd.getSingularValues()));
return trunV.multiply(trunS).multiply(trunUTrans).getData();
/*
* return new SingularValueDecomposition(new Array2DRowRealMatrix(aif,
* false)).getSolver().getInverse().getData();
*/
}
示例7: getTruncatedVT
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
static double[][] getTruncatedVT(INDArray matrix, int k) {
double[][] data = getDoubles(matrix);
SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(data));
double[][] truncatedVT = new double[k][svd.getVT().getColumnDimension()];
svd.getVT().copySubMatrix(0, k - 1, 0, truncatedVT[0].length - 1, truncatedVT);
return truncatedVT;
}
示例8: getTruncatedUT
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
static double[][] getTruncatedUT(INDArray matrix, int k) {
double[][] data = getDoubles(matrix);
SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(data));
double[][] truncatedU = new double[svd.getU().getRowDimension()][k];
svd.getU().copySubMatrix(0, truncatedU.length - 1, 0, k - 1, truncatedU);
return truncatedU;
}
示例9: printSVD
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
private void printSVD(SingularValueDecomposition svd) {
RealMatrix U = svd.getU();
RealMatrix S = svd.getS();
RealMatrix V = svd.getV();
System.out.println("------ SVD ---------------");
System.out.println("U = " + Matrix.toString(U.getData()));
System.out.println("S = " + Matrix.toString(S.getData()));
System.out.println("V = " + Matrix.toString(V.getData()));
System.out.println("--------------------------");
}
示例10: fit
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
@Override
public void fit(List<double[]> X, List<double[]> Y) { // fits n-dimensional data sets with affine model
if (X.size() != Y.size())
throw new IllegalArgumentException("point sequences X, Y must have same length");
this.m = X.size();
this.n = X.get(0).length;
RealMatrix M = MatrixUtils.createRealMatrix(2 * m, 2 * (n + 1));
RealVector b = new ArrayRealVector(2 * m);
// mount matrix M:
int row = 0;
for (double[] x : X) {
for (int j = 0; j < n; j++) {
M.setEntry(row, j, x[j]);
M.setEntry(row, n, 1);
row++;
}
for (int j = 0; j < n; j++) {
M.setEntry(row, j + n + 1, x[j]);
M.setEntry(row, 2 * n + 1, 1);
row++;
}
}
// mount vector b
row = 0;
for (double[] y : Y) {
for (int j = 0; j < n; j++) {
b.setEntry(row, y[j]);
row++;
}
}
SingularValueDecomposition svd = new SingularValueDecomposition(M);
DecompositionSolver solver = svd.getSolver();
RealVector a = solver.solve(b);
A = makeTransformationMatrix(a);
}
示例11: ProjectiveMapping
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
/**
* Constructor for more than 4 point pairs, finds a least-squares solution
* for the homography parameters.
* NOTE: this is UNFINISHED code!
* @param P sequence of points (source)
* @param Q sequence of points (target)
* @param dummy unused (only to avoid duplicate signature)
*/
public ProjectiveMapping(Point2D[] P, Point2D[] Q, boolean dummy) {
final int n = P.length;
double[] ba = new double[2 * n];
double[][] Ma = new double[2 * n][];
for (int i = 0; i < n; i++) {
double x = P[i].getX();
double y = P[i].getY();
double u = Q[i].getX();
double v = Q[i].getY();
ba[2 * i + 0] = u;
ba[2 * i + 1] = v;
Ma[2 * i + 0] = new double[] { x, y, 1, 0, 0, 0, -u * x, -u * y };
Ma[2 * i + 1] = new double[] { 0, 0, 0, x, y, 1, -v * x, -v * y };
}
RealMatrix M = MatrixUtils.createRealMatrix(Ma);
RealVector b = MatrixUtils.createRealVector(ba);
DecompositionSolver solver = new SingularValueDecomposition(M).getSolver();
RealVector h = solver.solve(b);
a00 = h.getEntry(0);
a01 = h.getEntry(1);
a02 = h.getEntry(2);
a10 = h.getEntry(3);
a11 = h.getEntry(4);
a12 = h.getEntry(5);
a20 = h.getEntry(6);
a21 = h.getEntry(7);
a22 = 1;
}
示例12: XSVD
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
/**
* Constructor
* @param x the input matrix
*/
XSVD(RealMatrix x) {
final SingularValueDecomposition svd = new SingularValueDecomposition(x);
this.rank = svd.getRank();
this.u = toDataFrame(svd.getU());
this.v = toDataFrame(svd.getV());
this.s = toDataFrame(svd.getS());
this.singularValues = Array.of(svd.getSingularValues());
}
示例13: computeL
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
private double computeL(double mu) {
double LPenalty = lpenalty * mu;
SingularValueDecomposition svd = new SingularValueDecomposition(X.subtract(S));
double[] penalizedD = softThreshold(svd.getSingularValues(), LPenalty);
RealMatrix D_matrix = MatrixUtils.createRealDiagonalMatrix(penalizedD);
L = svd.getU().multiply(D_matrix).multiply(svd.getVT());
return sum(penalizedD) * LPenalty;
}
示例14: pdf
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
/**
* pdf(x, x_hat) = exp(-0.5 * (x-x_hat) * inv(Σ) * (x-x_hat)T) / ( 2π^0.5d * det(Σ)^0.5)
*
* @return value of probabilistic density function
* @link https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Density_function
*/
public static double pdf(@Nonnull final RealVector x, @Nonnull final RealVector x_hat,
@Nonnull final RealMatrix sigma) {
final int dim = x.getDimension();
Preconditions.checkArgument(x_hat.getDimension() == dim, "|x| != |x_hat|, |x|=" + dim
+ ", |x_hat|=" + x_hat.getDimension());
Preconditions.checkArgument(sigma.getRowDimension() == dim, "|x| != |sigma|, |x|=" + dim
+ ", |sigma|=" + sigma.getRowDimension());
Preconditions.checkArgument(sigma.isSquare(), "Sigma is not square matrix");
LUDecomposition LU = new LUDecomposition(sigma);
final double detSigma = LU.getDeterminant();
double denominator = Math.pow(2.d * Math.PI, 0.5d * dim) * Math.pow(detSigma, 0.5d);
if (denominator == 0.d) { // avoid divide by zero
return 0.d;
}
final RealMatrix invSigma;
DecompositionSolver solver = LU.getSolver();
if (solver.isNonSingular() == false) {
SingularValueDecomposition svd = new SingularValueDecomposition(sigma);
invSigma = svd.getSolver().getInverse(); // least square solution
} else {
invSigma = solver.getInverse();
}
//EigenDecomposition eigen = new EigenDecomposition(sigma);
//double detSigma = eigen.getDeterminant();
//RealMatrix invSigma = eigen.getSolver().getInverse();
RealVector diff = x.subtract(x_hat);
RealVector premultiplied = invSigma.preMultiply(diff);
double sum = premultiplied.dotProduct(diff);
double numerator = Math.exp(-0.5d * sum);
return numerator / denominator;
}
示例15: inverse
import org.apache.commons.math3.linear.SingularValueDecomposition; //导入依赖的package包/类
@Nonnull
public static RealMatrix inverse(@Nonnull final RealMatrix m, final boolean exact)
throws SingularMatrixException {
LUDecomposition LU = new LUDecomposition(m);
DecompositionSolver solver = LU.getSolver();
final RealMatrix inv;
if (exact || solver.isNonSingular()) {
inv = solver.getInverse();
} else {
SingularValueDecomposition SVD = new SingularValueDecomposition(m);
inv = SVD.getSolver().getInverse();
}
return inv;
}