B
    n5[                 @   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]Zwy$d1d2lxZxexjyzet exj{zea W n e|k
r   Y nX 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 r`   3lib/python3.7/site-packages/mpmath/ctx_mp_python.py__new__$   s    zmpnumeric.__new__N)__name__
__module____qualname____doc__	__slots__rb   r`   r`   r`   ra   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|kr0t|d }|d|}t|| kr|j\}}}}|sb|rb|S t| }	t|||||||	_|	S t|tk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   typeri   newr   tuplelenr   r   
ValueErrorr"   mpf_convert_arg)
r^   r_   kwargsrj   rl   signmanexpbcvr`   r`   ra   rb   /   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 )Nri   _mpmath__mpi_z,can only create mpf from zero-width intervalzcannot create mpf from )
isinstancer   r   floatr   r   r   rp   constantfunchasattrri   convertr   r   rw   	TypeErrorrepr)r^   xrj   rl   tabr`   r`   ra   rx   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 )Nri   r   )r   r   r   r   r   complex_typesrp   mpcrZ   mpq_mpq_r   rj   r   ri   r   r   rq   NotImplemented)r^   r   pqr   r`   r`   ra   mpf_convert_rhsb   s"    
 
 
 

 

z_mpf.mpf_convert_rhsc             C   s&   |  |}t|tkr"| j|S |S )N)r   rs   ru   rp   make_mpf)r^   r   r`   r`   ra   mpf_convert_lhsr   s    
z_mpf.mpf_convert_lhsc             C   s   | j dd S )Nr      )ri   )selfr`   r`   ra   <lambda>y   s    z_mpf.<lambda>c             C   s
   | j d S )Nr   )ri   )r   r`   r`   ra   r   z   s    c             C   s
   | j d S )Nrm   )ri   )r   r`   r`   ra   r   {   s    c             C   s
   | j d S )Nr   )ri   )r   r`   r`   ra   r   |   s    c             C   s   | S )Nr`   )r   r`   r`   ra   r   ~   s    c             C   s   | j jS )N)rp   Zzero)r   r`   r`   ra   r      s    c             C   s   | S )Nr`   )r   r`   r`   ra   r      s    c             C   s
   t | jS )N)r   ri   )r   r`   r`   ra   __getstate__   s    z_mpf.__getstate__c             C   s   t || _d S )N)r   ri   )r   r_   r`   r`   ra   __setstate__   s    z_mpf.__setstate__c             C   s$   | j jrt| S dt| j| j j S )Nz	mpf('%s'))rp   prettystrr   ri   Z_repr_digits)sr`   r`   ra   __repr__   s    z_mpf.__repr__c             C   s   t | j| jjS )N)r   ri   rp   _str_digits)r   r`   r`   ra   __str__   s    z_mpf.__str__c             C   s
   t | jS )N)r2   ri   )r   r`   r`   ra   __hash__   s    z_mpf.__hash__c             C   s   t t| jS )N)intr   ri   )r   r`   r`   ra   __int__   s    z_mpf.__int__c             C   s   t t| jS )N)Zlongr   ri   )r   r`   r`   ra   __long__   s    z_mpf.__long__c             C   s   t | j| jjd dS )Nr   )rnd)r   ri   rp   rq   )r   r`   r`   ra   	__float__   s    z_mpf.__float__c             C   s   t t| S )N)complexr   )r   r`   r`   ra   __complex__   s    z_mpf.__complex__c             C   s
   | j tkS )N)ri   r   )r   r`   r`   ra   __nonzero__   s    z_mpf.__nonzero__c             C   s,   | j \}}\}}||}t| j|||_|S )N)_ctxdatar!   ri   )r   r^   rt   rj   rl   r~   r`   r`   ra   __abs__   s    z_mpf.__abs__c             C   s,   | j \}}\}}||}t| j|||_|S )N)r   r"   ri   )r   r^   rt   rj   rl   r~   r`   r`   ra   __pos__   s    z_mpf.__pos__c             C   s,   | j \}}\}}||}t| j|||_|S )N)r   r#   ri   )r   r^   rt   rj   rl   r~   r`   r`   ra   __neg__   s    z_mpf.__neg__c             C   s4   t |dr|j}n| |}|tkr(|S || j|S )Nri   )r   ri   r   r   )r   r   r   r`   r`   ra   _cmp   s    

