0

I have imported in python a Mathematica expression successfully, but I am not able to use it to evaluate in the numbers I have in my python code. Here it follows an example:

from sympy.parsing.mathematica import mathematica
from sympy import var
import numpy as np

G=1
c=1
m1in=1
m2in=1
Rxin=1
Ryin=1
Rzin=1
Vxin=1
Vyin=1
Vzin=1
sx1in=1
sy1in=1
sz1in=1
sx2in=1
sy2in=1
sz2in=1

G,c,m1in,m2in,Rxin,Ryin,Rzin,Vxin,Vyin,Vzin,sx1in,sy1in,sz1in,sx2in,sy2in,sz2in = var('G c m1 m2 Rx Ry Rz Vx Vy Vz sx1 sy1in sz1 sx2 sy2 sz2')

term1 = mathematica('-1/(3 (m1 + m2) (Rx^2 + Ry^2 + Rz^2)^4)*16 G^2 m1 m2 (Rz (-((m2^2 sy1 + m1^2 sy2) Vx) + (m2^2 sx1 + m1^2 sx2) Vy) + Ry ((m2^2 sz1 + m1^2 sz2) Vx - (m2^2 sx1 + m1^2 sx2) Vz) + Rx (-((m2^2 sz1 + m1^2 sz2) Vy) + (m2^2 sy1 + m1^2 sy2) Vz)) (2 G (m1 + m2) Sqrt[Rx^2 + Ry^2 + Rz^2] - 30 Ry Rz Vy Vz - 30 Rx Vx (Ry Vy + Rz Vz) + Rz^2 (14 (Vx^2 + Vy^2) - Vz^2) + Ry^2 (14 Vx^2 - Vy^2 + 14 Vz^2) + Rx^2 (-Vx^2 + 14 (Vy^2 + Vz^2)))')

print(term1)

I would like to evaluate term1 in the values I have written at the beginning of the code in order to have a number as final output.

2
  • 1
    You need to use subs e.g. term1.subs(Symbol('m1'), 1) Commented Nov 29, 2021 at 16:55
  • Thank you very much! It is a very useful suggestion Commented Nov 29, 2021 at 17:07

1 Answer 1

1

I'm not sure if it is common for mathematica variables to list 'in' before the = but if it does, the following might be useful for converting that list to a replacement dictionary that you can use on your parsed expression:

>>> def mathin2reps(s):
...   from sympy.parsing.sympy_parser import parse_expr
...   return dict(parse_expr(i.replace('in=','='), None, T[1,9]).args
...       for i in s.splitlines())
...
>>> reps = mathin2reps('''G=1
... c=1
... m1in=1
... m2in=1
... Rxin=1
... Ryin=1
... Rzin=1
... Vxin=1
... Vyin=1
... Vzin=1
... sx1in=1
... sy1in=1
... sz1in=1
... sx2in=1
... sy2in=1
... sz2in=1''')
>>> term1.xreplace(reps)
0
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much for your help. However it worked it out also with .subs(sim.Symbol('m1'),1). I have another problem dealing with the fact that when I compute something I have expressions like 1.6751 + 56*sqrt(2), where sqrt(2) and of any number that is not a perfect square it is not computed. How can I fix this issue?
@VDF if you want a numeric approximation instead of the exact symbolic expression you can call .evalf(), e.g. term1.xreplace(reps).evalf()
You can do evaluation with substitution by using term1.evalf(subs=reps); this is the way to get the most accurate result using the substituted values (assuming all variables are replaced).
Thank you very much for your suggestions! They are very helpful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.