endolith
endolith

Reputation: 26833

sympy simplifying fractional powers of imaginary number

Why doesn't -(-1)**(1/3) + (-1)**(2/3) reduce to -1?

wolfram alpha knows it's -1 but sympy gamma only does a float approximation

re(_) + I*im(_) produces a NegativeOne object, but none of the other simplification functions I tried did anything to it.

Upvotes: 1

Views: 492

Answers (1)

asmeurer
asmeurer

Reputation: 91610

I'm assuming you really mean -(-1)**Rational(1, 3) + (-1)**Rational(2, 3), as literally -(-1)**(1/3) + (-1)**(2/3) is all Python (no SymPy), and evaluates numerically.

Most SymPy objects do not do any kind of nontrivial simplification automatically. The reason is that sometimes you might want to represent -(-1)**(1/3) + (-1)**(2/3) without it simplifying. Also, simplification in general is an expensive operation, and doing so at operation creation time would be very inefficient, as often you create intermediate expressions that don't need to be simplified at the intermediate stage.

re(expr) + I*im(expr) is fine. A more automated way to do that is to use expand_complex():

In [19]: expand_complex(-(-1)**Rational(1, 3) + (-1)**Rational(2, 3))
Out[19]: -1

Ideally simplify() would call expand_complex(), and there is an open issue for this (https://github.com/sympy/sympy/issues/7569).

And a note that SymPy Gamma provides a lot of automation on top of SymPy directly. For instance, it converts -(-1)**(1/3) + (-1)**(2/3) to SymPy types and performs various functions to the expression, like numerical evaluation, simplification, differentiation, etc.

Upvotes: 3

Related Questions