
    نe,                        d Z dZddlZddlZddlmZ ddlmZmZ ddl	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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^mZ dd	l	m_Z_ dd
l	m`Z` eaj                  Zc ej                  d      Zeedk(  rddlfmgZh ddlfmic mjc mkZl nddlmmnZh ddl	mmZl ddlmmoZompZpmqZq  G d dehe      Zr G d d      Zsetdk(  rddluZu euj                          yy)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                   l   e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd8d
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ed        Zed        Zd9dZd9dZd9dZd9dZ d:d Z!d;d!Z"d" Z#d# Z$d$ Z%d%Z&d&Z'd<d'Z(d( Z)d) Z*d* Z+d+ Z,d, Z-d- Z.d. Z/d/ Z0d0 Z1d1 Z2d2 Z3d3 Z4d4 Z5d5 Z6	 d6gdfd7Z7y)=	MPContextzH
    Context for multiprecision arithmetic with a global precision.
    c                    t        j                  |        d| _        d| _        | j                  | j
                  | j                  g| _        t        j                  | _
        | j                          t        j                  |        t        j                  | _	        | j                          i | _        | j                          	 t         j"                  | j"                  j$                  _        t         j(                  | j(                  j$                  _        t         j*                  | j*                  j$                  _        t         j,                  | j,                  j$                  _        t         j2                  | j2                  _        t         j4                  | j4                  _        t         j6                  | j6                  _        y # t.        $ r t         j"                  | j"                  j0                  _        t         j(                  | j(                  j0                  _        t         j*                  | j*                  j0                  _        t         j,                  | j,                  j0                  _        Y w xY w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    //usr/lib/python3/dist-packages/mpmath/ctx_mp.pyrk   z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-I65I6c                    | j                   }| j                  }| j                  t              | _        | j                  t
              | _        | j                  t
        t        f      | _        | j                  t              | _
        | j                  t              | _        | j                  t              | _        | j                  d dd      }|| _        | j                  t"        dd      | _        | j                  t&        dd      | _        | j                  t*        dd      | _        | j                  t.        d	d
      | _        | j                  t2        dd      | _        | j                  t6        dd      | _        | j                  t:        dd      | _        | j                  t>        dd      | _         | j                  tB        dd      | _"        | j                  tF        dd      | _$        | j                  tJ        dd      | _&        | j                  tN        dd      | _(        | j                  tR        dd      | _*        | jW                  tX        jZ                  tX        j\                        | _/        | jW                  tX        j`                  tX        jb                        | _2        | jW                  tX        jf                  tX        jh                        | _5        | jW                  tX        jl                  tX        jn                        | _8        | jW                  tX        jr                  tX        jt                        | _;        | jW                  tX        jx                  tX        jz                        | _>        | jW                  tX        j~                  tX        j                        | _A        | jW                  tX        j                  tX        j                        | _D        | jW                  tX        j                  tX        j                        | _G        | jW                  tX        j                  tX        j                        | _J        | jW                  tX        j                  tX        j                        | _M        | jW                  tX        j                  tX        j                        | _P        | jW                  tX        j                  tX        j                        | _S        | jW                  tX        j                  tX        j                        | _V        | jW                  tX        j                  tX        j                        | _Y        | jW                  tX        jl                  tX        jn                        | _8        | jW                  tX        j                  tX        j                        | _\        | jW                  tX        j                  tX        j                        | __        | jW                  tX        j                  tX        j                        | _b        | jW                  tX        j                  tX        j                        | _e        | jW                  tX        j                  tX        j                        | _h        | jW                  tX        j                  tX        j                        | _k        | jW                  tX        j                  tX        j                        | _n        | jW                  tX        j                  tX        j                        | _q        | jW                  tX        j                  tX        j                        | _t        | jW                  tX        j                  tX        j                        x| _w        | _x        | jW                  tX        j                  tX        j                        | _{        | jW                  tX        j                  tX        j                        | _~        | jW                  tX        j                  tX        j                         | _        | jW                  tX        j                  tX        j                        x| _        | _        | jW                  tX        j                  tX        j                        | _        | jW                  tX        j                  tX        j                        | _        | jW                  tX        j                  tX        j                        | _        | jW                  tX        j                  tX        j                         | _        | jW                  tX        j$                  tX        j&                        | _        | jW                  tX        j*                  tX        j,                        | _        | jW                  tX        j0                  tX        j2                        | _        | jW                  tX        j6                  tX        j8                        | _        | jW                  tX        j<                  tX        j>                        | _        | jW                  tX        jB                  d       | _        | jW                  tX        jF                  d       | _        | jW                  tX        jJ                  tX        jL                        | _        | jW                  tX        jP                  tX        jR                        | _        tW        | d| j^                        | _/        tW        | d| jv                        | _;        tW        | d| jj                        | _5        tW        | d | j                        | _G        tW        | d!| j                        | _D        y )"Nc                     dt         d| z
  dfS )Nr   r   )r   )precrnds     r   <lambda>z)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)rn   ro   make_mpfr   oner    zeromake_mpcjr!   infr"   ninfr#   nanrp   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   rn   ro   r   s       r   ru   z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                 $    |j                  |      S N)r9   )r   xr   s      r   r9   zMPContext.to_fixed   s    zz$r   c                     | j                  |      }| j                  |      }| j                  t        j                  |j                  |j                  g| j
                         S )z
        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   hypotzMPContext.hypot   sK     KKNKKN||EOOAGGQWWRs?Q?QRSSr   c                 >   t        | j                  |            }|dk(  r| j                  |      S t        |d      st        | j
                  \  }}t        j                  ||j                  ||d      \  }}|| j                  |      S | j                  ||f      S )Nr   r*  T)r   )int_rer	  hasattrNotImplementedErrorr+  r   