z	_mpf._cmpc             C   s   |  |tS )N)r   r-   )r   r   r`   r`   ra   __cmp__   s    z_mpf.__cmp__c             C   s   |  |tS )N)r   r.   )r   r   r`   r`   ra   __lt__   s    z_mpf.__lt__c             C   s   |  |tS )N)r   r/   )r   r   r`   r`   ra   __gt__   s    z_mpf.__gt__c             C   s   |  |tS )N)r   r0   )r   r   r`   r`   ra   __le__   s    z_mpf.__le__c             C   s   |  |tS )N)r   r1   )r   r   r`   r`   ra   __ge__   s    z_mpf.__ge__c             C   s   |  |}|tkr|S | S )N)__eq__r   )r   r   r~   r`   r`   ra   __ne__   s    
z_mpf.__ne__c             C   s\   | j \}}\}}t|tkr>||}tt|| j|||_|S | |}|tkrT|S ||  S )N)r   rs   r   r%   r   ri   r   r   )r   r   r^   rt   rj   rl   r~   r`   r`   ra   __rsub__   s    
z_mpf.__rsub__c             C   sV   | j \}}\}}t|tr8||}t|| j|||_|S | |}|tkrN|S ||  S )N)r   r   r   r)   ri   r   r   )r   r   r^   rt   rj   rl   r~   r`   r`   ra   __rdiv__   s    

z_mpf.__rdiv__c             C   s   |  |}|tkr|S ||  S )N)r   r   )r   r   r`   r`   ra   __rpow__   s    
z_mpf.__rpow__c             C   s   |  |}|tkr|S ||  S )N)r   r   )r   r   r`   r`   ra   __rmod__   s    
z_mpf.__rmod__c             C   s   | j | S )N)rp   sqrt)r   r`   r`   ra   r      s    z	_mpf.sqrtNc             C   s   | j | |||S )N)rp   almosteq)r   r   rel_epsabs_epsr`   r`   ra   ae   s    z_mpf.aec             C   s   t | j|S )N)r6   ri   )r   rj   r`   r`   ra   r6      s    z_mpf.to_fixedc             G   s   t t| f| S )N)roundr   )r   argsr`   r`   ra   	__round__   s    z_mpf.__round__)NN)0rc   rd   re   rf   rg   r   rb   classmethodrx   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   r`   r`   r`   ra   rh   '   sT   !	
rh   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npr`   r`   ra   	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   )objectrb   r   r   getattrr[   rf   )r^   r   r   Zdocnamer   r`   r`   ra   rb   N  s
    
z_constant.__new__Nc             C   s<   | j j\}}|s|}|s|}|r(t|}| j | ||S )N)rp   rq   r   r   r   )r   rj   rk   rl   Zprec2Z	rounding2r`   r`   ra   __call__U  s       z_constant.__call__c             C   s   | j j\}}| ||S )N)rp   rq   r   )r   rj   rl   r`   r`   ra   ri   \  s    z_constant._mpf_c             C   s   d| j | j| ddf S )Nz	<%s: %s~>   )rk   )r   rp   Znstr)r   r`   r`   ra   r   a  s    z_constant.__repr__)r   )NNN)	rc   rd   re   rf   rb   r   r   ri   r   r`   r`   r`   ra   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_rn   c             C   sd   t | }t|tr$|j|j }}nt|dr:|j|_|S | j	|}| j	|}|j
|j
f|_|S )Nr   )r   rb   r   r   r   r   r   r   rp   mpfri   )r^   r   r   r   r`   r`   ra   rb   n  s    


