§
    JŠtjO  ã                   ó>  — d dl mZ ddlmZ 	 ej        Zn# e$ r
 ej        ZY nw xY wed„ ¦   «         Zd„ Z	edd„¦   «         Z
d„ Zedd	„¦   «         Zd
„ Zed„ ¦   «         Zi fd„Zed„ ¦   «         Zedd„¦   «         Zedd„¦   «         Zed„ ¦   «         Zed„ ¦   «         ZdS )é   )Úxrangeé   )Údefunc                 ó¤   — t          |¦  «        }| j        }d|dz  z  }t          |dz   ¦  «        D ]}||||         z  z  }|||z
  z  |dz   z  }Œ|S )zÄ
    Given a sequence `(s_k)` containing at least `n+1` items, returns the
    `n`-th forward difference,

    .. math ::

        \Delta^n = \sum_{k=0}^{\infty} (-1)^{k+n} {n \choose k} s_k.
    éÿÿÿÿr   )ÚintÚzeror   )ÚctxÚsÚnÚdÚbÚks         ú]/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/mpmath/calculus/differentiation.pyÚ
differencer      sj   € õ 	ˆA‰Œ€AØŒ€AØ	��Q‘‰€AÝ�A�a‘C‰[Œ[ð !ð !ˆØ	ˆQ��1”‰X‰ˆØ�!�A‘#‰Y˜A˜a™CÑ ˆˆØ€Hó    c                 óî  ‡‡‡— |                      d¦  «        }|                      dd¦  «        }|                      dd¦  «        }|d|z  z   |dz   z  }	| j        }
	 |	| _        |                      d¦  «        Š‰€X|                      d	¦  «        r#t          |                      ‰¦  «        ¦  «        }nd}|                      d| |z
  |z
  ¦  «        Šn|                      ‰¦  «        Š|                      dd¦  «        }|r-‰|                      |¦  «        z  Št          |dz   ¦  «        }‰}nt          | |dz   d¦  «        }d‰z  }|r‰d
‰z  z  Šˆˆˆfd„|D ¦   «         }|||	f|
| _        S # |
| _        w xY w)NÚsingularÚaddprecé
   Ú	directioné    r   r   ÚhÚrelativeg      à?c                 ó2   •— g | ]} ‰‰|‰z  z   ¦  «        ‘ŒS © r   )Ú.0r   Úfr   Úxs     €€€r   ú
<listcomp>zhsteps.<locals>.<listcomp>=   s)   ø€ Ð*Ð*Ð*˜q�!�!�A�a˜‘c‘E‘(”(Ð*Ð*Ð*r   )ÚgetÚprecr   ÚmagÚldexpÚconvertÚsignr   )r
   r   r   r   r"   Úoptionsr   r   r   ÚworkprecÚorigÚ	hextramagÚstepsÚnormÚvaluesr   s    ``            @r   Úhstepsr.      s™  øøø€ Ø�{Š{˜:Ñ&Ô&€HØ�kŠk˜) RÑ(Ô(€GØ—’˜K¨Ñ+Ô+€IØ�Q�w‘Y‘ 1 Q¡3Ñ'€HØŒ8€DðØˆŒØ�KŠK˜ÑÔˆØˆ9Ø�{Š{˜:Ñ&Ô&ð Ý §¢¨¡