mpf_expintr*  r   r   r   nzr   roundingrealimags          r   _gamma_upper_intzMPContext._gamma_upper_int   s    
O666!9q'"%%++h%%a$M
d<<<%%<<t--r   c                    t        |      }|dk(  r| j                  |      S t        |d      st        | j                  \  }}t        j                  ||j                  ||      \  }}|| j                  |      S | j                  ||f      S )Nr   r*  )
r/  r	  r1  r2  r+  r   r3  r*  r   r   r4  s          r   _expint_intzMPContext._expint_int   s    F666!9q'"%%++h%%a$A
d<<<%%<<t--r   c                 x   t        |d      r;	 | j                  t        j                  |j                  |g| j
                         S |j                  }| j                  t        j                  ||g| j
                         S # t        $ r, | j                  r |j                  t        j                  f}Y dw xY wNr*  )r1  r   r   mpf_nthrootr*  r+  r   rl   r    _mpc_r   mpc_nthrootr   r&  r5  s      r   _nthrootzMPContext._nthroot   s    1g+||E$5$5aggq$V3CUCU$VWW A||E--aHS5G5GHII ! +##WWekk*+s   9B 2B98B9c                    | j                   \  }}t        |d      r1| j                  t        j                  ||j
                  ||            S t        |d      r1| j                  t        j                  ||j                  ||            S y Nr*  r@  )	r+  r1  r   r   mpf_besseljnr*  r   mpc_besseljnr@  )r   r5  r6  r   r7  s        r   _besseljzMPContext._besselj   sr    ++h1g<< 2 21aggtX NOOQ << 2 21aggtX NOO !r   c                    | j                   \  }}t        |d      rJt        |d      r>	 t        j                  |j                  |j                  ||      }| j                  |      S t        |d      r|j                  t        j                  f}n|j                  }t        |d      r|j                  t        j                  f}n|j                  }| j                  t        j                  ||||            S # t        $ r Y w xY wr>  )r+  r1  r   mpf_agmr*  r   r   r    r@  r   mpc_agm)r   abr   r7  vs         r   _agmzMPContext._agm   s    ++h1g71g#6MM!''177D(C||A& 1gQWWekk$:''a1gQWWekk$:''a||EMM!Qh?@@ ! s   <C7 7	DDc                 r    | j                  t        j                  t        |      g| j                         S r%  )r   r   mpf_bernoullir/  r+  r   r5  s     r   rx   zMPContext.bernoulli   s+    ||E//AL9K9KLMMr   c                 r    | j                  t        j                  t        |      g| j                         S r%  )r   r   mpf_zeta_intr/  r+  rR  s     r   	_zeta_intzMPContext._zeta_int   s+    ||E..s1vK8J8JKLLr   c                     | j                  |      }| j                  |      }| j                  t        j                  |j                  |j                  g| j
                         S r%  )r(  r   r   	mpf_atan2r*  r+  )r   r,  r&  s      r   r}   zMPContext.atan2  sI    KKNKKN||EOOAGGQWWRs?Q?QRSSr   c                 D   | j                  |      }t        |      }| j                  |      r:| j                  t	        j
                  ||j                  g| j                         S | j                  t	        j                  ||j                  g| j                         S r%  )r(  r/  _is_real_typer   r   mpf_psir*  r+  r   mpc_psir@  )r   mr6  s      r   r|   zMPContext.psi  sx    KKNFQ<<a N3;M;M NOO<<a N3;M;M NOOr   c                    t        |      | j                  vr| j                  |      }| j                  |      \  }}t	        |d      rFt        j                  |j                  ||      \  }}| j                  |      | j                  |      fS t	        |d      rFt        j                  |j                  ||      \  }}| j                  |      | j                  |      fS  | j                  |fi | | j                  |fi |fS rE  )typerq   r(  _parse_precr1  r   mpf_cos_sinr*  r   mpc_cos_sinr@  r   r   r   r   r&  kwargsr   r7  css          r   cos_sinzMPContext.cos_sin  s    7#))#AA0h1g$$QWWdH=DAq<<?CLLO33Q $$QWWdH=DAq<<?CLLO333771'')=f)===r   c                    t        |      | j                  vr| j                  |      }| j                  |      \  }}t	        |d      rFt        j                  |j                  ||      \  }}| j                  |      | j                  |      fS t	        |d      rFt        j                  |j                  ||      \  }}| j                  |      | j                  |      fS  | j                  |fi | | j                  |fi |fS rE  )r^  rq   r(  r_  r1  r   mpf_cos_sin_pir*  r   mpc_cos_sin_pir@  r   r   r   rb  s          r   cospi_sinpizMPContext.cospi_sinpi  s    7#))#AA0h1g''x@DAq<<?CLLO33Q ''x@DAq<<?CLLO333771'')=f)===r   c                 H    | j                         }| j                  |_        |S )zP
        Create a copy of the context, with the same working precision.
        )	__class__r   )r   rL  s     r   clonezMPContext.clone)  s     MMOr   c                 @    t        |d      st        |      t        u ryy)Nr@  FTr1  r^  complexr   r&  s     r   rY  zMPContext._is_real_type4  s    1g$q'W"4r   c                 @    t        |d      st        |      t        u ryy)Nr@  TFro  rq  s     r   _is_complex_typezMPContext._is_complex_type9  s    1g$q'W"4r   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

        r*  r@  Fzisnan() needs a number as input)r1  r*  r#   r@  
