ó ]ÐKc@ sîdZddlmZddddddd d d d d dddddddddgZddlZddljZddl Z ddl Z ddl m Z e j Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd „Zejdd!gƒZejd"gƒZejd!gƒZejd"d!gƒZd#„Zd$„Zd%„Zd&„Zd'„Z d(„Z!d)d*„Z"d!d!d+„Z#d!gd"d!d,„Z$d-„Z%d.„Z&de(d/„Z)d0„Z*e j+d1dd2d3d4d5ƒdUdS(6sÑFunctions for dealing with Chebyshev series. This module provide s a number of functions that are useful in dealing with Chebyshev series as well as a ``Chebyshev`` class that encapsuletes the usual arithmetic operations. All the Chebyshev series are assumed to be ordered from low to high, thus ``array([1,2,3])`` will be treated as the series ``T_0 + 2*T_1 + 3*T_2`` Constants --------- - chebdomain -- Chebyshev series default domain - chebzero -- Chebyshev series that evaluates to 0. - chebone -- Chebyshev series that evaluates to 1. - chebx -- Chebyshev series of the identity map (x). Arithmetic ---------- - chebadd -- add a Chebyshev series to another. - chebsub -- subtract a Chebyshev series from another. - chebmul -- multiply a Chebyshev series by another - chebdiv -- divide one Chebyshev series by another. - chebval -- evaluate a Chebyshev series at given points. Calculus -------- - chebder -- differentiate a Chebyshev series. - chebint -- integrate a Chebyshev series. Misc Functions -------------- - chebfromroots -- create a Chebyshev series with specified roots. - chebroots -- find the roots of a Chebyshev series. - chebvander -- Vandermode like matrix for Chebyshev polynomials. - chebfit -- least squares fit returning a Chebyshev series. - chebtrim -- trim leading coefficients from a Chebyshev series. - chebline -- Chebyshev series of given straight line - cheb2poly -- convert a Chebyshev series to a polynomial. - poly2cheb -- convert a polynomial to a Chebyshev series. Classes ------- - Chebyshev -- Chebyshev series class. Notes ----- The implementations of multiplication, division, integration, and differentiation use the algebraic identities: .. math :: T_n(x) = \frac{z^n + z^{-n}}{2} \\ z\frac{dx}{dz} = \frac{z - z^{-1}}{2}. where .. math :: x = \frac{z + z^{-1}}{2}. These identities allow a Chebyshev series to be expressed as a finite, symmetric Laurent series. These sorts of Laurent series are referred to as z-series in this module. iÿÿÿÿ(tdivisiontchebzerotchebonetchebxt chebdomaintcheblinetchebaddtchebsubtchebmultchebdivtchebvaltchebdertchebintt cheb2polyt poly2chebt chebfromrootst chebvandertchebfittchebtrimt chebrootst ChebyshevN(t polytemplatecC sP|j}tjd|dd|jƒ}|d||d)||ddd…S(sôCovert Chebyshev series to z-series. Covert a Chebyshev series to the equivalent z-series. The result is never an empty array. The dtype of the return is the same as that of the input. No checks are run on the arguments as this routine is for internal use. Parameters ---------- cs : 1-d ndarray Chebyshev coefficients, ordered from low to high Returns ------- zs : 1-d ndarray Odd length symmetric z-series, ordered from low to high. iitdtypeNiÿÿÿÿ(tsizetnptzerosR(tcstntzs((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyt_cseries_to_zseriesRs  cC s<|jdd}||djƒ}|d|c!d9+|S(søCovert z-series to a Chebyshev series. Covert a z series to the equivalent Chebyshev series. The result is never an empty array. The dtype of the return is the same as that of the input. No checks are run on the arguments as this routine is for internal use. Parameters ---------- zs : 1-d ndarray Odd length symmetric z-series, ordered from low to high. Returns ------- cs : 1-d ndarray Chebyshev coefficients, ordered from low to high. ii(Rtcopy(RRR((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyt_zseries_to_cseriesjscC stj||ƒS(sÄMultiply two z-series. Multiply two z-series to produce a z-series. Parameters ---------- z1, z2 : 1-d ndarray The arrays must be 1-d but this is not checked. Returns ------- product : 1-d ndarray The product z-series. Notes ----- This is simply convolution. If symmetic/anti-symmetric z-series are denoted by S/A then the following rules apply: S*S, A*A -> S S*A, A*S -> A (Rtconvolve(tz1tz2((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyt _zseries_mul‚sc C s­|jƒ}|jƒ}t|ƒ}t|ƒ}|dkrX||}||d dfS||krv|d d|fS||}|d}||}tj|dd|jƒ}d}|}x‚||kr@||} ||||<| |||<| |} ||||c!| 8+||||c!| 8+|d7}|d8}q¿W||} | ||<| |} ||||c!| 8+||}||d|d|!jƒ} || fSdS(s¹Divide the first z-series by the second. Divide `z1` by `z2` and return the quotient and remainder as z-series. Warning: this implementation only applies when both z1 and z2 have the same symmetry, which is sufficient for present purposes. Parameters ---------- z1, z2 : 1-d ndarray The arrays must be 1-d and have the same symmetry, but this is not checked. Returns ------- (quotient, remainder) : 1-d ndarrays Quotient and remainder as z-series. Notes ----- This is not the same as polynomial division on account of the desired form of the remainder. If symmetic/anti-symmetric z-series are denoted by S/A then the following rules apply: S/S -> S,S A/A -> S,A The restriction to types of the same symmetry could be fixed but seems like uneeded generality. There is no natural form for the remainder in the case where there is no symmetry. iiRN(RtlenRtemptyR( R!R"tlen1tlen2tdlentscltquotitjtrttmptrem((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyt _zseries_divœs>!                 cC sit|ƒd}tjdddgd|jƒ}|tj| |dƒd9}t||ƒ\}}|S(sŒDifferentiate a z-series. The derivative is with respect to x, not z. This is achieved using the chain rule and the value of dx/dz given in the module notes. Parameters ---------- zs : z-series The z-series to differentiate. Returns ------- derivative : z-series The derivative Notes ----- The zseries for x (ns) has been multiplied by two in order to avoid using floats that are incompatible with Decimal and likely other specialized scalar types. This scaling has been compensated by multiplying the value of zs by two also so that the two cancels in the division. iiÿÿÿÿiiR(R$RtarrayRtarangeR0(RRtnstdR-((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyt _zseries_derÞs !cC sdt|ƒd}tjdddgd|jƒ}t||ƒ}tj| |dƒd}||c || *||dc||d)d||<|S(sMIntegrate a z-series. The integral is with respect to x, not z. This is achieved by a change of variable using dx/dz given in the module notes. Parameters ---------- zs : z-series The z-series to integrate Returns ------- integral : z-series The indefinite integral Notes ----- The zseries for x (ns) has been multiplied by two in order to avoid using floats that are incompatible with Decimal and likely other specialized scalar types. This scaling has been compensated by dividing the resulting zs by two. iiiÿÿÿÿiR(R$RR1RR#R2(RRR3tdiv((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyt _zseries_intýs! cC s¦tj|gƒ\}|ddd…}|d jƒ}tjdddgd|jƒ}x@tdt|ƒƒD])}t||ƒ}||c||7/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyR#s!cC s¹tj|gƒ\}tjt|ƒd|jƒ}t|ƒ}tjdddgd|jƒ}xDtdt|ƒdƒD])}t ||ƒ\}}|d||/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyR Ds ! iicC s3|dkrtj||gƒStj|gƒSdS(sFChebyshev series whose graph is a straight line The line has the formula ``off + scl*x`` Parameters: ----------- off, scl : scalars The specified line is given by ``off + scl*x``. Returns: -------- series : 1d ndarray The Chebyshev series representation of ``off + scl*x``. iN(RR1(toffR)((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyRws cC s¥t|ƒdkrtjdƒStj|gdtƒ\}tjdgd|jƒ}x?|D]7}tjd| dgd|jƒ}t||ƒ}q\Wt |ƒSdS(sèGenerate a Chebyschev series with given roots. Generate a Chebyshev series whose roots are given by `roots`. The resulting series is the produet `(x - roots[0])*(x - roots[1])*...` Inputs ------ roots : array_like 1-d array containing the roots in sorted order. Returns ------- series : ndarray 1-d array containing the coefficients of the Chebeshev series ordered from low to high. See Also -------- chebroots iittrimRgà?N( R$RtonesR8R9tFalseR1RR#R(trootstprdR-tfac((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyRŒs  "cC sutj||gƒ\}}t|ƒt|ƒkrO||jc |7*|}n||jc |7*|}tj|ƒS(sAdd one Chebyshev series to another. Returns the sum of two Chebyshev series `c1` + `c2`. The arguments are sequences of coefficients ordered from low to high, i.e., [1,2,3] is the series "T_0 + 2*T_1 + 3*T_2". Parameters ---------- c1, c2 : array_like 1d arrays of Chebyshev series coefficients ordered from low to high. Returns ------- out : ndarray Chebyshev series of the sum. See Also -------- chebsub, chebmul, chebdiv, chebpow (R8R9R$Rttrimseq(tc1tc2tret((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyR­s cC s|tj||gƒ\}}t|ƒt|ƒkrO||jc |8*|}n | }||jc |7*|}tj|ƒS(s+Subtract one Chebyshev series from another. Returns the difference of two Chebyshev series `c1` - `c2`. The sequences of coefficients are ordered from low to high, i.e., [1,2,3] is the series ``T_0 + 2*T_1 + 3*T_2.`` Parameters ---------- c1, c2 : array_like 1d arrays of Chebyshev series coefficients ordered from low to high. Returns ------- out : ndarray Chebyshev series of the difference. See Also -------- chebadd, chebmul, chebdiv, chebpow Examples -------- (R8R9R$RRD(RERFRG((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyRÏs cC s[tj||gƒ\}}t|ƒ}t|ƒ}t||ƒ}t|ƒ}tj|ƒS(sMultiply one Chebyshev series by another. Returns the product of two Chebyshev series `c1` * `c2`. The arguments are sequences of coefficients ordered from low to high, i.e., [1,2,3] is the series ``T_0 + 2*T_1 + 3*T_2.`` Parameters ---------- c1, c2 : array_like 1d arrays of chebyshev series coefficients ordered from low to high. Returns ------- out : ndarray Chebyshev series of the product. See Also -------- chebadd, chebsub, chebdiv, chebpow (R8R9RR#RRD(RERFR!R"RBRG((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyRõs    cC søtj||gƒ\}}|ddkr7tƒ‚nt|ƒ}t|ƒ}||krm|d d|fS|dkr“||d|d dfSt|ƒ}t|ƒ}t||ƒ\}}tjt|ƒƒ}tjt|ƒƒ}||fSdS(sBDivide one Chebyshev series by another. Returns the quotient of two Chebyshev series `c1` / `c2`. The arguments are sequences of coefficients ordered from low to high, i.e., [1,2,3] is the series ``T_0 + 2*T_1 + 3*T_2.`` Parameters ---------- c1, c2 : array_like 1d arrays of chebyshev series coefficients ordered from low to high. Returns ------- [quo, rem] : ndarray Chebyshev series of the quotient and remainder. See Also -------- chebadd, chebsub, chebmul, chebpow Examples -------- iÿÿÿÿiiN(R8R9tZeroDivisionErrorR$RR0RDR(RERFtlc1tlc2R!R"R*R/((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyR s       icC sôtj|gƒ\}t|ƒ}||ks9|dkrHtdƒ‚n¨|dk ro||krotdƒ‚n|dkr”tjdgd|jƒS|dkr¤|St|ƒ}|}x-t d|dƒD]}tj ||ƒ}qÊWt |ƒSdS(sRaise a Chebyshev series to a power. Returns the Chebyshev series `cs` raised to the power `pow`. The arguement `cs` is a sequence of coefficients ordered from low to high. i.e., [1,2,3] is the series ``T_0 + 2*T_1 + 3*T_2.`` Parameters ---------- cs : array_like 1d array of chebyshev series coefficients ordered from low to high. 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 umanageable size. Default is 16 Returns ------- coef : ndarray Chebyshev series of power. See Also -------- chebadd, chebsub, chebmul, chebdiv Examples -------- is%Power must be a non-negative integer.sPower is too largeiRiN( R8R9tintt ValueErrortNoneRR1RRR:R R(RtpowtmaxpowertpowerRRBR+((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pytchebpowBs     cC s·tj|gƒ\}|dkr-td‚ntj|ƒsHtd‚n|dkrX|S|t|ƒkrv|d dSt|ƒ}x$t|ƒD]}t|ƒ|}qWt |ƒSdS(s¨Differentiate a Chebyshev series. Returns the series `cs` differentiated `m` times. At each iteration the result is multiplied by `scl`. The scaling factor is for use in a linear change of variable. The argument `cs` is a sequence of coefficients ordered from low to high. i.e., [1,2,3] is the series ``T_0 + 2*T_1 + 3*T_2``. Parameters ---------- cs: array_like 1d array of chebyshev series coefficients ordered from low to high. m : int, optional Order of differentiation, must be non-negative. (default: 1) scl : scalar, optional The result of each derivation is multiplied by `scl`. The end result is multiplication by `scl`**`m`. This is for use in a linear change of variable. (default: 1) Returns ------- der : ndarray Chebyshev series of the derivative. See Also -------- chebint Examples -------- is(The order of derivation must be positives"The scl parameter must be a scalariN( R8R9RLRtisscalarR$RR:R5R(RtmR)RR+((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyR us"      cC s!tj|ƒr|g}n|dkr3td‚nt|ƒ|krQtd‚ntj|ƒsltd‚ntj|ƒs‡td‚ntj|gƒ\}t|ƒdg|t|ƒ}x]t|ƒD]O}t|ƒ|}t |ƒ}t |ƒ}|dc||t ||ƒ7/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyR ¨s$.      !  %cC stj|gƒ\}t|tƒs3t|tƒrEtj|ƒ}nt|ƒdkrj|d}d}nŽt|ƒdkr“|d}|d}ned|}|d}|d}xDtdt|ƒdƒD])}|}|| |}|||}qËW|||S(sÌEvaluate a Chebyshev series. If `cs` is of length `n`, this function returns : ``p(x) = cs[0]*T_0(x) + cs[1]*T_1(x) + ... + cs[n-1]*T_{n-1}(x)`` If x is a sequence or array then p(x) will have the same shape as x. If r is a ring_like object that supports multiplication and addition by the values in `cs`, then an object of the same type is returned. Parameters ---------- x : array_like, ring_like Array of numbers or objects that support multiplication and addition with themselves and with the elements of `cs`. cs : array_like 1-d array of Chebyshev coefficients ordered from low to high. Returns ------- values : ndarray, ring_like If the return is an ndarray then it has the same shape as `x`. See Also -------- chebfit Examples -------- Notes ----- The evaluation uses Clenshaw recursion, aka synthetic division. Examples -------- iiiiþÿÿÿiÿÿÿÿi( R8R9t isinstancettupleRTRtasarrayR$R:(R<Rtc0REtx2R+R.((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyR ës"(        cC sºtj|ƒd}t|ƒd}tj|j|fd|jƒ}|dkr¶d|}||d/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyR's"   ;c C sµt|ƒd}tj|ƒd}tj|ƒd}|dkrNtd‚n|jdkritd‚n|jdkr„td‚n|jdks¢|jdkr®td‚n|jd|jdkrÔtd ‚n|d krt |ƒtj |j ƒj }nt ||ƒ}tj||jdƒƒ}tj||||ƒ\}} } } |j|j}| |kr‘| r‘d } tj| tjƒn|r­|| | | |gfS|Sd S( sLeast squares fit of Chebyshev series to data. Fit a Chebyshev series ``p(x) = p[0] * T_{deq}(x) + ... + p[deg] * T_{0}(x)`` of degree `deg` to points `(x, y)`. Returns a vector of coefficients `p` that minimises the squared error. Parameters ---------- x : array_like, shape (M,) x-coordinates of the M sample points ``(x[i], y[i])``. y : array_like, shape (M,) or (M, K) y-coordinates of the sample points. Several data sets of sample points sharing the same x-coordinates can be fitted at once by passing in a 2D-array that contains one dataset per column. deg : int Degree of the fitting polynomial 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. Returns ------- coef : ndarray, shape (M,) or (M, K) Chebyshev coefficients ordered from low to high. If `y` was 2-D, the coefficients for the data in column k of `y` are in column `k`. [residuals, rank, singular_values, rcond] : present when `full` = True Residuals of the least-squares fit, the effective rank of the scaled Vandermonde matrix and its singular values, and the specified value of `rcond`. For more details, see `linalg.lstsq`. Warns ----- RankWarning 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', RankWarning) See Also -------- chebval : Evaluates a Chebyshev series. chebvander : Vandermonde matrix of Chebyshev series. polyfit : least squares fit using polynomials. linalg.lstsq : Computes a least-squares fit from the matrix. scipy.interpolate.UnivariateSpline : Computes spline fits. Notes ----- The solution are the coefficients ``c[i]`` of the Chebyshev series ``T(x)`` that minimizes the squared error ``E = \sum_j |y_j - T(x_j)|^2``. This problem is solved by setting up as the overdetermined matrix equation ``V(x)*c = y``, where ``V`` is the Vandermonde matrix of `x`, the elements of ``c`` are the coefficients to be solved for, and the elements of `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, then a `RankWarning` will be issued. This means that the coeficient values may be poorly determined. Using a lower order fit will usually get rid of the warning. 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. Fits using Chebyshev series are usually better conditioned than fits using power series, but much can 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. References ---------- .. [1] Wikipedia, "Curve fitting", http://en.wikipedia.org/wiki/Curve_fitting Examples -------- igisexpected deg >= 0sexpected 1D vector for xsexpected non-empty vector for xisexpected 1D or 2D array for ys$expected x and y to have same lengths!The fit may be poorly conditionedN(RKRRYRLtndimt TypeErrorRR\RMR$tfinfoRtepsRtsqrttsumtlatlstsqtTtwarningstwarnR8t RankWarning( R<tyR]trcondtfullR^tAR)tctresidstranktstmsg((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyRIs2_       "%cC s!tj|gƒ\}t|ƒdkr=tjgd|jƒSt|ƒdkrltj|d |dgƒSt|ƒd}tj||fd|jƒ}d|jdd|d…/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyRÍs , tnametnicktchebtdomains[-1,1](,t__doc__t __future__Rt__all__tnumpyRt numpy.linalgtlinalgRft polyutilsR8RiRttrimcoefRRRR#R0R5R7RR R1RRRRRRRRRR RQR R R RRMR@RRt substitute(((s>/usr/lib/python2.7/site-packages/numpy/polynomial/chebyshev.pyt=sJ        B  & ! (  ! " & - 33C < "„ *