# save a mathematica expression with Root[] as Python expression?

I want to convert a Mathematica expression that uses Root[] to Python (open to using sympy if needed). The expression is:

Root[-k2 k4 N0 + (B0 k2 k3 + A0 k1 k4 + k2 k4 - k2 k3 N0 -
k1 k4 N0) #1 + (A0 k1 k3 + B0 k1 k3 + k2 k3 + k1 k4 -
k1 k3 N0) #1^2 + k1 k3 #1^3 &, 3]


since FortranForm is close to Python, I could use that and manually edit the expression. But first the Root needs to be substituted. I tried using ToRadicals

ToRadicals[
Root[-k2 k4 N0 + (B0 k2 k3 + A0 k1 k4 + k2 k4 - k2 k3 N0 -
k1 k4 N0) #1 + (A0 k1 k3 + B0 k1 k3 + k2 k3 + k1 k4 -
k1 k3 N0) #1^2 + k1 k3 #1^3 &, 3]] // FortranForm


which gives a long expression:

 -(A0*k1*k3 + B0*k1*k3 + k2*k3 + k1*k4 - k1*k3*N0)/(3.*k1*k3) +
-  ((1 - (0,1)*Sqrt(3))*(-(A0*k1*k3 + B0*k1*k3 + k2*k3 + k1*k4 - k1*k3*N0)**2 + 3*k1*k3*(B0*k2*k3 + A0*k1*k4 + k2*k4 - k2*k3*N0 - k1*k4*N0)))...


but what does (0,1)*Sqrt(3) mean in FortranForm and what's the correct way to write it in Python? is it just 1j*sqrt(3)? Thanks.

• This sounds like an XY problem. What are you trying to ultimately achieve by "converting to a Python expression"? – Szabolcs Oct 19 '20 at 15:56
• you might be right. I have a system of equations that mathematica and only mathematica it seems can solve. The resulting solution is very hairy. I want to evaluate it multiple times in a separate piece of code (in Python) without calling mathematica. i.e., I just want to save the solution to the system of equations. – pythonuser Oct 19 '20 at 18:49
• Root represents the roots of polynomials. You can look for already implemented numerical methods in Python to get the roots of a polynomial. I don't do much Python, but I'd start here: numpy.org/doc/stable/reference/generated/numpy.roots.html If you obtained the Root by trying to solve a polynomial (system of) equations, you might as well just solve it numerically in Python. Even if not, you might as well just solve you equation numerically directly in Python, since you say that you want numerical results in the end. – Szabolcs Oct 19 '20 at 19:08

A pair of real constants in parentheses represents a complex constant in Fortran, so (0,1) is the representation of Mathematica's I, the imaginary unit.