+
    iU                     p   R t ^ RIt^ RIHt ^RIHt ^RIHtHtHtHtH	t	H
t
 ^RIHtHtHtHtHtHtHtHtHtHtHtHtHtHtHtHtHtHtHtHtH t H!t!H"t"H#t#H$t$H%t%H&t&H't'H(t(H)t)H*t*H+t+H,t,H-t-H.t.H/t/H0t0H1t1H2t2H3t3H4t4H5t5H6t6H7t7H8t8 ^RI9H:t: ]
R8X  d   Rt;MR	t;R
t<]
R8X  d   R	t=M^t=^t>/ t?Rt@/ tARtB^	tC/ tD^tERtF^tG/ tH^^.tI]! ^]! ]B4      ^,           4       F7  tJ]I]K! ^]J,          ]B4      ^,           .^]J^,
          ,          ,          ,          tIK9  	  R tLR tMR tNR tORNR ltP]LR 4       tQ]LR 4       tR ]! R4      tS]! R4      tT]! R4      tU]! ^4      tVR tW]LROR l4       tXR tYR tZ]LR 4       t[]LR 4       t\]M! ]\4      t]]M! ]X4      t^]M! ][4      t_]M! ]Y4      t`]M! ]Q4      ta]M! ]R4      tb]LR 4       tc]LR  4       td]M! ]d4      te]M! ]c4      tf]3R! ltgR" thR# ti]3R$ ltj]3R% ltkRPR& ltlR' tmR( tnRQR) ltoR* tp]3R+ ltqR, trR- tsR. ttR/ tuR0 tv]3R1 ltw]3R2 ltx]3R3 lty]3R4 ltz]3R5 lt{]3R6 lt|]3R7 lt}]3R8 lt~RQR9 ltR: tR; tR< t]3R= lt]^ 3R> ltR? t]^ R3R@ lt]3RA lt]3RB lt]3RC lt]3RD lt]3RE lt]3RF lt]3RG lt]3RH lt]3RI ltRPRJ ltRPRK lt]
RL8X  d    ^ RIHu Hu Ht ]Ph                  t4]EP                  t]P                  tq]EP                  t]EP                  t]P                  tg]EP"                  t]EP                   t]P                  tlR# R#   ]]3 d    ]! RM4        R# i ; i)Ra(  
This module implements computation of elementary transcendental
functions (powers, logarithms, trigonometric and hyperbolic
functions, inverse trigonometric and hyperbolic) for real
floating-point numbers.

For complex and interval implementations of the same functions,
see libmpc and libmpi.

N)bisect)xrange)MPZMPZ_ZEROMPZ_ONEMPZ_TWOMPZ_FIVEBACKEND)-round_floorround_ceiling
round_downround_upround_nearest
round_fastComplexResultbitcountbctablelshiftrshiftgiant_steps
sqrt_fixedfrom_intto_intfrom_man_expto_fixedto_float
from_floatfrom_rational	normalizefzerofonefnonefhalffinffninffnanmpf_cmpmpf_signmpf_absmpf_posmpf_negmpf_addmpf_submpf_mulmpf_div	mpf_shiftmpf_rdiv_intmpf_pow_intmpf_sqrtreciprocal_rndnegative_rndmpf_perturb
isqrt_fast)ifibpythonX  i  i  i  i	  i  c                t   a  RS n         RS n        V 3R lpS P                  Vn        S P                  Vn        V# )z
Decorator for caching computed values of mathematical
constants. This decorator should be applied to a
function taking a single argument prec as input and
returning a fixed-point value with the given precision.
Nc                    < SP                   pW8:  d   SP                  W ,
          ,	          # \        V R ,          ^
,           4      pS! V3/ VB Sn        VSn         SP                  W0,
          ,	          # )g?)	memo_precmemo_valint)preckwargsr<   newprecfs   &,  v/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/mpmath/libmp/libelefun.pygconstant_memo.<locals>.g^   s^    KK	::).11d4il#w)&)
zzgl++    )r<   r=   __name____doc__)rB   rD   s   f rC   constant_memorJ   U   s5     AKAJ, AJ		AIHrF   c                D   a  \         3V 3R llpS P                  Vn        V# )z
Create a function that computes the mpf value for a mathematical
constant, given a function that computes the fixed-point value.

Assumptions: the constant is positive and has magnitude ~= 1;
the fixed-point function rounds to floor.
c                    < V ^,           pS! V4      pV\         \        39   d
   V^,          p\        ^ W2) \        V4      W4      # )   )r   r   r   r   )r?   rndwpvfixeds   &&  rC   rB   def_mpf_constant.<locals>.fr   sB    BY"I8]++FAAsHQK;;rF   )r   rI   )rQ   rB   s   f rC   def_mpf_constantrS   j   s      < AIHrF   c                 x   W!,
          ^8X  d^   \        ^V,          ^,           4      pV'       g   V^,          '       d   \        W@^,          ,          V3# \        ) W@^,          ,          V3# W,           ^,          p\        WWS4      w  rgp\        WW#4      w  rpW,          W,          ,           Wz,          W,          3#    )r   r   bsp_acot)qab
hyperbolica1mp1q1r1p2q2r2s   &&&&        rC   rW   rW   {   s    uz1q\1BAIr))8RQ$Y**	
qA!.JBB!.JBB525="%&&rF   c                    \        RV,          \        P                  ! V 4      ,          ^,           4      p\        V ^ W24      w  rEpWE,           V,          WP,          ,          # )z
Compute acot(a) or acoth(a) for an integer a with binary splitting; see
http://numbers.computation.free.fr/Constants/Algorithms/splitting.html
ffffff?)r>   mathlogrW   )rY   r?   r[   NprX   rs   &&&    rC   
acot_fixedrk      sI    
 	D4K#b()Aq!A*GA!S4K13rF   Fc                    ^
p\         pV  F8  w  rVV\        V4      \        \        V4      W,           V4      ,          ,          pK:  	  WC,	          # )z
Evaluate a Machin-like formula, i.e., a linear combination of
acot(n) or acoth(n) for specific integer values of n, using fixed-
point arithmetic. The input should be a list [(c, n), ...], giving
c*acot[h](n) + ...
)r   r   rk   )coefsr?   r[   	extraprecsrY   rZ   s   &&&    rC   machinrp      sD     IA	SVjQDDD NrF   c                     \        . ROV R4      # )zn
Computes ln(2). This is done with a hyperbolic Machin-type formula,
with binary splitting at high precision.
T))      )i  )   i-"  rp   r?   s   &rC   	ln2_fixedrx      s     3T4@@rF   c                     \        . ROV R4      # )zF
Computes ln(10). This is done with a hyperbolic Machin-type formula.
T)).      )"   1   )rM      rv   rw   s   &rC   
ln10_fixedr      s    
 14>>rF   iqc i-~ i@	 c                D   W,
          ^8X  d   \        ^V,          ^,
          ^V,          ^,
          ,          ^V,          ^,
          ,          4      pV^,          \        ^,          ,          ^,          pRV,          V,          \        \        V,          ,           ,          pM}V'       d   V^8  d   \	        RW4       W,           ^,          p\        WV^,           V4      w  rp
\        WqV^,           V4      w  rpW,          pW,          pW,          W,          ,           pWEV3# )z
Computes the sum from a to b of the series in the Chudnovsky
formula. Returns g, p, q where p/q is the sum as an exact
fraction and g is a temporary value used to save work
for recursive calls.
z  binary splittingrG   )r   CHUD_CCHUD_ACHUD_Bprintbs_chudnovsky)rY   rZ   levelverboserD   ri   rX   midg1r^   r_   g2ra   rb   s   &&&&          rC   r   r      s     	sax1Q1Q1Q'(qD619"!GaK6&(?+uqy&-sQh"157G<
"357G<
EEEBEM7NrF   c                :   \        V R,          R,          ^,           4      pV'       d   \        RV4       \        ^ V^ V4      w  rEp\        \        ^V ,          ,          4      pV\        ,          V,          V\
        V,          ,           \        ,          ,          pV# )z
Compute floor(pi * 2**prec) as a big integer.

This is done using Chudnovsky's series (see comments in
libelefun.py for details).
gv	O
@g bi ],@zbinary splitting with N =)r>   r   r   r6   r   r   CHUD_D)	r?   r   verbose_baserh   rD   ri   rX   sqrtCrP   s	   &&&      rC   pi_fixedr      sv     	Dl*Q./A)1-Aq!W-GA!v$'(E	&!F1H*f,-AHrF   c                 &    \        V 4      ^,          # )   )r   rw   s   &rC   degree_fixedr      s    D>3rF   c                    W,
          ^8X  d   \         \        V4      3# W,           ^,          p\        W4      w  r4\        W!4      w  rVW6,          V,           WF,          3# )zY
Sum series for exp(1)-1 between a, b, returning the result
as an exact fraction (p, q).
)r   r   bspe)rY   rZ   r]   r^   r_   ra   rb   s   &&     rC   r   r      sN    
 	saxA	
qA!ZFB!ZFB58RU?rF   c                    \        RV ,          \        P                  ! V 4      ,          ^,           4      p\        ^ V4      w  r#W#,           V ,          V,          # )z
Computes exp(1). This is done using the ordinary Taylor series for
exp, with binary splitting. For a description of the algorithm,
see:

    http://numbers.computation.free.fr/Constants/
        Algorithms/splitting.html
g?)r>   rf   rg   r   )r?   rh   ri   rX   s   &   rC   e_fixedr   	  sB     	CHTXXd^#b()A!9DAS4K!rF   c                    V ^
,          p \        \        ^V ,          ,          4      \        V ,          ,           pV^,	          # )z*
Computes the golden ratio, (1+sqrt(5))/2
)r6   r   r   )r?   rY   s   & rC   	phi_fixedr     s2    
 	BJD8af%&'T/:A7NrF   c           	      t    V ^
,           p\        \        \        \        V4      ^4      V4      V ^,
          4      # )
   )r   mpf_logr/   mpf_pi)r?   rO   s   & rC   ln_sqrt2pi_fixedr   *  s.    	BGIfRj!4b946BBrF   c                 ,    \        \        V 4      V 4      # N)r   r   rw   s   &rC   sqrtpi_fixedr   0  s    htnd++rF   c           	     (   V w  rErgVw  rrV'       d   V
^ 8  d   \        R4      hV
^ 8  d"   \        V RV,          W,          ,          W#4      # V
R8X  d   V	^8X  dB   V'       d.   \        \        \	        W^
,           \
        V,          4      W#4      # \	        WV4      # V'       d+   \        \	        W^
,           \
        V,          4      V	) W#4      # \        \	        W^
,           V4      WV4      # \        W^
,           V4      p\        \        W4      W#4      # )zJ
Compute s**t. Raises ComplexResult if s is negative and t is
fractional.
z,negative number raised to a fractional powerrG   )	r   r1   r.   r    r2   r3   r   mpf_expr-   )ro   tr?   rN   ssignsmansexpsbctsigntmantexptbccs   &&&&         rC   mpf_powr   >  s    
 EEJKKqy1rEkTZ8$DDrz19tXab"3'&)*.5 5AS))"8ABw"3'$)+/%< <x7C8$cJJ 	7C A71=$,,rF   c                   V^8X  d   W ,          ^ 3# \        V 4      p^ p^V^\        V4      ,          ,           ^,           ,          p\        w  rgr V^,          '       d~   Wp,          pW,           pW^,
          ,          p	V	\        \        Wy,	          4      ,          ,           p	W8  d!   WyV,
          ,	          pWV,
          ,          pTp	V^,          pV'       g    Wx3# W ,          p WD,           pW3,           ^,
          pV\        \        W,	          4      ,          ,           pW58  d!   WV,
          ,	          p WCV,
          ,          pTpV^,          pEK  )zun-th power of a fixed point number with precision prec

Returns the power in the form man, exp,
man * 2**exp ~= y**n
)r   r    r   r>   )
ynr?   bcexpworkprec_pmpepbcs
   &&&       rC   int_pow_fixedr   Z  s    	Avax	!B
CD1Xa[=(1,-HNA2
q55BB6MCBI//C~L)Hn$FA 6M CgWq['#ag,''=k"A= CBFrF   c                    ^2p \        WW,          ,
          4      p\        \        VRV,          ,          4      4      p^
pTp	Tp
\        WBV,           4       F  p\        Wa^,
          V
4      w  r\        W^,
          V
,          V,
          V,
          V	,
          4      p\        V ^V,          V,
          V	,           4      V,          pW^,
          \        WkV
,
          4      ,          ,           V,          pTp
K  	  V#   \         d?    \	        XT4      p\	        T4      p\        ^Yt4      p\        YWT4      p\        T4      p ELi ; i)2   g      ?)r   r   r>   OverflowErrorr   r0   r   r   r   r   r   )r   r   r?   exp1starty1rj   fnextraextra1prevpri   r   r   rc   Bs   &&&&            rC   nthroot_fixedr     s   EAag~&BQK ! EFEU
+qA#u-B1ea",v561ac$hvo&*A#U7+++a/ , H  b% a[!R'BE"1Is   :C9 9AEEc                l   V w  rErgV'       d   \        R4      hV'       g^   V \        8X  d   \        # V \        8X  d!   V^ 8  d   \        # V^ 8X  d   \        # \        # V'       g   \        # V^ 8  d   \        # \        # RpV^8  dU   V^ 8X  d   \        # V^8X  d   \        WV4      # VR8X  d   \        \        WV4      # \        V,          pRp^p	W),          pV) pV^8  d   VR8  g&   V\        ^RVR,          ,          ,           4      8  dz   V^
,           p
\        V4      p\        ^W4      p\        WW4      p\        V^ ,          V^,          V^,          V^,          W#4      p V'       d   \        \        WX	,
          V4      # V # V^V,          ,           W!,          ,
          p
V^
8  d   W^
,          ,          p
WV,          ,
          p
Wz,
          p^ pWn,           pV^ 8  d   ^pV) pV'       d   VVV,          ,          pMVVV,          ,          p\        W^4      p^
pWn,           V^,
          V
,          ,
          V,          V,
          p^ pV'       d   VR8X  g   VR8X  d   ^pMVR	8X  g   VR
8X  d   ^p\        VV,           WV4      p\        VVW#4      p V'       d   \        \        WX	,
          V4      # V # )zYnth-root of a positive number

Use the Newton method when faster, otherwise use x**(1/n)
znth root of a negative numberFTi N  gL<@gףp=
?ur   drB   rG   )r   r%   r   r    r#   r)   r.   r3   r>   r   r0   r   r   r   r   r   )ro   r   r?   rN   signmanr   r   flag_inverseextra_inverseprec2r   nthrj   shiftsign1esr   r   	rnd_shifts   &&&&                rC   mpf_nthrootr     si   
 Ds;<<9K:1uAvKKq5LL1u6K61C((74#..S!B2v1:C$D.,@(A!Ar	a[1b(AE'adAaD!A$!d84$6<<H1Q3J$&!E 	2vaJEE	B	AvSAA

CEY!U{"Q&%/DI#:I#:I
Iq
6CS$*AtQ] 2C88rF   c                    \        V ^W4      # )zcubic root of a positive number)r   )ro   r?   rN   s   &&&rC   mpf_cbrtr     s    q!T''rF   c                   V \         9   d%   \         V ,          w  r4WA8  d   W4V,
          ,	          # V^
,           pV\        8:  dC   Vf   \        V4      p\        V 4      pWV,
          ,          p\	        Wu4      Wb,          ,           pM&\        \        \        V 4      V^,           4      V4      pV \        8  d   W3\         V &   WV,
          ,	          # )zT
Fast computation of log(n), caching the value for small n,
intended for zeta sums.
)	log_int_cacheLOG_TAYLOR_SHIFTrx   r   log_taylor_cachedr   r   r   MAX_LOG_INT_CACHE)	r   r?   ln2valuevprecrO   rj   xrP   s	   &&&      rC   log_int_fixedr     s    
 	M$Q'=T\**	B	;B-CQKQ$Ka$qu,WXa["Q$/47aD>rF   c                    ^ p W,           ^,	          pV^8  d   \        W,
          4      ^8  d   V # \        W,          4      pTp V^,          pKM  )zR
Fixed-point computation of agm(a,b), assuming
a, b both close to unit magnitude.
)absr6   )rY   rZ   r?   ianews   &&&  rC   	agm_fixedr     sH    
 	
A
axq5S[1_HqsO	QrF   c                $   W ,          V,	          pT;p;rEV'       d)   WR,          V,	          pWE,          V,	          pW4,          pK0  V\         V,          ,          pW3,          V^,
          ,	          pV\        W,          4      ,          V,	          pT ;p;rEV'       d)   WR,          V,	          pWE,          V,	          pWd,          pK0  \         V,          V^,          ,           pWf,          V,	          p\        W6V4      p\        V4      V,          V,          # )a  
Fixed-point computation of -log(x) = log(1/x), suitable
for large precision. It is required that 0 < x < 1. The
algorithm used is the Sasaki-Kanada formula

    -log(x) = pi/agm(theta2(x)^2,theta3(x)^2). [1]

For faster convergence in the theta functions, x should
be chosen closer to 0.

Guard bits must be added by the caller.

HYPOTHESIS: if x = 2^(-n), n bits need to be added to
account for the truncation to a fixed-point number,
and this is the only significant cancellation error.

The number of bits lost to roundoff is small and can be
considered constant.

[1] Richard P. Brent, "Fast Algorithms for High-Precision
    Computation of Elementary Functions (extended abstract)",
    http://wwwmaths.anu.edu.au/~brent/pd/RNC7-Brent.pdf

)r   r6   r   r   )r   r?   x2ro   rY   rZ   r   ri   s   &&      rC   log_agmr   )  s    2 #$BNAN
TdNSTM	'4-A	
QA	
:ag	%AMAM
TdNSTM		$1a4 A	
tA!ATNd"q((rF   c                $   \        V4       F  p\        W,          4      p K  	  \        V,          pW,
          V,          W,           ,          pV^ 8  pV'       d   V) pWU,          V,	          pWw,          V,	          pTp	V^,          p
WX,          V,	          p^pV'       dB   WV,          ,          p	V^,          pWV,          ,          p
WX,          V,	          pV^,          pKI  W,          V,	          p
W,           ^V,           ,          pV'       d   V) # V# )a"  
Fixed-point calculation of log(x). It is assumed that x is close
enough to 1 for the Taylor series to converge quickly. Convergence
can be improved by specifying r > 0 to compute
log(x^(1/2^r))*2^r, at the cost of performing r square roots.

The caller must provide sufficient guard bits.
)r   r6   r   )r   r?   rj   r   onerP   r   v2v4s0s1kro   s   &&&          rC   
log_taylorr   X  s     AYqw 
T/C
%$!% Aq5DB
#$B
%DB	
B	
AB	
$A	A

1f	Q
1fTdN	Q
%DB	AaCAr	HrF   c                   W\         ,
          ,	          p\        V,          pW1,
          pW#3\        9   d   \        W#3,          w  rVM*W#\         ,
          ,          p\        WS^4      pWV3\        W#3&   WT,          pWd,          pW,
          V,          V,          pWq,          \        V,          V,           ,          pW,          V,	          p	W,          V,	          p
TpV^,          pW,          V,	          p^pV'       dB   WV,          ,          pV^,          pWV,          ,          pW,          V,	          pV^,          pKI  W,          V,	          pW,           ^,          pWn,           # )zX
Fixed-point computation of log(x), assuming x in (0.5, 2)
and prec <= LOG_TAYLOR_PREC.
)r   cache_prec_stepslog_taylor_cacher   r   )r   r?   r   cached_precdprecrY   log_ar   rP   r   r   r   r   r   ro   s   &&             rC   r   r   z  s$   
 	
##$A"4(KE	++#AN35 00111-,-:(KA	OE
%DQA	
DA-.A
#$B
%DB	
B	
AB	
$A	A

d
	Q
d
TdN	Q
%DB	1A9rF   c                $   V w  r4rVV'       g4   V \         8X  d   \        # V \        8X  d   \        # V \        8X  d   \        # V'       d   \	        R4      hV^,           pV^8X  d-   V'       g   \         # \        V\        V4      ,          V) W4      # WV,           p\        V4      p	V	^8:  d   ^V	,
          p
V
'       d   \        V,          V,
          pMV\        V^,
          ,          ,
          p\        V4      pWl,
          pW8  d!   \        WW,
          WR4      p\        WW4      # W},          pV	R8  d/   \        V	4      V8  d   \        V\        V4      ,          V) W4      # V\        8:  d>   \        \        WGV,
          4      V4      pV'       d   W\        V4      ,          ,          pM\V) \        ,          pVV,
          p\!        V V4      p VV) ,          p\#        \%        W4      V4      ) pVV\        V4      ,          ,          p\        W) W4      # )z^
Compute the natural logarithm of the mpf value x. If x is negative,
ComplexResult is raised.
zlogarithm of a negative numberr   i'  )r   r$   r#   r%   r   r   rx   r   r   r   r   r5   LOG_TAYLOR_PRECr   r   LOG_AGM_MAG_PREC_RATIOr/   r   r   )r   r?   rN   r   r   r   r   rO   magabs_magr   r   r   cancellationr   r]   optimal_magr   s   &&&               rC   r   r     s   
 Ds :e|9Tk9Tk<==	B axLC	"-sD>>
&C#hG !|'	RK3&D'BqD/*Dtnx%wz3SAAq33B Gr!IbM 1B3BB 
_fSR%0"5Yr]""Ac11#aO
|Xa_b))	Qy}_3**rF   c           
        V^,          '       g   YrV ^,          '       gy   V^,          '       g3   Yu;8X  d   \         8X  d	    \        #  \        W39   d   \        # \        # V \         8X  d   \	        \        V4      W#4      # V \        8X  d   \        # \        # \        W 4      p\        W4      p^p\        WEW&,           4      p\        V\        ^
4      pV^,          V^,          ,           p	V\         8X  g   W) ^,          8  d2   \        WEW&,           \        V^,          V^,          4      ,
          4      p\        \	        WrV4      R4      # )z)
Computes log(sqrt(a^2+b^2)) accurately.
rG   )r   r$   r%   r#   r   r(   r-   r+   r!   minr/   )
rY   rZ   r?   rN   a2b2r   h2	cancelledmag_cancelleds
   &&&&      rC   mpf_log_hypotr    s   
 Q441Q44tt v~K:71:t119K	B	BE		$BE2&IaL1-M E]VQY6RTZBqE"Q%(889WRs+R00rF   c                 |   V^d8  d5   \         P                  ! \        W^5,
          ,	          4      R,          4      pM-\         P                  ! \        V 4      RV,          ,          4      p^2p\        \        VR,          4      ^5V,
          ,	          4      p^2p\	        W14       F  pWT,          pW%V,
          ,          p\        W%4      w  rgWu,          V,          pV\        WV,
          4      ,
          V,          \        V,          V^,          V,	          ,           ,          p	W),
          pTpK  	  \        W#V,
          4      # )d   g       @g      @C)rf   atanr>   r   r   cos_sin_fixedr   r   )
r   r?   rj   r   extra_prO   cossintanrY   s
   &&        rC   atan_newtonr    s    s{IIc1Bw<)'12IIc!fS$Y&'ECGE*+AG%&
U(O 'yS &G$$+'2+36B,1OPE ' !4Z  rF   c                     ^\        V^,
          4      ,          ^,           pW!,
          pW3\        9   d   \        W3,          w  rEM)W\        ,
          ,          p\        WB4      pWE3\        W3&   WC,	          WS,	          3# rU   )r   atan_taylor_cacheATAN_TAYLOR_SHIFTr  )r   r?   r   r   rY   atan_as   &&    rC   atan_taylor_get_cachedr  "  st    
 $q&!"b(ELE	z&&%ah/	6++,Q&'(k!(#J&/**rF   c                    W\         ,
          ,	          p\        W!4      w  r4W,
          pWQ,          V^,          V,	          W5,          V,	          ,           \        V,          ,           ,          ;rgV^,          V,	          pW,          V,	          p	V^,          p
Wy,          V,	          p^pV'       dB   WgV,          ,          pV^,          pWV,          ,          p
Wy,          V,	          pV^,          pKI  W,          V,	          p
Wj,
          pWL,           #    )r  r  r   )r   r?   r   rY   r  r   r   rP   r   r   r   r   ro   s   &&           rC   atan_taylorr  1  s    	
%%	&A&q/IA	Aiaddlqsd{;w$OPPB
Q$$,B
'd	B	
AB	
DA	A

1f	Q
1fV	Q
'd	B
A:rF   c           	          V '       g   \        \        W4      R4      # \        \        \        V\        V,          4      R4      4      # )rV   rG   )r/   r   r*   r4   )r   r?   rN   s   &&&rC   atan_infr  E  s5    *B//9VD,s*;<bABBrF   c                    V w  r4rVV'       gF   V \         8X  d   \         # V \        8X  d   \        ^ W4      # V \        8X  d   \        ^W4      # \        # WV,           pWq^,           8  d   \        W1V4      # V) V^,           8  d   \        V ^V,
          W4      # V^,           \        V4      ,           pV^8  d   \        ^W4      p Rp	MRp	\        W4      p
V'       d   V
) p
V\        8  d   \        W4      pM\        W4      pV	'       d!   \        V4      ^,	          ^,           V,
          pV'       d   V) p\        W) W4      # )    TF)r   r#   r  r$   r%   r5   r   r0   r   ATAN_TAYLOR_PRECr  r  r   r   )r   r?   rN   r   r   r   r   r   rO   
reciprocalr   rY   s   &&&         rC   mpf_atanr  J  s   Ds:e|9Xa33:hq$44
(C
"W}C((td2g~1afd00	SX	B
axA"

AB	rlAoq A%B3**rF   c           	      ^   Vw  rErgV w  rrV	'       g   V \         8X  d-   V\        8w  d"   \        V4      ^ 8  d   \         # \        W#4      # V \        \
        39   db   V\        \
        39   d   \        # V \        8X  d   \        \        W#4      R4      # \        \        \        V\        V,          4      R4      4      # \        # V'       d+   \        \        \        V 4      W\        V,          4      4      # V'       g_   V\        8X  d   \        # V\        8X  d   \         # V\
        8X  d   \        W#4      # V \         8X  d   \         # \        \        W#4      R4      # \        \        WV^,           4      V^,           4      pV'       d   \        \        V^,           4      WV4      # \        WV4      # )r  rG   )r   r%   r'   r   r#   r$   r/   r*   r4   	mpf_atan2r  r.   r+   r)   )r   r   r?   rN   xsignxmanxexpxbcysignymanyexpybctquos   &&&&         rC   r  r  m  sH   EE:!t){a$$$uT5M!Dy !2B779VD,s2C%DbIJJyQl36GHII9K9L:$$$:L*B//GA$q&)462Dvd1f~t377t3''rF   c           
         V w  r4rVWe,           ^ 8  d   V \         \        39  d   \        R4      hV^,           p\        W 4      p\	        \         \        \        \         W4      V4      V4      p	\        W	V4      p
\        \        WV4      ^4      # )r  z%asin(x) is real only for -1 <= x <= 1)
r    r!   r   r-   r+   r2   r,   r.   r/   r  r   r?   rN   r   r   r   r   rO   rY   rZ   r   s   &&&        rC   mpf_asinr*    sz    Ds	vzae},CDD	BAhwtQ3R8"=AbAXas+Q//rF   c                 H   V w  r4rVWe,           ^ 8  d3   V \         \        39  d   \        R4      hV \        8X  d   \        W4      # V^,           p\	        W 4      p\        \        \         W4      V4      p	\        V	\        \         W4      V4      p
\        \        WV4      ^4      # )r  z%acos(x) is real only for -1 <= x <= 1)r    r!   r   r   r-   r2   r,   r.   r+   r/   r  r)  s   &&&        rC   mpf_acosr,    s    Ds	x!|T5M! GHH:$$$	BAq%r*A74',AXas+Q//rF   c                 ^   V^,           pV w  rErgWg,           pVR8  d%   W) 8  d   \        V ^V,
          W4      # W8) ,          p\        \        \        W 4      \        V4      V4      p	\        \        V 4      W4      p	V'       d!   \        \        W\        V,          4      4      # \        WV4      # )rM   )	r5   r2   r+   r-   r    r(   r*   r   r4   )
r   r?   rN   rO   r   r   r   r   r   rX   s
   &&&       rC   	mpf_asinhr/    s    	BDs
&C
Rx9q!D&$44
t 	b126A
A"AwqS(9:;;q$$rF   c                     V^,           p\        V \        4      R8X  d   \        R4      h\        \	        \        W 4      \        V4      V4      p\        \	        WV4      W4      # )   z acosh(x) is real only for x >= 1rG   )r&   r    r   r2   r+   r-   r!   r   )r   r?   rN   rO   rX   s   &&&  rC   	mpf_acoshr2    sS    	Bq$2>??ub126A71$d00rF   c           	         V w  r4rVV'       g'   V'       d   V \         \        39   d   V # \        R 4      hWe,           pV^ 8  d-   V^8X  d   V^8X  d   \        \        .V,          # \        R 4      hV^,           pVR8  d   Wx) 8  d   \        WW4      # W) ,          p\        V \        V4      p	\        \        W4      p
\        \        \        WV4      W4      R4      # )z&atanh(x) is real only for -1 <= x <= 1r.  rG   )r   r%   r   r#   r$   r5   r+   r    r,   r/   r   r.   )r   r?   rN   r   r   r   r   r   rO   rY   rZ   s   &&&        rC   	mpf_atanhr4    s    DsSHDEE
(C
Qw!8q%=&&DEE	B
Rx9q22
t4AaAWWQ2.:B??rF   c                    V w  r4rVV'       g   V \         8X  d   \        # V # \        WV,           4      pV^ 8  d6   V^
8  g   V\        V4      8:  d   \	        \        \        V 4      4      W4      # W,           ^,           p\        V4      p	\        \        V	^4      \        V4      p
\        WV4      p\        W4      p\        WV4      p\        WV4      p\        WW4      pV# r  )r$   r%   r   r   r   r7   r   mpf_phir+   r/   r!   r   
mpf_cos_pir.   r,   )r   r?   rN   r   r   r   r   sizerO   rY   rZ   r   rP   s   &&&          rC   mpf_fibonaccir:    s    Ds:Ksv;D
ax"9.DOT77	r	BA	!Q+AbA1AbAbAd AHrF   c                &   V ^ 8  d   V ) p ^pM^ p\        RVR,          ,          4      p\        V 4      V,
          p\        ^ WT,           4      p^
^\        WE) 4      ,          ,           pW,           pWV,
          ,          p \        V,          pV^8H  p	V\        8  d   W ,          V,	          ;rW,          V,	          p\
        ;r^pV'       d^   W^,
          V,          ,          qV,          q^,          pW^,
          V,          ,          qV,          q^,          pW,          V,	          pKe  W,          V,	          pV	'       d   W,
          V,           pEM`W,           V,           pEMO\        RVR,          ,          4      pW ,          V,	          ;rW.p\        ^V4       F)  pVP                  VR,          V