isinstancer   r^   rr   r(  isnan	TypeErrorrq  s     r   rv  zMPContext.isnan>  s    " 1g77d?"1g177?"a#z!X\\'BKKN1g'!W"599Q<9::r   c                 J    | j                  |      s| j                  |      ryy)a  
        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)isinfrv  rq  s     r   isfinitezMPContext.isfiniteZ  s    , 99Q<399Q<r   c                    |syt        |d      r|j                  \  }}}}|xr |dk\  S t        |d      r*|j                   xr | j                  |j                        S t        |      t        v r|dk  S t        || j                        r|j                  \  }}|sy|dk(  xr |dk  S | j                  | j                  |            S )z<
        Determine if *x* is a nonpositive integer.
        Tr*  r   r@  r   )r1  r*  r9  isnpintr8  r^  r   ru  rr   _mpq_r(  )r   r&  signmanr   bcpqs           r   r|  zMPContext.isnpintt  s     1g!"D#sB$C1H$1gvv:5#++aff"557i6Ma!77DAq6$a1f${{3;;q>**r   c                     dd| j                   z  j                  d      dz   d| j                  z  j                  d      dz   d| j                  z  j                  d      dz   g}d	j	                  |      S )
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dpsrl   join)r   liness     r   __str__zMPContext.__str__  st    #(//3oEsww&--b1OC%(8(88??CFXX

 yyr   c                 ,    t        | j                        S r%  )r   _precr   s    r   _repr_digitszMPContext._repr_digits  s    		""r   c                     | j                   S r%  )_dpsr   s    r   _str_digitszMPContext._str_digits  s    xxr   Fc                 &    t        | fdd|      S )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                     | z   S r%   r  r5  s    r   r   z%MPContext.extraprec.<locals>.<lambda>  s    q1u r   NPrecisionManagerr   r5  normalize_outputs    ` r   	extrapreczMPContext.extraprec  s       _d<LMMr   c                 &    t        | dfd|      S )z
        This function is analogous to extraprec (see documentation)
        but changes the decimal precision instead of the number of bits.
        Nc                     | z   S r%  r  dr5  s    r   r   z$MPContext.extradps.<locals>.<lambda>  s    QU r   r  r  s    ` r   extradpszMPContext.extradps  s    
  T?<LMMr   c                 &    t        | fdd|      S )a  
        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                     S r%  r  r  s    r   r   z$MPContext.workprec.<locals>.<lambda>  s    q r   Nr  r  s    ` r   workpreczMPContext.workprec  s      [$8HIIr   c                 &    t        | dfd|      S )z
        This function is analogous to workprec (see documentation)
        but changes the decimal precision instead of the number of bits.
        Nc                     S r%  r  r  s    r   r   z#MPContext.workdps.<locals>.<lambda>  s    Q r   r  r  s    ` r   workdpszMPContext.workdps  s    
  T;8HIIr   Nc                 "      fd}|S )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                  *   	j                   }	j                  |      }n}	 |dz   	_         	  
