ó 2ÄÈ[c@`s1dZddlmZmZmZddddddd d d d d ddddddddddddddddgZddlZddlZddl j Z ddl m Z d d!lmZd d"lmZejZejd#d gƒZejdgƒZejd gƒZejdd gƒZd$„Zd%„Zd&„Zd'„Zd(„Zd)„Zd*„Z dd+„Z"d d dd,„Z#d gdd dd-„Z$e%d.„Z&e%d/„Z'd0„Z(d1„Z)d2„Z*d3„Z+d4„Z,d5„Z-d6„Z.de/dd7„Z0d8„Z1d9„Z2defd:„ƒYZ3dS(;sý Objects for dealing with polynomials. This module provides a number of objects (mostly functions) useful for dealing with polynomials, including a `Polynomial` class that encapsulates the usual arithmetic operations. (General information on how this module represents and works with polynomial objects is in the docstring for its "parent" sub-package, `numpy.polynomial`). Constants --------- - `polydomain` -- Polynomial default domain, [-1,1]. - `polyzero` -- (Coefficients of the) "zero polynomial." - `polyone` -- (Coefficients of the) constant polynomial 1. - `polyx` -- (Coefficients of the) identity map polynomial, ``f(x) = x``. Arithmetic ---------- - `polyadd` -- add two polynomials. - `polysub` -- subtract one polynomial from another. - `polymul` -- multiply two polynomials. - `polydiv` -- divide one polynomial by another. - `polypow` -- raise a polynomial to an positive integer power - `polyval` -- evaluate a polynomial at given points. - `polyval2d` -- evaluate a 2D polynomial at given points. - `polyval3d` -- evaluate a 3D polynomial at given points. - `polygrid2d` -- evaluate a 2D polynomial on a Cartesian product. - `polygrid3d` -- evaluate a 3D polynomial on a Cartesian product. Calculus -------- - `polyder` -- differentiate a polynomial. - `polyint` -- integrate a polynomial. Misc Functions -------------- - `polyfromroots` -- create a polynomial with specified roots. - `polyroots` -- find the roots of a polynomial. - `polyvalfromroots` -- evaluate a polynomial at given points from roots. - `polyvander` -- Vandermonde-like matrix for powers. - `polyvander2d` -- Vandermonde-like matrix for 2D power series. - `polyvander3d` -- Vandermonde-like matrix for 3D power series. - `polycompanion` -- companion matrix in power series form. - `polyfit` -- least-squares fit returning a polynomial. - `polytrim` -- trim leading coefficients from a polynomial. - `polyline` -- polynomial representing given straight line. Classes ------- - `Polynomial` -- polynomial class. See Also -------- `numpy.polynomial` i(tdivisiontabsolute_importtprint_functiontpolyzerotpolyonetpolyxt polydomaintpolylinetpolyaddtpolysubtpolymulxtpolymultpolydivtpolypowtpolyvaltpolyvalfromrootstpolydertpolyintt polyfromrootst polyvandertpolyfittpolytrimt polyrootst Polynomialt polyval2dt polyval3dt polygrid2dt polygrid3dt polyvander2dt polyvander3dN(tnormalize_axis_indexi(t polyutils(t ABCPolyBaseiÿÿÿÿcC`s3|dkrtj||gƒStj|gƒSdS(s Returns an array representing a linear polynomial. Parameters ---------- off, scl : scalars The "y-intercept" and "slope" of the line, respectively. Returns ------- y : ndarray This module's representation of the linear polynomial ``off + scl*x``. See Also -------- chebline Examples -------- >>> from numpy.polynomial import polynomial as P >>> P.polyline(1,-1) array([ 1, -1]) >>> P.polyval(1, P.polyline(1,-1)) # should be 0 0.0 iN(tnptarray(tofftscl((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyRbs cC`s t|ƒdkrtjdƒStj|gdtƒ\}|jƒg|D]}t| dƒ^qK}t|ƒ}x‰|dkrþt|dƒ\}}gt |ƒD]!}t |||||ƒ^q¤}|rït |d|dƒ|d>> from numpy.polynomial import polynomial as P >>> P.polyfromroots((-1,0,1)) # x(x - 1)(x + 1) = x^3 - x array([ 0., -1., 0., 1.]) >>> j = complex(0,1) >>> P.polyfromroots((-j,j)) # complex returned, though values are real array([ 1.+0.j, 0.+0.j, 1.+0.j]) iittrimiiÿÿÿÿN( tlenR!tonestput as_seriestFalsetsortRtdivmodtrangeR (trootstrtptntmtittmp((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyR„s:  # 4 cC`sutj||gƒ\}}t|ƒt|ƒkrO||jc |7*|}n||jc |7*|}tj|ƒS(s Add one polynomial to another. Returns the sum of two polynomials `c1` + `c2`. The arguments are sequences of coefficients from lowest order term to highest, i.e., [1,2,3] represents the polynomial ``1 + 2*x + 3*x**2``. Parameters ---------- c1, c2 : array_like 1-D arrays of polynomial coefficients ordered from low to high. Returns ------- out : ndarray The coefficient array representing their sum. See Also -------- polysub, polymul, polydiv, polypow Examples -------- >>> from numpy.polynomial import polynomial as P >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> sum = P.polyadd(c1,c2); sum array([ 4., 4., 4.]) >>> P.polyval(2, sum) # 4 + 4(2) + 4(2**2) 28.0 (R(R)R&tsizettrimseq(tc1tc2tret((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyRÏs" cC`s|tj||gƒ\}}t|ƒt|ƒkrO||jc |8*|}n | }||jc |7*|}tj|ƒS(s# Subtract one polynomial from another. Returns the difference of two polynomials `c1` - `c2`. The arguments are sequences of coefficients from lowest order term to highest, i.e., [1,2,3] represents the polynomial ``1 + 2*x + 3*x**2``. Parameters ---------- c1, c2 : array_like 1-D arrays of polynomial coefficients ordered from low to high. Returns ------- out : ndarray Of coefficients representing their difference. See Also -------- polyadd, polymul, polydiv, polypow Examples -------- >>> from numpy.polynomial import polynomial as P >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> P.polysub(c1,c2) array([-2., 0., 2.]) >>> P.polysub(c2,c1) # -P.polysub(c1,c2) array([ 2., 0., -2.]) (R(R)R&R5R6(R7R8R9((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyR ûs# cC`s}tj|gƒ\}t|ƒdkr;|ddkr;|Stjt|ƒdd|jƒ}|dd|d<||d)|S(sMultiply a polynomial by x. Multiply the polynomial `c` by x, where x is the independent variable. Parameters ---------- c : array_like 1-D array of polynomial coefficients ordered from low to high. Returns ------- out : ndarray Array representing the result of the multiplication. Notes ----- .. versionadded:: 1.5.0 iitdtype(R(R)R&R!temptyR:(tctprd((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyR )s"" cC`s:tj||gƒ\}}tj||ƒ}tj|ƒS(s$ Multiply one polynomial by another. Returns the product of two polynomials `c1` * `c2`. The arguments are sequences of coefficients, from lowest order term to highest, e.g., [1,2,3] represents the polynomial ``1 + 2*x + 3*x**2.`` Parameters ---------- c1, c2 : array_like 1-D arrays of coefficients representing a polynomial, relative to the "standard" basis, and ordered from lowest order term to highest. Returns ------- out : ndarray Of the coefficients of their product. See Also -------- polyadd, polysub, polydiv, polypow Examples -------- >>> from numpy.polynomial import polynomial as P >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> P.polymul(c1,c2) array([ 3., 8., 14., 8., 3.]) (R(R)R!tconvolveR6(R7R8R9((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyR Ms!cC`s2tj||gƒ\}}|ddkr7tƒ‚nt|ƒ}t|ƒ}|dkru||d|d dfS||kr“|d d|fS||}|d}|d |}|}|d}x?|dkr|||c!|||8+|d8}|d8}qÈW||d|tj||d ƒfSdS(sG Divide one polynomial by another. Returns the quotient-with-remainder of two polynomials `c1` / `c2`. The arguments are sequences of coefficients, from lowest order term to highest, e.g., [1,2,3] represents ``1 + 2*x + 3*x**2``. Parameters ---------- c1, c2 : array_like 1-D arrays of polynomial coefficients ordered from low to high. Returns ------- [quo, rem] : ndarrays Of coefficient series representing the quotient and remainder. See Also -------- polyadd, polysub, polymul, polypow Examples -------- >>> from numpy.polynomial import polynomial as P >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> P.polydiv(c1,c2) (array([ 3.]), array([-8., -4.])) >>> P.polydiv(c2,c1) (array([ 0.33333333]), array([ 2.66666667, 1.33333333])) iÿÿÿÿiiN(R(R)tZeroDivisionErrorR&R6(R7R8tlen1tlen2tdlenR$R3tj((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyR ss&"         cC`sâtj|gƒ\}t|ƒ}||ks9|dkrHtdƒ‚n–|dk ro||krotdƒ‚no|dkr”tjdgd|jƒS|dkr¤|S|}x-td|dƒD]}tj ||ƒ}q¾W|SdS(sôRaise a polynomial to a power. Returns the polynomial `c` raised to the power `pow`. The argument `c` is a sequence of coefficients ordered from low to high. i.e., [1,2,3] is the series ``1 + 2*x + 3*x**2.`` Parameters ---------- c : array_like 1-D array of array of series coefficients ordered from low to high degree. pow : integer Power to which the series will be raised maxpower : integer, optional Maximum power allowed. This is mainly to limit growth of the series to unmanageable size. Default is 16 Returns ------- coef : ndarray Power series of power. See Also -------- polyadd, polysub, polymul, polydiv Examples -------- is%Power must be a non-negative integer.sPower is too largeiR:iN( R(R)tintt ValueErrortNoneR!R"R:R-R>(R<tpowtmaxpowertpowerR=R3((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyR ¬s    c C`s¿tj|ddddƒ}|jjdkr:|d}n|j}g||gD]}t|ƒ^qP\}}||kr‰tdƒ‚n|dkr¤tdƒ‚n||kr¿td ƒ‚nt||jƒ}|dkrá|Stj||dƒ}t |ƒ}||kr|d d}n‡x„t |ƒD]v} |d}||9}tj |f|j dd |ƒ} x0t |dd ƒD]} | || | | d>> from numpy.polynomial import polynomial as P >>> c = (1,2,3,4) # 1 + 2x + 3x**2 + 4x**3 >>> P.polyder(c) # (d/dx)(c) = 2 + 6x + 12x**2 array([ 2., 6., 12.]) >>> P.polyder(c,3) # (d**3/dx**3)(c) = 24 array([ 24.]) >>> P.polyder(c,scl=-1) # (d/d(-x))(c) = -2 - 6x - 12x**2 array([ -2., -6., -12.]) >>> P.polyder(c,2,-1) # (d**2/d(-x)**2)(c) = 6 + 24x array([ 6., 24.]) tndminitcopys ?bBhHiIlLqQpPgs'The order of derivation must be integeris,The order of derivation must be non-negativesThe axis must be integerR:iÿÿÿÿ( R!R"R:tcharRDRERtndimtmoveaxisR&R-R;tshape( R<R2R$taxistcdttttcnttiaxisR1R3tderRC((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyRßs64  +        # cC`s¿tj|ddddƒ}|jjdkr:|d}n|j}tj|ƒs^|g}ng||gD]}t|ƒ^qk\}} ||kr¤tdƒ‚n|dkr¿tdƒ‚nt|ƒ|kràtd ƒ‚ntj|ƒdkrtd ƒ‚ntj|ƒdkr(td ƒ‚n| |krCtd ƒ‚nt | |jƒ} |dkre|St |ƒdg|t|ƒ}tj || dƒ}xt |ƒD]ú} t|ƒ} ||9}| dkrtj |ddkƒr|dc|| 7 m``, ``np.ndim(lbnd) != 0``, or ``np.ndim(scl) != 0``. See Also -------- polyder Notes ----- Note that the result of each integration is *multiplied* by `scl`. Why is this important to note? Say one is making a linear change of variable :math:`u = ax + b` in an integral relative to `x`. Then :math:`dx = du/a`, so one will need to set `scl` equal to :math:`1/a` - perhaps not what one would have first thought. Examples -------- >>> from numpy.polynomial import polynomial as P >>> c = (1,2,3) >>> P.polyint(c) # should return array([0, 1, 1, 1]) array([ 0., 1., 1., 1.]) >>> P.polyint(c,3) # should return array([0, 0, 0, 1/6, 1/12, 1/20]) array([ 0. , 0. , 0. , 0.16666667, 0.08333333, 0.05 ]) >>> P.polyint(c,k=3) # should return array([3, 1, 1, 1]) array([ 3., 1., 1., 1.]) >>> P.polyint(c,lbnd=-2) # should return array([6, 1, 1, 1]) array([ 6., 1., 1., 1.]) >>> P.polyint(c,scl=-2) # should return array([0, -2, -2, -2]) array([ 0., -2., -2., -2.]) RJiRKs ?bBhHiIlLqQpPgs(The order of integration must be integeris-The order of integration must be non-negativesToo many integration constantsslbnd must be a scalar.sscl must be a scalar.sThe axis must be integerR:(R!R"R:RLtiterableRDRER&RMRtlistRNR-tallR;ROR(R<R2tktlbndR$RPRQRRRSRTR3R1R4RC((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyR5sLN   +    !  %'! cC`sætj|ddddƒ}|jjdkr:|d}nt|ttfƒratj|ƒ}nt|tjƒr™|r™|j |j d |j ƒ}n|d|d}x4t dt |ƒdƒD]}|| ||}qÅW|S( sY Evaluate a polynomial at points x. If `c` is of length `n + 1`, this function returns the value .. math:: p(x) = c_0 + c_1 * x + ... + c_n * x^n The parameter `x` is converted to an array only if it is a tuple or a list, otherwise it is treated as a scalar. In either case, either `x` or its elements must support multiplication and addition both with themselves and with the elements of `c`. If `c` is a 1-D array, then `p(x)` will have the same shape as `x`. If `c` is multidimensional, then the shape of the result depends on the value of `tensor`. If `tensor` is true the shape will be c.shape[1:] + x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that scalars have shape (,). Trailing zeros in the coefficients will be used in the evaluation, so they should be avoided if efficiency is a concern. Parameters ---------- x : array_like, compatible object If `x` is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, `x` or its elements must support addition and multiplication with with themselves and with the elements of `c`. c : array_like Array of coefficients ordered so that the coefficients for terms of degree n are contained in c[n]. If `c` is multidimensional the remaining indices enumerate multiple polynomials. In the two dimensional case the coefficients may be thought of as stored in the columns of `c`. tensor : boolean, optional If True, the shape of the coefficient array is extended with ones on the right, one for each dimension of `x`. Scalars have dimension 0 for this action. The result is that every column of coefficients in `c` is evaluated for every element of `x`. If False, `x` is broadcast over the columns of `c` for the evaluation. This keyword is useful when `c` is multidimensional. The default value is True. .. versionadded:: 1.7.0 Returns ------- values : ndarray, compatible object The shape of the returned array is described above. See Also -------- polyval2d, polygrid2d, polyval3d, polygrid3d Notes ----- The evaluation uses Horner's method. Examples -------- >>> from numpy.polynomial.polynomial import polyval >>> polyval(1, [1,2,3]) 6.0 >>> a = np.arange(4).reshape(2,2) >>> a array([[0, 1], [2, 3]]) >>> polyval(a, [1,2,3]) array([[ 1., 6.], [ 17., 34.]]) >>> coef = np.arange(4).reshape(2,2) # multidimensional coefficients >>> coef array([[0, 1], [2, 3]]) >>> polyval([1,2], coef, tensor=True) array([[ 2., 4.], [ 4., 7.]]) >>> polyval([1,2], coef, tensor=False) array([ 2., 7.]) RJiRKis ?bBhHiIlLqQpPgiÿÿÿÿi(i(R!R"R:RLt isinstancettupleRWtasarraytndarraytreshapeRORMR-R&(txR<ttensortc0R3((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyR°sQ   cC`sÜtj|ddddƒ}|jjdkrB|jtjƒ}nt|ttfƒritj |ƒ}nt|tj ƒrÅ|r¡|j |j d|j ƒ}qÅ|j |j krÅtdƒ‚qÅntj||ddƒS( s} Evaluate a polynomial specified by its roots at points x. If `r` is of length `N`, this function returns the value .. math:: p(x) = \prod_{n=1}^{N} (x - r_n) The parameter `x` is converted to an array only if it is a tuple or a list, otherwise it is treated as a scalar. In either case, either `x` or its elements must support multiplication and addition both with themselves and with the elements of `r`. If `r` is a 1-D array, then `p(x)` will have the same shape as `x`. If `r` is multidimensional, then the shape of the result depends on the value of `tensor`. If `tensor is ``True`` the shape will be r.shape[1:] + x.shape; that is, each polynomial is evaluated at every value of `x`. If `tensor` is ``False``, the shape will be r.shape[1:]; that is, each polynomial is evaluated only for the corresponding broadcast value of `x`. Note that scalars have shape (,). .. versionadded:: 1.12 Parameters ---------- x : array_like, compatible object If `x` is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, `x` or its elements must support addition and multiplication with with themselves and with the elements of `r`. r : array_like Array of roots. If `r` is multidimensional the first index is the root index, while the remaining indices enumerate multiple polynomials. For instance, in the two dimensional case the roots of each polynomial may be thought of as stored in the columns of `r`. tensor : boolean, optional If True, the shape of the roots array is extended with ones on the right, one for each dimension of `x`. Scalars have dimension 0 for this action. The result is that every column of coefficients in `r` is evaluated for every element of `x`. If False, `x` is broadcast over the columns of `r` for the evaluation. This keyword is useful when `r` is multidimensional. The default value is True. Returns ------- values : ndarray, compatible object The shape of the returned array is described above. See Also -------- polyroots, polyfromroots, polyval Examples -------- >>> from numpy.polynomial.polynomial import polyvalfromroots >>> polyvalfromroots(1, [1,2,3]) 0.0 >>> a = np.arange(4).reshape(2,2) >>> a array([[0, 1], [2, 3]]) >>> polyvalfromroots(a, [-1, 0, 1]) array([[ -0., 0.], [ 6., 24.]]) >>> r = np.arange(-2, 2).reshape(2,2) # multidimensional coefficients >>> r # each column of r defines one polynomial array([[-2, -1], [ 0, 1]]) >>> b = [-2, 1] >>> polyvalfromroots(b, r, tensor=True) array([[-0., 3.], [ 3., 0.]]) >>> polyvalfromroots(b, r, tensor=False) array([-0., 0.]) RJiRKis ?bBhHiIlLqQpPs,x.ndim must be < r.ndim when tensor == FalseRP(i(R!R"R:RLtastypetdoubleR[R\RWR]R^R_RORMREtprod(R`R/Ra((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyRsK cC`smy%tj||fddƒ\}}Wntk rDtdƒ‚nXt||ƒ}t||dtƒ}|S(sJ Evaluate a 2-D polynomial at points (x, y). This function returns the value .. math:: p(x,y) = \sum_{i,j} c_{i,j} * x^i * y^j The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`. If `c` has fewer than two dimensions, ones are implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape. Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points `(x, y)`, where `x` and `y` must have the same shape. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j is contained in `c[i,j]`. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients. Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points formed with pairs of corresponding values from `x` and `y`. See Also -------- polyval, polygrid2d, polyval3d, polygrid3d Notes ----- .. versionadded:: 1.7.0 RKisx, y are incompatibleRa(R!R"t ExceptionRERR*(R`tyR<((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyRhs/% cC`s"t||ƒ}t||ƒ}|S(sÒ Evaluate a 2-D polynomial on the Cartesian product of x and y. This function returns the values: .. math:: p(a,b) = \sum_{i,j} c_{i,j} * a^i * b^j where the points `(a, b)` consist of all pairs formed by taking `a` from `x` and `b` from `y`. The resulting points form a grid with `x` in the first dimension and `y` in the second. The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`. If `c` has fewer than two dimensions, ones are implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape + y.shape. Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points in the Cartesian product of `x` and `y`. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficients for terms of degree i,j are contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients. Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points in the Cartesian product of `x` and `y`. See Also -------- polyval, polyval2d, polyval3d, polygrid3d Notes ----- .. versionadded:: 1.7.0 (R(R`RgR<((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyR¡s2cC`sˆy+tj|||fddƒ\}}}Wntk rJtdƒ‚nXt||ƒ}t||dtƒ}t||dtƒ}|S(s” Evaluate a 3-D polynomial at points (x, y, z). This function returns the values: .. math:: p(x,y,z) = \sum_{i,j,k} c_{i,j,k} * x^i * y^j * z^k The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`. If `c` has fewer than 3 dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape. Parameters ---------- x, y, z : array_like, compatible object The three dimensional series is evaluated at the points `(x, y, z)`, where `x`, `y`, and `z` must have the same shape. If any of `x`, `y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension greater than 3 the remaining indices enumerate multiple sets of coefficients. Returns ------- values : ndarray, compatible object The values of the multidimensional polynomial on points formed with triples of corresponding values from `x`, `y`, and `z`. See Also -------- polyval, polyval2d, polygrid2d, polygrid3d Notes ----- .. versionadded:: 1.7.0 RKisx, y, z are incompatibleRa(R!R"RfRERR*(R`RgtzR<((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyRØs0+ cC`s1t||ƒ}t||ƒ}t||ƒ}|S(s@ Evaluate a 3-D polynomial on the Cartesian product of x, y and z. This function returns the values: .. math:: p(a,b,c) = \sum_{i,j,k} c_{i,j,k} * a^i * b^j * c^k where the points `(a, b, c)` consist of all triples formed by taking `a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form a grid with `x` in the first dimension, `y` in the second, and `z` in the third. The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`. If `c` has fewer than three dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape + y.shape + z.shape. Parameters ---------- x, y, z : array_like, compatible objects The three dimensional series is evaluated at the points in the Cartesian product of `x`, `y`, and `z`. If `x`,`y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficients for terms of degree i,j are contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients. Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points in the Cartesian product of `x` and `y`. See Also -------- polyval, polyval2d, polygrid2d, polyval3d Notes ----- .. versionadded:: 1.7.0 (R(R`RgRhR<((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyRs5cC`st|ƒ}||kr'tdƒ‚n|dkrBtdƒ‚ntj|ddddƒd}|df|j}|j}tj|d|ƒ}|dd|d<|dkrò||dcC`sâtj|ƒd}tj|ƒd}tj|ƒ}|jdkse|jjdkse|jdkrttdƒ‚n|jƒdkr•tdƒ‚n|jdkr³tdƒ‚n|jdkrÑtdƒ‚n|jdksï|jd krþtd ƒ‚nt |ƒt |ƒkr%td ƒ‚n|jdkrV|}|d}t ||ƒ}nDtj |ƒ}|d }t |ƒ}t ||ƒd d …|f}|j } |j } |d k r'tj|ƒd}|jdkrétdƒ‚nt |ƒt |ƒkrtdƒ‚n| |} | |} n|d krUt |ƒtj|jƒj}nt| jjtjƒr¤tjtj| jƒtj| jƒjdƒƒ} n!tjtj| ƒjdƒƒ} d| | dk= 1.11.0 a list of integers specifying the degrees of the terms to include may be used instead. rcond : float, optional Relative condition number of the fit. Singular values smaller than `rcond`, relative to the largest singular value, will be ignored. The default value is ``len(x)*eps``, where `eps` is the relative precision of the platform's float type, about 2e-16 in most cases. full : bool, optional Switch determining the nature of the return value. When ``False`` (the default) just the coefficients are returned; when ``True``, diagnostic information from the singular value decomposition (used to solve the fit's matrix equation) is also returned. w : array_like, shape (`M`,), optional Weights. If not None, the contribution of each point ``(x[i],y[i])`` to the fit is weighted by `w[i]`. Ideally the weights are chosen so that the errors of the products ``w[i]*y[i]`` all have the same variance. The default value is None. .. versionadded:: 1.5.0 Returns ------- coef : ndarray, shape (`deg` + 1,) or (`deg` + 1, `K`) Polynomial coefficients ordered from low to high. If `y` was 2-D, the coefficients in column `k` of `coef` represent the polynomial fit to the data in `y`'s `k`-th column. [residuals, rank, singular_values, rcond] : list These values are only returned if `full` = True resid -- sum of squared residuals of the least squares fit rank -- the numerical rank of the scaled Vandermonde matrix sv -- singular values of the scaled Vandermonde matrix rcond -- value of `rcond`. For more details, see `linalg.lstsq`. Raises ------ RankWarning Raised if the matrix in the least-squares fit is rank deficient. The warning is only raised if `full` == False. The warnings can be turned off by: >>> import warnings >>> warnings.simplefilter('ignore', RankWarning) See Also -------- chebfit, legfit, lagfit, hermfit, hermefit polyval : Evaluates a polynomial. polyvander : Vandermonde matrix for powers. linalg.lstsq : Computes a least-squares fit from the matrix. scipy.interpolate.UnivariateSpline : Computes spline fits. Notes ----- The solution is the coefficients of the polynomial `p` that minimizes the sum of the weighted squared errors .. math :: E = \sum_j w_j^2 * |y_j - p(x_j)|^2, where the :math:`w_j` are the weights. This problem is solved by setting up the (typically) over-determined matrix equation: .. math :: V(x) * c = w * y, where `V` is the weighted pseudo Vandermonde matrix of `x`, `c` are the coefficients to be solved for, `w` are the weights, and `y` are the observed values. This equation is then solved using the singular value decomposition of `V`. If some of the singular values of `V` are so small that they are neglected (and `full` == ``False``), a `RankWarning` will be raised. This means that the coefficient values may be poorly determined. Fitting to a lower order polynomial will usually get rid of the warning (but may not be what you want, of course; if you have independent reason(s) for choosing the degree which isn't working, you may have to: a) reconsider those reasons, and/or b) reconsider the quality of your data). The `rcond` parameter can also be set to a value smaller than its default, but the resulting fit may be spurious and have large contributions from roundoff error. Polynomial fits using double precision tend to "fail" at about (polynomial) degree 20. Fits using Chebyshev or Legendre series are generally better conditioned, but much can still depend on the distribution of the sample points and the smoothness of the data. If the quality of the fit is inadequate, splines may be a good alternative. Examples -------- >>> from numpy.polynomial import polynomial as P >>> x = np.linspace(-1,1,51) # x "data": [-1, -0.96, ..., 0.96, 1] >>> y = x**3 - x + np.random.randn(len(x)) # x^3 - x + N(0,1) "noise" >>> c, stats = P.polyfit(x,y,3,full=True) >>> c # c[0], c[2] should be approx. 0, c[1] approx. -1, c[3] approx. 1 array([ 0.01909725, -1.30598256, -0.00577963, 1.02644286]) >>> stats # note the large SSR, explaining the rather poor results [array([ 38.06116253]), 4, array([ 1.38446749, 1.32119158, 0.50443316, 0.28853036]), 1.1324274851176597e-014] Same thing without the added noise >>> y = x**3 - x >>> c, stats = P.polyfit(x,y,3,full=True) >>> c # c[0], c[2] should be "very close to 0", c[1] ~= -1, c[3] ~= 1 array([ -1.73362882e-17, -1.00000000e+00, -2.67471909e-16, 1.00000000e+00]) >>> stats # note the minuscule SSR [array([ 7.46346754e-31]), 4, array([ 1.38446749, 1.32119158, 0.50443316, 0.28853036]), 1.1324274851176597e-014] gitiuis0deg must be an int or non-empty 1-D array of intsexpected deg >= 0sexpected 1D vector for xsexpected non-empty vector for xisexpected 1D or 2D array for ys$expected x and y to have same lengthiÿÿÿÿNsexpected 1D vector for ws$expected x and w to have same lengthR:s!The fit may be poorly conditionedt stacklevel( R!R]RMR:tkindR5t TypeErrortminRER&RR+tTRFtfinfotepst issubclassttypetcomplexfloatingtsqrttsquaretrealtimagtsumtlatlstsqtzerosROtwarningstwarnR(t RankWarning(R`RgRitrcondtfulltwtlmaxtordertvantlhstrhsR$R<tresidstranktstcctmsg((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyRsjŽ0         "7!+,  cC`sìtj|gƒ\}t|ƒdkr6tdƒ‚nt|ƒdkrhtj|d |dggƒSt|ƒd}tj||fd|jƒ}|jdƒ|d|d…}d|d<|dd…dfc|d |d8<|S( sê Return the companion matrix of c. The companion matrix for power series cannot be made symmetric by scaling the basis, so this function differs from those for the orthogonal polynomials. Parameters ---------- c : array_like 1-D array of polynomial coefficients ordered from low to high degree. Returns ------- mat : ndarray Companion matrix of dimensions (deg, deg). Notes ----- .. versionadded:: 1.7.0 is.Series must have maximum degree of at least 1.iiR:iÿÿÿÿN.( R(R)R&RER!R"RŠR:R_(R<R1tmattbot((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyt polycompanionàs   (cC`s•tj|gƒ\}t|ƒdkr=tjgd|jƒSt|ƒdkrltj|d |dgƒSt|ƒ}tj|ƒ}|j ƒ|S(sU Compute the roots of a polynomial. Return the roots (a.k.a. "zeros") of the polynomial .. math:: p(x) = \sum_i c[i] * x^i. Parameters ---------- c : 1-D array_like 1-D array of polynomial coefficients. Returns ------- out : ndarray Array of the roots of the polynomial. If all the roots are real, then `out` is also real, otherwise it is complex. See Also -------- chebroots Notes ----- The root estimates are obtained as the eigenvalues of the companion matrix, Roots far from the origin of the complex plane may have large errors due to the numerical instability of the power series for such values. Roots with multiplicity greater than 1 will also show larger errors as the value of the series near such points is relatively insensitive to errors in the roots. Isolated roots near the origin can be improved by a few iterations of Newton's method. Examples -------- >>> import numpy.polynomial.polynomial as poly >>> poly.polyroots(poly.polyfromroots((-1,0,1))) array([-1., 0., 1.]) >>> poly.polyroots(poly.polyfromroots((-1,0,1))).dtype dtype('float64') >>> j = complex(0,1) >>> poly.polyroots(poly.polyfromroots((-j,0,j))) array([ 0.00000000e+00+0.j, 0.00000000e+00+1.j, 2.77555756e-17-1.j]) iR:ii( R(R)R&R!R"R:RRˆteigvalsR+(R<R2R/((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyRs.  cB`sÂeZdZeeƒZeeƒZeeƒZ ee ƒZ ee ƒZ eeƒZeeƒZeeƒZeeƒZeeƒZeeƒZeeƒZdZejeƒZ ejeƒZ!RS(s A power series class. The Polynomial class provides the standard Python numerical methods '+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the attributes and methods listed in the `ABCPolyBase` documentation. Parameters ---------- coef : array_like Polynomial coefficients in order of increasing degree, i.e., ``(1, 2, 3)`` give ``1 + 2*x + 3*x**2``. domain : (2,) array_like, optional Domain to use. The interval ``[domain[0], domain[1]]`` is mapped to the interval ``[window[0], window[1]]`` by shifting and scaling. The default value is [-1, 1]. window : (2,) array_like, optional Window, see `domain` for its use. The default value is [-1, 1]. .. versionadded:: 1.6.0 tpoly("t__name__t __module__t__doc__t staticmethodRt_addR t_subR t_mulR t_divR t_powRt_valRt_intRt_derRt_fitRt_lineRt_rootsRt _fromrootstnicknameR!R"Rtdomaintwindow(((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyRFs             (4R¢t __future__RRRt__all__R‹tnumpyR!t numpy.linalgtlinalgRˆtnumpy.core.multiarrayRtRR(t _polybaseR ttrimcoefRR"RRRRRRRR R R R RFR RRtTrueRRRRRRRRRR*RRRR(((s:/tmp/pip-build-fiC0ax/numpy/numpy/polynomial/polynomial.pyt8sP    " K , . $ & 9 3V{ ` X 9 7 ; ; 8 < CÛ ( >