
    g                        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e]      Z` G d de`      Zad	Zbd
ZcdZddecdeddZed3dZf efdddd      ea_g         efddecz   decz   dedz         ea_h         efddecz   decz   dedz         ea_i         efddecz   decz   dedz         ea_j         efd d!ecz   d"ecz   d#edz         ea_k         efd$d%ecz   d&ecz   d'      ea_l         efd(eed)ecz   d*edz         ea_m        eaj                  ea_n        eaj                  ea_o        eaj                  ea_p        eaj                  ea_r         G d+ d,ea      Zs G d- d.e`      ZteuetfZv G d/ d0e]      Zw	 d1d2lxZxexj                  j                  et       exj                  j                  ea       y2# e|$ r Y y2w xY w)4   )
basestringexec_)WMPZMPZ_ZEROMPZ_ONE	int_typesrepr_dpsround_floorround_ceilingdps_to_precround_nearestprec_to_dpsComplexResultto_pickablefrom_pickable	normalizefrom_int
from_floatfrom_npfloatfrom_Decimalfrom_strto_intto_floatto_strfrom_rationalfrom_man_expfonefzerofinffninffnanmpf_absmpf_posmpf_negmpf_addmpf_submpf_mulmpf_mul_intmpf_divmpf_rdiv_intmpf_pow_intmpf_modmpf_eqmpf_cmpmpf_ltmpf_gtmpf_lempf_gempf_hashmpf_randmpf_sumbitcountto_fixed
mpc_to_strmpc_to_complexmpc_hashmpc_posmpc_is_nonzerompc_negmpc_conjugatempc_absmpc_addmpc_add_mpfmpc_submpc_sub_mpfmpc_mulmpc_mul_mpfmpc_mul_intmpc_divmpc_div_mpfmpc_powmpc_pow_mpfmpc_pow_intmpc_mpf_divmpf_powmpf_pi
mpf_degreempf_empf_phimpf_ln2mpf_ln10	mpf_eulermpf_catalan	mpf_aperympf_khinchinmpf_glaishermpf_twinprimempf_mertensr   )rational)function_docsc                       e Zd ZdZg Zd Zy)	mpnumericzBase class for mpf and mpc.c                     t         N)NotImplementedError)clsvals     I/var/www/openai/venv/lib/python3.12/site-packages/mpmath/ctx_mp_python.py__new__zmpnumeric.__new__$   s    !!    N)__name__
__module____qualname____doc__	__slots__re    rf   rd   r^   r^   !   s    %I"rf   r^   c                   p   e Zd ZdZdgZefdZed        Zed        Z	ed        Z
 ed       Z ed       Z ed	       Z ed
       Z ed       Z ed       Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZeZd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d  Z&d! Z'd" Z(d# Z)d$ Z*d% Z+d& Z,d+d(Z-d) Z.d* Z/y'),_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                    | j                   j                  \  }}|r6|j                  d|      }d|v rt        |d         }|j                  d|      }t	        |      | u r9|j
                  \  }}}}|s|r|S t        |       }	t        ||||||      |	_        |	S t	        |      t        u rt        |      dk(  r&t        |       }	t        |d   |d   ||      |	_        |	S t        |      dk(  rG|t        t        t        fvr |\  }}}}t        |t        |      ||||      }t        |       }	||	_        |	S t        t        |       }	t!        | j#                  |||      ||      |	_        |	S )zA new mpf can be created from a Python float, an int, a
        or a decimal string representing a number in floating-point
        format.precdpsrounding       r      )context_prec_roundinggetr   typero   newr   tuplelenr   r   r    r!   r   
ValueErrorr#   mpf_convert_arg)
rb   rc   kwargsrq   rs   signmanexpbcvs
             rd   re   z_mpf.__new__/   sb    33h::fd+D"6%=1zz*h7H9!$D#sBS
CAc3D(CAGH#Y%3x1}H&s1vs1vtXF3x1}tUD11),&D#sB#D#c(CT8LCHCAc11#tXFhWAGHrf   c                 b   t        |t              rt        |      S t        |t              rt	        |      S t        |t
              rt        |||      S t        || j                  j                        r|j                  ||      S t        |d      r|j                  S t        |d      rC| j                  j                  |j                  ||            }t        |d      r|j                  S t        |d      r!|j                  \  }}||k(  r|S t        d      t!        dt#        |      z         )Nro   _mpmath__mpi_z,can only create mpf from zero-width intervalcannot create mpf from )
isinstancer   r   floatr   r   r   rw   constantfunchasattrro   convertr   r   r~   	TypeErrorrepr)rb   xrq   rs   tabs          rd   r   z_mpf.mpf_convert_argR   s    a#HQK%7a
1!5a$Xax-H&Ha--.qvvdH7M0M1gqww1j!##AJJtX$>?Aq'"ww1g77DAqAvKLL1DG;<<rf   c                 X   t        |t              rt        |      S t        |t              rt	        |      S t        |t
              r| j                  j                  |      S t        |t        j                        r0|j                  \  }}t        ||| j                  j                        S t        |d      r|j                  S t        |d      rV| j                  j                   |j                   | j                  j"                         }t        |d      r|j                  S |S t$        S )Nro   r   )r   r   r   r   r   complex_typesrw   mpcr[   mpq_mpq_r   rq   r   ro   r   r   rx   NotImplemented)rb   r   pqr   s        rd   mpf_convert_rhsz_mpf.mpf_convert_rhsd   s    a#HQK%7a