,          V,	          4       K+  	  \
        .V,          p^pV'       d   \        V4       Fd  pW^,
          V,          ,          pV	'       d&   V^,          '       d   VV;;,          V,          uu&   MVV;;,          V,          uu&   V^,          pKf  	  VVR,          ,          V,	          pK  \        ^V4       F$  pVV,          VV,          ,          V,	          VV&   K&  	  \        V4      V,           pV^ 8X  df   \        VV,          W,          ,
          4      pV'       d   VV,
          pM	VV,           p\        V4       F  pVV,          V,	          pK  	  VV,	          # V^,
          p\        V4       F  pVV,          V,	          V,
          pK  	  \        \        W,          VV,          ,
          4      4      pV'       d   V) pVV,	          VV,	          3# )z
Taylor series for cosh/sinh or cos/sin.

type = 0 -- returns exp(x)  (slightly faster than cosh+sinh)
type = 1 -- returns (cosh(x), sinh(x))
type = 2 -- returns (cos(x), sin(x))
      ?g333333?re   rG   )r>   r   maxr   EXP_SERIES_U_CUTOFFr   r   appendsumr6   r   )r   r?   typer   rj   xmagr   rO   r   altr   rY   x4r   r   r   r   r   xpowersr   sumsro   rP   pshifts   &&&                     rC   exponential_seriesrH    s    	1uBCc	MAA;DAtxA3q<E	B19A
