List of usage examples for org.apache.commons.math3.linear QRDecomposition QRDecomposition
public QRDecomposition(RealMatrix matrix, double threshold)
From source file:io.yields.math.concepts.operator.Smoothness.java
private RealMatrix computeDistance(List<Tuple> normalizedData) { RealMatrix a = new Array2DRowRealMatrix(normalizedData.size(), order + 1); RealMatrix b = new Array2DRowRealMatrix(normalizedData.size(), 1); int row = 0;/* ww w . j a v a2s. c o m*/ for (Tuple tuple : normalizedData) { final int currentRow = row; IntStream.rangeClosed(0, order) .forEach(power -> a.setEntry(currentRow, power, Math.pow(tuple.getX(), power))); b.setEntry(currentRow, 0, tuple.getY()); row++; } DecompositionSolver solver = new QRDecomposition(a, EPS).getSolver(); if (solver.isNonSingular() && !isConstant(b)) { RealMatrix solution = solver.solve(b); return a.multiply(solution).subtract(b); } else { return new Array2DRowRealMatrix(normalizedData.size(), 1); } }
From source file:org.orekit.utils.AngularCoordinates.java
/** Find a vector from two known cross products. * <p>//from w w w . j a v a2s .co m * We want to find such that: v? = c? and v = c * </p> * <p> * The first equation ( v? = c?) will always be fulfilled exactly, * and the second one will be fulfilled if possible. * </p> * @param v1 vector forming the first known cross product * @param c1 know vector for cross product v? * @param v2 vector forming the second known cross product * @param c2 know vector for cross product v * @param tolerance relative tolerance factor used to check singularities * @return vector such that: v? = c? and v = c * @exception MathIllegalArgumentException if vectors are inconsistent and * no solution can be found */ private static Vector3D inverseCrossProducts(final Vector3D v1, final Vector3D c1, final Vector3D v2, final Vector3D c2, final double tolerance) throws MathIllegalArgumentException { final double v12 = v1.getNormSq(); final double v1n = FastMath.sqrt(v12); final double v22 = v2.getNormSq(); final double v2n = FastMath.sqrt(v22); final double threshold = tolerance * FastMath.max(v1n, v2n); Vector3D omega; try { // create the over-determined linear system representing the two cross products final RealMatrix m = MatrixUtils.createRealMatrix(6, 3); m.setEntry(0, 1, v1.getZ()); m.setEntry(0, 2, -v1.getY()); m.setEntry(1, 0, -v1.getZ()); m.setEntry(1, 2, v1.getX()); m.setEntry(2, 0, v1.getY()); m.setEntry(2, 1, -v1.getX()); m.setEntry(3, 1, v2.getZ()); m.setEntry(3, 2, -v2.getY()); m.setEntry(4, 0, -v2.getZ()); m.setEntry(4, 2, v2.getX()); m.setEntry(5, 0, v2.getY()); m.setEntry(5, 1, -v2.getX()); final RealVector rhs = MatrixUtils.createRealVector( new double[] { c1.getX(), c1.getY(), c1.getZ(), c2.getX(), c2.getY(), c2.getZ() }); // find the best solution we can final DecompositionSolver solver = new QRDecomposition(m, threshold).getSolver(); final RealVector v = solver.solve(rhs); omega = new Vector3D(v.getEntry(0), v.getEntry(1), v.getEntry(2)); } catch (SingularMatrixException sme) { // handle some special cases for which we can compute a solution final double c12 = c1.getNormSq(); final double c1n = FastMath.sqrt(c12); final double c22 = c2.getNormSq(); final double c2n = FastMath.sqrt(c22); if (c1n <= threshold && c2n <= threshold) { // simple special case, velocities are cancelled return Vector3D.ZERO; } else if (v1n <= threshold && c1n >= threshold) { // this is inconsistent, if v? is zero, c? must be 0 too throw new NumberIsTooLargeException(c1n, 0, true); } else if (v2n <= threshold && c2n >= threshold) { // this is inconsistent, if v is zero, c must be 0 too throw new NumberIsTooLargeException(c2n, 0, true); } else if (Vector3D.crossProduct(v1, v2).getNorm() <= threshold && v12 > threshold) { // simple special case, v is redundant with v?, we just ignore it // use the simplest : orthogonal to both v? and c? omega = new Vector3D(1.0 / v12, Vector3D.crossProduct(v1, c1)); } else { throw sme; } } // check results final double d1 = Vector3D.distance(Vector3D.crossProduct(omega, v1), c1); if (d1 > threshold) { throw new NumberIsTooLargeException(d1, 0, true); } final double d2 = Vector3D.distance(Vector3D.crossProduct(omega, v2), c2); if (d2 > threshold) { throw new NumberIsTooLargeException(d2, 0, true); } return omega; }