Search in sources :

Example 1 with NumberPartitionsIterator

use of org.matheclipse.combinatoric.NumberPartitionsIterator 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)

Aggregations

MultisetPartitionsIterator (org.matheclipse.combinatoric.MultisetPartitionsIterator)1 NumberPartitionsIterator (org.matheclipse.combinatoric.NumberPartitionsIterator)1 IAST (org.matheclipse.core.interfaces.IAST)1 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)1 IPatternSequence (org.matheclipse.core.interfaces.IPatternSequence)1