o
    ¦ƒðjq>  ã                   @   s  d dl mZmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  G dd„ de!ƒZ"G dd„ de"eee
eeeeeeeeeeƒZ#dS )é    )ÚgtÚlté   )Úxrange)ÚSpecialFunctions)ÚRSCache)ÚQuadratureMethods)Ú LaplaceTransformInversionMethods)ÚCalculusMethods)ÚOptimizationMethods)Ú
ODEMethods)ÚMatrixMethods)ÚMatrixCalculusMethods)ÚLinearAlgebraMethods)ÚEigen)ÚIdentificationMethods)ÚVisualizationMethods)Úlibmpc                   @   s   e Zd ZdS )ÚContextN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú‘/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/mpmath/ctx_base.pyr      s    r   c                   @   sp  e Zd ZejZejZdd„ Zdd„ ZdZdZ	dd„ Z
dd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd?dd„Zd@dd„Zdd „ ZdAd"d#„ZdBd$d%„ZdCd&d'„Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zeej ƒZ!eej"ƒZ"eej#ƒZ#eej$ƒZ$eej%ƒZ%eej&ƒZ'eej(ƒZ)eej*ƒZ+eej,ƒZ-dDd3d4„Z.dDd5d6„Z/d7d8„ Z0d9d:„ Z1d;d<„ Z2d=d>„ Z3dS )EÚStandardBaseContextc                 C   sF   i | _ t | ¡ t | ¡ t | ¡ t | ¡ t | ¡ t | ¡ d S ©N)Ú_aliasesr   Ú__init__r   r   r	   r
   r   )Úctxr   r   r   r   *   s   




zStandardBaseContext.__init__c              	   C   s@   | j  ¡ D ]\}}zt| |t| |ƒƒ W q ty   Y qw d S r   )r   ÚitemsÚsetattrÚgetattrÚAttributeError)r   ÚaliasÚvaluer   r   r   Ú_init_aliases4   s   ÿýz!StandardBaseContext._init_aliasesFc                 C   s   t d|ƒ d S )NzWarning:)Úprint©r   Úmsgr   r   r   Úwarn@   s   zStandardBaseContext.warnc                 C   s   t |ƒ‚r   )Ú
ValueErrorr'   r   r   r   Ú
bad_domainC   s   zStandardBaseContext.bad_domainc                 C   s   t |dƒr|jS |S )NÚreal)Úhasattrr,   ©r   Úxr   r   r   Ú_reF   s   
zStandardBaseContext._rec                 C   s   t |dƒr|jS | jS )NÚimag)r-   r1   Úzeror.   r   r   r   Ú_imK   s   
zStandardBaseContext._imc                 C   s   |S r   r   r.   r   r   r   Ú
_as_pointsP   s   zStandardBaseContext._as_pointsc                 K   s   |   |¡ S r   ©Úconvert)r   r/   Úkwargsr   r   r   ÚfnegS   s   zStandardBaseContext.fnegc                 K   s   |   |¡|   |¡ S r   r5   ©r   r/   Úyr7   r   r   r   ÚfaddV   ó   zStandardBaseContext.faddc                 K   s   |   |¡|   |¡ S r   r5   r9   r   r   r   ÚfsubY   r<   zStandardBaseContext.fsubc                 K   s   |   |¡|   |¡ S r   r5   r9   r   r   r   Úfmul\   r<   zStandardBaseContext.fmulc                 K   s   |   |¡|   |¡ S r   r5   r9   r   r   r   Úfdiv_   r<   zStandardBaseContext.fdivc                 C   sZ   |r|rt dd„ |D ƒ| jƒS t dd„ |D ƒ| jƒS |r't dd„ |D ƒ| jƒS t || jƒS )Nc                 s   s   � | ]	}t |ƒd  V  qdS ©é   N©Úabs©Ú.0r/   r   r   r   Ú	<genexpr>e   ó   € z+StandardBaseContext.fsum.<locals>.<genexpr>c                 s   s   � | ]}t |ƒV  qd S r   rB   rD   r   r   r   rF   f   ó   € c                 s   s   � | ]}|d  V  qdS r@   r   rD   r   r   r   rF   h   rH   )Úsumr2   )r   ÚargsÚabsoluteÚsquaredr   r   r   Úfsumb   s   zStandardBaseContext.fsumNc                    sL   |d ur	t ||ƒ}|r| j‰ t‡ fdd„|D ƒ| jƒS tdd„ |D ƒ| jƒS )Nc                 3   s    � | ]\}}|ˆ |ƒ V  qd S r   r   ©rE   r/   r:   ©Úcfr   r   rF   p   s   € z+StandardBaseContext.fdot.<locals>.<genexpr>c                 s   s   � | ]	\}}|| V  qd S r   r   rN   r   r   r   rF   r   rG   )ÚzipÚconjrI   r2   )r   ÚxsÚysÚ	conjugater   rO   r   Úfdotk   s   
zStandardBaseContext.fdotc                 C   s   | j }|D ]}||9 }q|S r   )Úone)r   rJ   ÚprodÚargr   r   r   Úfprodt   s   
zStandardBaseContext.fprodé   c                 K   s   t | j||fi |¤Žƒ dS )z6
        Equivalent to ``print(nstr(x, n))``.
        N)r&   Únstr)r   r/   Únr7   r   r   r   Únprintz   s   zStandardBaseContext.nprintc                    sì   ˆdu r	dˆ j  ‰z?ˆ  |¡}t|ƒ}t|ƒˆk rˆ jW S ˆ  |¡rCtˆ|ˆ ƒ}t|jƒ|k r4|jW S t|jƒ|k rFˆ  d|j¡W S W |S W |S  t	yu   t
