sympy 简化虚数的分数幂 [英] sympy simplifying fractional powers of imaginary number

查看:31
本文介绍了sympy 简化虚数的分数幂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 -(-1)**(1/3) + (-1)**(2/3) 不会减少到 -1?

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

wolfram alpha 知道它是 -1 但sympy gamma 只做一个浮点近似

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

re(_) + I*im(_) 生成一个 NegativeOne 对象,但我尝试过的其他简化函数都没有对它做任何事情.

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

推荐答案

我假设你的意思是 -(-1)**Rational(1, 3) + (-1)**Rational(2), 3),字面意思是 -(-1)**(1/3) + (-1)**(2/3) 都是 Python(没有 SymPy),并且数值计算.

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.

大多数 SymPy 对象不会自动进行任何类型的非平凡简化.原因是有时您可能想要表示 -(-1)**(1/3) + (-1)**(2/3) 而不进行简化.此外,简化通常是一项代价高昂的操作,在操作创建时这样做会非常低效,因为您通常会创建不需要在中间阶段进行简化的中间表达式.

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) 没问题.一种更自动化的方法是使用 expand_complex():

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

理想情况下 simplify() 会调用 expand_complex(),并且有一个未解决的问题 (https://github.com/sympy/sympy/issues/7569).

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

请注意,SymPy Gamma 直接在 SymPy 之上提供了大量自动化.例如,它将 -(-1)**(1/3) + (-1)**(2/3) 转换为 SymPy 类型并对表达式执行各种功能,如数值计算、简化、差异化等.

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.

这篇关于sympy 简化虚数的分数幂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