| i |}|dz   }	 |	_         	  
| i |}||k(  rn}	j                  ||z
        	j                  |      z
  }|| k  rnPrt	        d|d|d|        |}||k\  r	j                  d|z        |t        |dz        z  }t        ||      }|	_         |S # $ r 	j                  }Y w xY w# $ r 	j                  }Y w xY w# |	_         w xY w)N
      zautoprec: target=z, prec=z, accuracy=z2autoprec: prec increased to %i without convergence   )r   _default_hyper_maxprecr   magprintNoConvergencer/  min)argsrc  r   maxprec2v1prec2v2errcatchr   fmaxprecverboses           r   f_autoprec_wrappedz.MPContext.autoprec.<locals>.f_autoprec_wrapped  s_   88D55d;" "9!D+F+B r	$CH%// Rx''"R%.3772;6Cte}#USD2 3B(!//L ! ! Sq\)Ex0E) ,  3J5  !B! ! % WW%$  sR   
D	 C D	 C2 BD	 C/,D	 .C//D	 2DD	 DD	 		Dr  )r   r  r  r  r  r  s   ````` r   autopreczMPContext.autoprec  s    H$	 $	J "!r   c                     t        |t              rddj                   fd|D              z  S t        |t              rddj                   fd|D              z  S t	        |d      rt        |j                  fi S t	        |d      rdt        |j                  fi z   d	z   S t        |t              rt        |      S t        | j                        r |j                  fi S t        |      S )
a3  
        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   F   K   | ]  } j                   |fi   y wr%  nstr.0rd  r   rc  r5  s     r   	<genexpr>z!MPContext.nstr.<locals>.<genexpr>]  #     &KAxsxx1'?'?&K   !z(%s)c              3   F   K   | ]  } j                   |fi   y wr%  r  r  s     r   r  z!MPContext.nstr.<locals>.<genexpr>_  r  r  r*  r@  ())ru  listr  tupler1  r   r*  r:   r@  r   reprmatrix__nstr__str)r   r&  r5  rc  s   ` ``r   r  z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$1::a*6**1vr   c                 $   |rt        |t              rd|j                         v r|j                         j                  dd      }t        j                  |      }|j                  d      }|sd}|j                  d      j                  d      }| j                  | j                  |      | j                  |            S t        |d      r0|j                  \  }}||k(  r| j                  |      S t        d      t        d	t        |      z         )
