use of org.matheclipse.core.patternmatching.PatternMatcher in project symja_android_library by axkr.
the class PatternMatchingTestCase 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);
}
}
use of org.matheclipse.core.patternmatching.PatternMatcher in project symja_android_library by axkr.
the class PatternMatchingTestCase method checkPattern.
public void checkPattern(String patternString, String evalString, String resultString) {
try {
ASTNode node = fParser.parse(patternString);
IExpr pat = AST2Expr.CONST.convert(node);
node = fParser.parse(evalString);
IExpr eval = AST2Expr.CONST.convert(node);
PatternMatcher matcher = new PatternMatcher(pat);
if (matcher.test(eval)) {
ArrayList<IExpr> resultList = new ArrayList<IExpr>();
matcher.getPatterns(resultList, pat);
assertEquals(resultList.toString(), resultString);
return;
}
assertEquals("", resultString);
} catch (Exception e) {
e.printStackTrace();
assertEquals("", resultString);
}
}
use of org.matheclipse.core.patternmatching.PatternMatcher in project symja_android_library by axkr.
the class PatternMatchingTestCase method comparePriority.
public void comparePriority(String patternString1, String patternString2, int result) {
try {
ASTNode node = fParser.parse(patternString1);
IExpr pat1 = AST2Expr.CONST.convert(node);
node = fParser.parse(patternString1);
IExpr pat2 = AST2Expr.CONST.convert(node);
PatternMatcher matcher1 = new PatternMatcher(pat1);
PatternMatcher matcher2 = new PatternMatcher(pat2);
assertEquals(matcher1.compareTo(matcher2), result);
} catch (Exception e) {
e.printStackTrace();
assertEquals(Integer.MAX_VALUE, result);
}
}
use of org.matheclipse.core.patternmatching.PatternMatcher in project symja_android_library by axkr.
the class PatternsTest method testPriority002.
public void testPriority002() {
IAST ast1 = ast(f);
ast1.append(Times(a, x));
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);
}
use of org.matheclipse.core.patternmatching.PatternMatcher 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;
}
Aggregations