Search in sources :

Example 51 with ISymbol

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

the class AbstractAST method isValue.

/** {@inheritDoc} */
@Override
public final boolean isValue() {
    EvalEngine engine = EvalEngine.get();
    ISymbol symbol = topHead();
    IExpr result = engine.evalAttributes(symbol, this);
    if (result.isPresent()) {
        if (result.isAST(symbol)) {
            return engine.evalRules(symbol, (IAST) result).isPresent();
        }
        return false;
    }
    return engine.evalRules(symbol, this).isPresent();
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 52 with ISymbol

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

the class PatternMap method setValue.

public boolean setValue(IPatternSequence pattern, IAST sequence) {
    ISymbol sym = pattern.getSymbol();
    IExpr temp = pattern;
    if (sym != null) {
        temp = sym;
    }
    int indx = get(temp);
    if (indx >= 0) {
        fPatternValuesArray[indx] = sequence;
        return true;
    }
    throw new IllegalStateException("Patternsequence:" + pattern + " is not available");
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 53 with ISymbol

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

the class PatternMap method addSinglePattern.

protected void addSinglePattern(IPatternObject pattern) {
    fRuleWithoutPattern = false;
    this.fSymbolsArray = new IExpr[1];
    this.fPatternValuesArray = new IExpr[1];
    ISymbol sym = pattern.getSymbol();
    if (sym != null) {
        fSymbolsArray[0] = sym;
    } else {
        fSymbolsArray[0] = pattern;
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol)

Example 54 with ISymbol

use of org.matheclipse.core.interfaces.ISymbol 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 55 with ISymbol

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

the class RulesData method addSimpleOrderlessPatternDownRule.

private PatternMatcher addSimpleOrderlessPatternDownRule(final Set<ISymbol> headerSymbols, final IExpr leftHandSide, final PatternMatcher pmEvaluator) {
    for (ISymbol head : headerSymbols) {
        final int hash = head.hashCode();
        if (F.isSystemInitialized && fSimpleOrderlesPatternDownRules.containsEntry(hash, pmEvaluator)) {
            fSimpleOrderlesPatternDownRules.remove(hash, pmEvaluator);
        }
        fSimpleOrderlesPatternDownRules.put(hash, pmEvaluator);
    }
    return pmEvaluator;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol)

Aggregations

ISymbol (org.matheclipse.core.interfaces.ISymbol)117 IExpr (org.matheclipse.core.interfaces.IExpr)79 IAST (org.matheclipse.core.interfaces.IAST)76 IInteger (org.matheclipse.core.interfaces.IInteger)18 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)12 INum (org.matheclipse.core.interfaces.INum)10 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)10 IFraction (org.matheclipse.core.interfaces.IFraction)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IComplex (org.matheclipse.core.interfaces.IComplex)7 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 ArrayList (java.util.ArrayList)6 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)6 HashSet (java.util.HashSet)5 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 Num (org.matheclipse.core.expression.Num)5 GenPolynomial (edu.jas.poly.GenPolynomial)4 Options (org.matheclipse.core.eval.util.Options)4 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)4 ExpVector (edu.jas.poly.ExpVector)3