使用多项式对象和`np.polyval`在NumPy中评估多项式的区别是什么?

huangapple go评论66阅读模式
英文:

What is the difference between using a polynomial object and np.polyval to evaluate a polynomial in NumPy?

问题

np.poly1d函数用于创建多项式对象,而np.polyval函数用于在特定值处评估多项式。这两种方法都可以得到相同的结果,但是np.poly1d创建了一个多项式对象,可以像函数一样调用,而np.polyval直接在特定值处计算多项式的值。在选择方法时,取决于你的需求和代码的上下文。

英文:

I am working with polynomials in NumPy and I am wondering about the difference between using a polynomial object and the np.polyval function to evaluate a polynomial at a specific value. For example, what is the difference between these two code snippets:

equation = np.poly1d(coeff_list)
x = equation(z)
x = (np.polyval(equation, z))

Here, the equation is a polynomial object obtained by using the np.poly1d function and z is the value at which we want to evaluate the polynomial. Both code snippets will give me the same result, but I am curious about the underlying differences.

Can anyone explain the difference between these two approaches? Is one approach preferred over the other in certain situations? Thanks!

答案1

得分: 1

poly1d 是一个类,所以 eq 是该类的一个实例:

In [8]: eq = np.poly1d([1,2,3])

In [9]: type(eq)
Out[9]: numpy.poly1d

eq 有一个 __call__ 方法(它是一个可调用对象),可以这样使用:

In [10]: eq(np.arange(5))
Out[10]: array([ 3,  6, 11, 18, 27])

polyval 是一个函数,其代码可以直接从文档页面中阅读得到:

使用 poly1d 实例调用它与使用相同系数列表(加一个0)调用它相同:

In [11]: np.polyval(eq, np.arange(5))
Out[11]: array([ 3,  6, 11, 18, 27])

In [12]: np.polyval([0,1,2,3], np.arange(5))
Out[12]: array([ 3,  6, 11, 18, 27])

这是因为如果你在 eq 上进行迭代,你会得到它的系数:

In [13]: list(eq)
Out[13]: [1, 2, 3]

我手头找不到 poly1d 的代码;但我怀疑它可能是相当简单直接的 Python。它甚至可能以与 polyval 相同的方式评估多项式。直接使用 eq(x) 稍微快一些,但这可能更多地是由于函数调用级别而不是真正的评估差异。

关于 "首选 numpy.polynomial" 的注释适用于 polyvalpoly1d。如果你想对多项式进行高级操作或更一般的类型,可能更倾向于使用它,但对于使用系数列表进行简单评估,我没有看到很大的优势。

这是 polyval 的代码:

def polyval(p, x):
    p = NX.asarray(p)
    if isinstance(x, poly1d):
        y = 0
    else:
        x = NX.asanyarray(x)
        y = NX.zeros_like(x)
    for pv in p:
        y = y * x + pv
    return y

编辑

poly1dcall 方法实际上是对 polyval 的调用:

def __call__(self, val):
    return polyval(self.coeffs, val)

所有这些都在 np.lib.polynomial.py 文件中。

英文:

poly1d is a class, so eq is an instance of that class:

In [8]: eq = np.poly1d([1,2,3])

In [9]: type(eq)
Out[9]: numpy.poly1d

eq has a __call__ method (it's a callable), so can be used as:

In [10]: eq(np.arange(5))
Out[10]: array([ 3,  6, 11, 18, 27])

polyval is a function, whose code is readily readable from the doc page:

Calling it with a poly1d instance is the same as calling it with the same list of coefficients (plus a 0):

In [11]: np.polyval(eq, np.arange(5))
Out[11]: array([ 3,  6, 11, 18, 27])

In [12]: np.polyval([0,1,2,3], np.arange(5))
Out[12]: array([ 3,  6, 11, 18, 27])

That's because if you iterate on eq you get its coefficients:

In [13]: list(eq)
Out[13]: [1, 2, 3]

I can't offhand find the code for poly1d; but I suspect it's fairly straight forward python. It may even evaluate the polynomial in the same way as polyval. Direct use of eq(x) is slightly faster, but that may more the result of levels of function calls than real evaluation differences.

The note about "numpy.polynomial is preferred" applies to both polyval and poly1d. It may be preferred if you want to do fancy stuff with polynomials, or more general types, but for simple evaluation with a list of coefficients, I don't see much advantage.

Here's the code for polyval:

def polyval(p, x):
    p = NX.asarray(p)
    if isinstance(x, poly1d):
        y = 0
    else:
        x = NX.asanyarray(x)
        y = NX.zeros_like(x)
    for pv in p:
        y = y * x + pv
    return y

edit

The call method of poly1d is actually a call to polyval:

def __call__(self, val):
    return polyval(self.coeffs, val)

This is all in the np.lib.polynomial.py file

huangapple
  • 本文由 发表于 2023年4月17日 13:39:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定