Nr     rer   im_mpi_z,can only create mpf from zero-width intervalzcannot create mpf from )ru  r   lowerreplaceget_complexmatchgrouprstripro   r(  r1  r  r   
ValueErrorrw  r  )r   r&  stringsr  r  r  rL  rM  s           r   _convert_fallbackzMPContext._convert_fallbackj  s    z!Z0aggiGGI%%c2.#))!,[[&B[[&--c2wws{{2B@@1g77DAqAv||A& !OPP1DG;<<r   c                 &     | j                   |i |S r%  )r(  )r   r  rc  s      r   	mpmathifyzMPContext.mpmathify|  s    s{{D+F++r   c                    |rz|j                  d      ry| j                  \  }}d|v r|d   }d|v r$|d   }|| j                  k(  ryt        |      }||fS d|v r |d   }|| j                  k(  ryt	        |      }||fS | j                  S )Nexact)r   r  r7  r   r  )getr+  r   r/  r   )r   rc  r   r7  r  s        r   r_  zMPContext._parse_prec  s    zz'" //ND(V#!*-f~377?!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                    t        |d      r|||df}|j                  }	nt        |d      r|||df}|j                  }	| j                  vr%t	        j
                  |      d   | j                  |<   | j                  |   }
| j                  }|j                  d| j                  |            }d}d}i }d	}t        |      D ]  \  }}||   d
k(  rG||k\  rA|d	k  r<d}t        |d |       D ]  \  }}||   d
k(  s|d	k  s||k  sd} |st        d      U| j                  |      \  }}t        |       }| }||k\  r3|d	k\  r.|dkD  r)||v r||xx   |z  cc<   n|||<   t        |||z
  dz         }|t        |      z  } 	 ||kD  rt        | j                   |||z   fz        ||z   }|rt#        d |D              }ni } |
|	||||fi |\  }}}| }d}||k  r |j%                         D ]  }|||k  sd} n ||dz
  dz
  k  xs | }|r:|rnG|j                  d      } | $|| kD  r|r| j'                  d	      S | j(                  S |dz  }|dz  }|dz  }t+        |      t,        u r$|r| j/                  |      S | j1                  |      S |S )Nr*  Rr@  Cr   r  2      r   ZFTzpole in hypergeometric series   <   c              3   $   K   | ]  }|d f 
 y wr%  r  )r  r5  s     r   r  z#MPContext.hypsum.<locals>.<genexpr>  s     BQ4Bs      zeroprecr  )r1  r*  r@  rv   r   make_hyp_summatorr   r  r  	enumerateZeroDivisionErrornint_distancer/  maxabsr  _hypsum_msgdictvaluesro   r   r^  r  r   r   )!r   r  r  flagscoeffsr6  accurate_smallrc  keyrN  summatorr   r  r  epsshiftmagnitude_checkmax_total_jumpird  okiiccr5  r  wpmag_dictzvhave_complex	magnitudecanceljumps_resolvedaccurater  s!                                    r   hypsumzMPContext.hypsum  s   1gQs"CAQ 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& /0OPP$$Q'DAqQAAAv!q&QU'#A&!+&)*OA&	1t8b=9	c!f$N)	%* 7" D$y.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                     | j                  |      }| j                  t        j                  |j                  |            S )a  
        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*  rB  s      r   ldexpzMPContext.ldexp  s/     KKN||EOOAGGQ788r   c                     | j                  |      }t        j                  |j                        \  }}| j	                  |      |fS )a=  
        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&  r,  r5  s       r   frexpzMPContext.frexp   s:     KKNqww'1||A!!r   c                 *   | j                  |      \  }}| j                  |      }t        |d      r&| j                  t	        |j
                  ||            S t        |d      r&| j                  t        |j                  ||            S t        d      )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*  r@  2Arguments need to be mpf or mpc compatible numbers)