1!5a'0B)Ba&77DAq As{{'7'7881gqww1j!##JAJJ0J0J$KLAq'"wwHrf   c                     | j                  |      }t        |      t        u r| j                  j	                  |      S |S r`   )r   rz   r|   rw   make_mpf)rb   r   s     rd   mpf_convert_lhsz_mpf.mpf_convert_lhst   s8    "7e;;''**rf   c                      | j                   dd S )Nr      ro   selfs    rd   <lambda>z_mpf.<lambda>{   s    DJJqOrf   c                      | j                   d   S Nr   r   r   s    rd   r   z_mpf.<lambda>|       

1rf   c                      | j                   d   S )Nrt   r   r   s    rd   r   z_mpf.<lambda>}   r   rf   c                      | j                   d   S )Nr   r   r   s    rd   r   z_mpf.<lambda>~   s    tzz!}rf   c                     | S r`   rl   r   s    rd   r   z_mpf.<lambda>   s    rf   c                 .    | j                   j                  S r`   )rw   zeror   s    rd   r   z_mpf.<lambda>   s    !2!2rf   c                     | S r`   rl   r   s    rd   r   z_mpf.<lambda>   s    Trf   c                 ,    t        | j                        S r`   )r   ro   r   s    rd   __getstate__z_mpf.__getstate__   s    ;tzz#::rf   c                 $    t        |      | _        y r`   )r   ro   r   rc   s     rd   __setstate__z_mpf.__setstate__   s    mC.@rf   c                     | j                   j                  rt        |       S dt        | j                  | j                   j
                        z  S )Nz	mpf('%s'))rw   prettystrr   ro   _repr_digitsss    rd   __repr__z_mpf.__repr__   s8    99q6MVAGGQYY-C-CDDDrf   c                 V    t        | j                  | j                  j                        S r`   )r   ro   rw   _str_digitsr   s    rd   __str__z_mpf.__str__   s    6!''199+@+@AArf   c                 ,    t        | j                        S r`   )r3   ro   r   s    rd   __hash__z_mpf.__hash__   s    HQWW--rf   c                 >    t        t        | j                              S r`   )intr   ro   r   s    rd   __int__z_mpf.__int__   s    3vagg//rf   c                 >    t        t        | j                              S r`   )longr   ro   r   s    rd   __long__z_mpf.__long__   s    D11rf   c                 ^    t        | j                  | j                  j                  d         S Nr   )rnd)r   ro   rw   rx   r   s    rd   	__float__z_mpf.__float__   s!    Xagg1993K3KA3NOOrf   c                 *    t        t        |             S r`   )complexr   r   s    rd   __complex__z_mpf.__complex__   s    wuQx00rf   c                 (    | j                   t        k7  S r`   )ro   r   r   s    rd   __nonzero__z_mpf.__nonzero__   s    qww%//rf   c                 t    | j                   \  }}\  }} ||      }t        | j                  ||      |_        |S r`   )_ctxdatar"   ro   r   rb   r{   rq   rs   r   s         rd   __abs__z_mpf.__abs__   9    %&ZZ"S"4H!''42rf   c                 t    | j                   \  }}\  }} ||      }t        | j                  ||      |_        |S r`   )r   r#   ro   r   s         rd   __pos__z_mpf.__pos__   r   rf   c                 t    | j                   \  }}\  }} ||      }t        | j                  ||      |_        |S r`   )r   r$   ro   r   s         rd   __neg__z_mpf.__neg__   r   rf   c                     t        |d      r|j                  }n| j                  |      }|t        u r|S  || j                  |      S Nro   )r   ro   r   r   )r   r   r   s      rd   _cmpz	_mpf._cmp   sC    1gA!!!$AN"AGGQrf   c                 .    | j                  |t              S r`   )r   r.   r   r   s     rd   __cmp__z_mpf.__cmp__   s    affQ00rf   c                 .    | j                  |t              S r`   )r   r/   r   s     rd   __lt__z_mpf.__lt__       QVVAv..rf   c                 .    | j                  |t              S r`   )r   r0   r   s     rd   __gt__z_mpf.__gt__   r   rf   c                 .    | j                  |t              S r`   )r   r1   r   s     rd   __le__z_mpf.__le__   r   rf   c                 .    | j                  |t              S r`   )r   r2   r   s     rd   __ge__z_mpf.__ge__   r   rf   c                 >    | j                  |      }|t        u r|S | S r`   __eq__r   )r   r   r   s      rd   __ne__z_mpf.__ne__   #    HHQKHurf   c                     | j                   \  }}\  }}t        |      t        v r0 ||      }t        t	        |      | j
                  ||      |_        |S | j                  |      }|t        u r|S || z
  S r`   )r   rz   r   r&   r   ro   r   r   r   r   rb   r{   rq   rs   r   s          rd   __rsub__z_mpf.__rsub__   sq    %&ZZ"S"47iCAhqk177D(CAGHa H1urf   c                     | j                   \  }}\  }}t        |t              r' ||      }t        || j                  ||      |_        |S | j                  |      }|t        u r|S || z  S r`   )r   r   r   r*   ro   r   r   r   s          rd   __rdiv__z_mpf.__rdiv__   sk    %&ZZ"S"4a#CA"1aggtX>AGHa H1urf   c                 B    | j                  |      }|t        u r|S || z  S r`   r   r   r   s     rd   __rpow__z_mpf.__rpow__   (    a HAvrf   c                 B    | j                  |      }|t        u r|S || z  S r`   r   r   s     rd   __rmod__z_mpf.__rmod__   (    a H1urf   c                 8    | j                   j                  |       S r`   )rw   sqrtr   s    rd   r   z	_mpf.sqrt   s    yy~~a  rf   Nc                 >    | j                   j                  | |||      S r`   rw   almosteqr   r   rel_epsabs_epss       rd   aez_mpf.ae       yy!!!Q99rf   c                 .    t        | j                  |      S r`   )r7   ro   )r   rq   s     rd   r7   z_mpf.to_fixed   s    

