如何找出Sympy变量是否复杂? [英] How to find out if the Sympy variable is complex?

查看:78
本文介绍了如何找出Sympy变量是否复杂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写涉及求解此方程的代码

I am writing a code which involves solving this equation

X = solve(Theta_Mod_Eqn*Ramp_Equation/(x+PT) - C, x)

我正在使用sympy库,现在该方程式有7个根,很少有复数,很少是真实的。我无法隔离它们,因为 isinstance(i,complex)始终返回true

I am using sympy library, now the equation has 7 roots few are complex and few are real. I am unable to segregate them because isinstance(i,complex) is always returning true

for i in X:
    if not isinstance(i,complex):
        if (i>-0.01 and i<maxSheaveDisp):
            A = i;

一种情况
i = -0.000581431210287302-0.2540334478167 * I

for one case i = -0.000581431210287302 - 0.2540334478167*I

In:i == complex
Out[39]: False

如何找出变量是否复杂?

How to find out if the variable is complex?

推荐答案

实数是复数集合的子集。因此,每个实数都是一个复数。例如,3是一个复数。

The set of real numbers is a subset of the set of complex numbers. So, every real number is a complex number. For example, 3 is a complex number.

要问的正确问题是如何找出根是否真实。为此,如果我是SymPy符号,则可以使用 i.is_real

The correct question to ask is how to find out if a root is real. For that, you can use i.is_real if i is a SymPy symbol:

for i in X:
    if i.is_real:
        if (i>-0.01 and i<maxSheaveDisp):
            A = i

也可以将 im(i)与0 进行比较:如果 im(i)== 0 。这也适用于Python浮动。

One can also compare im(i) to 0: if im(i) == 0. This works for Python floats too.

这篇关于如何找出Sympy变量是否复杂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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