¤
™OœO�	�	à�	Ø—	’	˜!˜d˜U 7™]¨9Ñ4Ñ5Ô5ˆAˆAà—’˜A‘”ˆAà—K’K ¨QÑ/Ô/ˆ	Øð 	Ø�—’˜)Ñ$Ô$Ñ$ˆAÝ˜1˜Q™3‘K”KˆEØˆDˆDõ ˜A˜2˜q ™s AÑ&Ô&ˆEØ�a‘CˆDàð 	Ø��Q‘‰JˆAØ*Ð*Ð*Ð*Ð*Ð* EÐ*Ñ*Ô*ˆØ�t˜XÐ%àˆŒˆø�4ˆŒˆˆˆˆs   ÁDE+ Å+	E4c                 ó\  ‡ ‡‡‡‡— d}	 t          ‰¦  «        }t          ‰¦  «        Šd}n# t          $ r Y nw xY w|r!ˆ fd„‰D ¦   «         Št          ‰ ‰‰||¦  «        S |                     dd¦  «        }‰dk    r9|dk    r3|                     d¦  «        s ‰‰                      ‰¦  «        ¦  «        S ‰ j        }	 |dk    r9t          ‰ ‰‰‰|fi |¤Ž\  }	}
}|‰ _        ‰                      |	‰¦  «        |
‰z  z  }nž|dk    r†‰ xj        d	z  c_        ‰                      |                     d
d¦  «        ¦  «        Šˆ ˆˆˆˆfd„}‰                      |dd‰ j	        z  g¦  «        }|‰  
                    ‰¦  «        z  d‰ j	        z  z  }nt          d|z  ¦  «        ‚|‰ _        n# |‰ _        w xY w|
 S )aÌ  
    Numerically computes the derivative of `f`, `f'(x)`, or generally for
    an integer `n \ge 0`, the `n`-th derivative `f^{(n)}(x)`.
    A few basic examples are::

        >>> from mpmath import *
        >>> mp.dps = 15; mp.pretty = True
        >>> diff(lambda x: x**2 + x, 1.0)
        3.0
        >>> diff(lambda x: x**2 + x, 1.0, 2)
        2.0
        >>> diff(lambda x: x**2 + x, 1.0, 3)
        0.0
        >>> nprint([diff(exp, 3, n) for n in range(5)])   # exp'(x) = exp(x)
        [20.0855, 20.0855, 20.0855, 20.0855, 20.0855]

    Even more generally, given a tuple of arguments `(x_1, \ldots, x_k)`
    and order `(n_1, \ldots, n_k)`, the partial derivative
    `f^{(n_1,\ldots,n_k)}(x_1,\ldots,x_k)` is evaluated. For example::

        >>> diff(lambda x,y: 3*x*y + 2*y - x, (0.25, 0.5), (0,1))
        2.75
        >>> diff(lambda x,y: 3*x*y + 2*y - x, (0.25, 0.5), (1,1))
        3.0

    **Options**

    The following optional keyword arguments are recognized:

    ``method``
        Supported methods are ``'step'`` or ``'quad'``: derivatives may be
        computed using either a finite difference with a small step
        size `h` (default), or numerical quadrature.
    ``direction``
        Direction of finite difference: can be -1 for a left
        difference, 0 for a central difference (default), or +1
        for a right difference; more generally can be any complex number.
    ``addprec``
        Extra precision for `h` used to account for the function's
        sensitivity to perturbations (default = 10).
    ``relative``
        Choose `h` relative to the magnitude of `x`, rather than an
        absolute value; useful for large or tiny `x` (default = False).
    ``h``
        As an alternative to ``addprec`` and ``relative``, manually
        select the step size `h`.
    ``singular``
        If True, evaluation exactly at the point `x` is avoided; this is
        useful for differentiating functions with removable singularities.
        Default = False.
    ``radius``
        Radius of integration contour (with ``method = 'quad'``).
        Default = 0.25. A larger radius typically is faster and more
        accurate, but it must be chosen so that `f` has no
        singularities within the radius from the evaluation point.

    A finite difference requires `n+1` function evaluations and must be
    performed at `(n+1)` times the target precision. Accordingly, `f` must
    support fast evaluation at high precision.

    With integration, a larger number of function evaluations is
    required, but not much extra precision is required. For high order
    derivatives, this method may thus be faster if f is very expensive to
    evaluate at high precision.

    **Further examples**

    The direction option is useful for computing left- or right-sided
    derivatives of nonsmooth functions::

        >>> diff(abs, 0, direction=0)
        0.0
        >>> diff(abs, 0, direction=1)
        1.0
        >>> diff(abs, 0, direction=-1)
        -1.0

    More generally, if the direction is nonzero, a right difference
    is computed where the step size is multiplied by sign(direction).
    For example, with direction=+j, the derivative from the positive
    imaginary direction will be computed::

        >>> diff(abs, 0, direction=j)
        (0.0 - 1.0j)

    With integration, the result may have a small imaginary part
    even even if the result is purely real::

        >>> diff(sqrt, 1, method='quad')    # doctest:+ELLIPSIS
        (0.5 - 4.59...e-26j)
        >>> chop(_)
        0.5

    Adding precision to obtain an accurate value::

        >>> diff(cos, 1e-30)
        0.0
        >>> diff(cos, 1e-30, h=0.0001)
        -9.99999998328279e-31
        >>> diff(cos, 1e-30, addprec=100)
        -1.0e-30

    FTc                 ó:   •— g | ]}‰                      |¦  «        ‘ŒS r   )r%   )r   Ú_r
   s     €r   r    zdiff.<locals>.<listcomp>´   s#   ø€ Ð'Ð'Ð' ˆS�[Š[˜‰^Œ^Ð'Ð'Ð'r   ÚmethodÚstepr   Úquadr   r   Úradiusg      Ð?c                 ó`   •— ‰‰                      | ¦  «        z  }‰|z   } ‰|¦  «        |‰z  z  S ©N)Úexpj)ÚtÚreiÚzr
   r   r   r5   r   s      €€€€€r   Úgzdiff.<locals>.gÂ   s6   ø€ Ø˜SŸXšX a™[œ[Ñ(�Ø˜‘G�Ø�q˜‘t”t˜c 1™f‘}Ð$r   r   zunknown method: %r)ÚlistÚ	TypeErrorÚ_partial_diffr!   r%   r"   r.   r   ÚquadtsÚpiÚ	factorialÚ
ValueError)r
   r   r   r   r'   ÚpartialÚordersr2   r"   r-   r,   r(   Úvr<   r   r5   s   ````           @r   ÚdiffrG   C   s  øøøøø€ ðR €GðÝ�a‘”ˆÝ�‰GŒGˆØˆˆøÝð ð ð Øˆðøøøàð 9Ø'Ð'Ð'Ð' QÐ'Ñ'Ô'ˆÝ˜S ! Q¨°Ñ8Ô8Ð8Ø�[Š[˜ 6Ñ*Ô*€FØˆA‚v€v�&˜FÒ"Ð"¨7¯;ª;°zÑ+BÔ+BÐ"Øˆq�—’˜Q‘”Ñ Ô Ð ØŒ8€DðØ�VÒÐÝ%+¨C°°A°q¸$Ð%JÐ%JÀ'Ð%JÐ%JÑ"ˆF�D˜(ØˆCŒHØ—’˜v qÑ)Ô)¨D°!©GÑ3ˆAˆAØ�vÒÐØˆHŒH˜‰NˆHŒHØ—[’[ §¢¨X°tÑ!<Ô!<Ñ=Ô=ˆFð%ð %ð %ð %ð %ð %ð %ð %ð %ð —
’
˜1˜q ! C¤F¡(˜mÑ,Ô,ˆAØ�C—M’M !Ñ$Ô$Ñ$¨¨#¬&©Ñ1ˆAˆAåÐ1°FÑ:Ñ;Ô;Ð;àˆŒˆø�4ˆŒˆˆˆˆØˆ2€Is   ‰ * ª
7¶7Â:CF Æ	F(c                 óô   ‡ ‡‡‡‡— |s
 ‰¦   «         S t          |¦  «        s ‰|Ž S dŠt          t          |¦  «        ¦  «        D ]Š|‰         r nŒ|‰         Šˆ ˆˆˆˆfd„}d|‰<   t          ‰ |||‰¦  «        S )Nr   c                  ó@   •‡ — ˆˆ ˆfd„} ‰j         |‰ ‰         ‰fi ‰¤ŽS )Nc                 óB   •—  ‰‰d ‰…         | fz   ‰‰dz   d …         z   Ž S ©Nr   r   )r9   r   Úf_argsÚis    €€€r   Úinnerz1_partial_diff.<locals>.fdiff_inner.<locals>.innerÙ   s0   ø€ Ø�1�v˜b˜q˜b”z Q DÑ(¨6°!°A±#°$°$¬<Ñ7Ð9Ð9r   ©rG   )rL   rN   r
   r   rM   r'   Úorders   ` €€€€€r   Úfdiff_innerz"_partial_diff.<locals>.fdiff_innerØ   sI   øø€ ð	:ð 	:ð 	:ð 	:ð 	:ð 	:ð 	:àˆsŒx˜˜v aœy¨%Ð;Ð;°7Ð;Ð;Ð;r   )ÚsumÚrangeÚlenr?   )r
   r   ÚxsrE   r'   rQ   rM   rP   s   ``  ` @@r   r?   r?   Î   sÂ   øøøøø€ Øð Øˆq‰sŒsˆ
Ýˆv‰;Œ;ð Øˆq�"ˆvˆØ	€AÝ•3�v‘;”;ÑÔð ð ˆØ�!Œ9ð 	ØˆEð	à�1ŒI€Eð<ð <ð <ð <ð <ð <ð <ð <ð <ð €Fˆ1�IÝ˜˜k¨2¨v°wÑ?Ô?Ð?r   Nc              +   óè  K  — |€| j         }nt          |¦  «        }|                     dd¦  «        dk    r-d}||dz   k     r  | j        |||fi |¤ŽV — |dz  }||dz   k     ° dS |                     d¦  «        }|r|                      ||dd¬¦  «        V — n  ||                      |¦  «        ¦  «        V — |dk     rdS || j         k    rd	\  }}nd|dz   }}	 | j        }	t          | ||||	fi |¤Ž\  }
}}t          ||¦  «        D ]H}	 || _        |                      |
|¦  «        ||z  z  }|	| _        n# |	| _        w xY w|
 V — ||k    r dS ŒI|t          |d