R-C19C!!#"e]Q3'MA72FAQ3'MA72FA"Ae]#A#AD$J#")1ANNGBKNR/0 zA~AYsAg1q55$q'Q,'"&q'Q,'Q	 
 72;2%A1AAwwqz)b0DG IOqyqscg'AAAAA1A Ez
 AAA#&C'A  sCGqs?+,A5AuH%%rF   c                   V\         8  d   \        W^ 4      # \        VR,          4      pW,          p\        V,          ;r4^pW ,          V,	          ;rgV'       dB   We,          qcV,          q5^,          pWe,          qdV,          qE^,          pWg,          V,	          pKI  W@,          V,	          pW4,           pTp	V'       d   W,          V,	          pV^,          pK"  W,	          # )z
Compute exp(x) as a fixed-point number. Works for any x,
but for speed should have |x| < 1. For an arbitrary number,
use exp(x) = exp(x-m*log(2)) * 2^m where m = floor(x/log(2)).
r<  )EXP_COSH_CUTOFFrH  r>   r   )
r   r?   rj   r   r   r   rY   r   ro   r   s
   &&        rC   exp_basecaserK  >  s     o!!1--D#IAID$B	Acd]A
	qq&!	qq&!TdN
$4B
A	A
STM	Q6MrF   c                    V\         8  d   \        W^4      w  r#W#,           W#,
          3# \        W4      p\        W,           ,          V,          pWE3# )z 
