Search in sources :

Example 6 with OptionArgs

use of org.matheclipse.core.eval.util.OptionArgs in project symja_android_library by axkr.

the class FindMinimum method findExtremum.

protected static IExpr findExtremum(IAST ast, EvalEngine engine, GoalType goalType) {
    IExpr function = ast.arg1();
    IExpr arg2 = ast.arg2();
    if (!arg2.isList()) {
        arg2 = engine.evaluate(arg2);
    }
    if (arg2.isList() && arg2.argSize() >= 2) {
        String method = "Powell";
        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();
                }
            }
        }
        return optimizeGoal(method, maxIterations, goalType, function, (IAST) arg2, engine);
    }
    return F.NIL;
}
Also used : OptionArgs(org.matheclipse.core.eval.util.OptionArgs) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 7 with OptionArgs

use of org.matheclipse.core.eval.util.OptionArgs in project symja_android_library by axkr.

the class ListPointPlot3D method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    IOFunctions.printExperimental(S.ListPointPlot3D);
    if (ast.argSize() > 0) {
        if (ast.argSize() > 1) {
            final OptionArgs options = new OptionArgs(ast.topHead(), ast, 2, engine);
            if (options.isInvalidPosition(1)) {
                return options.printNonopt(ast, 1, engine);
            }
        // IExpr colorFunction = options.getOption(S.ColorFunction);
        // if (colorFunction.isPresent()) {
        // // ... color function is set...
        // }
        }
        int[] dimension = ast.arg1().isMatrix(false);
        if (dimension != null) {
            // convert possible sparse array expression:
            IAST heightValueMatrix = (IAST) ast.arg1().normal(false);
            if (dimension[0] > 3) {
                if (dimension[1] == 3) {
                    IASTAppendable pointList = F.ListAlloc(dimension[0]);
                    for (int i = 1; i < heightValueMatrix.size(); i++) {
                        IAST rowList = (IAST) heightValueMatrix.get(i);
                        pointList.append(rowList);
                    }
                    IASTAppendable result = F.Graphics3D(F.Point(pointList));
                    if (ast.argSize() > 1) {
                        // add same options to Graphics3D
                        result.appendAll(ast, 2, ast.size());
                    }
                    return result;
                } else if (dimension[1] > 3) {
                    IASTAppendable pointList = F.ListAlloc(dimension[0] * dimension[1]);
                    for (int i = 1; i < heightValueMatrix.size(); i++) {
                        IAST rowList = (IAST) heightValueMatrix.get(i);
                        for (int j = 1; j < rowList.size(); j++) {
                            // 
                            pointList.append(F.list(F.ZZ(i), F.ZZ(j), rowList.get(j)));
                        }
                    }
                    IASTAppendable result = F.Graphics3D(F.Point(pointList));
                    if (ast.argSize() > 1) {
                        // add same options to Graphics3D
                        result.appendAll(ast, 2, ast.size());
                    }
                    return result;
                }
            }
        }
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) OptionArgs(org.matheclipse.core.eval.util.OptionArgs) IAST(org.matheclipse.core.interfaces.IAST)

Example 8 with OptionArgs

use of org.matheclipse.core.eval.util.OptionArgs 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 9 with OptionArgs

use of org.matheclipse.core.eval.util.OptionArgs in project symja_android_library by axkr.

the class GraphicsFunctions method renderGraphics3D.

public static boolean renderGraphics3D(StringBuilder graphics3DBuffer, IAST graphics3DAST, EvalEngine engine) {
    IExpr arg1 = graphics3DAST.first();
    if (!arg1.isList()) {
        arg1 = F.list(arg1);
    }
    // .List(F.$str("Auto"), F.RGBColor(F.C1, F.C1, F.C1));
    IExpr lighting = S.Automatic;
    OptionArgs options = OptionArgs.createOptionArgs(graphics3DAST, engine);
    if (options != null) {
        lighting = options.getOption(S.Lighting).orElse(lighting);
    // if (option.isList1() && option.first().isList() && option.first().first().isString()) {
    // lighting = option.first();
    // }
    }
    IExpr data3D = engine.evaluate(F.N(arg1));
    if (data3D.isAST() && data3D.head().isBuiltInSymbol()) {
        StringBuilder jsonPrimitives = new StringBuilder();
        if (GraphicsFunctions.exportGraphics3DRecursive(jsonPrimitives, (IAST) data3D)) {
            try {
                graphics3DBuffer.append("drawGraphics3d(document.getElementById('graphics3d'),\n");
                graphics3DBuffer.append("{");
                graphics3DBuffer.append("\naxes: {},");
                graphics3DBuffer.append("\nelements: [");
                graphics3DBuffer.append(jsonPrimitives.toString());
                graphics3DBuffer.append("],");
                graphics3DLigthing(graphics3DBuffer, lighting);
                graphics3DBuffer.append("\nviewpoint: [1.3, -2.4, 2.0]");
                graphics3DBuffer.append("}\n");
                graphics3DBuffer.append(");");
                return true;
            } catch (Exception ex) {
                LOGGER.debug("GraphicsFunctions.renderGraphics3D() failed", ex);
            }
        }
    }
    return false;
}
Also used : OptionArgs(org.matheclipse.core.eval.util.OptionArgs) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 10 with OptionArgs

use of org.matheclipse.core.eval.util.OptionArgs 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

OptionArgs (org.matheclipse.core.eval.util.OptionArgs)13 IExpr (org.matheclipse.core.interfaces.IExpr)10 IAST (org.matheclipse.core.interfaces.IAST)8 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)4 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)3 MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)2 MathRuntimeException (org.hipparchus.exception.MathRuntimeException)2 EvalEngine (org.matheclipse.core.eval.EvalEngine)2 IAssumptions (org.matheclipse.core.eval.util.IAssumptions)2 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)2 Escaper (com.google.common.escape.Escaper)1 ArrayList (java.util.ArrayList)1 MathIllegalArgumentException (org.hipparchus.exception.MathIllegalArgumentException)1 ASTSeriesData (org.matheclipse.core.expression.ASTSeriesData)1 Dimensions2D (org.matheclipse.core.graphics.Dimensions2D)1