Search in sources :

Example 6 with MathIllegalArgumentException

use of org.hipparchus.exception.MathIllegalArgumentException in project Orekit by CS-SI.

the class BatchLSEstimator method getPhysicalCovariances.

/**
 * Get the covariances matrix in space flight dynamics physical units.
 * <p>
 * This method retrieve the {@link
 * org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem.Evaluation#getCovariances(double)
 * covariances} from the [@link {@link #getOptimum() optimum} and applies the scaling factors
 * to it in order to convert it from raw normalized values back to physical values.
 * </p>
 * @param threshold threshold to identify matrix singularity
 * @return covariances matrix in space flight dynamics physical units
 * @exception OrekitException if the covariance matrix cannot be computed (singular problem).
 * @since 9.1
 */
public RealMatrix getPhysicalCovariances(final double threshold) throws OrekitException {
    final RealMatrix covariances;
    try {
        // get the normalized matrix
        covariances = optimum.getCovariances(threshold).copy();
    } catch (MathIllegalArgumentException miae) {
        // the problem is singular
        throw new OrekitException(miae);
    }
    // retrieve the scaling factors
    final double[] scale = new double[covariances.getRowDimension()];
    int index = 0;
    for (final ParameterDriver driver : getOrbitalParametersDrivers(true).getDrivers()) {
        scale[index++] = driver.getScale();
    }
    for (final ParameterDriver driver : getPropagatorParametersDrivers(true).getDrivers()) {
        scale[index++] = driver.getScale();
    }
    for (final ParameterDriver driver : getMeasurementsParametersDrivers(true).getDrivers()) {
        scale[index++] = driver.getScale();
    }
    // unnormalize the matrix, to retrieve physical covariances
    for (int i = 0; i < covariances.getRowDimension(); ++i) {
        for (int j = 0; j < covariances.getColumnDimension(); ++j) {
            covariances.setEntry(i, j, scale[i] * scale[j] * covariances.getEntry(i, j));
        }
    }
    return covariances;
}
Also used : RealMatrix(org.hipparchus.linear.RealMatrix) MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException) OrekitException(org.orekit.errors.OrekitException) ParameterDriver(org.orekit.utils.ParameterDriver)

Example 7 with MathIllegalArgumentException

use of org.hipparchus.exception.MathIllegalArgumentException in project Orekit by CS-SI.

the class FieldAngularCoordinates method inverseCrossProducts.

/**
 * Find a vector from two known cross products.
 * <p>
 * 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
 * @param <T> the type of the field elements
 * @return vector Ω such that: Ω ⨯ v₁ = c₁ and Ω ⨯ v₂ = c₂
 * @exception MathIllegalArgumentException if vectors are inconsistent and
 * no solution can be found
 */
