WolframClient in Python: how to convert the output of wolframclient.evaluate into a sympy expression?

463 views Asked by At

I'm using wolframclient in Python and at certain point of my computation I have an output like the following

(Plus[-9, Times[2, Power[Global`y, 4]]], Plus[Times[3, Global`x], Times[-2, Power[Global`y, 3]]])

Now I need to convert this output into sympy format, something like

    [-9 + 2*y**4, 3*x - 2*y**3]

Is there a way to do that?

Thanks, Luca

1

There are 1 answers

3
smichr On

The mathematica parser doesn't seem to like the tuple, so if you can get the two parts separate and replace the "Global`" with a null string, it can work:

>>> a = 'Plus[-9, Times[2, Power[Global`y, 4]]]'
>>> b = 'Plus[Times[3, Global`x], Times[-2, Power[Global`y, 3]]]'
>>> from sympy.parsing.mathematica import parse_mathematica as p # in current dev version?
>>> [p(i.replace('Global`','')) for i in (a,b)]
[2*y**4 - 9, 3*x - 2*y**3]