Search in sources :

Example 6 with IPatternSequence

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

the class PatternMatcher method matchFlat.

/**
 * Match <code>Flat</code> LHS pattern expressions. It's assumed that the headers of the
 * expressions already matched.
 *
 * @param sym
 * @param lhsPattern
 * @param lhsEval
 * @param engine
 * @param stackMatcher
 * @return
 */
private boolean matchFlat(final ISymbol sym, final IAST lhsPattern, final IAST lhsEval, EvalEngine engine, StackMatcher stackMatcher) {
    if (lhsPattern.isAST1()) {
        int lhsEvalSize = lhsEval.size();
        if (lhsPattern.arg1().isPatternSequence(false)) {
            // TODO only the special case, where the last element is
            // a pattern sequence, is handled here
            IASTAppendable seq = F.Sequence();
            seq.appendAll(lhsEval, 1, lhsEvalSize);
            if (((IPatternSequence) lhsPattern.arg1()).matchPatternSequence(seq, fPatternMap, lhsPattern.topHead())) {
                return true;
            }
        }
        if (lhsPattern.size() == lhsEval.size()) {
            return matchASTSequence(lhsPattern, lhsEval, 0, engine, stackMatcher);
        }
        return false;
    }
    IAST lhsPatternAST = lhsPattern;
    IAST lhsEvalAST = lhsEval;
    // removeFlat already called a level up
    FlatStepVisitor visitor = new FlatStepVisitor(sym, lhsPatternAST, lhsEvalAST, stackMatcher, fPatternMap);
    NumberPartitionsIterator iter = new NumberPartitionsIterator(visitor, lhsEvalAST.argSize(), lhsPatternAST.argSize());
    return !iter.execute();
}
Also used : NumberPartitionsIterator(org.matheclipse.core.combinatoric.NumberPartitionsIterator) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IAST(org.matheclipse.core.interfaces.IAST)

Example 7 with IPatternSequence

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

the class PatternMatcher method remove.

/**
 * Remove parts which are "free of patterns" in <code>lhsPattern</code> and <code>lhsEval</code>.
 *
 * @param lhsPattern the expression which can contain pattern-matching objects
 * @param lhsEval the expression which can contain no patterns
 * @return <code>null</code> if the matching isn't possible.
 */
private IAST[] remove(final IAST lhsPattern, final IAST lhsEval, EvalEngine engine, StackMatcher stackMatcher) {
    IASTAppendable lhsPatternAST = F.NIL;
    IASTAppendable lhsEvalAST = F.NIL;
    int i = 1;
    int iIndex = 1;
    boolean evaled = false;
    boolean matched = false;
    while (i < lhsPattern.size()) {
        IExpr lhs = lhsPattern.get(i);
        IExpr rhs = lhsEval.get(i);
        i++;
        if (lhs instanceof IPatternObject) {
            if (lhs instanceof IPatternSequence) {
                return UNEVALED;
            }
            IPatternObject pattern = (IPatternObject) lhs;
            if (pattern.getSymbol() != null && !pattern.isPatternOptional()) {
                matched = matchPattern((IPatternObject) lhs, rhs, engine, stackMatcher);
                if (matched) {
                    if (!evaled) {
                        lhsPatternAST = lhsPattern.copyAppendable();
                        lhsEvalAST = lhsEval.copyAppendable();
                    }
                    lhsPatternAST.remove(iIndex);
                    lhsEvalAST.remove(iIndex);
                    evaled = true;
                    continue;
                }
                return null;
            }
        } else if (lhs.isFreeOfPatterns()) {
            if (lhs.equals(rhs)) {
                if (!evaled) {
                    lhsPatternAST = lhsPattern.copyAppendable();
                    lhsEvalAST = lhsEval.copyAppendable();
                }
                lhsPatternAST.remove(iIndex);
                lhsEvalAST.remove(iIndex);
                evaled = true;
                continue;
            }
            return null;
        }
        iIndex++;
    }
    if (!evaled) {
        return UNEVALED;
    }
    return new IAST[] { lhsPatternAST, lhsEvalAST };
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 8 with IPatternSequence

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

the class PatternMatcher method matchFlatAndFlatOrderlessAST.

private boolean matchFlatAndFlatOrderlessAST(final ISymbol sym, final IAST lhsPatternAST, final IAST lhsEvalAST, StackMatcher stackMatcher) {
    if ((sym.getAttributes() & ISymbol.ORDERLESS) == ISymbol.ORDERLESS) {
        if (lhsPatternAST.isAST1()) {
            // TODO check for OneIdentity?
            return matchExpr(lhsPatternAST.arg1(), lhsEvalAST, stackMatcher);
        }
        for (int i = 1; i < lhsPatternAST.size(); i++) {
            if (!(lhsPatternAST.get(i) instanceof IPatternObject)) {
                // try to find a matchin sub-expression
                for (int j = 1; j < lhsEvalAST.size(); j++) {
                    if (matchExpr(lhsPatternAST.get(i), lhsEvalAST.get(j))) {
                        if (matchFlatAndFlatOrderlessAST(sym, lhsPatternAST.removeAtClone(i), lhsEvalAST.removeAtClone(j), stackMatcher)) {
                            return true;
                        }
                    }
                }
                return false;
            }
        }
        FlatOrderlessStepVisitor visitor = new FlatOrderlessStepVisitor(sym, lhsPatternAST, lhsEvalAST, stackMatcher, fPatternMap);
        MultisetPartitionsIterator iter = new MultisetPartitionsIterator(visitor, lhsPatternAST.size() - 1);
        return !iter.execute();
    } else {
        if (lhsPatternAST.isAST1()) {
            if (lhsPatternAST.arg1().isPatternSequence()) {
                // TODO only the special case, where the last element is
                // a pattern sequence, is handled here
                IAST seq = F.Sequence();
                seq.appendAll(lhsEvalAST, 1, lhsEvalAST.size());
                if (((IPatternSequence) lhsPatternAST.arg1()).matchPatternSequence(seq, fPatternMap)) {
                    // }
                    return true;
                }
            }
            if (lhsPatternAST.size() == lhsEvalAST.size()) {
                return matchASTSequence(lhsPatternAST, lhsEvalAST, 0, stackMatcher);
            }
            return false;
        }
        FlatStepVisitor visitor = new FlatStepVisitor(sym, lhsPatternAST, lhsEvalAST, stackMatcher, fPatternMap);
        NumberPartitionsIterator iter = new NumberPartitionsIterator(visitor, lhsEvalAST.size() - 1, lhsPatternAST.size() - 1);
        return !iter.execute();
    }
}
Also used : NumberPartitionsIterator(org.matheclipse.combinatoric.NumberPartitionsIterator) MultisetPartitionsIterator(org.matheclipse.combinatoric.MultisetPartitionsIterator) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IAST(org.matheclipse.core.interfaces.IAST)

Example 9 with IPatternSequence

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

the class F method initPredefinedPatternSequence.

public static IPatternSequence initPredefinedPatternSequence(final ISymbol symbol, boolean zeroArgsAllowed) {
    PatternSequence temp = PatternSequence.valueOf(symbol, zeroArgsAllowed);
    PREDEFINED_PATTERNSEQUENCE_MAP.put(symbol.toString(), temp);
    return temp;
}
Also used : IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence)

