【问题标题】:SYMPY : How to simplify function with exp(-i.w.t) term?SYMPY:如何使用 exp(-i.w.t) 术语简化函数?
【发布时间】:2021-03-01 18:06:04
【问题描述】:

我使用 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

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

然而,在上面的例子中,如果 sinus(wt) 被明确地替换为 (exp(iwt)-exp(-iwt)) /(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) 项混合?

感谢回答

【问题讨论】:

    标签: sympy


    【解决方案1】:

    有很多函数可以用来操作这样的表达式。最简单的简化方法是将 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
    

    【讨论】:

    • 非常感谢。 rewrite 是秘密。我之前应该问过的。
    • 作为一个sympy专家,你对这个问题有什么想法吗:stackoverflow.com/q/66397006/7462275?我希望我不会浪费你的时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多