z  dz   ¦  «        }}t          ||¦  «        }Œ�)ad  
    Returns a generator that yields the sequence of derivatives

    .. math ::

        f(x), f'(x), f''(x), \ldots, f^{(k)}(x), \ldots

    With ``method='step'``, :func:`~mpmath.diffs` uses only `O(k)`
    function evaluations to generate the first `k` derivatives,
    rather than the roughly `O(k^2)` evaluations
    required if one calls :func:`~mpmath.diff` `k` separate times.

    With `n < \infty`, the generator stops as soon as the
    `n`-th derivative has been generated. If the exact number of
    needed derivatives is known in advance, this is further
    slightly more efficient.

    Options are the same as for :func:`~mpmath.diff`.

    **Examples**

        >>> from mpmath import *
        >>> mp.dps = 15
        >>> nprint(list(diffs(cos, 1, 5)))
        [0.540302, -0.841471, -0.540302, 0.841471, 0.540302, -0.841471]
        >>> for i, d in zip(range(6), diffs(cos, 1)):
        ...     print("%s %s" % (i, d))
        ...
        0 0.54030230586814
        1 -0.841470984807897
        2 -0.54030230586814
        3 0.841470984807897
        4 0.54030230586814
        5 -0.841470984807897

    Nr2   r3   r   r   r   T)r   )r   r   gffffffö?)
