o
    lhq>                     @   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   c/var/www/html/construction_image-detection-poc/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``.
        Nd   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@    