Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify an expression for a complex constant using sympy?

Tags:

People also ask

How do you simplify an expression in SymPy?

Sympy has powerful ability to simplify mathematical expressions. There are many functions in SymPy to perform various kinds of simplification. A general function called simplify() is there that attempts to arrive at the simplest form of an expression.

How do you write an exponential in SymPy?

With the help of sympy. stats. Exponential() method, we can get the continuous random variable representing the exponential distribution.

How do you write the square root in SymPy?

With the help of sympy. sqrt() method, we can find the square root of any number by using sympy. sqrt() method. Return : Return square root of any number.


I have done some calculations in sympy, and the result is in the end a set of constants. One of them is inserted directly into the snippet below:

from sympy import *
expr = (18**(Rational(1, 3))/(6*(3 + sqrt(3)*I)**(Rational(1, 3)))
        + 12**(Rational(1, 3))*(3 + sqrt(3)*I)**(Rational(1, 3))/12)
print(expr.evalf())
print(expr.simplify())

This returns

0.56857902130163 + 0.e-22*I
18**(1/3)/(6*(3 + sqrt(3)*I)**(1/3)) + (36 + 12*sqrt(3)*I)**(1/3)/12

so the expression appears to be a real number, yet sympy cannot simplify it further. With pen and paper, I have simplified this to

cos(pi/18) / sqrt(3)

which agrees with the numerical value returned by evalf().

I have tried many of the different simplification functions, but none seem to be able to reduce the expression any further. Using substitutions like

expr.subs(3 + sqrt(3)*I, sqrt(12) * exp(I*pi/6))

improves the expression, but still sympy is unable to conclude that it is real. Using Euler's formula for substitution,

expr.subs(3 + sqrt(3)*I, sqrt(12) * (cos(pi/6) + I*sin(pi/6)))

sympy is finally able to conclude that the expression is real, but the expression itself explodes in size when printed (even if I attempt simplify after the substitution).

Is there a better way to try to reduce this? I have many similar expressions for complex constants that I would like to know for sure are real (or not).