o
    lh                     @   s  d dl mZmZ d dlmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZ d dlZm[Z[ d dlZm\Z\ e]j^Z_G dd de]Z`G dd de`Zad	Zbd
ZcdZddecedf Zed3ddZfefddddea_gefddec dec ded ea_hefddec dec ded ea_iefddec dec ded ea_jefd d!ec d"ec d#ed ea_kefd$d%ec d&ec d'ea_lefd(eed)ec d*ed ea_meajhea_neajjea_oeajkea_peajqea_rG d+d, d,eaZsG d-d. d.e`ZteuetfZvG d/d0 d0e]Zwzd1d2lxZxexjyzet exj{zea W d2S  e|y   Y d2S w )4   )
basestringexec_)WMPZMPZ_ZEROMPZ_ONE	int_typesrepr_dpsround_floorround_ceilingdps_to_precround_nearestprec_to_dpsComplexResultto_pickablefrom_pickable	normalizefrom_int
from_floatfrom_npfloatfrom_Decimalfrom_strto_intto_floatto_strfrom_rationalfrom_man_expfonefzerofinffninffnanmpf_absmpf_posmpf_negmpf_addmpf_submpf_mulmpf_mul_intmpf_divmpf_rdiv_intmpf_pow_intmpf_modmpf_eqmpf_cmpmpf_ltmpf_gtmpf_lempf_gempf_hashmpf_randmpf_sumbitcountto_fixed
mpc_to_strmpc_to_complexmpc_hashmpc_posmpc_is_nonzerompc_negmpc_conjugatempc_absmpc_addmpc_add_mpfmpc_submpc_sub_mpfmpc_mulmpc_mul_mpfmpc_mul_intmpc_divmpc_div_mpfmpc_powmpc_pow_mpfmpc_pow_intmpc_mpf_divmpf_powmpf_pi
mpf_degreempf_empf_phimpf_ln2mpf_ln10	mpf_eulermpf_catalan	mpf_aperympf_khinchinmpf_glaishermpf_twinprimempf_mertensr   )rational)function_docsc                   @   s   e Zd ZdZg Zdd ZdS )	mpnumericzBase class for mpf and mpc.c                 C   s   t N)NotImplementedError)clsval ra   h/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/mpmath/ctx_mp_python.py__new__$   s   zmpnumeric.__new__N)__name__
__module____qualname____doc__	__slots__rc   ra   ra   ra   rb   r\   !   s    r\   c                   @   s|  e Zd ZdZdgZefddZedd Zedd Z	ed	d
 Z
edd Zedd Zedd Zedd Zedd Ze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$ Zd%d& ZeZd'd( Zd)d* Zd+d, Z d-d. Z!d/d0 Z"d1d2 Z#d3d4 Z$d5d6 Z%d7d8 Z&d9d: Z'd;d< Z(d=d> Z)d?d@ Z*dAdB Z+dCdD Z,dLdFdGZ-dHdI Z.dJdK Z/dES )M_mpfz
    An mpf instance holds a real-valued floating-point number. mpf:s
    work analogously to Python floats, but support arbitrary-precision
    arithmetic.
    _mpf_c           
      K   s4  | j j\}}|r|d|}d|v rt|d }|d|}t|| u rA|j\}}}}|s1|r1|S t| }	t|||||||	_|	S t|tu rt	|dkr_t| }	t
|d |d |||	_|	S t	|dkr|tttfvr}|\}}}}t|t|||||}t| }	||	_|	S tt| }	t| ||||||	_|	S )zA new mpf can be created from a Python float, an int, a
        or a decimal string representing a number in floating-point
        format.precdpsrounding       r      )context_prec_roundinggetr   typerj   newr   tuplelenr   r   r   r    r   
