numpy.polyval#

numpy.polyval(p, x)[原始碼]#

Evaluate a polynomial at specific values.

備註

這是舊版多項式 API 的一部分。自 1.4 版本起,建議使用 numpy.polynomial 中定義的新版多項式 API。有關差異的概述,請參閱 transition guide

如果 p 的長度為 N,則此函數傳回該值::

p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]

如果 x 是一個數列,則對於 x 的每個元素傳回 p(x)。如果 x 是另一個多項式,則傳回複合多項式 p(x(t))

參數:
p類別數組物件或一維多對象

1D array of polynomial coefficients (including coefficients equal to zero) from highest degree to the constant term, or an instance of poly1d.

x類別數組物件或一維多對象

A number, an array of numbers, or an instance of poly1d, at which to evaluate p.

回傳值:
valuesndarray 或 poly1d

If x is a poly1d instance, the result is the composition of the two polynomials, i.e., x is 「substituted」 in p and the simplified result is returned. In addition, the type of x - array_like or poly1d - governs the type of the output: x array_like => values array_like, x a poly1d object => values is also.

也參考

poly1d

一個多項式類別。

Notes

Horner’s scheme [1] is used to evaluate the polynomial. Even so, for polynomials of high degree the values may be inaccurate due to rounding errors. Use carefully.

If x is a subtype of ndarray the return value will be of the same type.

References

[1]

I. N. Bronshtein, K. A. Semendyayev, and K. A. Hirsch (Eng. trans. Ed.), Handbook of Mathematics, New York, Van Nostrand Reinhold Co., 1985, pg. 720.

範例

>>> import numpy as np
>>> np.polyval([3,0,1], 5)  # 3 * 5**2 + 0 * 5**1 + 1
76
>>> np.polyval([3,0,1], np.poly1d(5))
poly1d([76])
>>> np.polyval(np.poly1d([3,0,1]), 5)
76
>>> np.polyval(np.poly1d([3,0,1]), np.poly1d(5))
poly1d([76])