From 25f299a386fe2091288acf9e4750503d1df9aa1b Mon Sep 17 00:00:00 2001 From: rocky Date: Mon, 19 Aug 2024 06:24:15 -0400 Subject: [PATCH 1/2] Misc tweaks... (These were noticed in working on event tracing) `mathics/builtin/numbers/algebra.py`: black changes its autoformating `mathics/builtin/numbers/numbertheory.py`: some linting prefers triples quotes for docstrings `mathics/core/convert/sympy.py`: use accurate location for singleton class. This reduces unnecessary sympy imports `mathics/core/symbols.py`: shorten prefix and context names for SymPy variables. In tracing we will see these names a lot. A shorter prefix makes understanding expressions involving these easier to follow `CHANGES.rst`: document recent SetEnvironment addition and GetEnvironment changes `test/numbers/__init__.py`: needed if test/numbers is tested separately --- CHANGES.rst | 11 +++++++++++ mathics/builtin/numbers/algebra.py | 24 +++++++++++++++--------- mathics/builtin/numbers/numbertheory.py | 2 +- mathics/core/convert/sympy.py | 4 +--- mathics/core/symbols.py | 6 +++++- test/builtin/numbers/__init__.py | 0 test/core/test_sympy_python_convert.py | 6 +++--- 7 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 test/builtin/numbers/__init__.py diff --git a/CHANGES.rst b/CHANGES.rst index f0825b123..057e6594b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,6 +2,17 @@ CHANGES ======= +New Builtins +++++++++++++ + +* ``SetEnvironment`` + +Compatibility +------------- + +* ``GetEnvironment`` expanded to handle ``[]`` and ``{var1, var2,...}`` forms + + 7.0.0 ----- diff --git a/mathics/builtin/numbers/algebra.py b/mathics/builtin/numbers/algebra.py index 0d1b7d741..1a6df8541 100644 --- a/mathics/builtin/numbers/algebra.py +++ b/mathics/builtin/numbers/algebra.py @@ -231,9 +231,11 @@ def unconvert_subexprs(expr): elements = sub_expr.elements if target_pat: elements = [ - element - if element.is_free(target_pat, evaluation) - else _expand(element) + ( + element + if element.is_free(target_pat, evaluation) + else _expand(element) + ) for element in elements ] else: @@ -248,9 +250,11 @@ def unconvert_subexprs(expr): elements = sub_expr.elements if target_pat: elements = [ - element - if element.is_free(target_pat, evaluation) - else _expand(element) + ( + element + if element.is_free(target_pat, evaluation) + else _expand(element) + ) for element in elements ] else: @@ -335,9 +339,11 @@ def get_exponents_sorted(expr, var) -> list: # find exponent of terms multiplied with functions: sin, cos, log, exp, ... # e.g: x^3 * Sin[x^2] should give 3 muls = [ - term.as_coeff_mul(x)[1] - if term.as_coeff_mul(x)[1] - else (sympy.Integer(0),) + ( + term.as_coeff_mul(x)[1] + if term.as_coeff_mul(x)[1] + else (sympy.Integer(0),) + ) for term in coeff.as_ordered_terms() ] expos = [term.as_coeff_exponent(x)[1] for mul in muls for term in mul] diff --git a/mathics/builtin/numbers/numbertheory.py b/mathics/builtin/numbers/numbertheory.py index f718af00a..b227a77cf 100644 --- a/mathics/builtin/numbers/numbertheory.py +++ b/mathics/builtin/numbers/numbertheory.py @@ -508,7 +508,7 @@ class PartitionsP(SympyFunction): ) def eval(self, n, evaluation: Evaluation): - "PartitionsP[n_Integer]" + """PartitionsP[n_Integer]""" return super().eval(n, evaluation) diff --git a/mathics/core/convert/sympy.py b/mathics/core/convert/sympy.py index be34c247e..27cb5253c 100644 --- a/mathics/core/convert/sympy.py +++ b/mathics/core/convert/sympy.py @@ -8,9 +8,7 @@ import sympy from sympy import Symbol as Sympy_Symbol, false as SympyFalse, true as SympyTrue - -# Import the singleton class -from sympy.core.numbers import S +from sympy.core.singleton import S from mathics.core.atoms import ( MATHICS3_COMPLEX_I, diff --git a/mathics/core/symbols.py b/mathics/core/symbols.py index 1be4ce772..3aa20adb5 100644 --- a/mathics/core/symbols.py +++ b/mathics/core/symbols.py @@ -14,7 +14,11 @@ # I put this constants here instead of inside `mathics.core.convert.sympy` # to avoid a circular reference. Maybe they should be in its own module. -sympy_symbol_prefix = "_Mathics_User_" +# Prefix used for Sympy variables. +# We wan t this to be short to keep variable names short. +# In tracing values, long names make output messy. +sympy_symbol_prefix = "_m_" + sympy_slot_prefix = "_Mathics_Slot_" diff --git a/test/builtin/numbers/__init__.py b/test/builtin/numbers/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/test/core/test_sympy_python_convert.py b/test/core/test_sympy_python_convert.py index a6f4668e3..0c7f4778e 100644 --- a/test/core/test_sympy_python_convert.py +++ b/test/core/test_sympy_python_convert.py @@ -42,10 +42,10 @@ def compare(self, mathics_expr, sympy_expr, **kwargs): self.compare_to_mathics(mathics_expr, sympy_expr) def testSymbol(self): - self.compare(Symbol("Global`x"), sympy.Symbol("_Mathics_User_Global`x")) + self.compare(Symbol("Global`x"), sympy.Symbol("_mg`x")) self.compare( - Symbol("_Mathics_User_x"), - sympy.Symbol("_Mathics_User_System`_Mathics_User_x"), + Symbol("_mu_x"), + sympy.Symbol("_mu`x"), ) def testReal(self): From 000bfbbbacf2916dde47df76acd2a4cf461a0e04 Mon Sep 17 00:00:00 2001 From: rocky Date: Mon, 19 Aug 2024 09:15:26 -0400 Subject: [PATCH 2/2] Misc tweaks... mathics/builtin/numbers/algebra.py: black changes its autoformatting mathics/builtin/numbers/numbertheory.py: some linting prefers triples quotes for docstrings mathics/core/convert/sympy.py: use accurate location for singleton class. This reduces unnecessary sympy imports CHANGES.rst: document recent SetEnvironment addition and GetEnvironment changes test/builtin/numbers/__init__.py: needed if testing in this directory is done separately. --- mathics/core/symbols.py | 6 +----- test/core/test_sympy_python_convert.py | 6 +++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/mathics/core/symbols.py b/mathics/core/symbols.py index 3aa20adb5..1be4ce772 100644 --- a/mathics/core/symbols.py +++ b/mathics/core/symbols.py @@ -14,11 +14,7 @@ # I put this constants here instead of inside `mathics.core.convert.sympy` # to avoid a circular reference. Maybe they should be in its own module. -# Prefix used for Sympy variables. -# We wan t this to be short to keep variable names short. -# In tracing values, long names make output messy. -sympy_symbol_prefix = "_m_" - +sympy_symbol_prefix = "_Mathics_User_" sympy_slot_prefix = "_Mathics_Slot_" diff --git a/test/core/test_sympy_python_convert.py b/test/core/test_sympy_python_convert.py index 0c7f4778e..a6f4668e3 100644 --- a/test/core/test_sympy_python_convert.py +++ b/test/core/test_sympy_python_convert.py @@ -42,10 +42,10 @@ def compare(self, mathics_expr, sympy_expr, **kwargs): self.compare_to_mathics(mathics_expr, sympy_expr) def testSymbol(self): - self.compare(Symbol("Global`x"), sympy.Symbol("_mg`x")) + self.compare(Symbol("Global`x"), sympy.Symbol("_Mathics_User_Global`x")) self.compare( - Symbol("_mu_x"), - sympy.Symbol("_mu`x"), + Symbol("_Mathics_User_x"), + sympy.Symbol("_Mathics_User_System`_Mathics_User_x"), ) def testReal(self):