Search in sources :

Example 1 with IPatternSequence

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

the class PatternSequence method equivalent.

/**
	 * Check if the two left-hand-side pattern expressions are equivalent. (i.e.
	 * <code>f[x_,y_]</code> is equivalent to <code>f[a_,b_]</code> )
	 * 
	 * @param patternExpr2
	 * @param pm1
	 * @param pm2
	 * @return
	 */
@Override
public boolean equivalent(final IPatternObject patternExpr2, final PatternMap pm1, PatternMap pm2) {
    if (this == patternExpr2) {
        return true;
    }
    if (patternExpr2 instanceof PatternSequence) {
        // test if the pattern indices are equal
        final IPatternSequence p2 = (IPatternSequence) patternExpr2;
        if (getIndex(pm1) != p2.getIndex(pm2)) {
            return false;
        }
        // test if the "check" expressions are equal
        final Object o1 = getCondition();
        final Object o2 = p2.getCondition();
        if ((o1 == null) || (o2 == null)) {
            return o1 == o2;
        }
        return o1.equals(o2);
    }
    return false;
}
Also used : IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IPatternObject(org.matheclipse.core.interfaces.IPatternObject)

Example 2 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(@Nonnull final ISymbol symbol) {
    PatternSequence temp = PatternSequence.valueOf(symbol);
    PREDEFINED_PATTERNSEQUENCE_MAP.put(symbol.toString(), temp);
    return temp;
}
Also used : IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence)

Example 3 with IPatternSequence

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

the class PatternMatcher method matchAST.

protected boolean matchAST(final IAST lhsPatternAST, final IExpr lhsEvalExpr, StackMatcher stackMatcher) {
    // "+lhsEvalExpr.toString());
    if (lhsPatternAST.isAST(F.PatternTest, 3)) {
        if (matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher)) {
            return fPatternMap.isPatternTest(lhsPatternAST.arg1(), lhsPatternAST.arg2());
        }
        return false;
    }
    if (lhsPatternAST.isAST(F.Except, 2, 3)) {
        if (lhsPatternAST.isAST2()) {
            return !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher) && matchExpr(lhsPatternAST.arg2(), lhsEvalExpr, stackMatcher);
        } else {
            return !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher);
        }
    }
    if (lhsEvalExpr instanceof IAST) {
        if (!lhsPatternAST.isPatternExpr() && lhsPatternAST.equals(lhsEvalExpr)) {
            return stackMatcher.matchRest();
        }
        final IAST lhsEvalAST = (IAST) lhsEvalExpr;
        final ISymbol sym = lhsPatternAST.topHead();
        if (lhsPatternAST.size() <= lhsEvalAST.size()) {
            if ((lhsPatternAST.isFlatAST()) && sym.equals(lhsEvalAST.topHead()) && !(lhsPatternAST.isOrderlessAST() && lhsPatternAST.size() == lhsEvalAST.size())) {
                if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
                    return false;
                }
                return matchFlatAndFlatOrderlessAST(sym, lhsPatternAST, lhsEvalAST, stackMatcher);
            }
            if (lhsPatternAST.size() < lhsEvalAST.size()) {
                if (lhsPatternAST.isEvalFlagOn(IAST.CONTAINS_PATTERN_SEQUENCE)) {
                    if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
                        return false;
                    }
                    int lastPosition = lhsPatternAST.size() - 1;
                    if (lhsPatternAST.get(lastPosition).isAST(F.PatternTest, 3)) {
                        IAST patternTest = (IAST) lhsPatternAST.get(lastPosition);
                        if (patternTest.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, lastPosition, lhsEvalAST.size());
                            if (((IPatternSequence) patternTest.arg1()).matchPatternSequence(seq, fPatternMap)) {
                                if (matchAST(lhsPatternAST.copyUntil(lastPosition), lhsEvalAST.copyUntil(lastPosition), stackMatcher)) {
                                    return fPatternMap.isPatternTest(patternTest.arg1(), patternTest.arg2());
                                }
                                return false;
                            }
                        }
                    }
                    if (lhsPatternAST.get(lastPosition).isPatternSequence()) {
                        // TODO only the special case, where the last
                        // element is
                        // a pattern sequence, is handled here
                        IAST seq = F.Sequence();
                        seq.appendAll(lhsEvalAST.range(), lastPosition, lhsEvalAST.size());
                        if (((IPatternSequence) lhsPatternAST.get(lastPosition)).matchPatternSequence(seq, fPatternMap)) {
                            return matchAST(lhsPatternAST.copyUntil(lastPosition), lhsEvalAST.copyUntil(lastPosition), stackMatcher);
                        }
                    }
                }
                return false;
            }
        }
        if (lhsPatternAST.size() != lhsEvalAST.size()) {
            return false;
        }
        IExpr e1 = lhsPatternAST.head();
        IExpr e2 = lhsEvalAST.head();
        if (e1.isSymbol() && e2.isSymbol()) {
            if (!e1.equals(e2)) {
                return false;
            }
        } else {
            // TODO create correct stack-matcher for the following call:
            if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
                return false;
            }
        }
        if (lhsPatternAST.isOrderlessAST()) {
            // only "pure Orderless" and "FlatOrderless with same size()"
            // will be handled here:
            OrderlessStepVisitor visitor = new OrderlessStepVisitor(sym, lhsPatternAST, lhsEvalAST, stackMatcher, fPatternMap, (sym.hasOneIdentityAttribute()) || // matching
            (lhsPatternAST.size() == lhsEvalAST.size() && !sym.hasFlatAttribute()));
            MultisetPartitionsIterator iter = new MultisetPartitionsIterator(visitor, lhsPatternAST.size() - 1);
            return !iter.execute();
        }
        return matchASTSequence(lhsPatternAST, lhsEvalAST, 0, stackMatcher);
    }
    if (lhsPatternAST.isAST(F.Rational, 3) && lhsEvalExpr.isRational()) {
        IRational numer = ((IRational) lhsEvalExpr).getNumerator();
        IRational denom = ((IRational) lhsEvalExpr).getDenominator();
        if (matchExpr(lhsPatternAST.arg1(), numer) && matchExpr(lhsPatternAST.arg2(), denom)) {
            return true;
        }
    } else if (lhsPatternAST.isAST(F.Complex, 3) && lhsEvalExpr.isNumber()) {
        ISignedNumber re = ((INumber) lhsEvalExpr).re();
        ISignedNumber im = ((INumber) lhsEvalExpr).im();
        if (matchExpr(lhsPatternAST.arg1(), re) && matchExpr(lhsPatternAST.arg2(), im)) {
            return true;
        }
    }
    return false;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) MultisetPartitionsIterator(org.matheclipse.combinatoric.MultisetPartitionsIterator) IRational(org.matheclipse.core.interfaces.IRational) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 4 with IPatternSequence

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

