a
    <bq>                     @   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   _/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/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   sB   | j  D ]2\}}zt| |t| | W q
 ty:   Y q
0 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       z+StandardBaseContext.fsum.<locals>.<genexpr>c                 s   s   | ]}t |V  qd S r   rA   rC   r   r   r   rE   f   rF   c                 s   s   | ]}|d  V  qdS r?   r   rC   r   r   r   rE   h   rF   )sumr2   )r   argsabsoluteZsquaredr   r   r   fsumb   s    zStandardBaseContext.fsumNc                    sP   |d u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   rF   z+StandardBaseContext.fdot.<locals>.<genexpr>c                 s   s   | ]\}}|| V  qd S r   r   rK   r   r   r   rE   r   rF   )zipZconjrG   r2   )r   xsZys	conjugater   rL   r   fdotk   s    
zStandardBaseContext.fdotc                 C   s   | j }|D ]}||9 }q
|S r   )one)r   rH   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&   Znstr)r   r/   nr7   r   r   r   nprintz   s    zStandardBaseContext.nprintc                    s   du rd j  zv |}t|}t|k r: jW S  |rt| }t|j|k rh|jW S t|j|k r d|jW S W nX t	y   t
| jr| fdd Y S t|dr܇ fdd|D  Y S Y n0 |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 r   chop)ar   tolr   r   <lambda>   rF   z*StandardBaseContext.chop.<locals>.<lambda>__iter__c                    s   g | ]}  |qS r   rZ   )rD   r\   r]   r   r   
<listcomp>   rF   z,StandardBaseContext.chop.<locals>.<listcomp>)Zepsr6   rB   r2   Z_is_complex_typemaxr1   r,   Zmpc	TypeError
isinstanceZmatrixapplyr-   )r   r/   r^   ZabsxZpart_tolr   r]   r   r[      s&    



zStandardBaseContext.chopc           	      C   s   |  |}|du r2|du r2| d| j d  }}|du r@|}n|du 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sJ d||kr|dkrg S t}n|dk rg 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   mpfr   r   append)	r   rH   r\   dtbopresultirj   r   r   r   arange   sF     
"zStandardBaseContext.arangec                    s   t |dkr6| |d  | |d }t|d }nPt |dkrvt|d dsTJ |d j |d j}t|d }ntdt | |dk rtdd|v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')]

        rn   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   rv   r\   stepr   r   ra   G  rF   z0StandardBaseContext.linspace.<locals>.<listcomp>c                    s   g | ]}|   qS r   r   rx   ry   r   r   ra   K  rF   )	ro   rp   intr-   r\   rs   rc   r*   r   )r   rH   r7   rs   rW   r:   r   ry   r   linspace   s.    


zStandardBaseContext.linspacec                 K   s$   | j |fi || j|fi |fS r   )cossinr   zr7   r   r   r   cos_sinN  s    zStandardBaseContext.cos_sinc                 K   s$   | j |fi || j|fi |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      ?rf   )r|   )r   pr   r   r   _default_hyper_maxprecT  s    z*StandardBaseContext._default_hyper_maxprecr   c                 C   s   | j }zd}|| d | _ | j}| j}d}| D ]P}||7 }|| sx|rx| |}	t||	}| |}
|
|	 | j krx q|d7 }q0||
 }||krq||k s| jrq|t| j |7 }q|W || _ S || _ 0 d S N
      r   r   )rh   ninfr2   magrb   _fixed_precisionmin)r   Zterms
check_steprh   	extraprecmax_magri   ktermterm_magsum_magcancellationr   r   r   sum_accuratelya  s4    




z"StandardBaseContext.sum_accuratelyc                 C   s   | j }zd}|| d | _ | j}| j}|}d}| D ]V}	||	9 }|	| }
|| s| |
}t||}| || }| | j kr q|d7 }q4|| }||krq||k s| jrq|t| j |7 }q|W || _ S || _ 0 d S r   )rh   r   rR   r   rb   r   r   )r   Zfactorsr   rh   r   r   rR   ri   r   Z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   rW   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)rH   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 rB| \}}||krB|
 S | i |}||f|< |S r   )tupler   rh   )rH   r7   keyrh   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)rV   )N)NN)r   )r   )4r   r   r   r   r   ZComplexResultr   r%   r   verboser)   r+   r0   r3   r4   r8   r;   r<   r=   r>   rJ   rQ   rU   rX   r[   rm   rw   r}   r   r   r   staticmethodgcdZ_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>   