Search in sources :

Example 1 with MathIllegalStateException

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

the class NMinimize method simplexSolver.

protected static IExpr simplexSolver(VariablesSet vars, LinearObjectiveFunction f, OptimizationData... optData) {
    try {
        SimplexSolver solver = new SimplexSolver();
        PointValuePair solution = solver.optimize(optData);
        double[] values = solution.getPointRef();
        IAST result = F.List(F.num(f.value(values)), F.List(values));
        return result;
    } catch (MathIllegalStateException oe) {
        throw new WrappedException(oe);
    // if (Config.SHOW_STACKTRACE) {
    // oe.printStackTrace();
    // }
    }
}
Also used : WrappedException(org.matheclipse.core.eval.exception.WrappedException) SimplexSolver(org.hipparchus.optim.linear.SimplexSolver) IAST(org.matheclipse.core.interfaces.IAST) MathIllegalStateException(org.hipparchus.exception.MathIllegalStateException) PointValuePair(org.hipparchus.optim.PointValuePair)

Example 2 with MathIllegalStateException

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

the class FieldSpacecraftStateTest method doTestAdditionalStates.

private <T extends RealFieldElement<T>> void doTestAdditionalStates(final Field<T> field) throws OrekitException {
    T zero = field.getZero();
    T a = zero.add(rOrbit.getA());
    T e = zero.add(rOrbit.getE());
    T i = zero.add(rOrbit.getI());
    T pa = zero.add(1.9674147913622104);
    T raan = zero.add(FastMath.toRadians(261));
    T lv = zero.add(0);
    T mass = zero.add(2500);
    FieldAbsoluteDate<T> date = new FieldAbsoluteDate<>(field, new DateComponents(2004, 01, 01), TimeComponents.H00, TimeScalesFactory.getUTC());
    FieldKeplerianOrbit<T> orbit = new FieldKeplerianOrbit<>(a, e, i, pa, raan, lv, PositionAngle.TRUE, FramesFactory.getEME2000(), date, mu);
    BodyCenterPointing attitudeLaw = new BodyCenterPointing(orbit.getFrame(), earth);
    FieldKeplerianPropagator<T> propagator = new FieldKeplerianPropagator<>(orbit, attitudeLaw, mu, mass);
    final FieldSpacecraftState<T> state = propagator.propagate(orbit.getDate().shiftedBy(60));
    T[] add = MathArrays.buildArray(field, 2);
    add[0] = zero.add(1.);
    add[1] = zero.add(2.);
    final FieldSpacecraftState<T> extended = state.addAdditionalState("test-1", add).addAdditionalState("test-2", zero.add(42.0));
    Assert.assertEquals(0, state.getAdditionalStates().size());
    Assert.assertFalse(state.hasAdditionalState("test-1"));
    try {
        state.getAdditionalState("test-1");
        Assert.fail("an exception should have been thrown");
    } catch (OrekitException oe) {
        Assert.assertEquals(oe.getSpecifier(), OrekitMessages.UNKNOWN_ADDITIONAL_STATE);
        Assert.assertEquals(oe.getParts()[0], "test-1");
    }
    try {
        state.ensureCompatibleAdditionalStates(extended);
        Assert.fail("an exception should have been thrown");
    } catch (OrekitException oe) {
        Assert.assertEquals(oe.getSpecifier(), OrekitMessages.UNKNOWN_ADDITIONAL_STATE);
        Assert.assertTrue(oe.getParts()[0].toString().startsWith("test-"));
    }
    try {
        extended.ensureCompatibleAdditionalStates(state);
        Assert.fail("an exception should have been thrown");
    } catch (OrekitException oe) {
        Assert.assertEquals(oe.getSpecifier(), OrekitMessages.UNKNOWN_ADDITIONAL_STATE);
        Assert.assertTrue(oe.getParts()[0].toString().startsWith("test-"));
    }
    try {
        T[] kk = MathArrays.buildArray(field, 7);
        extended.ensureCompatibleAdditionalStates(extended.addAdditionalState("test-2", kk));
        Assert.fail("an exception should have been thrown");
    } catch (MathIllegalStateException mise) {
        Assert.assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, mise.getSpecifier());
        Assert.assertEquals(7, ((Integer) mise.getParts()[0]).intValue());
    }
    Assert.assertEquals(2, extended.getAdditionalStates().size());
    Assert.assertTrue(extended.hasAdditionalState("test-1"));
    Assert.assertTrue(extended.hasAdditionalState("test-2"));
    Assert.assertEquals(1.0, extended.getAdditionalState("test-1")[0].getReal(), 1.0e-15);
    Assert.assertEquals(2.0, extended.getAdditionalState("test-1")[1].getReal(), 1.0e-15);
    Assert.assertEquals(42.0, extended.getAdditionalState("test-2")[0].getReal(), 1.0e-15);
    // test various constructors
    T[] dd = MathArrays.buildArray(field, 1);
    dd[0] = zero.add(-6.0);
    Map<String, T[]> map = new HashMap<String, T[]>();
    map.put("test-3", dd);
    FieldSpacecraftState<T> sO = new FieldSpacecraftState<>(state.getOrbit(), map);
    Assert.assertEquals(-6.0, sO.getAdditionalState("test-3")[0].getReal(), 1.0e-15);
    FieldSpacecraftState<T> sOA = new FieldSpacecraftState<>(state.getOrbit(), state.getAttitude(), map);
    Assert.assertEquals(-6.0, sOA.getAdditionalState("test-3")[0].getReal(), 1.0e-15);
    FieldSpacecraftState<T> sOM = new FieldSpacecraftState<>(state.getOrbit(), state.getMass(), map);
    Assert.assertEquals(-6.0, sOM.getAdditionalState("test-3")[0].getReal(), 1.0e-15);
    FieldSpacecraftState<T> sOAM = new FieldSpacecraftState<>(state.getOrbit(), state.getAttitude(), state.getMass(), map);
    Assert.assertEquals(-6.0, sOAM.getAdditionalState("test-3")[0].getReal(), 1.0e-15);
    FieldSpacecraftState<T> sFromDouble = new FieldSpacecraftState<>(field, sOAM.toSpacecraftState());
    Assert.assertEquals(-6.0, sFromDouble.getAdditionalState("test-3")[0].getReal(), 1.0e-15);
}
Also used : HashMap(java.util.HashMap) DateComponents(org.orekit.time.DateComponents) MathIllegalStateException(org.hipparchus.exception.MathIllegalStateException) FieldKeplerianOrbit(org.orekit.orbits.FieldKeplerianOrbit) FieldKeplerianPropagator(org.orekit.propagation.analytical.FieldKeplerianPropagator) BodyCenterPointing(org.orekit.attitudes.BodyCenterPointing) OrekitException(org.orekit.errors.OrekitException) FieldAbsoluteDate(org.orekit.time.FieldAbsoluteDate)

