use of org.matheclipse.core.visit.VisitorReplaceAllWithPatternFlags in project symja_android_library by axkr.
the class IPatternMap method substituteASTPatternOrSymbols.
/**
* Substitute all already find matchings in <code>lhsPatternExpr</code> and return the new pattern
* expression.
*
* @param lhsPatternExpr
* @param onlyNamedPatterns
* @return {@link F#NIL} if no substitution can be found.
*/
default IExpr substituteASTPatternOrSymbols(final IAST lhsPatternExpr, boolean onlyNamedPatterns) {
VisitorReplaceAllWithPatternFlags visitor = new VisitorReplaceAllWithPatternFlags(input -> {
if (input instanceof IPatternObject) {
if (onlyNamedPatterns && !(input instanceof Pattern)) {
return F.NIL;
}
IExpr symbolOrPatternObject = ((IPatternObject) input).getSymbol();
if (symbolOrPatternObject == null) {
if (onlyNamedPatterns) {
return F.NIL;
}
symbolOrPatternObject = input;
}
return substitute(symbolOrPatternObject);
}
return F.NIL;
}, onlyNamedPatterns);
IASTMutable result = F.NIL;
for (int i = 1; i < lhsPatternExpr.size(); i++) {
IExpr temp = lhsPatternExpr.get(i).accept(visitor);
if (temp.isPresent()) {
if (!result.isPresent()) {
result = lhsPatternExpr.setAtCopy(i, temp);
// result.setEvalFlags(lhsPatternExpr.getEvalFlags());
} else {
result.set(i, temp);
}
}
}
if (result.isPresent()) {
return EvalAttributes.simpleEval(result);
// if (result.isFlatAST()) {
// IASTMutable temp = EvalAttributes.flattenDeep((IAST) result);
// if (temp.isPresent()) {
// result = temp;
// }
// }
// // don't test for OneIdentity attribute here !
// if (result.isOrderlessAST()) {
// EvalAttributes.sort(result);
// }
// // set the eval flags
// result.isFreeOfPatterns();
// return result;
}
return F.NIL;
}
use of org.matheclipse.core.visit.VisitorReplaceAllWithPatternFlags in project symja_android_library by axkr.
the class IPatternMap method substitutePatternOrSymbols.
/**
* Substitute all patterns and symbols in the given expression with the current value of the
* corresponding internal pattern values arrays
*
* @param lhsPatternExpr left-hand-side expression which may contain pattern objects
* @param onlyNamedPatterns TODO
* @return <code>F.NIL</code> if substitutions isn't possible
*/
default IExpr substitutePatternOrSymbols(final IExpr lhsPatternExpr, boolean onlyNamedPatterns) {
VisitorReplaceAllWithPatternFlags visitor = new VisitorReplaceAllWithPatternFlags(input -> {
if (input instanceof IPatternObject) {
if (onlyNamedPatterns && !(input instanceof Pattern)) {
return F.NIL;
}
IExpr symbolOrPatternObject = ((IPatternObject) input).getSymbol();
if (symbolOrPatternObject == null) {
if (onlyNamedPatterns) {
return F.NIL;
}
symbolOrPatternObject = input;
}
return substitute(symbolOrPatternObject);
}
return F.NIL;
}, onlyNamedPatterns);
IExpr result = lhsPatternExpr.accept(visitor);
if (result.isPresent()) {
// set the eval flags
result.isFreeOfPatterns();
return result;
}
return lhsPatternExpr;
}
Aggregations