zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
U
P±d²ã @s dZddddddddd    d
d d g Zd dlZd dlZd dlZd dlmmZd dl    m
Z
m Z m Z m Z mZmZmZmZd dl    mZd dlmZd dlmZmZd dlmZd dlmZmZmZmZd dlm Z m!Z!m"Z"ej#ej$ddZ$edƒGdd „d e%ƒƒZ&dd„Z'e$e'ƒdd„ƒZ(dd„Z)e$e)ƒdd„ƒZ*d;dd „Z+e$e+ƒd<d"d„ƒZ,d=d#d$„Z-e$e-ƒd>d%d„ƒZ.d?d&d'„Z/e$e/ƒd@d)d „ƒZ0d*d+„Z1e$e1ƒd,d    „ƒZ2d-d.„Z3e$e3ƒd/d„ƒZ4e$e3ƒd0d„ƒZ5e$e3ƒd1d„ƒZ6d2d3„Z7e$e7ƒd4d„ƒZ8e 9d5¡Z:dAd7d8„Z;edƒGd9d
„d
ƒƒZ<e =d:e&¡dS)Bz'
Functions to operate on polynomials.
 
ÚpolyÚrootsÚpolyintÚpolyderÚpolyaddÚpolysubÚpolymulÚpolydivÚpolyvalÚpoly1dÚpolyfitÚ RankWarningéN)ÚisscalarÚabsÚfinfoÚ
atleast_1dÚhstackÚdotÚarrayÚones)Ú    overrides)Ú
set_module)ÚdiagÚvander)Ú
trim_zeros)Ú    iscomplexÚrealÚimagÚ mintypecode)ÚeigvalsÚlstsqÚinvÚnumpy)Úmodulec@seZdZdZdS)r zÈ
    Issued by `polyfit` when the Vandermonde matrix is rank deficient.
 
    For more information, a way to suppress the warning, and an example of
    `RankWarning` being issued, see `polyfit`.
 
    N)Ú__name__Ú
__module__Ú __qualname__Ú__doc__©r(r(úKd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\numpy/lib/polynomial.pyr scCs|S©Nr()Ú seq_of_zerosr(r(r)Ú_poly_dispatcher(sr,cCs
t|ƒ}|j}t|ƒdkr@|d|dkr@|ddkr@t|ƒ}n4t|ƒdkrl|j}|tkrt| t|jƒ¡}nt    dƒ‚t|ƒdkr„dS|j}t
d|d}|D]"}t j |t d| g|ddd    }qšt|jjt jƒrt  |t¡}t  t  |¡t  | ¡¡k¡r|j ¡}|S)
a#
    Find the coefficients of a polynomial with the given sequence of roots.
 
    .. note::
       This forms part of the old polynomial API. Since version 1.4, the
       new polynomial API defined in `numpy.polynomial` is preferred.
       A summary of the differences can be found in the
       :doc:`transition guide </reference/routines.polynomials>`.
 
    Returns the coefficients of the polynomial whose leading coefficient
    is one for the given sequence of zeros (multiple roots must be included
    in the sequence as many times as their multiplicity; see Examples).
    A square matrix (or array, which will be treated as a matrix) can also
    be given, in which case the coefficients of the characteristic polynomial
    of the matrix are returned.
 
    Parameters
    ----------
    seq_of_zeros : array_like, shape (N,) or (N, N)
        A sequence of polynomial roots, or a square array or matrix object.
 
    Returns
    -------
    c : ndarray
        1D array of polynomial coefficients from highest to lowest degree:
 
        ``c[0] * x**(N) + c[1] * x**(N-1) + ... + c[N-1] * x + c[N]``
        where c[0] always equals 1.
 
    Raises
    ------
    ValueError
        If input is the wrong shape (the input must be a 1-D or square
        2-D array).
 
    See Also
    --------
    polyval : Compute polynomial values.
    roots : Return the roots of a polynomial.
    polyfit : Least squares polynomial fit.
    poly1d : A one-dimensional polynomial class.
 
    Notes
    -----
    Specifying the roots of a polynomial still leaves one degree of
    freedom, typically represented by an undetermined leading
    coefficient. [1]_ In the case of this function, that coefficient -
    the first one in the returned array - is always taken as one. (If
    for some reason you have one other point, the only automatic way
    presently to leverage that information is to use ``polyfit``.)
 
    The characteristic polynomial, :math:`p_a(t)`, of an `n`-by-`n`
    matrix **A** is given by
 
        :math:`p_a(t) = \mathrm{det}(t\, \mathbf{I} - \mathbf{A})`,
 
    where **I** is the `n`-by-`n` identity matrix. [2]_
 
    References
    ----------
    .. [1] M. Sullivan and M. Sullivan, III, "Algebra and Trignometry,
       Enhanced With Graphing Utilities," Prentice-Hall, pg. 318, 1996.
 
    .. [2] G. Strang, "Linear Algebra and Its Applications, 2nd Edition,"
       Academic Press, pg. 182, 1980.
 
    Examples
    --------
    Given a sequence of a polynomial's zeros:
 
    >>> np.poly((0, 0, 0)) # Multiple root example
    array([1., 0., 0., 0.])
 
    The line above represents z**3 + 0*z**2 + 0*z + 0.
 
    >>> np.poly((-1./2, 0, 1./2))
    array([ 1.  ,  0.  , -0.25,  0.  ])
 
    The line above represents z**3 - z/4
 
    >>> np.poly((np.random.random(1)[0], 0, np.random.random(1)[0]))
    array([ 1.        , -0.77086955,  0.08618131,  0.        ]) # random
 
    Given a square array object:
 
    >>> P = np.array([[0, 1./3], [-1./2, 0]])
    >>> np.poly(P)
    array([1.        , 0.        , 0.16666667])
 
    Note how in all cases the leading coefficient is always 1.
 
    ér éz.input must be 1d or non-empty square 2d array.çð?)r.©ÚdtypeÚfull)Úmode)rÚshapeÚlenrr1ÚobjectÚastyperÚcharÚ
