0

我对 Sympy Poly.all_coeffs() 返回的结果的数据类型有疑问。我最近才开始使用 Sympy。我的 Sympy 传递函数如下:

在此处输入图像描述

然后我运行这段代码:

n,d = fraction(Gs)
num = Poly(n,s)
den = Poly(d,s)
num_c = num.all_coeffs()
den_c = den.all_coeffs()

我得到:

在此处输入图像描述

然后我运行这段代码:

from scipy import signal
#nu = [5000000.0]
#de = [4.99, 509000.0]
nu = num_c
de = den_c
sys = signal.lti(nu, de)
w,mag,phase = signal.bode(sys)
plt.plot(w/(2*np.pi), mag)

结果是:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-131-fb960684259c> in <module>
      4 nu = num_c
      5 de = den_c
----> 6 sys = signal.lti(nu, de)

但是,如果我改用那些注释行 'nu' 和 'de' 直接 python 列表,程序就可以工作。那么这里有什么问题呢?

4

2 回答 2

2

为什么你只显示一点错误?为什么不是完整的消息,甚至是完整的回溯!

In [60]: sys = signal.lti(num_c, den_c)                                                                   
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-60-21f71ecd8884> in <module>
----> 1 sys = signal.lti(num_c, den_c)

/usr/local/lib/python3.6/dist-packages/scipy/signal/ltisys.py in __init__(self, *system, **kwargs)
    590         self._den = None
    591 
--> 592         self.num, self.den = normalize(*system)
    593 
    594     def __repr__(self):

/usr/local/lib/python3.6/dist-packages/scipy/signal/filter_design.py in normalize(b, a)
   1609     leading_zeros = 0
   1610     for col in num.T:
-> 1611         if np.allclose(col, 0, atol=1e-14):
   1612             leading_zeros += 1
   1613         else:

<__array_function__ internals> in allclose(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in allclose(a, b, rtol, atol, equal_nan)
   2169 
   2170     """
-> 2171     res = all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))
   2172     return bool(res)
   2173 

<__array_function__ internals> in isclose(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
   2267     y = array(y, dtype=dt, copy=False, subok=True)
   2268 
-> 2269     xfin = isfinite(x)
   2270     yfin = isfinite(y)
   2271     if all(xfin) and all(yfin):

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

现在查看num_c列表的元素(对于 相同den_c):

In [55]: num_c[0]                                                                                         
Out[55]: 500000.000000000

In [56]: type(_)                                                                                          
Out[56]: sympy.core.numbers.Float

scipy代码正在numpy对输入进行测试。所以它首先将列表变成了数组:

In [61]: np.array(num_c)                                                                                  
Out[61]: array([500000.000000000], dtype=object)

该数组包含sympy对象。它不能将其转换为带有“安全”的 numpy float。但是一个明确的astype用作unsafe默认值:

In [63]: np.array(num_c).astype(float)                                                                    
Out[63]: array([500000.])

因此,让我们将两个列表都转换为有效的 numpy 浮点数组:

In [64]: sys = signal.lti(np.array(num_c).astype(float), np.array(den_c).astype(float))                   

In [65]: sys                                                                                              
Out[65]: 
TransferFunctionContinuous(
array([100200.4008016]),
array([1.00000000e+00, 1.02004008e+05]),
dt: None
)

列表推导中的转换也有效:

sys = signal.lti([float(i) for i in num_c],[float(i) for i in den_c]) 
于 2020-01-10T18:43:42.943 回答
0

您可能需要将 sympy 对象转换为浮点数/浮点数列表。

于 2020-01-10T14:37:22.477 回答