Polynomials#

Polynomials in NumPy can becreated,manipulated, and evenfitted usingtheconvenience classesof thenumpy.polynomial package, introduced in NumPy 1.4.

Prior to NumPy 1.4,numpy.poly1d was the class of choice and it is stillavailable in order to maintain backward compatibility.However, the newerpolynomialpackage is more completeand itsconvenience classes provide amore consistent, better-behaved interface for working with polynomialexpressions.Thereforenumpy.polynomial is recommended for new coding.

Note

Terminology

The termpolynomial module refers to the old API defined innumpy.lib.polynomial, which includes thenumpy.poly1d class andthe polynomial functions prefixed withpoly accessible from thenumpynamespace (e.g.numpy.polyadd,numpy.polyval,numpy.polyfit, etc.).

The termpolynomial package refers to the new API defined innumpy.polynomial, which includes the convenience classes for thedifferent kinds of polynomials (Polynomial,Chebyshev, etc.).

Transitioning fromnumpy.poly1d tonumpy.polynomial#

As noted above, thepoly1dclass and associatedfunctions defined innumpy.lib.polynomial, such asnumpy.polyfitandnumpy.poly, are considered legacy and shouldnot be used in newcode.Since NumPy version 1.4, thenumpy.polynomial package is preferred forworking with polynomials.

Quick Reference#

The following table highlights some of the main differences between thelegacy polynomial module and the polynomial package for common tasks.ThePolynomial class is imported for brevity:

fromnumpy.polynomialimportPolynomial

How to…

Legacy (numpy.poly1d)

numpy.polynomial

Create apolynomial objectfrom coefficients[1]

p=np.poly1d([1,2,3])

p=Polynomial([3,2,1])

Create a polynomialobject from roots

r=np.poly([-1,1])p=np.poly1d(r)

p=Polynomial.fromroots([-1,1])

Fit a polynomial ofdegreedeg to data

np.polyfit(x,y,deg)

Polynomial.fit(x,y,deg)

[1]

Note the reversed ordering of the coefficients

Transition Guide#

There are significant differences betweennumpy.lib.polynomial andnumpy.polynomial.The most significant difference is the ordering of the coefficients for thepolynomial expressions.The various routines innumpy.polynomial alldeal with series whose coefficients go from degree zero upward,which is thereverse order of the poly1d convention.The easy way to remember this is that indicescorrespond to degree, i.e.,coef[i] is the coefficient of the term ofdegreei.

Though the difference in convention may be confusing, it is straightforward toconvert from the legacy polynomial API to the new.For example, the following demonstrates how you would convert anumpy.poly1dinstance representing the expression\(x^{2} + 2x + 3\) to aPolynomial instance representing the sameexpression:

>>>importnumpyasnp
>>>p1d=np.poly1d([1,2,3])>>>p=np.polynomial.Polynomial(p1d.coef[::-1])

In addition to thecoef attribute, polynomials from the polynomialpackage also havedomain andwindow attributes.These attributes are most relevant when fittingpolynomials to data, though it should be noted that polynomials withdifferentdomain andwindow attributes are not considered equal, andcan’t be mixed in arithmetic:

>>>p1=np.polynomial.Polynomial([1,2,3])>>>p1Polynomial([1., 2., 3.], domain=[-1.,  1.], window=[-1.,  1.], symbol='x')>>>p2=np.polynomial.Polynomial([1,2,3],domain=[-2,2])>>>p1==p2False>>>p1+p2Traceback (most recent call last):...TypeError:Domains differ

See the documentation for theconvenience classes for further details onthedomain andwindow attributes.

Another major difference between the legacy polynomial module and thepolynomial package is polynomial fitting. In the old module, fitting wasdone via thepolyfit function. In the polynomial package, thefit class method is preferred. Forexample, consider a simple linear fit to the following data:

In [1]:rng=np.random.default_rng()In [2]:x=np.arange(10)In [3]:y=np.arange(10)+rng.standard_normal(10)

With the legacy polynomial module, a linear fit (i.e. polynomial of degree 1)could be applied to these data withpolyfit:

In [4]:np.polyfit(x,y,deg=1)Out[4]:array([ 1.18670859, -1.41420215])

With the new polynomial API, thefitclass method is preferred:

In [5]:p_fitted=np.polynomial.Polynomial.fit(x,y,deg=1)In [6]:p_fittedOut[6]:Polynomial([3.92598652, 5.34018867], domain=[0., 9.], window=[-1.,  1.], symbol='x')

Note that the coefficients are givenin the scaled domain defined by thelinear mapping between thewindow anddomain.convert can be used to get thecoefficients in the unscaled data domain.

In [7]:p_fitted.convert()Out[7]:Polynomial([-1.41420215,  1.18670859], domain=[-1.,  1.], window=[-1.,  1.], symbol='x')

Documentation for thepolynomial package#

In addition to standard power series polynomials, the polynomial packageprovides several additional kinds of polynomials including Chebyshev,Hermite (two subtypes), Laguerre, and Legendre polynomials.Each of these has an associatedconvenience class available from thenumpy.polynomial namespace that provides a consistent interface for workingwith polynomials regardless of their type.

Documentation pertaining to specific functions defined for each kind ofpolynomial individually can be found in the corresponding module documentation:

Documentation for legacy polynomials#