ValueErrorrÚNXÚconvolverÚ
issubclassÚtypeÚcomplexfloatingÚasarrayÚcomplexÚallÚsortÚ    conjugaterÚcopy)r+ÚshÚdtÚaZzerorr(r(r)r,s(^(
      
cCs|Sr*r()Úpr(r(r)Ú_roots_dispatcher§srIcCst|ƒ}|jdkrtdƒ‚t t |¡¡d}t|ƒdkrDt g¡St|ƒ|dd}|t|dƒt|dƒd…}t    |j
j tj tj fƒs˜| t¡}t|ƒ}|dkrîtt |df|j
¡dƒ}|dd… |d|ddd…f<t|ƒ}n
t g¡}t|t ||j
¡fƒ}|S)aà
    Return the roots of a polynomial with coefficients given in p.
 
    .. note::
       This forms part of the old polynomial API. Since version 1.4, the
       new polynomial API defined in `numpy.polynomial` is preferred.
       A summary of the differences can be found in the
       :doc:`transition guide </reference/routines.polynomials>`.
 
    The values in the rank-1 array `p` are coefficients of a polynomial.
    If the length of `p` is n+1 then the polynomial is described by::
 
      p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n]
 
    Parameters
    ----------
    p : array_like
        Rank-1 array of polynomial coefficients.
 
    Returns
    -------
    out : ndarray
        An array containing the roots of the polynomial.
 
    Raises
    ------
    ValueError
        When `p` cannot be converted to a rank-1 array.
 
    See also
    --------
    poly : Find the coefficients of a polynomial with a given sequence
           of roots.
    polyval : Compute polynomial values.
    polyfit : Least squares polynomial fit.
    poly1d : A one-dimensional polynomial class.
 
    Notes
    -----
    The algorithm relies on computing the eigenvalues of the
    companion matrix [1]_.
 
    References
    ----------
    .. [1] R. A. Horn & C. R. Johnson, *Matrix Analysis*.  Cambridge, UK:
        Cambridge University Press, 1999, pp. 146-7.
 
    Examples
    --------
    >>> coeff = [3.2, 2, 1]
    >>> np.roots(coeff)
    array([-0.3125+0.46351241j, -0.3125-0.46351241j])
 
    r.zInput must be a rank-1 array.r éÿÿÿÿr-N)rÚndimr9r:ZnonzeroZravelr5rÚintr<r1r=Zfloatingr>r7ÚfloatrrrrÚzeros)rHZnon_zeroZtrailing_zerosÚNÚArr(r(r)r«s$9
 
 
"
 
cCs|fSr*r()rHÚmÚkr(r(r)Ú_polyint_dispatchersrSr.c    Csòt|ƒ}|dkrtdƒ‚|dkr,t |t¡}t|ƒ}t|ƒdkr\|dkr\|dt |t¡}t|ƒ|krptdƒ‚t|t    ƒ}t 
|¡}|dkrœ|r˜t    |ƒS|St  |  t  t|ƒdd¡¡|dgf¡}t||d|dd…d}|rêt    |ƒS|SdS)a¡
    Return an antiderivative (indefinite integral) of a polynomial.
 
    .. note::
       This forms part of the old polynomial API. Since version 1.4, the
       new polynomial API defined in `numpy.polynomial` is preferred.
       A summary of the differences can be found in the
       :doc:`transition guide </reference/routines.polynomials>`.
 
    The returned order `m` antiderivative `P` of polynomial `p` satisfies
    :math:`\frac{d^m}{dx^m}P(x) = p(x)` and is defined up to `m - 1`
    integration constants `k`. The constants determine the low-order
    polynomial part
 
    .. math:: \frac{k_{m-1}}{0!} x^0 + \ldots + \frac{k_0}{(m-1)!}x^{m-1}
 
    of `P` so that :math:`P^{(j)}(0) = k_{m-j-1}`.
 
    Parameters
    ----------
    p : array_like or poly1d
        Polynomial to integrate.
        A sequence is interpreted as polynomial coefficients, see `poly1d`.
    m : int, optional
        Order of the antiderivative. (Default: 1)
    k : list of `m` scalars or scalar, optional
        Integration constants. They are given in the order of integration:
        those corresponding to highest-order terms come first.
 
        If ``None`` (default), all constants are assumed to be zero.
        If `m = 1`, a single scalar can be given instead of a list.
 
    See Also
    --------
    polyder : derivative of a polynomial
    poly1d.integ : equivalent method
 
    Examples
    --------
    The defining property of the antiderivative:
 
    >>> p = np.poly1d([1,1,1])
    >>> P = np.polyint(p)
    >>> P
     poly1d([ 0.33333333,  0.5       ,  1.        ,  0.        ]) # may vary
    >>> np.polyder(P) == p
    True
 
    The integration constants default to zero, but can be specified:
 
    >>> P = np.polyint(p, 3)
    >>> P(0)
    0.0
    >>> np.polyder(P)(0)
    0.0
    >>> np.polyder(P, 2)(0)
    0.0
    >>> P = np.polyint(p, 3, k=[6,5,3])
    >>> P
    poly1d([ 0.01666667,  0.04166667,  0.16666667,  3. ,  5. ,  3. ]) # may vary
 
    Note that 3 = 6 / 2!, and that the constants are given in the order of
    integrations. Constant of the highest-order polynomial term comes first:
 
    >>> np.polyder(P, 2)(0)
    6.0
    >>> np.polyder(P, 1)(0)
    5.0
    >>> P(0)
    3.0
 
    r z0Order of integral must be positive (see polyder)Nr.z7k must be a scalar or a rank-1 array of length 1 or >m.rJ)rR)rLr9r:rNrMrr5rÚ
