
    [hq"                     `    S SK Jr  SSKJr  S rS
S jr\SS j5       r\S 5       r\S 5       rg	)   )xrange   )defunc                    U R                  S5      nU R                  S5      n[        SUS-   5       HM  nU R                  X-
  U-  5      n	Xa" XU-
  -  U-  X2-   U-  -   5      U R                  XHU-
  -  U-  5      -  -  nMO     SU-  U-  $ )N          ?r   r   )mpfrangecospi)
ctxfabjNshkts
             U/var/www/auris/envauris/lib/python3.13/site-packages/mpmath/calculus/approximation.py	chebcoeffr      s    
AA1ac]IIqsAg	QqA#wqyAC7"#ciiQ3	&:::  Q3q5L    c              #      #    S/nUv   X!/n Uv   S/U Vs/ s H  nSU-  U-  PM     sn-   n[        U5       H  u  pxXg==   SU-  U-  -  ss'   M     [        U5       H  u  pxXg==   U-  ss'   M     Xdp4Mk  s  snf 7f)Nr   r   r   )	enumerate)	r   r   r   TbTar   Tmpics	            r   chebTr       s     
B
H
B
cB'BqQqSUB''bMDA36QqSU?6MbMDA36Q;6MB  (s   A>A9AA>c                 t   U R                  U5      u  pVU R                  n U[        US-  5      -   S-   U l        [        U5       Vs/ s H  n[	        XXVX5      PM     n	nU R
                  /U-  n
