/ lib / numpy / lib / polynomial.pyc
polynomial.pyc
  1  o

  2  [��c��@sdZgd�ZddlZddlZddlZddlmmZddl	m
  3  Z
  4  mZmZm
Z
mZmZmZmZddl	mZddlmZddlmZmZddlmZdd	lmZmZmZmZdd
  5  lm Z m!Z!m"Z"ej#ej$dd�Z$ed�Gd
d�de%��Z&dd�Z'e$e'�dd��Z(dd�Z)e$e)�dd��Z*d<dd�Z+e$e+�d=dd��Z,d>dd�Z-e$e-�d?dd��Z.d@d d!�Z/e$e/�dAd#d$��Z0d%d&�Z1e$e1�d'd(��Z2d)d*�Z3e$e3�d+d,��Z4e$e3�d-d.��Z5e$e3�d/d0��Z6d1d2�Z7e$e7�d3d4��Z8e�9d5�Z:dBd7d8�Z;ed�Gd9d:�d:��Z<e�=d;e&�dS)Cz'
  6  Functions to operate on polynomials.
  7  
  8  )�poly�roots�polyint�polyder�polyadd�polysub�polymul�polydiv�polyval�poly1d�polyfit�RankWarning�N)�isscalar�abs�finfo�
  9  atleast_1d�hstack�dot�array�ones)�	overrides)�
 10  set_module)�diag�vander)�
 11  trim_zeros)�	iscomplex�real�imag�mintypecode)�eigvals�lstsq�inv�numpy)�modulec@seZdZdZdS)rz�
 12      Issued by `polyfit` when the Vandermonde matrix is rank deficient.
 13  
 14      For more information, a way to suppress the warning, and an example of
 15      `RankWarning` being issued, see `polyfit`.
 16  
 17      N)�__name__�
 18  __module__�__qualname__�__doc__�r(r(��C:\Users\Jacks.GUTTSPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\numpy\lib\polynomial.pyrsrcC�|S�Nr()�seq_of_zerosr(r(r)�_poly_dispatcher(�r-cCst|�}|j}t|�dkr |d|dkr |ddkr t|�}nt|�dkr6|j}|tkr5|�t|j��}nt	d��t|�dkrBdS|j}t
 19  d|d�}|D]}tj|t
d|g|d�dd	�}qMt|jjtj�r�t�|t�}t�t�|�t�|���k�r�|j��}|S)
 20  a#
 21      Find the coefficients of a polynomial with the given sequence of roots.
 22  
 23      .. note::
 24         This forms part of the old polynomial API. Since version 1.4, the
 25         new polynomial API defined in `numpy.polynomial` is preferred.
 26         A summary of the differences can be found in the
 27         :doc:`transition guide </reference/routines.polynomials>`.
 28  
 29      Returns the coefficients of the polynomial whose leading coefficient
 30      is one for the given sequence of zeros (multiple roots must be included
 31      in the sequence as many times as their multiplicity; see Examples).
 32      A square matrix (or array, which will be treated as a matrix) can also
 33      be given, in which case the coefficients of the characteristic polynomial
 34      of the matrix are returned.
 35  
 36      Parameters
 37      ----------
 38      seq_of_zeros : array_like, shape (N,) or (N, N)
 39          A sequence of polynomial roots, or a square array or matrix object.
 40  
 41      Returns
 42      -------
 43      c : ndarray
 44          1D array of polynomial coefficients from highest to lowest degree:
 45  
 46          ``c[0] * x**(N) + c[1] * x**(N-1) + ... + c[N-1] * x + c[N]``
 47          where c[0] always equals 1.
 48  
 49      Raises
 50      ------
 51      ValueError
 52          If input is the wrong shape (the input must be a 1-D or square
 53          2-D array).
 54  
 55      See Also
 56      --------
 57      polyval : Compute polynomial values.
 58      roots : Return the roots of a polynomial.
 59      polyfit : Least squares polynomial fit.
 60      poly1d : A one-dimensional polynomial class.
 61  
 62      Notes
 63      -----
 64      Specifying the roots of a polynomial still leaves one degree of
 65      freedom, typically represented by an undetermined leading
 66      coefficient. [1]_ In the case of this function, that coefficient -
 67      the first one in the returned array - is always taken as one. (If
 68      for some reason you have one other point, the only automatic way
 69      presently to leverage that information is to use ``polyfit``.)
 70  
 71      The characteristic polynomial, :math:`p_a(t)`, of an `n`-by-`n`
 72      matrix **A** is given by
 73  
 74          :math:`p_a(t) = \mathrm{det}(t\, \mathbf{I} - \mathbf{A})`,
 75  
 76      where **I** is the `n`-by-`n` identity matrix. [2]_
 77  
 78      References
 79      ----------
 80      .. [1] M. Sullivan and M. Sullivan, III, "Algebra and Trignometry,
 81         Enhanced With Graphing Utilities," Prentice-Hall, pg. 318, 1996.
 82  
 83      .. [2] G. Strang, "Linear Algebra and Its Applications, 2nd Edition,"
 84         Academic Press, pg. 182, 1980.
 85  
 86      Examples
 87      --------
 88      Given a sequence of a polynomial's zeros:
 89  
 90      >>> np.poly((0, 0, 0)) # Multiple root example
 91      array([1., 0., 0., 0.])
 92  
 93      The line above represents z**3 + 0*z**2 + 0*z + 0.
 94  
 95      >>> np.poly((-1./2, 0, 1./2))
 96      array([ 1.  ,  0.  , -0.25,  0.  ])
 97  
 98      The line above represents z**3 - z/4
 99  