isinstancer
r?Ú concatenateÚ __truediv__Úaranger)rHrQrRÚtruepolyÚyÚvalr(r(r)r s.J  ÿ
 
(cCs|fSr*r()rHrQr(r(r)Ú_polyder_dispatcherpsr[cCs~t|ƒ}|dkrtdƒ‚t|tƒ}t |¡}t|ƒd}|dd…t |dd¡}|dkr`|}nt||dƒ}|rzt|ƒ}|S)ax
    Return the derivative of the specified order of a polynomial.
 
    .. note::
       This forms part of the old polynomial API. Since version 1.4, the
       new polynomial API defined in `numpy.polynomial` is preferred.
       A summary of the differences can be found in the
       :doc:`transition guide </reference/routines.polynomials>`.
 
    Parameters
    ----------
    p : poly1d or sequence
        Polynomial to differentiate.
        A sequence is interpreted as polynomial coefficients, see `poly1d`.
    m : int, optional
        Order of differentiation (default: 1)
 
    Returns
    -------
    der : poly1d
        A new polynomial representing the derivative.
 
    See Also
    --------
    polyint : Anti-derivative of a polynomial.
    poly1d : Class for one-dimensional polynomials.
 
    Examples
    --------
    The derivative of the polynomial :math:`x^3 + x^2 + x^1 + 1` is:
 
    >>> p = np.poly1d([1,1,1,1])
    >>> p2 = np.polyder(p)
    >>> p2
    poly1d([3, 2, 1])
 
    which evaluates to:
 
    >>> p2(2.)
    17.0
 
    We can verify this, approximating the derivative with
    ``(f(x + h) - f(x))/h``:
 
    >>> (p(2. + 0.001) - p(2.)) / 0.001
    17.007000999997857
 
    The fourth-order derivative of a 3rd-order polynomial is zero:
 
    >>> np.polyder(p, 2)
    poly1d([6, 2])
    >>> np.polyder(p, 3)
    poly1d([6])
    >>> np.polyder(p, 4)
    poly1d([0])
 
    r z2Order of derivative must be positive (see polyint)r.NrJ)    rLr9rTr
r:r?r5rWr)rHrQrXÚnrYrZr(r(r)rts;
 
 cCs
|||fSr*r()ÚxrYÚdegÚrcondr2ÚwÚcovr(r(r)Ú_polyfit_dispatcherÀsrbFcCs\t|ƒd}t |¡d}t |¡d}|dkr8tdƒ‚|jdkrJtdƒ‚|jdkr\tdƒ‚|jdksp|jdkrxtdƒ‚|jd|jdkr”td    ƒ‚|d
kr°t|ƒt    |j
ƒj }t ||ƒ}|}    |d
k    rHt |¡d}|jdkrètd ƒ‚|jd|jdkrtd ƒ‚||d
d
…tj f9}|    jdkr@|    |d
d
…tj f9}    n|    |9}    t ||jdd ¡}
||
}t||    |ƒ\} } } }| j|
j} | |krª|sªd}tj|tdd|r¾| | | ||fS|rTtt|j|ƒƒ}|t |
|
¡}|dkrôd}n&t|ƒ|kr
tdƒ‚| t|ƒ|}|jdkr2| ||fS| |d
d
…d
d
…tj f|fSn| Sd
S)a¦
    Least squares polynomial fit.
 
    .. note::
       This forms part of the old polynomial API. Since version 1.4, the
       new polynomial API defined in `numpy.polynomial` is preferred.
       A summary of the differences can be found in the
       :doc:`transition guide </reference/routines.polynomials>`.
 
    Fit a polynomial ``p(x) = p[0] * x**deg + ... + p[deg]`` of degree `deg`
    to points `(x, y)`. Returns a vector of coefficients `p` that minimises
    the squared error in the order `deg`, `deg-1`, ... `0`.
 
    The `Polynomial.fit <numpy.polynomial.polynomial.Polynomial.fit>` class
    method is recommended for new code as it is more stable numerically. See
    the documentation of the method for more information.
 
    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.
    w : array_like, shape (M,), optional
        Weights. If not None, the weight ``w[i]`` applies to the unsquared
        residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are
        chosen so that the errors of the products ``w[i]*y[i]`` all have the
        same variance.  When using inverse-variance weighting, use
        ``w[i] = 1/sigma(y[i])``.  The default value is None.
    cov : bool or str, optional
        If given and not `False`, return not just the estimate but also its
        covariance matrix. By default, the covariance are scaled by
        chi2/dof, where dof = M - (deg + 1), i.e., the weights are presumed
        to be unreliable except in a relative sense and everything is scaled
        such that the reduced chi2 is unity. This scaling is omitted if
        ``cov='unscaled'``, as is relevant for the case that the weights are
        w = 1/sigma, with sigma known to be a reliable estimate of the
        uncertainty.
 
    Returns
    -------
    p : ndarray, shape (deg + 1,) or (deg + 1, K)
        Polynomial coefficients, highest power first.  If `y` was 2-D, the
        coefficients for `k`-th data set are in ``p[:,k]``.
 
    residuals, rank, singular_values, rcond
        These values are only returned if ``full == True``
 
        - residuals -- sum of squared residuals of the least squares fit
        - rank -- the effective rank of the scaled Vandermonde
           coefficient matrix
        - singular_values -- singular values of the scaled Vandermonde
           coefficient matrix
        - rcond -- value of `rcond`.
 
        For more details, see `numpy.linalg.lstsq`.
 
    V : ndarray, shape (M,M) or (M,M,K)
        Present only if ``full == False`` and ``cov == True``.  The covariance
        matrix of the polynomial coefficient estimates.  The diagonal of
        this matrix are the variance estimates for each coefficient.  If y
        is a 2-D array, then the covariance matrix for the `k`-th data set
        are in ``V[:,:,k]``
 
 
    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', np.RankWarning)
 
    See Also
    --------
    polyval : Compute polynomial values.
    linalg.lstsq : Computes a least-squares fit.
    scipy.interpolate.UnivariateSpline : Computes spline fits.
 
    Notes
    -----
    The solution minimizes the squared error
 
    .. math::
        E = \sum_{j=0}^k |p(x_j) - y_j|^2
 
    in the equations::
 
        x[0]**n * p[0] + ... + x[0] * p[n-1] + p[n] = y[0]
        x[1]**n * p[0] + ... + x[1] * p[n-1] + p[n] = y[1]
        ...
        x[k]**n * p[0] + ... + x[k] * p[n-1] + p[n] = y[k]
 
    The coefficient matrix of the coefficients `p` is a Vandermonde matrix.
 
    `polyfit` issues a `RankWarning` when the least-squares fit is badly
    conditioned. This implies that the best fit is not well-defined due
    to numerical error. The results may be improved by lowering the polynomial
    degree or by replacing `x` by `x` - `x`.mean(). The `rcond` parameter
    can also be set to a value smaller than its default, but the resulting
    fit may be spurious: including contributions from the small singular
    values can add numerical noise to the result.
 
    Note that fitting polynomial coefficients is inherently badly conditioned
    when the degree of the polynomial is large or the interval of sample points
    is badly centered. The quality of the fit should always be checked in these
    cases. When polynomial fits are not satisfactory, splines may be a good
    alternative.
 
    References
    ----------
    .. [1] Wikipedia, "Curve fitting",
           https://en.wikipedia.org/wiki/Curve_fitting
    .. [2] Wikipedia, "Polynomial interpolation",
           https://en.wikipedia.org/wiki/Polynomial_interpolation
 
    Examples
    --------
    >>> import warnings
    >>> x = np.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0])
    >>> y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
    >>> z = np.polyfit(x, y, 3)
    >>> z
    array([ 0.08703704, -0.81349206,  1.69312169, -0.03968254]) # may vary
 
    It is convenient to use `poly1d` objects for dealing with polynomials:
 
    >>> p = np.poly1d(z)
    >>> p(0.5)
    0.6143849206349179 # may vary
    >>> p(3.5)
    -0.34732142857143039 # may vary
    >>> p(10)
    22.579365079365115 # may vary
 
    High-order polynomials may oscillate wildly:
 
    >>> with warnings.catch_warnings():
    ...     warnings.simplefilter('ignore', np.RankWarning)
    ...     p30 = np.poly1d(np.polyfit(x, y, 30))
    ...
    >>> p30(4)
    -0.80000000000000204 # may vary
    >>> p30(5)
    -0.99999999999999445 # may vary
    >>> p30(4.5)
    -0.10547061179440398 # may vary
 
    Illustration:
 
    >>> import matplotlib.pyplot as plt
    >>> xp = np.linspace(-2, 6, 100)
    >>> _ = plt.plot(x, y, '.', xp, p(xp), '-', xp, p30(xp), '--')
    >>> plt.ylim(-2,2)
    (-2, 2)
    >>> plt.show()
 
    r.ç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)Zaxisz!Polyfit may be poorly conditionedé©Ú
stacklevelZunscaledzJthe number of data points must exceed order to scale the covariance matrix)rLr:r?r9rKÚ    TypeErrorÚsizer4r5rr1ZepsrZnewaxisÚsqrtÚsumr ÚTÚwarningsÚwarnr r!rÚouter)r]rYr^r_r2r`raÚorderÚlhsÚrhsÚscaleÚcZresidsZrankÚsÚmsgZVbaseZfacr(r(r)r Äsb1 
 
 
 
 
 
  "cCs||fSr*r()rHr]r(r(r)Ú_polyval_dispatcher¼srvcCsHt |¡}t|tƒrd}nt |¡}t |¡}|D]}|||}q2|S)a³
    Evaluate a polynomial at specific values.
 
    .. note::
       This forms part of the old polynomial API. Since version 1.4, the
       new polynomial API defined in `numpy.polynomial` is preferred.
       A summary of the differences can be found in the
       :doc:`transition guide </reference/routines.polynomials>`.
 
    If `p` is of length N, this function returns the value:
 
        ``p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]``
 
    If `x` is a sequence, then ``p(x)`` is returned for each element of ``x``.
    If `x` is another polynomial then the composite polynomial ``p(x(t))``
    is returned.
 
    Parameters
    ----------
    p : array_like or poly1d object
       1D array of polynomial coefficients (including coefficients equal
       to zero) from highest degree to the constant term, or an
       instance of poly1d.
    x : array_like or poly1d object
       A number, an array of numbers, or an instance of poly1d, at
       which to evaluate `p`.
 
    Returns
    -------
    values : ndarray or poly1d
       If `x` is a poly1d instance, the result is the composition of the two
       polynomials, i.e., `x` is "substituted" in `p` and the simplified
       result is returned. In addition, the type of `x` - array_like or
       poly1d - governs the type of the output: `x` array_like => `values`
       array_like, `x` a poly1d object => `values` is also.
 
    See Also
    --------
    poly1d: A polynomial class.
 
    Notes
    -----
    Horner's scheme [1]_ is used to evaluate the polynomial. Even so,
    for polynomials of high degree the values may be inaccurate due to
    rounding errors. Use carefully.
 
    If `x` is a subtype of `ndarray` the return value will be of the same type.
 
    References
    ----------
    .. [1] I. N. Bronshtein, K. A. Semendyayev, and K. A. Hirsch (Eng.
       trans. Ed.), *Handbook of Mathematics*, New York, Van Nostrand
       Reinhold Co., 1985, pg. 720.
 
    Examples
    --------
    >>> np.polyval([3,0,1], 5)  # 3 * 5**2 + 0 * 5**1 + 1
    76
    >>> np.polyval([3,0,1], np.poly1d(5))
    poly1d([76])
    >>> np.polyval(np.poly1d([3,0,1]), 5)
    76
    >>> np.polyval(np.poly1d([3,0,1]), np.poly1d(5))
    poly1d([76])
 
    r )r:r?rTr
