本文整理汇总了Java中org.apache.commons.math3.linear.DecompositionSolver类的典型用法代码示例。如果您正苦于以下问题:Java DecompositionSolver类的具体用法?Java DecompositionSolver怎么用?Java DecompositionSolver使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DecompositionSolver类属于org.apache.commons.math3.linear包,在下文中一共展示了DecompositionSolver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSolver
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的package包/类
/**
* @param data dense matrix represented in row-major form
* @return solver for the system Ax = b
*/
static Solver getSolver(double[][] data) {
if (data == null) {
return null;
}
RealMatrix M = new Array2DRowRealMatrix(data, false);
double infNorm = M.getNorm();
double singularityThreshold = infNorm * SINGULARITY_THRESHOLD_RATIO;
RRQRDecomposition decomposition = new RRQRDecomposition(M, singularityThreshold);
DecompositionSolver solver = decomposition.getSolver();
if (solver.isNonSingular()) {
return new Solver(solver);
}
// Otherwise try to report apparent rank
int apparentRank = decomposition.getRank(0.01); // Better value?
log.warn("{} x {} matrix is near-singular (threshold {}). Add more data or decrease the " +
"number of features, to <= about {}",
M.getRowDimension(),
M.getColumnDimension(),
singularityThreshold,
apparentRank);
throw new SingularMatrixSolverException(apparentRank, "Apparent rank: " + apparentRank);
}
示例2: findCenter
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的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);
}
示例3: findCenter
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的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);
}
示例4: computeSolve
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的package包/类
/**
* Function to solve a given system of equations.
*
* @param in1 matrix object 1
* @param in2 matrix object 2
* @return matrix block
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
private static MatrixBlock computeSolve(MatrixObject in1, MatrixObject in2)
throws DMLRuntimeException
{
Array2DRowRealMatrix matrixInput = DataConverter.convertToArray2DRowRealMatrix(in1);
Array2DRowRealMatrix vectorInput = DataConverter.convertToArray2DRowRealMatrix(in2);
/*LUDecompositionImpl ludecompose = new LUDecompositionImpl(matrixInput);
DecompositionSolver lusolver = ludecompose.getSolver();
RealMatrix solutionMatrix = lusolver.solve(vectorInput);*/
// Setup a solver based on QR Decomposition
QRDecomposition qrdecompose = new QRDecomposition(matrixInput);
DecompositionSolver solver = qrdecompose.getSolver();
// Invoke solve
RealMatrix solutionMatrix = solver.solve(vectorInput);
return DataConverter.convertToMatrixBlock(solutionMatrix.getData());
}
示例5: getCovariances
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的package包/类
/**
* Get the covariance matrix of the optimized parameters.
* <br/>
* Note that this operation involves the inversion of the
* <code>J<sup>T</sup>J</code> matrix, where {@code J} is the
* Jacobian matrix.
* The {@code threshold} parameter is a way for the caller to specify
* that the result of this computation should be considered meaningless,
* and thus trigger an exception.
*
* @param threshold Singularity threshold.
* @return the covariance matrix.
* @throws org.apache.commons.math3.linear.SingularMatrixException
* if the covariance matrix cannot be computed (singular problem).
*/
public double[][] getCovariances(double threshold) {
// Set up the jacobian.
updateJacobian();
// Compute transpose(J)J, without building intermediate matrices.
double[][] jTj = new double[cols][cols];
for (int i = 0; i < cols; ++i) {
for (int j = i; j < cols; ++j) {
double sum = 0;
for (int k = 0; k < rows; ++k) {
sum += weightedResidualJacobian[k][i] * weightedResidualJacobian[k][j];
}
jTj[i][j] = sum;
jTj[j][i] = sum;
}
}
// Compute the covariances matrix.
final DecompositionSolver solver
= new QRDecomposition(MatrixUtils.createRealMatrix(jTj), threshold).getSolver();
return solver.getInverse().getData();
}
示例6: solveLinearEquationSymmetric
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的package包/类
/**
* Find a solution of the linear equation A x = b where
* <ul>
* <li>A is an symmetric n x n - matrix given as double[n][n]</li>
* <li>b is an n - vector given as double[n],</li>
* <li>x is an n - vector given as double[n],</li>
* </ul>
*
* @param matrix The matrix A (left hand side of the linear equation).
* @param vector The vector b (right hand of the linear equation).
* @return A solution x to A x = b.
*/
public static double[] solveLinearEquationSymmetric(double[][] matrix, double[] vector) {
if(isSolverUseApacheCommonsMath) {
DecompositionSolver solver = new LUDecomposition(new Array2DRowRealMatrix(matrix)).getSolver();
return solver.solve(new Array2DRowRealMatrix(vector)).getColumn(0);
}
else {
return org.jblas.Solve.solveSymmetric(new org.jblas.DoubleMatrix(matrix), new org.jblas.DoubleMatrix(vector)).data;
/* To use the linear algebra package colt from cern.
cern.colt.matrix.linalg.Algebra linearAlgebra = new cern.colt.matrix.linalg.Algebra();
double[] x = linearAlgebra.solve(new DenseDoubleMatrix2D(A), linearAlgebra.transpose(new DenseDoubleMatrix2D(new double[][] { b }))).viewColumn(0).toArray();
return x;
*/
}
}
示例7: getSolver
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的package包/类
@Override
public Solver getSolver(RealMatrix M) {
if (M == null) {
return null;
}
RRQRDecomposition decomposition = new RRQRDecomposition(M, SINGULARITY_THRESHOLD);
DecompositionSolver solver = decomposition.getSolver();
if (solver.isNonSingular()) {
return new CommonsMathSolver(solver);
}
// Otherwise try to report apparent rank
int apparentRank = decomposition.getRank(0.01); // Better value?
log.warn("{} x {} matrix is near-singular (threshold {}). Add more data or decrease the value of model.features, " +
"to <= about {}",
M.getRowDimension(),
M.getColumnDimension(),
SINGULARITY_THRESHOLD,
apparentRank);
throw new SingularMatrixSolverException(apparentRank, "Apparent rank: " + apparentRank);
}
示例8: estimateCoefficients
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的package包/类
@Override
public SlopeCoefficients estimateCoefficients(final DerivationEquation eq)
throws EstimationException {
final double[][] sourceTriangleMatrix = eq.getCovarianceLowerTriangularMatrix();
// Copy matrix and enhance it to a full matrix as expected by CholeskyDecomposition
// FIXME: Avoid copy job to speed-up the solving process e.g. by extending the CholeskyDecomposition constructor
final int length = sourceTriangleMatrix.length;
final double[][] matrix = new double[length][];
for (int i = 0; i < length; i++) {
matrix[i] = new double[length];
final double[] s = sourceTriangleMatrix[i];
final double[] t = matrix[i];
for (int j = 0; j <= i; j++) {
t[j] = s[j];
}
for (int j = i + 1; j < length; j++) {
t[j] = sourceTriangleMatrix[j][i];
}
}
final RealMatrix coefficients =
new Array2DRowRealMatrix(matrix, false);
try {
final DecompositionSolver solver = new CholeskyDecomposition(coefficients).getSolver();
final RealVector constants = new ArrayRealVector(eq.getConstraints(), true);
final RealVector solution = solver.solve(constants);
return new DefaultSlopeCoefficients(solution.toArray());
} catch (final NonPositiveDefiniteMatrixException e) {
throw new EstimationException("Matrix inversion error due to data is linearly dependent", e);
}
}
示例9: fit
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的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);
}
示例10: ProjectiveMapping
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的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;
}
示例11: solve
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的package包/类
public static double[] solve(final double[][] A, double[] b) {
RealMatrix AA = MatrixUtils.createRealMatrix(A);
RealVector bb = MatrixUtils.createRealVector(b);
DecompositionSolver solver = new LUDecomposition(AA).getSolver();
double[] x = null;
try {
x = solver.solve(bb).toArray();
} catch (SingularMatrixException e) {}
return x;
}
示例12: solve
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的package包/类
/**
* @see AbstractSolver#solve(AbstractMatrix, AbstractVector)
*/
@Override
public AbstractVector solve(AbstractMatrix m, AbstractVector b) {
if (m instanceof ApacheMatrix && b instanceof ApacheVector) {
DecompositionSolver solver = new LUDecomposition(((ApacheMatrix) m).getMatrix()).getSolver();
RealVector bApache = ((ApacheVector) b).getVector();
RealVector solution = solver.solve(bApache);
return new ApacheVector(solution);
}
throw new UnsupportedOperationException();
}
示例13: pdf
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的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;
}
示例14: inverse
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的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;
}
示例15: solve
import org.apache.commons.math3.linear.DecompositionSolver; //导入依赖的package包/类
/**
* L = A x R
*
* @return a matrix A that minimizes A x R - L
*/
@Nonnull
public static RealMatrix solve(@Nonnull final RealMatrix L, @Nonnull final RealMatrix R,
final boolean exact) throws SingularMatrixException {
LUDecomposition LU = new LUDecomposition(R);
DecompositionSolver solver = LU.getSolver();
final RealMatrix A;
if (exact || solver.isNonSingular()) {
A = LU.getSolver().solve(L);
} else {
SingularValueDecomposition SVD = new SingularValueDecomposition(R);
A = SVD.getSolver().solve(L);
}
return A;
}