100      >>> np.poly((np.random.random(1)[0], 0, np.random.random(1)[0]))
101      array([ 1.        , -0.77086955,  0.08618131,  0.        ]) # random
102  
103      Given a square array object:
104  
105      >>> P = np.array([[0, 1./3], [-1./2, 0]])
106      >>> np.poly(P)
107      array([1.        , 0.        , 0.16666667])
108  
109      Note how in all cases the leading coefficient is always 1.
110  
111      �r
�z.input must be 1d or non-empty square 2d array.��?�r0��dtype�full)�mode)r�shape�lenrr4�object�astyper�char�
112  ValueErrorr�NX�convolver�
113  issubclass�type�complexfloating�asarray�complex�all�sort�	conjugater�copy)r,�sh�dt�a�zerorr(r(r)r,s*^(
114  � 
115  rcCr*r+r()�pr(r(r)�_roots_dispatcher�r.rMcCst|�}|jdkr
td��t�t�|��d}t|�dkr"t�g�St|�|dd}|t|d�t|d�d�}t	|j
116  jtjtj
f�sL|�t�}t|�}|dkrwtt�|df|j
117  �d�}|dd�|d|ddd�f<t|�}nt�g�}t|t�||j
118  �f�}|S)a�
119      Return the roots of a polynomial with coefficients given in p.
120  
121      .. note::
122         This forms part of the old polynomial API. Since version 1.4, the
123         new polynomial API defined in `numpy.polynomial` is preferred.
124         A summary of the differences can be found in the
125         :doc:`transition guide </reference/routines.polynomials>`.
126  
127      The values in the rank-1 array `p` are coefficients of a polynomial.
128      If the length of `p` is n+1 then the polynomial is described by::
129  
130        p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n]
131  
132      Parameters
133      ----------
134      p : array_like
135          Rank-1 array of polynomial coefficients.
136  
137      Returns
138      -------
139      out : ndarray
140          An array containing the roots of the polynomial.
141  
142      Raises
143      ------
144      ValueError
145          When `p` cannot be converted to a rank-1 array.
146  
147      See also
148      --------
149      poly : Find the coefficients of a polynomial with a given sequence
150             of roots.
151      polyval : Compute polynomial values.
152      polyfit : Least squares polynomial fit.
153      poly1d : A one-dimensional polynomial class.
154  
155      Notes
156      -----
157      The algorithm relies on computing the eigenvalues of the
158      companion matrix [1]_.
159  
160      References
161      ----------
162      .. [1] R. A. Horn & C. R. Johnson, *Matrix Analysis*.  Cambridge, UK:
163          Cambridge University Press, 1999, pp. 146-7.
164  
165      Examples
166      --------
167      >>> coeff = [3.2, 2, 1]
168      >>> np.roots(coeff)
169      array([-0.3125+0.46351241j, -0.3125-0.46351241j])
170  
171      r0zInput must be a rank-1 array.r
�����r/N)r�ndimr<r=�nonzero�ravelr8r�intr?r4r@�floatingrAr:�floatrrrr�zeros)rLZnon_zeroZtrailing_zeros�N�Arr(r(r)r�s$9
172  
173   
174  "
175  
176  rcC�|fSr+r()rL�m�kr(r(r)�_polyint_dispatcher�r[r0c	Cs�t|�}|dkrtd��|durt�|t�}t|�}t|�dkr.|dkr.|dt�|t�}t|�|kr8td��t|t	�}t�
177  |�}|dkrN|rLt	|�S|St�|�t�
t|�dd��|dgf�}t||d|dd�d�}|rut	|�S|S)a�
178      Return an antiderivative (indefinite integral) of a polynomial.
179  
180      .. note::
181         This forms part of the old polynomial API. Since version 1.4, the
182         new polynomial API defined in `numpy.polynomial` is preferred.
183         A summary of the differences can be found in the
184         :doc:`transition guide </reference/routines.polynomials>`.
185  
186      The returned order `m` antiderivative `P` of polynomial `p` satisfies
187      :math:`\frac{d^m}{dx^m}P(x) = p(x)` and is defined up to `m - 1`
188      integration constants `k`. The constants determine the low-order
189      polynomial part
190  
191      .. math:: \frac{k_{m-1}}{0!} x^0 + \ldots + \frac{k_0}{(m-1)!}x^{m-1}
192  
193      of `P` so that :math:`P^{(j)}(0) = k_{m-j-1}`.
194  
195      Parameters
196      ----------
197      p : array_like or poly1d
198          Polynomial to integrate.
199          A sequence is interpreted as polynomial coefficients, see `poly1d`.
200      m : int, optional
201          Order of the antiderivative. (Default: 1)
202      k : list of `m` scalars or scalar, optional
203          Integration constants. They are given in the order of integration:
204          those corresponding to highest-order terms come first.
205  
206          If ``None`` (default), all constants are assumed to be zero.
207          If `m = 1`, a single scalar can be given instead of a list.
208  
209      See Also
210      --------
211      polyder : derivative of a polynomial
212      poly1d.integ : equivalent method
213  
214      Examples
215      --------
216      The defining property of the antiderivative:
217  
218      >>> p = np.poly1d([1,1,1])
219      >>> P = np.polyint(p)
220      >>> P
221       poly1d([ 0.33333333,  0.5       ,  1.        ,  0.        ]) # may vary
222      >>> np.polyder(P) == p
223      True
224  
225      The integration constants default to zero, but can be specified:
226  
227      >>> P = np.polyint(p, 3)
228      >>> P(0)
229      0.0
230      >>> np.polyder(P)(0)
231      0.0
232      >>> np.polyder(P, 2)(0)
233      0.0
234      >>> P = np.polyint(p, 3, k=[6,5,3])
235      >>> P
236      poly1d([ 0.01666667,  0.04166667,  0.16666667,  3. ,  5. ,  3. ]) # may vary
237  
238      Note that 3 = 6 / 2!, and that the constants are given in the order of
239      integrations. Constant of the highest-order polynomial term comes first:
240  
241      >>> np.polyder(P, 2)(0)
242      6.0
243      >>> np.polyder(P, 1)(0)
244      5.0
245      >>> P(0)
246      3.0
247  
248      r
z0Order of integral must be positive (see polyder)Nr0z7k must be a scalar or a rank-1 array of length 1 or >m.rN)rZ)rRr<r=rUrTrr8r�
249  isinstancer
250  rB�concatenate�__truediv__�aranger)rLrYrZ�truepoly�y�valr(r(r)rs.J�
251  
252  (rcCrXr+r()rLrYr(r(r)�_polyder_dispatcherpr\rdcCs~t|�}|dkrtd��t|t�}t�|�}t|�d}|dd�t�|dd�}|dkr0|}nt||d�}|r=t|�}|S)ax
253      Return the derivative of the specified order of a polynomial.
254  
255      .. note::
256         This forms part of the old polynomial API. Since version 1.4, the
257         new polynomial API defined in `numpy.polynomial` is preferred.
258         A summary of the differences can be found in the
259         :doc:`transition guide </reference/routines.polynomials>`.
260  
261      Parameters
262      ----------
263      p : poly1d or sequence
264          Polynomial to differentiate.
265          A sequence is interpreted as polynomial coefficients, see `poly1d`.
266      m : int, optional
267          Order of differentiation (default: 1)
268  
269      Returns
270      -------
271      der : poly1d
272          A new polynomial representing the derivative.
273  
274      See Also
275      --------
276      polyint : Anti-derivative of a polynomial.
277      poly1d : Class for one-dimensional polynomials.
278  
279      Examples
280      --------
281      The derivative of the polynomial :math:`x^3 + x^2 + x^1 + 1` is:
282  
283      >>> p = np.poly1d([1,1,1,1])
284      >>> p2 = np.polyder(p)
285      >>> p2
286      poly1d([3, 2, 1])
287  
288      which evaluates to:
289  
290      >>> p2(2.)
291      17.0
292  
293      We can verify this, approximating the derivative with
294      ``(f(x + h) - f(x))/h``:
295  
296      >>> (p(2. + 0.001) - p(2.)) / 0.001
297      17.007000999997857
298  
299      The fourth-order derivative of a 3rd-order polynomial is zero:
300  
301      >>> np.polyder(p, 2)
302      poly1d([6, 2])
303      >>> np.polyder(p, 3)
304      poly1d([6])
305      >>> np.polyder(p, 4)
306      poly1d([0])
307  
308      r
z2Order of derivative must be positive (see polyint)r0NrN)	rRr<r]r
309  r=rBr8r`r)rLrYra�nrbrcr(r(r)rts;
310  
311  rcCs
312  |||fSr+r()�xrb�deg�rcondr5�w�covr(r(r)�_polyfit_dispatcher��
313  rkFcCsFt|�d}t�|�d}t�|�d}|dkrtd��|jdkr%td��|jdkr.td��|jdks8|jdkr<td��|jd|jdkrJtd	��|d
314  urXt|�t	|j
315  �j}t||�}|}	|d
316  ur�t�|�d}|jdkrstd��|jd|jdkr�td��||d
317  d
318  �tj
f9}|	jdkr�|	|d
319  d
320  �tj
f9}	n|	|9}	t�||jdd
��}
321  ||
322  }t||	|�\}}}
}|j|
323  j}|
|kr�|s�d}tj|tdd�|r�|||
||fS|�r!tt|j|��}|t�|
324  |
325  �}|dkr�d}nt|�|kr�td��|t|�|}|jdk�r|||fS||d
326  d
327  �d
328  d
329  �tj
f|fS|S)a�
330      Least squares polynomial fit.
331  
332      .. note::
333         This forms part of the old polynomial API. Since version 1.4, the
334         new polynomial API defined in `numpy.polynomial` is preferred.
335         A summary of the differences can be found in the
336         :doc:`transition guide </reference/routines.polynomials>`.
337  
338      Fit a polynomial ``p(x) = p[0] * x**deg + ... + p[deg]`` of degree `deg`
339      to points `(x, y)`. Returns a vector of coefficients `p` that minimises
340      the squared error in the order `deg`, `deg-1`, ... `0`.
341  
342      The `Polynomial.fit <numpy.polynomial.polynomial.Polynomial.fit>` class
343      method is recommended for new code as it is more stable numerically. See
344      the documentation of the method for more information.
345  
346      Parameters
347      ----------
348      x : array_like, shape (M,)
349          x-coordinates of the M sample points ``(x[i], y[i])``.
350      y : array_like, shape (M,) or (M, K)
351          y-coordinates of the sample points. Several data sets of sample
352          points sharing the same x-coordinates can be fitted at once by
353          passing in a 2D-array that contains one dataset per column.
354      deg : int
355          Degree of the fitting polynomial
356      rcond : float, optional
357          Relative condition number of the fit. Singular values smaller than
358          this relative to the largest singular value will be ignored. The
359          default value is len(x)*eps, where eps is the relative precision of
360          the float type, about 2e-16 in most cases.
361      full : bool, optional
362          Switch determining nature of return value. When it is False (the
363          default) just the coefficients are returned, when True diagnostic
364          information from the singular value decomposition is also returned.
365      w : array_like, shape (M,), optional
366          Weights. If not None, the weight ``w[i]`` applies to the unsquared
367          residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are
368          chosen so that the errors of the products ``w[i]*y[i]`` all have the
369          same variance.  When using inverse-variance weighting, use
370          ``w[i] = 1/sigma(y[i])``.  The default value is None.
371      cov : bool or str, optional
372          If given and not `False`, return not just the estimate but also its
373          covariance matrix. By default, the covariance are scaled by
374          chi2/dof, where dof = M - (deg + 1), i.e., the weights are presumed
375          to be unreliable except in a relative sense and everything is scaled
376          such that the reduced chi2 is unity. This scaling is omitted if
377          ``cov='unscaled'``, as is relevant for the case that the weights are
378          w = 1/sigma, with sigma known to be a reliable estimate of the
379          uncertainty.
380  
381      Returns
382      -------
383      p : ndarray, shape (deg + 1,) or (deg + 1, K)
384          Polynomial coefficients, highest power first.  If `y` was 2-D, the
385          coefficients for `k`-th data set are in ``p[:,k]``.
386  
387      residuals, rank, singular_values, rcond
388          These values are only returned if ``full == True``
389  
390          - residuals -- sum of squared residuals of the least squares fit
391          - rank -- the effective rank of the scaled Vandermonde
392             coefficient matrix
393          - singular_values -- singular values of the scaled Vandermonde
394             coefficient matrix
395          - rcond -- value of `rcond`.
396  
397          For more details, see `numpy.linalg.lstsq`.
398  
399      V : ndarray, shape (M,M) or (M,M,K)
400          Present only if ``full == False`` and ``cov == True``.  The covariance
401          matrix of the polynomial coefficient estimates.  The diagonal of
402          this matrix are the variance estimates for each coefficient.  If y
403          is a 2-D array, then the covariance matrix for the `k`-th data set
404          are in ``V[:,:,k]``
405  
406  
407      Warns
408      -----
409      RankWarning
410          The rank of the coefficient matrix in the least-squares fit is
411          deficient. The warning is only raised if ``full == False``.
412  
413          The warnings can be turned off by
414  
415          >>> import warnings
416          >>> warnings.simplefilter('ignore', np.RankWarning)
417  
418      See Also
419      --------
420      polyval : Compute polynomial values.
421      linalg.lstsq : Computes a least-squares fit.
422      scipy.interpolate.UnivariateSpline : Computes spline fits.
423  
424      Notes
425      -----
426      The solution minimizes the squared error
427  
428      .. math::
429          E = \sum_{j=0}^k |p(x_j) - y_j|^2
430  
431      in the equations::
432  
433          x[0]**n * p[0] + ... + x[0] * p[n-1] + p[n] = y[0]
434          x[1]**n * p[0] + ... + x[1] * p[n-1] + p[n] = y[1]
435          ...
436          x[k]**n * p[0] + ... + x[k] * p[n-1] + p[n] = y[k]
437  
438      The coefficient matrix of the coefficients `p` is a Vandermonde matrix.
439  
440      `polyfit` issues a `RankWarning` when the least-squares fit is badly
441      conditioned. This implies that the best fit is not well-defined due
442      to numerical error. The results may be improved by lowering the polynomial
443      degree or by replacing `x` by `x` - `x`.mean(). The `rcond` parameter
444      can also be set to a value smaller than its default, but the resulting
445      fit may be spurious: including contributions from the small singular
446      values can add numerical noise to the result.
447  
448      Note that fitting polynomial coefficients is inherently badly conditioned
449      when the degree of the polynomial is large or the interval of sample points
450      is badly centered. The quality of the fit should always be checked in these
451      cases. When polynomial fits are not satisfactory, splines may be a good
452      alternative.
453  
454      References
455      ----------
456      .. [1] Wikipedia, "Curve fitting",
457             https://en.wikipedia.org/wiki/Curve_fitting
458      .. [2] Wikipedia, "Polynomial interpolation",
459             https://en.wikipedia.org/wiki/Polynomial_interpolation
460  
461      Examples
462      --------
463      >>> import warnings
464      >>> x = np.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0])
465      >>> y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
466      >>> z = np.polyfit(x, y, 3)
467      >>> z
468      array([ 0.08703704, -0.81349206,  1.69312169, -0.03968254]) # may vary
469  
470      It is convenient to use `poly1d` objects for dealing with polynomials:
471  
472      >>> p = np.poly1d(z)
473      >>> p(0.5)
474      0.6143849206349179 # may vary
475      >>> p(3.5)
476      -0.34732142857143039 # may vary
477      >>> p(10)
478      22.579365079365115 # may vary
479  
480      High-order polynomials may oscillate wildly:
481  
482      >>> with warnings.catch_warnings():
483      ...     warnings.simplefilter('ignore', np.RankWarning)
484      ...     p30 = np.poly1d(np.polyfit(x, y, 30))
485      ...
486      >>> p30(4)
487      -0.80000000000000204 # may vary
488      >>> p30(5)
489      -0.99999999999999445 # may vary
490      >>> p30(4.5)
491      -0.10547061179440398 # may vary
492  
493      Illustration:
494  
495      >>> import matplotlib.pyplot as plt
496      >>> xp = np.linspace(-2, 6, 100)
497      >>> _ = plt.plot(x, y, '.', xp, p(xp), '-', xp, p30(xp), '--')
498      >>> plt.ylim(-2,2)
499      (-2, 2)
500      >>> plt.show()
501  
502      r0�r
zexpected deg >= 0zexpected 1D vector for xzexpected non-empty vector for xr/zexpected 1D or 2D array for yz$expected x and y to have same lengthNz expected a 1-d array for weightsz(expected w and y to have the same length)�axisz!Polyfit may be poorly conditioned���
503  stacklevel�unscaledzJthe number of data points must exceed order to scale the covariance matrix)rRr=rBr<rO�	TypeError�sizer7r8rr4�epsr�newaxis�sqrt�sumr �T�warnings�warnrr!r�outer)rfrbrgrhr5rirj�order�lhs�rhs�scale�c�resids�rank�s�msgZVbase�facr(r(r)r�sb1
504  
505  
506  
507  
508   rcC�||fSr+r()rLrfr(r(r)�_polyval_dispatcher��r�cCsHt�|�}t|t�r
d}n
509  t�|�}t�|�}|D]}|||}q|S)a�
510      Evaluate a polynomial at specific values.
511  
512      .. note::
513         This forms part of the old polynomial API. Since version 1.4, the
514         new polynomial API defined in `numpy.polynomial` is preferred.
515         A summary of the differences can be found in the
516         :doc:`transition guide </reference/routines.polynomials>`.
517  
518      If `p` is of length N, this function returns the value:
519  
520          ``p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]``
521  
522      If `x` is a sequence, then ``p(x)`` is returned for each element of ``x``.
523      If `x` is another polynomial then the composite polynomial ``p(x(t))``
524      is returned.
525  
526      Parameters
527      ----------
528      p : array_like or poly1d object
529         1D array of polynomial coefficients (including coefficients equal
530         to zero) from highest degree to the constant term, or an
531         instance of poly1d.
532      x : array_like or poly1d object
533         A number, an array of numbers, or an instance of poly1d, at
534         which to evaluate `p`.
535  
536      Returns
537      -------
538      values : ndarray or poly1d
539         If `x` is a poly1d instance, the result is the composition of the two
540         polynomials, i.e., `x` is "substituted" in `p` and the simplified
541         result is returned. In addition, the type of `x` - array_like or
542         poly1d - governs the type of the output: `x` array_like => `values`
543         array_like, `x` a poly1d object => `values` is also.
544  
545      See Also
546      --------
547      poly1d: A polynomial class.
548  
549      Notes
550      -----
551      Horner's scheme [1]_ is used to evaluate the polynomial. Even so,
552      for polynomials of high degree the values may be inaccurate due to
553      rounding errors. Use carefully.
554  
555      If `x` is a subtype of `ndarray` the return value will be of the same type.
556  
557      References
558      ----------
559      .. [1] I. N. Bronshtein, K. A. Semendyayev, and K. A. Hirsch (Eng.
560         trans. Ed.), *Handbook of Mathematics*, New York, Van Nostrand
561         Reinhold Co., 1985, pg. 720.
562  
563      Examples
564      --------
565      >>> np.polyval([3,0,1], 5)  # 3 * 5**2 + 0 * 5**1 + 1
566      76
567      >>> np.polyval([3,0,1], np.poly1d(5))
568      poly1d([76])
569      >>> np.polyval(np.poly1d([3,0,1]), 5)
570      76
571      >>> np.polyval(np.poly1d([3,0,1]), np.poly1d(5))
572      poly1d([76])
573  
574      r
)r=rBr]r
575576  asanyarray�
577  zeros_like)rLrfrb�pvr(r(r)r	�s
578  D
579  
580  
581  r	cCr�r+r()�a1�a2r(r(r)�_binary_op_dispatcherr�r�cCs�t|t�p	t|t�}t|�}t|�}t|�t|�}|dkr#||}n'|dkr8t�||j�}t�||f�|}nt�t|�|j�}|t�||f�}|rPt|�}|S)aN
582      Find the sum of two polynomials.
583  
584      .. note::
585         This forms part of the old polynomial API. Since version 1.4, the
586         new polynomial API defined in `numpy.polynomial` is preferred.
587         A summary of the differences can be found in the
588         :doc:`transition guide </reference/routines.polynomials>`.
589  
590      Returns the polynomial resulting from the sum of two input polynomials.
591      Each input must be either a poly1d object or a 1D sequence of polynomial
592      coefficients, from highest to lowest degree.
593  
594      Parameters
595      ----------
596      a1, a2 : array_like or poly1d object
597          Input polynomials.
598  
599      Returns
600      -------
601      out : ndarray or poly1d object
602          The sum of the inputs. If either input is a poly1d object, then the
603          output is also a poly1d object. Otherwise, it is a 1D array of
604          polynomial coefficients from highest to lowest degree.
605  
606      See Also
607      --------
608      poly1d : A one-dimensional polynomial class.
609      poly, polyadd, polyder, polydiv, polyfit, polyint, polysub, polyval
610  
611      Examples
612      --------
613      >>> np.polyadd([1, 2], [9, 5, 4])
614      array([9, 6, 6])
615  
616      Using poly1d objects:
617  
618      >>> p1 = np.poly1d([1, 2])
619      >>> p2 = np.poly1d([9, 5, 4])
620      >>> print(p1)
621      1 x + 2
622      >>> print(p2)
623         2
624      9 x + 5 x + 4
625      >>> print(np.polyadd(p1, p2))
626         2
627      9 x + 6 x + 6
628  
629      r
�	r]r
630  rr8r=rUr4r^r�r�r�ra�diffrc�zrr(r(r)rs3
631  rcCs�t|t�p	t|t�}t|�}t|�}t|�t|�}|dkr#||}n'|dkr8t�||j�}t�||f�|}nt�t|�|j�}|t�||f�}|rPt|�}|S)a
632      Difference (subtraction) of two polynomials.
633  
634      .. note::
635         This forms part of the old polynomial API. Since version 1.4, the
636         new polynomial API defined in `numpy.polynomial` is preferred.
637         A summary of the differences can be found in the
638         :doc:`transition guide </reference/routines.polynomials>`.
639  
640      Given two polynomials `a1` and `a2`, returns ``a1 - a2``.
641      `a1` and `a2` can be either array_like sequences of the polynomials'
642      coefficients (including coefficients equal to zero), or `poly1d` objects.
643  
644      Parameters
645      ----------
646      a1, a2 : array_like or poly1d
647          Minuend and subtrahend polynomials, respectively.
648  
649      Returns
650      -------
651      out : ndarray or poly1d
652          Array or `poly1d` object of the difference polynomial's coefficients.
653  
654      See Also
655      --------
656      polyval, polydiv, polymul, polyadd
657  
658      Examples
659      --------
660      .. math:: (2 x^2 + 10 x - 2) - (3 x^2 + 10 x -4) = (-x^2 + 2)
661  
662      >>> np.polysub([2, 10, -2], [3, 10, -4])
663      array([-1,  0,  2])
664  
665      r
r�r�r(r(r)rWs%
666  rcCsBt|t�p	t|t�}t|�t|�}}t�||�}|rt|�}|S)a;
667      Find the product of two polynomials.
668  
669      .. note::
670         This forms part of the old polynomial API. Since version 1.4, the
671         new polynomial API defined in `numpy.polynomial` is preferred.
672         A summary of the differences can be found in the
673         :doc:`transition guide </reference/routines.polynomials>`.
674  
675      Finds the polynomial resulting from the multiplication of the two input
676      polynomials. Each input must be either a poly1d object or a 1D sequence
677      of polynomial coefficients, from highest to lowest degree.
678  
679      Parameters
680      ----------
681      a1, a2 : array_like or poly1d object
682          Input polynomials.
683  
684      Returns
685      -------
686      out : ndarray or poly1d object
687          The polynomial resulting from the multiplication of the inputs. If
688          either inputs is a poly1d object, then the output is also a poly1d
689          object. Otherwise, it is a 1D array of polynomial coefficients from
690          highest to lowest degree.
691  
692      See Also
693      --------
694      poly1d : A one-dimensional polynomial class.
695      poly, polyadd, polyder, polydiv, polyfit, polyint, polysub, polyval
696      convolve : Array convolution. Same output as polymul, but has parameter
697                 for overlap mode.
698  
699      Examples
700      --------
701      >>> np.polymul([1, 2, 3], [9, 5, 1])
702      array([ 9, 23, 38, 17,  3])
703  
704      Using poly1d objects:
705  
706      >>> p1 = np.poly1d([1, 2, 3])
707      >>> p2 = np.poly1d([9, 5, 1])
708      >>> print(p1)
709         2
710      1 x + 2 x + 3
711      >>> print(p2)
712         2
713      9 x + 5 x + 1
714      >>> print(np.polymul(p1, p2))
715         4      3      2
716      9 x + 23 x + 38 x + 17 x + 3
717  
718      )r]r
719  r=r>)r�r�rarcr(r(r)r�s7rcCr�r+r()�u�vr(r(r)�_polydiv_dispatcher�r�r�cCsBt|t�p	t|t�}t|�d}t|�d}|d|d}t|�d}t|�d}d|d}t�t||dd�f|j�}|�|j�}t	d||d�D]}	|||	}
720  |
721  ||	<||	|	|d�|
722  |8<qNtj
723  |dddd�r�|jddkr�|dd�}tj
724  |dddd�r�|jddks||r�t|�t|�fS||fS)	a
725  
726      Returns the quotient and remainder of polynomial division.
727  
728      .. note::
729         This forms part of the old polynomial API. Since version 1.4, the
730         new polynomial API defined in `numpy.polynomial` is preferred.
731         A summary of the differences can be found in the
732         :doc:`transition guide </reference/routines.polynomials>`.
733  
734      The input arrays are the coefficients (including any coefficients
735      equal to zero) of the "numerator" (dividend) and "denominator"
736      (divisor) polynomials, respectively.
737  
738      Parameters
739      ----------
740      u : array_like or poly1d
741          Dividend polynomial's coefficients.
742  
743      v : array_like or poly1d
744          Divisor polynomial's coefficients.
745  
746      Returns
747      -------
748      q : ndarray
749          Coefficients, including those equal to zero, of the quotient.
750      r : ndarray
751          Coefficients, including those equal to zero, of the remainder.
752  
753      See Also
754      --------
755      poly, polyadd, polyder, polydiv, polyfit, polyint, polymul, polysub
756      polyval
757  
758      Notes
759      -----
760      Both `u` and `v` must be 0-d or 1-d (ndim = 0 or 1), but `u.ndim` need
761      not equal `v.ndim`. In other words, all four possible combinations -
762      ``u.ndim = v.ndim = 0``, ``u.ndim = v.ndim = 1``,
763      ``u.ndim = 1, v.ndim = 0``, and ``u.ndim = 0, v.ndim = 1`` - work.
764  
765      Examples
766      --------
767      .. math:: \frac{3x^2 + 5x + 2}{2x + 1} = 1.5x + 1.75, remainder 0.25
768  
769      >>> x = np.array([3.0, 5.0, 2.0])
770      >>> y = np.array([2.0, 1.0])
771      >>> np.polydiv(x, y)
772      (array([1.5 , 1.75]), array([0.25]))
773  
774      rmr
r0r1g�+����=)�rtolrNN)r]r
775  rr8r=rU�maxr4r:�range�allcloser7)r�r�rarirYrer��q�rrZ�dr(r(r)r�s&4"""�rz\*\*([0-9]*)�FcCsd}d}d}d}	t�||�}|durnf|��}|��d}|||d�}	|d}|	dt|�d}
776  dt|	�d|}t|�t|
777  �|ksRt|�t|�|kra||d|d7}|}|
778  }n||	dt|�d7}|dt|	�d|7}q	||d|7}|||d�S)Nr
�� Tr0�
779  z
780   )�	_poly_mat�search�span�groupsr8)�astr�wrapre�line1�line2�output�matr��powerZpartstrZtoadd2Ztoadd1r(r(r)�_raise_powers0�r�c@sPeZdZdZdZedd��Zejdd��Zedd��Zedd	��Z	ed
781  d��Z
782  edd
��Zejdd
��Ze
783  ZeZ
ZZe	ZdBdd�ZdCdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Z e Z!d0d1�Z"e"Z#d2d3�Z$d4d5�Z%d6d7�Z&d8d9�Z'd:d;�Z(dDd>d?�Z)dEd@dA�Z*dS)Fr
784  aX
785      A one-dimensional polynomial class.
786  
787      .. note::
788         This forms part of the old polynomial API. Since version 1.4, the
789         new polynomial API defined in `numpy.polynomial` is preferred.
790         A summary of the differences can be found in the
791         :doc:`transition guide </reference/routines.polynomials>`.
792  
793      A convenience class, used to encapsulate "natural" operations on
794      polynomials so that said operations may take on their customary
795      form in code (see Examples).
796  
797      Parameters
798      ----------
799      c_or_r : array_like
800          The polynomial's coefficients, in decreasing powers, or if
801          the value of the second parameter is True, the polynomial's
802          roots (values where the polynomial evaluates to 0).  For example,
803          ``poly1d([1, 2, 3])`` returns an object that represents
804          :math:`x^2 + 2x + 3`, whereas ``poly1d([1, 2, 3], True)`` returns
805          one that represents :math:`(x-1)(x-2)(x-3) = x^3 - 6x^2 + 11x -6`.
806      r : bool, optional
807          If True, `c_or_r` specifies the polynomial's roots; the default
808          is False.
809      variable : str, optional
810          Changes the variable used when printing `p` from `x` to `variable`
811          (see Examples).
812  
813      Examples
814      --------
815      Construct the polynomial :math:`x^2 + 2x + 3`:
816  
817      >>> p = np.poly1d([1, 2, 3])
818      >>> print(np.poly1d(p))
819         2
820      1 x + 2 x + 3
821  
822      Evaluate the polynomial at :math:`x = 0.5`:
823  
824      >>> p(0.5)
825      4.25
826  
827      Find the roots:
828  
829      >>> p.r
830      array([-1.+1.41421356j, -1.-1.41421356j])
831      >>> p(p.r)
832      array([ -4.44089210e-16+0.j,  -4.44089210e-16+0.j]) # may vary
833  
834      These numbers in the previous line represent (0, 0) to machine precision
835  
836      Show the coefficients:
837  
838      >>> p.c
839      array([1, 2, 3])
840  
841      Display the order (the leading zero-coefficients are removed):
842  
843      >>> p.order
844      2
845  
846      Show the coefficient of the k-th power in the polynomial
847      (which is equivalent to ``p.c[-(i+1)]``):
848  
849      >>> p[1]
850      2
851  
852      Polynomials can be added, subtracted, multiplied, and divided
853      (returns quotient and remainder):
854  
855      >>> p * p
856      poly1d([ 1,  4, 10, 12,  9])
857  
858      >>> (p**3 + 4) / p
859      (poly1d([ 1.,  4., 10., 12.,  9.]), poly1d([4.]))
860  
861      ``asarray(p)`` gives the coefficient array, so polynomials can be
862      used in all functions that accept arrays:
863  
864      >>> p**2 # square of polynomial
865      poly1d([ 1,  4, 10, 12,  9])
866  
867      >>> np.square(p) # square of individual coefficients
868      array([1, 4, 9])
869  
870      The variable used in the string representation of `p` can be modified,
871      using the `variable` parameter:
872  
873      >>> p = np.poly1d([1,2,3], variable='z')
874      >>> print(p)
875         2
876      1 z + 2 z + 3
877  
878      Construct a polynomial from its roots:
879  
880      >>> np.poly1d([1, 2], True)
881      poly1d([ 1., -3.,  2.])
882  
883      This is the same polynomial as obtained by:
884  
885      >>> np.poly1d([1, -1]) * np.poly1d([1, -2])
886      poly1d([ 1, -3,  2])
887  
888      NcC�|jS)z The polynomial coefficients )�_coeffs��selfr(r(r)�coeffs��z
poly1d.coeffscCs||jur	td��dS)NzCannot set attribute)r��AttributeError)r��valuer(r(r)r��s
889  �cCr�)z% The name of the polynomial variable )�	_variabler�r(r(r)�variable�r�zpoly1d.variablecCst|j�dS)z' The order or degree of the polynomial r0)r8r�r�r(r(r)r}�szpoly1d.ordercC�
890  t|j�S)z1 The roots of the polynomial, where self(x) == 0 )rr�r�r(r(r)r�s
891  zpoly1d.rootscCs
892  |jdS�Nr���__dict__r�r(r(r)r��s
893  zpoly1d._coeffscCs||jd<dSr�r�)r�r�r(r(r)r��sFcCs�t|t�r1|j|_|j|_t|j�t|j�r(d}tj|tdd�|j�	|j�|dur/||_dS|r7t
894  |�}t|�}|jdkrDt
d��t|dd�}t|�dkrYtjdg|jd	�}||_|durbd
895  }||_dS)NzbIn the future extra properties will not be copied across when constructing one poly1d from anotherr/rpr0zPolynomial must be 1d only.�f)�trimr
r3rf)r]r
896  r�r��setr�rzr{�
FutureWarning�updaterrrOr<rr8r=rr4)r�Zc_or_rr�r�r�r(r(r)�__init__�s,
897  
898  
899  zpoly1d.__init__cCs|r	t�|j|�St�|j�Sr+)r=rBr�)r��tr(r(r)�	__array__�szpoly1d.__array__cCst|j�}|dd�}d|S)N�rNz
900  poly1d(%s))�reprr�)r��valsr(r(r)�__repr__�s
901  zpoly1d.__repr__cCr�r+)r}r�r(r(r)�__len__�r\zpoly1d.__len__cCs~d}|j}|jtj�|jdk�}t|�d}dd�}t|�D]�\}}t|�s.|t|��}nt|�dkr=d|t	|��}nd|t|��|t	|��f}||}	|	dkrg|dkr]d|f}
902  n<|dkrdd}
903  n5d	}
904  n2|	dkr�|dkrrd	}
905  n'|d
906  kry|}
907  n d||f}
908  n|dkr�d	}
909  n|d
910  kr�d||	f}
911  nd
|||	f}
912  |dkr�|
913  d	kr�|
914915  d�r�d||
916  dd�f}qd||
917  f}q|
918  }qt|�S)N�0r
r0cSs"d|}|�d�r|dd�}|S)Nz%.4gz.0000�����)�endswith)r�r�r(r(r)�	fmt_float�s
919  z!poly1d.__str__.<locals>.fmt_floatz%sjz
920  (%s + %sj)z%sr��bz%s %sz%s**%dz	%s %s**%d�-z%s - %sz%s + %s)r�r�r=�
921  logical_or�
922  accumulater8�	enumeraterrr�
923  startswithr�)r�Zthestr�varr�rVr�rZ�coeffZcoefstrr��newstrr(r(r)�__str__�sN
924  �
925  �zpoly1d.__str__cCst|j|�Sr+)r	r�)r�rcr(r(r)�__call__,�zpoly1d.__call__cCst|j�Sr+)r
926  r�r�r(r(r)�__neg__/r�zpoly1d.__neg__cCr*r+r(r�r(r(r)�__pos__2r.zpoly1d.__pos__cCs0t|�rt|j|�St|�}tt|j|j��Sr+�rr
927  r�r�r��otherr(r(r)�__mul__5�zpoly1d.__mul__cCs0t|�rt||j�St|�}tt|j|j��Sr+r�r�r(r(r)�__rmul__<r�zpoly1d.__rmul__cC�t|�}tt|j|j��Sr+�r
928  rr�r�r(r(r)�__add__C�zpoly1d.__add__cCr�r+r�r�r(r(r)�__radd__Gr�zpoly1d.__radd__cCsLt|�rt|�|ks|dkrtd��dg}t|�D]}t|j|�}qt|�S)Nr
z$Power to non-negative integers only.r0)rrRr<r�rr�r
929  )r�rc�res�_r(r(r)�__pow__Kszpoly1d.__pow__cCr�r+�r
930  rr�r�r(r(r)�__sub__Sr�zpoly1d.__sub__cCst|�}tt|j|j��Sr+r�r�r(r(r)�__rsub__Wr�zpoly1d.__rsub__cCs(t|�rt|j|�St|�}t||�Sr+�rr
931  r�rr�r(r(r)�__div__[�
932  zpoly1d.__div__cCs(t|�rt||j�St|�}t||�Sr+r�r�r(r(r)�__rdiv__dr�zpoly1d.__rdiv__cCs2t|t�stS|jj|jjkrdS|j|jk��S)NF)r]r
933  �NotImplementedr�r7rDr�r(r(r)�__eq__ms
934  
935  z
poly1d.__eq__cCst|t�stS|�|�Sr+)r]r
936  r�r�r�r(r(r)�__ne__ts
937  z
poly1d.__ne__cCsB|j|}||jkr|jj�d�S|dkr|jj�d�S|j|S)Nr
)r}r�r4r@)r�rc�indr(r(r)�__getitem__zs
938  
939  
940  zpoly1d.__getitem__cCs^|j|}|dkr
td��||jkr(t�||j|jj�}t�||jf�|_d}||j|<dS)Nr
z!Does not support negative powers.)r}r<r=rUr�r4r^r�)r��keyrcr�r�r(r(r)�__setitem__�s
941  
942  
943  zpoly1d.__setitem__cCr�r+)�iterr�r�r(r(r)�__iter__�rlzpoly1d.__iter__r0r
cCstt|j||d��S)z�
944          Return an antiderivative (indefinite integral) of this polynomial.
945  
946          Refer to `polyint` for full documentation.
947  
948          See Also
949          --------
950          polyint : equivalent function
951  
952          )rYrZ)r
953  rr�)r�rYrZr(r(r)�integ�szpoly1d.integcCstt|j|d��S)z�
954          Return a derivative of this polynomial.
955  
956          Refer to `polyder` for full documentation.
957  
958          See Also
959          --------
960          polyder : equivalent function
961  
962          )rY)r
963  rr�)r�rYr(r(r)�deriv�szpoly1d.deriv)FNr+)r0r
r2)+r$r%r&r'�__hash__�propertyr��setterr�r}rr�r�r��coef�coefficients�or�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r_r��__rtruediv__r�r�r�r�r�r�r�r(r(r(r)r
964  4s\i
965  
966  
967  
968  
969  
970  
971  
972  
973  9
974  
r
975  �always)NN)r0Nr+r2)NNNN)NFNF)r�)>r'�__all__�	functools�rerz�numpy.core.numeric�core�numericr=�
976  numpy.corerrrrrrrrr�numpy.core.overridesr�numpy.lib.twodim_baserr�numpy.lib.function_baser�numpy.lib.type_checkrrrr�numpy.linalgrr r!�partial�array_function_dispatch�UserWarningrr-rrMrr[rrdrrkrr�r	r�rrrr�r�compiler�r�r
977  �simplefilterr(r(r(r)�<module>sl(�
978  z
979  
980  [
981  d
982  Kx
983  N
984  C
985  5
986  >
987  
988  G
989  y