簡體   English   中英

sympy 中的復數

[英]Complex numbers in sympy

我正在使用 lcapy 和 sympy 並嘗試處理電路中的復數。

我有以下同情表達:

import sympy
from lcapy import j

expr = sympy.parse_expr('L*w_0*(C*R*w_0 - I)')
expr

Output:

L⋅w₀⋅(C⋅R⋅w₀ - ⅉ)

上面的expr是一個復數表達式, 是虛數。 我可以使用 sympy 並從expr中刪除括號,以便以更規范的復數形式查看它

w₀^2⋅R⋅L⋅C - ⅉ⋅w₀⋅L

sympy 是否支持處理復數? 我想得到expr的論點,它應該是:

arctan(L⋅w₀ / w₀^2⋅R⋅L⋅C)

我可以(在isympy會話中):

In [13]: expr
Out[13]: L⋅w₀⋅(C⋅R⋅w₀ - ⅈ)

In [14]: expr.expand()
Out[14]: 
        2         
C⋅L⋅R⋅w₀  - ⅈ⋅L⋅w₀

編輯

嘗試

atan2(im(expr), re(expr))

https://docs.sympy.org/latest/modules/functions/elementary.html

細化變量:

In [53]: C,L,R,w_0=symbols('C L R w_0',real=True, positive=True)    
In [54]: expr=L*w_0*(C*R*w_0-I)

In [55]: expr
Out[55]: L⋅w₀⋅(C⋅R⋅w₀ - ⅈ)

In [56]: expr.expand()
Out[56]: 
        2         
C⋅L⋅R⋅w₀  - ⅈ⋅L⋅w₀

In [57]: im(_),re(_)
Out[57]: 
⎛               2⎞
⎝-L⋅w₀, C⋅L⋅R⋅w₀ ⎠

現在atan2被簡化:

In [59]: atan2(*_)
Out[59]: 
     ⎛  1   ⎞
-atan⎜──────⎟
     ⎝C⋅R⋅w₀⎠

arg做同樣的事情:

In [60]: arg(_56)
Out[60]: arg(C⋅R⋅w₀ - ⅈ)

In [62]: arg(expr)
Out[62]: arg(C⋅R⋅w₀ - ⅈ)

In [77]: arg(expr)._eval_rewrite_as_atan2(expr)
Out[77]: 
     ⎛  1   ⎞
-atan⎜──────⎟
     ⎝C⋅R⋅w₀⎠

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM