use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class AbstractAST method isValue.
/** {@inheritDoc} */
@Override
public final boolean isValue() {
EvalEngine engine = EvalEngine.get();
ISymbol symbol = topHead();
IExpr result = engine.evalAttributes(symbol, this);
if (result.isPresent()) {
if (result.isAST(symbol)) {
return engine.evalRules(symbol, (IAST) result).isPresent();
}
return false;
}
return engine.evalRules(symbol, this).isPresent();
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class PatternMap method setValue.
public boolean setValue(IPatternSequence pattern, IAST sequence) {
ISymbol sym = pattern.getSymbol();
IExpr temp = pattern;
if (sym != null) {
temp = sym;
}
int indx = get(temp);
if (indx >= 0) {
fPatternValuesArray[indx] = sequence;
return true;
}
throw new IllegalStateException("Patternsequence:" + pattern + " is not available");
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class PatternMap method addSinglePattern.
protected void addSinglePattern(IPatternObject pattern) {
fRuleWithoutPattern = false;
this.fSymbolsArray = new IExpr[1];
this.fPatternValuesArray = new IExpr[1];
ISymbol sym = pattern.getSymbol();
if (sym != null) {
fSymbolsArray[0] = sym;
} else {
fSymbolsArray[0] = pattern;
}
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class PatternMatcher method matchAST.
protected boolean matchAST(final IAST lhsPatternAST, final IExpr lhsEvalExpr, StackMatcher stackMatcher) {
// "+lhsEvalExpr.toString());
if (lhsPatternAST.isAST(F.PatternTest, 3)) {
if (matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher)) {
return fPatternMap.isPatternTest(lhsPatternAST.arg1(), lhsPatternAST.arg2());
}
return false;
}
if (lhsPatternAST.isAST(F.Except, 2, 3)) {
if (lhsPatternAST.isAST2()) {
return !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher) && matchExpr(lhsPatternAST.arg2(), lhsEvalExpr, stackMatcher);
} else {
return !matchExpr(lhsPatternAST.arg1(), lhsEvalExpr, stackMatcher);
}
}
if (lhsEvalExpr instanceof IAST) {
if (!lhsPatternAST.isPatternExpr() && lhsPatternAST.equals(lhsEvalExpr)) {
return stackMatcher.matchRest();
}
final IAST lhsEvalAST = (IAST) lhsEvalExpr;
final ISymbol sym = lhsPatternAST.topHead();
if (lhsPatternAST.size() <= lhsEvalAST.size()) {
if ((lhsPatternAST.isFlatAST()) && sym.equals(lhsEvalAST.topHead()) && !(lhsPatternAST.isOrderlessAST() && lhsPatternAST.size() == lhsEvalAST.size())) {
if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
return false;
}
return matchFlatAndFlatOrderlessAST(sym, lhsPatternAST, lhsEvalAST, stackMatcher);
}
if (lhsPatternAST.size() < lhsEvalAST.size()) {
if (lhsPatternAST.isEvalFlagOn(IAST.CONTAINS_PATTERN_SEQUENCE)) {
if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
return false;
}
int lastPosition = lhsPatternAST.size() - 1;
if (lhsPatternAST.get(lastPosition).isAST(F.PatternTest, 3)) {
IAST patternTest = (IAST) lhsPatternAST.get(lastPosition);
if (patternTest.arg1().isPatternSequence()) {
// TODO only the special case, where the last
// element is
// a pattern sequence, is handled here
IAST seq = F.Sequence();
seq.appendAll(lhsEvalAST, lastPosition, lhsEvalAST.size());
if (((IPatternSequence) patternTest.arg1()).matchPatternSequence(seq, fPatternMap)) {
if (matchAST(lhsPatternAST.copyUntil(lastPosition), lhsEvalAST.copyUntil(lastPosition), stackMatcher)) {
return fPatternMap.isPatternTest(patternTest.arg1(), patternTest.arg2());
}
return false;
}
}
}
if (lhsPatternAST.get(lastPosition).isPatternSequence()) {
// TODO only the special case, where the last
// element is
// a pattern sequence, is handled here
IAST seq = F.Sequence();
seq.appendAll(lhsEvalAST.range(), lastPosition, lhsEvalAST.size());
if (((IPatternSequence) lhsPatternAST.get(lastPosition)).matchPatternSequence(seq, fPatternMap)) {
return matchAST(lhsPatternAST.copyUntil(lastPosition), lhsEvalAST.copyUntil(lastPosition), stackMatcher);
}
}
}
return false;
}
}
if (lhsPatternAST.size() != lhsEvalAST.size()) {
return false;
}
IExpr e1 = lhsPatternAST.head();
IExpr e2 = lhsEvalAST.head();
if (e1.isSymbol() && e2.isSymbol()) {
if (!e1.equals(e2)) {
return false;
}
} else {
// TODO create correct stack-matcher for the following call:
if (!matchExpr(lhsPatternAST.head(), lhsEvalAST.head())) {
return false;
}
}
if (lhsPatternAST.isOrderlessAST()) {
// only "pure Orderless" and "FlatOrderless with same size()"
// will be handled here:
OrderlessStepVisitor visitor = new OrderlessStepVisitor(sym, lhsPatternAST, lhsEvalAST, stackMatcher, fPatternMap, (sym.hasOneIdentityAttribute()) || // matching
(lhsPatternAST.size() == lhsEvalAST.size() && !sym.hasFlatAttribute()));
MultisetPartitionsIterator iter = new MultisetPartitionsIterator(visitor, lhsPatternAST.size() - 1);
return !iter.execute();
}
return matchASTSequence(lhsPatternAST, lhsEvalAST, 0, stackMatcher);
}
if (lhsPatternAST.isAST(F.Rational, 3) && lhsEvalExpr.isRational()) {
IRational numer = ((IRational) lhsEvalExpr).getNumerator();
IRational denom = ((IRational) lhsEvalExpr).getDenominator();
if (matchExpr(lhsPatternAST.arg1(), numer) && matchExpr(lhsPatternAST.arg2(), denom)) {
return true;
}
} else if (lhsPatternAST.isAST(F.Complex, 3) && lhsEvalExpr.isNumber()) {
ISignedNumber re = ((INumber) lhsEvalExpr).re();
ISignedNumber im = ((INumber) lhsEvalExpr).im();
if (matchExpr(lhsPatternAST.arg1(), re) && matchExpr(lhsPatternAST.arg2(), im)) {
return true;
}
}
return false;
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class RulesData method addSimpleOrderlessPatternDownRule.
private PatternMatcher addSimpleOrderlessPatternDownRule(final Set<ISymbol> headerSymbols, final IExpr leftHandSide, final PatternMatcher pmEvaluator) {
for (ISymbol head : headerSymbols) {
final int hash = head.hashCode();
if (F.isSystemInitialized && fSimpleOrderlesPatternDownRules.containsEntry(hash, pmEvaluator)) {
fSimpleOrderlesPatternDownRules.remove(hash, pmEvaluator);
}
fSimpleOrderlesPatternDownRules.put(hash, pmEvaluator);
}
return pmEvaluator;
}
Aggregations