简体   繁体   English

sympy 中的复数

[英]Complex numbers in sympy

I am using lcapy together with sympy and trying to process complex numbers from a circuit.我正在使用 lcapy 和 sympy 并尝试处理电路中的复数。

I have the following sympy expression:我有以下同情表达:

import sympy
from lcapy import j

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

Output: Output:

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

expr above is an complex expression with being the imaginary number.上面的expr是一个复数表达式, 是虚数。 Can I use sympy and remove the brackets from expr in order to view it in the more canonical form for complex numbers as我可以使用 sympy 并从expr中删除括号,以便以更规范的复数形式查看它

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

And does sympy have support for handling complex numbers? sympy 是否支持处理复数? I want to get the argument of expr which should be:我想得到expr的论点,它应该是:

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

I can do (in an isympy session):我可以(在isympy会话中):

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

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

edit编辑

try尝试

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

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

Refining the variables:细化变量:

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₀ ⎠

Now the atan2 is simplified:现在atan2被简化:

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

And arg does the same: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