Skip to content

Commit

Permalink
new Flag IAST#CONTAINS_ALL_DEFAULT_PATTERN
Browse files Browse the repository at this point in the history
- the flag `IAST#CONTAINS_ALL_DEFAULT_PATTERN` signals that all args of
an IAST are default or optional patterns
- match patterns like for example `MatchQ(x, ( a_. + b_. ) )` -> `True`
  • Loading branch information
axkr committed Nov 25, 2024
1 parent e9b68ab commit e94a845
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3464,13 +3464,15 @@ public final boolean isFreeOfPatterns() {
return false;
}
boolean isFreeOfPatterns = true;
int defaultOrOptionalCounter = 0;
for (int i = 0; i < size(); i++) {
// all elements including head element
IExpr temp = getRule(i);
if (temp.isASTOrAssociation() && !temp.isFreeOfPatterns()) {
isFreeOfPatterns = false;
if (temp.isOptional()) {
addEvalFlags(IAST.CONTAINS_DEFAULT_PATTERN | IAST.CONTAINS_PATTERN);
defaultOrOptionalCounter++;
} else {
addEvalFlags(((IAST) temp).getEvalFlags() & IAST.CONTAINS_PATTERN_EXPR);
}
Expand All @@ -3480,12 +3482,14 @@ public final boolean isFreeOfPatterns() {
if (temp instanceof IPatternSequence) {
if (temp.isPatternDefault()) {
addEvalFlags(IAST.CONTAINS_DEFAULT_PATTERN | IAST.CONTAINS_PATTERN_SEQUENCE);
defaultOrOptionalCounter++;
} else {
addEvalFlags(IAST.CONTAINS_PATTERN_SEQUENCE);
}
} else {
if (temp.isPatternDefault()) {
addEvalFlags(IAST.CONTAINS_DEFAULT_PATTERN | IAST.CONTAINS_PATTERN);
defaultOrOptionalCounter++;
} else {
addEvalFlags(IAST.CONTAINS_PATTERN);
}
Expand All @@ -3494,6 +3498,10 @@ public final boolean isFreeOfPatterns() {
}
if (isFreeOfPatterns) {
addEvalFlags(IAST.CONTAINS_NO_PATTERN);
} else {
if (defaultOrOptionalCounter == argSize()) {
addEvalFlags(IAST.CONTAINS_ALL_DEFAULT_PATTERN);
}
}
return isFreeOfPatterns;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,17 @@ public enum PROPERTY {
public static final int BUILT_IN_EVALED = 0x00040000;

/**
* One of the arguments of the list contains a pattern object which can be set to a default value.
* One of the arguments of the list contains a pattern object which can be set to a default value
* (or optional value).
*/
public static final int CONTAINS_DEFAULT_PATTERN = 0x0004;

/**
* All of the arguments of the list are pattern objects which can be set to a default value (or
* optional value).
*/
public static final int CONTAINS_ALL_DEFAULT_PATTERN = 0x10000000;

/** Negative flag mask for CONTAINS_DEFAULT_PATTERN */
public static final int CONTAINS_NO_DEFAULT_PATTERN_MASK = 0xFFFB;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3183,7 +3183,8 @@ static IPatternMap determinePatterns(final IExpr lhsPatternExpr, int[] priority,
return new PatternMap0();
}

public static IPatternMap createSymbolToValueMap(List<GenericPair<IExpr, ISymbol>> patternIndexMap) {
public static IPatternMap createSymbolToValueMap(
List<GenericPair<IExpr, ISymbol>> patternIndexMap) {
int size = patternIndexMap.size();
switch (size) {
case 1:
Expand Down Expand Up @@ -3374,18 +3375,24 @@ private static int determinePatternsRecursive(
determinePatternsRecursive(condition, patternIndexMap, dummyPriority, ruleWithoutPattern,
listEvalFlags, treeLevel);
} else {
// int[] dummyPriority = new int[] {IPatternMap.DEFAULT_RULE_PRIORITY};
// determinePatternsRecursive(lhsPatternExpr, patternIndexMap, dummyPriority,
// ruleWithoutPattern,
// listEvalFlags, treeLevel);

// get the patterns from right argument to left argument, to get the pattern x_ in D(f,x_) and
// Integrate(f,x_) as first pattern in the pattern map
for (int i = lhsPatternExpr.size() - 1; i >= 0; i--) {
determinePatternsRecursive(i == 0 ? lhsPatternExpr.getValue(i) : lhsPatternExpr.getRule(i),
patternIndexMap, priority, ruleWithoutPattern, listEvalFlags, treeLevel);
}
// lhsPatternExpr.forEachRule(x -> determinePatternsRecursive(x, patternIndexMap, priority,
// ruleWithoutPattern, listEvalFlags, treeLevel), 0);
}
lhsPatternExpr.setEvalFlags(listEvalFlags[0]);
// disable flag "pattern with default value"
// listEvalFlags &= IAST.CONTAINS_NO_DEFAULT_PATTERN_MASK;
if (lhsPatternExpr.size() > 1 //
&& ((listEvalFlags[0] & IAST.CONTAINS_DEFAULT_PATTERN) == IAST.CONTAINS_DEFAULT_PATTERN)//
&& lhsPatternExpr.forAll(IExpr::isPatternDefault)) {
lhsPatternExpr.addEvalFlags(IAST.CONTAINS_ALL_DEFAULT_PATTERN);
}
return listEvalFlags[0];
}

Expand All @@ -3401,7 +3408,11 @@ private static void determinePatternsRecursive(final IExpr x,
ruleWithoutPattern, treeLevel + 1);
priority[0] -= 11;
if (x.isPatternDefault()) {
listEvalFlags[0] |= IAST.CONTAINS_DEFAULT_PATTERN;
listEvalFlags[0] |= IAST.CONTAINS_DEFAULT_PATTERN | IAST.CONTAINS_ALL_DEFAULT_PATTERN;
} else if (lhsPatternAST.size() > 1 //
&& ((listEvalFlags[0] & IAST.CONTAINS_DEFAULT_PATTERN) == IAST.CONTAINS_DEFAULT_PATTERN) //
&& lhsPatternAST.forAll(IExpr::isPatternDefault)) {
lhsPatternAST.addEvalFlags(IAST.CONTAINS_ALL_DEFAULT_PATTERN);
}
} else if (x instanceof IPatternObject) {
ruleWithoutPattern[0] = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,23 +881,25 @@ private boolean matchASTExpr(IAST lhsPatternAST, final IExpr lhsEvalExpr, EvalEn
if (head.isSymbol()) {
ISymbol patternHead = (ISymbol) head;
IExpr evalHead = lhsEvalExpr.head();
// if (patternHead.hasOneIdentityAttribute() && lhsPatternAST.isOrderlessAST()) {
// if (patternHead.equals(evalHead) && lhsEvalExpr.isAST()) {
// if (lhsPatternAST.size() - 1 >= lhsEvalExpr.size()) {
// IAST lhsEvalAST = (IAST) lhsEvalExpr;
// int[] ignoredPositions = new int[lhsEvalAST.size()];
// matched = matchDefaultArgsRecursive(patternHead, evalHead, lhsPatternAST,
// lhsEvalAST, 1, patternValues, lastStackSize, ignoredPositions, engine,
// stackMatcher);
// }
// } else {
// IAST lhsEvalAST = F.unaryAST1(patternHead, lhsEvalExpr);
// int[] ignoredPositions = new int[lhsEvalAST.size()];
// matched =
// matchDefaultArgsRecursive(patternHead, evalHead, lhsPatternAST, lhsEvalAST, 1,
// patternValues, lastStackSize, ignoredPositions, engine, stackMatcher);
// }
// }
if ((lhsPatternAST.getEvalFlags()
& IAST.CONTAINS_ALL_DEFAULT_PATTERN) == IAST.CONTAINS_ALL_DEFAULT_PATTERN
&& patternHead.hasOneIdentityAttribute() && lhsPatternAST.isOrderlessAST()) {
if (patternHead.equals(evalHead) && lhsEvalExpr.isAST()) {
if (lhsPatternAST.size() - 1 >= lhsEvalExpr.size()) {
IAST lhsEvalAST = (IAST) lhsEvalExpr;
int[] ignoredPositions = new int[lhsEvalAST.size()];
matched = matchDefaultArgsRecursive(patternHead, evalHead, lhsPatternAST,
lhsEvalAST, 1, patternValues, lastStackSize, ignoredPositions, engine,
stackMatcher);
}
} else {
IAST lhsEvalAST = F.unaryAST1(patternHead, lhsEvalExpr);
int[] ignoredPositions = new int[lhsEvalAST.size()];
matched =
matchDefaultArgsRecursive(patternHead, evalHead, lhsPatternAST, lhsEvalAST, 1,
patternValues, lastStackSize, ignoredPositions, engine, stackMatcher);
}
}
if (!matched) {
IExpr temp = matchDefaultArgumentsAST(patternHead, lhsPatternAST, engine);
if (temp.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14211,12 +14211,12 @@ public void testMatchQRubi002() {
"{x,0}");
check("f( a_. + b_. ):={a,b}", //
"");
// check("f( x ) ", //
// "{x,0}");
// check("MatchQ(2+x, ( a_. + b_. ) )", //
// "True");
// check("MatchQ(x, ( a_. + b_. ) )", //
// "True");
check("f( x ) ", //
"{x,0}");
check("MatchQ(2+x, ( a_. + b_. ) )", //
"True");
check("MatchQ(x, ( a_. + b_. ) )", //
"True");
}

@Test
Expand Down

0 comments on commit e94a845

Please sign in to comment.