Search in sources :

Example 36 with IASTMutable

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

the class ModuleReplaceAll method visitNestedScope.

/**
 * Handle nested Module(), With() or Function()
 *
 * @param ast
 * @param isFunction <code>ast</code> has the form <code>Function(a1, a2)</code>
 * @return
 */
private IAST visitNestedScope(IAST ast, boolean isFunction) {
    IAST localVariablesList = F.NIL;
    if (ast.arg1().isSymbol()) {
        localVariablesList = F.list(ast.arg1());
    } else if (ast.arg1().isList()) {
        localVariablesList = (IAST) ast.arg1();
    }
    ModuleReplaceAll visitor = this;
    if (localVariablesList.isPresent()) {
        IdentityHashMap<ISymbol, IExpr> variables = renamedVariables(localVariablesList, isFunction);
        if (variables != null) {
            visitor = new ModuleReplaceAll(variables, fEngine, moduleCounter);
        }
    }
    IExpr temp;
    int i = fOffset;
    while (i < ast.size()) {
        temp = ast.get(i).accept(visitor);
        if (temp.isPresent()) {
            // something was evaluated - return a new IAST:
            IASTMutable result = ast.setAtCopy(i++, temp);
            while (i < ast.size()) {
                temp = ast.get(i).accept(visitor);
                if (temp.isPresent()) {
                    result.set(i, temp);
                }
                i++;
            }
            return result;
        }
        i++;
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 37 with IASTMutable

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

the class ModuleReplaceAll method visitASTModule.

private IExpr visitASTModule(IAST ast) {
    IExpr temp;
    int i = fOffset;
    final int size = ast.size();
    while (i < size) {
        temp = ast.get(i).accept(this);
        if (temp.isPresent()) {
            // something was evaluated - return a new IAST:
            IASTMutable result = ast.setAtCopy(i++, temp);
            ast.forEach(i, size, (x, j) -> {
                IExpr t = x.accept(this);
                if (t.isPresent()) {
                    result.set(j, t);
                }
            });
            return result;
        }
        i++;
    }
    return F.NIL;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 38 with IASTMutable

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

the class VisitorPlusTimesPowerReplaceAll method visitAST.

@Override
protected IExpr visitAST(IAST ast) {
    if (ast.isPower()) {
        IExpr base = ast.base().accept(this);
        return base.isPresent() ? ast.setAtCopy(1, base) : F.NIL;
    }
    int i = fOffset;
    int size = ast.size();
    IExpr temp;
    while (i < size) {
        temp = ast.get(i).accept(this);
        if (temp.isPresent()) {
            // something was evaluated - return a new IAST:
            IASTMutable result = ast.setAtCopy(i++, temp);
            ast.forEach(i, size, (x, j) -> {
                IExpr t = x.accept(this);
                if (t.isPresent()) {
                    result.set(j, t);
                }
            });
            return result;
        }
        i++;
    }
    return F.NIL;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 39 with IASTMutable

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

the class VisitorReplaceAll method visit.

/**
 * @return <code>F.NIL</code>, if no evaluation is possible
 */
@Override
public IExpr visit(IAssociation assoc) {
    IExpr replacement = fFunction.apply(assoc);
    if (replacement.isPresent()) {
        return replacement;
    }
    int size = assoc.size();
    for (int i = fOffset; i < size; i++) {
        IExpr temp = assoc.getValue(i).accept(this);
        if (temp.isPresent()) {
            // something was evaluated - return a new IAST:
            IAST iRuleOrNIL = assoc.getRule(i);
            IASTMutable result;
            if (iRuleOrNIL.isPresent()) {
                result = assoc.setAtCopy(i, iRuleOrNIL.setAtCopy(2, temp));
            } else {
                result = assoc.copy();
            }
            assoc.forEach(i + 1, size, (x, j) -> {
                IExpr t = x.accept(this);
                if (t.isPresent()) {
                    result.set(j, assoc.getRule(j).setAtCopy(2, t));
                }
            });
            return postProcessing(result);
        }
    }
    return F.NIL;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 40 with IASTMutable

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

the class VisitorReplaceAllWithPatternFlags method visit.

@Override
public IExpr visit(IASTMutable ast) {
    if (ast.isPatternMatchingFunction()) {
        return F.NIL;
    }
    int i = fOffset;
    int size = ast.size();
    IASTMutable result = F.NIL;
    while (i < size) {
        IExpr temp = ast.get(i).accept(this);
        if (temp.isPresent()) {
            // something was evaluated - return a new IAST:
            result = ast.setAtCopy(i++, temp);
            while (i < size) {
                temp = ast.get(i).accept(this);
                if (temp.isPresent()) {
                    result.set(i, temp);
                }
                i++;
            }
            if (result.isAST()) {
                return EvalAttributes.simpleEval(result);
            // if (result.isFlatAST()) {
            // IASTAppendable flattened = EvalAttributes.flattenDeep((IAST) result);
            // if (flattened.isPresent()) {
            // result = flattened;
            // }
            // }
            // if (result.isOneIdentityAST1()) {
            // return result.first();
            // } else if (result.isOrderlessAST()) {
            // EvalAttributes.sort((IASTMutable) result);
            // }
            // ((IAST) result).addEvalFlags(ast.getEvalFlags() & IAST.CONTAINS_PATTERN_EXPR);
            }
            // }
            return result;
        }
        i++;
    }
    return F.NIL;
}
Also used : IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

IASTMutable (org.matheclipse.core.interfaces.IASTMutable)92 IExpr (org.matheclipse.core.interfaces.IExpr)60 IAST (org.matheclipse.core.interfaces.IAST)34 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)26 ISymbol (org.matheclipse.core.interfaces.ISymbol)12 IInteger (org.matheclipse.core.interfaces.IInteger)5 Map (java.util.Map)4 IComplex (org.matheclipse.core.interfaces.IComplex)4 IRational (org.matheclipse.core.interfaces.IRational)4 ArrayList (java.util.ArrayList)3 TreeMap (java.util.TreeMap)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)3 ValidateException (org.matheclipse.core.eval.exception.ValidateException)3 INumber (org.matheclipse.core.interfaces.INumber)3 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)3 ISparseArray (org.matheclipse.core.interfaces.ISparseArray)3 ASTNode (org.matheclipse.parser.client.ast.ASTNode)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2