Search in sources :

Example 1 with MultisetPartitionsIterator

use of org.matheclipse.core.combinatoric.MultisetPartitionsIterator in project symja_android_library by axkr.

the class CombinatoricTestCase method testRosenIterator.

public void testRosenIterator() {
    IAST lhsPatternAST = F.Plus(F.x_, F.y_, F.z_);
    IAST lhsEvalAST = F.Plus(F.a, F.b, F.c, F.d);
    PatternMatcher patternMatcher = new PatternMatcher(lhsPatternAST);
    IPatternMap patternMap = patternMatcher.createPatternMap();
    // StackMatcher stackMatcher = patternMatcher.new StackMatcher(EvalEngine.get());
    FlatOrderlessStepVisitor visitor = new FlatOrderlessStepVisitor(F.Plus, lhsPatternAST, lhsEvalAST, patternMatcher, patternMap);
    MultisetPartitionsIterator iter = new MultisetPartitionsIterator(visitor, lhsPatternAST.argSize());
    boolean b = false;
    while (!b) {
        b = iter.execute();
        if (!b) {
            System.out.println(iter.toString());
            iter.initPatternMap();
        }
    }
    assertEquals(true, b);
}
Also used : IPatternMap(org.matheclipse.core.patternmatching.IPatternMap) FlatOrderlessStepVisitor(org.matheclipse.core.patternmatching.FlatOrderlessStepVisitor) MultisetPartitionsIterator(org.matheclipse.core.combinatoric.MultisetPartitionsIterator) IAST(org.matheclipse.core.interfaces.IAST) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher)

Example 2 with MultisetPartitionsIterator

use of org.matheclipse.core.combinatoric.MultisetPartitionsIterator in project symja_android_library by axkr.

the class PatternMatcher method matchFlatOrderless.

