U
    ZÊ¹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   úN/var/www/html/bwave/python/venv/lib/python3.8/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.dEd5d6„Z/d7d8„ Z0d9d:„ Z1d;d<„ Z2d=d>„ Z3dS )FÚ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   sD   | j  ¡ D ]4\}}zt| |t| |ƒƒ W q
 tk
r<   Y q
X q
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 |ƒ‚d S 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   s    zStandardBaseContext.faddc                 K   s   |   |¡|   |¡ S r   r5   r9   r   r   r   ÚfsubY   s    zStandardBaseContext.fsubc                 K   s   |   |¡|   |¡ S r   r5   r9   r   r   r   Úfmul\   s    zStandardBaseContext.fmulc                 K   s   |   |¡|   |¡ S r   r5   r9   r   r   r   Úfdiv_   s    zStandardBaseContext.fdivc                 C   sZ   |r4|rt dd„ |D ƒ| jƒS t dd„ |D ƒ| jƒS |rNt 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   s     z+StandardBaseContext.fsum.<locals>.<genexpr>c                 s   s   | ]}t |ƒV  qd S r   rA   rC   r   r   r   rE   f   s     c                 s   s   | ]}|d  V  qdS r?   r   rC   r   r   r   rE   h   s     )Úsumr2   )r   ÚargsÚabsoluteZsquaredr   r   r   Úfsumb   s    zStandardBaseContext.fsumNc                    sP   |d k	rt ||ƒ}|r6| j‰ t‡ fdd„|D ƒ| jƒS tdd„ |D ƒ| jƒS d S )Nc                 3   s   | ]\}}|ˆ |ƒ V  qd S r   r   ©rD   r/   r:   ©Úcfr   r   rE   p   s     z+StandardBaseContext.fdot.<locals>.<genexpr>c                 s   s   | ]\}}|| V  qd S r   r   rJ   r   r   r   rE   r   s     )ÚzipZconjrF   r2   )r   ZxsZysÚ	conjugater   rK   r   Úfdotk   s    
zStandardBaseContext.fdotc                 C   s   | j }|D ]}||9 }q
|S r   )Úone)r   rG   ÚprodÚargr   r   r   Úfprodt   s    
zStandardBaseContext.fprodé   c                 K   s   t | j||f|Žƒ dS )z6
        Equivalent to ``print(nstr(x, n))``.
        N)r&   Znstr)r   r/   Únr7   r   r   r   Únprintz   s    zStandardBaseContext.nprintc                    sè   ˆdkrdˆ j  ‰zvˆ  |¡}t|ƒ}t|ƒˆk r:ˆ jW S ˆ  |¡r†tˆ|ˆ ƒ}t|jƒ|k rh|jW S t|jƒ|k r†ˆ  d|j¡W S W nZ t	k
râ   t
|ˆ jƒr¼| ‡ ‡fdd„¡ Y S t|dƒrÞ‡ ‡fdd„|D ƒ Y S Y nX |S )	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>Ÿ   ó    z*StandardBaseContext.chop.<locals>.<lambda>Ú__iter__c                    s   g | ]}ˆ   |ˆ¡‘qS r   rX   )rD   rZ   r[   r   r   Ú
<listcomp>¡   s     z,StandardBaseContext.chop.<locals>.<listcomp>)Úepsr6   rB   r2   Z_is_complex_typeÚmaxr1   r,   ZmpcÚ	TypeErrorÚ
isinstanceÚmatrixÚapplyr-   )r   r/   r\   ZabsxZpart_tolr   r[   r   rY   €   s&    



zStandardBaseContext.chopc           	      C   s–   |   |¡}|dkr2|dkr2|  d| j d ¡ }}|dkr@|}n|dkrL|}t|| ƒ}||krddS t|ƒ}t|ƒ}||k r†|| }n|| }||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ÚprecrB   )	r   ÚsÚtZrel_epsZabs_epsÚdiffZabssZabstÚerrr   r   r   Úalmosteq¤   s     !