|ˆ jƒra| ‡ ‡fdd„¡ Y S t|dƒrr‡ ‡fdd„|D ƒ Y S Y |S w )	aÖ  
        Chops off small real or imaginary parts, or converts
        numbers close to zero to exact zeros. The input can be a
        single number or an iterable::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> chop(5+1e-10j, tol=1e-9)
            mpf('5.0')
            >>> nprint(chop([1.0, 1e-20, 3+1e-18j, -4, 2]))
            [1.0, 0.0, 3.0, -4.0, 2.0]

        The tolerance defaults to ``100*eps``.
        Néd   r   c                    s   ˆ   | ˆ¡S r   ©Úchop)Úa©r   Útolr   r   Ú<lambda>Ÿ   s    z*StandardBaseContext.chop.<locals>.<lambda>Ú__iter__c                    s   g | ]}ˆ   |ˆ¡‘qS r   r`   )rE   rb   rc   r   r   Ú
<listcomp>¡   ó    z,StandardBaseContext.chop.<locals>.<listcomp>)Úepsr6   rC   r2   Ú_is_complex_typeÚmaxr1   r,   ÚmpcÚ	TypeErrorÚ
isinstanceÚmatrixÚapplyr-   )r   r/   rd   ÚabsxÚpart_tolr   rc   r   ra   €   s2   


úùû
ÿûzStandardBaseContext.chopc           	      C   sœ   |   |¡}|du r|du r|  d| j d ¡ }}|du r |}n|du r&|}t|| ƒ}||kr2dS t|ƒ}t|ƒ}||k rF|| }||kS || }||kS )aÖ  
        Determine whether the difference between `s` and `t` is smaller
        than a given epsilon, either relatively or absolutely.

        Both a maximum relative difference and a maximum difference
        ('epsilons') may be specified. The absolute difference is
        defined as `|s-t|` and the relative difference is defined
        as `|s-t|/\max(|s|, |t|)`.

        If only one epsilon is given, both are set to the same value.
        If none is given, both epsilons are set to `2^{-p+m}` where
        `p` is the current working precision and `m` is a small
        integer. The default setting typically allows :func:`~mpmath.almosteq`
        to be used to check for mathematical equality
        in the presence of small rounding errors.

        **Examples**

            >>> from mpmath import *
            >>> mp.dps = 15
            >>> almosteq(3.141592653589793, 3.141592653589790)
            True
            >>> almosteq(3.141592653589793, 3.141592653589700)
            False
            >>> almosteq(3.141592653589793, 3.141592653589700, 1e-10)
            True
            >>> almosteq(1e-20, 2e-20)
            True
            >>> almosteq(1e-20, 2e-20, rel_eps=0, abs_eps=0)
            False

        Nr   é   T)r6   ÚldexpÚprecrC   )	r   ÚsÚtÚrel_epsÚabs_epsÚdiffÚabssÚabstÚerrr   r   r   Úalmosteq¤   s"   
!ÿzStandardBaseContext.almosteqc           	      G   s&  t |ƒdkstdt |ƒ ƒ‚t |ƒdkstdt |ƒ ƒ‚d}d}t |ƒdkr+|d }nt |ƒdkr9|d }|d }t |ƒdkrC|d }|  |¡|  |¡|  |¡}}}|| |ks^J dƒ‚||krk|dkrhg S t}n|dk rqg S t}g }d}|}	 |||  }|d7 }|||ƒr�| |¡ n	 |S qz)aa  
        This is a generalized version of Python's :func:`~mpmath.range` function
        that accepts fractional endpoints and step sizes and
        returns a list of ``mpf`` instances. Like :func:`~mpmath.range`,
        :func:`~mpmath.arange` can be called with 1, 2 or 3 arguments:

        ``arange(b)``
            `[0, 1, 2, \ldots, x]`
        ``arange(a, b)``
            `[a, a+1, a+2, \ldots, x]`
        ``arange(a, b, h)``
            `[a, a+h, a+h, \ldots, x]`

        where `b-1 \le x < b` (in the third case, `b-h \le x < b`).

        Like Python's :func:`~mpmath.range`, the endpoint is not included. To
        produce ranges where the endpoint is included, :func:`~mpmath.linspace`
        is more convenient.

        **Examples**

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> arange(4)
            [mpf('0.0'), mpf('1.0'), mpf('2.0'), mpf('3.0')]
            >>> arange(1, 2, 0.25)
            [mpf('1.0'), mpf('1.25'), mpf('1.5'), mpf('1.75')]
            >>> arange(1, -1, -0.75)
            [mpf('1.0'), mpf('0.25'), mpf('-0.5')]

        é   z+arange expected at most 3 arguments, got %ir   z+arange expected at least 1 argument, got %ir   rA   z0dt is too small and would cause an infinite loop)Úlenrm   Úmpfr   r   Úappend)	r   rJ   rb   ÚdtÚbÚopÚresultÚirw   r   r   r   Úarange×   sJ    ÿÿ
"
ùzStandardBaseContext.arangec                    s"  t |ƒdkr|  |d ¡‰ |  |d ¡}t|d ƒ}n(t |ƒdkr;t|d dƒs*J ‚|d j‰ |d j}t|d ƒ}ntdt |ƒ ƒ‚|dk rKtdƒ‚d|vsS|d rz|dkr]|  ˆ ¡gS |ˆ  |  |d ¡ ‰‡ ‡fd	d
„t|ƒD ƒ}||d< |S |ˆ  |  |¡ ‰‡ ‡fdd
„t|ƒD ƒ}|S )aÄ  
        ``linspace(a, b, n)`` returns a list of `n` evenly spaced
        samples from `a` to `b`. The syntax ``linspace(mpi(a,b), n)``
        is also valid.

        This function is often more convenient than :func:`~mpmath.arange`
        for partitioning an interval into subintervals, since
        the endpoint is included::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> linspace(1, 4, 4)
            [mpf('1.0'), mpf('2.0'), mpf('3.0'), mpf('4.0')]

        You may also provide the keyword argument ``endpoint=False``::

            >>> linspace(1, 4, 4, endpoint=False)
            [mpf('1.0'), mpf('1.75'), mpf('2.5'), mpf('3.25')]

        r   r   r   rA   Ú_mpi_z*linspace expected 2 or 3 arguments, got %izn must be greater than 0Úendpointc                    ó   g | ]}|ˆ ˆ  ‘qS r   r   ©rE   r‡   ©rb   Ústepr   r   rg   G  rh   z0StandardBaseContext.linspace.<locals>.<listcomp>éÿÿÿÿc                    r‹   r   r   rŒ   r�   r   r   rg   K  rh   )	r€   r�   Úintr-   rb   r„   rm   r*   r   )r   rJ   r7   r„   r]   r:   r   r�   r   Úlinspace   s0   

ÿþzStandardBaseContext.linspacec                 K   ó$   | j |fi |¤Ž| j|fi |¤ŽfS r   )ÚcosÚsin©r   Úzr7   r   r   r   Úcos_sinN  ó   $zStandardBaseContext.cos_sinc                 K   r’   r   )ÚcospiÚsinpir•   r   r   r   Úcospi_sinpiQ  r˜   zStandardBaseContext.cospi_sinpic                 C   s   t d|d  d|  ƒS )Niè  g      Ð?rs   )r�   )r   Úpr   r   r   Ú_default_hyper_maxprecT  s   z*StandardBaseContext._default_hyper_maxprecr   c                 C   sÌ   | j }z^d}	 || d | _ | j}| j}d}|ƒ D ](}||7 }|| s=|r=|  |¡}	t||	ƒ}|  |¡}
|
|	 | j kr= n|d7 }q||
 }||krKn||k sR| jrSn	|t| j |ƒ7 }q|W || _ S || _ w ©Né
   r   é   r   )ru   Úninfr2   Úmagrk   Ú_fixed_precisionÚmin)r   ÚtermsÚ
check_stepru   Ú	extraprecÚmax_magrv   ÚkÚtermÚterm_magÚsum_magÚcancellationr   r   r   Úsum_accuratelya  s4   




íz"StandardBaseContext.sum_accuratelyc                 C   sÖ   | j }zcd}	 || d | _ | j}| j}|}d}|ƒ D ]+}	||	9 }|	| }
|| sB|  |
¡}t||ƒ}|  || ¡}| | j krB n|d7 }q|| }||krPn||k sW| jrXn	|t| j |ƒ7 }q|W || _ S || _ w rž   )ru   r¡   rW   r¢   rk   r£   r¤   )r   Úfactorsr¦   ru   r§   r¨   rW   rv   r©   Úfactorrª   r«   r¬   r­   r   r   r   Úmul_accurately}  s8   



éz"StandardBaseContext.mul_accuratelyc                 C   s   |   |¡|   |¡ S )a  Converts `x` and `y` to mpmath numbers and evaluates
        `x^y = \exp(y \log(x))`::

            >>> from mpmath import *
            >>> mp.dps = 30; mp.pretty = True
            >>> power(2, 0.5)
            1.41421356237309504880168872421

        This shows the leading few digits of a large Mersenne prime
        (performing the exact calculation ``2**43112609-1`` and
        displaying the result in Python would be very slow)::

            >>> power(2, 43112609)-1
            3.16470269330255923143453723949e+12978188
        r5   )r   r/   r:   r   r   r   Úpower�  s   zStandardBaseContext.powerc                 C   s
   |   |¡S r   )Úzeta)r   r]   r   r   r   Ú	_zeta_int¯  s   
zStandardBaseContext._zeta_intc                    s   dg‰‡ ‡‡‡fdd„}|S )aù  
        Return a wrapped copy of *f* that raises ``NoConvergence`` when *f*
        has been called more than *N* times::

            >>> from mpmath import *
            >>> mp.dps = 15
            >>> f = maxcalls(sin, 10)
            >>> print(sum(f(n) for n in range(10)))
            1.95520948210738
            >>> f(10) # doctest: +IGNORE_EXCEPTION_DETAIL
            Traceback (most recent call last):
              ...
            NoConvergence: maxcalls: function evaluated 10 times

        r   c                     s8   ˆd  d7  < ˆd ˆ krˆ  dˆ  ¡‚ˆ| i |¤ŽS )Nr   r   z%maxcalls: function evaluated %i times)ÚNoConvergence)rJ   r7   ©ÚNÚcounterr   Úfr   r   Úf_maxcalls_wrappedÃ  s   z8StandardBaseContext.maxcalls.<locals>.f_maxcalls_wrappedr   )r   r¹   r·   rº   r   r¶   r   Úmaxcalls²  s   zStandardBaseContext.maxcallsc                    s(   i ‰‡ ‡‡fdd„}ˆj |_ ˆj|_|S )a™  
        Return a wrapped copy of *f* that caches computed values, i.e.
        a memoized copy of *f*. Values are only reused if the cached precision
        is equal to or higher than the working precision::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = True
            >>> f = memoize(maxcalls(sin, 1))
            >>> f(2)
            0.909297426825682
            >>> f(2)
            0.909297426825682
            >>> mp.dps = 25
            >>> f(2) # doctest: +IGNORE_EXCEPTION_DETAIL
            Traceback (most recent call last):
              ...
            NoConvergence: maxcalls: function evaluated 1 times

        c                     s`   |r| t | ¡ ƒf}n| }ˆ j}|ˆv r!ˆ| \}}||kr!|
 S ˆ| i |¤Ž}||fˆ|< |S r   )Útupler   ru   )rJ   r7   Úkeyru   ÚcprecÚcvaluer$   ©r   r¹   Úf_cacher   r   Úf_cachedß  s   z-StandardBaseContext.memoize.<locals>.f_cached)r   Ú__doc__)r   r¹   rÂ   r   rÀ   r   ÚmemoizeÊ  s
   zStandardBaseContext.memoize)FF)NF)r[   r   )NN)r   )4r   r   r   r   rµ   ÚComplexResultr   r%   r£   Úverboser)   r+   r0   r3   r4   r8   r;   r=   r>   r?   rM   rV   rZ   r^   ra   r~   rˆ   r‘   r—   r›   r�   ÚstaticmethodÚgcdÚ_gcdÚlist_primesÚisprimeÚbernfracÚmoebiusÚifacÚ_ifacÚeulernumÚ	_eulernumÚ	stirling1Ú
_stirling1Ú	stirling2Ú
_stirling2r®   r±   r²   r´   r»   rÄ   r   r   r   r   r      sV    


		


$3I.










 r   N)$Úoperatorr   r   Úlibmp.backendr   Úfunctions.functionsr   Úfunctions.rszetar   Úcalculus.quadraturer   Úcalculus.inverselaplacer	   Úcalculus.calculusr
   Úcalculus.optimizationr   Úcalculus.odesr   Úmatrices.matricesr   Úmatrices.calculusr   Úmatrices.linalgr   Úmatrices.eigenr   Úidentificationr   Úvisualizationr   Ú r   Úobjectr   r   r   r   r   r   Ú<module>   s@    ó