我正在使用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不够强大,无法识别该简化?提前感谢!
compVel
的实部与U
、R
等实部的关系。如果您通过U,z,R,theta=sym.symbols('U z R theta',real=True)
将变量声明为实数,则Sympy将返回预期的输出。 - Stelios