
    [h,                        S r SrSSKrSSKrSSKJr  SSKJrJr  SSK	J
r
  SSK
JrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/J0r0J1r1J2r2J3r3J4r4J5r5J6r6J7r7J8r8J9r9J:r:J;r;J<r<J=r=J>r>J?r?J@r@JArAJBrBJCrCJDrDJErEJFrFJGrGJHrHJIrIJJrJJKrKJLrLJMrMJNrNJOrOJPrPJQrQJRrRJSrSJTrTJUrUJVrVJWrWJXrXJYrYJZrZJ[r[J\r\J]r]J^r^Jr  SS	K	J_r_  SS
K	J`r`  \aR                  rc\R                  " S5      re\S:X  a  SSKfJgrh  SSKfJis  Jjs  Jkrl  OSSKmJnrh  SSK	Jmrl  SSKmJoroJprpJqrq   " S S\h\5      rr " S S5      rs\tS:X  a  SSKuru\uR                  " 5         gg)z[
This module defines the mpf, mpc classes, and standard functions for
operating with them.
	plaintext    N   )StandardBaseContext)
basestringBACKEND)libmp)U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_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   )function_docs)rationalz\^\(?(?P<re>[\+\-]?\d*(\.\d*)?(e[\+\-]?\d+)?)??(?P<im>[\+\-]?\d*(\.\d*)?(e[\+\-]?\d+)?j)?\)?$sage)Context)PythonMPContext)ctx_mp_python)_mpf_mpc	mpnumericc                      \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS:S jrS rS rS rS rS rS rS rS rS rS rS rS rS r\S 5       r\S 5       rS;S jrS;S jrS;S jr S;S jr!S<S" jr"S=S# jr#S$ r$S% r%S& r&S'r'S(r(S>S) jr)S* r*S+ r+S, r,S- r-S. r.S/ r/S0 r0S1 r1S2 r2S3 r3S4 r4S5 r5S6 r6S7 r7 S8/S4S9 jr8S!r9g )?	MPContext:   z@
Context for multiprecision arithmetic with a global precision.
c                    [         R                  " U 5        SU l        SU l        U R                  U R
                  U R                  /U l        [        R                  U l
        U R                  5         [        R                  " U 5        [        R                  U l	        U R                  5         0 U l        U R                  5          [         R"                  U R"                  R$                  l        [         R(                  U R(                  R$                  l        [         R*                  U R*                  R$                  l        [         R,                  U R,                  R$                  l        [         R2                  U R2                  l        [         R4                  U R4                  l        [         R6                  U R6                  l        g ! [.         a    [         R"                  U R"                  R0                  l        [         R(                  U R(                  R0                  l        [         R*                  U R*                  R0                  l        [         R,                  U R,                  R0                  l         GNf = fNF)BaseMPContext__init__trap_complexprettympfmpcconstanttypesr^   mpq_mpqdefaultr   init_builtinshyp_summators_init_aliasesr]   	bernoulliim_funcfunc_docprimepipsiatan2AttributeError__func__digammacospisinpictxs    E/var/www/auris/envauris/lib/python3.13/site-packages/mpmath/ctx_mp.pyrl   MPContext.__init__?   s   s# 
WWcggs||4	<<$$S),,
	>-:-D-DCMM!!*+8+@+@CKK('4'8'8CGGOO$)6)<)<CII&  -44*00		*00		  	>.;.E.ECMM""+,9,A,ACKK  )(5(9(9CGG%*7*=*=CII'	>s   B$G B.I98I9c                    U R                   nU R                  nU R                  [        5      U l        U R                  [
        5      U l        U R                  [
        [        45      U l        U R                  [        5      U l
        U R                  [        5      U l        U R                  [        5      U l        U R                  S SS5      nX0l        U R                  ["        SS5      U l        U R                  [&        SS5      U l        U R                  [*        SS5      U l        U R                  [.        S	S
5      U l        U R                  [2        SS5      U l        U R                  [6        SS5      U l        U R                  [:        SS5      U l        U R                  [>        SS5      U l         U R                  [B        SS5      U l"        U R                  [F        SS5      U l$        U R                  [J        SS5      U l&        U R                  [N        SS5      U l(        U R                  [R        SS5      U l*        U RW                  [X        RZ                  [X        R\                  5      U l/        U RW                  [X        R`                  [X        Rb                  5      U l2        U RW                  [X        Rf                  [X        Rh                  5      U l5        U RW                  [X        Rl                  [X        Rn                  5      U l8        U RW                  [X        Rr                  [X        Rt                  5      U l;        U RW                  [X        Rx                  [X        Rz                  5      U l>        U RW                  [X        R~                  [X        R                  5      U lA        U RW                  [X        R                  [X        R                  5      U lD        U RW                  [X        R                  [X        R                  5      U lG        U RW                  [X        R                  [X        R                  5      U lJ        U RW                  [X        R                  [X        R                  5      U lM        U RW                  [X        R                  [X        R                  5      U lP        U RW                  [X        R                  [X        R                  5      U lS        U RW                  [X        R                  [X        R                  5      U lV        U RW                  [X        R                  [X        R                  5      U lY        U RW                  [X        Rl                  [X        Rn                  5      U l8        U RW                  [X        R                  [X        R                  5      U l\        U RW                  [X        R                  [X        R                  5      U l_        U RW                  [X        R                  [X        R                  5      U lb        U RW                  [X        R                  [X        R                  5      U le        U RW                  [X        R                  [X        R                  5      U lh        U RW                  [X        R                  [X        R                  5      U lk        U RW                  [X        R                  [X        R                  5      U ln        U RW                  [X        R                  [X        R                  5      U lq        U RW                  [X        R                  [X        R                  5      U lt        U RW                  [X        R                  [X        R                  5      =U lw        U lx        U RW                  [X        R                  [X        R                  5      U l{        U RW                  [X        R                  [X        R                  5      U l~        U RW                  [X        R                  [X        GR                   5      U l        U RW                  [X        GR                  [X        GR                  5      =U l        U l        U RW                  [X        GR                  [X        GR                  5      U l        U RW                  [X        GR                  [X        GR                  5      U l        U RW                  [X        GR                  [X        GR                  5      U l        U RW                  [X        GR                  [X        GR                   5      U l        U RW                  [X        GR$                  [X        GR&                  5      U l        U RW                  [X        GR*                  [X        GR,                  5      U l        U RW                  [X        GR0                  [X        GR2                  5      U l        U RW                  [X        GR6                  [X        GR8                  5      U l        U RW                  [X        GR<                  [X        GR>                  5      U l        U RW                  [X        GRB                  S 5      U l        U RW                  [X        GRF                  S 5      U l        U RW                  [X        GRJ                  [X        GRL                  5      U l        U RW                  [X        GRP                  [X        GRR                  5      U l        G[W        U SU R^                  5      U l/        G[W        U SU Rv                  5      U l;        G[W        U SU Rj                  5      U l5        G[W        U S U R                  5      U lG        G[W        U S!U R                  5      U lD        g )"Nc                     S[         SU -
  S4$ )Nr   r   )r   )precrnds     r   <lambda>)MPContext.init_builtins.<locals>.<lambda>m   s    a!D&!-D    zepsilon of working precisionepspizln(2)ln2zln(10)ln10zGolden ratio phiphiz
e = exp(1)ezEuler's constanteulerzCatalan's constantcatalanzKhinchin's constantkhinchinzGlaisher's constantglaisherzApery's constantaperyz1 deg = pi / 180degreezTwin prime constant	twinprimezMertens' constantmertens
_sage_sqrt	_sage_exp_sage_ln	_sage_cos	_sage_sin)ro   rp   make_mpfr   oner    zeromake_mpcjr!   infr"   ninfr#   nanrq   r   rP   r   rT   r   rU   r   rS   r   rR   r   rV   r   rW   r   rY   r   rZ   r   rX   r   rQ   r   r[   r   r\   r   _wrap_libmp_functionr   mpf_sqrtmpc_sqrtsqrtmpf_cbrtmpc_cbrtcbrtmpf_logmpc_loglnmpf_atanmpc_atanatanmpf_expmpc_expexpmpf_expjmpc_expjexpj
mpf_expjpi
mpc_expjpiexpjpimpf_sinmpc_sinsinmpf_cosmpc_coscosmpf_tanmpc_tantanmpf_sinhmpc_sinhsinhmpf_coshmpc_coshcoshmpf_tanhmpc_tanhtanhmpf_asinmpc_asinasinmpf_acosmpc_acosacos	mpf_asinh	mpc_asinhasinh	mpf_acosh	mpc_acoshacosh	mpf_atanh	mpc_atanhatanh
mpf_sin_pi
mpc_sin_pir   
mpf_cos_pi
mpc_cos_pir   	mpf_floor	mpc_floorfloormpf_ceilmpc_ceilceilmpf_nintmpc_nintnintmpf_fracmpc_fracfracmpf_fibonaccimpc_fibonaccifib	fibonacci	mpf_gamma	mpc_gammagamma
mpf_rgamma
mpc_rgammargammampf_loggammampc_loggammaloggammampf_factorialmpc_factorialfac	factorialmpf_psi0mpc_psi0r   mpf_harmonicmpc_harmonicharmonicmpf_eimpc_eieimpf_e1mpc_e1e1mpf_cimpc_ci_cimpf_simpc_si_si
mpf_ellipk
mpc_ellipkellipk
mpf_ellipe
mpc_ellipe_ellipempf_agm1mpc_agm1agm1mpf_erf_erfmpf_erfc_erfcmpf_zetampc_zeta_zetampf_altzetampc_altzeta_altzetagetattr)r   ro   rp   r   s       r   rv   MPContext.init_builtins`   s   gggg ,,t$<<&eD\*,,t$<<&,,t$llD*E3 fdD1,,w7<<(F;,,w(:EBUL#6LL,>H	ll;0DiP||L2GT||L2GTLL,>H	\\*.@(K
]4I;Wll;0CYO ++ENNENNK++ENNENNK))%--G++ENNENNK**5==%--H++ENNENNK--e.>.>@P@PQ
**5==%--H**5==%--H**5==%--H++ENNENNK++ENNENNK++ENNENNK++ENNENNK++ENNENNK++ENNENNK,,U__eooN	,,U__eooN	,,U__eooN	,,U-=-=u?O?OP	,,U-=-=u?O?OP	,,U__eooN	++ENNENNK++ENNENNK++ENNENNK"%":":5;N;NPUPcPc"dd#-,,U__eooN	--e.>.>@P@PQ
//0B0BEDVDVW"%":":5;N;NPUPcPc"dd#-..u~~u~~N//0B0BEDVDVW))%,,E))%,,E**5<<F**5<<F--e.>.>@P@PQ
..u/?/?AQAQR++ENNENNK++EMM4@,,U^^TB	,,U^^U^^L	//0A0A5CTCTU 3chh7#{CGG4j#&&1#{CGG4#{CGG4r   c                 $    UR                  U5      $ N)r9   )r   xr   s      r   r9   MPContext.to_fixed   s    zz$r   c                     U R                  U5      nU R                  U5      nU R                  [        R                  " UR                  UR                  /U R
                  Q76 5      $ )zp
Computes the Euclidean norm of the vector `(x, y)`, equal
to `\sqrt{x^2 + y^2}`. Both `x` and `y` must be real.)convertr   r   	mpf_hypot_mpf__prec_rounding)r   r*  ys      r   hypotMPContext.hypot   sK     KKNKKN||EOOAGGQWWRs?Q?QRSSr   c                 >   [        U R                  U5      5      nUS:X  a  U R                  U5      $ [        US5      (       d  [        eU R
                  u  p4[        R                  " XR                  X4SS9u  pVUc  U R                  U5      $ U R                  XV45      $ )Nr   r/  T)r   )int_rer  hasattrNotImplementedErrorr0  r   
mpf_expintr/  r   r   r   nzr   roundingrealimags          r   _gamma_upper_intMPContext._gamma_upper_int   s    
O666!9q'""%%++%%a$M
<<<%%<<--r   c                 "   [        U5      nUS:X  a  U R                  U5      $ [        US5      (       d  [        eU R                  u  p4[
        R                  " XR                  X45      u  pVUc  U R                  U5      $ U R                  XV45      $ )Nr   r/  )
r5  r  r7  r8  r0  r   r9  r/  r   r   r:  s          r   _expint_intMPContext._expint_int   s}    F666!9q'""%%++%%a$A
<<<%%<<--r   c                    [        US5      (       a<   U R                  [        R                  " UR                  U/U R
                  Q76 5      $ UR                  nU R                  [        R                  " X/U R
                  Q76 5      $ ! [         a1    U R                  (       a  e UR                  [        R                  4n Njf = fNr/  )r7  r   r   mpf_nthrootr/  r0  r   rm   r    _mpc_r   mpc_nthrootr   r*  r;  s      r   _nthrootMPContext._nthroot   s    1g+||E$5$5aggq$V3CUCU$VWW A||E--aHS5G5GHII ! +##WWekk*+s   :B
 
8CCc                 $   U R                   u  p4[        US5      (       a0  U R                  [        R                  " XR
                  X45      5      $ [        US5      (       a0  U R                  [        R                  " XR                  X45      5      $ g Nr/  rH  )	r0  r7  r   r   mpf_besseljnr/  r   mpc_besseljnrH  )r   r;  r<  r   r=  s        r   _besseljMPContext._besselj   sn    ++1g<< 2 21ggt NOOQ  << 2 21ggt NOO !r   c                 2   U R                   u  p4[        US5      (       aO  [        US5      (       a>   [        R                  " UR                  UR                  X45      nU R                  U5      $ [        US5      (       a  UR                  [        R                  4nOUR                  n[        US5      (       a  UR                  [        R                  4nOUR                  nU R                  [        R                  " XX45      5      $ ! [         a     Nf = frF  )r0  r7  r   mpf_agmr/  r   r   r    rH  r   mpc_agm)r   abr   r=  vs         r   _agmMPContext._agm   s    ++1g71g#6#6MM!''177DC||A& 1gQWWekk$:''a1gQWWekk$:''a||EMM!?@@ ! s   <D	 	
DDc                 t    U R                  [        R                  " [        U5      /U R                  Q76 5      $ r)  )r   r   mpf_bernoullir5  r0  r   r;  s     r   ry   MPContext.bernoulli   s+    ||E//AL9K9KLMMr   c                 t    U R                  [        R                  " [        U5      /U R                  Q76 5      $ r)  )r   r   mpf_zeta_intr5  r0  r]  s     r   	_zeta_intMPContext._zeta_int   s+    ||E..s1vK8J8JKLLr   c                     U R                  U5      nU R                  U5      nU R                  [        R                  " UR                  UR                  /U R
                  Q76 5      $ r)  )r-  r   r   	mpf_atan2r/  r0  )r   r1  r*  s      r   r~   MPContext.atan2  sI    KKNKKN||EOOAGGQWWRs?Q?QRSSr   c                 N   U R                  U5      n[        U5      nU R                  U5      (       a:  U R                  [        R
                  " XR                  /U R                  Q76 5      $ U R                  [        R                  " XR                  /U R                  Q76 5      $ r)  )r-  r5  _is_real_typer   r   mpf_psir/  r0  r   mpc_psirH  )r   mr<  s      r   r}   MPContext.psi  sw    KKNFQ<<a N3;M;M NOO<<a N3;M;M NOOr   c                     [        U5      U R                  ;  a  U R                  U5      nU R                  U5      u  p4[	        US5      (       aE  [
        R                  " UR                  X45      u  pVU R                  U5      U R                  U5      4$ [	        US5      (       aE  [
        R                  " UR                  X45      u  pVU R                  U5      U R                  U5      4$ U R                  " U40 UD6U R                  " U40 UD64$ rN  )typerr   r-  _parse_precr7  r   mpf_cos_sinr/  r   mpc_cos_sinrH  r   r   r   r   r*  kwargsr   r=  css          r   cos_sinMPContext.cos_sin  s    7#))#AA01g$$QWWd=DA<<?CLLO33Q  $$QWWd=DA<<?CLLO33771'')=f)===r   c                     [        U5      U R                  ;  a  U R                  U5      nU R                  U5      u  p4[	        US5      (       aE  [
        R                  " UR                  X45      u  pVU R                  U5      U R                  U5      4$ [	        US5      (       aE  [
        R                  " UR                  X45      u  pVU R                  U5      U R                  U5      4$ U R                  " U40 UD6U R                  " U40 UD64$ rN  )rm  rr   r-  rn  r7  r   mpf_cos_sin_pir/  r   mpc_cos_sin_pirH  r   r   r   rq  s          r   cospi_sinpiMPContext.cospi_sinpi  s    7#))#AA01g''@DA<<?CLLO33Q  ''@DA<<?CLLO33771'')=f)===r   c                 H    U R                  5       nU R                  Ul        U$ )z@
Create a copy of the context, with the same working precision.
)	__class__r   )r   rV  s     r   cloneMPContext.clone)  s     MMOr   c                 L    [        US5      (       d  [        U5      [        L a  gg)NrH  FTr7  rm  complexr   r*  s     r   rg  MPContext._is_real_type4  s     1g$q'W"4r   c                 L    [        US5      (       d  [        U5      [        L a  gg)NrH  TFr  r  s     r   _is_complex_typeMPContext._is_complex_type9  s     1g$q'W"4r   c                    [        US5      (       a  UR                  [        :H  $ [        US5      (       a  [        UR                  ;   $ [	        U[
        5      (       d  [	        U[        R                  5      (       a  gU R                  U5      n[        US5      (       d  [        US5      (       a  U R                  U5      $ [        S5      e)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

r/  rH  Fzisnan() needs a number as input)r7  r/  r#   rH  
isinstancer   r^   rs   r-  isnan	TypeErrorr  s     r   r  MPContext.isnan>  s    " 1g77d?"1g177?"a##z!X\\'B'BKKN1g'!W"5"599Q<9::r   c                 ^    U R                  U5      (       d  U R                  U5      (       a  gg)aX  
Return *True* if *x* is a finite number, i.e. neither
an infinity or a NaN.

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

FT)isinfr  r  s     r   isfiniteMPContext.isfiniteZ  s#    , 99Q<<399Q<<r   c                    U(       d  g[        US5      (       a  UR                  u  p#pEU=(       a    US:  $ [        US5      (       a3  UR                  (       + =(       a    U R                  UR                  5      $ [        U5      [        ;   a  US:*  $ [        XR                  5      (       a'  UR                  u  pgU(       d  gUS:H  =(       a    US:*  $ U R                  U R                  U5      5      $ )z,
Determine if *x* is a nonpositive integer.
Tr/  r   rH  r   )r7  r/  r?  isnpintr>  rm  r   r  rs   _mpq_r-  )r   r*  signmanr   bcpqs           r   r  MPContext.isnpintt  s     1g!"Ds$C1H$1gvv:5#++aff"557i6Ma!!77DA6$a1f${{3;;q>**r   c                     SSU R                   -  R                  S5      S-   SU R                  -  R                  S5      S-   SU R                  -  R                  S5      S-   /nS	R	                  U5      $ )
NzMpmath settings:z  mp.prec = %s   z[default: 53]z  mp.dps = %sz[default: 15]z  mp.trap_complex = %sz[default: False]
)r   ljustdpsrm   join)r   liness     r   __str__MPContext.__str__  st    #(//3oEsww&--b1OC%(8(88??CFXX

 yyr   c                 ,    [        U R                  5      $ r)  )r   _precr   s    r   _repr_digitsMPContext._repr_digits  s    		""r   c                     U R                   $ r)  )_dpsr   s    r   _str_digitsMPContext._str_digits  s    xxr   Fc                 (   ^ [        U U4S jSU5      $ )a  
The block

    with extraprec(n):
        <code>

increases the precision n bits, executes <code>, and then
restores the precision.

extraprec(n)(f) returns a decorated version of the function f
that increases the working precision by n bits before execution,
and restores the parent precision afterwards. With
normalize_output=True, it rounds the return value to the parent
precision.
c                    > U T-   $ r)   r  r;  s    r   r   %MPContext.extraprec.<locals>.<lambda>  s	    q1ur   NPrecisionManagerr   r;  normalize_outputs    ` r   	extraprecMPContext.extraprec  s       _d<LMMr   c                 (   ^ [        U SU4S jU5      $ )z~
This function is analogous to extraprec (see documentation)
but changes the decimal precision instead of the number of bits.
Nc                    > U T-   $ r)  r  dr;  s    r   r   $MPContext.extradps.<locals>.<lambda>  s	    QUr   r  r  s    ` r   extradpsMPContext.extradps  s    
  T?<LMMr   c                 (   ^ [        U U4S jSU5      $ )ak  
The block

    with workprec(n):
        <code>

sets the precision to n bits, executes <code>, and then restores
the precision.

workprec(n)(f) returns a decorated version of the function f
that sets the precision to n bits before execution,
and restores the precision afterwards. With normalize_output=True,
it rounds the return value to the parent precision.
c                    > T$ r)  r  r  s    r   r   $MPContext.workprec.<locals>.<lambda>  s    qr   Nr  r  s    ` r   workprecMPContext.workprec  s      [$8HIIr   c                 (   ^ [        U SU4S jU5      $ )z}
This function is analogous to workprec (see documentation)
but changes the decimal precision instead of the number of bits.
Nc                    > T$ r)  r  r  s    r   r   #MPContext.workdps.<locals>.<lambda>  s    Qr   r  r  s    ` r   workdpsMPContext.workdps  s    
  T;8HIIr   Nr  c                 $   ^ ^^^^ UU UUU4S jnU$ )a  
Return a wrapped copy of *f* that repeatedly evaluates *f*
with increasing precision until the result converges to the
full precision used at the point of the call.

This heuristically protects against rounding errors, at the cost of
roughly a 2x slowdown compared to manually setting the optimal
precision. This method can, however, easily be fooled if the results
from *f* depend "discontinuously" on the precision, for instance
if catastrophic cancellation can occur. Therefore, :func:`~mpmath.autoprec`
should be used judiciously.

**Examples**

Many functions are sensitive to perturbations of the input arguments.
If the arguments are decimal numbers, they may have to be converted
to binary at a much higher precision. If the amount of required
extra precision is unknown, :func:`~mpmath.autoprec` is convenient::

    >>> from mpmath import *
    >>> mp.dps = 15
    >>> mp.pretty = True
    >>> besselj(5, 125 * 10**28)    # Exact input
    -8.03284785591801e-17
    >>> besselj(5, '1.25e30')   # Bad
    7.12954868316652e-16
    >>> autoprec(besselj)(5, '1.25e30')   # Good
    -8.03284785591801e-17

The following fails to converge because `\sin(\pi) = 0` whereas all
finite-precision approximations of `\pi` give nonzero values::

    >>> autoprec(sin)(pi) # doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
      ...
    NoConvergence: autoprec: prec increased to 2910 without convergence

As the following example shows, :func:`~mpmath.autoprec` can protect against
cancellation, but is fooled by too severe cancellation::

    >>> x = 1e-10
    >>> exp(x)-1; expm1(x); autoprec(lambda t: exp(t)-1)(x)
    1.00000008274037e-10
    1.00000000005e-10
    1.00000000005e-10
    >>> x = 1e-50
    >>> exp(x)-1; expm1(x); autoprec(lambda t: exp(t)-1)(x)
    0.0
    1.0e-50
    0.0

With *catch*, an exception or list of exceptions to intercept
may be specified. The raised exception is interpreted
as signaling insufficient precision. This permits, for example,
evaluating a function where a too low precision results in a
division by zero::

    >>> f = lambda x: 1/(exp(x)-1)
    >>> f(1e-30)
    Traceback (most recent call last):
      ...
    ZeroDivisionError
    >>> autoprec(f, catch=ZeroDivisionError)(1e-30)
    1.0e+30


c                  >  > T	R                   nTc  T	R                  U5      nOTn US-   T	l          T
" U 0 UD6nUS-   n UT	l          T
" U 0 UD6nXF:X  a  OT	R                  Xd-
  5      T	R                  U5      -
  nXr* :  a  OXT(       a  [	        SU< SU< SU* < 35        UnXS:  a  T	R                  SU-  5      eU[        US-  5      -  n[        XS5      nM  UT	l         U7$ ! T a    T	R                  n Nf = f! T a    T	R                  n Nf = f! UT	l         f = f)N
      zautoprec: target=z, prec=z, accuracy=z2autoprec: prec increased to %i without convergence   )r   _default_hyper_maxprecr   magprintNoConvergencer5  min)argsrr  r   maxprec2v1prec2v2errcatchr   fmaxprecverboses           r   f_autoprec_wrapped.MPContext.autoprec.<locals>.f_autoprec_wrapped  sU   88D55d;" "9!D+F+B r	$CH%// x''"%.3772;6Ce}#USD2 3B(!//L ! ! Sq\)E0E) ,  3J5  !B! ! % WW%$  sR   
D C# D C; B
D #C85D 7C88D ;DD DD 	Dr  )r   r  r  r  r  r  s   ````` r   autoprecMPContext.autoprec  s    H$	 $	J "!r   c                 :  ^ ^^ [        U[        5      (       a   SSR                  U UU4S jU 5       5      -  $ [        U[        5      (       a   SSR                  U UU4S jU 5       5      -  $ [	        US5      (       a  [        UR                  T40 TD6$ [	        US5      (       a  S[        UR                  T40 TD6-   S	-   $ [        U[        5      (       a  [        U5      $ [        UT R                  5      (       a  UR                  " T40 TD6$ [        U5      $ )
a;  
Convert an ``mpf`` or ``mpc`` to a decimal string literal with *n*
significant digits. The small default value for *n* is chosen to
make this function useful for printing collections of numbers
(lists, matrices, etc).

If *x* is a list or tuple, :func:`~mpmath.nstr` is applied recursively
to each element. For unrecognized classes, :func:`~mpmath.nstr`
simply returns ``str(x)``.

The companion function :func:`~mpmath.nprint` prints the result
instead of returning it.

The keyword arguments *strip_zeros*, *min_fixed*, *max_fixed*
and *show_zero_exponent* are forwarded to :func:`~mpmath.libmp.to_str`.

The number will be printed in fixed-point format if the position
of the leading digit is strictly between min_fixed
(default = min(-dps/3,-5)) and max_fixed (default = dps).

To force fixed-point format always, set min_fixed = -inf,
max_fixed = +inf. To force floating-point format, set
min_fixed >= max_fixed.

    >>> from mpmath import *
    >>> nstr([+pi, ldexp(1,-500)])
    '[3.14159, 3.05494e-151]'
    >>> nprint([+pi, ldexp(1,-500)])
    [3.14159, 3.05494e-151]
    >>> nstr(mpf("5e-10"), 5)
    '5.0e-10'
    >>> nstr(mpf("5e-10"), 5, strip_zeros=False)
    '5.0000e-10'
    >>> nstr(mpf("5e-10"), 5, strip_zeros=False, min_fixed=-11)
    '0.00000000050000'
    >>> nstr(mpf(0), 5, show_zero_exponent=True)
    '0.0e+0'

z[%s]z, c              3   L   >#    U  H  nTR                   " UT40 TD6v   M     g 7fr)  nstr.0rs  r   rr  r;  s     r   	<genexpr>!MPContext.nstr.<locals>.<genexpr>]  !     &KAsxx1'?'?   !$z(%s)c              3   L   >#    U  H  nTR                   " UT40 TD6v   M     g 7fr)  r  r  s     r   r  r  _  r  r  r/  rH  ())r  listr  tupler7  r   r/  r:   rH  r   reprmatrix__nstr__str)r   r*  r;  rr  s   ` ``r   r  MPContext.nstr4  s    P aTYY&K&KKLLaTYY&K&KKLL1g!''1///1gAGGQ9&99S@@a$$7Na$$::a*6**1vr   c                 N   U(       a  [        U[        5      (       a  SUR                  5       ;   a  UR                  5       R                  SS5      n[        R                  U5      nUR                  S5      nU(       d  SnUR                  S5      R                  S5      nU R                  U R                  U5      U R                  U5      5      $ [        US5      (       a/  UR                  u  pgXg:X  a  U R                  U5      $ [        S5      e[        S	[        U5      -   5      e)
Nr     rer   im_mpi_z,can only create mpf from zero-width intervalzcannot create mpf from )r  r   lowerreplaceget_complexmatchgrouprstriprp   r-  r7  r  r   
ValueErrorr  r  )r   r*  stringsr  r  r  rV  rW  s           r   _convert_fallbackMPContext._convert_fallbackj  s    z!Z00aggiGGI%%c2.#))!,[[&B[[&--c2wws{{2B@@1g77DAv||A& !OPP1DG;<<r   c                 &    U R                   " U0 UD6$ r)  )r-  )r   r  rr  s      r   	mpmathifyMPContext.mpmathify|  s    {{D+F++r   c                 .   U(       a  UR                  S5      (       a  gU R                  u  p#SU;   a  US   nSU;   a$  US   nX R                  :X  a  g[        U5      n X#4$ SU;   a   US   nX@R                  :X  a  g[	        U5      nX#4$ U R                  $ )Nexact)r   r  r=  r   r  )getr0  r   r5  r   )r   rr  r   r=  r  s        r   rn  MPContext._parse_prec  s    zz'"" //NDV#!*-f~77?!t9D >! &Um''>!"3'>!!!!r   z'the exact result does not fit in memoryzhypsum() failed to converge to the requested %i bits of accuracy
using a working precision of %i bits. Try with a higher maxprec,
maxterms, or set zeroprec.c                    [        US5      (       a  XUS4nUR                  n	O"[        US5      (       a  XUS4nUR                  n	WU R                  ;  a&  [        R
                  " U5      S   U R                  U'   U R                  U   n
U R                  nUR                  SU R                  U5      5      nSnSn0 nS	n[        U5       H  u  nnUU   S
:X  aW  UU:  aO  US	::  aI  Sn[        US U 5       H#  u  nnUU   S
:X  d  M  US	::  d  M  UU::  d  M!  SnM%     U(       d  [        S5      eMf  U R                  U5      u  nn[        U5      * nU* nUU:  a7  US	:  a1  US:  a+  UU;   a  UU==   U-  ss'   OUUU'   [        UUU-
  S-   5      nU[        U5      -  nM      X:  a  [        U R                   XU-   4-  5      eX-   nU(       a  [#        S U 5       5      nO0 nU
" UW	UUUU40 UD6u  nnnU* nSnUU:  a$  UR%                  5        H  nUb  UU:  d  M  Sn  O   UUS-
  S-
  :  =(       d    U(       + nU(       aF  U(       a  OOUR                  S5      n U b*  UU :  a$  U(       a  U R'                  S	5      $ U R(                  $ US-  nUS-  nUS-  nM  [+        U5      [,        L a)  U(       a  U R/                  U5      $ U R1                  U5      $ U$ )Nr/  RrH  Cr   r  2      r   ZFTzpole in hypergeometric series   <   c              3   (   #    U  H  oS 4v   M
     g 7fr)  r  )r  r;  s     r   r  #MPContext.hypsum.<locals>.<genexpr>  s     B/Q4/s      zeroprecr  )r7  r/  rH  rw   r   make_hyp_summatorr   r  r  	enumerateZeroDivisionErrornint_distancer5  maxabsr  _hypsum_msgdictvaluesrp   r   rm  r  r   r   )!r   r  r  flagscoeffsr<  accurate_smallrr  keyrX  summatorr   r  r  epsshiftmagnitude_checkmax_total_jumpirs  okiiccr;  r  wpmag_dictzvhave_complex	magnitudecanceljumps_resolvedaccurater  s!                                    r   hypsumMPContext.hypsum  s   1gs"CAQ  s"CAc'''%*%<%<S%A!%DCc"$$S)xx**Y(B(B4(HI	 f%DAqQx36a1fB"+F2AJ"7B 9+aAG!%B #8 /0OPP$$Q'DAqQAAAv!q&QU'#A&!+&)*OA&	1t8b=9	c!f$N) &* " Dy.3I!IJJ!BB/BB*261dB(+.&,+.'BiZF!N>)!*A	q4x). + 2a/E~3EH!::j1'('#&771:-#&88O NIMHNIG J 8u||B''||B''Ir   c                     U R                  U5      nU R                  [        R                  " UR                  U5      5      $ )aF  
Computes `x 2^n` efficiently. No rounding is performed.
The argument `x` must be a real floating-point number (or
possible to convert into one) and `n` must be a Python ``int``.

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = False
    >>> ldexp(1, 10)
    mpf('1024.0')
    >>> ldexp(1, -3)
    mpf('0.125')

)r-  r   r   	mpf_shiftr/  rJ  s      r   ldexpMPContext.ldexp  s/     KKN||EOOAGGQ788r   c                     U R                  U5      n[        R                  " UR                  5      u  p#U R	                  U5      U4$ )z
Given a real number `x`, returns `(y, n)` with `y \in [0.5, 1)`,
`n` a Python integer, and such that `x = y 2^n`. No rounding is
performed.

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = False
    >>> frexp(7.5)
    (mpf('0.9375'), 3)

)r-  r   	mpf_frexpr/  r   )r   r*  r1  r;  s       r   frexpMPContext.frexp   s8     KKNqww'||A!!r   c                 8   U R                  U5      u  p4U R                  U5      n[        US5      (       a%  U R                  [	        UR
                  X45      5      $ [        US5      (       a%  U R                  [        UR                  X45      5      $ [        S5      e)a  
Negates the number *x*, giving a floating-point result, optionally
using a custom precision and rounding mode.

See the documentation of :func:`~mpmath.fadd` for a detailed description
of how to specify precision and rounding.

**Examples**

An mpmath number is returned::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = False
    >>> fneg(2.5)
    mpf('-2.5')
    >>> fneg(-5+2j)
    mpc(real='5.0', imag='-2.0')

Precise control over rounding is possible::

    >>> x = fadd(2, 1e-100, exact=True)
    >>> fneg(x)
    mpf('-2.0')
    >>> fneg(x, rounding='f')
    mpf('-2.0000000000000004')

Negating with and without roundoff::

    >>> n = 200000000000000000000001
    >>> print(int(-mpf(n)))
    -200000000000000016777216
    >>> print(int(fneg(n)))
    -200000000000000016777216
    >>> print(int(fneg(n, prec=log(n,2)+1)))
    -200000000000000000000001
    >>> print(int(fneg(n, dps=log(n,10)+1)))
    -200000000000000000000001
    >>> print(int(fneg(n, prec=inf)))
    -200000000000000000000001
    >>> print(int(fneg(n, dps=inf)))
    -200000000000000000000001
    >>> print(int(fneg(n, exact=True)))
    -200000000000000000000001

r/  rH  2Arguments need to be mpf or mpc compatible numbers)
rn  r-  r7  r   r&   r/  r   r?   rH  r  )r   r*  rr  r   r=  s        r   fnegMPContext.fneg  s|    \ 0KKN1g<< @AA1g<< @AAMNNr   c                 "   U R                  U5      u  pEU R                  U5      nU R                  U5      n [        US5      (       a  [        US5      (       a0  U R                  [	        UR
                  UR
                  XE5      5      $ [        US5      (       a0  U R                  [        UR                  UR
                  XE5      5      $ [        US5      (       a  [        US5      (       a0  U R                  [        UR                  UR
                  XE5      5      $ [        US5      (       a0  U R                  [        UR                  UR                  XE5      5      $ [        S5      e! [        [        4 a    [        U R                  5      ef = f)a3  
Adds the numbers *x* and *y*, giving a floating-point result,
optionally using a custom precision and rounding mode.

The default precision is the working precision of the context.
You can specify a custom precision in bits by passing the *prec* keyword
argument, or by providing an equivalent decimal precision with the *dps*
keyword argument. If the precision is set to ``+inf``, or if the flag
*exact=True* is passed, an exact addition with no rounding is performed.

When the precision is finite, the optional *rounding* keyword argument
specifies the direction of rounding. Valid options are ``'n'`` for
nearest (default), ``'f'`` for floor, ``'c'`` for ceiling, ``'d'``
for down, ``'u'`` for up.

**Examples**

Using :func:`~mpmath.fadd` with precision and rounding control::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = False
    >>> fadd(2, 1e-20)
    mpf('2.0')
    >>> fadd(2, 1e-20, rounding='u')
    mpf('2.0000000000000004')
    >>> nprint(fadd(2, 1e-20, prec=100), 25)
    2.00000000000000000001
    >>> nprint(fadd(2, 1e-20, dps=15), 25)
    2.0
    >>> nprint(fadd(2, 1e-20, dps=25), 25)
    2.00000000000000000001
    >>> nprint(fadd(2, 1e-20, exact=True), 25)
    2.00000000000000000001

Exact addition avoids cancellation errors, enforcing familiar laws
of numbers such as `x+y-x = y`, which don't hold in floating-point
arithmetic with finite precision::

    >>> x, y = mpf(2), mpf('1e-1000')
    >>> print(x + y - x)
    0.0
    >>> print(fadd(x, y, prec=inf) - x)
    1.0e-1000
    >>> print(fadd(x, y, exact=True) - x)
    1.0e-1000

Exact addition can be inefficient and may be impossible to perform
with large magnitude differences::

    >>> fadd(1, '1e-100000000000000000000', prec=inf)
    Traceback (most recent call last):
      ...
    OverflowError: the exact result does not fit in memory

r/  rH  r>  )rn  r-  r7  r   r'   r/  r   rC   rH  rB   r  OverflowError_exact_overflow_msgr   r*  r1  rr  r   r=  s         r   faddMPContext.faddF  s7   p 0KKNKKN	9q'""1g&&<<$(QRR1g&&<<AGGQWWd(UVVq'""1g&&<<AGGQWWd(UVV1g&&<<$(QRR MNN M* 	9 7 788	9!   AE( 	A E( 
AE( A E( (&Fc                 .   U R                  U5      u  pEU R                  U5      nU R                  U5      n [        US5      (       a  [        US5      (       a0  U R                  [	        UR
                  UR
                  XE5      5      $ [        US5      (       a6  U R                  [        UR
                  [        4UR                  XE5      5      $ [        US5      (       a  [        US5      (       a0  U R                  [        UR                  UR
                  XE5      5      $ [        US5      (       a0  U R                  [        UR                  UR                  XE5      5      $ [        S5      e! [        [        4 a    [        U R                  5      ef = f)aY  
Subtracts the numbers *x* and *y*, giving a floating-point result,
optionally using a custom precision and rounding mode.

See the documentation of :func:`~mpmath.fadd` for a detailed description
of how to specify precision and rounding.

**Examples**

Using :func:`~mpmath.fsub` with precision and rounding control::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = False
    >>> fsub(2, 1e-20)
    mpf('2.0')
    >>> fsub(2, 1e-20, rounding='d')
    mpf('1.9999999999999998')
    >>> nprint(fsub(2, 1e-20, prec=100), 25)
    1.99999999999999999999
    >>> nprint(fsub(2, 1e-20, dps=15), 25)
    2.0
    >>> nprint(fsub(2, 1e-20, dps=25), 25)
    1.99999999999999999999
    >>> nprint(fsub(2, 1e-20, exact=True), 25)
    1.99999999999999999999

Exact subtraction avoids cancellation errors, enforcing familiar laws
of numbers such as `x-y+y = x`, which don't hold in floating-point
arithmetic with finite precision::

    >>> x, y = mpf(2), mpf('1e1000')
    >>> print(x - y + y)
    0.0
    >>> print(fsub(x, y, prec=inf) + y)
    2.0
    >>> print(fsub(x, y, exact=True) + y)
    2.0

Exact addition can be inefficient and may be impossible to perform
with large magnitude differences::

    >>> fsub(1, '1e-100000000000000000000', prec=inf)
    Traceback (most recent call last):
      ...
    OverflowError: the exact result does not fit in memory

r/  rH  r>  )rn  r-  r7  r   r(   r/  r   rD   r    rH  rE   r  rB  rC  rD  s         r   fsubMPContext.fsub  s<   ` 0KKNKKN	9q'""1g&&<<$(QRR1g&&<<%0@!''4(Z[[q'""1g&&<<AGGQWWd(UVV1g&&<<$(QRR MNN M* 	9 7 788	9s!   AE. 	AE. AE. "A E. .&Fc                 "   U R                  U5      u  pEU R                  U5      nU R                  U5      n [        US5      (       a  [        US5      (       a0  U R                  [	        UR
                  UR
                  XE5      5      $ [        US5      (       a0  U R                  [        UR                  UR
                  XE5      5      $ [        US5      (       a  [        US5      (       a0  U R                  [        UR                  UR
                  XE5      5      $ [        US5      (       a0  U R                  [        UR                  UR                  XE5      5      $ [        S5      e! [        [        4 a    [        U R                  5      ef = f)ae  
Multiplies the numbers *x* and *y*, giving a floating-point result,
optionally using a custom precision and rounding mode.

See the documentation of :func:`~mpmath.fadd` for a detailed description
of how to specify precision and rounding.

**Examples**

The result is an mpmath number::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = False
    >>> fmul(2, 5.0)
    mpf('10.0')
    >>> fmul(0.5j, 0.5)
    mpc(real='0.0', imag='0.25')

Avoiding roundoff::

    >>> x, y = 10**10+1, 10**15+1
    >>> print(x*y)
    10000000001000010000000001
    >>> print(mpf(x) * mpf(y))
    1.0000000001e+25
    >>> print(int(mpf(x) * mpf(y)))
    10000000001000011026399232
    >>> print(int(fmul(x, y)))
    10000000001000011026399232
    >>> print(int(fmul(x, y, dps=25)))
    10000000001000010000000001
    >>> print(int(fmul(x, y, exact=True)))
    10000000001000010000000001

Exact multiplication with complex numbers can be inefficient and may
be impossible to perform with large magnitude differences between
real and imaginary parts::

    >>> x = 1+2j
    >>> y = mpc(2, '1e-100000000000000000000')
    >>> fmul(x, y)
    mpc(real='2.0', imag='4.0')
    >>> fmul(x, y, rounding='u')
    mpc(real='2.0', imag='4.0000000000000009')
    >>> fmul(x, y, exact=True)
    Traceback (most recent call last):
      ...
    OverflowError: the exact result does not fit in memory

r/  rH  r>  )rn  r-  r7  r   r)   r/  r   rG   rH  rF   r  rB  rC  rD  s         r   fmulMPContext.fmul  s7   f 0KKNKKN	9q'""1g&&<<$(QRR1g&&<<AGGQWWd(UVVq'""1g&&<<AGGQWWd(UVV1g&&<<$(QRR MNN M* 	9 7 788	9rG  c                    U R                  U5      u  pEU(       d  [        S5      eU R                  U5      nU R                  U5      n[        US5      (       a  [        US5      (       a0  U R	                  [        UR                  UR                  XE5      5      $ [        US5      (       a6  U R                  [        UR                  [        4UR                  XE5      5      $ [        US5      (       a  [        US5      (       a0  U R                  [        UR                  UR                  XE5      5      $ [        US5      (       a0  U R                  [        UR                  UR                  XE5      5      $ [        S5      e)a  
Divides the numbers *x* and *y*, giving a floating-point result,
optionally using a custom precision and rounding mode.

See the documentation of :func:`~mpmath.fadd` for a detailed description
of how to specify precision and rounding.

**Examples**

The result is an mpmath number::

    >>> from mpmath import *
    >>> mp.dps = 15; mp.pretty = False
    >>> fdiv(3, 2)
    mpf('1.5')
    >>> fdiv(2, 3)
    mpf('0.66666666666666663')
    >>> fdiv(2+4j, 0.5)
    mpc(real='4.0', imag='8.0')

The rounding direction and precision can be controlled::

    >>> fdiv(2, 3, dps=3)    # Should be accurate to at least 3 digits
    mpf('0.6666259765625')
    >>> fdiv(2, 3, rounding='d')
    mpf('0.66666666666666663')
    >>> fdiv(2, 3, prec=60)
    mpf('0.66666666666666667')
    >>> fdiv(2, 3, rounding='u')
    mpf('0.66666666666666674')

Checking the error of a division by performing it at higher precision::

    >>> fdiv(2, 3) - fdiv(2, 3, prec=100)
    mpf('-3.7007434154172148e-17')

Unlike :func:`~mpmath.fadd`, :func:`~mpmath.fmul`, etc., exact division is not
allowed since the quotient of two floating-point numbers generally
does not have an exact floating-point representation. (In the
future this might be changed to allow the case where the division
is actually exact.)

    >>> fdiv(2, 3, exact=True)
    Traceback (most recent call last):
      ...
    ValueError: division is not an exact operation

z"division is not an exact operationr/  rH  r>  )rn  r  r-  r7  r   r+   r/  r   rI   r    rH  rJ   rD  s         r   fdivMPContext.fdiv  s   b 0ABBKKNKKN1gq'""||GAGGQWWd$MNNq'""||GQWWe,<aggt$VWW1gq'""||K$$QRRq'""||GAGGQWWd$MNNMNNr   c                 f   [        U5      nU[        ;   a  [        U5      U R                  4$ U[        R
                  L af  UR                  u  p4[        X45      u  pVSU-  U:  a  US-  nOU(       d  XPR                  4$ [        [        X5U-  -
  5      5      [        U5      -
  nXW4$ [        US5      (       a  UR                  nU R                  n	O[        US5      (       aA  UR                  u  pU
u  ppU(       a  X-   n	OqU
[        :X  a  U R                  n	OZ[        S5      eU R                  U5      n[        US5      (       d  [        US5      (       a  U R!                  U5      $ [#        S5      eUu  nnnnUU-   nUS:  a  SnUnOU(       ar  US:  a  UU-  nU R                  nOOUS:X  a  US-	  S-   nSnO>U* S-
  nUU-	  nUS-  (       a  US-  nUU-  U-
  nOUUU-  -  nUS-	  nU[        U5      -   nU(       a  U* nO$U[        :X  a  U R                  nSnO[        S5      eU[%        UU	5      4$ )	a  
Return `(n,d)` where `n` is the nearest integer to `x` and `d` is
an estimate of `\log_2(|x-n|)`. If `d < 0`, `-d` gives the precision
(measured in bits) lost to cancellation when computing `x-n`.

    >>> from mpmath import *
    >>> n, d = nint_distance(5)
    >>> print(n); print(d)
    5
    -inf
    >>> n, d = nint_distance(mpf(5))
    >>> print(n); print(d)
    5
    -inf
    >>> n, d = nint_distance(mpf(5.00000001))
    >>> print(n); print(d)
    5
    -26
    >>> n, d = nint_distance(mpf(4.99999999))
    >>> print(n); print(d)
    5
    -26
    >>> n, d = nint_distance(mpc(5,10))
    >>> print(n); print(d)
    5
    4
    >>> n, d = nint_distance(mpc(5,0.000001))
    >>> print(n); print(d)
    5
    -19

r  r   r/  rH  zrequires a finite numberzrequires an mpf/mpcr   )rm  r   r5  r   r^   rs   r  divmodr8   r  r7  r/  rH  r    r  r-  r  r  r  )r   r*  typxr  r  r;  rr  r  im_distr  isignimaniexpibcr  r  r   r  r  re_distts                         r   r  MPContext.nint_distanceY  s,   B Aw9q6388##X\\!77DA!<DAsaxQ(({"QsU$x{2A4K1gBhhGQ  WWFB%'"E*u(( !;<<AAq'""ga&9&9((++ 566c3"f7AGax3J((!VQJT!V1Hq5FAa43,CAqDMCqDhsm+B5[hhGA788#gw'''r   c                 v    U R                   n U R                  nU H  nX4-  nM	     X l         U7$ ! X l         f = f)a  
Calculates a product containing a finite number of factors (for
infinite products, see :func:`~mpmath.nprod`). The factors will be
converted to mpmath numbers.

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

)r   r   )r   factorsorigrX  r  s        r   fprodMPContext.fprod  sC     xx	A  Hr	 Hs   0 8c                 J    U R                  [        U R                  5      5      $ )z
Returns an ``mpf`` with value chosen randomly from `[0, 1)`.
The number of randomly generated bits in the mantissa is equal
to the working precision.
)r   r6   r  r   s    r   randMPContext.rand  s     ||HSYY/00r   c                 B   ^^ U R                  UU4S jT< ST< 35      $ )a  
Given Python integers `(p, q)`, returns a lazy ``mpf`` representing
the fraction `p/q`. The value is updated with the precision.

    >>> from mpmath import *
    >>> mp.dps = 15
    >>> a = fraction(1,100)
    >>> b = mpf(1)/100
    >>> print(a); print(b)
    0.01
    0.01
    >>> mp.dps = 30
    >>> print(a); print(b)      # a will be accurate
    0.01
    0.0100000000000000002081668171172
    >>> mp.dps = 15
c                    > [        TTX5      $ r)  )r   )r   r   r  r  s     r   r   $MPContext.fraction.<locals>.<lambda>  s    mAq$.Lr   /)rq   )r   r  r  s    ``r   fractionMPContext.fraction  s!    $ ||L! 	r   c                 6    [        U R                  U5      5      $ r)  r  r-  r  s     r   absminMPContext.absmin      3;;q>""r   c                 6    [        U R                  U5      5      $ r)  rm  r  s     r   absmaxMPContext.absmax  rp  r   c                     [        US5      (       a0  UR                  u  p#U R                  U5      U R                  U5      /$ U$ )Nr  )r7  r  r   )r   r*  rV  rW  s       r   
_as_pointsMPContext._as_points  s:    1g77DALLOS\\!_55r   r   c                 n   U R                  U5      (       a  [        US5      (       d  [        e[        U5      nU R                  n[
        R                  " UR                  X#XEU5      u  pxU V	s/ s H  oR                  U	5      PM     nn	U V
s/ s H  oR                  U
5      PM     nn
Xx4$ s  sn	f s  sn
f )NrH  )	isintr7  r8  r5  r  r   mpc_zetasumrH  r   )r   rt  rV  r;  derivativesreflectr   xsysr*  r1  s              r   _zetasum_fastMPContext._zetasum_fast  s    		!G!4!4%%Fyy""177A+M')*r!ll1or*')*r!ll1or*v +*s   .B-B2)r   F)Nr  F)   )T):__name__
__module____qualname____firstlineno____doc__rl   rv   r9   r2  r@  rC  rK  rQ  rY  ry   ra  r~   r}   ru  rz  r~  rg  r  r  r  r  r  propertyr  r  r  r  r  r  r  r  r  r  rn  rC  r  r3  r7  r;  r?  rE  rI  rL  rO  r  ra  rd  rj  rn  rr  ru  r~  __static_attributes__r  r   r   rg   rg   :   s\   1BT5l T..
JPANMT
P>>

;84+(  # #  N$NJ"Ji"V4l=$,"* DKSj9"" 4OlHOT@ODCOJ@OD`(D*1*##" 23U r   rg   c                   0    \ rS rSrSS jrS rS rS rSrg)	r  i  c                 4    Xl         X l        X0l        X@l        g r)  )r   precfundpsfunr  )selfr   r  r  r  s        r   rl   PrecisionManager.__init__  s     0r   c                 J   ^ ^ [         R                  " T5      UU 4S j5       nU$ )Nc                  f  > TR                   R                  n TR                  (       a5  TR                  TR                   R                  5      TR                   l        O4TR                  TR                   R                  5      TR                   l        TR
                  (       a[  T" U 0 UD6n[        U5      [        L a-  [        U Vs/ s H  oD7PM     sn5      UTR                   l        $ U7UTR                   l        $ T" U 0 UD6UTR                   l        $ s  snf ! UTR                   l        f = fr)  )r   r   r  r  r  r  rm  r  )r  rr  r`  rX  rV  r  r  s        r   g$PrecisionManager.__call__.<locals>.g  s    88==D%<<$(LL$?DHHM#';;txx||#<DHHL((4*6*AAw%'$!_!Qb!_5
 !%	 2 !% d-f- $ &5
 !%s*   B.D DD +D ?D D D0)	functoolswraps)r  r  r  s   `` r   __call__PrecisionManager.__call__  s%    			% 
	%  r   c                 .   U R                   R                  U l        U R                  (       a5  U R                  U R                   R                  5      U R                   l        g U R	                  U R                   R
                  5      U R                   l        g r)  )r   r   origpr  r  r  )r  s    r   	__enter__PrecisionManager.__enter__.  sP    XX]]
<< LL7DHHM;;txx||4DHHLr   c                 :    U R                   U R                  l        grj   )r  r   r   )r  exc_typeexc_valexc_tbs       r   __exit__PrecisionManager.__exit__4  s    

r   )r   r  r  r  r  Nr  )	r  r  r  r  rl   r  r  r  r  r  r   r   r  r    s    1
&5r   r  __main__)wr  __docformat__r  r  ctx_baser   libmp.backendr   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^   object__new__newcompiler  sage.libs.mpmath.ext_mainr`   rk   libsmpmathext_main_mpf_modulerb   ra   rc   rd   re   rg   r  r  doctesttestmodr  r   r   <module>r     s     	 ) .                      .  nnjj K L fB33?. 0 0Y2 Yv&! !H zOO r   