Úinfr   r!   rG   r%   r"   r.   r   r   Úmin)r
   r   r   r   r'   r   r   ÚAÚBÚcallprecÚyr,   r(   r   s                 r   Údiffsr]   ß   sñ  è è € ðL 	€yØŒGˆˆå�‰FŒFˆØ‡{‚{�8˜VÑ$Ô$¨Ò.Ð.ØˆØ�!�a‘%ŠiˆiØ�#”(˜1˜a Ð.Ð. gÐ.Ð.Ð.Ð.Ð.Ø�‰FˆAð �!�a‘%Šiˆið 	ˆØ�{Š{˜:Ñ&Ô&€HØð  Ø�hŠh�q˜!˜Q¨ˆhÑ.Ô.Ð.Ð.Ð.Ð.àˆa�—’˜A‘”ÑÔÐÐÐØˆ1‚u€uØˆØˆCŒG‚|€|Ø‰ˆˆ1ˆ1à�!�A‘#ˆ1ˆðØ”8ˆÝ" 3¨¨1¨a°ÐEÐE¸WÐEÐEÑˆˆ4�Ý˜˜1‘”ð 	ð 	ˆAð$Ø#�”Ø—N’N 1 aÑ(Ô(¨4°©7Ñ2�à#�”�ø˜8�”Ð#Ð#Ð#Ð#Ø�"ˆHˆHˆHØ�AŠvˆvØ��ð à•#�a˜‘e˜A‘g‘,”,ˆ1ˆÝ��1‰IŒIˆðs   Ä#D1Ä1	D:c                 ó8   ‡ ‡— t          ‰ ¦  «        Š g Šˆˆ fd„}|S )Nc                 óž   •— t          t          ‰¦  «        | dz   ¦  «        D ]$}‰                     t          ‰¦  «        ¦  «         Œ%‰|          S rK   )r   rT   ÚappendÚnext)r   rM   ÚdataÚgens     €€r   r   ziterable_to_function.<locals>.f,  sJ   ø€ Ý�˜D™	œ	 1 Q¡3Ñ'Ô'ð 	#ð 	#ˆAØ�KŠK�˜S™	œ	Ñ"Ô"Ð"Ð"Ø�AŒwˆr   )Úiter)rc   r   rb   s   ` @r   Úiterable_to_functionre   )  s9   øø€ Ý
ˆs‰)Œ)€CØ€Dðð ð ð ð ð ð €Hr   c              #   óÚ  K  — t          |¦  «        }|dk    r|d         D ]}|V — ŒdS t          |                      |d|dz  …         ¦  «        ¦  «        }t          |                      ||dz  d…         ¦  «        ¦  «        }d}	  ||¦  «         |d¦  «        z  }d}t          d|dz   ¦  «        D ]0}	|||	z
  dz   z  |	z  }|| |||	z
  ¦  «        z   ||	¦  «        z  z  }Œ1|V — |dz  }Œg)aV  
    Given a list of `N` iterables or generators yielding
    `f_k(x), f'_k(x), f''_k(x), \ldots` for `k = 1, \ldots, N`,
    generate `g(x), g'(x), g''(x), \ldots` where
    `g(x) = f_1(x) f_2(x) \cdots f_N(x)`.

    At high precision and for large orders, this is typically more efficient
    than numerical differentiation if the derivatives of each `f_k(x)`
    admit direct computation.

    Note: This function does not increase the working precision internally,
    so guard digits may have to be added externally for full accuracy.

    **Examples**

        >>> from mpmath import *
        >>> mp.dps = 15; mp.pretty = True
        >>> f = lambda x: exp(x)*cos(x)*sin(x)
        >>> u = diffs(f, 1)
        >>> v = mp.diffs_prod([diffs(exp,1), diffs(cos,1), diffs(sin,1)])
        >>> next(u); next(v)
        1.23586333600241
        1.23586333600241
        >>> next(u); next(v)
        0.104658952245596
        0.104658952245596
        >>> next(u); next(v)
        -5.96999877552086
        -5.96999877552086
        >>> next(u); next(v)
        -12.4632923122697
        -12.4632923122697

    r   r   Nr   )rT   re   Ú
