From 8ab726997e965a7b90481fbba481b5aabc58f6ec Mon Sep 17 00:00:00 2001 From: axexlck Date: Tue, 30 Apr 2024 04:28:41 +0200 Subject: [PATCH] POC #972 `FullDefiniton` better recursive collecting unprotected symbols --- .../matheclipse/core/expression/Symbol.java | 35 +++++++++++++------ .../core/system/FileFunctionsTest.java | 1 + 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/Symbol.java b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/Symbol.java index 7a90cd3757..04400de3a1 100644 --- a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/Symbol.java +++ b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/Symbol.java @@ -276,13 +276,9 @@ public IAST definition() { @Override public IAST fullDefinition() { Set symbolSet = new HashSet(); - IAST rules = definition(); - for (int i = 1; i < rules.size(); i++) { - IExpr rule = rules.get(i); - collectSymbolsRecursive(rule, symbolSet, x -> x.isSymbol() && !(x.isProtected())); - } + collectSymbolsRecursive(this, symbolSet, x -> x.isSymbol() && !(x.isProtected())); if (symbolSet.size() > 0) { - IASTAppendable fullDefinition = F.ListAlloc(rules.size() + symbolSet.size()); + IASTAppendable fullDefinition = F.ListAlloc(); Iterator iterator = symbolSet.iterator(); while (iterator.hasNext()) { ISymbol symbol = iterator.next(); @@ -299,17 +295,30 @@ public IAST fullDefinition() { return F.NIL; } + private static void collectSymbolsRecursive(ISymbol symbol, Set symbolSet, + Predicate predicate) { + final IAST rules = symbol.definition(); + for (int i = 1; i < rules.size(); i++) { + IExpr rule = rules.get(i); + collectSymbolsRecursive(rule, symbolSet, predicate); + } + } + private static void collectSymbolsRecursive(IExpr expr, Set symbolSet, Predicate predicate) { if (expr.isAST()) { IAST list = (IAST) expr; IExpr head = expr.head(); if (head.isSymbol()) { - if (predicate.test((ISymbol) head)) { - symbolSet.add((ISymbol) head); + final ISymbol symbol = (ISymbol) head; + if (predicate.test(symbol)) { + if (!symbolSet.contains(symbol)) { + symbolSet.add(symbol); + collectSymbolsRecursive(symbol, symbolSet, predicate); + } } } else { - collectSymbolsRecursive(head, symbolSet, x -> x.isSymbol()); + collectSymbolsRecursive(head, symbolSet, predicate); } for (int i = 1; i < list.size(); i++) { IExpr arg = list.getRule(i); @@ -317,8 +326,12 @@ private static void collectSymbolsRecursive(IExpr expr, Set symbolSet, } } else { if (expr.isSymbol()) { - if (predicate.test((ISymbol) expr)) { - symbolSet.add((ISymbol) expr); + final ISymbol symbol = (ISymbol) expr; + if (predicate.test(symbol)) { + if (!symbolSet.contains(symbol)) { + symbolSet.add(symbol); + collectSymbolsRecursive(symbol, symbolSet, predicate); + } } } } diff --git a/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/FileFunctionsTest.java b/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/FileFunctionsTest.java index bce9574cae..567c15d46a 100644 --- a/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/FileFunctionsTest.java +++ b/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/FileFunctionsTest.java @@ -11,6 +11,7 @@ public void testSave() { check("temp = FileNameJoin({$TemporaryDirectory, \"saved.txt\"});Print(temp);", // ""); check("g(x_) := x^3;"// + + "g(x_,y_) := f(x,y);"// + "SetAttributes(f, Listable);"// + "f(x_) := g(x^2);", // "");