U	S   * S-  U
S'   U R                  S5      n[        X R                  S5      Xe-
  -  U R                  S5      Xe-   -  Xe-
  -  5      n[        [        U5      U5       H3  u  p[        [        U5      5       H  nX==   X   X   -  -  ss'   M     M5     U
SSS2   n
U R
                  n[        U5       Hb  nU R                  U R                  U-  U-  5      Xe-
  -  U-  Xe-   U-  -   n[        U[        U" U5      U R                  U
U5      -
  5      5      nMd     Xpl        U(       a  X74$ U
$ s  snf ! Xpl        f = f)a8  
Computes a polynomial of degree `N-1` that approximates the
given function `f` on the interval `[a, b]`. With ``error=True``,
:func:`~mpmath.chebyfit` also returns an accurate estimate of the
maximum absolute error; that is, the maximum value of
`|f(x) - P(x)|` for `x \in [a, b]`.

:func:`~mpmath.chebyfit` uses the Chebyshev approximation formula,
which gives a nearly optimal solution: that is, the maximum
error of the approximating polynomial is very close to
the smallest possible for any polynomial of the same degree.

Chebyshev approximation is very useful if one needs repeated
evaluation of an expensive function, such as function defined
implicitly by an integral or a differential equation. (For
example, it could be used to turn a slow mpmath function
into a fast machine-precision version of the same.)

**Examples**

Here we use :func:`~mpmath.chebyfit` to generate a low-degree approximation
of `f(x) = \cos(x)`, valid on the interval `[1, 2]`::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = True
    >>> poly, err = chebyfit(cos, [1, 2], 5, error=True)
    >>> nprint(poly)
    [0.00291682, 0.146166, -0.732491, 0.174141, 0.949553]
    >>> nprint(err, 12)
    1.61351758081e-5

The polynomial can be evaluated using ``polyval``::

    >>> nprint(polyval(poly, 1.6), 12)
    -0.0291858904138
    >>> nprint(cos(1.6), 12)
    -0.0291995223013

Sampling the true error at 1000 points shows that the error
estimate generated by ``chebyfit`` is remarkably good::

    >>> error = lambda x: abs(cos(x) - polyval(poly, x))
    >>> nprint(max([error(1+n/1000.) for n in range(1000)]), 12)
    1.61349954245e-5

**Choice of degree**

The degree `N` can be set arbitrarily high, to obtain an
arbitrarily good approximation. As a rule of thumb, an
`N`-term Chebyshev approximation is good to `N/(b-a)` decimal
places on a unit interval (although this depends on how
well-behaved `f` is). The cost grows accordingly: ``chebyfit``
evaluates the function `(N^2)/2` times to compute the
coefficients and an additional `N` times to estimate the error.

**Possible issues**

One should be careful to use a sufficiently high working
precision both when calling ``chebyfit`` and when evaluating
the resulting polynomial, as the polynomial is sometimes
ill-conditioned. It is for example difficult to reach
15-digit accuracy when evaluating the polynomial using
machine precision floats, no matter the theoretical
accuracy of the polynomial. (The option to return the
coefficients in Chebyshev form should be made available
in the future.)

It is important to note the Chebyshev approximation works
poorly if `f` is not smooth. A function containing singularities,
rapid oscillation, etc can be approximated more effectively by
multiplying it by a weight function that cancels out the
nonsmooth features, or by dividing the interval into several
segments.
r      r   r   N)
_as_pointsprecintr
   r   zeror	   r    ziplencospimaxabspolyval)r   r   intervalr   errorr   r   origr   r   dr   TTkr   errxs                    r   chebyfitr7   &   s   X >>(#DA88D#af+%*/4Qx8x!YsQ%x8XXJN!uQw!GGCL#wwqz13'ac):AC)@A58Q'GQ3r7^RU
" $ ( ddGhhqAq
#qs+A-a7Ac3qtckk!Q&7789C  $w' 9 s   &F/ F*D9F/ *F/ /F7c                   ^ ^^ T R                  U5      nUS   nUS   nXT-
  n/ n/ nT R                  S-  n	[        US-   5       H  n
SU
-  T R                  -  U-  mST R	                  U UU4S jU5      -  U-  nST R	                  U UU4S jU5      -  U-  nU
S:X  a  US-  n[        U5      U	:  a  T R                  n[        U5      U	:  a  T R                  nUR                  U5        UR                  U5        M     Xx4$ )ac  
Computes the Fourier series of degree `N` of the given function
on the interval `[a, b]`. More precisely, :func:`~mpmath.fourier` returns
two lists `(c, s)` of coefficients (the cosine series and sine
series, respectively), such that

.. math ::

    f(x) \sim \sum_{k=0}^N
        c_k \cos(k m x) + s_k \sin(k m x)

where `m = 2 \pi / (b-a)`.

Note that many texts define the first coefficient as `2 c_0` instead
of `c_0`. The easiest way to evaluate the computed series correctly
is to pass it to :func:`~mpmath.fourierval`.

**Examples**

The function `f(x) = x` has a simple Fourier series on the standard
interval `[-\pi, \pi]`. The cosine coefficients are all zero (because
the function has odd symmetry), and the sine coefficients are
rational numbers::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = True
    >>> c, s = fourier(lambda x: x, [-pi, pi], 5)
    >>> nprint(c)
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    >>> nprint(s)
    [0.0, 2.0, -1.0, 0.666667, -0.5, 0.4]

This computes a Fourier series of a nonsymmetric function on
a nonstandard interval::

    >>> I = [-1, 1.5]
    >>> f = lambda x: x**2 - 4*x + 1
    >>> cs = fourier(f, I, 4)
    >>> nprint(cs[0])
    [0.583333, 1.12479, -1.27552, 0.904708, -0.441296]
    >>> nprint(cs[1])
    [0.0, -2.6255, 0.580905, 0.219974, -0.540057]

It is instructive to plot a function along with its truncated
Fourier series::

    >>> plot([f, lambda x: fourierval(cs, I, x)], I) #doctest: +SKIP

Fourier series generally converge slowly (and may not converge
pointwise). For example, if `f(x) = \cosh(x)`, a 10-term Fourier
series gives an `L^2` error corresponding to 2-digit accuracy::

    >>> I = [-1, 1]
    >>> cs = fourier(cosh, I, 9)
    >>> g = lambda x: (cosh(x) - fourierval(cs, I, x))**2
    >>> nprint(sqrt(quad(g, I)))
    0.00467963

:func:`~mpmath.fourier` uses numerical quadrature. For nonsmooth functions,
the accuracy (and speed) can be improved by including all singular
points in the interval specification::

    >>> nprint(fourier(abs, [-1, 1], 0), 10)
    ([0.5000441648], [0.0])
    >>> nprint(fourier(abs, [-1, 0, 1], 0), 10)
    ([0.5], [0.0])

r   r#   
   r   r   c                 >   > T" U 5      TR                  TU -  5      -  $ Nr*   r   r   r   ms    r   <lambda>fourier.<locals>.<lambda>       AaD1$5r   c                 >   > T" U 5      TR                  TU -  5      -  $ r;   sinr=   s    r   r?   r@      rA   r   )r$   epsr   r+   quadglr-   r'   append)r   r   r/   r   r   r   L
cos_series
sin_seriescutoffnanbnr>   s   ``           @r   fourierrO      s    L ~~h'HAA	AJJWWRZFAaC[aCJqLszz5x@@Bszz5x@@B6!GBr7V#((Rr7V#((R""  !!r   c           	      j  ^ ^^^	^
 Uu  mm
T R                  U5      nUS   nUS   nST R                  -  US   US   -
  -  m	T R                  nUT R                  UU U	U4S j[	        [        T5      5       5       5      -  nUT R                  U U	U
U4S j[	        [        T
5      5       5       5      -  nU$ )a
  
Evaluates a Fourier series (in the format computed by
by :func:`~mpmath.fourier` for the given interval) at the point `x`.

The series should be a pair `(c, s)` where `c` is the
cosine series and `s` is the sine series. The two lists
need not have the same length.
r   r#   r   c              3   v   >#    U  H.  nTU   (       d  M  TU   TR                  TU-  T-  5      -  v   M0     g 7fr;   r<   ).0rL   csr   r>   r6   s     r   	<genexpr>fourierval.<locals>.<genexpr>   3     I12a5&"Q%!A&   9$9c              3   v   >#    U  H.  nTU   (       d  M  TU   TR                  TU-  T-  5      -  v   M0     g 7fr;   rC   )rR   rL   r   r>   ssr6   s     r   rT   rU      rV   rW   )r$   r+   r'   fsumr   r)   )r   seriesr/   r6   abr   r   r   rS   r>   rY   s   `  `    @@@r   
fouriervalr]      s     FB		!BAA	#&&"R&A,AAIs2wI	IIAIs2wI	IIAHr   N)r   r   )F)	libmp.backendr   calculusr   r   r    r7   rO   r]    r   r   <module>ra      sW    " 
 b bH V" V"p  r   