diffs_prodr   )
r
   ÚfactorsÚNÚcÚurF   r   r   Úar   s
             r   rg   rg   2  s*  è è € õH 	ˆG‰Œ€AØˆA‚v€vØ˜”ð 	ð 	ˆAØˆGˆGˆGˆGð	ð 	õ ! §¢°¸¸¸A¹¸´Ñ!?Ô!?Ñ@Ô@ˆÝ  §¢°¸¸1¹¸¸´Ñ!?Ô!?Ñ@Ô@ˆØˆð	à��!‘”�q�q˜‘t”t‘ˆAØˆAÝ˜A˜a ™c‘]”]ð 'ð '�Ø˜˜1™˜Q™‘K 1Ñ$�Ø�Q˜˜˜1˜Q™3™œ‘Z ! ! A¡$¤$Ñ&Ñ&��ØˆGˆGˆGØ�‰FˆAð	r   c                 óV  — | |v r||          S |sddi|d<   t          | dz
  ¦  «        }t          d„ t          |¦  «        D ¦   «         ¦  «        }i }t          |¦  «        D ]6\  }}|d         dz   f|dd…         z   }||v r||xx         |z  cc<   Œ1|||<   Œ7t          |¦  «        D ]x\  }}t          |¦  «        sŒt	          |¦  «        D ]S\  }}|rL|d|…         |dz
  ||dz            dz   fz   ||dz   d…         z   }	|	|v r||	xx         ||z  z  cc<   ŒK||z  ||	<   ŒTŒy||| <   ||          S )z¤
    nth differentiation polynomial for exp (Faa di Bruno's formula).

    TODO: most exponents are zero, so maybe a sparse representation
    would be better.
    ©r   r   r   c              3   ó*   K  — | ]\  }}|d z   |fV — ŒdS )rn   Nr   )r   rj   rF   s      r   ú	<genexpr>zdpoly.<locals>.<genexpr>t  s.   è è € Ð2Ð2™E˜Q˜qˆa�‰f�QˆZÐ2Ð2Ð2Ð2Ð2Ð2r   Nr   )ÚdpolyÚdictÚ	iteritemsrR   Ú	enumerate)
r   Ú_cacheÚRÚRaÚpowersÚcountÚpowers1r   ÚpÚpowers2s
             r   rq   rq   h  s•  € ð 	ˆF€{€{Ø�aŒyÐØð Ø˜!�Hˆˆq‰	Ýˆa�‰c‰
Œ
€AÝÐ2Ð2¥Y¨q¡\¤\Ð2Ñ2Ô2Ñ2Ô2€AØ	€BÝ" 1™œð  ð  ‰ˆ�Ø˜!”9˜Q‘;�. 6¨!¨"¨"¤:Ñ-ˆØ�bˆ=ˆ=ØˆwˆKˆKŒK˜5Ñ ˆKˆK‰KˆKàˆBˆw‰KˆKÝ" 1™œð 	*ð 	*‰ˆ�Ý�6‰{Œ{ð 	ØÝ˜VÑ$Ô$ð 	*ð 	*‰CˆAˆaØð *Ø   ! œ*¨¨!©¨F°1°Q±3¬K¸©MÐ':Ñ:¸VÀAÀaÁCÀDÀD¼\ÑI�Ø˜b�=�=Ø�w�K�K”K 1 U¡7Ñ*�K�K‘K�Kà"# E¡'�B�w‘Køð	*ð €Fˆ1�IØ�!Œ9Ðr   c           	   #   ó`  ‡K  — t          |¦  «        Š|                       ‰d¦  «        ¦  «        }|V — d}	 |                      d¦  «        }t          t	          |¦  «        ¦  «        D ]9\  }}|||                      ˆfd„t          |¦  «        D ¦   «         ¦  «        z  z  }Œ:||z  V — |dz  }Œx)aÐ  
    Given an iterable or generator yielding `f(x), f'(x), f''(x), \ldots`
    generate `g(x), g'(x), g''(x), \ldots` where `g(x) = \exp(f(x))`.

    At high precision and for large orders, this is typically more efficient
    than numerical differentiation if the derivatives of `f(x)`
    admit direct computation.

    Note: This function does not increase the working precision internally,
    so guard digits may have to be added externally for full accuracy.

    **Examples**

    The derivatives of the gamma function can be computed using
    logarithmic differentiation::

        >>> from mpmath import *
        >>> mp.dps = 15; mp.pretty = True
        >>>
        >>> def diffs_loggamma(x):
        ...     yield loggamma(x)
        ...     i = 0
        ...     while 1:
        ...         yield psi(i,x)
        ...         i += 1
        ...
        >>> u = diffs_exp(diffs_loggamma(3))
        >>> v = diffs(gamma, 3)
        >>> next(u); next(v)
        2.0
        2.0
        >>> next(u); next(v)
        1.84556867019693
        1.84556867019693
        >>> next(u); next(v)
        2.49292999190269
        2.49292999190269
        >>> next(u); next(v)
        3.44996501352367
        3.44996501352367

    r   r   c              3   óD   •K  — | ]\  }}|¯ ‰|d z   ¦  «        |z  V — ŒdS )r   Nr   )r   r   r{   Úfns      €r   rp   zdiffs_exp.<locals>.<genexpr>¼  s<   øè è € ÐLÐL©E¨Q¨qÈ!ÐL˜R˜R  !¡™WœW a™ZÐLÐLÐLÐLÐLÐLr   )re   ÚexpÚmpfrs   rq   Úfprodrt   )r
   ÚfdiffsÚf0rM   r   rx   rj   r   s          @r   Ú	diffs_expr…   ‰  sÈ   øè è € õX 