zStandardBaseContext.almosteqc           	      G   s(  t |ƒdkstdt |ƒ ƒ‚t |ƒdks8tdt |ƒ ƒ‚d}d}t |ƒdkrV|d }nt |ƒdkrr|d }|d }t |ƒdkr†|d }|  |¡|  |¡|  |¡  }}}|| |ks¼tdƒ‚||krÖ|dkrÐg S t}n|dk râg S t}g }d}|}|||  }|d7 }|||ƒ�r$| |¡ qò�q$qò|S )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   r@   z0dt is too small and would cause an infinite loop)Úlenrc   ÚmpfÚAssertionErrorr   r   Úappend)	r   rG   rZ   ÚdtÚbÚopÚresultÚirk   r   r   r   Úarange×   sF     ÿÿ
"zStandardBaseContext.arangec                    s   t |ƒdkr6|  |d ¡‰ |  |d ¡}t|d ƒ}nPt |ƒdkrvt|d dƒsTt‚|d j‰ |d j}t|d ƒ}ntdt |ƒ ƒ‚|dk r–tdƒ‚d|ks¦|d rò|dkrº|  ˆ ¡gS |ˆ  |  |d ¡ ‰‡ ‡fd	d
„t	|ƒD ƒ}||d< n*|ˆ  |  |¡ ‰‡ ‡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')]

        ro   r   r   r@   Z_mpi_z*linspace expected 2 or 3 arguments, got %izn must be greater than 0Zendpointc                    s   g | ]}|ˆ ˆ  ‘qS r   r   ©rD   rx   ©rZ   Ústepr   r   r`   G  s     z0StandardBaseContext.linspace.<locals>.<listcomp>éÿÿÿÿc                    s   g | ]}|ˆ ˆ  ‘qS r   r   rz   r{   r   r   r`   K  s     )
rp   rq   Úintr-   rr   rZ   ru   rc   r*   r   )r   rG   r7   ru   rU   r:   r   r{   r   Úlinspace   s.    

ÿ
zStandardBaseContext.linspacec                 K   s   | j |f|Ž| j|f|ŽfS r   )ÚcosÚsin©r   Úzr7   r   r   r   Úcos_sinN  s    zStandardBaseContext.cos_sinc                 K   s   | j |f|Ž| j|f|ŽfS r   )ZcospiZsinpir‚   r   r   r   Úcospi_sinpiQ  s    zStandardBaseContext.cospi_sinpic                 C   s   t d|d  d|  ƒS )Niè  g      Ð?rg   )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 ]P}||7 }|| sx|rx|  |¡}	t||	ƒ}|  |¡}
|
|	 | j krx q‚|d7 }q0||
 }||kr”q¶||k s¶| jr¤q¶|t| j |ƒ7 }q|W ¢S || _ X d S ©Né
   é   r   r   )ri   Úninfr2   Úmagrb   Ú_fixed_precisionÚmin)r   ZtermsÚ
check_stepri   Ú	extraprecÚmax_magrj   ÚkÚtermÚterm_magÚsum_magÚcancellationr   r   r   Úsum_accuratelya  s0    




z"StandardBaseContext.sum_accuratelyc                 C   sÔ   | j }zÀd}|| d | _ | j}| j}|}d}|ƒ D ]V}	||	9 }|	| }
|| s‚|  |
¡}t||ƒ}|  || ¡}| | j kr‚ qŒ|d7 }q4|| }||kržqÀ||k sÀ| jr®qÀ|t| j |ƒ7 }q|W ¢S || _ X d S rˆ   )ri   r‹   rP   rŒ   rb   r�   rŽ   )r   Zfactorsr�   ri   r�   r‘   rP   rj   r’   Úfactorr“   r”   r•   r–   r   r   r   Úmul_accurately}  s4    



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   rU   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                     s4   ˆd  d7  < ˆd ˆ kr*ˆ  dˆ  ¡‚ˆ| |ŽS )Nr   r   z%maxcalls: function evaluated %i times)ÚNoConvergence)rG   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}|ˆkrBˆ| \}}||krB|
 S ˆ| |Ž}||fˆ|< |S r   )Útupler   ri   )rG   r7   Úkeyri   ZcprecZcvaluer$   ©r   r¡   Z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)rT   )N)NN)r   )r   )4r   r   r   r   r�   ZComplexResultr   r%   r�   Úverboser)   r+   r0   r3   r4   r8   r;   r<   r=   r>   rI   rO   rS   rV   rY   rn   ry   r   r„   r…   r‡   ÚstaticmethodÚgcdÚ_gcdZlist_primesZisprimeZbernfracZmoebiusZifacZ_ifacZeulernumZ	_eulernumZ	stirling1Z
_stirling1Z	stirling2Z
_stirling2r—   r™   rš   rœ   r£   r©   r   r   r   r   r      sT   

	
	

$
3I.










 r   N)$Úoperatorr   r   Zlibmp.backendr   Zfunctions.functionsr   Zfunctions.rszetar   Zcalculus.quadraturer   Zcalculus.inverselaplacer	   Zcalculus.calculusr
   Zcalculus.optimizationr   Zcalculus.odesr   Zmatrices.matricesr   Zmatrices.calculusr   Zmatrices.linalgr   Zmatrices.eigenr   Zidentificationr   Zvisualizationr   Ú r   Úobjectr   r   r   r   r   r   Ú<module>   s>   ó