Computation of exp(x), exp(-x)
)rJ  rH  rK  r   )r   r?   coshsinhrY   rZ   s   &&    rC   exp_expneg_basecaserO  W  sK     o'3
y$)##QA	TY	A%A4KrF   c                   V\         8  d   \        W^4      # V\        ,
          pW,	          p\        V4      pV\        9  dS   V^
\         ,           \        ,
          ,          p\        V^
\         ,           ^4      w  rgV^
,	          V^
,	          3\        V&   \        V,          w  rg\         V,
          pWh,          pWx,          pWV,          ,          p \
        V,          p	T p
^pW ,          V,	          ) pV'       dR   W,          qV,          q^,          qV ,          V,	          pW,          qV,          q^,          qV ,          V,	          ) pKY  W,          W,          ,
          V,	          W,          W,          ,           V,	          3# )z
Compute cos(x), sin(x) as fixed-point numbers, assuming x
in [0, pi/2). For an arbitrary number, use x' = x - m*(pi/2)
where m = floor(x/(pi/2)) along with quarter-period symmetries.
)COS_SIN_CACHE_PRECrH  COS_SIN_CACHE_STEPr>   cos_sin_cacher   )r   r?   precsr   r   wcos_tsin_toffsetr	  r
  r   rY   s   &&           rC   cos_sin_basecaserY  b  s9      !!1--%%E	
