§
    OŠtj%  ã                   ó<   — d Z ddlmZ ddlmZ ddlmZ d„ Zd	d„ZdS )
aB  
Convergence acceleration / extrapolation methods for series and
sequences.

References:
Carl M. Bender & Steven A. Orszag, "Advanced Mathematical Methods for
Scientists and Engineers: Asymptotic Methods and Perturbation Theory",
Springer 1999. (Shanks transformation: pp. 368-375, Richardson
extrapolation: pp. 375-377.)
é    )ÚInteger)ÚS)Ú	factorialc           	      ó>  — t           j        }t          d|dz   ¦  «        D ]|}||                      |t	          ||z   ¦  «        ¦  «                             ¦   «         ||z   |z  z  t           j        ||z   z  z  t          |¦  «        t          ||z
  ¦  «        z  z  z  }Œ}|S )aÃ  
    Calculate an approximation for lim k->oo A(k) using Richardson
    extrapolation with the terms A(n), A(n+1), ..., A(n+N+1).
    Choosing N ~= 2*n often gives good results.

    Examples
    ========

    A simple example is to calculate exp(1) using the limit definition.
    This limit converges slowly; n = 100 only produces two accurate
    digits:

        >>> from sympy.abc import n
        >>> e = (1 + 1/n)**n
        >>> print(round(e.subs(n, 100).evalf(), 10))
        2.7048138294

    Richardson extrapolation with 11 appropriately chosen terms gives
    a value that is accurate to the indicated precision:

        >>> from sympy import E
        >>> from sympy.series.acceleration import richardson
        >>> print(round(richardson(e, n, 10, 20).evalf(), 10))
        2.7182818285
        >>> print(round(E.evalf(), 10))
        2.7182818285

    Another useful application is to speed up convergence of series.
    Computing 100 terms of the zeta(2) series 1/k**2 yields only
    two accurate digits:

        >>> from sympy.abc import k, n
        >>> from sympy import Sum
        >>> A = Sum(k**-2, (k, 1, n))
        >>> print(round(A.subs(n, 100).evalf(), 10))
        1.6349839002

    Richardson extrapolation performs much better:

        >>> from sympy import pi
        >>> print(round(richardson(A, n, 10, 20).evalf(), 10))
        1.6449340668
        >>> print(round(((pi**2)/6).evalf(), 10))     # Exact value
        1.6449340668

    r   é   )r   ÚZeroÚrangeÚsubsr   ÚdoitÚNegativeOner   )ÚAÚkÚnÚNÚsÚjs         úW/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/series/acceleration.pyÚ
richardsonr      s¡   € õ^ 	
Œ€AÝ�1�a˜!‘e‰_Œ_ð Jð JˆØ	ˆa�fŠf�Q�  A¡™œÑ'Ô'×,Ò,Ñ.Ô.°!°a±%¸!±Ñ;ÝŒm˜a !™eÑ$ñ%Ý(1°!©¬µyÀÀQÁÑ7GÔ7GÑ(GñIñ 	Jˆˆà€Hó    r   c                 óx  ‡ ‡— ˆ ˆfd„t          ||z   dz   ¦  «        D ¦   «         }|                     ¦   «         }t          d|dz   ¦  «        D ]g}t          |||z   dz   ¦  «        D ]:}||dz
           ||         ||dz            }
}	}|
|z  |	dz  z
  |
|z   d|	z  z
  z  ||<   Œ;|                     ¦   «         }Œh||         S )a7  
    Calculate an approximation for lim k->oo A(k) using the n-term Shanks
    transformation S(A)(n). With m > 1, calculate the m-fold recursive
    Shanks transformation S(S(...S(A)...))(n).

    The Shanks transformation is useful for summing Taylor series that
    converge slowly near a pole or singularity, e.g. for log(2):

        >>> from sympy.abc import k, n
        >>> from sympy import Sum, Integer
        >>> from sympy.series.acceleration import shanks
        >>> A = Sum(Integer(-1)**(k+1) / k, (k, 1, n))
        >>> print(round(A.subs(n, 100).doit().evalf(), 10))
        0.6881721793
        >>> print(round(shanks(A, n, 25).evalf(), 10))
        0.6931396564
        >>> print(round(shanks(A, n, 25, 5).evalf(), 10))
        0.6931471806

    The correct value is 0.6931471805599453094172321215.
    c                 óz   •— g | ]7}‰                      ‰t          |¦  «        ¦  «                             ¦   «         ‘Œ8S © )r
   r   r   )Ú.0r   r   r   s     €€r   ú
<listcomp>zshanks.<locals>.<listcomp>]   s9   ø€ ÐDÐDÐD¨aˆQ�VŠV�A•w˜q‘z”zÑ"Ô"×'Ò'Ñ)Ô)ÐDÐDÐDr   é   r   )r	   Úcopy)r   r   r   ÚmÚtableÚtable2Úir   ÚxÚyÚzs   ``         r   Úshanksr$   G   sÝ   øø€ ð, EÐDÐDÐDÐDµ5¸¸Q¹À¹Ñ3CÔ3CÐDÑDÔD€EØ�ZŠZ‰\Œ\€Få�1�a˜!‘e‰_Œ_ð ð ˆÝ�q˜!˜a™% !™)Ñ$Ô$ð 	5ð 	5ˆAØ˜A ™E”l E¨!¤H¨e°A¸±E¬l�!ˆqˆAØ˜1™˜q !™t™¨¨A©°°!±©Ñ4ˆF�1‰IˆIØ—’‘”ˆˆØ�Œ8€Or   N)r   )	Ú__doc__Úsympy.core.numbersr   Úsympy.core.singletonr   Ú(sympy.functions.combinatorial.factorialsr   r   r$   r   r   r   ú<module>r)      su   ðð	ð 	ð 'Ð &Ð &Ð &Ð &Ð &Ø "Ð "Ð "Ð "Ð "Ð "Ø >Ð >Ð >Ð >Ð >Ð >ð3ð 3ð 3ðlð ð ð ð ð r   