use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class UnaryBindIth method apply.
/**
* Clone the given AST and set the i-th argument of the new AST to {@code arg}
* .
*
* @param index
* the i-th index should be used
* @param arg
* the i-th argument in the new AST
*/
public IExpr apply(int index, final IExpr arg) {
final IAST ast = fConstant.copy();
ast.set(index, arg);
return ast;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class HashedOrderlessMatcher method evaluateRepeated.
/**
* Evaluate an <code>Orderless</code> AST with the defined
* <code>HashedPatternRules</code> as long as the header of the given
* expression equals the evaluated expression.
*
* @param orderlessAST
* @return
* @see HashedPatternRules
*/
public IAST evaluateRepeated(final IAST orderlessAST) {
if (orderlessAST.isEvalFlagOn(IAST.IS_HASH_EVALED)) {
return F.NIL;
}
IAST temp = orderlessAST;
if (checkMinimmumASTs(temp)) {
boolean evaled = false;
int[] hashValues;
if (!fPatternHashRuleMap.isEmpty()) {
IExpr head = orderlessAST.head();
IAST result = F.NIL;
while (temp.isPresent()) {
hashValues = new int[(temp.size() - 1)];
HashValueVisitor v = new HashValueVisitor();
for (int i = 0; i < hashValues.length; i++) {
hashValues[i] = temp.get(i + 1).accept(v);
v.setUp();
}
result = evaluateHashedValues(temp, fPatternHashRuleMap, hashValues);
if (result.isPresent()) {
temp = result;
evaled = true;
if (!temp.head().equals(head)) {
temp.setEvalFlags(IAST.IS_HASH_EVALED);
return temp;
}
} else {
break;
}
}
}
if (!fHashRuleMap.isEmpty()) {
hashValues = new int[(temp.size() - 1)];
for (int i = 0; i < hashValues.length; i++) {
hashValues[i] = temp.get(i + 1).head().hashCode();
}
IAST result = evaluateHashedValues(temp, fHashRuleMap, hashValues);
if (result.isPresent()) {
result.setEvalFlags(IAST.IS_HASH_EVALED);
return result;
}
}
if (evaled) {
temp.setEvalFlags(IAST.IS_HASH_EVALED);
return temp;
}
}
orderlessAST.setEvalFlags(IAST.IS_HASH_EVALED);
return F.NIL;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class HashedOrderlessMatcher method evaluateHashedValues.
private static IAST evaluateHashedValues(final IAST orderlessAST, OpenIntToList<AbstractHashedPatternRules> hashRuleMap, int[] hashValues) {
boolean evaled = false;
IAST result = orderlessAST.copyHead();
for (int i = 0; i < hashValues.length - 1; i++) {
if (hashValues[i] == 0) {
// already used entry
continue;
}
evaled: for (int j = i + 1; j < hashValues.length; j++) {
if (hashValues[j] == 0) {
// already used entry
continue;
}
final List<AbstractHashedPatternRules> hashRuleList = hashRuleMap.get(AbstractHashedPatternRules.calculateHashcode(hashValues[i], hashValues[j]));
if (hashRuleList != null) {
for (AbstractHashedPatternRules hashRule : hashRuleList) {
if (!hashRule.isPattern1() && !hashRule.isPattern2()) {
if (hashValues[i] != hashRule.getHash1() || hashValues[j] != hashRule.getHash2()) {
if (hashValues[i] != hashRule.getHash2() || hashValues[j] != hashRule.getHash1()) {
// hash code of both entries aren't matching
continue;
}
if (updateHashValues(result, orderlessAST, hashRule, hashValues, j, i)) {
evaled = true;
break evaled;
}
continue;
}
if (updateHashValues(result, orderlessAST, hashRule, hashValues, i, j)) {
evaled = true;
break evaled;
}
if (hashValues[i] != hashRule.getHash2() || hashValues[j] != hashRule.getHash1()) {
// hash code of both entries aren't matching
continue;
}
if (updateHashValues(result, orderlessAST, hashRule, hashValues, j, i)) {
evaled = true;
break evaled;
}
continue;
}
if (updateHashValues(result, orderlessAST, hashRule, hashValues, i, j)) {
evaled = true;
break evaled;
}
if (updateHashValues(result, orderlessAST, hashRule, hashValues, j, i)) {
evaled = true;
break evaled;
}
}
}
}
}
if (evaled) {
// append the rest of the unevaluated arguments
for (int i = 0; i < hashValues.length; i++) {
if (hashValues[i] != 0) {
result.append(orderlessAST.get(i + 1));
}
}
return result;
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class PatternMap method isPatternTest.
public boolean isPatternTest(IExpr expr, IExpr patternTest) {
IExpr temp = substitutePatternOrSymbols(expr);
if (temp == null) {
temp = expr;
}
IAST test = F.unaryAST1(patternTest, null);
EvalEngine engine = EvalEngine.get();
if (temp.isSequence()) {
for (int i = 1; i < ((IAST) temp).size(); i++) {
test.set(1, ((IAST) temp).get(i));
if (!engine.evalTrue(test)) {
return false;
}
}
return true;
}
test.set(1, temp);
if (!engine.evalTrue(test)) {
return false;
}
return true;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class PatternMap method determinePatternsRecursive.
/**
* Determine all patterns (i.e. all objects of instance IPattern) in the
* given expression
*
* Increments this classes pattern counter.
*
* @param patternIndexMap
* @param lhsPatternExpr
* the (left-hand-side) expression which could contain pattern
* objects.
* @param treeLevel
* TODO
*/
private int determinePatternsRecursive(Map<IExpr, Integer> patternIndexMap, final IAST lhsPatternExpr, int treeLevel) {
final IAST ast = lhsPatternExpr;
if (lhsPatternExpr.isAST(F.Except, 2, 3)) {
fRuleWithoutPattern = false;
}
int listEvalFlags = IAST.NO_FLAG;
for (int i = 0; i < ast.size(); i++) {
IExpr temp = ast.get(i);
if (temp.isAST()) {
listEvalFlags |= determinePatternsRecursive(patternIndexMap, (IAST) temp, treeLevel + 1);
fPriority -= 11;
} else if (temp instanceof IPatternObject) {
int[] result = ((IPatternObject) temp).addPattern(this, patternIndexMap);
listEvalFlags |= result[0];
fPriority -= result[1];
} else {
fPriority -= (50 - treeLevel);
}
}
ast.setEvalFlags(listEvalFlags);
// listEvalFlags &= IAST.CONTAINS_NO_DEFAULT_PATTERN_MASK;
return listEvalFlags;
}
Aggregations