ValueErrorr"   mpf_convert_arg)
r_   r`   kwargsrk   rm   signmanexpbcvra   ra   rb   rc   /   s:   z_mpf.__new__c                 C   s   t |tr	t|S t |trt|S t |trt|||S t || jjr*|	||S t
|dr2|jS t
|drI| j|||}t
|drI|jS t
|dr]|j\}}||krY|S tdtdt| )Nrj   _mpmath__mpi_z,can only create mpf from zero-width intervalcannot create mpf from )
isinstancer   r   floatr   r   r   rq   constantfunchasattrrj   convertr   r   rx   	TypeErrorrepr)r_   xrk   rm   tabra   ra   rb   ry   R   s   



z_mpf.mpf_convert_argc                 C   s   t |tr	t|S t |trt|S t |tr| j|S t |tj	r0|j
\}}t||| jjS t|dr8|jS t|drR| j|j| jj }t|drP|jS |S tS )Nrj   r   )r   r   r   r   r   complex_typesrq   mpcrZ   mpq_mpq_r   rk   r   rj   r   r   rr   NotImplemented)r_   r   pqr   ra   ra   rb   mpf_convert_rhsd   s   


z_mpf.mpf_convert_rhsc                 C   s&   |  |}t|tu r| j|S |S r]   )r   rt   rv   rq   make_mpf)r_   r   ra   ra   rb   mpf_convert_lhst   s   
z_mpf.mpf_convert_lhsc                 C   s   | j dd S )Nr      rj   selfra   ra   rb   <lambda>{       z_mpf.<lambda>c                 C   
   | j d S Nr   r   r   ra   ra   rb   r   |      
 c                 C   r   )Nrn   r   r   ra   ra   rb   r   }   r   c                 C   r   )Nr   r   r   ra   ra   rb   r   ~   r   c                 C      | S r]   ra   r   ra   ra   rb   r          c                 C   s   | j jS r]   )rq   zeror   ra   ra   rb   r      s    c                 C   r   r]   ra   r   ra   ra   rb   r      r   c                 C   
   t | jS r]   )r   rj   r   ra   ra   rb   __getstate__   r   z_mpf.__getstate__c                 C   s   t || _d S r]   )r   rj   r   r`   ra   ra   rb   __setstate__   r   z_mpf.__setstate__c                 C   s$   | j jrt| S dt| j| j j S )Nz	mpf('%s'))rq   prettystrr   rj   _repr_digitssra   ra   rb   __repr__   s   z_mpf.__repr__c                 C   s   t | j| jjS r]   )r   rj   rq   _str_digitsr   ra   ra   rb   __str__   s    z_mpf.__str__c                 C   r   r]   )r2   rj   r   ra   ra   rb   __hash__   r   z_mpf.__hash__c                 C      t t| jS r]   )intr   rj   r   ra   ra   rb   __int__   r   z_mpf.__int__c                 C   r   r]   )longr   rj   r   ra   ra   rb   __long__   r   z_mpf.__long__c                 C      t | j| jjd dS Nr   )rnd)r   rj   rq   rr   r   ra   ra   rb   	__float__   s    z_mpf.__float__c                 C   s   t t| S r]   )complexr   r   ra   ra   rb   __complex__       z_mpf.__complex__c                 C   s
   | j tkS r]   )rj   r   r   ra   ra   rb   __nonzero__   r   z_mpf.__nonzero__c                 C   ,   | j \}}\}}||}t| j|||_|S r]   )_ctxdatar!   rj   r   r_   ru   rk   rm   r   ra   ra   rb   __abs__      z_mpf.__abs__c                 C   r   r]   )r   r"   rj   r   ra   ra   rb   __pos__   r   z_mpf.__pos__c                 C   r   r]   )r   r#   rj   r   ra   ra   rb   __neg__   r   z_mpf.__neg__c                 C   s4   t |dr	|j}n| |}|tu r|S || j|S Nrj   )r   rj   r   r   )r   r   r   ra   ra   rb   _cmp   s   

z	_mpf._cmpc                 C      |  |tS r]   )r   r-   r   r   ra   ra   rb   __cmp__   r   z_mpf.__cmp__c                 C   r   r]   )r   r.   r   ra   ra   rb   __lt__   r   z_mpf.__lt__c                 C   r   r]   )r   r/   r   ra   ra   rb   __gt__   r   z_mpf.__gt__c                 C   r   r]   )r   r0   r   ra   ra   rb   __le__   r   z_mpf.__le__c                 C   r   r]   )r   r1   r   ra   ra   rb   __ge__   r   z_mpf.__ge__c                 C      |  |}|tu r|S | S r]   __eq__r   )r   r   r   ra   ra   rb   __ne__      
z_mpf.__ne__c                 C   s\   | j \}}\}}t|tv r||}tt|| j|||_|S | |}|tu r*|S ||  S r]   )r   rt   r   r%   r   rj   r   r   r   r   r_   ru   rk   rm   r   ra   ra   rb   __rsub__   s   
z_mpf.__rsub__c                 C   sV   | j \}}\}}t|tr||}t|| j|||_|S | |}|tu r'|S ||  S r]   )r   r   r   r)   rj   r   r   r   ra   ra   rb   __rdiv__      