Z
asanyarrayZ
zeros_like)rHr]rYÚpvr(r(r)r    ÀsD
 
 
 
cCs||fSr*r()Úa1Úa2r(r(r)Ú_binary_op_dispatchersrzcCs¤t|tƒpt|tƒ}t|ƒ}t|ƒ}t|ƒt|ƒ}|dkrF||}nN|dkrpt ||j¡}t ||f¡|}n$t t|ƒ|j¡}|t ||f¡}|r t|ƒ}|S)aN
    Find the sum of two polynomials.
 
    .. note::
       This forms part of the old polynomial API. Since version 1.4, the
       new polynomial API defined in `numpy.polynomial` is preferred.
       A summary of the differences can be found in the
       :doc:`transition guide </reference/routines.polynomials>`.
 
    Returns the polynomial resulting from the sum of two input polynomials.
    Each input must be either a poly1d object or a 1D sequence of polynomial
    coefficients, from highest to lowest degree.
 
    Parameters
    ----------
    a1, a2 : array_like or poly1d object
        Input polynomials.
 
    Returns
    -------
    out : ndarray or poly1d object
        The sum of the inputs. If either input is a poly1d object, then the
        output is also a poly1d object. Otherwise, it is a 1D array of
        polynomial coefficients from highest to lowest degree.
 
    See Also
    --------
    poly1d : A one-dimensional polynomial class.
    poly, polyadd, polyder, polydiv, polyfit, polyint, polysub, polyval
 
    Examples
    --------
    >>> np.polyadd([1, 2], [9, 5, 4])
    array([9, 6, 6])
 
    Using poly1d objects:
 
    >>> p1 = np.poly1d([1, 2])
    >>> p2 = np.poly1d([9, 5, 4])
    >>> print(p1)
    1 x + 2
    >>> print(p2)
       2
    9 x + 5 x + 4
    >>> print(np.polyadd(p1, p2))
       2
    9 x + 6 x + 6
 
    r ©    rTr