z_mpc.__new__c             C   s   | j | jd S )Nrn   )rp   r   r   )r   r`   r`   ra   r   z  s    z_mpc.<lambda>c             C   s   | j | jd S )Nr   )rp   r   r   )r   r`   r`   ra   r   {  s    c             C   s   t | jd t | jd fS )Nrn   r   )r   r   )r   r`   r`   ra   r   }  s    z_mpc.__getstate__c             C   s   t |d t |d f| _d S )Nrn   r   )r   r   )r   r_   r`   r`   ra   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 )Nro   z%s(real=%s, imag=%s))rp   r   r   r   r   r   rs   rc   )r   rir`   r`   ra   r     s
    z_mpc.__repr__c             C   s   dt | j| jj S )Nz(%s))r7   r   rp   r   )r   r`   r`   ra   r     s    z_mpc.__str__c             C   s   t | j| jjd dS )Nr   )r   )r8   r   rp   rq   )r   r`   r`   ra   r     s    z_mpc.__complex__c             C   s,   | j \}}\}}||}t| j|||_|S )N)r   r:   r   )r   r^   rt   rj   rl   r~   r`   r`   ra   r     s    z_mpc.__pos__c             C   s,   | j j\}}t| j j}t| j|||_|S )N)rp   rq   rt   r   r>   r   ri   )r   rj   rl   r~   r`   r`   ra   r     s    z_mpc.__abs__c             C   s,   | j \}}\}}||}t| j|||_|S )N)r   r<   r   )r   r^   rt   rj   rl   r~   r`   r`   ra   r     s    z_mpc.__neg__c             C   s,   | j \}}\}}||}t| j|||_|S )N)r   r=   r   )r   r^   rt   rj   rl   r~   r`   r`   ra   r     s    z_mpc.conjugatec             C   s
   t | jS )N)r;   r   )r   r`   r`   ra   r     s    z_mpc.__nonzero__c             C   s
   t | jS )N)r9   r   )r   r`   r`   ra   r     s    z_mpc.__hash__c             C   s*   y| j |}|S  tk
r$   tS X d S )N)rp   r   r   r   )r^   r   yr`   r`   ra   mpc_convert_lhs  s
    z_mpc.mpc_convert_lhsc             C   sF   t |ds.t|trdS | |}|tkr.|S | j|jkoD| j|jkS )Nr   F)r   r   r   r   r   r   r   )r   r   r`   r`   ra   r     s    


z_mpc.__eq__c             C   s   |  |}|tkr|S | S )N)r   r   )r   r   r   r`   r`   ra   r     s    
z_mpc.__ne__c              G   s   t dd S )Nz3no ordering relation is defined for complex numbers)r   )r   r`   r`   ra   _compare  s    z_mpc._comparec             C   sz   | j \}}\}}t|dsZ| |}|tkr0|S t|drZ||}t| j|j|||_|S ||}t| j|j|||_|S )Nr   ri   )r   r   r   r   r@   r   ri   r?   )r   r   r^   rt   rj   rl   r~   r`   r`   ra   r     s    


z_mpc.__add__c             C   sz   | j \}}\}}t|dsZ| |}|tkr0|S t|drZ||}t| j|j|||_|S ||}t| j|j|||_|S )Nr   ri   )r   r   r   r   rB   r   ri   rA   )r   r   r^   rt   rj   rl   r~   r`   r`   ra   r     s    


z_mpc.__sub__c             C   s   | j \}}\}}t|dst|trB||}t| j||||_|S | |}|tkrX|S t|dr||}t| j|j	|||_|S | |}||}t
| j|j|||_|S )Nr   ri   )r   r   r   r   rE   r   r   r   rD   ri   rC   )r   r   r^   rt   rj   rl   r~   r`   r`   ra   r     s"    




z_mpc.__mul__c             C   sz   | j \}}\}}t|dsZ| |}|tkr0|S t|drZ||}t| j|j|||_|S ||}t| j|j|||_|S )Nr   ri   )r   r   r   r   rG   r   ri   rF   )r   r   r^   rt   rj   rl   r~   r`   r`   ra   r     s    


z_mpc.__div__c             C   s   | j \}}\}}t|tr8||}t| j||||_|S | |}|tkrN|S ||}t|drvt| j|j	|||_nt
| j|j|||_|S )Nri   )r   r   r   rJ   r   r   r   r   rI   ri   rH   )r   r   r^   rt   rj   rl   r~   r`   r`   ra   r     s    


z_mpc.__pow__c             C   s   |  |}|tkr|S ||  S )N)r   r   )r   r   r`   r`   ra   r     s    
z_mpc.__rsub__c             C   sV   | j \}}\}}t|tr8||}t| j||||_|S | |}|tkrN|S ||  S )N)r   r   r   rE   r   r   r   )r   r   r^   rt   rj   rl   r~   r`   r`   ra   __rmul__$  s    