private static <T extends RealFieldElement<T>> FieldVector3D<T> inverseCrossProducts(final FieldVector3D<T> v1, final FieldVector3D<T> c1, final FieldVector3D<T> v2, final FieldVector3D<T> c2, final double tolerance) throws MathIllegalArgumentException {
    final T v12 = v1.getNormSq();
    final T v1n = v12.sqrt();
    final T v22 = v2.getNormSq();
    final T v2n = v22.sqrt();
    final T threshold;
    if (v1n.getReal() >= v2n.getReal()) {
        threshold = v1n.multiply(tolerance);
    } else {
        threshold = v2n.multiply(tolerance);
    }
    FieldVector3D<T> omega = null;
    try {
        // create the over-determined linear system representing the two cross products
        final FieldMatrix<T> m = MatrixUtils.createFieldMatrix(v12.getField(), 6, 3);
        m.setEntry(0, 1, v1.getZ());
        m.setEntry(0, 2, v1.getY().negate());
        m.setEntry(1, 0, v1.getZ().negate());
        m.setEntry(1, 2, v1.getX());
        m.setEntry(2, 0, v1.getY());
        m.setEntry(2, 1, v1.getX().negate());
        m.setEntry(3, 1, v2.getZ());
        m.setEntry(3, 2, v2.getY().negate());
        m.setEntry(4, 0, v2.getZ().negate());
        m.setEntry(4, 2, v2.getX());
        m.setEntry(5, 0, v2.getY());
        m.setEntry(5, 1, v2.getX().negate());
        final T[] kk = MathArrays.buildArray(v2n.getField(), 6);
        kk[0] = c1.getX();
        kk[1] = c1.getY();
        kk[2] = c1.getZ();
        kk[3] = c2.getX();
        kk[4] = c2.getY();
        kk[5] = c2.getZ();
        final FieldVector<T> rhs = MatrixUtils.createFieldVector(kk);
        // find the best solution we can
        final FieldDecompositionSolver<T> solver = new FieldQRDecomposition<>(m).getSolver();
        final FieldVector<T> v = solver.solve(rhs);
        omega = new FieldVector3D<>(v.getEntry(0), v.getEntry(1), v.getEntry(2));
    } catch (MathIllegalArgumentException miae) {
        if (miae.getSpecifier() == LocalizedCoreFormats.SINGULAR_MATRIX) {
            // handle some special cases for which we can compute a solution
            final T c12 = c1.getNormSq();
            final T c1n = c12.sqrt();
            final T c22 = c2.getNormSq();
            final T c2n = c22.sqrt();
            if (c1n.getReal() <= threshold.getReal() && c2n.getReal() <= threshold.getReal()) {
                // simple special case, velocities are cancelled
                return new FieldVector3D<>(v12.getField().getZero(), v12.getField().getZero(), v12.getField().getZero());
            } else if (v1n.getReal() <= threshold.getReal() && c1n.getReal() >= threshold.getReal()) {
                // this is inconsistent, if v₁ is zero, c₁ must be 0 too
                throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, c1n.getReal(), 0, true);
            } else if (v2n.getReal() <= threshold.getReal() && c2n.getReal() >= threshold.getReal()) {
                // this is inconsistent, if v₂ is zero, c₂ must be 0 too
                throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, c2n.getReal(), 0, true);
            } else if (v1.crossProduct(v1).getNorm().getReal() <= threshold.getReal() && v12.getReal() > threshold.getReal()) {
                // simple special case, v₂ is redundant with v₁, we just ignore it
                // use the simplest Ω: orthogonal to both v₁ and c₁
                omega = new FieldVector3D<>(v12.reciprocal(), v1.crossProduct(c1));
            }
        } else {
            throw miae;
        }
    }
    // check results
    final T d1 = FieldVector3D.distance(omega.crossProduct(v1), c1);
    if (d1.getReal() > threshold.getReal()) {
        throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, 0, true);
    }
    final T d2 = FieldVector3D.distance(omega.crossProduct(v2), c2);
    if (d2.getReal() > threshold.getReal()) {
        throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, 0, true);
    }
    return omega;
}
Also used : MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException)

Example 8 with MathIllegalArgumentException

use of org.hipparchus.exception.MathIllegalArgumentException in project Orekit by CS-SI.

the class FieldAngularCoordinatesTest method checkInverse.

private <T extends RealFieldElement<T>> void checkInverse(FieldVector3D<T> omega, FieldVector3D<T> v1, FieldVector3D<T> c1, FieldVector3D<T> v2, FieldVector3D<T> c2) throws MathIllegalArgumentException {
    try {
        Method inverse;
        inverse = FieldAngularCoordinates.class.getDeclaredMethod("inverseCrossProducts", FieldVector3D.class, FieldVector3D.class, FieldVector3D.class, FieldVector3D.class, Double.TYPE);
        inverse.setAccessible(true);
        @SuppressWarnings("unchecked") FieldVector3D<T> rebuilt = (FieldVector3D<T>) inverse.invoke(null, v1, c1, v2, c2, 1.0e-9);
        Assert.assertEquals(0.0, FieldVector3D.distance(omega, rebuilt).getReal(), 5.0e-12 * omega.getNorm().getReal());
    } catch (NoSuchMethodException e) {
        Assert.fail(e.getLocalizedMessage());
    } catch (SecurityException e) {
        Assert.fail(e.getLocalizedMessage());
    } catch (IllegalAccessException e) {
        Assert.fail(e.getLocalizedMessage());
    } catch (IllegalArgumentException e) {
        Assert.fail(e.getLocalizedMessage());
    } catch (InvocationTargetException e) {
        throw (MathIllegalArgumentException) e.getCause();
    }
}
Also used : Method(java.lang.reflect.Method) FieldVector3D(org.hipparchus.geometry.euclidean.threed.FieldVector3D) MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 9 with MathIllegalArgumentException