rr5r:rNr1rUr©rxryrXZdiffrZÚzrr(r(r)rs3
cCs¤t|tƒpt|tƒ}t|ƒ}t|ƒ}t|ƒt|ƒ}|dkrF||}nN|dkrpt ||j¡}t ||f¡|}n$t t|ƒ|j¡}|t ||f¡}|r t|ƒ}|S)a
    Difference (subtraction) of two polynomials.
 
    .. note::
       This forms part of the old polynomial API. Since version 1.4, the
       new polynomial API defined in `numpy.polynomial` is preferred.
       A summary of the differences can be found in the
       :doc:`transition guide </reference/routines.polynomials>`.
 
    Given two polynomials `a1` and `a2`, returns ``a1 - a2``.
    `a1` and `a2` can be either array_like sequences of the polynomials'
    coefficients (including coefficients equal to zero), or `poly1d` objects.
 
    Parameters
    ----------
    a1, a2 : array_like or poly1d
        Minuend and subtrahend polynomials, respectively.
 
    Returns
    -------
    out : ndarray or poly1d
        Array or `poly1d` object of the difference polynomial's coefficients.
 
    See Also
    --------
    polyval, polydiv, polymul, polyadd
 
    Examples
    --------
    .. math:: (2 x^2 + 10 x - 2) - (3 x^2 + 10 x -4) = (-x^2 + 2)
 
    >>> np.polysub([2, 10, -2], [3, 10, -4])
    array([-1,  0,  2])
 
    r r{r|r(r(r)rWs%
cCsBt|tƒpt|tƒ}t|ƒt|ƒ}}t ||¡}|r>t|ƒ}|S)a;
    Find the product of two polynomials.
 
    .. note::
       This forms part of the old polynomial API. Since version 1.4, the
       new polynomial API defined in `numpy.polynomial` is preferred.
       A summary of the differences can be found in the
       :doc:`transition guide </reference/routines.polynomials>`.
 
    Finds the polynomial resulting from the multiplication of the two input
    polynomials. Each input must be either a poly1d object or a 1D sequence
    of polynomial coefficients, from highest to lowest degree.
 
    Parameters
    ----------
    a1, a2 : array_like or poly1d object
        Input polynomials.
 
    Returns
    -------
    out : ndarray or poly1d object
        The polynomial resulting from the multiplication of the inputs. If
        either inputs is a poly1d object, then the output is also a poly1d
        object. Otherwise, it is a 1D array of polynomial coefficients from
        highest to lowest degree.
 
    See Also
    --------
    poly1d : A one-dimensional polynomial class.
    poly, polyadd, polyder, polydiv, polyfit, polyint, polysub, polyval
    convolve : Array convolution. Same output as polymul, but has parameter
               for overlap mode.
 
    Examples
    --------
    >>> np.polymul([1, 2, 3], [9, 5, 1])
    array([ 9, 23, 38, 17,  3])
 
    Using poly1d objects:
 
    >>> p1 = np.poly1d([1, 2, 3])
    >>> p2 = np.poly1d([9, 5, 1])
    >>> print(p1)
       2
    1 x + 2 x + 3
    >>> print(p2)
       2
    9 x + 5 x + 1
    >>> print(np.polymul(p1, p2))
       4      3      2
    9 x + 23 x + 38 x + 17 x + 3
 
    )rTr
