use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class PatternMatcher method matchASTSpecialBuiltIn.
/**
* Test first if <code>functionID = lhsPatternAST.headID()</code> is a special pattern-matching
* construct (i.e. <code>
* Association, HoldPattern, Literal, Condition, Alternatives, Except, Complex, Rational, Optional, PatternTest, Verbatim
* </code>). If <code>true</code> evaluate the special pattern-matching construct otherwise
* continue with <code>lhsPatternAST</code> pattern matching.
*
* @param lhsPatternAST left-hand-side pattern AST
* @param lhsEvalExpr left-hand-side expression which should be matched by the pattern expression
* @param engine the evaluation engine
* @param stackMatcher a stack matcher
* @return
*/
private boolean matchASTSpecialBuiltIn(IAST lhsPatternAST, final IExpr lhsEvalExpr, EvalEngine engine, StackMatcher stackMatcher) {
int functionID = lhsPatternAST.headID();
if (functionID >= ID.Association && functionID <= ID.Verbatim) {
boolean matched = false;
if (lhsPatternAST.size() == 2) {
final IExpr[] patternValues;
switch(functionID) {
case ID.Association:
patternValues = fPatternMap.copyPattern();
try {
if (lhsEvalExpr.isAssociation()) {
IAST lhsPatternAssociation = lhsPatternAST;
// TODO set/determine pattern matching flags?
IASTMutable lhsPatternList = (IASTMutable) lhsPatternAssociation.normal(false);
lhsPatternList.set(0, S.Association);
IAssociation lhsEvalAssociation = (IAssociation) lhsEvalExpr;
IASTMutable lhsEvalList = lhsEvalAssociation.normal(false);
lhsEvalList.set(0, S.Association);
matched = matchExpr(lhsPatternList, lhsEvalList, engine, stackMatcher);
return matched;
}
matched = matchASTExpr(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
} finally {
if (!matched) {
fPatternMap.resetPattern(patternValues);
}
}
return matched;
case ID.Except:
patternValues = fPatternMap.copyPattern();
try {
matched = !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, engine, stackMatcher);
} finally {
if (!matched) {
fPatternMap.resetPattern(patternValues);
}
}
return matched;
case ID.HoldPattern:
case ID.Literal:
patternValues = fPatternMap.copyPattern();
try {
matched = matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, engine, stackMatcher);
} finally {
if (!matched) {
fPatternMap.resetPattern(patternValues);
}
}
return matched;
case ID.Optional:
return matchOptional(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
case ID.Verbatim:
return lhsPatternAST.arg1().equals(lhsEvalExpr);
default:
}
} else if (lhsPatternAST.size() == 3) {
if (functionID >= ID.Complex && functionID <= ID.Rational) {
final IExpr[] patternValues;
switch(functionID) {
case ID.Complex:
patternValues = fPatternMap.copyPattern();
try {
if (lhsEvalExpr.isNumber()) {
INumber number = (INumber) lhsEvalExpr;
matched = matchExpr(lhsPatternAST.arg1(), number.re(), engine, stackMatcher) && matchExpr(lhsPatternAST.arg2(), number.im(), engine, stackMatcher);
return matched;
}
matched = matchASTExpr(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
} finally {
if (!matched) {
fPatternMap.resetPattern(patternValues);
}
}
return matched;
case ID.Condition:
return matchCondition(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
case ID.Except:
patternValues = fPatternMap.copyPattern();
try {
matched = !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, engine, stackMatcher) && matchExpr(lhsPatternAST.arg2(), lhsEvalExpr, engine, stackMatcher);
} finally {
if (!matched) {
fPatternMap.resetPattern(patternValues);
}
}
return matched;
case ID.Optional:
return matchOptional(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
case ID.PatternTest:
patternValues = fPatternMap.copyPattern();
try {
final IExpr lhsPatternExpr = lhsPatternAST.arg1();
final IExpr patternTest = lhsPatternAST.arg2();
if (lhsPatternExpr instanceof IPatternObject && patternTest.isFreeOfPatterns()) {
// other pattern symbol
if (matchPattern((IPatternObject) lhsPatternExpr, lhsEvalExpr, engine, stackMatcher)) {
if (fPatternMap.isPatternTest(lhsPatternExpr, patternTest, engine)) {
matched = stackMatcher.matchRest();
}
}
} else if (matchExpr(lhsPatternExpr, lhsEvalExpr, engine, stackMatcher)) {
matched = fPatternMap.isPatternTest(lhsPatternExpr, patternTest, engine);
}
} finally {
if (!matched) {
fPatternMap.resetPattern(patternValues);
}
}
return matched;
case ID.Rational:
patternValues = fPatternMap.copyPattern();
try {
// check for fractions (and no integers) here to be compatible with MMA
if (lhsEvalExpr.isFraction()) {
IFraction rational = (IFraction) lhsEvalExpr;
matched = matchExpr(lhsPatternAST.arg1(), rational.numerator(), engine, stackMatcher) && matchExpr(lhsPatternAST.arg2(), rational.denominator(), engine, stackMatcher);
return matched;
}
matched = matchASTExpr(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
return matched;
} finally {
if (!matched) {
fPatternMap.resetPattern(patternValues);
}
}
default:
}
}
}
} else {
if (lhsPatternAST.isAlternatives()) {
return matchAlternatives(lhsPatternAST, lhsEvalExpr, engine);
}
}
return matchASTExpr(lhsPatternAST, lhsEvalExpr, engine, stackMatcher);
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class PolynomialHomogenizationNew method replaceTimes.
private IExpr replaceTimes(final IAST ast, final IExpr base, IExpr exp) {
IExpr first = exp.first();
if (first.isComplex() && ((IComplex) first).reRational().isZero()) {
IRational imPart = ((IComplex) first).imRational();
int exponent = imPart.toIntDefault();
if (exponent == Integer.MIN_VALUE) {
return replaceExpression(ast).orElse(ast);
} else if (exponent > 0) {
IASTMutable restExponent = ((IAST) exp).setAtCopy(1, F.CI);
return F.Power(replaceExpression(base.power(restExponent)), exponent);
}
return replaceExpression(ast);
}
int exponent = first.toIntDefault();
if (exponent == Integer.MIN_VALUE) {
return replaceExpression(ast);
} else if (exponent > 0) {
IExpr rest = exp.rest().oneIdentity1();
return F.Power(replaceExpression(base.power(rest)), exponent);
}
return replaceExpression(ast).orElse(ast);
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class PolynomialHomogenizationNew method determineTimes.
private void determineTimes(final IAST ast, final IExpr base, IAST timesExponent) {
IExpr first = timesExponent.first();
if (first.isComplex() && ((IComplex) first).reRational().isZero()) {
IRational pureImPart = ((IComplex) first).imRational();
int exponent = pureImPart.toIntDefault();
if (exponent == Integer.MIN_VALUE) {
replaceExpressionLCM(ast, F.C1);
return;
} else if (exponent > 0) {
IASTMutable restExponent = timesExponent.setAtCopy(1, F.CI);
replaceExpressionLCM(base.power(restExponent), F.C1);
return;
}
replaceExpressionLCM(ast, F.C1);
return;
}
int exponent = first.toIntDefault();
if (exponent == Integer.MIN_VALUE) {
replaceExpressionLCM(ast, F.C1);
return;
} else if (exponent > 0) {
IExpr rest = timesExponent.rest().oneIdentity1();
replaceExpressionLCM(base.power(rest), F.C1);
return;
}
replaceExpressionLCM(ast, F.C1);
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class PolynomialHomogenization method replaceTimes.
private IExpr replaceTimes(final IAST ast, final IExpr base, IExpr exp) {
IExpr first = exp.first();
if (first.isComplex() && ((IComplex) first).reRational().isZero()) {
IRational imPart = ((IComplex) first).imRational();
int exponent = imPart.toIntDefault();
if (exponent == Integer.MIN_VALUE) {
return replaceExpression(ast).orElse(ast);
} else if (exponent > 0) {
IASTMutable restExponent = ((IAST) exp).setAtCopy(1, F.CI);
return F.Power(replaceExpression(base.power(restExponent)), exponent);
}
return replaceExpression(ast);
}
int exponent = first.toIntDefault();
if (exponent == Integer.MIN_VALUE) {
return replaceExpression(ast);
} else if (exponent > 0) {
IExpr rest = exp.rest().oneIdentity1();
return F.Power(replaceExpression(base.power(rest)), exponent);
}
return replaceExpression(ast).orElse(ast);
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class PolynomialHomogenization method determineTimes.
private void determineTimes(final IAST ast, final IExpr base, IAST timesExponent) {
IExpr first = timesExponent.first();
if (first.isComplex() && ((IComplex) first).reRational().isZero()) {
IRational pureImPart = ((IComplex) first).imRational();
int exponent = pureImPart.toIntDefault();
if (exponent == Integer.MIN_VALUE) {
replaceExpressionLCM(ast, F.C1);
return;
} else if (exponent > 0) {
IASTMutable restExponent = timesExponent.setAtCopy(1, F.CI);
replaceExpressionLCM(base.power(restExponent), F.C1);
return;
}
replaceExpressionLCM(ast, F.C1);
return;
}
int exponent = first.toIntDefault();
if (exponent == Integer.MIN_VALUE) {
replaceExpressionLCM(ast, F.C1);
return;
} else if (exponent > 0) {
IExpr rest = timesExponent.rest().oneIdentity1();
replaceExpressionLCM(base.power(rest), F.C1);
return;
}
replaceExpressionLCM(ast, F.C1);
}
Aggregations