Variable | array_function_dispatch |
Undocumented |
Class | poly1d |
A one-dimensional polynomial class. |
Class | RankWarning |
Issued by polyfit when the Vandermonde matrix is rank deficient. |
Function | _binary_op_dispatcher |
Undocumented |
Function | _poly_dispatcher |
Undocumented |
Function | _polyder_dispatcher |
Undocumented |
Function | _polydiv_dispatcher |
Undocumented |
Function | _polyfit_dispatcher |
Undocumented |
Function | _polyint_dispatcher |
Undocumented |
Function | _polyval_dispatcher |
Undocumented |
Function | _raise_power |
Undocumented |
Function | _roots_dispatcher |
Undocumented |
Function | poly |
Find the coefficients of a polynomial with the given sequence of roots. |
Function | polyadd |
Find the sum of two polynomials. |
Function | polyder |
Return the derivative of the specified order of a polynomial. |
Function | polydiv |
Returns the quotient and remainder of polynomial division. |
Function | polyfit |
Least squares polynomial fit. |
Function | polyint |
Return an antiderivative (indefinite integral) of a polynomial. |
Function | polymul |
Find the product of two polynomials. |
Function | polysub |
Difference (subtraction) of two polynomials. |
Function | polyval |
Evaluate a polynomial at specific values. |
Function | roots |
Return the roots of a polynomial with coefficients given in p. |
Variable | _poly_mat |
Undocumented |
Find the coefficients of a polynomial with the given sequence of roots.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial
is preferred.
A summary of the differences can be found in the
:doc:`transition guide </reference/routines.polynomials>`.
Returns the coefficients of the polynomial whose leading coefficient is one for the given sequence of zeros (multiple roots must be included in the sequence as many times as their multiplicity; see Examples). A square matrix (or array, which will be treated as a matrix) can also be given, in which case the coefficients of the characteristic polynomial of the matrix are returned.
1D array of polynomial coefficients from highest to lowest degree:
c[0] * x**(N) + c[1] * x**(N-1) + ... + c[N-1] * x + c[N] where c[0] always equals 1.
polyval : Compute polynomial values. roots : Return the roots of a polynomial. polyfit : Least squares polynomial fit. poly1d : A one-dimensional polynomial class.
Specifying the roots of a polynomial still leaves one degree of freedom, typically represented by an undetermined leading coefficient. [1] In the case of this function, that coefficient - the first one in the returned array - is always taken as one. (If for some reason you have one other point, the only automatic way presently to leverage that information is to use polyfit.)
The characteristic polynomial, pa(t), of an n
-by-n
matrix A is given by
pa(t) = det(t I − A),
where I is the n
-by-n
identity matrix. [2]
[1] | M. Sullivan and M. Sullivan, III, "Algebra and Trignometry, Enhanced With Graphing Utilities," Prentice-Hall, pg. 318, 1996. |
[2] | G. Strang, "Linear Algebra and Its Applications, 2nd Edition," Academic Press, pg. 182, 1980. |
Given a sequence of a polynomial's zeros:
>>> np.poly((0, 0, 0)) # Multiple root example array([1., 0., 0., 0.])
The line above represents z**3 + 0*z**2 + 0*z + 0.
>>> np.poly((-1./2, 0, 1./2)) array([ 1. , 0. , -0.25, 0. ])
The line above represents z**3 - z/4
>>> np.poly((np.random.random(1)[0], 0, np.random.random(1)[0])) array([ 1. , -0.77086955, 0.08618131, 0. ]) # random
Given a square array object:
>>> P = np.array([[0, 1./3], [-1./2, 0]]) >>> np.poly(P) array([1. , 0. , 0.16666667])
Note how in all cases the leading coefficient is always 1.
Find the sum of two polynomials.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial
is preferred.
A summary of the differences can be found in the
:doc:`transition guide </reference/routines.polynomials>`.
Returns the polynomial resulting from the sum of two input polynomials. Each input must be either a poly1d object or a 1D sequence of polynomial coefficients, from highest to lowest degree.
poly1d : A one-dimensional polynomial class. poly, polyadd, polyder, polydiv, polyfit, polyint, polysub, polyval
>>> np.polyadd([1, 2], [9, 5, 4]) array([9, 6, 6])
Using poly1d objects:
>>> p1 = np.poly1d([1, 2]) >>> p2 = np.poly1d([9, 5, 4]) >>> print(p1) 1 x + 2 >>> print(p2) 2 9 x + 5 x + 4 >>> print(np.polyadd(p1, p2)) 2 9 x + 6 x + 6
Return the derivative of the specified order of a polynomial.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial
is preferred.
A summary of the differences can be found in the
:doc:`transition guide </reference/routines.polynomials>`.
poly1d
.polyint : Anti-derivative of a polynomial. poly1d : Class for one-dimensional polynomials.
The derivative of the polynomial x3 + x2 + x1 + 1 is:
>>> p = np.poly1d([1,1,1,1]) >>> p2 = np.polyder(p) >>> p2 poly1d([3, 2, 1])
which evaluates to:
>>> p2(2.) 17.0
We can verify this, approximating the derivative with (f(x + h) - f(x))/h:
>>> (p(2. + 0.001) - p(2.)) / 0.001 17.007000999997857
The fourth-order derivative of a 3rd-order polynomial is zero:
>>> np.polyder(p, 2) poly1d([6, 2]) >>> np.polyder(p, 3) poly1d([6]) >>> np.polyder(p, 4) poly1d([0])
Returns the quotient and remainder of polynomial division.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial
is preferred.
A summary of the differences can be found in the
:doc:`transition guide </reference/routines.polynomials>`.
The input arrays are the coefficients (including any coefficients equal to zero) of the "numerator" (dividend) and "denominator" (divisor) polynomials, respectively.
poly, polyadd, polyder, polydiv, polyfit, polyint, polymul, polysub polyval
Both u
and v
must be 0-d or 1-d (ndim = 0 or 1), but u.ndim
need
not equal v.ndim
. In other words, all four possible combinations -
u.ndim = v.ndim = 0, u.ndim = v.ndim = 1,
u.ndim = 1, v.ndim = 0, and u.ndim = 0, v.ndim = 1 - work.
>>> x = np.array([3.0, 5.0, 2.0]) >>> y = np.array([2.0, 1.0]) >>> np.polydiv(x, y) (array([1.5 , 1.75]), array([0.25]))
Least squares polynomial fit.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial
is preferred.
A summary of the differences can be found in the
:doc:`transition guide </reference/routines.polynomials>`.
Fit a polynomial p(x) = p[0] * x**deg + ... + p[deg] of degree deg
to points (x, y)
. Returns a vector of coefficients p
that minimises
the squared error in the order deg
, deg-1
, ... 0
.
The Polynomial.fit
class
method is recommended for new code as it is more stable numerically. See
the documentation of the method for more information.
False
, return not just the estimate but also its
covariance matrix. By default, the covariance are scaled by
chi2/dof, where dof = M - (deg + 1), i.e., the weights are presumed
to be unreliable except in a relative sense and everything is scaled
such that the reduced chi2 is unity. This scaling is omitted if
cov='unscaled', as is relevant for the case that the weights are
w = 1/sigma, with sigma known to be a reliable estimate of the
uncertainty.y
was 2-D, the
coefficients for k
-th data set are in p[:,k].These values are only returned if full == True
rcond
.For more details, see numpy.linalg.lstsq
.
k
-th data set
are in V[:,:,k]The rank of the coefficient matrix in the least-squares fit is deficient. The warning is only raised if full == False.
The warnings can be turned off by
>>> import warnings >>> warnings.simplefilter('ignore', np.RankWarning)
polyval : Compute polynomial values. linalg.lstsq : Computes a least-squares fit. scipy.interpolate.UnivariateSpline : Computes spline fits.
The solution minimizes the squared error
in the equations:
x[0]**n * p[0] + ... + x[0] * p[n-1] + p[n] = y[0] x[1]**n * p[0] + ... + x[1] * p[n-1] + p[n] = y[1] ... x[k]**n * p[0] + ... + x[k] * p[n-1] + p[n] = y[k]
The coefficient matrix of the coefficients p
is a Vandermonde matrix.
polyfit
issues a RankWarning
when the least-squares fit is badly
conditioned. This implies that the best fit is not well-defined due
to numerical error. The results may be improved by lowering the polynomial
degree or by replacing x
by x
- x
.mean(). The rcond
parameter
can also be set to a value smaller than its default, but the resulting
fit may be spurious: including contributions from the small singular
values can add numerical noise to the result.
Note that fitting polynomial coefficients is inherently badly conditioned when the degree of the polynomial is large or the interval of sample points is badly centered. The quality of the fit should always be checked in these cases. When polynomial fits are not satisfactory, splines may be a good alternative.
[1] | Wikipedia, "Curve fitting", https://en.wikipedia.org/wiki/Curve_fitting |
[2] | Wikipedia, "Polynomial interpolation", https://en.wikipedia.org/wiki/Polynomial_interpolation |
>>> import warnings >>> x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) >>> y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0]) >>> z = np.polyfit(x, y, 3) >>> z array([ 0.08703704, -0.81349206, 1.69312169, -0.03968254]) # may vary
It is convenient to use poly1d
objects for dealing with polynomials:
>>> p = np.poly1d(z) >>> p(0.5) 0.6143849206349179 # may vary >>> p(3.5) -0.34732142857143039 # may vary >>> p(10) 22.579365079365115 # may vary
High-order polynomials may oscillate wildly:
>>> with warnings.catch_warnings(): ... warnings.simplefilter('ignore', np.RankWarning) ... p30 = np.poly1d(np.polyfit(x, y, 30)) ... >>> p30(4) -0.80000000000000204 # may vary >>> p30(5) -0.99999999999999445 # may vary >>> p30(4.5) -0.10547061179440398 # may vary
Illustration:
>>> import matplotlib.pyplot as plt >>> xp = np.linspace(-2, 6, 100) >>> _ = plt.plot(x, y, '.', xp, p(xp), '-', xp, p30(xp), '--') >>> plt.ylim(-2,2) (-2, 2) >>> plt.show()
Return an antiderivative (indefinite integral) of a polynomial.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial
is preferred.
A summary of the differences can be found in the
:doc:`transition guide </reference/routines.polynomials>`.
The returned order m
antiderivative P
of polynomial p
satisfies
(dm)/(dxm)P(x) = p(x) and is defined up to m - 1
integration constants k
. The constants determine the low-order
polynomial part
of P
so that P(j)(0) = km − j − 1.
poly1d
.m
scalars or scalar, optionalIntegration constants. They are given in the order of integration: those corresponding to highest-order terms come first.
If None (default), all constants are assumed to be zero.
If m = 1
, a single scalar can be given instead of a list.
polyder : derivative of a polynomial poly1d.integ : equivalent method
The defining property of the antiderivative:
>>> p = np.poly1d([1,1,1]) >>> P = np.polyint(p) >>> P poly1d([ 0.33333333, 0.5 , 1. , 0. ]) # may vary >>> np.polyder(P) == p True
The integration constants default to zero, but can be specified:
>>> P = np.polyint(p, 3) >>> P(0) 0.0 >>> np.polyder(P)(0) 0.0 >>> np.polyder(P, 2)(0) 0.0 >>> P = np.polyint(p, 3, k=[6,5,3]) >>> P poly1d([ 0.01666667, 0.04166667, 0.16666667, 3. , 5. , 3. ]) # may vary
Note that 3 = 6 / 2!, and that the constants are given in the order of integrations. Constant of the highest-order polynomial term comes first:
>>> np.polyder(P, 2)(0) 6.0 >>> np.polyder(P, 1)(0) 5.0 >>> P(0) 3.0
Find the product of two polynomials.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial
is preferred.
A summary of the differences can be found in the
:doc:`transition guide </reference/routines.polynomials>`.
Finds the polynomial resulting from the multiplication of the two input polynomials. Each input must be either a poly1d object or a 1D sequence of polynomial coefficients, from highest to lowest degree.
poly1d : A one-dimensional polynomial class. poly, polyadd, polyder, polydiv, polyfit, polyint, polysub, polyval convolve : Array convolution. Same output as polymul, but has parameter
for overlap mode.
>>> np.polymul([1, 2, 3], [9, 5, 1]) array([ 9, 23, 38, 17, 3])
Using poly1d objects:
>>> p1 = np.poly1d([1, 2, 3]) >>> p2 = np.poly1d([9, 5, 1]) >>> print(p1) 2 1 x + 2 x + 3 >>> print(p2) 2 9 x + 5 x + 1 >>> print(np.polymul(p1, p2)) 4 3 2 9 x + 23 x + 38 x + 17 x + 3
Difference (subtraction) of two polynomials.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial
is preferred.
A summary of the differences can be found in the
:doc:`transition guide </reference/routines.polynomials>`.
Given two polynomials a1
and a2
, returns a1 - a2.
a1
and a2
can be either array_like sequences of the polynomials'
coefficients (including coefficients equal to zero), or poly1d
objects.
poly1d
object of the difference polynomial's coefficients.polyval, polydiv, polymul, polyadd
>>> np.polysub([2, 10, -2], [3, 10, -4]) array([-1, 0, 2])
Evaluate a polynomial at specific values.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial
is preferred.
A summary of the differences can be found in the
:doc:`transition guide </reference/routines.polynomials>`.
If p
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]
If x
is a sequence, then p(x) is returned for each element of x.
If x
is another polynomial then the composite polynomial p(x(t))
is returned.
p
.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: A polynomial class.
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.
[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. |
>>> 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])
Return the roots of a polynomial with coefficients given in p.
Note
This forms part of the old polynomial API. Since version 1.4, the
new polynomial API defined in numpy.polynomial
is preferred.
A summary of the differences can be found in the
:doc:`transition guide </reference/routines.polynomials>`.
The values in the rank-1 array p
are coefficients of a polynomial.
If the length of p
is n+1 then the polynomial is described by:
p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n]
p
cannot be converted to a rank-1 array.polyval : Compute polynomial values. polyfit : Least squares polynomial fit. poly1d : A one-dimensional polynomial class.
The algorithm relies on computing the eigenvalues of the companion matrix [1].
[1] | R. A. Horn & C. R. Johnson, Matrix Analysis. Cambridge, UK: Cambridge University Press, 1999, pp. 146-7. |
>>> coeff = [3.2, 2, 1] >>> np.roots(coeff) array([-0.3125+0.46351241j, -0.3125-0.46351241j])