AAA%%&889)!R0B-BAF!2I3a #LE$&F	E	EeOA
T/C
C	A34-A
	61!}1	61A#$'71Ysy T)ci	.Ad-JKKrF   c                    V w  r4rVV'       Ed   We,           pV^,           pV'       d   V) pVR8  d=   V^ 8  d6   \        V\        RV,          4      ,           4      p	\        WV,          W4      # Wx) 8  d   \        \        W1V4      # V^8  dV   W,           p
WZ,           pV^ 8  d
   WK,          pM	WK) ,	          p\        V
4      p\        W4      w  r\        V4      pW,          pM#WX,           pV^ 8  d
   WK,          pM	WK) ,	          p^ p\        W4      p\        WNV,
          W4      # V'       g   \        # V \        8X  d   \        # V # )   r9   g333333?)mpf_er>   r1   r5   r    rx   divmodrK  r   r$   r   )r   r?   rN   r   r   r   r   r   rO   ewpmodrX  r   lg2r   s   &&&            rC   r   r     s"   Ds
shBY$C#:#(bT#X&'Aqs(D669tT557 HE[F{MG$E"C!>DAAAIAXF{MG$A1!C2t11EzHrF   c                "   V w  rErgV'       gs   V'       dk   V'       d)   V \         8X  d   \        # V \        8X  d   \        # \        # V \         8X  d   \         \         3# V \        8X  d   \         \        3# \        \        3# Wg,           pV^,           p	VR8  dM   W) 8  d=   V'       d   \        V ^V,
          W4      # \        \        ^ W4      p
