sympy中的复数:解析欧拉身份 [英] complex numbers in sympy: resolving Euler's Identity

查看:117
本文介绍了sympy中的复数:解析欧拉身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sympy(python 3)中的复杂函数,并且在获取sympy来简化方程式时遇到麻烦.特别是,我无法使用Euler的Identity将复杂的指数分解为实部和虚部.这是我的代码:

I am working with complex functions in sympy (python 3) and am having trouble getting sympy to simplify the equations. In particular I can't get sympy to use Euler's Identity to break up the complex exponential into real and imaginary parts. Here is my code:

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)

运行此代码时,输​​出为:

When I run this code the output is:

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的功能不足以识别这种简化?预先感谢!

Am I missing something or is sympy not powerful enough to do recognize this simplification? Thanks in advance!

推荐答案

我知道了;@Stelios是正确的,但另外,当您使用sympify时,必须传递一个包含表达式包含的局部变量的字典.

I figured it out; @Stelios is correct but in addition when you use sympify you must pass a dictionary with the local variables that the expression contains.

exp1=sym.sympify('R*exp(I*theta)',locals={'R':R,'theta':theta})

这篇关于sympy中的复数:解析欧拉身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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