numpy.polyval#
- numpy.polyval(p,x)[source]#
Evaluate a polynomial at specific values.
Note
This forms part of the old polynomial API. Since version 1.4, thenew polynomial API defined in
numpy.polynomialis preferred.A summary of the differences can be found in thetransition guide.Ifp is of length N, this function returns the value:
p[0]*x**(N-1)+p[1]*x**(N-2)+...+p[N-2]*x+p[N-1]
Ifx is a sequence, then
p(x)is returned for each element ofx.Ifx is another polynomial then the composite polynomialp(x(t))is returned.- Parameters:
- parray_like or poly1d object
1D array of polynomial coefficients (including coefficients equalto zero) from highest degree to the constant term, or aninstance of poly1d.
- xarray_like or poly1d object
A number, an array of numbers, or an instance of poly1d, atwhich to evaluatep.
- Returns:
- valuesndarray or poly1d
Ifx is a poly1d instance, the result is the composition of the twopolynomials, i.e.,x is “substituted” inp and the simplifiedresult is returned. In addition, the type ofx - array_like orpoly1d - governs the type of the output:x array_like =>valuesarray_like,x a poly1d object =>values is also.
See also
poly1dA polynomial class.
Notes
Horner’s scheme[1] is used to evaluate the polynomial. Even so,for polynomials of high degree the values may be inaccurate due torounding errors. Use carefully.
Ifx is a subtype of
ndarraythe 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 NostrandReinhold Co., 1985, pg. 720.
Examples
>>>importnumpyasnp>>>np.polyval([3,0,1],5)# 3 * 5**2 + 0 * 5**1 + 176>>>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])