谁能解释我们如何获取numpy.poly函数的输出

时间:2019-01-09 23:19:17

标签: python numpy

我想知道当我们调用numpy.poly函数时在幕后发生的事情。一个示例演示将不胜感激。

>>> numpy.poly((0, 0, 0)) # Multiple root example
array([1, 0, 0, 0])

2 个答案:

答案 0 :(得分:2)

可以通过编写(x-a)(x-b)(x-c)轻松创建以'a','b'和'c'为根的多项式,可以将其乘以生成-abc + abx + acx + bcx-ax < sup> 2 -bx 2 -cx 2 + x 3 。在您的情况下,a,b,c全部为零,这简化为x 3 。您[1, 0, 0, 0]的结果是将该多项式表示为系数数组。

答案 1 :(得分:0)

以@jason的答案为基础:

对于基本情况,np.poly代码可以简化为:

def foo(seq):
    a = np.array([1.])
    for s in seq:
        a = np.convolve(a, [1,-s])
    return a

In [39]: foo([0,0,0])
Out[39]: array([1., 0., 0., 0.])
In [40]: np.poly([0,0,0])
Out[40]: array([1., 0., 0., 0.])
In [41]: foo([2,0,-2])
Out[41]: array([ 1.,  0., -4.,  0.])
In [42]: np.poly([2,0,-2])
Out[42]: array([ 1.,  0., -4.,  0.])
In [43]: foo([.1,.2,.3])
Out[43]: array([ 1.   , -0.6  ,  0.11 , -0.006])
In [44]: np.poly([.1,.2,.3])
Out[44]: array([ 1.   , -0.6  ,  0.11 , -0.006])

np.convolve的作用以及为什么要完成此任务的原因是另一个问题层。

在每次迭代后打印a

In [46]: foo([.1,.2,.3])
[ 1.  -0.1]
[ 1.   -0.3   0.02]
[ 1.    -0.6    0.11  -0.006]

分组@jason方程:

-abc + abx + acx + bcx - ax2 - bx2 - cx2 + x3
-abc + (ab + ac + bc)*x + (- a - b - c)*x2 + x3

-.1*.2*.3, (.1*.2 + .1*.3 + .2*.3), (-.1-.2-.3), 1
-.006, .02+.03+.06, -.6, 1

或逐步建造

-a + x
-ab + (-a-b)x + x2
-abc + ...