Example 10 with IPatternSequence

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

the class AbstractAST method isFreeOfPatterns.

/**
 * {@inheritDoc}
 */
@Override
public final boolean isFreeOfPatterns() {
    final int evalFlags = getEvalFlags();
    if ((evalFlags & IAST.CONTAINS_NO_PATTERN) == IAST.CONTAINS_NO_PATTERN) {
        return true;
    }
    if ((evalFlags & IAST.CONTAINS_PATTERN_EXPR) != IAST.NO_FLAG) {
        return false;
    }
    if (isPatternMatchingFunction()) {
        addEvalFlags(IAST.CONTAINS_PATTERN);
        return false;
    }
    boolean isFreeOfPatterns = true;
    for (int i = 0; i < size(); i++) {
        // all elements including head element
        IExpr temp = get(i);
        if (temp.isAST() && !temp.isFreeOfPatterns()) {
            isFreeOfPatterns = false;
            addEvalFlags(((IAST) temp).getEvalFlags() & IAST.CONTAINS_PATTERN_EXPR);
            continue;
        } else if (temp instanceof IPatternObject) {
            isFreeOfPatterns = false;
            if (temp instanceof IPatternSequence) {
                if (temp.isPatternDefault()) {
                    addEvalFlags(IAST.CONTAINS_DEFAULT_PATTERN);
                }
                addEvalFlags(IAST.CONTAINS_PATTERN_SEQUENCE);
            } else {
                if (temp.isPatternDefault()) {
                    addEvalFlags(IAST.CONTAINS_DEFAULT_PATTERN);
                }
                addEvalFlags(IAST.CONTAINS_PATTERN);
            }
        }
    }
    if (isFreeOfPatterns) {
        addEvalFlags(IAST.CONTAINS_NO_PATTERN);
    }
    return isFreeOfPatterns;
}
Also used : IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

IPatternSequence (org.matheclipse.core.interfaces.IPatternSequence)11 IAST (org.matheclipse.core.interfaces.IAST)7 IExpr (org.matheclipse.core.interfaces.IExpr)5 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)5 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)4 ISymbol (org.matheclipse.core.interfaces.ISymbol)3 MultisetPartitionsIterator (org.matheclipse.combinatoric.MultisetPartitionsIterator)2 Serializable (java.io.Serializable)1 Comparator (java.util.Comparator)1 BiPredicate (java.util.function.BiPredicate)1 Predicate (java.util.function.Predicate)1 NumberPartitionsIterator (org.matheclipse.combinatoric.NumberPartitionsIterator)1 MultisetPartitionsIterator (org.matheclipse.core.combinatoric.MultisetPartitionsIterator)1 NumberPartitionsIterator (org.matheclipse.core.combinatoric.NumberPartitionsIterator)1 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 F (org.matheclipse.core.expression.F)1 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)1 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)1 IPattern (org.matheclipse.core.interfaces.IPattern)1 IRational (org.matheclipse.core.interfaces.IRational)1