B
    bdq>                 @   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   \/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/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   sH   xB| j  D ]4\}}yt| |t| | W q tk
r>   Y qX qW d S )N)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 )N)
ValueError)r   r&   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+   r.   zero)r   r,   r   r   r   _imK   s    
zStandardBaseContext._imc             C   s   |S )Nr   )r   r,   r   r   r   
_as_pointsP   s    zStandardBaseContext._as_pointsc             K   s   |  | S )N)convert)r   r,   kwargsr   r   r   fnegS   s    zStandardBaseContext.fnegc             K   s   |  ||  | S )N)r2   )r   r,   yr3   r   r   r   faddV   s    zStandardBaseContext.faddc             K   s   |  ||  | S )N)r2   )r   r,   r5   r3   r   r   r   fsubY   s    zStandardBaseContext.fsubc             K   s   |  ||  | S )N)r2   )r   r,   r5   r3   r   r   r   fmul\   s    zStandardBaseContext.fmulc             K   s   |  ||  | S )N)r2   )r   r,   r5   r3   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 )N)r;   )r<   r,   r   r   r   r=   f   s    c             s   s   | ]}|d  V  qdS )r:   Nr   )r<   r,   r   r   r   r=   h   s    )sumr/   )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 )Nr   )r<   r,   r5   )cfr   r   r=   p   s    z+StandardBaseContext.fdot.<locals>.<genexpr>c             s   s   | ]\}}|| V  qd S )Nr   )r<   r,   r5   r   r   r   r=   r   s    )zipZconjr>   r/   )r   ZxsZys	conjugater   )rB   r   fdotk   s    
zStandardBaseContext.fdotc             C   s    | j }x|D ]}||9 }qW |S )N)one)r   r?   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,   nr3   r   r   r   nprintz   s    zStandardBaseContext.nprintc                s   dkrd j  yp |}t|}t|k r8 jS  |rt| }t|j|k rd|jS t|j|k r d|jS W nR t	k
r   t
| jr| fddS t|drЇ fdd|D 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``.
        Nd   r   c                s     | S )N)chop)a)r   tolr   r   <lambda>       z*StandardBaseContext.chop.<locals>.<lambda>__iter__c                s   g | ]}  |qS r   )rN   )r<   rO   )r   rP   r   r   
<listcomp>   s    z,StandardBaseContext.chop.<locals>.<listcomp>)Zepsr2   r;   r/   Z_is_complex_typemaxr.   r*   Zmpc	TypeError
isinstancematrixapplyr+   )r   r,   rP   ZabsxZpart_tolr   )r   rP   r   rN      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)r2   ldexpprecr;   )	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std||kr|dkrg S t}n|dk rg S t}g }d}|}x2|||  }|d7 }|||r || qP qW |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)lenrV   mpfAssertionErrorr   r   append)	r   r?   rO   dtbopresultir^   r   r   r   arange   sD     
"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 rt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')]

        rb   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   )r<   rk   )rO   stepr   r   rT   G  s    z0StandardBaseContext.linspace.<locals>.<listcomp>c                s   g | ]}|   qS r   r   )r<   rk   )rO   rm   r   r   rT   K  s    )
rc   rd   intr+   re   rO   rh   rV   r(   r   )r   r?   r3   rh   rK   r5   r   )rO   rm   r   linspace   s,    


zStandardBaseContext.linspacec             K   s   | j |f|| j|f|fS )N)cossin)r   zr3   r   r   r   cos_sinN  s    zStandardBaseContext.cos_sinc             K   s   | j |f|| j|f|fS )N)ZcospiZsinpi)r   rs   r3   r   r   r   cospi_sinpiQ  s    zStandardBaseContext.cospi_sinpic             C   s   t d|d  d|  S )Ni  g      ?rZ   )ro   )r   pr   r   r   _default_hyper_maxprecT  s    z*StandardBaseContext._default_hyper_maxprecr   c             C   s   | j }zd}x|| d | _ | j}| j}d}xX| D ]N}||7 }|| sz|rz| |}	t||	}| |}
|
|	 | j krzP |d7 }q4W ||
 }||krP ||k s| jrP |t| j |7 }qW |S || _ X d S )N
      r   r   )r\   ninfr/   magrU   _fixed_precisionmin)r   Zterms
check_stepr\   	extraprecmax_magr]   ktermterm_magsum_magcancellationr   r   r   sum_accuratelya  s2    


z"StandardBaseContext.sum_accuratelyc             C   s   | j }zd}x|| d | _ | j}| j}|}d}x^| D ]T}	||	9 }|	| }
|| s| |
}t||}| || }| | j krP |d7 }q8W || }||krP ||k s| jrP |t| j |7 }qW |S || _ X d S )Nrx   ry   r   r   )r\   rz   rF   r{   rU   r|   r}   )r   Zfactorsr~   r\   r   r   rF   r]   r   factorr   r   r   r   r   r   r   mul_accurately}  s6    

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
        )r2   )r   r,   r5   r   r   r   power  s    zStandardBaseContext.powerc             C   s
   |  |S )N)zeta)r   rK   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)r?   r3   )Ncounterr   fr   r   f_maxcalls_wrapped  s    z8StandardBaseContext.maxcalls.<locals>.f_maxcalls_wrappedr   )r   r   r   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 )N)tupler   r\   )r?   r3   keyr\   ZcprecZcvaluer#   )r   r   f_cacher   r   f_cached  s    
z-StandardBaseContext.memoize.<locals>.f_cached)r   __doc__)r   r   r   r   )r   r   r   r   memoize  s
    zStandardBaseContext.memoize)FF)NF)rJ   )N)NN)r   )r   )4r   r   r   r   r   ZComplexResultr   r$   r|   verboser'   r)   r-   r0   r1   r4   r6   r7   r8   r9   rA   rE   rI   rL   rN   ra   rl   rp   rt   ru   rw   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<   