\        WW4      pW3# W) ,          p	V^
8  d   ^^V^,
          ,          ,          V	8  dd   V'       d&   \        \        \        .V,          ^V,
          W4      # \        \        \        V 4      W4      R4      ;rV'       d   \        V4      pW3# V^8  dY   W,           pWn,           pV^ 8  d
   W_,          pM	W_) ,	          p\        V4      p\        VV4      w  pp\        V4      pVV,          pM#Wi,           pV^ 8  d
   W_,          pM	W_) ,	          p^ p\        VV	4      w  ppVV^V,          ,	          ,           p
VV^V,          ,	          ,
          pV'       d   V) pV'       d   W,          V
,          p\        WY) W4      # \        V
VV	,
          ^,
          W4      p
\        VVV	,
          ^,
          W4      pW3# )z4Simultaneously compute (cosh(x), sinh(x)) for real xrG   )r#   r    r$   r!   r%   r5   r/   r   r(   r*   rx   r]  r>   rO  r   )r   r?   rN   tanhr   r   r   r   r   rO   rM  rN  r   ro   r_  rX  r   r`  r   rY   rZ   s   &&&&                 rC   mpf_cosh_sinhrd    s)   DsSDy+Ez%<K9dD\):tUm+Tz
&C	bB
Rx9"1afd88tQ2Dq2D:
t
Rxa#a%j>B"D<#5qvtIIggaj$<bAAAAJ4K
QwQ;A Aa~1F	c	Q;A Aq"%DAqAaC>DAaC>Duzd"Cd00D!B$q&$4D!B$q&$4zrF   c                    V^ 8  d   ^ p ^V,          pW2,           V,           p\        V^,
          4      pV^,	          pWa,           p	V	^ 8  d
   W	,          p
