Search in sources :

Example 1 with AbstractFunctionOptionEvaluator

use of org.matheclipse.core.eval.interfaces.AbstractFunctionOptionEvaluator in project symja_android_library by axkr.

the class EvalEngine method evalASTBuiltinFunction.

/**
 * @param symbol
 * @param ast
 * @return <code>F.NIL</code> if no evaluation happened
 */
private IExpr evalASTBuiltinFunction(final ISymbol symbol, IAST ast) {
    final int attributes = symbol.getAttributes();
    if (fEvalLHSMode) {
        if ((ISymbol.HOLDALL & attributes) == ISymbol.HOLDALL) {
            // (i.e. Sin, Cos,...)
            if (!(symbol.equals(S.Set) || symbol.equals(S.SetDelayed) || symbol.equals(S.UpSet) || symbol.equals(S.UpSetDelayed))) {
                return F.NIL;
            }
        } else {
            if ((ISymbol.NUMERICFUNCTION & attributes) != ISymbol.NUMERICFUNCTION) {
                return F.NIL;
            }
        }
    }
    if (!symbol.equals(S.Integrate)) {
        IExpr result;
        if ((result = symbol.evalDownRule(this, ast)).isPresent()) {
            return result;
        }
    }
    if (symbol.isBuiltInSymbol()) {
        final IEvaluator evaluator = ((IBuiltInSymbol) symbol).getEvaluator();
        if (evaluator instanceof IFunctionEvaluator) {
            if (ast.isEvalFlagOn(IAST.BUILT_IN_EVALED) && isSymbolicMode(attributes)) {
                return F.NIL;
            }
            // evaluate a built-in function.
            final IFunctionEvaluator functionEvaluator = (IFunctionEvaluator) evaluator;
            OptionsResult opres = checkBuiltinArguments(ast, functionEvaluator);
            if (opres == null) {
                return F.NIL;
            }
            ast = opres.result;
            try {
                if (evaluator instanceof AbstractFunctionOptionEvaluator) {
                    AbstractFunctionOptionEvaluator optionsEvaluator = (AbstractFunctionOptionEvaluator) evaluator;
                    IExpr result = optionsEvaluator.evaluate(ast, opres.argSize, opres.options, this);
                    if (result.isPresent()) {
                        return result;
                    }
                } else {
                    IExpr result = fNumericMode ? functionEvaluator.numericEval(ast, this) : functionEvaluator.evaluate(ast, this);
                    if (result.isPresent()) {
                        return result;
                    }
                }
            } catch (ValidateException ve) {
                return IOFunctions.printMessage(ast.topHead(), ve, this);
            } catch (FlowControlException e) {
                throw e;
            } catch (SymjaMathException ve) {
                LOGGER.log(getLogLevel(), ast.topHead(), ve);
                return F.NIL;
            }
        // cannot generally set the result as evaluated in built-in function. Especially problems in
        // `togetherMode`
        // if (isSymbolicMode(attributes) && !isTogetherMode()) {
        // ast.addEvalFlags(IAST.BUILT_IN_EVALED);
        // }
        }
    }
    return F.NIL;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) ValidateException(org.matheclipse.core.eval.exception.ValidateException) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) IFunctionEvaluator(org.matheclipse.core.eval.interfaces.IFunctionEvaluator) AbstractFunctionOptionEvaluator(org.matheclipse.core.eval.interfaces.AbstractFunctionOptionEvaluator) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) IExpr(org.matheclipse.core.interfaces.IExpr) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException)

Example 2 with AbstractFunctionOptionEvaluator

use of org.matheclipse.core.eval.interfaces.AbstractFunctionOptionEvaluator in project symja_android_library by axkr.

the class EvalEngine method checkBuiltinArguments.

/**
 * Check the number of arguments if requested and transform the <code>ast</code> from an
 * <i>operator form</i> to <i>normal form</i> if it is allowed.
 *
 * @param ast
 * @param functionEvaluator
 * @return
 */
public OptionsResult checkBuiltinArguments(IAST ast, final IFunctionEvaluator functionEvaluator) {
    int[] expected;
    OptionsResult opres = new OptionsResult(ast, ast.argSize(), null);
    if ((expected = functionEvaluator.expectedArgSize(ast)) != null) {
        if (expected.length == 2 && !ast.head().isBuiltInSymbol()) {
            return null;
        } else if (expected.length == 3 && expected[2] > 0) {
            switch(expected[2]) {
                case 1:
                    if (ast.isAST1()) {
                        opres.result = F.operatorForm1Append(ast);
                        if (!opres.result.isPresent()) {
                            return null;
                        }
                    }
                    break;
                case 2:
                    if (ast.head().isAST1()) {
                        opres.result = F.operatorForm2Prepend(ast, expected, this);
                        if (!opres.result.isPresent()) {
                            return null;
                        }
                    }
                    break;
                default:
            }
        }
        ast = opres.result;
        if (ast.argSize() < expected[0] || ast.argSize() > expected[1]) {
            if (ast.isAST1() && expected.length > 2) {
                // because an operator form is allowed do not print a message
                return null;
            }
            if (ast.argSize() > expected[0]) {
                if (functionEvaluator instanceof AbstractFunctionOptionEvaluator) {
                    AbstractFunctionOptionEvaluator optionEvaluator = (AbstractFunctionOptionEvaluator) functionEvaluator;
                    opres = getOptions(optionEvaluator, opres, ast, expected);
                    if (opres != null) {
                        return opres;
                    }
                }
            }
            IOFunctions.printArgMessage(ast, expected, this);
            return null;
        }
    }
    if (functionEvaluator instanceof AbstractFunctionOptionEvaluator) {
        AbstractFunctionOptionEvaluator optionEvaluator = (AbstractFunctionOptionEvaluator) functionEvaluator;
        opres = getOptions(optionEvaluator, opres, ast, expected);
        if (opres != null) {
            return opres;
        }
    }
    return opres;
}
Also used : AbstractFunctionOptionEvaluator(org.matheclipse.core.eval.interfaces.AbstractFunctionOptionEvaluator)

Aggregations

AbstractFunctionOptionEvaluator (org.matheclipse.core.eval.interfaces.AbstractFunctionOptionEvaluator)2 FlowControlException (org.matheclipse.core.eval.exception.FlowControlException)1 SymjaMathException (org.matheclipse.core.eval.exception.SymjaMathException)1 ValidateException (org.matheclipse.core.eval.exception.ValidateException)1 IFunctionEvaluator (org.matheclipse.core.eval.interfaces.IFunctionEvaluator)1 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)1 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)1 IExpr (org.matheclipse.core.interfaces.IExpr)1