D))rf   c                 ,    t        t        |       g| S r`   )roundr   )r   argss     rd   	__round__z_mpf.__round__   s    U4[(4((rf   NN)0rg   rh   ri   rj   rk   r   re   classmethodr   r   r   propertyman_expr   r   r   realimag	conjugater   r   r   r   r   r   r   r   r   r   __bool__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r7   r  rl   rf   rd   rn   rn   '   s   
 	I !F = ="     34G
-
.C
-
.C	,	-B%&D23D!I:@E
 B-/1O0/H  1....		!:*)rf   rn   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 objzD
        try:
            val = mpf_pow(sval, tval, prec, rounding) z
        except ComplexResult:
            if mpf.context.trap_complex:
                raise
            mpc = mpf.context.mpc
            val = mpc_pow((sval, fzero), (tval, fzero), prec, rounding) 
c                     t         }|j                  d|      }|j                  d|      }|j                  d|      }|j                  d|       }i }t        |t               |       ||    S )Nz
%WITH_INT%z
%WITH_MPC%z
%WITH_MPF%z%NAME%)mpf_binary_opreplacer   globals)namewith_mpfwith_intwith_mpccodenps         rd   	binary_opr    sc    D<<h/D<<h/D<<h/D<<$'D	B	$	2d8Orf   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                   6    e Zd ZdZddZddZed        Zd Zy)		_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.c                 x    t         j                  |       }||_        ||_        t	        t
        |d      |_        |S )N )objectre   r  r   getattrr\   rj   )rb   r   r  docnamer   s        rd   re   z_constant.__new__P  s3    NN3M7B7	rf   Nc                     | j                   j                  \  }}|s|}|s|}|rt        |      }| j                   j                  | j	                  ||            S r`   )rw   rx   r   r   r   )r   rq   rr   rs   prec2	rounding2s         rd   __call__z_constant.__call__W  sP    <<66yETI{3'||$$TYYtX%>??rf   c                 X    | j                   j                  \  }}| j                  ||      S r`   )rw   rx   r   )r   rq   rs   s      rd   ro   z_constant._mpf_^  s&    44hyyx((rf   c                 h    d| j                   d| j                  j                   | d            dS )N<z:    )rr   z~>)r  rw   nstrr   s    rd   r   z_constant.__repr__c  s$    "ii):):4B<)HIIrf   )r&  )NNN)	rg   rh   ri   rj   re   r-  r  ro   r   rl   rf   rd   r$  r$  J  s-    @
@ ) )Jrf   r$  c                      e Zd ZdZdgZd dZ ed       Z ed       Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd ZeZd Zed        Zd Zd Zd ZeZeZeZeZd Zd Zd Zd Z d Z!eZ"d Z#d Z$d Z%d Z&e Z'e%Z(d!dZ)y)"_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_c                 f   t         j                  |       }t        |t              r|j                  |j
                  }}nt        |d      r|j                  |_        |S | j                  j                  |      }| j                  j                  |      }|j                  |j                  f|_        |S )Nr5  )r'  re   r   r   r  r  r   r5  rw   mpfro   )rb   r  r  r   s       rd   re   z_mpc.__new__p  s    NN3dM*DII$DT7#jjAGH{{t${{t$::tzz*rf   c                 R    | j                   j                  | j                  d         S )Nru   rw   r   r5  r   s    rd   r   z_mpc.<lambda>|      !6!6tzz!}!Erf   c                 R    | j                   j                  | j                  d         S r   r9  r   s    rd   r   z_mpc.<lambda>}  r:  rf   c                 b    t        | j                  d         t        | j                  d         fS Nru   r   )r   r5  r   s    rd   r   z_mpc.__getstate__  s'    4::a=);tzz!}+EEErf   c                 F    t        |d         t        |d         f| _        y r=  )r   r5  r   s     rd   r   z_mpc.__setstate__  s     "3q6*M#a&,AA