M	W	) ,	          p
\        W4      w  rW8  d
   W|,
          pMTpWV,           ^
,
          ,	          '       d   \        V4      pW,	          p
Wb,
          pM7V^,          pK  W2) ,          pW,           p	V	^ 8  d
   W	,          p
M	W	) ,	          p
^ pWV3# r6  )r   r]  r>   )r   r   r   rO   r   cancellation_precr_  pi2pi4rX  r   r   r   smalls   &&&&          rC   mod_pi2rj    s    
Qw "aH00E57#C(C[F{MG$!>DAwC##FH[FA
tQ;A A8OrF   c                |   V w  rVrxV'       gE   V'       d   \         \         rM\        \        rV^ 8X  d   W3# V^8X  d   V	# V^8X  d   V
# V^8X  d   V
# W,           pV^
,           pV^ 8  dy   W) 8  dr   V'       d   \        V \	        V4      4      p \        \        ^W4      p	\        V ^V,
          W4      p
V^ 8X  d   W3# V^8X  d   V	# V^8X  d   V
# V^8X  d   \        WW4      # V'       Ed   VR8  d   VR8X  d2   \        p	\        \        3\        V^,          4      V,          ,          p
MV^ 8X  d   \        \        rM\        \        rV^ 8X  d   W3# V^8X  d   V	# V^8X  d   V
# V^8X  d   \        WW4      # Wg) ^,
          ,	          ^,           ^,	          pWmV) ^,
          ,          ,
          p\        V4      V,           pV^
