Search in sources :

Example 1 with IPatternMatcher

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

the class Collect method collectSingleVariable.

/**
	 * Collect terms in <code>expr</code> containing the same power expressions
	 * of <code>x</code>.
	 * 
	 * @param expr
	 * @param x
	 *            the current variable from the list of variables which should
	 *            be collected
	 * @param listOfVariables
	 *            list of variables which should be collected or
	 *            <code>null</code> if no list is available
	 * @param listPosition
	 *            position of the next variable in the list after <code>x</code>
	 *            which should be collected recursively
	 * @param head
	 *            the head which should be applied to each coefficient or
	 *            <code>null</code> if no head should be applied
	 * @param engine
	 *            the evaluation engine
	 * @return
	 */
private IExpr collectSingleVariable(IExpr expr, IExpr x, final IAST listOfVariables, final int listPosition, IExpr head, EvalEngine engine) {
    if (expr.isAST()) {
        Map<IExpr, IAST> map = new HashMap<IExpr, IAST>();
        IAST poly = (IAST) expr;
        IAST rest = F.PlusAlloc(poly.size());
        IPatternMatcher matcher = new PatternMatcher(x);
        collectToMap(poly, matcher, map, rest);
        if (listOfVariables != null && listPosition < listOfVariables.size()) {
            // collect next pattern in sub-expressions
            IAST result = F.PlusAlloc(map.size() + 1);
            if (rest.size() > 1) {
                result.append(collectSingleVariable(rest, listOfVariables.get(listPosition), listOfVariables, listPosition + 1, head, engine));
            }
            for (Entry<IExpr, IAST> entry : map.entrySet()) {
                IExpr temp = collectSingleVariable(entry.getValue().getOneIdentity(F.C0), listOfVariables.get(listPosition), listOfVariables, listPosition + 1, head, engine);
                result.append(F.Times(entry.getKey(), temp));
            }
            return result;
        }
        if (head != null) {
            IAST simplifyAST = F.unaryAST1(head, null);
            IExpr coefficient;
            for (int i = 1; i < rest.size(); i++) {
                simplifyAST.set(1, rest.get(i));
                coefficient = engine.evaluate(simplifyAST);
                rest.set(i, coefficient);
            }
            for (Map.Entry<IExpr, IAST> entry : map.entrySet()) {
                simplifyAST.set(1, entry.getValue());
                coefficient = engine.evaluate(simplifyAST);
                if (coefficient.isPlus()) {
                    rest.append(F.Times(entry.getKey()).appendOneIdentity((IAST) coefficient));
                } else {
                    rest.append(entry.getKey().times(coefficient));
                }
            }
        } else {
            IAST coefficient;
            for (IExpr key : map.keySet()) {
                coefficient = map.get(key);
                IAST times = F.TimesAlloc(2);
                times.append(key);
                times.appendOneIdentity(coefficient);
                rest.append(times);
            }
        }
        return rest.getOneIdentity(F.C0);
    }
    return expr;
}
Also used : HashMap(java.util.HashMap) 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) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with IPatternMatcher

use of org.matheclipse.core.patternmatching.IPatternMatcher 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 3 with IPatternMatcher

use of org.matheclipse.core.patternmatching.IPatternMatcher 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)

Example 4 with IPatternMatcher

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

the class Trace method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 2, 3);
    final IExpr temp = ast.arg1();
    IPatternMatcher matcher = null;
    if (ast.isAST2()) {
        matcher = engine.evalPatternMatcher(ast.arg2());
    }
    return engine.evalTrace(temp, matcher, F.List());
}
Also used : IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 5 with IPatternMatcher

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

the class VisitorReplacePart method visit.

@Override
public IExpr visit(IASTMutable ast) {
    IASTAppendable positionsToMatch = F.ast(S.Sequence);
    for (int j = 0; j < patternMatcherList.size(); j++) {
        IPatternMatcher matcher = patternMatcherList.get(j);
        IExpr lhs = matcher.getLHS();
        if (lhs.isAST(S.Sequence, 1)) {
            // empty sequence matches with complete expression
            return matcher.getRHS();
        }
    }
    return visitPatternIndexList(ast, positionsToMatch);
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IPatternMatcher(org.matheclipse.core.patternmatching.IPatternMatcher) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

IExpr (org.matheclipse.core.interfaces.IExpr)7 IPatternMatcher (org.matheclipse.core.patternmatching.IPatternMatcher)7 IAST (org.matheclipse.core.interfaces.IAST)4 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 PatternMatcher (org.matheclipse.core.patternmatching.PatternMatcher)3 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 Serializable (java.io.Serializable)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 BiPredicate (java.util.function.BiPredicate)1 Predicate (java.util.function.Predicate)1 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 ReturnException (org.matheclipse.core.eval.exception.ReturnException)1 F (org.matheclipse.core.expression.F)1 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)1 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)1 IInteger (org.matheclipse.core.interfaces.IInteger)1 IPattern (org.matheclipse.core.interfaces.IPattern)1