U
    ZÊ¹j·“  ã                   @   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$d1d2lxZxexjy zet¡ 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© ra   úS/var/www/html/bwave/python/venv/lib/python3.8/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|kr0t|d ƒ}| d|¡}t|ƒ| kr‚|j\}}}}|sb|rb|S t| ƒ}	t||||||ƒ|	_|	S t|ƒtk�rt	|ƒdkrÀt| ƒ}	t
|d |d ||ƒ|	_|	S t	|ƒdk�r|tttfk�r |\}}}}t|t|ƒ||||ƒ}t| ƒ}	||	_|	S t‚n$t| ƒ}	t|  |||¡||ƒ|	_|	S dS )	z‹A 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   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ƒ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   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	ƒ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_rhsd   s"    
 
 
 

 

z_mpf.mpf_convert_rhsc                 C   s&   |   |¡}t|ƒtk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   s
   | j d S ©Nr   rš   r›   ra   ra   rb   r�   |   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   Ú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k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kr|S | S r]   ©Ú__eq__r“   )r§   rŒ   r   ra   ra   rb   Ú__ne__¸   s    
z_mpf.__ne__c                 C   s\   | j \}}\}}t|ƒtkr>||ƒ}tt|ƒ| j||ƒ|_|S |  |¡}|tk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krN|S ||  S r]   )r¶   rƒ   r   r)   rj   r˜   r“   rÆ   ra   ra   rb   Ú__rdiv__É   s    

z_mpf.__rdiv__c                 C   s   |   |¡}|tkr|S ||  S r]   ©r˜   r“   r½   ra   ra   rb   Ú__rpow__Ô   s    
z_mpf.__rpow__c                 C   s   |   |¡}|tk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| ƒf|žŽ 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 )Ú	_constantzõRepresents 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   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   Zprec2Z	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   Znstrr›   ra   ra   rb   r¨   c  s    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   s   | j  | jd ¡S )Nro   ©rq   r—   rí   r›   ra   ra   rb   r�   |  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   s0   z| j  |¡}|W S  tk
r*   t Y S X 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k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k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k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k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ƒsŒt|tƒrB||ƒ}t| j|||ƒ|_|S |  |¡}|tk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k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k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k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krN|S ||  S r]   )r¶   rƒ   r   rE   rí   rõ   r“   rÆ   ra   ra   rb   Ú__rmul__&  s    

z_mpc.__rmul__c                 C   s   |   |¡}|tkr|S ||  S r]   rø   r½   ra   ra   rb   rÈ   1  s    
z_mpc.__rdiv__c                 C   s   |   |¡}|tkr|S ||  S r]   rø   r½   ra   ra   rb   rÊ   7  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ì   g  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 )Né5   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   s   t | jƒ}||_|S r]   )ru   rî   rj   ©r   r   r�   ra   ra   rb   r—   U  s    
zPythonMPContext.make_mpfc                 C   s   t | jƒ}||_|S r]   )ru   r�   rí   r  ra   ra   rb   Úmake_mpcZ  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_precd  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_dpsh  s    zPythonMPContext._set_dpsc                 C   s   | j S r]   )r  rÿ   ra   ra   rb   r�   l  rž   zPythonMPContext.<lambda>c                 C   s   | j S r]   )r  rÿ   ra   ra   rb   r�   m  rž   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ƒr´zt t|jƒt|jƒ¡}W n   Y nX | j\}}t|tjƒræ|j\}}|  t|||ƒ¡S |�r(t|tƒ�r(zt|||ƒ}|  |¡W S  tk
�r&   Y nX t|dƒ�r@|  |j¡S t|dƒ�rX|  	|j¡S t|dƒ�rv|  | ||¡¡S t |ƒjdk�r¨z|  t |||ƒ¡W 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')

        Ú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ˆ   o  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.
        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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

        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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

        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ƒr‚t|ƒS |  	|¡}t |dƒs t |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ƒsît|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Ö   ÚtermZrevalZimvalr§   ra   ra   rb   Úfsum@  sJ    









zPythonMPContext.fsumNc                 C   sê  |dk	rt ||ƒ}| j\}}g }g }t}| j| jf}	|D �]v\}
}t|
ƒ|	krX|  |
¡}
t|ƒ|	krn|  |¡}||
dƒ}||dƒ}|r |r | t|
j	|j	ƒ¡ q8||
dƒ}||dƒ}|rú|rú|
j	}|j
\}}|rØt|ƒ}| 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   Úfdot|  s\    '

 
 









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ƒrªzˆ  ˆ| j||ƒ¡W S  t	k
r¦   ˆ j
rŠ‚ ˆ  ˆ| jtf||ƒ¡ Y S X 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                    sP   | j ‰ ‡ fdd„|D ƒ}| j}z"|  jd7  _ˆ| f|ž|Ž}W 5 || _X |
 S )Nc                    s   g | ]}ˆ |ƒ‘qS ra   ra   )Ú.0r�   ©rˆ   ra   rb   Ú
<listcomp>û  s     zDPythonMPContext._wrap_specfun.<locals>.f_wrapped.<locals>.<listcomp>r  )rˆ   rk   )r   rÑ   rz   rk   Úretval©r+  r0  rb   Ú	f_wrappedù  s    z0PythonMPContext._wrap_specfun.<locals>.f_wrapped)r[   r,  rs   rg   Úsetattr)r_   rÝ   r+  Úwrapr4  ra   r3  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}nòt|ƒtkrRt|ƒdfS d }t|tƒrj|\}}nFt |dƒr€|j	\}}n0t|t
ƒr°d|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�rœt|ƒd| >  }}|  ||¡dfS |  |¡}|dfS |�s¸dS |dfS d S )Nrí   ÚCrj   ÚZr’   ú/ÚQÚUéüÿÿÿro   r   ÚR)ro   r9  )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_mag9  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ˆ|r‚tt	|ƒƒS | j
S t|tjƒrÀ|j\}}|rºdtt	|ƒƒ t|ƒ S | j
S |  |¡}t |dƒsÞt |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‡   rD  rj   rí   r   r  rƒ   r   r5   ÚabsrA  rZ   r‘   r’   rˆ   Úmagr‰   )r   r‹   rò   ró   r”   r•   ra   ra   rb   rF  C  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Ó   r7  r@  rD  rF  ra   ra   ra   rb   rý   G  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 Fø

ýýýýýýý ^    5