Search in sources :

Example 6 with PatternMatcher

use of org.matheclipse.core.patternmatching.PatternMatcher in project symja_android_library by axkr.

the class Exponent method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3, 4);
    final IExpr form = engine.evalPattern(ast.arg2());
    if (form.isList()) {
        return ((IAST) form).mapThread(ast, 2);
    }
    ISymbol sym = F.Max;
    if (ast.isAST3()) {
        final IExpr arg3 = engine.evaluate(ast.arg3());
        if (arg3.isSymbol()) {
            sym = (ISymbol) arg3;
        }
    }
    Set<IExpr> collector = new TreeSet<IExpr>();
    IExpr expr = F.evalExpandAll(ast.arg1());
    if (expr.isZero()) {
        collector.add(F.CNInfinity);
    } else if (expr.isAST()) {
        IAST arg1 = (IAST) expr;
        final IPatternMatcher matcher = new PatternMatcher(form);
        if (arg1.isPower()) {
            if (matcher.test(arg1.arg1())) {
                collector.add(arg1.arg2());
            } else {
                collector.add(F.C0);
            }
        } else if (arg1.isPlus()) {
            for (int i = 1; i < arg1.size(); i++) {
                if (arg1.get(i).isAtom()) {
                    if (arg1.get(i).isSymbol()) {
                        if (matcher.test(arg1.get(i))) {
                            collector.add(F.C1);
                        } else {
                            collector.add(F.C0);
                        }
                    } else {
                        collector.add(F.C0);
                    }
                } else if (arg1.get(i).isPower()) {
                    IAST pow = (IAST) arg1.get(i);
                    if (matcher.test(pow.arg1())) {
                        collector.add(pow.arg2());
                    } else {
                        collector.add(F.C0);
                    }
                } else if (arg1.get(i).isTimes()) {
                    timesExponent((IAST) arg1.get(i), matcher, collector);
                } else {
                    collector.add(F.C0);
                }
            }
        } else if (arg1.isTimes()) {
            timesExponent(arg1, matcher, collector);
        }
    } else if (expr.isSymbol()) {
        final PatternMatcher matcher = new PatternMatcher(form);
        if (matcher.test(expr)) {
            collector.add(F.C1);
        } else {
            collector.add(F.C0);
        }
    } else {
        collector.add(F.C0);
    }
    IAST result = F.ast(sym);
    if (collector.size() == 0) {
        collector.add(F.C0);
    }
    for (IExpr exponent : collector) {
        result.append(exponent);
    }
    return result;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) TreeSet(java.util.TreeSet) IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher)

Example 7 with PatternMatcher

use of org.matheclipse.core.patternmatching.PatternMatcher in project symja_android_library by axkr.

the class PatternsTest method testPriority001.

public void testPriority001() {
    IAST ast1 = ast(f);
    ast1.append(a_);
    IAST ast2 = ast(f);
    ast2.append(Times(a_, x_));
    PatternMatcher pm1 = new PatternMatcher(ast1);
    PatternMatcher pm2 = new PatternMatcher(ast2);
    int cpr = pm1.compareTo(pm2);
    assertEquals(cpr, 1);
}
Also used : IAST(org.matheclipse.core.interfaces.IAST) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher)

Example 8 with PatternMatcher

use of org.matheclipse.core.patternmatching.PatternMatcher in project symja_android_library by axkr.

the class BasicPatternPropertiesTestCase method checkPriority.

public void checkPriority(String patternString, int priority) {
    try {
        ASTNode node = fParser.parse(patternString);
        IExpr pat = AST2Expr.CONST.convert(node);
        PatternMatcher matcher = new PatternMatcher(pat);
        assertEquals(matcher.getPriority(), priority);
    } catch (Exception e) {
        e.printStackTrace();
        assertEquals(0, priority);
    }
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode) IExpr(org.matheclipse.core.interfaces.IExpr) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher)

Example 9 with PatternMatcher

use of org.matheclipse.core.patternmatching.PatternMatcher in project symja_android_library by axkr.

the class BasicPatternPropertiesTestCase method comparePriority.

public void comparePriority(String patternString1, String patternString2, int result) {
    try {
        ASTNode node = fParser.parse(patternString1);
        IExpr pat1 = AST2Expr.CONST.convert(node);
        PatternMatcher matcher1 = new PatternMatcher(pat1);
        node = fParser.parse(patternString2);
        IExpr pat2 = AST2Expr.CONST.convert(node);
        PatternMatcher matcher2 = new PatternMatcher(pat2);
        assertEquals(matcher1.compareTo(matcher2), result);
    } catch (Exception e) {
        e.printStackTrace();
        assertEquals(Integer.MAX_VALUE, result);
    }
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode) IExpr(org.matheclipse.core.interfaces.IExpr) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher)

Aggregations

PatternMatcher (org.matheclipse.core.patternmatching.PatternMatcher)9 IExpr (org.matheclipse.core.interfaces.IExpr)7 ASTNode (org.matheclipse.parser.client.ast.ASTNode)5 IAST (org.matheclipse.core.interfaces.IAST)4 IPatternMatcher (org.matheclipse.core.patternmatching.IPatternMatcher)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1