I am using the tool sympy.parsing.mathematica
to parse Mathematica expressions into python syntax. I would like to be able to handle variable names that contain digits followed by letters.
For example, when parsing the string "1 + a23b + 4"
by calling mathematica("1 + a23b")
I get the output "a23*b + 1"
. How can I indicate that I want "a23b"
to be treated as a single variable, so that the output in the above example becomes "a23b + 1"
instead?
I tried passing a dictionary of the form {'a23b':'a23b'}
by calling mathematica("1 + a23b",{'a23b':'a23b'})
. However, this raises a ValueError
with the following message ValueError: 'a23b' function form is invalid.
.
Any ideas how to solve this?
Advertisement
Answer
In SymPy 1.11 the mathematica
parsing function is deprecated:
In [3]: from sympy.parsing.mathematica import mathematica In [4]: mathematica("1 + a23b") <ipython-input-4-925ed25e63e8>:1: SymPyDeprecationWarning: The ``mathematica`` function for the Mathematica parser is now deprecated. Use ``parse_mathematica`` instead. The parameter ``additional_translation`` can be replaced by SymPy's .replace( ) or .subs( ) methods on the output expression instead. See https://docs.sympy.org/latest/explanation/active-deprecations.html#mathematica-parser-new for details. This has been deprecated since SymPy version 1.11. It will be removed in a future version of SymPy. mathematica("1 + a23b") Out[4]: a₂₃⋅b + 1
Instead it is recommended to use the parse_mathematica
function which handles this case in the way that you want it to:
In [5]: from sympy.parsing.mathematica import parse_mathematica In [6]: parse_mathematica("1 + a23b") Out[6]: a23b + 1