˜fÑ	%Ô	%€BØ	�Š���A‘”‰Œ€BØ
€H€H€HØ	€AðØ�GŠG�A‰JŒJˆÝ"¥5¨¡8¤8Ñ,Ô,ð 	Mð 	M‰IˆF�AØ��3—9’9ÐLÐLÐLÐLµY¸vÑ5FÔ5FÐLÑLÔLÑLÔLÑLÑLˆAˆAØ�"‰fˆˆˆØ	ˆQ‰ˆðr   r   c           	      ó  ‡ ‡‡‡— t          t          ‰                      ‰                      |¦  «        ¦  «        ¦  «        dz   d¦  «        }||z
  dz
  Šˆ ˆˆˆfd„}‰                      |||¦  «        ‰                      ||z
  ¦  «        z  S )a…	  
    Calculates the Riemann-Liouville differintegral, or fractional
    derivative, defined by

    .. math ::

        \,_{x_0}{\mathbb{D}}^n_xf(x) = \frac{1}{\Gamma(m-n)} \frac{d^m}{dx^m}
        \int_{x_0}^{x}(x-t)^{m-n-1}f(t)dt

    where `f` is a given (presumably well-behaved) function,
    `x` is the evaluation point, `n` is the order, and `x_0` is
    the reference point of integration (`m` is an arbitrary
    parameter selected automatically).

    With `n = 1`, this is just the standard derivative `f'(x)`; with `n = 2`,
    the second derivative `f''(x)`, etc. With `n = -1`, it gives
    `\int_{x_0}^x f(t) dt`, with `n = -2`
    it gives `\int_{x_0}^x \left( \int_{x_0}^t f(u) du \right) dt`, etc.

    As `n` is permitted to be any number, this operator generalizes
    iterated differentiation and iterated integration to a single
    operator with a continuous order parameter.

    **Examples**

    There is an exact formula for the fractional derivative of a
    monomial `x^p`, which may be used as a reference. For example,
    the following gives a half-derivative (order 0.5)::

        >>> from mpmath import *
        >>> mp.dps = 15; mp.pretty = True
        >>> x = mpf(3); p = 2; n = 0.5
        >>> differint(lambda t: t**p, x, n)
        7.81764019044672
        >>> gamma(p+1)/gamma(p-n+1) * x**(p-n)
        7.81764019044672

    Another useful test function is the exponential function, whose
    integration / differentiation formula easy generalizes
    to arbitrary order. Here we first compute a third derivative,
    and then a triply nested integral. (The reference point `x_0`
    is set to `-\infty` to avoid nonzero endpoint terms.)::

        >>> differint(lambda x: exp(pi*x), -1.5, 3)
        0.278538406900792
        >>> exp(pi*-1.5) * pi**3
        0.278538406900792
        >>> differint(lambda x: exp(pi*x), 3.5, -3, -inf)
        1922.50563031149
        >>> exp(pi*3.5) / pi**3
        1922.50563031149

    However, for noninteger `n`, the differentiation formula for the
    exponential function must be modified to give the same result as the
    Riemann-Liouville differintegral::

        >>> x = mpf(3.5)
        >>> c = pi
        >>> n = 1+2*j
        >>> differint(lambda x: exp(c*x), x, n)
        (-123295.005390743 + 140955.117867654j)
        >>> x**(-n) * exp(c)**x * (x*c)**n * gammainc(-n, 0, x*c) / gamma(-n)
        (-123295.005390743 + 140955.117867654j)


    r   c                 ó@   •‡ — ‰                      ˆˆˆ fd„‰‰ g¦  «        S )Nc                 ó,   •— ‰| z
  ‰z   ‰| ¦  «        z  S r7   r   )r9   r   Úrr   s    €€€r   ú<lambda>z-differint.<locals>.<lambda>.<locals>.<lambda>  s   ø€  a¨¡c¨A¡X°°°!±´¡_€ r   )r4   )r   r
   r   r‰   Úx0s   `€€€€r   rŠ   zdifferint.<locals>.<lambda>  s*   øø€ �#—(’(Ð4Ð4Ð4Ð4Ð4Ð4°r¸1°gÑ>Ô>€ r   )Úmaxr   ÚceilÚrerG   Úgamma)r
   r   r   r   r‹   Úmr<   r‰   s   ``  `  @r   Ú	differintr‘   À  sˆ   øøøø€ õH 	�C�—’˜Ÿš ™œÑ#Ô#Ñ$Ô$ QÑ&¨Ñ*Ô*€AØ	ˆ!‰ˆA‰€AØ>Ð>Ð>Ð>Ð>Ð>Ð>€AØ�8Š8�A�q˜!ÑÔ˜sŸyšy¨¨1©™~œ~Ñ-Ð-r   c                 ó.   ‡ ‡‡‡— ‰dk    r‰S ˆ ˆˆˆfd„}|S )a3  
    Given a function `f`, returns a function `g(x)` that evaluates the nth
    derivative `f^{(n)}(x)`::

        >>> from mpmath import *
        >>> mp.dps = 15; mp.pretty = True
        >>> cos2 = diffun(sin)
        >>> sin2 = diffun(sin, 4)
        >>> cos(1.3), cos2(1.3)
        (0.267498828624587, 0.267498828624587)
        >>> sin(1.3), sin2(1.3)
        (0.963558185417193, 0.963558185417193)

    The function `f` must support arbitrary precision evaluation.
    See :func:`~mpmath.diff` for additional details and supported
    keyword options.
    r   c                 ó$   •—  ‰j         ‰| ‰fi ‰¤ŽS r7   rO   )r   r
   r   r   r'   s    €€€€r   r<   zdiffun.<locals>.g  s!   ø€ ØˆsŒx˜˜1˜aÐ+Ð+ 7Ð+Ð+Ð+r   r   )r
   r   r   r'   r<   s   ```` r   Údiffunr”   	  sC   øøøø€ ð& 	ˆA‚v€vØˆð,ð ,ð ,ð ,ð ,ð ,ð ,ð ,à€Hr   c                 ó¢   ‡ — t           ‰ j        |||fi |¤Ž¦  «        }|                     dd¦  «        rˆ fd„|D ¦   «         S ˆ fd„|D ¦   «         S )a§  
    Produces a degree-`n` Taylor polynomial around the point `x` of the
    given function `f`. The coefficients are returned as a list.

        >>> from mpmath import *
        >>> mp.dps = 15; mp.pretty = True
        >>> nprint(chop(taylor(sin, 0, 5)))
        [0.0, 1.0, 0.0, -0.166667, 0.0, 0.00833333]

    The coefficients are computed using high-order numerical
    differentiation. The function must be possible to evaluate
    to arbitrary precision. See :func:`~mpmath.diff` for additional details
    and supported keyword options.

    Note that to evaluate the Taylor polynomial as an approximation
    of `f`, e.g. with :func:`~mpmath.polyval`, the coefficients must be reversed,
    and the point of the Taylor expansion must be subtracted from
    the argument:

        >>> p = taylor(exp, 2.0, 10)
        >>> polyval(p[::-1], 2.5 - 2.0)
        12.1824939606092
        >>> exp(2.5)
        12.1824939607035

    ÚchopTc                 ól   •— g | ]0\  }}‰                      |¦  «        ‰                     |¦  «        z  ‘Œ1S r   )r–   rB   ©r   rM   r   r
   s      €r   r    ztaylor.<locals>.<listcomp>@  s8   ø€ Ð=Ð=Ð=±°°A�—’˜‘”˜CŸMšM¨!Ñ,Ô,Ñ,Ð=Ð=Ð=r   c                 óF   •— g | ]\  }}|‰                      |¦  «        z  ‘ŒS r   )rB   r˜   s      €r   r    ztaylor.<locals>.<listcomp>B  s.   ø€ Ð3Ð3Ð3¡t q¨!��#—-’- Ñ"Ô"Ñ"Ð3Ð3Ð3r   )rt   r]   r!   )r
   r   r   r   r'   rc   s   `     r   Útaylorrš   "  st   ø€ õ8 �I�C”I˜a  AÐ1Ð1¨Ð1Ð1Ñ
