Search in sources :

Example 1 with VisitorLevelSpecification

use of org.matheclipse.core.visit.VisitorLevelSpecification in project symja_android_library by axkr.

the class MapAll method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3);
    final IAST arg1AST = F.ast(ast.arg1());
    final VisitorLevelSpecification level = new VisitorLevelSpecification(Functors.append(arg1AST), 0, Integer.MAX_VALUE, false);
    final IExpr result = ast.arg2().accept(level);
    return result.isPresent() ? result : ast.arg2();
}
Also used : VisitorLevelSpecification(org.matheclipse.core.visit.VisitorLevelSpecification) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 2 with VisitorLevelSpecification

use of org.matheclipse.core.visit.VisitorLevelSpecification in project symja_android_library by axkr.

the class Replace method replaceExprWithLevelSpecification.

private static IExpr replaceExprWithLevelSpecification(final IAST ast, IExpr arg1, IExpr rules, IExpr exprLevelSpecification, EvalEngine engine) {
    // use replaceFunction#setRule() method to set the current rules which
    // are initialized with null
    ReplaceFunction replaceFunction = new ReplaceFunction(ast, null, engine);
    VisitorLevelSpecification level = new VisitorLevelSpecification(replaceFunction, exprLevelSpecification, false);
    if (rules.isListOfLists()) {
        IAST result = F.List();
        for (IExpr list : (IAST) rules) {
            IExpr temp = F.NIL;
            IAST subList = (IAST) list;
            for (IExpr element : subList) {
                if (element.isRuleAST()) {
                    IAST rule = (IAST) element;
                    replaceFunction.setRule(rule);
                    temp = arg1.accept(level);
                    if (temp.isPresent()) {
                        break;
                    }
                } else {
                    WrongArgumentType wat = new WrongArgumentType(ast, ast, -1, "Rule expression (x->y) expected: ");
                    throw wat;
                }
            }
            result.append(temp.orElse(arg1));
        }
        return result;
    }
    replaceFunction.setRule(rules);
    return arg1.accept(level).orElse(arg1);
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) VisitorLevelSpecification(org.matheclipse.core.visit.VisitorLevelSpecification) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

IAST (org.matheclipse.core.interfaces.IAST)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 VisitorLevelSpecification (org.matheclipse.core.visit.VisitorLevelSpecification)2 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)1