【发布时间】:2017-10-31 10:18:30
【问题描述】:
我正在使用 sympy (python 3) 中的复杂函数,并且无法让 sympy 简化方程。特别是我不能同情使用欧拉恒等式将复指数分解为实部和虚部。这是我的代码:
import sympy as sym
from sympy import I, init_printing
# setup printing
init_printing()
# complex potential cylinder in uniform flow
U,z,R,theta=sym.symbols('U z R theta')
F=U*z+U/z
# complex velocity cylinder in uniform flow
compVel=sym.diff(F,z)
exp1=sym.sympify('R*exp(I*theta)')
compVel=compVel.subs(z,exp1)
print(compVel)
phi,psi=sym.symbols('phi psi')
phi=sym.re(compVel)
psi=sym.im(compVel)
print(phi)
print(psi)
当我运行这段代码时,输出是:
U - U*exp(-2*I*theta)/R**2
re(U) - re(U*exp(-2*I*theta)/R**2)
im(U) - im(U*exp(-2*I*theta)/R**2)
是我遗漏了什么,还是 sympy 功能不够强大,无法识别这种简化?提前致谢!
【问题讨论】:
-
Sympy 假设所有变量都是复数,因此它正确地返回
compVel的实部以U、R等的实部。如果您将变量声明为reals byU,z,R,theta=sym.symbols('U z R theta', real = True),sympy 返回预期输出 -
@Stelios 应该是答案。
-
实际上它解决了孤立的U,但它没有解决指数:
标签: python sympy complex-numbers simplify