use of org.hipparchus.exception.MathIllegalArgumentException in project symja_android_library by axkr.

the class NIntegrate method integrate.

// public final static ISymbol LegendreGauss = F
// .initFinalSymbol(Config.PARSER_USE_LOWERCASE_SYMBOLS ? "legendregauss" : "LegendreGauss");
/**
 * Integrate a function numerically.
 *
 * @param method the following methods are possible: LegendreGauss, Simpson, Romberg, Trapezoid
 * @param list a list of the form <code>{x, lowerBound, upperBound}</code>, where <code>lowerBound
 *     </code> and <code>upperBound</code> are numbers which could be converted to a Java double
 *        value.
 * @param min Lower bound of the integration interval.
 * @param max Upper bound of the integration interval.
 * @param function the function which should be integrated.
 * @param maxPoints maximum number of points
 * @param maxIterations maximum number of iterations
 * @return
 * @throws MathIllegalStateException
 */
public static double integrate(String method, IAST list, double min, double max, IExpr function, int maxPoints, int maxIterations) throws MathIllegalStateException {
    GaussIntegratorFactory factory = new GaussIntegratorFactory();
    if (!list.arg1().isSymbol()) {
        // `1` is not a valid variable.
        String str = IOFunctions.getMessage("ivar", F.list(list.arg1()), EvalEngine.get());
        throw new ArgumentTypeException(str);
    }
    ISymbol xVar = (ISymbol) list.arg1();
    final EvalEngine engine = EvalEngine.get();
    IExpr tempFunction = F.eval(function);
    UnivariateFunction f = new UnaryNumerical(tempFunction, xVar, engine);
    UnivariateIntegrator integrator;
    if ("Simpson".equalsIgnoreCase(method)) {
        integrator = new SimpsonIntegrator();
    } else if ("Romberg".equalsIgnoreCase(method)) {
        integrator = new RombergIntegrator();
    } else if ("Trapezoid".equalsIgnoreCase(method)) {
        integrator = new TrapezoidIntegrator();
    } else {
        if (maxPoints > 1000) {
            // see also https://github.com/Hipparchus-Math/hipparchus/issues/61
            throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, maxPoints, 1000);
        }
        // default: LegendreGauss
        GaussIntegrator integ = factory.legendre(maxPoints, min, max);
        return integ.integrate(f);
    }
    return integrator.integrate(maxIterations, f, min, max);
}
Also used : SimpsonIntegrator(org.hipparchus.analysis.integration.SimpsonIntegrator) UnaryNumerical(org.matheclipse.core.generic.UnaryNumerical) ISymbol(org.matheclipse.core.interfaces.ISymbol) UnivariateFunction(org.hipparchus.analysis.UnivariateFunction) UnivariateIntegrator(org.hipparchus.analysis.integration.UnivariateIntegrator) RombergIntegrator(org.hipparchus.analysis.integration.RombergIntegrator) GaussIntegratorFactory(org.hipparchus.analysis.integration.gauss.GaussIntegratorFactory) GaussIntegrator(org.hipparchus.analysis.integration.gauss.GaussIntegrator) MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException) TrapezoidIntegrator(org.hipparchus.analysis.integration.TrapezoidIntegrator)

Example 10 with MathIllegalArgumentException

use of org.hipparchus.exception.MathIllegalArgumentException in project symja_android_library by axkr.

the class NIntegrate method evaluate.

