Skip to content

Commit

Permalink
Misc tweaks...
Browse files Browse the repository at this point in the history
(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
  • Loading branch information
rocky committed Aug 19, 2024
1 parent 81499bd commit 686d375
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
11 changes: 11 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
CHANGES
=======

New Builtins
++++++++++++

* ``SetEnvironment``

Compatibility
-------------

* ``GetEnvironment`` expanded to handle ``[]`` and ``{var1, var2,...}`` forms


7.0.0
-----

Expand Down
24 changes: 15 additions & 9 deletions mathics/builtin/numbers/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/numbers/numbertheory.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ class PartitionsP(SympyFunction):
)

def eval(self, n, evaluation: Evaluation):
"PartitionsP[n_Integer]"
"""PartitionsP[n_Integer]"""
return super().eval(n, evaluation)


Expand Down
4 changes: 1 addition & 3 deletions mathics/core/convert/sympy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion mathics/core/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_"


Expand Down
6 changes: 3 additions & 3 deletions test/core/test_sympy_python_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 686d375

Please sign in to comment.