r_  r(  r1  r   r&   r*  r   r?   r@  r  )r   r&  rc  r   r7  s        r   fnegzMPContext.fneg  s|    \ 0hKKN1g<<x @AA1g<<x @AAMNNr   c                    | j                  |      \  }}| j                  |      }| j                  |      }	 t        |d      rzt        |d      r1| j                  t	        |j
                  |j
                  ||            S t        |d      r1| j                  t        |j                  |j
                  ||            S t        |d      rzt        |d      r1| j                  t        |j                  |j
                  ||            S t        |d      r1| j                  t        |j                  |j                  ||            S t        d      # t        t        f$ r t        | j                        w xY w)a  
        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*  r@  r  )r_  r(  r1  r   r'   r*  r   rC   r@  rB   r  OverflowError_exact_overflow_msgr   r&  r,  rc  r   r7  s         r   faddzMPContext.faddF  s/   p 0hKKNKKN	9q'"1g&<<$(QRR1g&<<AGGQWWdH(UVVq'"1g&<<AGGQWWdH(UVV1g&<<$(QRR MNN M* 	9 7 788	9   AE <E >AE <E %E4c                    | j                  |      \  }}| j                  |      }| j                  |      }	 t        |d      rt        |d      r1| j                  t	        |j
                  |j
                  ||            S t        |d      r7| j                  t        |j
                  t        f|j                  ||            S t        |d      rzt        |d      r1| j                  t        |j                  |j
                  ||            S t        |d      r1| j                  t        |j                  |j                  ||            S t        d      # t        t        f$ r t        | j                        w xY w)a  
        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*  r@  r  )r_  r(  r1  r   r(   r*  r   rD   r    r@  rE   r  r  r  r  s         r   fsubzMPContext.fsub  s5   ` 0hKKNKKN	9q'"1g&<<$(QRR1g&<<%0@!''4QY(Z[[q'"1g&<<AGGQWWdH(UVV1g&<<$(QRR MNN M* 	9 7 788	9s    AE AE AE <E %E:c                    | j                  |      \  }}| j                  |      }| j                  |      }	 t        |d      rzt        |d      r1| j                  t	        |j
                  |j
                  ||            S t        |d      r1| j                  t        |j                  |j
                  ||            S t        |d      rzt        |d      r1| j                  t        |j                  |j
                  ||            S t        |d      r1| j                  t        |j                  |j                  ||            S t        d      # t        t        f$ r t        | j                        w xY w)a  
        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*  r@  r  )r_  r(  r1  r   r)   r*  r   rG   r@  rF   r  r  r  r  s         r   fmulzMPContext.fmul  s/   f 0hKKNKKN	9q'"1g&<<$(QRR1g&<<AGGQWWdH(UVVq'"1g&<<AGGQWWdH(UVV1g&<<$(QRR MNN M* 	9 7 788	9r  c                    | j                  |      \  }}|st        d      | j                  |      }| j                  |      }t        |d      rt        |d      r1| j	                  t        |j                  |j                  ||            S t        |d      r7| j                  t        |j                  t        f|j                  ||            S t        |d      rzt        |d      r1| j                  t        |j                  |j                  ||            S t        |d      r1| j                  t        |j                  |j                  ||            S t        d      )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*  r@  r  )r_  r  r(  r1  r   r+   r*  r   rI   r    r@  rJ   r  s         r   fdivzMPContext.fdiv  s   b 0hABBKKNKKN1gq'"||GAGGQWWdH$MNNq'"||GQWWe,<aggtX$VWW1gq'"||K$$QRRq'"||GAGGQWWdH$MNNMNNr   c                    t        |      }|t        v rt        |      | j                  fS |t        j
                  u rf|j                  \  }}t        ||      \  }}d|z  |k\  r|dz  }n|s|| j                  fS t        t        |||z  z
              t        |      z
  }||fS t        |d      r|j                  }| j                  }	nt        |d      r?|j                  \  }}
|
\  }}}}|r||z   }	nf|
t        k(  r| j                  }	nPt        d      | j                  |      }t        |d      st        |d      r| j!                  |      S t#        d      |\  }}}}||z   }|dk  rd}|}n|rf|dk\  r||z  }| j                  }nI|dk(  r|dz	  dz   }d}n9| dz
  }||z	  }|dz  r|dz  }||z  |z
  }n|||z  z  }|dz	  }|t        |      z   }|r'| }n#|t        k(  r| j                  }d}nt        d      |t%        ||	      fS )	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*  r@  zrequires a finite numberzrequires an mpf/mpcr   )r^  r   r/  r   r^   rr   r}  divmodr8   r  r1  r*  r@  r    r  r(  r  rw  r  )r   r&  typxr  r  r5  rr  r  im_distr  isignimaniexpibcr~  r  r   r  r  re_distts                         r   r  zMPContext.nint_distanceY  s3   B Aw9q6388##X\\!77DAq!Q<DAqsaxQ#(({"QqsU$x{2Aa4K1gBhhGQ WWFB%'"E4s*u(( !;<<AAq'"ga&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                 z    | j                   }	 | j                  }|D ]  }||z  }	 	 || _         |S # || _         w xY w)aT  
        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origrN  r  s        r   fprodzMPContext.fprod  sN     xx	A Q CHr	 CHs   1 	:c                 J    | j                  t        | j                              S )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   randzMPContext.rand  s     ||HSYY/00r   c                 <    | j                  fdd      S )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        | |      S r%  )r   )r   r   r  r  s     r   r   z$MPContext.fraction.<locals>.<lambda>  s    mAq$.L r   /)rp   )r   r  r  s    ``r   fractionzMPContext.fraction  s!    $ ||L! 	r   c                 6    t        | j                  |            S r%  r  r(  rq  s     r   absminzMPContext.absmin      3;;q>""r   c                 6    t        | j                  |            S r%  r6  rq  s     r   absmaxzMPContext.absmax  r8  r   c                     t        |d      r1|j                  \  }}| j                  |      | j                  |      gS |S )Nr  )r1  r  r   )r   r&  rL  rM  s       r   
_as_pointszMPContext._as_points  s9    1g77DAqLLOS\\!_55r   r   c                 X   | j                  |      rt        |d      st        t        |      }| j                  }t        j                  |j                  |||||      \  }}|D 	cg c]  }	| j                  |	       }}	|D 
cg c]  }
| j                  |
       }}
||fS c c}	w c c}
w )Nr@  )	isintr1  r2  r/  r  r   mpc_zetasumr@  r   )r   re  rL  r5  derivativesreflectr   xsysr&  r,  s              r   _zetasum_fastzMPContext._zetasum_fast  s    		!G!4%%Fyy""177Aq+wMB')*!cll1o**')*!cll1o**2v +*s   &B"B')r   F)Nr  F)   )T)8__name__
__module____qualname____doc__rk   ru   r9   r-  r:  r<  rC  rH  rO  rx   rU  r}   r|   rf  rj  rm  rY  rs  rv  rz  r|  r  propertyr  r  r  r  r  r  r  r  r  r  r_  r  r  r	  r  r  r  r  r  r  r  r  r.  r0  r4  r7  r:  r<  rD  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                   &    e Zd ZddZd Zd Zd Zy)r  c                 <    || _         || _        || _        || _        y r%  )r   precfundpsfunr  )selfr   rN  rO  r  s        r   rk   zPrecisionManager.__init__  s     0r   c                 F     t        j                         fd       }|S )Nc                  L   j                   j                  }	 j                  r5j                  j                   j                        j                   _        n4j                  j                   j                        j                   _        j
                  rX | i |}t        |      t        u r+t        |D cg c]  }| c}      |j                   _        S ||j                   _        S  | i ||j                   _        S c c}w # |j                   _        w xY wr%  )r   r   rN  rO  r  r  r^  r  )r  rc  r-  rN  rL  r  rP  s        r   gz$PrecisionManager.__call__.<locals>.g  s    88==D%<<$(LL$?DHHM#';;txx||#<DHHL((4*6*AAw%'$!_Qqb_5
 !%	 2 !% d-f- $ &5
 !%s*   B#D <
DD D 2D D D#)	functoolswraps)rP  r  rS  s   `` r   __call__zPrecisionManager.__call__  s%    			% 
	%  r   c                 $   | j                   j                  | _        | j                  r5| j                  | j                   j                        | j                   _        y | j	                  | j                   j
                        | j                   _        y r%  )r   r   origprN  rO  r  )rP  s    r   	__enter__zPrecisionManager.__enter__.  sP    XX]]
<< LL7DHHM;;txx||4DHHLr   c                 :    | j                   | j                  _        yri   )rX  r   r   )rP  exc_typeexc_valexc_tbs       r   __exit__zPrecisionManager.__exit__4  s    

r   NrE  )rG  rH  rI  rk   rV  rY  r^  r  r   r   r  r    s    1
&5r   r  __main__)wrJ  __docformat__rT  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`   rj   libsmpmathext_main_mpf_modulerb   ra   rc   rd   re   rg   r  rG  doctesttestmodr  r   r   <module>rn     s     	 ) .                      .  nnbjj K L fB33?. 0 0Y2 Yv&! !H zGOO r   