r:r;)rxryrXrZr(r(r)rs 7 cCs||fSr*r()ÚuÚvr(r(r)Ú_polydiv_dispatcherÌsr€c Cs(t|tƒpt|tƒ}t|ƒd}t|ƒd}|d|d}t|ƒd}t|ƒd}d|d}t t||ddƒf|j¡}| |j¡}t    d||dƒD]8}    |||    }
|
||    <||    |    |d…|
|8<qœtj
|ddddr
|j ddkr
|dd…}qÖ|r t|ƒt|ƒfS||fS)    a
 
    Returns the quotient and remainder of polynomial division.
 
    .. note::
       This forms part of the old polynomial API. Since version 1.4, the
       new polynomial API defined in `numpy.polynomial` is preferred.
       A summary of the differences can be found in the
       :doc:`transition guide </reference/routines.polynomials>`.
 
    The input arrays are the coefficients (including any coefficients
    equal to zero) of the "numerator" (dividend) and "denominator"
    (divisor) polynomials, respectively.
 
    Parameters
    ----------
    u : array_like or poly1d
        Dividend polynomial's coefficients.
 
    v : array_like or poly1d
        Divisor polynomial's coefficients.
 
    Returns
    -------
    q : ndarray
        Coefficients, including those equal to zero, of the quotient.
    r : ndarray
        Coefficients, including those equal to zero, of the remainder.
 
    See Also
    --------
    poly, polyadd, polyder, polydiv, polyfit, polyint, polymul, polysub
    polyval
 
    Notes
    -----
    Both `u` and `v` must be 0-d or 1-d (ndim = 0 or 1), but `u.ndim` need
    not equal `v.ndim`. In other words, all four possible combinations -
    ``u.ndim = v.ndim = 0``, ``u.ndim = v.ndim = 1``,
    ``u.ndim = 1, v.ndim = 0``, and ``u.ndim = 0, v.ndim = 1`` - work.
 
    Examples
    --------
    .. math:: \frac{3x^2 + 5x + 2}{2x + 1} = 1.5x + 1.75, remainder 0.25
 
    >>> x = np.array([3.0, 5.0, 2.0])
    >>> y = np.array([2.0, 1.0])
    >>> np.polydiv(x, y)
    (array([1.5 , 1.75]), array([0.25]))
 
    rcr r.r/g›+¡†›„=)ZrtolrJN) rTr
