Search in sources :

Example 6 with IPatternMatcher

use of org.matheclipse.core.patternmatching.IPatternMatcher in project symja_android_library by axkr.

the class VisitorReplacePart method initPatternMatcher.

private void initPatternMatcher(IAST rule, IExpr.COMPARE_TERNARY heads) {
    IExpr fromPositions = rule.arg1();
    try {
        // try extracting an int[] array of expressions
        if (fromPositions.isList()) {
            IAST list = (IAST) fromPositions;
            if (list.isListOfLists()) {
                for (int j = 1; j < list.size(); j++) {
                    IAST subList = list.getAST(j);
                    int[] positions = new int[subList.argSize()];
                    for (int k = 1; k < subList.size(); k++) {
                        positions[k - 1] = subList.get(k).toIntDefault();
                        if (positions[k - 1] == Integer.MIN_VALUE) {
                            throw ReturnException.RETURN_FALSE;
                        }
                        if (positions[k - 1] == 0) {
                            offset = 0;
                        }
                    }
                    IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(F.Sequence(positions), rule.arg2());
                    this.patternMatcherList.add(evalPatternMatcher);
                }
            } else {
                if (list.argSize() > 0) {
                    int[] positions = new int[list.argSize()];
                    for (int j = 1; j < list.size(); j++) {
                        positions[j - 1] = list.get(j).toIntDefault();
                        if (positions[j - 1] == Integer.MIN_VALUE) {
                            throw ReturnException.RETURN_FALSE;
                        }
                        if (positions[j - 1] == 0) {
                            offset = 0;
                        }
                    }
                    IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(F.Sequence(positions), rule.arg2());
                    this.patternMatcherList.add(evalPatternMatcher);
                }
            }
        } else {
            int[] positions = new int[] { rule.arg1().toIntDefault() };
            if (positions[0] == Integer.MIN_VALUE) {
                throw ReturnException.RETURN_FALSE;
            }
            if (positions[0] == 0) {
                offset = 0;
            }
            IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(F.Sequence(positions), rule.arg2());
            this.patternMatcherList.add(evalPatternMatcher);
        }
    } catch (ReturnException rex) {
        if (fromPositions.isList()) {
            IAST list = ((IAST) fromPositions).apply(S.Sequence, 1);
            IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(list, rule.arg2());
            this.patternMatcherList.add(evalPatternMatcher);
        } else {
            IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(fromPositions, rule.arg2());
            this.patternMatcherList.add(evalPatternMatcher);
        }
    }
}
Also used : IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ReturnException(org.matheclipse.core.eval.exception.ReturnException)

Example 7 with IPatternMatcher

use of org.matheclipse.core.patternmatching.IPatternMatcher in project symja_android_library by axkr.

the class VisitorReplacePart method visitPatternIndexList.

private IExpr visitPatternIndexList(IAST ast, IASTAppendable positions) {
    IASTAppendable result = F.NIL;
    for (int i = offset; i < ast.size(); i++) {
        final IInteger position = F.ZZ(i);
        for (int j = 0; j < patternMatcherList.size(); j++) {
            IPatternMatcher matcher = patternMatcherList.get(j);
            IASTAppendable positionsToMatch = positions.copyAppendable();
            positionsToMatch.append(position);
            IASTAppendable temp = patternIndexRecursive(matcher, ast, positionsToMatch, i, result);
            if (temp.isPresent()) {
                result = temp;
                break;
            }
            IInteger negativePart = F.ZZUniqueReference(i - ast.size());
            positionsToMatch.set(positionsToMatch.size() - 1, negativePart);
            temp = patternIndexRecursive(matcher, ast, positionsToMatch, i, result);
            if (temp.isPresent()) {
                IExpr ex = temp.replaceAll(x -> {
                    if (x == negativePart) {
                        // compare unique reference from F.ZZUniqueReference(); don't use equals() method!
                        return position;
                    }
                    return F.NIL;
                });
                if (ex.isPresent() && (ex instanceof IASTAppendable)) {
                    result = (IASTAppendable) ex;
                } else {
                    result = temp;
                }
                break;
            }
            if (ast.isAssociation() && i > 0) {
                // for associations the key instead of the position can also match
                positionsToMatch.set(positionsToMatch.size() - 1, ast.getRule(i).first());
                temp = patternIndexRecursive(matcher, ast, positionsToMatch, i, result);
                if (temp.isPresent()) {
                    result = temp;
                    break;
                }
            }
        }
    }
    return result;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IInteger(org.matheclipse.core.interfaces.IInteger) IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

IExpr (org.matheclipse.core.interfaces.IExpr)7 IPatternMatcher (org.matheclipse.core.patternmatching.IPatternMatcher)7 IAST (org.matheclipse.core.interfaces.IAST)4 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 PatternMatcher (org.matheclipse.core.patternmatching.PatternMatcher)3 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 Serializable (java.io.Serializable)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 BiPredicate (java.util.function.BiPredicate)1 Predicate (java.util.function.Predicate)1 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 ReturnException (org.matheclipse.core.eval.exception.ReturnException)1 F (org.matheclipse.core.expression.F)1 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)1 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)1 IInteger (org.matheclipse.core.interfaces.IInteger)1 IPattern (org.matheclipse.core.interfaces.IPattern)1