,           V,
          pW|,           pV^ 8  d
   Wo,          pM	Wo) ,	          pV\        V4      ,          V,	          pM\        WgW4      w  pr\        VV4      w  rV^,          pV^8X  d   V
) T	rMV^8X  d   V	) V
) rM
V^8X  d   Y) rV'       d   V
) p
V^ 8X  d   \        W) W4      p	\        W) W4      p
W3# V^8X  d   \        W) W4      # V^8X  d   \        W) W4      # V^8X  d   \        WW4      # R# )zz
which:
0 -- return cos(x), sin(x)
1 -- return cos(x)
2 -- return sin(x)
3 -- return tan(x)

if pi=True, compute for pi*x
NrG   )r%   r    r   r-   r   r5   r!   boolr.   r   r   rj  rY  r   r   )r   r?   rN   whichpir   r   r   r   r   ro   r   rO   r   mag2rX  r   r]   s   &&&&&             rC   mpf_cos_sinrp    s    DsqqA:ad{A:axA:axA:ax
(C	B Qw9Avbz*D!T/AAqvt1Az!$;z!8z!8z+at"AA	r"9by5M$sQw-$"67u1e1z!$;z!8z!8z'!"::d1fo"q(C46]#}s"BYQ;A Ax|^"3S-1Ar"DA	AA	
aAA	
aQBA	
a2ABzC+C+tzAsD..zAsD..zQ4-- rF   c                     \        WV^4      # rU   rp  r   r?   rN   s   &&&rC   mpf_cosrt  b      [#q-I&IrF   c                     \        WV^4      # r  rr  rs  s   &&&rC   mpf_sinrw  c  ru  rF   c                     \        WV^4      # )   rr  rs  s   &&&rC   mpf_tanrz  d  ru  rF   c                     \        WV^ ^4      # r6  rr  rs  s   &&&rC   mpf_cos_sin_pir|  e  s    KaQR4S-SrF   c                     \        WV^^4      # rU   rr  rs  s   &&&rC   r8  r8  f      AS!Q0O)OrF   c                     \        WV^^4      # r  rr  rs  s   &&&rC   
mpf_sin_pir  g  r~  rF   c                 (    \        WV4      ^ ,          # r6  rd  rs  s   &&&rC   mpf_coshr  h      mAS.I!.L'LrF   c                 (    \        WV4      ^,          # rU   r  rs  s   &&&rC   mpf_sinhr  i  r  rF   c                     \        WV^R7      # )rV   )rc  r  rs  s   &&&rC   mpf_tanhr  j  s    mASq.Q'QrF   c                     Vf   \        V^,
          4      p\        W4      w  r4\        V4      p\        WA4      w  rVV^,          pV^ 8X  d   WV3# V^8X  d   V) V3# V^8X  d   V) V) 3# V^8X  d   We) 3# R # r   )r   r]  r>   rY  )r   r?   rg  r   r   r   ro   r]   s   &&&     rC   r  r  o  s    
{tAv!>DAAAA$DA	AAAvad{Avqb!e|Avqb1"f}Avae|vrF   c                     Vf   \        V4      p\        W4      w  r4\        V4      p\        WA4      pV^ 8  d	   WS,          # WS) ,	          # r   )rx   r]  r>   rK  )r   r?   r   r   r   rP   s   &&&   rC   	exp_fixedr  {  sF    
{o!>DAAAQAAvvRyrF   sagez)Warning: Sage imports in libelefun failed)F)FNr   r6  )rI   rf   r   backendr   r   r   r   r   r   r	   libmpfr
   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   
libintmathr7   rJ  r>  rQ  rR  rS  r   r   r   r   r   r   r  r  r  r   r   r   rJ   rS   rW   rk   rp   rx   r   r   r   r   r   r   r   r   r   r   r   r7  r   r\  
mpf_degreempf_ln2mpf_ln10r   r   
mpf_sqrtpimpf_ln_sqrt2pir   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r*  r,  r/  r2  r4  r:  rH  rK  rO  rY  r   rd  rj  rp  rt  rw  rz  r|  r8  r  r  r  r  r  r  sage.libs.mpmath.ext_libmplibsmpmath	ext_libmp_lbmpImportErrorAttributeErrorr    rF   rC   <module>r     s.  	    G G             hOO  h          r7 	8O,Q.	/AQT/2256QqSAA 
0*"
'   A A ? ?8 
X	Y	V	R,   
     i(h'g&l+
i(j) C C
 , ,  -
#$45 ' -8"l, !+ Qf % (,-)^ D D $ F+P%1X!$+(C
 %  +F ) !(F % 	0 % 0 & %" & 1 & @*  * 8I&V2	L: $ *Z  * @F!H (qU M.^ $ I# I# I * S& O& O$ L$ L$ Q

	 f;22>>----------OO	++++  ( ;9:;s   A=L   L54L5