z_mpc.__rmul__c             C   s   |  |}|tkr|S ||  S )N)r   r   )r   r   r`   r`   ra   r   /  s    
z_mpc.__rdiv__c             C   s   |  |}|tkr|S ||  S )N)r   r   )r   r   r`   r`   ra   r   5  s    
z_mpc.__rpow__Nc             C   s   | j | |||S )N)rp   r   )r   r   r   r   r`   r`   ra   r   >  s    z_mpc.ae)rn   rn   )NN)*rc   rd   re   rf   rg   rb   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   r`   r`   r`   ra   r   e  sL   
	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   rq   rs   rh   r   r   r   rt   r   rp   r   r   )ctxr`   r`   ra   __init__G  s    
zPythonMPContext.__init__c             C   s   t | j}||_|S )N)rt   r   ri   )r   r~   r   r`   r`   ra   r   S  s    
zPythonMPContext.make_mpfc             C   s   t | j}||_|S )N)rt   r   r   )r   r~   r   r`   r`   ra   make_mpcX  s    
zPythonMPContext.make_mpcc             C   s    d | _ | jd< d| _d| _d S )Nr   rn   r   F)_precrq   _dpstrap_complex)r   r`   r`   ra   default]  s    zPythonMPContext.defaultc             C   s(   t dt| | _| jd< t|| _d S )Nr   rn   )maxr   r   rq   r   r   )r   nr`   r`   ra   	_set_precb  s    zPythonMPContext._set_precc             C   s(   t | | _| jd< tdt|| _d S )Nrn   r   )r   r   rq   r   r   r   )r   r   r`   r`   ra   _set_dpsf  s    zPythonMPContext._set_dpsc             C   s   | j S )N)r   )r   r`   r`   ra   r   j  s    zPythonMPContext.<lambda>c             C   s   | j S )N)r   )r   r`   r`   ra   r   k  s    Tc             C   s  t || jk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rytt|jt|j}W n   Y nX | j\}}t|tjr|j\}}| t|||S |r&t|tr&yt|||}| |S  tk
r$   Y nX t|dr>| |jS t|drV| 	|jS t|drt| |||S t |jdkry| t |||S    Y nX | !||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')

        numpyri   r   r   Zdecimal)"rs   typesr   r   r   r   r   r   r   r   r   r   rd   	npconvertnumbersZRationalrZ   r   r   	numeratordenominatorrq   r   r   r   r   rw   r   ri   r   r   r   r   Z_convert_fallback)r   r   Zstringsrj   rl   r   r   ri   r`   r`   ra   r   m  sJ     
 
 
 
  


    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.
        rn   Nzcannot create mpf from )r   r   Zintegerr   r   r   Zfloatingr   Zcomplexfloatingr   r   r   r   r   )r   r   r   r`   r`   ra   r     s      zPythonMPContext.npconvertc             C   sv   t |dr|jtkS t |dr(t|jk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

        ri   r   Fzisnan() needs a number as inputN)r   ri   r    r   r   r   rZ   r   r   isnanr   )r   r   r`   r`   ra   r     s    





zPythonMPContext.isnanc             C   s   t |dr|jttfkS t |drD|j\}}|ttfkpB|ttfk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

        ri   r   Fzisinf() needs a number as inputN)r   ri   r   r   r   r   r   rZ   r   r   isinfr   )r   r   reimr`   r`   ra   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
        ri   r   r   z"isnormal() needs a number as inputN)r   boolri   r   r   r   r   rZ   r   r   isnormalr   )r   r   r   r   Z	re_normalZ	im_normalr`   r`   ra   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

        Tri   rn   r   zisint() needs a number as inputN)r   r   r   ri   r   r   r   rZ   r   r   r   isintr   )r   r   Zgaussianrz   r{   r|   r}   Zxvalr   r   ZrsignZrmanZrexpZrbcZisignZimanZiexpZibcZre_isintZim_isintr   r   r`   r`   ra   r    s*    





zPythonMPContext.isintc             C   s  | j \}}g }g }d}xP|D ]F}	d }
}t|	dr>|	j}
nlt|	drT|	j\}
}nV| |	}	t|	drp|	j}
n:t|	dr|	j\}
}n$|r| |	}	|r|	d }	||	7 }q|r<|r
|r|t|
|
 |t|| n,t|
|fd|d \}
}||
 || n0|r&|t	|
|f| n||
 || q|rNt|
|
}
n|r\t
|
}
||
 qW t||||}|r| |t|||f}n
| |}|dkr|S || S d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.
        rn   ri   r   rm   
   N)rq   r   ri   r   r   Zabsmaxappendr&   rJ   r>   r!   r4   r   r   )r   ZtermsZabsoluteZsquaredrj   r   r   r   otherZtermZrevalZimvalr   r`   r`   ra   fsum>  sZ    





 
 



zPythonMPContext.fsumNc             C   s0  |dk	rt ||}| j\}}g }g }d}t}	| j| jf}
x|D ]\}}t||
kr`| |}t||
krv| |}|	|d}|	|d}|r|r|t|j	|j	 q@|	|d}|	|d}|r|r|j	}|j
\}}|rt|}|t|| |t|| q@|rD|rD|j
\}}|j	}|t|| |t|| q@|r|r|j
\}}|j
\}}|rrt|}|t|| |tt|| |t|| |t|| q@|r||| | 7 }q@||| 7 }q@W t|||}|r| |t|||f}n
| |}|dkr$|S || S d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')

        Nrn   ri   r   )ziprq   r   r   r   rs   r   r  r&   ri   r   r#   Zconjr4   r   r   )r   ABr   rj   r   r   r   r  Zhasattr_r   r   r   Za_realZb_realZ	a_complexZ	b_complexZavalZbreZbimZareZaimZbvalr   r`   r`   ra   fdot  sf    '

 
 










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kr | }  j\}}|rR|d|}d|krFt|d }|d|}t| dry | j||S  t	k
r    j
r  | jtf||S X nt| dr | j||S tdt | f d S )Nrj   rk   rl   ri   r   z
%s of a %s)rs   r   r   rq   rr   r   r   r   ri   r   r   r   r   r   r]   )r   ry   rj   rl   )r   mpc_fmpf_fr   r`   ra   f  s$    



z/PythonMPContext._wrap_libmp_function.<locals>.fro   NzComputes the %s of x)rc   r[   __dict__rr   rf   )r   r  r  Zmpi_fdocr  r`   )r   r  r  r   ra   _wrap_libmp_function  s    
z$PythonMPContext._wrap_libmp_functionc                s8   |r fdd}n }t j| j|_t| || d S )Nc                sP   | j   fdd|D }| j}z"|  jd7  _| f||}W d || _X |
 S )Nc                s   g | ]} |qS r`   r`   ).0r   )r   r`   ra   
<listcomp>  s    zDPythonMPContext._wrap_specfun.<locals>.f_wrapped.<locals>.<listcomp>r  )r   rj   )r   r   ry   rj   Zretval)r  )r   ra   	f_wrapped  s    z0PythonMPContext._wrap_specfun.<locals>.f_wrapped)r[   r  rr   rf   setattr)r^   r   r  Zwrapr  r`   )r  ra   _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krRt|dfS d }t|trj|\}}nFt |dr|j	\}}n0t|t
rd|kr|d\}}t|}t|}|d k	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   Cri   Zr   /QUrn   r   R)rn   r  )r   r   r   ri   rs   r   r   r   ru   r   r   splitr   r   r   )
r   r   r~   r   r   r   rz   r{   r|   r}   r`   r`   ra   _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 )N)r   ninfr   r   infnan)r   r   rz   r{   r|   r}   r`   r`   ra   _mpf_magE  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)

        ri   r   r   zrequires an mpf/mpcN)r   r#  ri   r   r   r   r   r   r5   absr   rZ   r   r   r   magr   )r   r   r   r   r   r   r`   r`   ra   r%  O  s,    








zPythonMPContext.mag)T)F)FF)NF)NNr  )rc   rd   re   r   r   r   r   r   r   r   rj   rk   r   r   r   r   r  r  r  r
  r  r   r  r  r#  r%  r`   r`   r`   ra   r   E  s*   
2 (
/
C
^
#1
r   rn   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   rb   rt   r\   rh   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   ZComplexregisterZRealImportErrorr`   r`   r`   ra   <module>   sx    e `

 ^    C