rr5r:rNÚmaxr1r7ÚrangeZallcloser4) r~rrXr`rQr\rrÚqÚrrRÚdr(r(r)rÐs$4       "&z \*\*([0-9]*)éFc Csd}d}d}d}t ||¡}|dkr&qò| ¡}| ¡d}|||d…}    |d}|    dt|ƒd}
dt|    ƒd|} t|ƒt|
ƒ|ks¢t|ƒt| ƒ|krÀ||d|d7}| }|
}q||    dt|ƒd7}|dt|    ƒd|7}q||d|7}|||d…S)Nr Úú r.Ú
z
 )Ú    _poly_matÚsearchÚspanÚgroupsr5) ZastrÚwrapr\Zline1Zline2ÚoutputÚmatrŒÚpowerZpartstrZtoadd2Ztoadd1r(r(r)Ú _raise_powers.  ÿr’c@sPeZdZdZdZedd„ƒZejdd„ƒZedd„ƒZedd    „ƒZ    ed
d „ƒZ
ed d „ƒZ e jdd „ƒZ e
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
aX
    A one-dimensional polynomial class.
 
    .. note::
       This forms part of the old polynomial API. Since version 1.4, the
       new polynomial API defined in `numpy.polynomial` is preferred.
       A summary of the differences can be found in the
       :doc:`transition guide </reference/routines.polynomials>`.
 
    A convenience class, used to encapsulate "natural" operations on
    polynomials so that said operations may take on their customary
    form in code (see Examples).
 
    Parameters
    ----------
    c_or_r : array_like
        The polynomial's coefficients, in decreasing powers, or if
        the value of the second parameter is True, the polynomial's
        roots (values where the polynomial evaluates to 0).  For example,
        ``poly1d([1, 2, 3])`` returns an object that represents
        :math:`x^2 + 2x + 3`, whereas ``poly1d([1, 2, 3], True)`` returns
        one that represents :math:`(x-1)(x-2)(x-3) = x^3 - 6x^2 + 11x -6`.
    r : bool, optional
        If True, `c_or_r` specifies the polynomial's roots; the default
        is False.
    variable : str, optional
        Changes the variable used when printing `p` from `x` to `variable`
        (see Examples).
 
    Examples
    --------
    Construct the polynomial :math:`x^2 + 2x + 3`:
 
    >>> p = np.poly1d([1, 2, 3])
    >>> print(np.poly1d(p))
       2
    1 x + 2 x + 3
 
    Evaluate the polynomial at :math:`x = 0.5`:
 
    >>> p(0.5)
    4.25
 
    Find the roots:
 
    >>> p.r
    array([-1.+1.41421356j, -1.-1.41421356j])
    >>> p(p.r)
    array([ -4.44089210e-16+0.j,  -4.44089210e-16+0.j]) # may vary
 
    These numbers in the previous line represent (0, 0) to machine precision
 
    Show the coefficients:
 
    >>> p.c
    array([1, 2, 3])
 
    Display the order (the leading zero-coefficients are removed):
 
    >>> p.order
    2
 
    Show the coefficient of the k-th power in the polynomial
    (which is equivalent to ``p.c[-(i+1)]``):
 
    >>> p[1]
    2
 
    Polynomials can be added, subtracted, multiplied, and divided
    (returns quotient and remainder):
 
    >>> p * p
    poly1d([ 1,  4, 10, 12,  9])
 
    >>> (p**3 + 4) / p
    (poly1d([ 1.,  4., 10., 12.,  9.]), poly1d([4.]))
 
    ``asarray(p)`` gives the coefficient array, so polynomials can be
    used in all functions that accept arrays:
 
    >>> p**2 # square of polynomial
    poly1d([ 1,  4, 10, 12,  9])
 
    >>> np.square(p) # square of individual coefficients
    array([1, 4, 9])
 
    The variable used in the string representation of `p` can be modified,
    using the `variable` parameter:
 
    >>> p = np.poly1d([1,2,3], variable='z')
    >>> print(p)
       2
    1 z + 2 z + 3
 
    Construct a polynomial from its roots:
 
    >>> np.poly1d([1, 2], True)
    poly1d([ 1., -3.,  2.])
 
    This is the same polynomial as obtained by:
 
    >>> np.poly1d([1, -1]) * np.poly1d([1, -2])
    poly1d([ 1, -3,  2])
 
    NcCs|jS)z The polynomial coefficients )Ú_coeffs©Úselfr(r(r)Úcoeffs¡sz poly1d.coeffscCs||jk    rtdƒ‚dS)NzCannot set attribute)r“ÚAttributeError)r•Úvaluer(r(r)r–¦s
cCs|jS)z% The name of the polynomial variable )Ú    _variabler”r(r(r)Úvariable¬szpoly1d.variablecCst|jƒdS)z' The order or degree of the polynomial r.)r5r“r”r(r(r)ro²sz poly1d.ordercCs
t|jƒS)z1 The roots of the polynomial, where self(x) == 0 )rr“r”r(r(r)r·sz poly1d.rootscCs
|jdS©Nr–©Ú__dict__r”r(r(r)r“¾szpoly1d._coeffscCs||jd<dSr›rœ)r•r–r(r(r)r“ÁsFcCsÎt|tƒrb|j|_|j|_t|jƒt|jƒrPd}tj|tdd|j     |j¡|dk    r^||_dS|rnt
|ƒ}t |ƒ}|j dkrˆt dƒ‚t|dd}t|ƒdkr²tjdg|jd    }||_|dkrÄd
}||_dS) NzbIn the future extra properties will not be copied across when constructing one poly1d from anotherr-rer.zPolynomial must be 1d only.Úf)Ztrimr r0r])rTr
r™r“ÚsetrrlrmÚ FutureWarningÚupdaterrrKr9rr5r:rr1)r•Zc_or_rr„ršrur(r(r)Ú__init__Ês,
 
  zpoly1d.__init__cCs"|rt |j|¡St |j¡SdSr*)r:r?r–)r•Útr(r(r)Ú    __array__åszpoly1d.__array__cCst|jƒ}|dd…}d|S)NérJz
poly1d(%s))Úreprr–)r•Úvalsr(r(r)Ú__repr__ës
 zpoly1d.__repr__cCs|jSr*)ror”r(r(r)Ú__len__ðszpoly1d.__len__c CsŒd}|j}|jtj |jdk¡}t|ƒd}dd„}t|ƒD]B\}}t|ƒs^|t|ƒƒ}n:t|ƒdkr|d|t    |ƒƒ}nd|t|ƒƒ|t    |ƒƒf}||}    |    dkrÐ|dkr¼d|f}
n|dkrÊd}
nd    }
nj|    dkr|dkrèd    }
n|d
krö|}
n d ||f}
n6|dkrd    }
n&|d
kr,d ||    f}
nd |||    f}
|dkr~|
d    kr‚|
 
d¡rpd||
dd…f}n d||
f}q>|
}q>t |ƒS)NÚ0r r.cSs"d|}| d¡r|dd…}|S)Nz%.4gz.0000éûÿÿÿ)Úendswith)rƒrtr(r(r)Ú    fmt_floatûs
 z!poly1d.__str__.<locals>.fmt_floatz%sjz
(%s + %sj)z%sr‡Úbz%s %sz%s**%dz    %s %s**%dú-z%s - %sz%s + %s) ršr–r:Ú
logical_orÚ
accumulater5Ú    enumeraterrrÚ
startswithr’) r•ZthestrÚvarr–rOr­rRZcoeffZcoefstrr‘Znewstrr(r(r)Ú__str__ósL   
ÿ 
 
 
 
 
 zpoly1d.__str__cCs t|j|ƒSr*)r    r–)r•rZr(r(r)Ú__call__,szpoly1d.__call__cCs t|j ƒSr*)r
r–r”r(r(r)Ú__neg__/szpoly1d.__neg__cCs|Sr*r(r”r(r(r)Ú__pos__2szpoly1d.__pos__cCs4t|ƒrt|j|ƒSt|ƒ}tt|j|jƒƒSdSr*©rr
r–r©r•Úotherr(r(r)Ú__mul__5szpoly1d.__mul__cCs4t|ƒrt||jƒSt|ƒ}tt|j|jƒƒSdSr*r¹rºr(r(r)Ú__rmul__<szpoly1d.__rmul__cCst|ƒ}tt|j|jƒƒSr*©r
rr–rºr(r(r)Ú__add__Cszpoly1d.__add__cCst|ƒ}tt|j|jƒƒSr*r¾rºr(r(r)Ú__radd__Gszpoly1d.__radd__cCsLt|ƒrt|ƒ|ks|dkr$tdƒ‚dg}t|ƒD]}t|j|ƒ}q2t|ƒS)Nr z$Power to non-negative integers only.r.)rrLr9r‚rr–r
)r•rZÚresÚ_r(r(r)Ú__pow__Ks  zpoly1d.__pow__cCst|ƒ}tt|j|jƒƒSr*©r
rr–rºr(r(r)Ú__sub__Sszpoly1d.__sub__cCst|ƒ}tt|j|jƒƒSr*rÄrºr(r(r)Ú__rsub__Wszpoly1d.__rsub__cCs,t|ƒrt|j|ƒSt|ƒ}t||ƒSdSr*©rr
r–rrºr(r(r)Ú__div__[szpoly1d.__div__cCs,t|ƒrt||jƒSt|ƒ}t||ƒSdSr*rÇrºr(r(r)Ú__rdiv__dszpoly1d.__rdiv__cCs2t|tƒstS|jj|jjkr"dS|j|jk ¡S)NF)rTr
ÚNotImplementedr–r4rArºr(r(r)Ú__eq__ms
 
z poly1d.__eq__cCst|tƒstS| |¡ Sr*)rTr
rÊrËrºr(r(r)Ú__ne__ts
z poly1d.__ne__cCsB|j|}||jkr"|jj d¡S|dkr8|jj d¡S|j|S)Nr )ror–r1r=)r•rZÚindr(r(r)Ú __getitem__zs 
 
zpoly1d.__getitem__cCs^|j|}|dkrtdƒ‚||jkrPt ||j|jj¡}t ||jf¡|_d}||j|<dS)Nr z!Does not support negative powers.)ror9r:rNr–r1rUr“)r•ÚkeyrZrÍr}r(r(r)Ú __setitem__‚s
 
 
zpoly1d.__setitem__cCs
t|jƒSr*)Úiterr–r”r(r(r)Ú__iter__szpoly1d.__iter__r.r cCstt|j||dƒS)zÒ
        Return an antiderivative (indefinite integral) of this polynomial.
 
        Refer to `polyint` for full documentation.
 
        See Also
        --------
        polyint : equivalent function
 
        )rQrR)r
rr–)r•rQrRr(r(r)Úintegs z poly1d.integcCstt|j|dƒS)z·
        Return a derivative of this polynomial.
 
        Refer to `polyder` for full documentation.
 
        See Also
        --------
        polyder : equivalent function
 
        )rQ)r
rr–)r•rQr(r(r)Úderivs z poly1d.deriv)FN)N)r.r )r.)+r$r%r&r'Ú__hash__Úpropertyr–Úsetterršrorr“r„rsZcoefZ coefficientsÚor¢r¤r¨r©rµr¶r·r¸r¼r½r¿rÀrÃrÅrÆrÈrVrÉÚ __rtruediv__rËrÌrÎrÐrÒrÓrÔr(r(r(r)r
4sZi
 
 
 
 
 
 
 
 
9 
Úalways)NN)r.N)N)r.)NNNN)NFNF)r†)>r'Ú__all__Ú    functoolsÚrerlZnumpy.core.numericÚcoreÚnumericr:Z
numpy.corerrrrrrrrrZnumpy.core.overridesrZnumpy.lib.twodim_baserrZnumpy.lib.function_baserZnumpy.lib.type_checkrrrrZ numpy.linalgrr r!ÚpartialZarray_function_dispatchÚ UserWarningr r,rrIrrSrr[rrbr rvr    rzrrrr€rÚcompilerŠr’r
Ú simplefilterr(r(r(r)Ú<module>s|
þ(   ÿ 
z
[
 d
 K
 x
N
C
5
>
G
 
y