B @`C@sLdZddlZddlZddlZddlZddlmZdgZ Gdddej Z dS)a Abstract base class for the various polynomial Classes. The ABCPolyBase class provides the methods needed to implement the common API for the various polynomial classes. It operates as a mixin, but uses the abc module from the stdlib, hence it is only available for Python >= 2.6. N) polyutils ABCPolyBasec @s|eZdZdZdZdZdZedddddd d d d d d Z eddddddddddd Z e j dk Z eejddZeejddZeejddZeejd d!Zeejd"d#Zeejd$d%Zeejd&d'Zeejdd(d)Zeejd*d+Zeejd,d-Zeejd.d/Zeejd0d1Zeejd2d3Zeejd4d5Zeejd6d7Z d8d9Z!d:d;Z"dd?Z$d@dAZ%ddBdCZ&dDdEZ'dFdGZ(dHdIZ)dJdKZ*e+dLdMZ,e+dNdOZ-e+dPdQZ.edRdSZ/dTdUZ0dVdWZ1dXdYZ2dZd[Z3d\d]Z4d^d_Z5d`daZ6dbdcZ7dddeZ8dfdgZ9dhdiZ:djdkZ;dldmZdrdsZ?dtduZ@dvdwZAdxdyZBdzd{ZCd|d}ZDd~dZEddZFddZGddZHddZIddZJddZKddZLdddZMddZNdddZOddZPdgdfddZQdddZRddZSdddZTe+dddZUe+gdfddZVe+dddZWe+dddZXe+dddZYdS)raAn abstract base class for immutable series classes. ABCPolyBase provides the standard Python numerical methods '+', '-', '*', '//', '%', 'divmod', '**', and '()' along with the methods listed below. .. versionadded:: 1.9.0 Parameters ---------- coef : array_like Series coefficients in order of increasing degree, i.e., ``(1, 2, 3)`` gives ``1*P_0(x) + 2*P_1(x) + 3*P_2(x)``, where ``P_i`` is the basis polynomials of degree ``i``. 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 the derived class domain. window : (2,) array_like, optional Window, see domain for its use. The default value is the derived class window. Attributes ---------- coef : (N,) ndarray Series coefficients in order of increasing degree. domain : (2,) ndarray Domain that is mapped to window. window : (2,) ndarray Window that domain is mapped to. Class Attributes ---------------- maxpower : int Maximum power allowed, i.e., the largest number ``n`` such that ``p(x)**n`` is allowed. This is to limit runaway polynomial size. domain : (2,) ndarray Default domain of the class. window : (2,) ndarray Default window of the class. Ndu⁰¹²³u⁴u⁵u⁶u⁷u⁸u⁹) 0123456789u₀u₁u₂u₃u₄u₅u₆u₇u₈u₉ntcCsdS)N)selfrr>/tmp/pip-unpacked-wheel-ecq9wray/numpy/polynomial/_polybase.pydomainfszABCPolyBase.domaincCsdS)Nr)rrrrwindowkszABCPolyBase.windowcCsdS)Nr)rrrr basis_namepszABCPolyBase.basis_namecCsdS)Nr)c1c2rrr_adduszABCPolyBase._addcCsdS)Nr)rrrrr_subzszABCPolyBase._subcCsdS)Nr)rrrrr_mulszABCPolyBase._mulcCsdS)Nr)rrrrr_divszABCPolyBase._divcCsdS)Nr)cpowmaxpowerrrr_powszABCPolyBase._powcCsdS)Nr)xr rrr_valszABCPolyBase._valcCsdS)Nr)r mklbndsclrrr_intszABCPolyBase._intcCsdS)Nr)r r&r)rrr_derszABCPolyBase._dercCsdS)Nr)r$ydegrcondfullrrr_fitszABCPolyBase._fitcCsdS)Nr)offr)rrr_lineszABCPolyBase._linecCsdS)Nr)r rrr_rootsszABCPolyBase._rootscCsdS)Nr)rrrr _fromrootsszABCPolyBase._fromrootscCs6t|jt|jkrdSt|j|jks.dSdSdS)aGCheck if coefficients match. .. versionadded:: 1.6.0 Parameters ---------- other : class instance The other class must have the ``coef`` attribute. Returns ------- bool : boolean True if the coefficients are the same, False otherwise. FTN)lencoefnpall)rotherrrr has_samecoefs zABCPolyBase.has_samecoefcCst|j|jkS)a?Check if domains match. .. versionadded:: 1.6.0 Parameters ---------- other : class instance The other class must have the ``domain`` attribute. Returns ------- bool : boolean True if the domains are the same, False otherwise. )r8r9r)rr:rrrhas_samedomainszABCPolyBase.has_samedomaincCst|j|jkS)a?Check if windows match. .. versionadded:: 1.6.0 Parameters ---------- other : class instance The other class must have the ``window`` attribute. Returns ------- bool : boolean True if the windows are the same, False otherwise. )r8r9r)rr:rrrhas_samewindowszABCPolyBase.has_samewindowcCs t||jS)aCheck if types match. .. versionadded:: 1.7.0 Parameters ---------- other : object Class instance. Returns ------- bool : boolean True if other is same class as self ) isinstance __class__)rr:rrr has_sametypeszABCPolyBase.has_sametypecCs`t|tr\t||js tdn6t|j|jksrr? TypeErrorr8r9rrr7)rr:rrr_get_coefficientss    zABCPolyBase._get_coefficientscCstj|gdd\}||_|dk rLtj|gdd\}t|dkrFtd||_|dk rtj|gdd\}t|dkrztd||_dS)NF)trimz$Domain has wrong number of elements.z$Window has wrong number of elements.)pu as_seriesr7r6 ValueErrorrr)rr7rrrrr__init__"s  zABCPolyBase.__init__cCsZt|jdd}t|jdd}t|jdd}|jj}|d|d|d|dS)N(z , domain=z , window=))reprr7rrr?__name__)rr7rrnamerrr__repr__2s zABCPolyBase.__repr__cCsP|dkr|S|dkr0td|d|jd|dkrD||jS||jS)N)asciiunicodezUnsupported format string 'z ' passed to z4.__format__. Valid options are 'ascii' and 'unicode'rR)__str__rGr?_generate_string_str_term_ascii_str_term_unicode)rZfmt_strrrr __format__9s zABCPolyBase.__format__cCs|jr||jS||jS)N) _use_unicoderUrWrV)rrrrrTFs zABCPolyBase.__str__c Cs tdd}|dkrd}|jd}xt|jddD]\}}|d7}t|d}y$|dkrnd|}n d| }Wntk rd|}YnX|||d 7}t|d d t|}|t|jdddkr|d 7}||kr| dd d}||7}q|dkrb||d}d}n ||d||d}d}dj}g}xt|jD]\}}|dkr||} nNt|tjsd ||d } n.t |sd||} nd || } | |||} | d kr"| } n| d | } |dkrB|| } | | qW|rbd |} nd} d| dS)Nrrr$Fz + xTz + z\color{{LightGray}}{{{}}}z + (rLz - r z\,rQr z $x \mapsto $) mapparmsrrrqr_r7r>numbersRealr8Zsignbitrpappendjoin) rr1ZscaleZtermroZmutepartsrdr Zcoef_strZterm_strpartbodyrrr _repr_latex_sB      zABCPolyBase._repr_latex_cCs8|j}|j|d<|j|d<|j|d<|S)Nr7rr)__dict__copyr7rr)rretrrr __getstate__s  zABCPolyBase.__getstate__cCs ||_dS)N)r})rdictrrr __setstate__szABCPolyBase.__setstate__cCs.t|j|j\}}|||}|||jS)N)rErtrrr%r7)rargr1r)rrr__call__s zABCPolyBase.__call__cCs t|jS)N)iterr7)rrrr__iter__szABCPolyBase.__iter__cCs t|jS)N)r6r7)rrrr__len__szABCPolyBase.__len__cCs||j |j|jS)N)r?r7rr)rrrr__neg__szABCPolyBase.__neg__cCs|S)Nr)rrrr__pos__szABCPolyBase.__pos__cCsD||}y||j|}Wntk r0tSX|||j|jS)N)rBrr7 ExceptionNotImplementedr?rr)rr: othercoefr7rrr__add__s  zABCPolyBase.__add__cCsD||}y||j|}Wntk r0tSX|||j|jS)N)rBrr7rrr?rr)rr:rr7rrr__sub__s  zABCPolyBase.__sub__cCsD||}y||j|}Wntk r0tSX|||j|jS)N)rBrr7rrr?rr)rr:rr7rrr__mul__s  zABCPolyBase.__mul__cCs>t|tjrt|tr4tdt|dt|d||S)Nz&unsupported types for true division: 'z', '')r>ruNumberboolrAtype __floordiv__)rr:rrr __truediv__ szABCPolyBase.__truediv__cCs||}|tkr|S|dS)Nr) __divmod__r)rr:resrrrrs zABCPolyBase.__floordiv__cCs||}|tkr|S|dS)Nr)rr)rr:rrrr__mod__s zABCPolyBase.__mod__cCsv||}y||j|\}}Wn(tk r6Yntk rHtSX|||j|j}|||j|j}||fS)N) rBrr7ZeroDivisionErrorrrr?rr)rr:rquoremrrrr"s zABCPolyBase.__divmod__cCs*|j|j||jd}|||j|j}|S)N)r")r#r7r"r?rr)rr:r7rrrr__pow__.szABCPolyBase.__pow__cCs:y|||j}Wntk r&tSX|||j|jS)N)rr7rrr?rr)rr:r7rrr__radd__3s zABCPolyBase.__radd__cCs:y|||j}Wntk r&tSX|||j|jS)N)rr7rrr?rr)rr:r7rrr__rsub__:s zABCPolyBase.__rsub__cCs:y|||j}Wntk r&tSX|||j|jS)N)rr7rrr?rr)rr:r7rrr__rmul__As zABCPolyBase.__rmul__cCs ||S)N) __rfloordiv__)rr:rrr__rdiv__HszABCPolyBase.__rdiv__cCstS)N)r)rr:rrr __rtruediv__LszABCPolyBase.__rtruediv__cCs||}|tkr|S|dS)Nr) __rdivmod__r)rr:rrrrrQs zABCPolyBase.__rfloordiv__cCs||}|tkr|S|dS)Nr)rr)rr:rrrr__rmod__Ws zABCPolyBase.__rmod__cCsly|||j\}}Wn(tk r,Yntk r>tSX|||j|j}|||j|j}||fS)N)rr7rrrr?rr)rr:rrrrrr]szABCPolyBase.__rdivmod__cCsVt||joPt|j|jkoPt|j|jkoP|jj|jjkoPt|j|jk}|S)N)r>r?r8r9rrr7shape)rr:rrrr__eq__hs  zABCPolyBase.__eq__cCs || S)N)r)rr:rrr__ne__pszABCPolyBase.__ne__cCs||j|j|jS)zoReturn a copy. Returns ------- new_series : series Copy of self. )r?r7rr)rrrrr~ws zABCPolyBase.copycCs t|dS)zThe degree of the series. .. versionadded:: 1.5.0 Returns ------- degree : int Degree of the series, one less than the number of coefficients. r)r6)rrrrdegrees zABCPolyBase.degreecCs||dS)aTruncate series to the given degree. Reduce the degree of the series to `deg` by discarding the high order terms. If `deg` is greater than the current degree a copy of the current series is returned. This can be useful in least squares where the coefficients of the high degree terms may be very small. .. versionadded:: 1.5.0 Parameters ---------- deg : non-negative int The series is reduced to degree `deg` by discarding the high order terms. The value of `deg` must be a non-negative integer. Returns ------- new_series : series New instance of series with reduced degree. r)truncate)rr-rrrcutdegszABCPolyBase.cutdegrcCs t|j|}|||j|jS)aRemove trailing coefficients Remove trailing coefficients until a coefficient is reached whose absolute value greater than `tol` or the beginning of the series is reached. If all the coefficients would be removed the series is set to ``[0]``. A new series instance is returned with the new coefficients. The current instance remains unchanged. Parameters ---------- tol : non-negative number. All trailing coefficients less than `tol` will be removed. Returns ------- new_series : series Contains the new set of coefficients. )rEZtrimcoefr7r?rr)rZtolr7rrrrCszABCPolyBase.trimcCsVt|}||ks|dkr td|t|jkr6|j}n|jd|}|||j|jS)aTruncate series to length `size`. Reduce the series to length `size` by discarding the high degree terms. The value of `size` must be a positive integer. This can be useful in least squares where the coefficients of the high degree terms may be very small. Parameters ---------- size : positive int The series is reduced to length `size` by discarding the high degree terms. The value of `size` must be a positive integer. Returns ------- new_series : series New instance of series with truncated coefficients. rzsize must be a positive integerN)intrGr6r7r?rr)rsizeisizer7rrrrszABCPolyBase.truncatecCs<|dkr|j}|dkr|j}|dkr*|j}||j||dS)a Convert series to a different kind and/or domain and/or window. Parameters ---------- domain : array_like, optional The domain of the converted series. If the value is None, the default domain of `kind` is used. kind : class, optional The polynomial series type class to which the current instance should be converted. If kind is None, then the class of the current instance is used. window : array_like, optional The window of the converted series. If the value is None, the default window of `kind` is used. Returns ------- new_series : series The returned class can be of different type than the current instance and/or have a different domain and/or different window. Notes ----- Conversion between domains and class types can result in numerically ill defined series. Examples -------- N)r)r?rridentity)rrkindrrrrconverts zABCPolyBase.convertcCst|j|jS)aReturn the mapping parameters. The returned values define a linear map ``off + scl*x`` that is applied to the input arguments before the series is evaluated. The map depends on the ``domain`` and ``window``; if the current ``domain`` is equal to the ``window`` the resulting map is the identity. If the coefficients of the series instance are to be used by themselves outside this class, then the linear function must be substituted for the ``x`` in the standard representation of the base polynomials. Returns ------- off, scl : float or complex The mapping function is defined by ``off + scl*x``. Notes ----- If the current domain is the interval ``[l1, r1]`` and the window is ``[l2, r2]``, then the linear mapping function ``L`` is defined by the equations:: L(l1) = l2 L(r1) = r2 )rErtrr)rrrrrtszABCPolyBase.mapparmsrcCsP|\}}|dkrd}n |||}||j|||d|}|||j|jS)aIntegrate. Return a series instance that is the definite integral of the current series. Parameters ---------- m : non-negative int The number of integrations to perform. k : array_like Integration constants. The first constant is applied to the first integration, the second to the second, and so on. The list of values must less than or equal to `m` in length and any missing values are set to zero. lbnd : Scalar The lower bound of the definite integral. Returns ------- new_series : series A new series representing the integral. The domain is the same as the domain of the integrated series. Nrg?)rtr*r7r?rr)rr&r'r(r1r)r7rrrinteg!s   zABCPolyBase.integcCs.|\}}||j||}|||j|jS)aDifferentiate. Return a series instance of that is the derivative of the current series. Parameters ---------- m : non-negative int Find the derivative of order `m`. Returns ------- new_series : series A new series representing the derivative. The domain is the same as the domain of the differentiated series. )rtr+r7r?rr)rr&r1r)r7rrrderivBs zABCPolyBase.derivcCs||j}t||j|jS)a,Return the roots of the series polynomial. Compute the roots for the series. Note that the accuracy of the roots decrease the further outside the domain they lie. Returns ------- roots : ndarray Array containing the roots of the series. )r3r7rE mapdomainrr)rrootsrrrrXs zABCPolyBase.rootscCs4|dkr|j}t|d|d|}||}||fS)aReturn x, y values at equally spaced points in domain. Returns the x, y values at `n` linearly spaced points across the domain. Here y is the value of the polynomial at the points x. By default the domain is the same as that of the series instance. This method is intended mostly as a plotting aid. .. versionadded:: 1.5.0 Parameters ---------- n : int, optional Number of point pairs to return. The default value is 100. domain : {None, array_like}, optional If not None, the specified domain is used instead of that of the calling instance. It should be of the form ``[beg,end]``. The default is None which case the class domain is used. Returns ------- x, y : ndarray x is equal to linspace(self.domain[0], self.domain[1], n) and y is the series evaluated at element of x. Nrr)rr8linspace)rnrr$r,rrrrgs zABCPolyBase.linspaceFc Cs|dkrt|}nt|tkr2t|dkr2|j}|dkr@|j}t|||} |j| |||||d} |r| \} } || ||d| fS| } || ||dSdS)ao Least squares fit to data. Return a series instance that is the least squares fit to the data `y` sampled at `x`. The domain of the returned instance can be specified and this will often result in a superior fit with less chance of ill conditioning. Parameters ---------- x : array_like, shape (M,) x-coordinates of the M sample points ``(x[i], y[i])``. y : array_like, shape (M,) y-coordinates of the M sample points ``(x[i], y[i])``. deg : int or 1-D array_like Degree(s) of the fitting polynomials. If `deg` is a single integer all terms up to and including the `deg`'th term are included in the fit. For NumPy versions >= 1.11.0 a list of integers specifying the degrees of the terms to include may be used instead. domain : {None, [beg, end], []}, optional Domain to use for the returned series. If ``None``, then a minimal domain that covers the points `x` is chosen. If ``[]`` the class domain is used. The default value was the class domain in NumPy 1.4 and ``None`` in later versions. The ``[]`` option was added in numpy 1.5.0. rcond : float, optional Relative condition number of the fit. Singular values smaller than this relative to the largest singular value will be ignored. The default value is len(x)*eps, where eps is the relative precision of the float type, about 2e-16 in most cases. full : bool, optional Switch determining nature of return value. When it is False (the default) just the coefficients are returned, when True diagnostic information from the singular value decomposition 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 window : {[beg, end]}, optional Window to use for the returned series. The default value is the default class domain .. versionadded:: 1.6.0 Returns ------- new_series : series A series that represents the least squares fit to the data and has the domain and window specified in the call. If the coefficients for the unscaled and unshifted basis polynomials are of interest, do ``new_series.convert().coef``. [resid, rank, sv, 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`. Nr)wr.r/)rr) rE getdomainrlistr6rrrr0) rkr$r,r-rr.r/rrZxnewrr7statusrrrfitsG zABCPolyBase.fitc Cstj|gdd\}|dkr&t|}nt|tkrDt|dkrD|j}|dkrR|j}t|}t||\}}|||}| |||}||||dS)a+Return series instance that has the specified roots. Returns a series representing the product ``(x - r[0])*(x - r[1])*...*(x - r[n-1])``, where ``r`` is a list of roots. Parameters ---------- roots : array_like List of roots. domain : {[], None, array_like}, optional Domain for the resulting series. If None the domain is the interval from the smallest root to the largest. If [] the domain is the class domain. The default is []. window : {None, array_like}, optional Window for the returned series. If None the class window is used. The default is None. Returns ------- new_series : series Series with the specified roots. F)rCNr)rr) rErFrrrr6rrrtr5) rkrrrr-r1r)Zrnewr7rrr fromrootss  zABCPolyBase.fromrootscCsD|dkr|j}|dkr|j}t||\}}|||}||||S)a6Identity function. If ``p`` is the returned series, then ``p(x) == x`` for all values of x. Parameters ---------- domain : {None, array_like}, optional If given, the array must be of the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the domain. If None is given then the class domain is used. The default is None. window : {None, array_like}, optional If given, the resulting array must be if the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the window. If None is given then the class window is used. The default is None. Returns ------- new_series : series Series of representing the identity. N)rrrErtr2)rkrrr1r)r7rrrrs zABCPolyBase.identitycCsT|dkr|j}|dkr|j}t|}||ks4|dkr= 0. domain : {None, array_like}, optional If given, the array must be of the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the domain. If None is given then the class domain is used. The default is None. window : {None, array_like}, optional If given, the resulting array must be if the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the window. If None is given then the class window is used. The default is None. Returns ------- new_series : series A series with the coefficient of the `deg` term set to one and all others zero. Nrz deg must be non-negative integerr)rrrrG)rkr-rrZidegrrrbasis)szABCPolyBase.basiscCs*|dkr|j}|dkr|j}||||S)aConvert series to series of this class. The `series` is expected to be an instance of some polynomial series of one of the types supported by by the numpy.polynomial module, but could be some other class that supports the convert method. .. versionadded:: 1.7.0 Parameters ---------- series : series The series instance to be converted. domain : {None, array_like}, optional If given, the array must be of the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the domain. If None is given then the class domain is used. The default is None. window : {None, array_like}, optional If given, the resulting array must be if the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the window. If None is given then the class window is used. The default is None. Returns ------- new_series : series A series of the same kind as the calling class and equal to `series` when evaluated. See Also -------- convert : similar instance method N)rrr)rkZseriesrrrrrcastPs $zABCPolyBase.cast)N)NN)r)NNN)r)rN)NNFNN)NN)NN)NN)ZrN __module__ __qualname____doc____hash__Z__array_ufunc__r"r` maketransZ_superscript_mappingrjosrOrYpropertyabcabstractmethodrrr staticmethodrrrrr#r%r*r+r0r2r3r5r;r<r=r@rBrHrPrXrTrU classmethodrWrVrprrr|rrrrrrrrrrrrrrrrrrrrrrrrrr~rrrCrrrtrrrrrrrrrrrrrrs*   $  &  9       (!   V(  &) rrrruZnumpyr8rQrrE__all__ABCrrrrrs