2Ô
2€CØ‡{‚{�6˜4Ñ Ô ð 4Ø=Ð=Ð=Ð=¸Ð=Ñ=Ô=Ð=à3Ð3Ð3Ð3¨sÐ3Ñ3Ô3Ð3r   c                 óò  — t          |¦  «        ||z   dz   k     rt          d¦  «        ‚|dk    r+|dk    r| j        g| j        gfS |d|dz   …         | j        gfS |                      |¦  «        }t	          |¦  «        D ];}t	          t          |||z   dz   ¦  «        ¦  «        D ]}|||z   |z
           |||f<   ŒŒ<|                      ||dz   ||z   dz   …         ¦  «         }|                      ||¦  «        }| j        gt          |¦  «        z   }	dg|dz   z  }
t	          |dz   ¦  «        D ]J}||         }t	          dt          ||¦  «        dz   ¦  «        D ]}||	|         |||z
           z  z  }Œ||
|<   ŒK|
|	fS )aä  
    Computes a Pade approximation of degree `(L, M)` to a function.
    Given at least `L+M+1` Taylor coefficients `a` approximating
    a function `A(x)`, :func:`~mpmath.pade` returns coefficients of
    polynomials `P, Q` satisfying

    .. math ::

        P = \sum_{k=0}^L p_k x^k

        Q = \sum_{k=0}^M q_k x^k

        Q_0 = 1

        A(x) Q(x) = P(x) + O(x^{L+M+1})

    `P(x)/Q(x)` can provide a good approximation to an analytic function
    beyond the radius of convergence of its Taylor series (example
    from G.A. Baker 'Essentials of Pade Approximants' Academic Press,
    Ch.1A)::

        >>> from mpmath import *
        >>> mp.dps = 15; mp.pretty = True
        >>> one = mpf(1)
        >>> def f(x):
        ...     return sqrt((one + 2*x)/(one + x))
        ...
        >>> a = taylor(f, 0, 6)
        >>> p, q = pade(a, 3, 3)
        >>> x = 10
        >>> polyval(p[::-1], x)/polyval(q[::-1], x)
        1.38169105566806
        >>> f(x)
        1.38169855941551

    r   z%L+M+1 Coefficients should be providedr   N)rT   rC   ÚoneÚmatrixrS   rX   Úlu_solver=   )r
   rl   ÚLÚMrY   ÚjrM   rF   r   Úqr{   r   s               r   Úpader£   D  s®  € õP ˆ1�v„v��!‘�A‘‚~€~ÝÐ@ÑAÔAÐAàˆA‚v€vØ�Š6ˆ6Ø”G�9˜sœw˜iÐ'Ð'à�T�a˜‘c�T”7˜SœW˜IÐ%Ð%ð 	�
Š
�1‰Œ€AÝ�1‰XŒXð ð ˆÝ•s˜1˜a ™c !™e‘}”}Ñ%Ô%ð 	ð 	ˆAØ˜˜!™˜A™”hˆAˆa�ˆd‰GˆGð	à	�Š�A�q˜‘s˜Q˜q™S ™U�mÔ$Ñ	%Ô	%Ð%€AØ�Š�Q˜ÑÔ€AØ	Œˆ	•D˜‘G”GÑ€Aà	
ˆˆQˆq‰S‰	€AÝ�1�Q‘3‰ZŒZð ð ˆØˆaŒDˆÝ�q�#˜a ™(œ( Q™,Ñ'Ô'ð 	ð 	ˆAØ��1”�a˜˜!™”f‘ÑˆAˆAØˆˆ!‰ˆØˆaˆ4€Kr   )r   r7   )r   r   )Úlibmp.backendr   Úcalculusr   rr   rs   ÚAttributeErrorÚitemsr   r.   rG   r?   r]   re   rg   rq   r…   r‘   r”   rš   r£   r   r   r   ú<module>r¨      sÐ  ðØ "Ð "Ð "Ð "Ð "Ð "Ø Ð Ð Ð Ð Ð ðØ”€I€IøØð ð ð Ø”
€I€I€Iðøøøð ðð ñ „ðð"!ð !ð !ðH ðHð Hð Hñ „ðHðT@ð @ð @ð" ðGð Gð Gñ „ðGðRð ð ð ð3ð 3ñ „ð3ðj ð ð ð ð ðB ð4ð 4ñ „ð4ðl ðF.ð F.ð F.ñ „ðF.ðP ðð ð ñ „ðð0 ð4ð 4ñ „ð4ðB ðBð Bñ „ðBð Bð Bs   Ž –%¤%