rf   c                     | j                   j                  rt        |       S t        | j                        dd }t        | j
                        dd }t        |       j                  d|d|dS )Nrv   z(real=z, imag=))rw   r   r   r   r  r  rz   rg   )r   ris      rd   r   z_mpc.__repr__  sW    99q6ML2L2)-a)9)91a@@rf   c                 \    dt        | j                  | j                  j                        z  S )Nz(%s))r8   r5  rw   r   r   s    rd   r   z_mpc.__str__  s"    
177AII,A,ABBBrf   c                 ^    t        | j                  | j                  j                  d         S r   )r9   r5  rw   rx   r   s    rd   r   z_mpc.__complex__  s"    agg199+C+CA+FGGrf   c                 t    | j                   \  }}\  }} ||      }t        | j                  ||      |_        |S r`   )r   r;   r5  r   s         rd   r   z_mpc.__pos__  r   rf   c                     | j                   j                  \  }}t        | j                   j                        }t	        | j
                  ||      |_        |S r`   )rw   rx   r{   r7  r?   r5  ro   )r   rq   rs   r   s       rd   r   z_mpc.__abs__  s@    11h		!''42rf   c                 t    | j                   \  }}\  }} ||      }t        | j                  ||      |_        |S r`   )r   r=   r5  r   s         rd   r   z_mpc.__neg__  r   rf   c                 t    | j                   \  }}\  }} ||      }t        | j                  ||      |_        |S r`   )r   r>   r5  r   s         rd   r  z_mpc.conjugate  s9    %&ZZ"S"4Hx8rf   c                 ,    t        | j                        S r`   )r<   r5  r   s    rd   r   z_mpc.__nonzero__  s    agg&&rf   c                 ,    t        | j                        S r`   )r:   r5  r   s    rd   r   z_mpc.__hash__  s      rf   c                 h    	 | j                   j                  |      }|S # t        $ r	 t        cY S w xY wr`   )rw   r   r   r   )rb   r   ys      rd   mpc_convert_lhsz_mpc.mpc_convert_lhs  s5    	"##A&AH 	"!!	"s    11c                     t        |d      s,t        |t              ry| j                  |      }|t        u r|S | j
                  |j
                  k(  xr | j                  |j                  k(  S )Nr5  F)r   r   r   rN  r   r  r  r   s     rd   r   z_mpc.__eq__  s[    q'"!S!!!!$AN"vv4AFFaff$44rf   c                 >    | j                  |      }|t        u r|S | S r`   r   )r   r   r   s      rd   r   z_mpc.__ne__  r   rf   c                      t        d      )Nz3no ordering relation is defined for complex numbers)r   )r  s    rd   _comparez_mpc._compare  s    MNNrf   c                 R   | j                   \  }}\  }}t        |d      sX| j                  |      }|t        u r|S t        |d      r1 ||      }t	        | j
                  |j                  ||      |_        |S  ||      }t        | j
                  |j
                  ||      |_        |S Nr5  ro   )r   r   rN  r   rA   r5  ro   r@   r   s          rd   r  z_mpc.__add__      %&ZZ"S"4q'"!!!$AN"q'"H%aggqwwhGH!''177D(;rf   c                 R   | j                   \  }}\  }}t        |d      sX| j                  |      }|t        u r|S t        |d      r1 ||      }t	        | j
                  |j                  ||      |_        |S  ||      }t        | j
                  |j
                  ||      |_        |S rT  )r   r   rN  r   rC   r5  ro   rB   r   s          rd   r  z_mpc.__sub__  rU  rf   c                    | j                   \  }}\  }}t        |d      st        |t              r' ||      }t	        | j
                  |||      |_        |S | j                  |      }|t        u r|S t        |d      r1 ||      }t        | j
                  |j                  ||      |_        |S | j                  |      } ||      }t        | j
                  |j
                  ||      |_        |S rT  )r   r   r   r   rF   r5  rN  r   rE   ro   rD   r   s          rd   r  z_mpc.__mul__  s    %&ZZ"S"4q'"!Y'H%aggq$A!!!$AN"q'"H%aggqwwhG!!!$AH!''177D(;rf   c                 R   | j                   \  }}\  }}t        |d      sX| j                  |      }|t        u r|S t        |d      r1 ||      }t	        | j
                  |j                  ||      |_        |S  ||      }t        | j
                  |j
                  ||      |_        |S rT  )r   r   rN  r   rH   r5  ro   rG   r   s          rd   r   z_mpc.__div__   rU  rf   c                    | j                   \  }}\  }}t        |t              r' ||      }t        | j                  |||      |_        |S | j                  |      }|t        u r|S  ||      }t        |d      r)t        | j                  |j                  ||      |_        |S t        | j                  |j                  ||      |_        |S r   )r   r   r   rK   r5  rN  r   r   rJ   ro   rI   r   s          rd   r"  z_mpc.__pow__  s    %&ZZ"S"4a#CA!!''1dH=AGHa HH1g!!''177D(CAG  aggqwwh?AGrf   c                 B    | j                  |      }|t        u r|S || z
  S r`   rN  r   r   s     rd   r   z_mpc.__rsub__   r   rf   c                     | j                   \  }}\  }}t        |t              r' ||      }t        | j                  |||      |_        |S | j                  |      }|t        u r|S || z  S r`   )r   r   r   rF   r5  rN  r   r   s          rd   __rmul__z_mpc.__rmul__&  sk    %&ZZ"S"4a#CA!!''1dH=AGHa H1urf   c                 B    | j                  |      }|t        u r|S || z  S r`   r[  r   s     rd   r   z_mpc.__rdiv__1  r   rf   c                 B    | j                  |      }|t        u r|S || z  S r`   r[  r   s     rd   r   z_mpc.__rpow__7  r   rf   Nc                 >    | j                   j                  | |||      S r`   r   r   s       rd   r  z_mpc.ae@  r  rf   )ru   ru   r	  )*rg   rh   ri   rj   rk   re   r  r  r  r   r   r   r   r   r   r   r   r  r   r  r   r
  rN  r   r   rR  r   r   r   r  r  r  r   r"  __radd__r   r]  r   r   __truediv____rtruediv__r  rl   rf   rd   r4  r4  g  s     	I
 EFDEFDFBACH' H! " "5O FFFF&  H	 KL:rf   r4  c                       e Zd Zd Zd Zd Zd Zd Zd Z e	d e      Z
 e	d e      Zdd	Zd
 Zd Zd Zd ZddZddZddZddZed        Zd Zd Zd Zy)PythonMPContextc                    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                  _	        y )N5   r7  r   r   )r   rx   rz   rn   r7  r4  r   r{   r   rw   r$  r   ctxs    rd   __init__zPythonMPContext.__init__I  s     -0utgr*utgr*GGS#*<*<=GGS#*<*<=Jb9!$#s/A/A B"rf   c                 >    t        | j                        }||_        |S r`   )r{   r7  ro   ri  r   r   s      rd   r   zPythonMPContext.make_mpfU      Lrf   c                 >    t        | j                        }||_        |S r`   )r{   r   r5  rl  s      rd   make_mpczPythonMPContext.make_mpcZ  rm  rf   c                 L    dx| _         | j                  d<   d| _        d| _        y )Nrg  ru   r1  F)_precrx   _dpstrap_complexrh  s    rd   defaultzPythonMPContext.default_  s(    ,..	C&&q) rf   c                 v    t        dt        |            x| _        | j                  d<   t	        |      | _        y )Nr   ru   )maxr   rq  rx   r   rr  ri  ns     rd   	_set_preczPythonMPContext._set_precd  s.    ,/3q6N:	C&&q)q>rf   c                 v    t        |      x| _        | j                  d<   t        dt	        |            | _        y r=  )r   rq  rx   rv  r   rr  rw  s     rd   _set_dpszPythonMPContext._set_dpsh  s.    ,7N:	C&&q)q#a&>rf   c                     | j                   S r`   )rq  rh  s    rd   r   zPythonMPContext.<lambda>l  s    		rf   c                     | j                   S r`   )rr  rh  s    rd   r   zPythonMPContext.<lambda>m  s    sxxrf   c                 4   t        |      | j                  v r|S t        |t              r| j	                  t        |            S t        |t              r| j	                  t        |            S t        |t              r9| j                  t        |j                        t        |j                        f      S t        |      j                  dk(  r| j                  |      S t        |t        j                        r<t!        j"                  t%        |j&                        t%        |j(                              }| j*                  \  }}t        |t         j"                        r+|j,                  \  }}| j	                  t/        |||            S |r/t        |t0              r	 t3        |||      }| j	                  |      S t7        |d      r| j	                  |j8                        S t7        |d      r| j                  |j:                        S t7        |d      r!| j=                  |j?                  ||            S t        |      j                  dk(  r| j	                  tA        |||            S | jC                  ||      S #  Y KxY w# t4        $ r Y w xY w#  Y -xY w)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')

        numpyro   r5  r   decimal)"rz   typesr   r   r   r   r   r   r   ro  r  r  rh   	npconvertnumbersRationalr[   r   r   	numeratordenominatorrx   r   r   r   r   r~   r   ro   r5  r   r   r   _convert_fallback)ri  r   stringsrq   rs   r   r   ro   s           rd   r   zPythonMPContext.converto  s   , 7ciia#CLL!,E%EaZ](C!Ca!<<AFF!3Z5G HII7(q1A*Aa))*\\#akk"2C4FG++ha&77DAq<<aD 9::z!Z0 D(3||E** 1gs||AGG'< <1gs||AGG'< <1j!;;qzz$9::7*\!T8%DEE$$Q00% D   Ds*   :<I< J J <J	JJJc                    ddl }t        ||j                        r#| j                  t	        t        |                  S t        ||j                        r| j                  t        |            S t        ||j                        r9| j                  t        |j                        t        |j                        f      S t        dt        |      z         )z^
        Converts *x* to an ``mpf`` or ``mpc``. *x* should be a numpy
        scalar.
        ru   Nr   )r  r   integerr   r   r   floatingr   complexfloatingro  r  r  r   r   )ri  r   r  s      rd   r  zPythonMPContext.npconvert  s    
 	a$S\\(3q6:J-K&Ka%cll<?.K'Ka++,<<aff!5|AFF7K LMM1DG;<<rf   c                 \   t        |d      r|j                  t        k(  S t        |d      rt        |j                  v S t	        |t
              st	        |t        j                        ry| j                  |      }t        |d      st        |d      r| j                  |      S t        d      )a  
        Return *True* if *x* is a NaN (not-a-number), or for a complex
        number, whether either the real or complex part is NaN;
        otherwise return *False*::

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

        ro   r5  Fzisnan() needs a number as input)r   ro   r!   r5  r   r   r[   r   r   isnanr   )ri  r   s     rd   r  zPythonMPContext.isnan  s    " 1g77d?"1g177?"a#z!X\\'BKKN1g'!W"599Q<9::rf   c                    t        |d      r|j                  t        t        fv S t        |d      r-|j                  \  }}|t        t        fv xs |t        t        fv S t        |t              st        |t        j                        ry| j                  |      }t        |d      st        |d      r| j                  |      S t        d      )a  
        Return *True* if the absolute value of *x* is infinite;
        otherwise return *False*::

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

        ro   r5  Fzisinf() needs a number as input)r   ro   r   r    r5  r   r   r[   r   r   isinfr   )ri  r   reims       rd   r  zPythonMPContext.isinf  s    ( 1g77tUm++1gWWFB$&="u*==a#z!X\\'BKKN1g'!W"599Q<9::rf   c                    t        |d      rt        |j                  d         S t        |d      rG|j                  \  }}t        |d         }t        |d         }|t        k(  r|S |t        k(  r|S |xr |S t        |t              st        |t        j                        rt        |      S | j                  |      }t        |d      st        |d      r| j                  |      S t        d      )a  
        Determine whether *x* is "normal" in the sense of floating-point
        representation; that is, return *False* if *x* is zero, an
        infinity or NaN; otherwise return *True*. By extension, a
        complex number *x* is considered "normal" if its magnitude is
        normal::

            >>> from mpmath import *
            >>> isnormal(3)
            True
            >>> isnormal(0)
            False
            >>> isnormal(inf); isnormal(-inf); isnormal(nan)
            False
            False
            False
            >>> isnormal(0+0j)
            False
            >>> isnormal(0+3j)
            True
            >>> isnormal(mpc(2,nan))
            False
        ro   r   r5  z"isnormal() needs a number as input)r   boolro   r5  r   r   r   r[   r   r   isnormalr   )ri  r   r  r  	re_normal	im_normals         rd   r  zPythonMPContext.isnormal  s    0 1g
