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;
}
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;
}
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;
}
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());
}
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);
}
Aggregations