the class PatternSequence method equivalent.

/**
 * Check if the two left-hand-side pattern expressions are equivalent. (i.e. <code>f[x_,y_]</code>
 * is equivalent to <code>f[a_,b_]</code> )
 *
 * @param patternExpr2
 * @param pm1
 * @param pm2
 * @return
 */
@Override
public boolean equivalent(final IPatternObject patternExpr2, final IPatternMap pm1, IPatternMap pm2) {
    if (this == patternExpr2) {
        return true;
    }
    if (patternExpr2 instanceof PatternSequence) {
        // test if the pattern indices are equal
        final IPatternSequence p2 = (IPatternSequence) patternExpr2;
        if (getIndex(pm1) != p2.getIndex(pm2)) {
            return false;
        }
        // test if the "check" expressions are equal
        final Object o1 = getHeadTest();
        final Object o2 = p2.getHeadTest();
        if ((o1 == null) || (o2 == null)) {
            return o1 == o2;
        }
        return o1.equals(o2);
    }
    return false;
}
Also used : IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IPatternObject(org.matheclipse.core.interfaces.IPatternObject)

Example 5 with IPatternSequence

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

the class Predicates method toFreeQ.

/**
 * Convert the pattern into a pattern-matching predicate used in {@link F#FreeQ(IExpr, IExpr)}.
 * FreeQ does test for subsequences (MemberQ does not test for subsequences).
 *
 * @param pattern
 * @return
 * @see IExpr#isFree(Predicate, boolean)
 */
public static Predicate<IExpr> toFreeQ(IExpr pattern) {
    if (pattern.isSymbol() || pattern.isNumber() || pattern.isString()) {
        return x -> x.equals(pattern);
    }
    final IPatternMatcher matcher;
    if (pattern.isOrderlessAST() && pattern.isFreeOfPatterns()) {
        // append a BlankNullSequence[] to match the parts of an Orderless expression
        IPatternSequence blankNullRest = F.$ps(null, true);
        IASTAppendable newPattern = ((IAST) pattern).copyAppendable();
        newPattern.append(blankNullRest);
        matcher = new PatternMatcher(newPattern);
    } else {
        matcher = new PatternMatcher(pattern);
    }
    return matcher;
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) Predicate(java.util.function.Predicate) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) ISymbol(org.matheclipse.core.interfaces.ISymbol) Serializable(java.io.Serializable) BiPredicate(java.util.function.BiPredicate) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher) IExpr(org.matheclipse.core.interfaces.IExpr) IPattern(org.matheclipse.core.interfaces.IPattern) Comparator(java.util.Comparator) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IAST(org.matheclipse.core.interfaces.IAST) IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher)

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