##1gWWFBRUIRUIU{9,U{9,**a#z!X\\'B7NKKN1g'!W"5<<?"<==rf   c                 V   t        |t              ryt        |d      r0|j                  x\  }}}}}t	        |xr |dk\  xs	 |t
        k(        S t        |d      rZ|j                  \  }}	|\  }
}}}|	\  }}}}|xr |dk\  xs	 |t
        k(  }|r|xr |dk\  xs	 |	t
        k(  }|xr |S |xr	 |	t
        k(  S t        |t        j                        r|j                  \  }}||z  dk(  S | j                  |      }t        |d      st        |d      r| j                  ||      S t        d      )a
  
        Return *True* if *x* is integer-valued; otherwise return
        *False*::

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

        Optionally, Gaussian integers can be checked for::

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

        Tro   ru   r5  zisint() needs a number as input)r   r   r   ro   r  r   r5  r[   r   r   r   isintr   )ri  r   gaussianr   r   r   r   xvalr  r  rsignrmanrexprbcisignimaniexpibcre_isintim_isintr   r   s                         rd   r  zPythonMPContext.isint  s3   2 a#1g()/D#sB);dem<<1gWWFB%'"E4s%'"E4s*:rU{H .TQY>2;,H,+e+a&77DAqq5A:KKN1g'!W"599Q))9::rf   c                    | j                   \  }}g }g }|D ]o  }dx}	}
