List of usage examples for org.apache.commons.math3.linear DecompositionSolver isNonSingular
boolean isNonSingular();
From source file:hivemall.utils.math.StatsUtils.java
/** * pdf(x, x_hat) = exp(-0.5 * (x-x_hat) * inv() * (x-x_hat)T) / ( 2^0.5d * det()^0.5) * /*w w w . j a va 2s . c om*/ * @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; }
From source file:hivemall.utils.math.MatrixUtils.java
/** * L = A x R// w ww . ja v a 2 s.co m * * @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; }
From source file:hivemall.utils.math.MatrixUtils.java
@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();/* w w w. jav a 2s . com*/ } else { SingularValueDecomposition SVD = new SingularValueDecomposition(m); inv = SVD.getSolver().getInverse(); } return inv; }
From source file:edu.tum.cs.vis.model.util.algorithm.ACCUM.java
/** * Calculate curvature for vertices of triangle * //from w w w .j av a 2 s . c o m * @param curvatures * vertex curvature mapping * @param tri * triangle to calculate curvature for */ static void calculateCurvatureForTriangle(HashMap<Vertex, Curvature> curvatures, Triangle tri) { // Edges Vector3f e[] = tri.getEdges(); // N-T-B coordinate system per face Vector3f t = new Vector3f(e[0]); t.normalize(); Vector3f n = new Vector3f(); n.cross(e[0], e[1]); Vector3f b = new Vector3f(); b.cross(n, t); b.normalize(); // Estimate curvature based on variation of normals // along edges float m[] = { 0, 0, 0 }; double w[][] = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }; for (int j = 0; j < 3; j++) { float u = e[j].dot(t); float v = e[j].dot(b); w[0][0] += u * u; w[0][1] += u * v; // w[1][1] += v*v + u*u; // w[1][2] += u*v; w[2][2] += v * v; Vector3f dn = new Vector3f(tri.getPosition()[(j + 2) % 3].getNormalVector()); dn.sub(tri.getPosition()[(j + 1) % 3].getNormalVector()); float dnu = dn.dot(t); float dnv = dn.dot(b); m[0] += dnu * u; m[1] += dnu * v + dnv * u; m[2] += dnv * v; } w[1][1] = w[0][0] + w[2][2]; w[1][2] = w[0][1]; RealMatrix coefficients = new Array2DRowRealMatrix(w, false); DecompositionSolver solver = new LUDecomposition(coefficients).getSolver(); if (!solver.isNonSingular()) { return; } RealVector constants = new ArrayRealVector(new double[] { m[0], m[1], m[2] }, false); RealVector solution = solver.solve(constants); m[0] = (float) solution.getEntry(0); m[1] = (float) solution.getEntry(1); m[2] = (float) solution.getEntry(2); // Push it back out to the vertices for (int j = 0; j < 3; j++) { Vertex vj = tri.getPosition()[j]; float c1, c12, c2; float ret[] = proj_curv(t, b, m[0], m[1], m[2], curvatures.get(vj).getPrincipleDirectionMax(), curvatures.get(vj).getPrincipleDirectionMin()); c1 = ret[0]; c12 = ret[1]; c2 = ret[2]; Curvature c = curvatures.get(vj); double wt; if (j == 0) wt = tri.getCornerarea().x / vj.getPointarea(); else if (j == 1) wt = tri.getCornerarea().y / vj.getPointarea(); else wt = tri.getCornerarea().z / vj.getPointarea(); synchronized (c) { c.setCurvatureMax((float) (c.getCurvatureMax() + wt * c1)); c.setCurvatureMinMax((float) (c.getCurvatureMinMax() + wt * c12)); c.setCurvatureMin((float) (c.getCurvatureMin() + wt * c2)); } } }
From source file:com.anhth12.lambda.common.math.LinearSystemSolver.java
public boolean isNonSingular(RealMatrix M) { QRDecomposition decomposition = new RRQRDecomposition(M, SINGULARITY_THRESHOLD); DecompositionSolver solver = decomposition.getSolver(); return solver.isNonSingular(); }
From source file:com.cloudera.oryx.common.math.CommonsMathLinearSystemSolver.java
@Override public boolean isNonSingular(RealMatrix M) { QRDecomposition decomposition = new RRQRDecomposition(M, SINGULARITY_THRESHOLD); DecompositionSolver solver = decomposition.getSolver(); return solver.isNonSingular(); }
From source file:com.cloudera.oryx.common.math.LinearSystemSolver.java
public boolean isNonSingular(RealMatrix M) { QRDecomposition decomposition = new RRQRDecomposition(M, SINGULARITY_THRESHOLD); DecompositionSolver solver = decomposition.getSolver(); return solver.isNonSingular(); }
From source file:com.anhth12.lambda.common.math.LinearSystemSolver.java
public Solver getSolver(RealMatrix M) { if (M == null) { return null; }// www. ja va 2 s. c o m RRQRDecomposition decomposition = new RRQRDecomposition(M, SINGULARITY_THRESHOLD); DecompositionSolver solver = decomposition.getSolver(); if (solver.isNonSingular()) { return new Solver(solver); } int apparentRank = decomposition.getRank(0.01); log.warn( "{} x {} matrix is near-singular (threshold {}). Add more data or decrease the " + "value of als.hyperparams.features, to <= about {}", M.getRowDimension(), M.getColumnDimension(), SINGULARITY_THRESHOLD, apparentRank); throw new SingularMatrixSolverException(apparentRank, "Apparent rank: " + apparentRank); }
From source file:com.cloudera.oryx.common.math.CommonsMathLinearSystemSolver.java
@Override public Solver getSolver(RealMatrix M) { if (M == null) { return null; }//from www . j a v a 2 s. c o m 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); }
From source file:com.cloudera.oryx.common.math.LinearSystemSolver.java
public Solver getSolver(RealMatrix M) { if (M == null) { return null; }//from w w w . j a va 2 s .co m RRQRDecomposition decomposition = new RRQRDecomposition(M, SINGULARITY_THRESHOLD); 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 " + "value of als.hyperparams.features, to <= about {}", M.getRowDimension(), M.getColumnDimension(), SINGULARITY_THRESHOLD, apparentRank); throw new SingularMatrixSolverException(apparentRank, "Apparent rank: " + apparentRank); }