/**
 * Match <code>Flat</code> and <code>Orderless</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 matchFlatOrderless(final ISymbol sym, IAST lhsPattern, IAST lhsEval, EvalEngine engine, StackMatcher stackMatcher) {
    if (lhsPattern.isAST1()) {
        return matchExpr(lhsPattern.arg1(), lhsEval, engine, stackMatcher);
    }
    IAST lhsPatternAST = lhsPattern;
    IAST lhsEvalAST = lhsEval;
    // removeOrderless already called a level up
    boolean matched = false;
    IExpr[] patternValues = fPatternMap.copyPattern();
    if (lhsPatternAST.size() <= 2) {
        try {
            if (lhsPatternAST.isAST1()) {
                matched = matchExpr(lhsPatternAST.arg1(), lhsEvalAST, engine, stackMatcher);
                return matched;
            }
            if (lhsPatternAST.isEmpty() && lhsEvalAST.size() > 1) {
                matched = false;
                return matched;
            }
            matched = stackMatcher.matchRest();
            return matched;
        } finally {
            if (!matched) {
                fPatternMap.resetPattern(patternValues);
            }
        }
    }
    lhsPattern = lhsPatternAST;
    lhsEval = lhsEvalAST;
    final IAST lhsPatternFinal = lhsPattern;
    final IAST lhsEvalFinal = lhsEval;
    for (int i = 1; i < lhsPatternFinal.size(); i++) {
        IExpr patternArg = lhsPatternFinal.get(i);
        if (!(patternArg instanceof IPatternObject)) {
            final int index = i;
            IAST reduced = lhsPatternFinal.splice(index);
            boolean evaled = false;
            for (int k = 1; k < lhsEvalFinal.size(); k++) {
                try {
                    IExpr evalArg = lhsEvalFinal.get(k);
                    if (!(patternArg.head() instanceof IPatternObject)) {
                        if (patternArg.isASTOrAssociation()) {
                            if ((((IAST) patternArg).getEvalFlags() & IAST.CONTAINS_DEFAULT_PATTERN) == IAST.CONTAINS_DEFAULT_PATTERN) {
                                continue;
                            }
                        }
                        if (patternArg.head().equals(evalArg.head()) && patternArg.isFree(x -> x.isOrderlessAST(), true)) {
                            evaled = true;
                            matched = matchExpr(patternArg, evalArg, engine, stackMatcher);
                        }
                        if (matched) {
                            matched = matchFlatAndFlatOrderless(sym, reduced, lhsEvalFinal.removeAtCopy(k), engine, stackMatcher);
                            if (matched) {
                                return true;
                            }
                        }
                    }
                } finally {
                    if (!matched) {
                        fPatternMap.resetPattern(patternValues);
                    }
                }
            }
            if (evaled) {
                return false;
            }
        }
    }
    FlatOrderlessStepVisitor visitor = new FlatOrderlessStepVisitor(sym, lhsPatternFinal, lhsEvalFinal, stackMatcher, fPatternMap, sym.hasFlatAttribute());
    MultisetPartitionsIterator iter = new MultisetPartitionsIterator(visitor, lhsPattern.argSize());
    return !iter.execute();
}
Also used : ResultException(org.matheclipse.core.eval.exception.ResultException) ObjectOutput(java.io.ObjectOutput) MultisetPartitionsIterator(org.matheclipse.core.combinatoric.MultisetPartitionsIterator) IAssociation(org.matheclipse.core.interfaces.IAssociation) IFraction(org.matheclipse.core.interfaces.IFraction) INumber(org.matheclipse.core.interfaces.INumber) NumberPartitionsIterator(org.matheclipse.core.combinatoric.NumberPartitionsIterator) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IPattern(org.matheclipse.core.interfaces.IPattern) ReturnException(org.matheclipse.core.eval.exception.ReturnException) ConditionException(org.matheclipse.core.eval.exception.ConditionException) EvalEngine(org.matheclipse.core.eval.EvalEngine) ID(org.matheclipse.core.expression.ID) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) Externalizable(java.io.Externalizable) IOException(java.io.IOException) ISymbol(org.matheclipse.core.interfaces.ISymbol) S(org.matheclipse.core.expression.S) List(java.util.List) PatternNested(org.matheclipse.core.expression.PatternNested) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) Logger(org.apache.logging.log4j.Logger) IExpr(org.matheclipse.core.interfaces.IExpr) ObjectInput(java.io.ObjectInput) ArrayDeque(java.util.ArrayDeque) LogManager(org.apache.logging.log4j.LogManager) MultisetPartitionsIterator(org.matheclipse.core.combinatoric.MultisetPartitionsIterator) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 3 with MultisetPartitionsIterator

use of org.matheclipse.core.combinatoric.MultisetPartitionsIterator in project symja_android_library by axkr.

the class PatternMatcher method matchAST.

protected boolean matchAST(IAST lhsPatternAST, final IExpr lhsEvalExpr, EvalEngine engine, StackMatcher stackMatcher) {
    if (lhsEvalExpr instanceof IAST) {
        if (lhsPatternAST.isFreeOfPatterns() && lhsPatternAST.equals(lhsEvalExpr)) {
            return stackMatcher.matchRest();
        }
        IAST lhsEvalAST = (IAST) lhsEvalExpr;
        final ISymbol sym = lhsPatternAST.topHead();
        if (lhsPatternAST.size() <= lhsEvalAST.size()) {
            if (lhsPatternAST.isOrderlessAST()) {
                IExpr temp = fPatternMap.substituteASTPatternOrSymbols(lhsPatternAST, false).orElse(lhsPatternAST);
                if (temp.isAST(lhsPatternAST.head())) {
                    lhsPatternAST = (IAST) temp;
                    IAST[] removed = removeOrderless(lhsPatternAST, lhsEvalAST);
                    if (removed == null) {
                        return false;
                    }
                    lhsPatternAST = removed[0];
                    lhsEvalAST = removed[1];
                }
            } else if (lhsPatternAST.isFlatAST()) {
                IExpr temp = fPatternMap.substituteASTPatternOrSymbols(lhsPatternAST, false).orElse(lhsPatternAST);
                if (temp.isAST(lhsPatternAST.head())) {
                    IAST[] removed = removeFlat((IAST) temp, lhsEvalAST);
                    if (removed == null) {
                        return false;
                    }
                    lhsPatternAST = removed[0];
                    lhsEvalAST = removed[1];
                }
            }
            if ((lhsPatternAST.isFlatAST()) && sym.equals(lhsEvalAST.topHead()) && !(lhsPatternAST.isOrderlessAST() && lhsPatternAST.size() == lhsEvalAST.size())) {
                if (!matchHeads(lhsPatternAST, lhsEvalAST, engine)) {
                    return false;
                }
                if (lhsPatternAST.size() == 1 && lhsEvalAST.size() == 1) {
                    return stackMatcher.matchRest();
                }
                return matchFlatAndFlatOrderless(sym, lhsPatternAST, lhsEvalAST, engine, stackMatcher);
            }
        }
        int lhsEvalSize = lhsEvalAST.size();
        if (lhsPatternAST.isEvalFlagOn(IAST.CONTAINS_PATTERN_SEQUENCE)) {
            if (!matchHeads(lhsPatternAST, lhsEvalAST, engine)) {
                return false;
            }
            if (lhsPatternAST.isEmpty() && lhsEvalAST.isEmpty()) {
                return stackMatcher.matchRest();
            }
            final int lastPosition = lhsPatternAST.argSize();
            if (lastPosition == 1 && lhsPatternAST.get(lastPosition).isAST(S.PatternTest, 3)) {
                if (lhsPatternAST.size() <= lhsEvalSize) {
                    IAST patternTest = (IAST) lhsPatternAST.get(lastPosition);
                    if (patternTest.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(lhsEvalAST, lastPosition, lhsEvalSize);
                        if (((IPatternSequence) patternTest.arg1()).matchPatternSequence(seq, fPatternMap, lhsPatternAST.topHead())) {
                            if (matchAST(lhsPatternAST.removeFromEnd(lastPosition), lhsEvalAST.removeFromEnd(lastPosition), engine, stackMatcher)) {
                                return fPatternMap.isPatternTest(patternTest.arg1(), patternTest.arg2(), engine);
                            }
                            return false;
                        }
                    }
                }
            } else if (lhsPatternAST.size() > 1 && lhsPatternAST.arg1().isPatternSequence(false)) {
                IPatternSequence patternSequence = (IPatternSequence) lhsPatternAST.arg1();
                return matchBlankSequence(patternSequence, lhsPatternAST, 1, lhsEvalAST, engine, stackMatcher);
            } else {
                if (lhsPatternAST.size() > 1 && lhsEvalSize > 1) {
                    if (matchExpr(lhsPatternAST.arg1(), lhsEvalAST.arg1(), engine)) {
                        return matchAST(lhsPatternAST.rest().addEvalFlags(IAST.CONTAINS_PATTERN_SEQUENCE), lhsEvalAST.rest(), engine, stackMatcher);
                    }
                }
            }
            return false;
        }
        if (lhsPatternAST.size() != lhsEvalSize || !matchHeads(lhsPatternAST, lhsEvalAST, engine)) {
            return false;
        }
        if (lhsPatternAST.isOrderlessAST() && lhsPatternAST.size() > 2) {
            // only "pure Orderless" and "FlatOrderless with same size()" will be handled here:
            OrderlessStepVisitor visitor = new OrderlessStepVisitor(sym, lhsPatternAST, lhsEvalAST, stackMatcher, fPatternMap, (sym.hasOneIdentityAttribute() || sym.hasFlatAttribute()) || // same size ==> use OneIdentity in pattern matching
            (lhsPatternAST.size() == lhsEvalSize && !sym.hasFlatAttribute()));
            MultisetPartitionsIterator iter = new MultisetPartitionsIterator(visitor, lhsPatternAST.argSize());
            return !iter.execute();
        }
        return matchASTSequence(lhsPatternAST, lhsEvalAST, 0, engine, stackMatcher);
    }
    return false;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) MultisetPartitionsIterator(org.matheclipse.core.combinatoric.MultisetPartitionsIterator) IPatternSequence(org.matheclipse.core.interfaces.IPatternSequence) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

MultisetPartitionsIterator (org.matheclipse.core.combinatoric.MultisetPartitionsIterator)3 IAST (org.matheclipse.core.interfaces.IAST)3 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 IPatternSequence (org.matheclipse.core.interfaces.IPatternSequence)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 Externalizable (java.io.Externalizable)1 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 ArrayDeque (java.util.ArrayDeque)1 List (java.util.List)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 NumberPartitionsIterator (org.matheclipse.core.combinatoric.NumberPartitionsIterator)1 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 ConditionException (org.matheclipse.core.eval.exception.ConditionException)1 ResultException (org.matheclipse.core.eval.exception.ResultException)1 ReturnException (org.matheclipse.core.eval.exception.ReturnException)1 F (org.matheclipse.core.expression.F)1