t        |d      r|j                  }	nht        |d      r|j                  \  }	}
nL| j	                  |      }t        |d      r|j                  }	n"t        |d      r|j                  \  }	}
nt
        |
r|rq|r7|j                  t        |	|	             |j                  t        |
|
             t        |	|
fd|dz         \  }	}
|j                  |	       |j                  |
       |r|j                  t        |	|
f|             |j                  |	       |j                  |
       C|rt        |	|	      }	n|rt        |	      }	|j                  |	       r t        ||||      }|r | j                  |t        |||      f      }|S | j                  |      }|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.
        ru   ro   r5  rt   
   )rx   r   ro   r5  r   ra   appendr'   rK   r?   r"   r5   ro  r   )ri  termsabsolutesquaredrq   r   r  r  termrevalimvalr   s               rd   fsumzPythonMPContext.fsum@  s     &&	cDEEtW%

w'#zzu{{4(4) JJET7+#'::LE5--GE%$89GE%$89'2E%=47'KuE*E*KKut <=KK&KK&#E51E#ENEE"C D D$X.atS!9:;A  QArf   Nc           	         |t        ||      }| j                  \  }}g }g }t        }| j                  | j                  f}	|D ]  \  }
}t        |
      |	vr| j                  |
      }
t        |      |	vr| j                  |      } ||
d      } ||d      }|r2|r0|j                  t        |
j                  |j                                ||
d      } ||d      }|ra|r_|
j                  }|j                  \  }}|rt        |      }|j                  t        ||             |j                  t        ||             |rU|rS|
j                  \  }}|j                  }|j                  t        ||             |j                  t        ||             U|r|r|
j                  \  }}|j                  \  }}|rt        |      }|j                  t        ||             |j                  t        t        ||                   |j                  t        ||             |j                  t        ||             t         t        |||      }|r | j                  |t        |||      f      }|S | j                  |      }|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')

        ro   r5  )ziprx   r   r7  r   rz   r   r  r'   ro   r5  r$   ra   r5   ro  r   )ri  ABr  rq   r   r  r  hasattr_r  r   r   a_realb_real	a_complex	b_complexavalbrebimareaimbvalr   s                          rd   fdotzPythonMPContext.fdot|  s2   N =Aq	A&&	c#''"DAqAwe#QQAwe#QQa)Fa)F&GAGGQWW56 G,I G,I)ww77S!#,CGD#./GD#./I77SwwGC./GC./y77S77S!#,CGC-.GGC$567GC-.GC-.))C D D$$atS!9:;A  QArf   c                       fd}j                   dd t        j                  j                  d|z        |_        |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                 \   t        |       j                  vrj                  |       } j                  \  }}|r6|j	                  d|      }d|v rt        |d         }|j	                  d|      }t        | d      r$	 j                   | j                  ||            S t        | d      r#j                   | j                  ||            S t        dt        |             # t        $ r9 j                  r j                   | j                  t        f||            cY S w xY w)Nrq   rr   rs   ro   r5  z of a )rz   r  r   rx   ry   r   r   r   ro   r   rs  ro  r   r5  ra   )r   r   rq   rs   ri  mpc_fmpf_fr  s       rd   fz/PythonMPContext._wrap_libmp_function.<locals>.f  s   Awcii'KKN //ND(zz&$/F?&ve}5D!::j(;q'"Q<<aggtX(FGG G$||E!''4$BCC%dDG&DEE % Q''<<qww.>h(OPP	Qs   >"C) )?D+*D+rv   NzComputes the %s of x)rg   r\   __dict__ry   rj   )ri  r  r  mpi_fdocr  r  s   ```   @rd   _wrap_libmp_functionz$PythonMPContext._wrap_libmp_function  s@    	F( ~~ab!!**..t5Kc5QR	rf   c                     |rfd}n}t         j                  j                  |j                        |_        t	        | ||       y )Nc                     | j                   }|D cg c]
  } ||       }}| j                  }	 | xj                  dz  c_         | g|i |}|| _        |S c c}w # || _        w xY w)Nr  )r   rq   )ri  r  r   r   r   rq   retvalr  s          rd   	f_wrappedz0PythonMPContext._wrap_specfun.<locals>.f_wrapped  sq    ++,01Dq
