SYMPY:如何用exp(-i.w.t)项简化函数?

  • 本文关键字:函数 何用 exp SYMPY sympy
  • 更新时间 :
  • 英文 :


我使用sympy对虚数指数项进行符号计算(exp(i.a)),但我在简化方面存在问题。例如,我得到了以下函数(经过一些哈密顿对角化和时间演化,这不是这个问题中的问题):

import sympy as sp
w, th, t =sp.symbols("omega theta t", real=True)
myFunc = (sp.exp(sp.I*w*t)*sp.sin(th)**2 + sp.exp(-sp.I*w*t)*sp.cos(th)**2)*(sp.exp(sp.I*w*t)*sp.cos(th)**2 + sp.exp(-sp.I*w*t)*sp.sin(th)**2) + sp.sin(2*th)**2*sp.sin(w*t)**2

在计算过程中,从exp(-i.wt)和exp(i.wt)隐式生成sin(w.t)和cos(w.t)项。这很正常。但是,我不能设法用https://docs.sympy.org/latest/tutorial/simplification.html的不同方法来简化它们。

然而,在上面的例子中,如果鼻窦(w.t)显式地被(exp(i.w.t)-exp(-i.w.t))/(2.i)) 代替
firstStep=myFunc.subs(sp.sin(w*t),((sp.exp(sp.I*w*t)-sp.exp(-sp.I*w*t))/(2*sp.I)))

(sp.simplify(firstStep))输出1

是否有一些sympy函数可以直接简化exp(i.wt), exp(-i.wt)项与sin(wt), cos(wt)项混合的表达式?

谢谢你的回答

有很多函数可以操作这样的表达式。最简单的简化方法是将exp重写为sin或将sin重写为exp:

In [22]: myFunc
Out[22]: 
⎛ ⅈ⋅ω⋅t    2       -ⅈ⋅ω⋅t    2   ⎞ ⎛ ⅈ⋅ω⋅t    2       -ⅈ⋅ω⋅t    2   ⎞      2         2     
⎝ℯ     ⋅sin (θ) + ℯ      ⋅cos (θ)⎠⋅⎝ℯ     ⋅cos (θ) + ℯ      ⋅sin (θ)⎠ + sin (2⋅θ)⋅sin (ω⋅t)
In [23]: myFunc.expand()
Out[23]: 
2⋅ⅈ⋅ω⋅t    2       2         4         2         2           4       -2⋅ⅈ⋅ω⋅t    2       2   
ℯ       ⋅sin (θ)⋅cos (θ) + sin (θ) + sin (2⋅θ)⋅sin (ω⋅t) + cos (θ) + ℯ        ⋅sin (θ)⋅cos (θ)
In [24]: myFunc.rewrite(exp)
Out[24]: 
⎛              2                         2       ⎞ ⎛              2                        2        ⎞                     2                  
⎜⎛ ⅈ⋅θ    -ⅈ⋅θ⎞            ⎛ ⅈ⋅θ    -ⅈ⋅θ⎞   ⅈ⋅ω⋅t⎟ ⎜⎛ ⅈ⋅θ    -ⅈ⋅θ⎞           ⎛ ⅈ⋅θ    -ⅈ⋅θ⎞   -ⅈ⋅ω⋅t⎟   ⎛ 2⋅ⅈ⋅θ    -2⋅ⅈ⋅θ⎞  ⎛ ⅈ⋅ω⋅t    -ⅈ⋅ω⋅t
⎜⎜ℯ      ℯ    ⎟   -ⅈ⋅ω⋅t   ⎝ℯ    - ℯ    ⎠ ⋅ℯ     ⎟ ⎜⎜ℯ      ℯ    ⎟   ⅈ⋅ω⋅t   ⎝ℯ    - ℯ    ⎠ ⋅ℯ      ⎟   ⎝ℯ      - ℯ      ⎠ ⋅⎝ℯ      - ℯ      
⎜⎜──── + ─────⎟ ⋅ℯ       - ──────────────────────⎟⋅⎜⎜──── + ─────⎟ ⋅ℯ      - ───────────────────────⎟ + ─────────────────────────────────────
⎝⎝ 2       2  ⎠                      4           ⎠ ⎝⎝ 2       2  ⎠                      4           ⎠                      16                
2
⎞ 
⎠ 
──

In [25]: myFunc.rewrite(exp).expand()
Out[25]: 1
In [26]: myFunc.rewrite(sin)
Out[26]: 
⎛                            2                                 2⎛    π⎞⎞ ⎛                            2⎛    π⎞                              2
⎜(-ⅈ⋅sin(ω⋅t) + cos(ω⋅t))⋅sin (θ) + (ⅈ⋅sin(ω⋅t) + cos(ω⋅t))⋅sin ⎜θ + ─⎟⎟⋅⎜(-ⅈ⋅sin(ω⋅t) + cos(ω⋅t))⋅sin ⎜θ + ─⎟ + (ⅈ⋅sin(ω⋅t) + cos(ω⋅t))⋅sin 
⎝                                                               ⎝    2⎠⎠ ⎝                             ⎝    2⎠                               
⎞      2         2     
(θ)⎟ + sin (2⋅θ)⋅sin (ω⋅t)
⎠                      
In [27]: myFunc.rewrite(sin).expand()
Out[27]: 
4       2           4       2             2       2         2⎛    π⎞        2       2⎛    π⎞    2           2         2           2       
sin (θ)⋅sin (ω⋅t) + sin (θ)⋅cos (ω⋅t) - 2⋅sin (θ)⋅sin (ω⋅t)⋅sin ⎜θ + ─⎟ + 2⋅sin (θ)⋅sin ⎜θ + ─⎟⋅cos (ω⋅t) + sin (2⋅θ)⋅sin (ω⋅t) + sin (ω⋅t)⋅s
⎝    2⎠                 ⎝    2⎠                                              
4⎛    π⎞      4⎛    π⎞    2     
in ⎜θ + ─⎟ + sin ⎜θ + ─⎟⋅cos (ω⋅t)
⎝    2⎠       ⎝    2⎠          
In [28]: myFunc.rewrite(sin).expand().trigsimp()
Out[28]: 1

最新更新