sympy simplifying fractional powers of imaginary number

喜夏-厌秋 提交于 2019-12-11 10:26:58

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/24100509/sympy-simplifying-fractional-powers-of-imaginary-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!