/**
 * Function for <a href="http://en.wikipedia.org/wiki/Numerical_integration">numerical
 * integration</a> of univariate real functions.
 *
 * <p>
 * Uses the LegendreGaussIntegrator, RombergIntegrator, SimpsonIntegrator, TrapezoidIntegrator
 * implementations.
 */
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    String method = "LegendreGauss";
    int maxPoints = DEFAULT_MAX_POINTS;
    int maxIterations = DEFAULT_MAX_ITERATIONS;
    // automatic scale value
    int precisionGoal = 16;
    if (ast.size() >= 4) {
        final OptionArgs options = new OptionArgs(ast.topHead(), ast, 3, engine);
        IExpr option = options.getOption(S.Method);
        if (option.isSymbol()) {
            method = option.toString();
        }
        option = options.getOption(S.MaxPoints);
        if (option.isReal()) {
            maxPoints = ((ISignedNumber) option).toIntDefault(-1);
            if (maxPoints <= 0) {
                LOGGER.log(engine.getLogLevel(), "NIntegrate: Error in option MaxPoints. Using default value: {}", maxPoints);
                maxPoints = DEFAULT_MAX_POINTS;
            }
        }
        maxIterations = options.getOptionMaxIterations(S.MaxIterations);
        if (maxIterations == Integer.MIN_VALUE) {
            return F.NIL;
        }
        if (maxIterations < 0) {
            maxIterations = DEFAULT_MAX_ITERATIONS;
        }
        option = options.getOption(S.PrecisionGoal);
        if (option.isReal()) {
            precisionGoal = ((ISignedNumber) option).toIntDefault(-1);
            if (precisionGoal <= 0) {
                LOGGER.log(engine.getLogLevel(), "NIntegrate: Error in option PrecisionGoal. Using default value: {}", precisionGoal);
                precisionGoal = 16;
            }
        }
    }
    if (ast.arg2().isList()) {
        IAST list = (IAST) ast.arg2();
        IExpr function = ast.arg1();
        if (list.isAST3() && list.arg1().isSymbol()) {
            ISignedNumber min = list.arg2().evalReal();
            ISignedNumber max = list.arg3().evalReal();
            if (min != null && max != null) {
                if (function.isEqual()) {
                    IAST equalAST = (IAST) function;
                    function = F.Plus(equalAST.arg1(), F.Negate(equalAST.arg2()));
                }
                try {
                    double result = integrate(method, list, min.doubleValue(), max.doubleValue(), function, maxPoints, maxIterations);
                    result = Precision.round(result, precisionGoal);
                    return Num.valueOf(result);
                } catch (MathIllegalArgumentException | MathIllegalStateException miae) {
                    // `1`.
                    return IOFunctions.printMessage(ast.topHead(), "error", F.list(F.$str(miae.getMessage())), engine);
                } catch (MathRuntimeException mre) {
                    LOGGER.log(engine.getLogLevel(), ast.topHead(), mre);
                } catch (Exception e) {
                    LOGGER.log(engine.getLogLevel(), "NIntegrate: (method={}) ", method, e);
                }
            }
        }
    }
    return F.NIL;
}
Also used : MathRuntimeException(org.hipparchus.exception.MathRuntimeException) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException) OptionArgs(org.matheclipse.core.eval.util.OptionArgs) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) MathIllegalStateException(org.hipparchus.exception.MathIllegalStateException) MathRuntimeException(org.hipparchus.exception.MathRuntimeException) MathIllegalStateException(org.hipparchus.exception.MathIllegalStateException) MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Aggregations

MathIllegalArgumentException (org.hipparchus.exception.MathIllegalArgumentException)11 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 MathRuntimeException (org.hipparchus.exception.MathRuntimeException)2 FieldVector3D (org.hipparchus.geometry.euclidean.threed.FieldVector3D)2 Vector3D (org.hipparchus.geometry.euclidean.threed.Vector3D)2 RealMatrix (org.hipparchus.linear.RealMatrix)2 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)2 IAST (org.matheclipse.core.interfaces.IAST)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 OrekitException (org.orekit.errors.OrekitException)2 BigInteger (java.math.BigInteger)1 UnivariateFunction (org.hipparchus.analysis.UnivariateFunction)1 RombergIntegrator (org.hipparchus.analysis.integration.RombergIntegrator)1 SimpsonIntegrator (org.hipparchus.analysis.integration.SimpsonIntegrator)1 TrapezoidIntegrator (org.hipparchus.analysis.integration.TrapezoidIntegrator)1 UnivariateIntegrator (org.hipparchus.analysis.integration.UnivariateIntegrator)1 GaussIntegrator (org.hipparchus.analysis.integration.gauss.GaussIntegrator)1