z_mpf.__rdiv__c                 C      |  |}|tu r|S ||  S r]   r   r   r   ra   ra   rb   __rpow__      
z_mpf.__rpow__c                 C   s   |  |}|tu r|S ||  S r]   r   r   ra   ra   rb   __rmod__   r   z_mpf.__rmod__c                 C   s   | j | S r]   )rq   sqrtr   ra   ra   rb   r         z	_mpf.sqrtNc                 C      | j | |||S r]   rq   almosteqr   r   rel_epsabs_epsra   ra   rb   ae      z_mpf.aec                 C   s   t | j|S r]   )r6   rj   )r   rk   ra   ra   rb   r6      r   z_mpf.to_fixedc                 G   s   t t| g|R  S r]   )roundr   )r   argsra   ra   rb   	__round__      z_mpf.__round__NN)0rd   re   rf   rg   rh   r   rc   classmethodry   r   r   propertyman_expr|   r}   r~   realimag	conjugater   r   r   r   r   r   r   r   r   r   __bool__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r   ra   ra   ra   rb   ri   '   s\    #


	
ri   a  
def %NAME%(self, other):
    mpf, new, (prec, rounding) = self._ctxdata
    sval = self._mpf_
    if hasattr(other, '_mpf_'):
        tval = other._mpf_
        %WITH_MPF%
    ttype = type(other)
    if ttype in int_types:
        %WITH_INT%
    elif ttype is float:
        tval = from_float(other)
        %WITH_MPF%
    elif hasattr(other, '_mpc_'):
        tval = other._mpc_
        mpc = type(other)
        %WITH_MPC%
    elif ttype is complex:
        tval = from_float(other.real), from_float(other.imag)
        mpc = self.context.mpc
        %WITH_MPC%
    if isinstance(other, mpnumeric):
        return NotImplemented
    try:
        other = mpf.context.convert(other, strings=False)
    except TypeError:
        return NotImplemented
    return self.%NAME%(other)
z-; obj = new(mpf); obj._mpf_ = val; return objz-; obj = new(mpc); obj._mpc_ = val; return obja  
        try:
            val = mpf_pow(sval, tval, prec, rounding) %s
        except ComplexResult:
            if mpf.context.trap_complex:
                raise
            mpc = mpf.context.mpc
            val = mpc_pow((sval, fzero), (tval, fzero), prec, rounding) %s
 c                 C   sN   t }|d|}|d|}|d|}|d| }i }t|t | ||  S )Nz
%WITH_INT%z
%WITH_MPC%z
%WITH_MPF%z%NAME%)mpf_binary_opreplacer   globals)namewith_mpfwith_intwith_mpccodenpra   ra   rb   	binary_op  s   r   r   zreturn mpf_eq(sval, tval)z$return mpf_eq(sval, from_int(other))z3return (tval[1] == fzero) and mpf_eq(tval[0], sval)__add__z)val = mpf_add(sval, tval, prec, rounding)z4val = mpf_add(sval, from_int(other), prec, rounding)z-val = mpc_add_mpf(tval, sval, prec, rounding)__sub__z)val = mpf_sub(sval, tval, prec, rounding)z4val = mpf_sub(sval, from_int(other), prec, rounding)z2val = mpc_sub((sval, fzero), tval, prec, rounding)__mul__z)val = mpf_mul(sval, tval, prec, rounding)z.val = mpf_mul_int(sval, other, prec, rounding)z-val = mpc_mul_mpf(tval, sval, prec, rounding)__div__z)val = mpf_div(sval, tval, prec, rounding)z4val = mpf_div(sval, from_int(other), prec, rounding)z-val = mpc_mpf_div(sval, tval, prec, rounding)__mod__z)val = mpf_mod(sval, tval, prec, rounding)z4val = mpf_mod(sval, from_int(other), prec, rounding)z+raise NotImplementedError("complex modulo")__pow__z.val = mpf_pow_int(sval, other, prec, rounding)z2val = mpc_pow((sval, fzero), tval, prec, rounding)c                   @   s8   e Zd ZdZdddZdddZedd	 Zd
d ZdS )	_constantzRepresents a mathematical constant with dynamic precision.
    When printed or used in an arithmetic operation, a constant
    is converted to a regular mpf at the working precision. A
    regular mpf can also be obtained using the operation +x.r   c                 C   s(   t | }||_||_tt|d|_|S )Nr   )objectrc   r   r   getattrr[   rg   )r_   r   r   docnamer   ra   ra   rb   rc   P  s
   
z_constant.__new__Nc                 C   s<   | j j\}}|s
|}|s|}|rt|}| j | ||S r]   )rq   rr   r   r   r   )r   rk   rl   rm   prec2	rounding2ra   ra   rb   __call__W  s
   z_constant.__call__c                 C   s   | j j\}}| ||S r]   )rq   rr   r   )r   rk   rm   ra   ra   rb   rj   ^  s   z_constant._mpf_c                 C   s   d| j | j| ddf S )Nz	<%s: %s~>   )rl   )r   rq   nstrr   ra   ra   rb   r   c     z_constant.__repr__)r   )NNN)	rd   re   rf   rg   rc   r
  r   rj   r   ra   ra   ra   rb   r  J  s    


r  c                   @   s&  e Zd ZdZdgZd<ddZedd Ze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 ZeZdd Zedd  Zd!d" Zd#d$ Zd%d& ZeZeZeZeZd'd( Zd)d* Zd+d, Zd-d. Z d/d0 Z!eZ"d1d2 Z#d3d4 Z$d5d6 Z%d7d8 Z&e Z'e%Z(d=d:d;Z)d9S )>_mpcz
    An mpc represents a complex number using a pair of mpf:s (one
    for the real part and another for the imaginary part.) The mpc
    class behaves fairly similarly to Python's complex type.
    _mpc_ro   c                 C   sd   t | }t|tr|j|j}}nt|dr|j|_|S | j	|}| j	|}|j
|j
f|_|S )Nr  )r  rc   r   r   r   r   r   r  rq   mpfrj   )r_   r   r   r   ra   ra   rb   rc   p  s   


z_mpc.__new__c                 C      | j | jd S )Nro   rq   r   r  r   ra   ra   rb   r   |      z_mpc.<lambda>c                 C   r  r   r  r   ra   ra   rb   r   }  r  c                 C   s   t | jd t | jd fS Nro   r   )r   r  r   ra   ra   rb   r     r  z_mpc.__getstate__c                 C   s   t |d t |d f| _d S r  )r   r  r   ra   ra   rb   r     s   z_mpc.__setstate__c                 C   sH   | j jrt| S t| jdd }t| jdd }dt| j||f S )Nrp   z%s(real=%s, imag=%s))rq   r   r   r   r   r   rt   rd   )r   rira   ra   rb   r     s
   z_mpc.__repr__c                 C   s   dt | j| jj S )Nz(%s))r7   r  rq   r   r   ra   ra   rb   r     r   z_mpc.__str__c                 C   r   r   )r8   r  rq   rr   r   ra   ra   rb   r     s   z_mpc.__complex__c                 C   r   r]   )r   r:   r  r   ra   ra   rb   r     r   z_mpc.__pos__c                 C   s,   | j j\}}t| j j}t| j|||_|S r]   )rq   rr   ru   r  r>   r  rj   )r   rk   rm   r   ra   ra   rb   r     s   z_mpc.__abs__c                 C   r   r]   )r   r<   r  r   ra   ra   rb   r     r   z_mpc.__neg__c                 C   r   r]   )r   r=   r  r   ra   ra   rb   r     r   z_mpc.conjugatec                 C   r   r]   )r;   r  r   ra   ra   rb   r        
z_mpc.__nonzero__c                 C   r   r]   )r9   r  r   ra   ra   rb   r     r  z_mpc.__hash__c                 C   s*   z	| j |}|W S  ty   t Y S w r]   )rq   r   r   r   )r_   r   yra   ra   rb   mpc_convert_lhs  s   z_mpc.mpc_convert_lhsc                 C   sF   t |dst|trdS | |}|tu r|S | j|jko"| j|jkS )Nr  F)r   r   r   r  r   r   r   r   ra   ra   rb   r     s   


z_mpc.__eq__c                 C   r   r]   r   )r   r   r   ra   ra   rb   r     r   z_mpc.__ne__c                  G   s   t d)Nz3no ordering relation is defined for complex numbers)r   )r   ra   ra   rb   _compare  s   z_mpc._comparec                 C   z   | j \}}\}}t|ds-| |}|tu r|S t|dr-||}t| j|j|||_|S ||}t| j|j|||_|S Nr  rj   )r   r   r  r   r@   r  rj   r?   r   ra   ra   rb   r        


z_mpc.__add__c                 C   r  r  )r   r   r  r   rB   r  rj   rA   r   ra   ra   rb   r     r  z_mpc.__sub__c                 C   s   | j \}}\}}t|dsFt|tr!||}t| j||||_|S | |}|tu r,|S t|drA||}t| j|j	|||_|S | |}||}t
| j|j|||_|S r  )r   r   r   r   rE   r  r  r   rD   rj   rC   r   ra   ra   rb   r     s"   




z_mpc.__mul__c                 C   r  r  )r   r   r  r   rG   r  rj   rF   r   ra   ra   rb   r     r  z_mpc.__div__c                 C   s   | j \}}\}}t|tr||}t| j||||_|S | |}|tu r'|S ||}t|dr<t| j|j	|||_|S t
| j|j|||_|S r   )r   r   r   rJ   r  r  r   r   rI   rj   rH   r   ra   ra   rb   r    s   


z_mpc.__pow__c                 C   s   |  |}|tu r|S ||  S r]   r  r   r   ra   ra   rb   r      r   z_mpc.__rsub__c                 C   sV   | j \}}\}}t|tr||}t| j||||_|S | |}|tu r'|S ||  S r]   )r   r   r   rE   r  r  r   r   ra   ra   rb   __rmul__&  r   z_mpc.__rmul__c                 C   s   |  |}|tu r|S ||  S r]   r  r   ra   ra   rb   r   1  r   z_mpc.__rdiv__c                 C   r   r]   r  r   ra   ra   rb   r   7  r   z_mpc.__rpow__Nc                 C   r   r]   r   r   ra   ra   rb   r   @  r   z_mpc.ae)ro   ro   r   )*rd   re   rf   rg   rh   rc   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r  r   r   r   r   r   r   r  r  __radd__r   r   r   r   __truediv____rtruediv__r   ra   ra   ra   rb   r  g  sP    

	r  c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	dd eZ
e	dd eZd.ddZdd Zdd Zdd Zdd Zd/ddZd0ddZd1d!d"Zd2d$d%Zed&d' Zd(d) Zd*d+ Zd,d- Zd S )3PythonMPContextc                 C   s   dt g| _tdtfi | _tdtfi | _| jt| jg| j_| jt| jg| j_| | j_	| | j_	tdt
fi | _| jt| jg| j_| | j_	d S )N5   r  r   r   )r   rr   rt   ri   r  r  r   ru   r   rq   r  r   ctxra   ra   rb   __init__I  s   
zPythonMPContext.__init__c                 C      t | j}||_|S r]   )ru   r  rj   r'  r   r   ra   ra   rb   r   U     
zPythonMPContext.make_mpfc                 C   r)  r]   )ru   r   r  r*  ra   ra   rb   make_mpcZ  r+  zPythonMPContext.make_mpcc                 C   s    d | _ | jd< d| _d| _d S )Nr%  ro   r  F)_precrr   _dpstrap_complexr&  ra   ra   rb   default_  s   
zPythonMPContext.defaultc                 C   s(   t dt| | _| jd< t|| _d S )Nr   ro   )maxr   r-  rr   r   r.  r'  nra   ra   rb   	_set_precd  s   zPythonMPContext._set_precc                 C   s(   t | | _| jd< tdt|| _d S r  )r   r-  rr   r1  r   r.  r2  ra   ra   rb   _set_dpsh  s   zPythonMPContext._set_dpsc                 C      | j S r]   )r-  r&  ra   ra   rb   r   l      zPythonMPContext.<lambda>c                 C   r6  r]   )r.  r&  ra   ra   rb   r   m  r7  Tc                 C   s  t || jv r	|S t|tr| t|S t|tr!| t|S t|tr3| 	t|j
t|jfS t |jdkr?| |S t|tjrXztt|jt|j}W n   Y | j\}}t|tjrq|j\}}| t|||S |rt|trzt|||}| |W S  ty   Y nw t|dr| |jS t|dr| 	|jS t|dr| |||S t |jdkrz
| t |||W S    Y | !||S )a  
        Converts *x* to an ``mpf`` or ``mpc``. If *x* is of type ``mpf``,
        ``mpc``, ``int``, ``float``, ``complex``, the conversion
        will be performed losslessly.

        If *x* is a string, the result will be rounded to the present
        working precision. Strings representing fractions or complex
        numbers are permitted.

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> mpmathify(3.5)
            mpf('3.5')
            >>> mpmathify('2.1')
            mpf('2.1000000000000001')
            >>> mpmathify('3/4')
            mpf('0.75')
            >>> mpmathify('2+3j')
            mpc(real='2.0', imag='3.0')

        numpyrj   r  r   decimal)"rt   typesr   r   r   r   r   r   r   r,  r   r   re   	npconvertnumbersRationalrZ   r   r   	numeratordenominatorrr   r   r   r   r   rx   r   rj   r  r   r   r   _convert_fallback)r'  r   stringsrk   rm   r   r   rj   ra   ra   rb   r   o  s8   



zPythonMPContext.convertc                 C   sv   ddl }t||jr| tt|S t||jr | t|S t||jr3| 	t|j
t|jfS tdt| )z^
        Converts *x* to an ``mpf`` or ``mpc``. *x* should be a numpy
        scalar.
        ro   Nr   )r8  r   integerr   r   r   floatingr   complexfloatingr,  r   r   r   r   )r'  r   r   ra   ra   rb   r;    s   zPythonMPContext.npconvertc                 C   sr   t |dr
|jtkS t |drt|jv S t|tst|tjr!dS | |}t |ds0t |dr5| 	|S t
d)a  
        Return *True* if *x* is a NaN (not-a-number), or for a complex
        number, whether either the real or complex part is NaN;
        otherwise return *False*::

            >>> from mpmath import *
            >>> isnan(3.14)
            False
            >>> isnan(nan)
            True
            >>> isnan(mpc(3.14,2.72))
            False
            >>> isnan(mpc(3.14,nan))
            True

        rj   r  Fzisnan() needs a number as input)r   rj   r    r  r   r   rZ   r   r   isnanr   )r'  r   ra   ra   rb   rE    s   





zPythonMPContext.isnanc                 C   s   t |dr|jttfv S t |dr"|j\}}|ttfv p!|ttfv S t|ts-t|tjr/dS | 	|}t |ds>t |drC| 
|S td)a  
        Return *True* if the absolute value of *x* is infinite;
        otherwise return *False*::

            >>> from mpmath import *
            >>> isinf(inf)
            True
            >>> isinf(-inf)
            True
            >>> isinf(3)
            False
            >>> isinf(3+4j)
            False
            >>> isinf(mpc(3,inf))
            True
            >>> isinf(mpc(inf,3))
            True

        rj   r  Fzisinf() needs a number as input)r   rj   r   r   r  r   r   rZ   r   r   isinfr   )r'  r   reimra   ra   rb   rF    s   




zPythonMPContext.isinfc                 C   s   t |drt|jd S t |dr2|j\}}t|d }t|d }|tkr(|S |tkr.|S |o1|S t|ts=t|tjrAt|S | 	|}t |dsPt |drU| 
|S td)a  
        Determine whether *x* is "normal" in the sense of floating-point
        representation; that is, return *False* if *x* is zero, an
        infinity or NaN; otherwise return *True*. By extension, a
        complex number *x* is considered "normal" if its magnitude is
        normal::

            >>> from mpmath import *
            >>> isnormal(3)
            True
            >>> isnormal(0)
            False
            >>> isnormal(inf); isnormal(-inf); isnormal(nan)
            False
            False
            False
            >>> isnormal(0+0j)
            False
            >>> isnormal(0+3j)
            True
            >>> isnormal(mpc(2,nan))
            False
        rj   r   r  z"isnormal() needs a number as input)r   boolrj   r  r   r   r   rZ   r   r   isnormalr   )r'  r   rG  rH  	re_normal	im_normalra   ra   rb   rJ    s   




zPythonMPContext.isnormalFc                 C   s  t |trdS t|dr!|j \}}}}}t|r|dkp|tkS t|drW|j\}}	|\}
}}}|	\}}}}|r=|dkp@|tk}|rQ|rI|dkpL|	tk}|oP|S |oV|	tkS t |tjrh|j	\}}|| dkS | 
|}t|dswt|dr}| ||S td)a
  
        Return *True* if *x* is integer-valued; otherwise return
        *False*::

            >>> from mpmath import *
            >>> isint(3)
            True
            >>> isint(mpf(3))
            True
            >>> isint(3.2)
            False
            >>> isint(inf)
            False

        Optionally, Gaussian integers can be checked for::

            >>> isint(3+0j)
            True
            >>> isint(3+2j)
            False
            >>> isint(3+2j, gaussian=True)
            True

        Trj   ro   r  zisint() needs a number as input)r   r   r   rj   rI  r   r  rZ   r   r   r   isintr   )r'  r   gaussianr{   r|   r}   r~   xvalrG  rH  rsignrmanrexprbcisignimaniexpibcre_isintim_isintr   r   ra   ra   rb   rM    s*   





zPythonMPContext.isintc                 C   sn  | j \}}g }g }|D ]}d }	}
t|dr|j}	n&t|dr%|j\}	}
n| |}t|dr3|j}	nt|dr>|j\}	}
nt|
r|rn|rW|t|	|	 |t|
|
 qt|	|
fd|d \}	}
||	 ||
 q|r{|t	|	|
f| q||	 ||
 q|rt|	|	}	n|rt
|	}	||	 qt||||}|r| |t|||f}|S | |}|S )aX  
        Calculates a sum containing a finite number of terms (for infinite
        series, see :func:`~mpmath.nsum`). The terms will be converted to
        mpmath numbers. For len(terms) > 2, this function is generally
        faster and produces more accurate results than the builtin
        Python function :func:`sum`.

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> fsum([1, 2, 0.5, 7])
            mpf('10.5')

        With squared=True each term is squared, and with absolute=True
        the absolute value of each term is used.
        ro   rj   r  rn   
   )rr   r   rj   r  r   r^   appendr&   rJ   r>   r!   r4   r,  r   )r'  termsabsolutesquaredrk   r   r   r   termrevalimvalr   ra   ra   rb   fsum@  sL   








zPythonMPContext.fsumNc                 C   s  |dur	t ||}| j\}}g }g }t}| j| jf}	|D ]\}
}t|
|	vr+| |
}
t||	vr6| |}||
d}||d}|rO|rO|t|
j	|j	 q||
d}||d}|r||r||
j	}|j
\}}|rkt|}|t|| |t|| q|r|r|
j
\}}|j	}|t|| |t|| q|r|r|
j
\}}|j
\}}|rt|}|t|| |tt|| |t|| |t|| qtt|||}|r| |t|||f}|S | |}|S )a.  
        Computes the dot product of the iterables `A` and `B`,

        .. math ::

            \sum_{k=0} A_k B_k.

        Alternatively, :func:`~mpmath.fdot` accepts a single iterable of pairs.
        In other words, ``fdot(A,B)`` and ``fdot(zip(A,B))`` are equivalent.
        The elements are automatically converted to mpmath numbers.

        With ``conjugate=True``, the elements in the second vector
        will be conjugated:

        .. math ::

            \sum_{k=0} A_k \overline{B_k}

        **Examples**

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> A = [2, 1.5, 3]
            >>> B = [1, -1, 2]
            >>> fdot(A, B)
            mpf('6.5')
            >>> list(zip(A, B))
            [(2, 1), (1.5, -1), (3, 2)]
            >>> fdot(_)
            mpf('6.5')
            >>> A = [2, 1.5, 3j]
            >>> B = [1+j, 3, -1-j]
            >>> fdot(A, B)
            mpc(real='9.5', imag='-1.0')
            >>> fdot(A, B, conjugate=True)
            mpc(real='3.5', imag='-5.0')

        Nrj   r  )ziprr   r   r  r   rt   r   r[  r&   rj   r  r#   r^   r4   r,  r   )r'  ABr   rk   r   r   r   hasattr_r:  r   r   a_realb_real	a_complex	b_complexavalbrebimareaimbvalr   ra   ra   rb   fdot|  sZ   '










zPythonMPContext.fdot<no doc>c                    s8    fdd}j dd tjd| |_|S )aO  
        Given a low-level mpf_ function, and optionally similar functions
        for mpc_ and mpi_, defines the function as a context method.

        It is assumed that the return type is the same as that of
        the input; the exception is that propagation from mpf to mpc is possible
        by raising ComplexResult.

        c              	      s   t |  jvr | }  j\}}|r)|d|}d|v r#t|d }|d|}t| drSz | j||W S  t	yR    j
rD  | jtf|| Y S w t| drb | j||S tdt | f )Nrk   rl   rm   rj   r  z
%s of a %s)rt   r:  r   rr   rs   r   r   r   rj   r   r/  r,  r   r  r^   )r   rz   rk   rm   r'  mpc_fmpf_fr   ra   rb   f  s&   



z/PythonMPContext._wrap_libmp_function.<locals>.frp   NzComputes the %s of x)rd   r[   __dict__rs   rg   )r'  ru  rt  mpi_fdocrv  ra   rs  rb   _wrap_libmp_function  s   
z$PythonMPContext._wrap_libmp_functionc                    s8   |r	 fdd}n }t j| j|_t| || d S )Nc                    sZ   | j   fdd|D }| j}z|  jd7  _| g|R i |}W || _|
 S || _w )Nc                    s   g | ]} |qS ra   ra   ).0r   r   ra   rb   
<listcomp>  s    zDPythonMPContext._wrap_specfun.<locals>.f_wrapped.<locals>.<listcomp>rZ  )r   rk   )r'  r   rz   rk   retvalrv  r|  rb   	f_wrapped  s   z0PythonMPContext._wrap_specfun.<locals>.f_wrapped)r[   rw  rs   rg   setattr)r_   r   rv  wrapr  ra   r  rb   _wrap_specfun  s
   zPythonMPContext._wrap_specfunc           
      C   s  t |dr|j\}}|tkr|dfS nt |dr|j}nvt|tv r(t|dfS d }t|tr4|\}}n#t |dr?|j	\}}nt|t
rWd|v rW|d\}}t|}t|}|d urm|| se|| dfS | ||dfS | |}t |dr|j\}}|tkr|dfS nt |dr|j}n|dfS |\}}}}	|r|d	kr|r| }|d
krt||> dfS |d	krt|d| > }}| ||dfS | |}|dfS |sdS |dfS )Nr  Crj   Zr   /QUro   r   R)ro   r  )r   r  r   rj   rt   r   r   r   rv   r   r   splitr   r   r   )
r'  r   r   rH  r   r   r{   r|   r}   r~   ra   ra   rb   _convert_param  s\   










zPythonMPContext._convert_paramc                 C   sB   |\}}}}|r|| S |t kr| jS |tks|tkr| jS | jS r]   )r   ninfr   r   infnan)r'  r   r{   r|   r}   r~   ra   ra   rb   _mpf_mag9  s   zPythonMPContext._mpf_magc                 C   s   t |dr| |jS t |dr4|j\}}|tkr| |S |tkr'| |S dt| || | S t|trD|rAtt	|S | j
S t|tjr`|j\}}|r]dtt	| t| S | j
S | |}t |dsot |drt| |S td)a  
        Quick logarithmic magnitude estimate of a number. Returns an
        integer or infinity `m` such that `|x| <= 2^m`. It is not
        guaranteed that `m` is an optimal bound, but it will never
        be too large by more than 2 (and probably not more than 1).

        **Examples**

            >>> from mpmath import *
            >>> mp.pretty = True
            >>> mag(10), mag(10.0), mag(mpf(10)), int(ceil(log(10,2)))
            (4, 4, 4, 4)
            >>> mag(10j), mag(10+10j)
            (4, 5)
            >>> mag(0.01), int(ceil(log(0.01,2)))
            (-6, -6)
            >>> mag(0), mag(inf), mag(-inf), mag(nan)
            (-inf, +inf, +inf, nan)

        rj   r  r   zrequires an mpf/mpc)r   r  rj   r  r   r1  r   r   r5   absr  rZ   r   r   r   magr   )r'  r   r  r  r   r   ra   ra   rb   r  C  s,   








zPythonMPContext.mag)T)F)FF)NF)NNrr  )rd   re   rf   r(  r   r,  r0  r4  r5  r   rk   rl   r   r;  rE  rF  rJ  rM  rb  rq  rz  r   r  r  r  r  ra   ra   ra   rb   r$  G  s.    
2 
(
/
<
W#
1
r$  ro   N)r   r   r   )}libmp.backendr   r   libmpr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r   rZ   r[   r  rc   ru   r\   ri   r   
return_mpf
return_mpcmpf_pow_samer   r   r   r   r   r  r  r  r!  r   r"  r   r#  r  r  r   r   r$  r<  ComplexregisterRealImportErrorra   ra   ra   rb   <module>   s   f  F


 ^    5