� H�gO��>�ddlmZddlmZ ejZn#e$r ejZYnwxYwed���Zd�Z edd���Z d�Z edd ���Z d �Z ed ���Zifd �Zed ���Zedd���Zedd���Zed���Zed���ZdS)�)�xrange�)�defunc��t|��}|j}d|dzz}t|dz��D]}||||zz }|||z z|dzz}�|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 �o/home/asafur/pinokio/api/open-webui.git/app/env/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��}|�dd��}|�dd��}|d|zz|dzz} |j} | |_|�d�����X|�d ��r#t|������} nd} |�d| |z | z ���n|�����|�dd��}|r-�|�|��z�t|dz��} �} nt| |dzd��} d�z} |r�d �zz ����fd �| D��}|| | f| |_S#| |_wxYw) N�singular�addprec� � direction�rr�h�relativeg�?c�2��g|]}��|�zz����S�r)�.0r�fr�xs ���r� <listcomp>zhsteps.<locals>.<listcomp>=s)���*�*�*�q�!�!�A�a��c�E�(�(�*�*�*r)�get�precr�mag�ldexp�convert�signr)r rrr r"�optionsrrr�workprec�orig� hextramag�steps�norm�valuesrs `` @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 �DE+�+ E4c �\������d} t���}t����d}n#t$rYnwxYw|r!�fd��D���t���||��S|�dd��}�dkr9|dkr3|�d��s��������S�j} |dkr9t ����|fi|��\} } } | �_��| ���| �zz } n�|dkr��xjd z c_��|�d d ����������fd �} ��| dd �j zg��}|�� ���zd �j zz } ntd|z���|�_n #|�_wxYw| 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|]}��|����Sr)r%)r�_r s �rr zdiff.<locals>.<listcomp>�s#��� '� '� '��S�[�[��^�^� '� '� 'r�method�stepr�quadrr�radiusg�?c�`�����|��z}�|z}�|��|�zz S�N)�expj)�t�rei�zr rr r5rs �����r�gzdiff.<locals>.g�s6����S�X�X�a�[�[�(����G���q��t�t�c�1�f�}�$rrzunknown method: %r) �list� TypeError� _partial_diffr!r%r"r.r�quadts�pi� factorial� ValueError)r rrr r'�partial�ordersr2r"r-r,r(�vr<r r5s```` @r�diffrGCs�������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�:CF� F(c��������|s ���St|��s�|�Sd�tt|����D] �|�rn� |�������fd�}d|�<t�|||���S)Nrc�@������fd�}�j|���fi���S)Nc�B����d��|fz��dzd�z�S�Nrr)r9r�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)rLrNr rrMr'�orders` �����r� fdiff_innerz"_partial_diff.<locals>.fdiff_inner�sI���� :� :� :� :� :� :� :��s�x��v�a�y�%�;�;�7�;�;�;r)�sum�range�lenr?)r r�xsrEr'rQrMrPs`` ` @@rr?r?�s�������� ���q�s�s� � �v�;�;���q�"�v� � �A� �3�v�;�;� � ���� �!�9� � �E� � �1�I�E�<�<�<�<�<�<�<�<�<��F�1�I� ��k�2�v�w� ?� ?�?rNc+��K�|�|j}nt|��}|�dd��dkr-d}||dzkr |j|||fi|��V�|dz }||dzk� dS|�d��}|r|�||dd���V�n ||�|����V�|dkrdS||jkrd \}}nd|dz}} |j} t ||||| fi|��\} } } t||��D]H} | |_|�| |��| |zz } | |_n #| |_wxYw| V�||krdS�I|t|d zdz��}}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 Nr2r3rrrT)r)rrgffffff�?) �infrr!rGr%r"r.rr�min)r rrr r'rr�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���fd�}|S)Nc���tt���|dz��D]$}��t������%�|SrK)rrT�append�next)rrM�data�gens ��rrziterable_to_function.<locals>.f,sJ�����D� � �1�Q�3�'�'� #� #�A� �K�K��S� � � "� "� "� "��A�w�r)�iter)rcrrbs` @r�iterable_to_functionre)s9���� �s�)�)�C� �D������� �Hrc#��K�t|��}|dkr|dD]}|V��dSt|�|d|dz�����}t|�||dzd�����}d} ||��|d��z}d}td|dz��D]0} ||| z dzz| z}||||| z ��z|| ��zz }�1|V�|dz }�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 rrNr)rTre� diffs_prodr) r �factors�N�c�urFr r �ars rrgrg2s*����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� rc�V�||vr||S|sddi|d<t|dz ��}td�t|��D����}i}t|��D]6\}}|ddzf|dd�z}||vr||xx|z cc<�1|||<�7t|��D]x\}}t|��s�t |��D]S\}}|rL|d|�|dz ||dzdzfz||dzd�z} | |vr|| xx||zz cc<�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. �rrrc3�*K�|]\}}|dz|fV��dS)rnNr)rrjrFs r� <genexpr>zdpoly.<locals>.<genexpr>ts.���� 2� 2�E�Q�q�a��f�Q�Z� 2� 2� 2� 2� 2� 2rNr)�dpoly�dict� iteritemsrR� enumerate) r �_cache�R�Ra�powers�count�powers1r�p�powers2s rrqrqhs��� �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�rc #�`�K�t|���|��d����}|V�d} |�d��}tt |����D]9\}}|||��fd�t |��D����zz }�:||zV�|dz }�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 rrc3�D�K�|]\}}|��|dz��|zV��dS)rNr)rrr{�fns �rrpzdiffs_exp.<locals>.<genexpr>�s<�����L�L�E�Q�q�!�L�R�R��!��W�W�a�Z�L�L�L�L�L�Lr)re�exp�mpfrsrq�fprodrt)r �fdiffs�f0rMr rxrjrs @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��� rrc ������tt����|������dzd��}||z dz �����fd�}��|||����||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) rc�@��������fd���g��S)Nc�,���|z �z�|��zSr7r)r9r�rrs ���r�<lambda>z-differint.<locals>.<lambda>.<locals>.<lambda>s���a��c�A�X���!���_�r)r4)rr rr��x0s`����rr�zdifferint.<locals>.<lambda>s*����#�(�(�4�4�4�4�4�4�r�1�g�>�>�r)�maxr�ceil�rerG�gamma)r rrr r��mr<r�s`` ` @r� differintr��s�������H �C����������#�#� $� $�Q� &��*�*�A� �!��A��A�>�>�>�>�>�>�>�A� �8�8�A�q�!� � �s�y�y��1��~�~� -�-rc �.������dkr�S����fd�}|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. rc�$���j�|�fi���Sr7rO)rr rr r's ����rr<zdiffun.<locals>.gs!����s�x��1�a�+�+�7�+�+�+rr)r rr r'r<s```` r�diffunr� sC������& �A�v�v���,�,�,�,�,�,�,�,� �Hrc ���t�j|||fi|����}|�dd��r�fd�|D��S�fd�|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 ��1Sr)r�rB�rrMr r s �rr ztaylor.<locals>.<listcomp>@s8���=�=�=���A����� � �C�M�M�!�,�,�,�=�=�=rc�F��g|]\}}|��|��z ��Sr)rBr�s �rr ztaylor.<locals>.<listcomp>Bs.���3�3�3�t�q�!��#�-�-��"�"�"�3�3�3r)rtr]r!)r rrr r'rcs` 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�3rc���t|��||zdzkrtd���|dkr+|dkr|jg|jgfS|d|dz�|jgfS|�|��}t |��D];}t t |||zdz����D]}|||z|z |||f<��<|�||dz||zdz��� }|�||��}|jgt|��z} dg|dzz} t |dz��D]J}||} t dt ||��dz��D]}| | ||||z zz } �| | |<�K| | fS)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 rz%L+M+1 Coefficients should be providedrN)rTrC�one�matrixrSrX�lu_solver=) r rl�L�MrY�jrMrFr�qr{r s r�pader�Ds���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)rr7)rr)� libmp.backendr�calculusrrrrs�AttributeError�itemsrr.rGr?r]rergrqr�r�r�r�r�rrr�<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�Bs �� %�%
Memory