Skip to content
Advertisement

Python: Handle digits followed by letters in variable names when using sympy.parsing.mathematica

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:

JavaScript

Instead it is recommended to use the parse_mathematica function which handles this case in the way that you want it to:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement