a
    <bb                     @   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 Zedddfd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 n e|y   Y n0 d2S )3   )
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 d S N)NotImplementedError)clsval ra   d/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/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   s*  | j j\}}|r<|d|}d|v r0t|d }|d|}t|| u r|j\}}}}|sb|rb|S t| }	t|||||||	_|	S t|tu rt	|dkrt| }	t
|d |d |||	_|	S t	|dkr|\}}}}t| }	t|t||||||	_|	S tn$t| }	t| ||||||	_|	S d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      N)context_prec_roundinggetr   typerj   newr   tuplelenr   r   
ValueErrorr"   mpf_convert_arg)
r_   r`   kwargsrk   rm   signmanexpbcvra   ra   rb   rc   /   s6    z_mpf.__new__c                 C   s   t |trt|S t |tr$t|S t |tr:t|||S t || jjrT|	||S t
|drd|jS t
|dr| j|||}t
|dr|jS t
|dr|j\}}||kr|S tdtdt| d S )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   P   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	r`|j
\}}t||| jjS t|drp|jS t|dr| j|j| jj }t|dr|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_rhsb   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_lhsr   s    
z_mpf.mpf_convert_lhsc                 C   s   | j dd S )Nr      rj   selfra   ra   rb   <lambda>y       z_mpf.<lambda>c                 C   s
   | j d S Nr   r   r   ra   ra   rb   r   z   r   c                 C   s
   | j d S )Nrn   r   r   ra   ra   rb   r   {   r   c                 C   s
   | j d S )Nr   r   r   ra   ra   rb   r   |   r   c                 C   s   | S r]   ra   r   ra   ra   rb   r   ~   r   c                 C   s   | j jS r]   )rq   Zzeror   ra   ra   rb   r      r   c                 C   s   | S r]   ra   r   ra   ra   rb   r      r   c                 C   s
   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   Z_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__   r   z_mpf.__str__c                 C   s
   t | jS r]   )r2   rj   r   ra   ra   rb   __hash__   r   z_mpf.__hash__c                 C   s   t t| jS r]   )intr   rj   r   ra   ra   rb   __int__   r   z_mpf.__int__c                 C   s   t t| jS r]   )longr   rj   r   ra   ra   rb   __long__   r   z_mpf.__long__c                 C   s   t | j| jjd dS Nr   )rnd)r   rj   rq   rr   r   ra   ra   rb   	__float__   r   z_mpf.__float__c                 C   s   t t| S r]   )complexr   r   ra   ra   rb   __complex__   r   z_mpf.__complex__c                 C   s
   | j tkS r]   )rj   r   r   ra   ra   rb   __nonzero__   r   z_mpf.__nonzero__c                 C   s,   | j \}}\}}||}t| j|||_|S r]   )_ctxdatar!   rj   r   r_   ru   rk   rm   r   ra   ra   rb   __abs__   s    z_mpf.__abs__c                 C   s,   | j \}}\}}||}t| j|||_|S r]   )r   r"   rj   r   ra   ra   rb   __pos__   s    z_mpf.__pos__c                 C   s,   | j \}}\}}||}t| j|||_|S r]   )r   r#   rj   r   ra   ra   rb   __neg__   s    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   s   |  |tS r]   )r   r-   r   r   ra   ra   rb   __cmp__   r   z_mpf.__cmp__c                 C   s   |  |tS r]   )r   r.   r   ra   ra   rb   __lt__   r   z_mpf.__lt__c                 C   s   |  |tS r]   )r   r/   r   ra   ra   rb   __gt__   r   z_mpf.__gt__c                 C   s   |  |tS r]   )r   r0   r   ra   ra   rb   __le__   r   z_mpf.__le__c                 C   s   |  |tS r]   )r   r1   r   ra   ra   rb   __ge__   r   z_mpf.__ge__c                 C   s   |  |}|tu r|S | S r]   __eq__r   )r   r   r   ra   ra   rb   __ne__   s    
z_mpf.__ne__c                 C   s\   | j \}}\}}t|tv r>||}tt|| j|||_|S | |}|tu rT|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r8||}t|| j|||_|S | |}|tu rN|S ||  S r]   )r   r   r   r)   rj   r   r   r   ra   ra   rb   __rdiv__   s    

z_mpf.__rdiv__c                 C   s   |  |}|tu r|S ||  S r]   r   r   r   ra   ra   rb   __rpow__   s    
z_mpf.__rpow__c                 C   s   |  |}|tu r|S ||  S r]   r   r   ra   ra   rb   __rmod__   s    
z_mpf.__rmod__c                 C   s   | j | S r]   )rq   sqrtr   ra   ra   rb   r      s    z	_mpf.sqrtNc                 C   s   | j | |||S r]   rq   Zalmosteqr   r   Zrel_epsZabs_epsra   ra   rb   ae   s    z_mpf.aec                 C   s   t | j|S r]   )r6   rj   )r   rk   ra   ra   rb   r6      s    z_mpf.to_fixedc                 G   s   t t| g|R  S r]   )roundr   )r   argsra   ra   rb   	__round__   s    z_mpf.__round__)NN)0rd   re   rf   rg   rh   r   rc   classmethodry   r   r   propertyZ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   '   sZ   !


	
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)nameZwith_mpfZwith_intZ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   Zdocnamer   ra   ra   rb   rc   N  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   Zprec2Z	rounding2ra   ra   rb   __call__U  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   Znstrr   ra   ra   rb   r   a  s    z_constant.__repr__)r   )NNN)	rd   re   rf   rg   rc   r   r   rj   r   ra   ra   ra   rb   r   H  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   n  s    


z_mpc.__new__c                 C   s   | j | jd S )Nro   rq   r   r   r   ra   ra   rb   r   z  r   z_mpc.<lambda>c                 C   s   | j | jd S 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   }  s    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     s    z_mpc.__str__c                 C   s   t | j| jjd dS r   )r8   r   rq   rr   r   ra   ra   rb   r     s    z_mpc.__complex__c                 C   s,   | j \}}\}}||}t| j|||_|S r]   )r   r:   r   r   ra   ra   rb   r     s    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   s,   | j \}}\}}||}t| j|||_|S r]   )r   r<   r   r   ra   ra   rb   r     s    z_mpc.__neg__c                 C   s,   | j \}}\}}||}t| j|||_|S r]   )r   r=   r   r   ra   ra   rb   r     s    z_mpc.conjugatec                 C   s
   t | jS r]   )r;   r   r   ra   ra   rb   r     s    z_mpc.__nonzero__c                 C   s
   t | jS r]   )r9   r   r   ra   ra   rb   r     s    z_mpc.__hash__c                 C   s.   z| j |}|W S  ty(   t Y S 0 d S 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oD| j|jkS )Nr   F)r   r   r   r   r   r   r   r   ra   ra   rb   r     s    


z_mpc.__eq__c                 C   s   |  |}|tu r|S | S r]   r   )r   r   r   ra   ra   rb   r     s    
z_mpc.__ne__c                  G   s   t dd S )Nz3no ordering relation is defined for complex numbers)r   )r   ra   ra   rb   _compare  s    z_mpc._comparec                 C   sz   | j \}}\}}t|dsZ| |}|tu r0|S t|drZ||}t| j|j|||_|S ||}t| j|j|||_|S Nr   rj   )r   r   r   r   r@   r   rj   r?   r   ra   ra   rb   r     s    


z_mpc.__add__c                 C   sz   | j \}}\}}t|dsZ| |}|tu r0|S t|drZ||}t| j|j|||_|S ||}t| j|j|||_|S r   )r   r   r   r   rB   r   rj   rA   r   ra   ra   rb   r     s    


z_mpc.__sub__c                 C   s   | j \}}\}}t|dst|trB||}t| j||||_|S | |}|tu rX|S t|dr||}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   sz   | j \}}\}}t|dsZ| |}|tu r0|S t|drZ||}t| j|j|||_|S ||}t| j|j|||_|S r   )r   r   r   r   rG   r   rj   rF   r   ra   ra   rb   r     s    


z_mpc.__div__c                 C   s   | j \}}\}}t|tr8||}t| j||||_|S | |}|tu rN|S ||}t|drvt| j|j	|||_n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     s    
z_mpc.__rsub__c                 C   sV   | j \}}\}}t|tr8||}t| j||||_|S | |}|tu rN|S ||  S r]   )r   r   r   rE   r   r   r   r   ra   ra   rb   __rmul__$  s    

z_mpc.__rmul__c                 C   s   |  |}|tu r|S ||  S r]   r   r   ra   ra   rb   r   /  s    
z_mpc.__rdiv__c                 C   s   |  |}|tu r|S ||  S r]   r   r   ra   ra   rb   r   5  s    
z_mpc.__rpow__Nc                 C   s   | j | |||S r]   r   r   ra   ra   rb   r   >  s    z_mpc.ae)ro   ro   )NN)*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   e  sN   

	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__G  s    
zPythonMPContext.__init__c                 C   s   t | j}||_|S r]   )ru   r   rj   r   r   r   ra   ra   rb   r   S  s    
zPythonMPContext.make_mpfc                 C   s   t | j}||_|S r]   )ru   r   r   r  ra   ra   rb   make_mpcX  s    
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_precb  s    zPythonMPContext._set_precc                 C   s(   t | | _| jd< tdt|| _d S r   )r   r  rr   r  r   r  r  ra   ra   rb   _set_dpsf  s    zPythonMPContext._set_dpsc                 C   s   | j S r]   )r  r   ra   ra   rb   r   j  r   zPythonMPContext.<lambda>c                 C   s   | j S r]   )r  r   ra   ra   rb   r   k  r   Tc                 C   s  t || jv r|S t|tr*| t|S t|trB| t|S t|trf| 	t|j
t|jfS t |jdkr~| |S t|tjrztt|jt|j}W n   Y n0 | j\}}t|tjr|j\}}| t|||S |r&t|tr&zt|||}| |W S  ty$   Y n0 t|dr>| |jS t|drV| 	|jS t|drt| |||S t |jdkrz| t |||W S    Y n0 | !||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   Z_convert_fallback)r   r   stringsrk   rm   r   r   rj   ra   ra   rb   r   m  s6    


zPythonMPContext.convertc                 C   sz   ddl }t||jr&| tt|S t||jr@| t|S t||jrf| 	t|j
t|jfS tdt| dS )z^
        Converts *x* to an ``mpf`` or ``mpc``. *x* should be a numpy
        scalar.
        ro   Nr   )r  r   integerr   r   r   Zfloatingr   Zcomplexfloatingr  r   r   r   r   )r   r   r   ra   ra   rb   r    s    zPythonMPContext.npconvertc                 C   sv   t |dr|jtkS t |dr(t|jv S t|ts>t|tjrBdS | |}t |ds`t |drj| 	|S t
ddS )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 inputN)r   rj   r    r   r   r   rZ   r   r   isnanr   )r   r   ra   ra   rb   r    s    





zPythonMPContext.isnanc                 C   s   t |dr|jttfv S t |drD|j\}}|ttfv pB|ttfv S t|tsZt|tjr^dS | 	|}t |ds|t |dr| 
|S tddS )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 inputN)r   rj   r   r   r   r   r   rZ   r   r   isinfr   )r   r   reimra   ra   rb   r    s    




zPythonMPContext.isinfc                 C   s   t |drt|jd S t |drd|j\}}t|d }t|d }|tkrP|S |tkr\|S |ob|S t|tszt|tjrt|S | 	|}t |dst |dr| 
|S tddS )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 inputN)r   boolrj   r   r   r   r   rZ   r   r   isnormalr   )r   r   r  r  Z	re_normalZ	im_normalra   ra   rb   r    s    




zPythonMPContext.isnormalFc                 C   s  t |trdS t|drB|j \}}}}}t|r8|dkp>|tkS t|dr|j\}}	|\}
}}}|	\}}}}|rz|dkp|tk}|r|r|dkp|	tk}|o|S |o|	tkS t |tjr|j	\}}|| dkS | 
|}t|dst|dr| ||S tddS )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 inputN)r   r   r   rj   r  r   r   rZ   r   r   r   isintr   )r   r   Zgaussianr{   r|   r}   r~   Zxvalr  r  ZrsignZrmanZrexpZrbcZisignZimanZiexpZibcZre_isintZim_isintr   r   ra   ra   rb   r    s*    





zPythonMPContext.isintc                 C   sv  | j \}}g }g }|D ]"}d }	}
t|dr6|j}	nLt|drL|j\}	}
n6| |}t|drh|j}	nt|dr~|j\}	}
nt|
r|r|r|t|	|	 |t|
|
 n,t|	|
fd|d \}	}
||	 ||
 n.|r|t	|	|
f| n||	 ||
 q|r"t|	|	}	n|r0t
|	}	||	 qt||||}|rh| |t|||f}n
| |}|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   ZtermsabsoluteZsquaredrk   r   r   r   ZtermZrevalZimvalr   ra   ra   rb   fsum>  sJ    









zPythonMPContext.fsumNc                 C   s  |durt ||}| j\}}g }g }t}| j| jf}	|D ]v\}
}t|
|	vrX| |
}
t||	vrn| |}||
d}||d}|r|r|t|
j	|j	 q8||
d}||d}|r|r|
j	}|j
\}}|rt|}|t|| |t|| q8|r8|r8|
j
\}}|j	}|t|| |t|| q8|r|r|
j
\}}|j
\}}|rft|}|t|| |tt|| |t|| |t|| q8tq8t|||}|r| |t|||f}n
| |}|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   Zhasattr_r  r   r   Za_realZb_realZ	a_complexZ	b_complexZavalZbreZbimZareZaimZbvalr   ra   ra   rb   fdotz  sX    '










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\}}|rR|d|}d|v rFt|d }|d|}t| drz | j||W S  t	y    j
r  | jtf|| Y S 0 nt| drƈ | j||S tdt | f d S )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   r(  r'  Zmpi_fdocr)  ra   r&  rb   _wrap_libmp_function  s    
z$PythonMPContext._wrap_libmp_functionc                    s8   |r fdd}n }t j| j|_t| || d S )Nc                    s\   | j   fdd|D }| j}z.|  jd7  _| g|R i |}W || _n|| _0 |
 S )Nc                    s   g | ]} |qS ra   ra   ).0r   r   ra   rb   
<listcomp>  r   zDPythonMPContext._wrap_specfun.<locals>.f_wrapped.<locals>.<listcomp>r  )r   rk   )r   r   rz   rk   retvalr)  r.  rb   	f_wrapped  s    z0PythonMPContext._wrap_specfun.<locals>.f_wrapped)r[   r*  rs   rg   setattr)r_   r   r)  wrapr2  ra   r1  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}nt|tv rRt|dfS d }t|trj|\}}nFt |dr|j	\}}n0t|t
rd|v r|d\}}t|}t|}|d ur|| s|| 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|rT| }|d
krnt||> dfS |d	krt|d| >  }}| ||dfS | |}|dfS |sdS |dfS d S )Nr   Crj   Zr   /QUro   r   R)ro   r7  )r   r   r   rj   rt   r   r   r   rv   r   r   splitr   r   r   )
r   r   r   r  r   r   r{   r|   r}   r~   ra   ra   rb   _convert_param  sX    













zPythonMPContext._convert_paramc                 C   sB   |\}}}}|r|| S |t kr&| jS |tks6|tkr<| jS | jS r]   )r   ninfr   r   infnan)r   r   r{   r|   r}   r~   ra   ra   rb   _mpf_mag7  s    zPythonMPContext._mpf_magc                 C   s   t |dr| |jS t |drh|j\}}|tkr<| |S |tkrN| |S dt| || | S t|tr|rtt	|S | j
S t|tjr|j\}}|rdtt	| t| S | j
S | |}t |dst |dr| |S tddS )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/mpcN)r   rB  rj   r   r   r  r   r   r5   absr?  rZ   r   r   r   magr   )r   r   r   r   r   r   ra   ra   rb   rD  A  s,    








zPythonMPContext.mag)T)F)FF)NF)NNr%  )rd   re   rf   r   r   r  r  r
  r  r   rk   rl   r   r  r  r  r  r  r   r$  r,  r   r5  r>  rB  rD  ra   ra   ra   rb   r   E  s,   
2 (
/
<
W
#
1
r   ro   N)}Zlibmp.backendr   r   Z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   Z
return_mpfZ
return_mpcZ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    e D

 ^    5