D1xx$HHNHs4T4V4F#CHw 2  $CHs   A!A   	A))r\   r  ry   rj   setattr)rb   r  r  wrapr  s     `  rd   _wrap_specfunzPythonMPContext._wrap_specfun  s=    	 I)2266tQYYG	T9%rf   c                 t   t        |d      r|j                  \  }}|t        k7  r&|dfS t        |d      r|j                  }nt	        |      t
        v rt        |      dfS d }t        |t              r|\  }}nZt        |d      r|j                  \  }}n>t        |t              r.d|v r*|j                  d      \  }}t        |      }t        |      }| |z  s||z  dfS | j                  ||      dfS | j                  |      }t        |d      r|j                  \  }}|t        k7  r!|dfS t        |d      r|j                  }n|dfS |\  }}}}	|r^|d	k\  rD|r| }|d
k\  rt        |      |z  dfS |d	k\  r%t        |      d| z  }}| j                  ||      dfS | j                  |      }|dfS |sy|dfS )Nr5  Cro   Zr   /QUru   r   R)ru   r  )r   r5  r   ro   rz   r   r   r   r|   r   r   splitr   r   r   )
ri  r   r   r  r   r   r   r   r   r   s
             rd   _convert_paramzPythonMPContext._convert_param  s   1gGGEArU{#vQ AAw)#1vs{"A!U#1G$ww1Az*saxwws|1FF}1u63;&wwq|S((AAq'"2;c6MG$GG#vc3by$C!8s8s?C//"9s8a3$iqA771Q<,,QAc6Mc6Mrf   c                     |\  }}}}|r||z   S |t         k(  r| j                  S |t        k(  s	|t        k(  r| j                  S | j
                  S r`   )r   ninfr   r    infnan)ri  r   r   r   r   r   s         rd   _mpf_magzPythonMPContext._mpf_mag9  sK    c3r6M:88O9U
77Nwwrf   c                    t        |d      r| j                  |j                        S t        |d      rp|j                  \  }}|t        k(  r| j                  |      S |t        k(  r| j                  |      S dt        | j                  |      | j                  |            z   S t        |t              r"|rt        t        |            S | j                  S t        |t        j                        r@|j                  \  }}|r#dt        t        |            z   t        |      z
  S | j                  S | j                  |      }t        |d      st        |d      r| j                  |      S t!        d      )a  
        Quick logarithmic magnitude estimate of a number. Returns an
        integer or infinity `m` such that `|x| <= 2^m`. It is not
        guaranteed that `m` is an optimal bound, but it will never
        be too large by more than 2 (and probably not more than 1).

        **Examples**

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

        ro   r5  r   zrequires an mpf/mpc)r   r  ro   r5  r   rv  r   r   r6   absr  r[   r   r   r   magr   )ri  r   rB  rC  r   r   s         rd   r  zPythonMPContext.magC  s'   * 1g<<((Q 77DAqEz||A&Ez||A&Sa#,,q/:::9%A''88O8<<(77DAq8CF++hqk9988OAAq'"ga&9wwqz! 566rf   )T)F)FF)NF)NNz<no doc>)rg   rh   ri   rj  r   ro  rt  ry  r{  r  rq   rr   r   r  r  r  r  r  r  r  r  r
  r  r  r  r  rl   rf   rd   re  re  G  s    
#

!
"" )95D
'
2C01d
=;8;@&>P-;^:xUn F & &"/b,7rf   re  ru   N)r&  r&  r&  )}libmp.backendr   r   libmpr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   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   rZ   r&  r[   r\   r'  re   r{   r^   rn   r  
return_mpf
return_mpcmpf_pow_samer  r   r  r  r  r   r!  r"  ra  r]  rb  r   rc  r$  r4  r   r   re  r  ComplexregisterRealImportErrorrl   rf   rd   <module>r     sx   -                      .  nn" "C)9 C)J< =
<
 : *9;
 /*<:ZG3j@B
 /*<:ZG8:EG
 /*<4zA3j@B
 /*<:ZG3j@B
 /*<:ZG13
 4zA8:EG
 << MM J J:Z:9 Z:z $h7f h7b	OOT"LL$ 		s   :I I	I	