Example 3 with MathIllegalStateException

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

the class FindRoot method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    // default: BracketingNthOrderBrentSolver
    String method = "Brent";
    int maxIterations = 100;
    if (ast.size() >= 4) {
        final OptionArgs options = new OptionArgs(ast.topHead(), ast, 3, engine);
        maxIterations = options.getOptionMaxIterations(S.MaxIterations);
        if (maxIterations == Integer.MIN_VALUE) {
            return F.NIL;
        }
        if (maxIterations < 0) {
            maxIterations = 100;
        }
        IExpr optionMethod = options.getOption(S.Method);
        if (optionMethod.isSymbol() || optionMethod.isString()) {
            method = optionMethod.toString();
        } else {
            if (ast.arg3().isSymbol()) {
                method = ast.arg3().toString();
            }
        }
    }
    IExpr arg2 = ast.arg2();
    if (!arg2.isList()) {
        arg2 = engine.evaluate(arg2);
    }
    if (arg2.isList()) {
        IAST list = (IAST) arg2;
        if (list.size() >= 3 && list.arg1().isSymbol()) {
            ISignedNumber min = list.arg2().evalReal();
            if (min != null) {
                ISignedNumber max = null;
                if (list.size() > 3) {
                    max = list.arg3().evalReal();
                }
                try {
                    UnivariateSolverSupplier optimizeSupplier = new UnivariateSolverSupplier(ast.arg1(), list, min, max, maxIterations, method, engine);
                    IExpr result = engine.evalBlock(optimizeSupplier, list);
                    return F.list(F.Rule(list.arg1(), result));
                } catch (MathIllegalStateException miae) {
                    // `1`.
                    return IOFunctions.printMessage(ast.topHead(), "error", F.list(F.$str(miae.getMessage())), engine);
                } catch (MathRuntimeException mre) {
                    IOFunctions.printMessage(ast.topHead(), "error", F.list(F.$str(mre.getMessage())), engine);
                    return F.CEmptyList;
                }
            }
        }
    }
    return F.NIL;
}
Also used : MathRuntimeException(org.hipparchus.exception.MathRuntimeException) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) OptionArgs(org.matheclipse.core.eval.util.OptionArgs) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) MathIllegalStateException(org.hipparchus.exception.MathIllegalStateException)

Example 4 with MathIllegalStateException

use of org.hipparchus.exception.MathIllegalStateException 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

MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)4 IAST (org.matheclipse.core.interfaces.IAST)3 MathRuntimeException (org.hipparchus.exception.MathRuntimeException)2 OptionArgs (org.matheclipse.core.eval.util.OptionArgs)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)2 HashMap (java.util.HashMap)1 MathIllegalArgumentException (org.hipparchus.exception.MathIllegalArgumentException)1 PointValuePair (org.hipparchus.optim.PointValuePair)1 SimplexSolver (org.hipparchus.optim.linear.SimplexSolver)1 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)1 WrappedException (org.matheclipse.core.eval.exception.WrappedException)1 BodyCenterPointing (org.orekit.attitudes.BodyCenterPointing)1 OrekitException (org.orekit.errors.OrekitException)1 FieldKeplerianOrbit (org.orekit.orbits.FieldKeplerianOrbit)1 FieldKeplerianPropagator (org.orekit.propagation.analytical.FieldKeplerianPropagator)1 DateComponents (org.orekit.time.DateComponents)1 FieldAbsoluteDate (org.orekit.time.FieldAbsoluteDate)1