Skip to content

Commit 8ab7269

Browse files
committed
POC #972 FullDefiniton better recursive collecting unprotected symbols
1 parent 6b86bde commit 8ab7269

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/Symbol.java

+24-11
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,9 @@ public IAST definition() {
276276
@Override
277277
public IAST fullDefinition() {
278278
Set<ISymbol> symbolSet = new HashSet<ISymbol>();
279-
IAST rules = definition();
280-
for (int i = 1; i < rules.size(); i++) {
281-
IExpr rule = rules.get(i);
282-
collectSymbolsRecursive(rule, symbolSet, x -> x.isSymbol() && !(x.isProtected()));
283-
}
279+
collectSymbolsRecursive(this, symbolSet, x -> x.isSymbol() && !(x.isProtected()));
284280
if (symbolSet.size() > 0) {
285-
IASTAppendable fullDefinition = F.ListAlloc(rules.size() + symbolSet.size());
281+
IASTAppendable fullDefinition = F.ListAlloc();
286282
Iterator<ISymbol> iterator = symbolSet.iterator();
287283
while (iterator.hasNext()) {
288284
ISymbol symbol = iterator.next();
@@ -299,26 +295,43 @@ public IAST fullDefinition() {
299295
return F.NIL;
300296
}
301297

298+
private static void collectSymbolsRecursive(ISymbol symbol, Set<ISymbol> symbolSet,
299+
Predicate<ISymbol> predicate) {
300+
final IAST rules = symbol.definition();
301+
for (int i = 1; i < rules.size(); i++) {
302+
IExpr rule = rules.get(i);
303+
collectSymbolsRecursive(rule, symbolSet, predicate);
304+
}
305+
}
306+
302307
private static void collectSymbolsRecursive(IExpr expr, Set<ISymbol> symbolSet,
303308
Predicate<ISymbol> predicate) {
304309
if (expr.isAST()) {
305310
IAST list = (IAST) expr;
306311
IExpr head = expr.head();
307312
if (head.isSymbol()) {
308-
if (predicate.test((ISymbol) head)) {
309-
symbolSet.add((ISymbol) head);
313+
final ISymbol symbol = (ISymbol) head;
314+
if (predicate.test(symbol)) {
315+
if (!symbolSet.contains(symbol)) {
316+
symbolSet.add(symbol);
317+
collectSymbolsRecursive(symbol, symbolSet, predicate);
318+
}
310319
}
311320
} else {
312-
collectSymbolsRecursive(head, symbolSet, x -> x.isSymbol());
321+
collectSymbolsRecursive(head, symbolSet, predicate);
313322
}
314323
for (int i = 1; i < list.size(); i++) {
315324
IExpr arg = list.getRule(i);
316325
collectSymbolsRecursive(arg, symbolSet, predicate);
317326
}
318327
} else {
319328
if (expr.isSymbol()) {
320-
if (predicate.test((ISymbol) expr)) {
321-
symbolSet.add((ISymbol) expr);
329+
final ISymbol symbol = (ISymbol) expr;
330+
if (predicate.test(symbol)) {
331+
if (!symbolSet.contains(symbol)) {
332+
symbolSet.add(symbol);
333+
collectSymbolsRecursive(symbol, symbolSet, predicate);
334+
}
322335
}
323336
}
324337
}

symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/FileFunctionsTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public void testSave() {
1111
check("temp = FileNameJoin({$TemporaryDirectory, \"saved.txt\"});Print(temp);", //
1212
"");
1313
check("g(x_) := x^3;"//
14+
+ "g(x_,y_) := f(x,y);"//
1415
+ "SetAttributes(f, Listable);"//
1516
+ "f(x_) := g(x^2);", //
1617
"");

0 commit comments

Comments
 (0)