
    [hU                     *   S r SSKrSSKJr  SSKJr  SSKJrJrJrJrJ	r	J
r
  SSKJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/J0r0J1r1J2r2J3r3J4r4J5r5J6r6J7r7J8r8  SSK9J:r:  \
S	:X  a  S
r;OSr;Sr<\
S	:X  a  Sr=OSr=Sr>0 r?Sr@0 rASrBSrC0 rDSrESrFSrG0 rHSS/rI\" S\" \B5      S-   5       H  rJ\I\K" S\J-  \B5      S-   /S\JS-
  -  -  -  rIM!     S rLS rMS rNS rOSXS jrP\LS 5       rQ\LS 5       rR \" S5      rS\" S 5      rT\" S!5      rU\" S"5      rVS# rW\LSYS$ j5       rXS% rYS& rZ\LS' 5       r[\LS( 5       r\\M" \\5      r]\M" \X5      r^\M" \[5      r_\M" \Y5      r`\M" \Q5      ra\M" \R5      rb\LS) 5       rc\LS* 5       rd\M" \d5      re\M" \c5      rf\4S+ jrgS, rhS- ri\4S. jrj\4S/ jrkSZS0 jrlS1 rmS2 rnS[S3 jroS4 rp\4S5 jrqS6 rrS7 rsS8 rtS9 ruS: rv\4S; jrw\4S< jrx\4S= jry\4S> jrz\4S? jr{\4S@ jr|\4SA jr}\4SB jr~S[SC jrSD rSE rSF r\4SG jr\S4SH jrSI r\SS4SJ jr\4SK jr\4SL jr\4SM jr\4SN jr\4SO jr\4SP jr\4SQ jr\4SR jr\4SS jrSZST jrSZSU jr\
SV:X  a   SSKJs  Js  Jr  \Rh                  r4\GR                  r\R                  rq\GR                  r\GR                  r\R                  rg\GR"                  r\GR                   r\R                  rlgg! \\4 a    \" SW5         gf = f)\a(  
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   ^  ST l         ST l        U 4S jnT R                  Ul        T R                  Ul        U$ )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                    > TR                   nX::  a  TR                  X -
  -	  $ [        U S-  S-   5      nT" U40 UD6Tl        UTl         TR                  X0-
  -	  $ )Ng?
   )	memo_precmemo_valint)preckwargsrG   newprecfs       N/var/www/auris/envauris/lib/python3.13/site-packages/mpmath/libmp/libelefun.pygconstant_memo.<locals>.g^   s^    KK	::).11d4il#w)&)
zzgl++    )rG   rH   __name____doc__)rM   rO   s   ` rN   constant_memorT   U   s5     AKAJ, AJ		AIHrQ   c                 D   ^  [         4U 4S jjnT R                  Ul        U$ )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                 x   > U S-   nT" U5      nU[         [        4;   a  US-  n[        SX2* [        U5      X5      $ )Nr?   r   r   )r   r   r    r   )rJ   rndwpvfixeds       rN   rM   def_mpf_constant.<locals>.fr   sB    BY"I8]++FAAsHQK;;rQ   )r   rS   )rZ   rM   s   ` rN   def_mpf_constantr\   j   s      < AIHrQ   c                     X!-
  S:X  a?  [        SU-  S-   5      nU(       d
  US-  (       a  [        X@S-  -  U4$ [        * X@S-  -  U4$ X-   S-  n[        XXS5      u  pgn[        XX#5      u  pnX-  X-  -   Xz-  X-  4$ )Nr   rB      )r   r   bsp_acot)qab
hyperbolica1mp1q1r1p2q2r2s               rN   r_   r_   {   s    uz1q\1BAIr))8RQ$Y**	
qA!.JBB!.JBB525="%&&rQ   c                     [        SU-  [        R                  " U 5      -  S-   5      n[        U SX25      u  pEnXE-   U-  XP-  -  $ )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?   r   )rI   mathlogr_   )ra   rJ   rc   Npr`   rs          rN   
acot_fixedrs      sI    
 	D4K#b()Aq!A*GA!S4K13rQ   Fc                 ~    Sn[         nU  H,  u  pVU[        U5      [        [        U5      X-   U5      -  -  nM.     XC-	  $ )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) + ...
rF   )r   r   rs   )coefsrJ   rc   	extraprecsra   rb   s          rN   machinrx      sD     IA	SVjQDDD NrQ   c                      [        / SQU S5      $ )zn
Computes ln(2). This is done with a hyperbolic Machin-type formula,
with binary splitting at high precision.
))      )i  )r=   i-"  Trx   rJ   s    rN   	ln2_fixedr      s     3T4@@rQ   c                      [        / SQU S5      $ )zF
Computes ln(10). This is done with a hyperbolic Machin-type formula.
)).      )"   1   )r?      Tr}   r~   s    rN   
ln10_fixedr      s    
 14>>rQ   iqc i-~ i@	    c                 f   X-
  S:X  aO  [        SU-  S-
  SU-  S-
  -  SU-  S-
  -  5      nUS-  [        S-  -  S-  nSU-  U-  [        [        U-  -   -  nOWU(       a  US:  a  [	        S	X5        X-   S-  n[        XUS-   U5      u  pn
[        XqUS-   U5      u  pnX-  nX-  nX-  X-  -   nXEU4$ )
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.
r         rB   r^      rD      z  binary splitting)r   CHUD_CCHUD_ACHUD_Bprintbs_chudnovsky)ra   rb   levelverboserO   rq   r`   midg1rf   rg   g2ri   rj   s                 rN   r   r      s     	sax1Q1Q1Q'(qD619"!GaK6&(?+uqy&-sQh"157G<
"357G<
EEEBEM7NrQ   c                     [        U S-  S-  S-   5      nU(       a  [        SU5        [        SUSU5      u  pEn[        [        SU -  -  5      nU[        -  U-  U[
        U-  -   [        -  -  nU$ )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 ],@rB   zbinary splitting with N =r   )rI   r   r   r8   r   r   CHUD_D)	rJ   r   verbose_baserp   rO   rq   r`   sqrtCrY   s	            rN   pi_fixedr      sv     	Dl*Q./A)1-Aq!W-GA!v$'(E	&!F1H*f,-AHrQ   c                     [        U 5      S-  $ )N   )r   r~   s    rN   degree_fixedr      s    D>3rQ   c                     X-
  S:X  a  [         [        U5      4$ X-   S-  n[        X5      u  p4[        X!5      u  pVX6-  U-   XF-  4$ )zY
Sum series for exp(1)-1 between a, b, returning the result
as an exact fraction (p, q).
r   rB   )r   r   bspe)ra   rb   re   rf   rg   ri   rj   s          rN   r   r      sN    
 	saxA	
qA!ZFB!ZFB58RU?rQ   c                     [        SU -  [        R                  " U 5      -  S-   5      n[        SU5      u  p#X#-   U -  U-  $ )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?   r   )rI   rn   ro   r   )rJ   rp   rq   r`   s       rN   e_fixedr   	  sB     	CHTXXd^#b()A!9DAS4K!rQ   c                 T    U S-  n [        [        SU -  -  5      [        U -  -   nUS-	  $ )z*
Computes the golden ratio, (1+sqrt(5))/2
rF   rB      )r8   r
   r   )rJ   ra   s     rN   	phi_fixedr     s2    
 	BJD8af%&'T/:A7NrQ   c           	      d    U S-   n[        [        [        [        U5      S5      U5      U S-
  5      $ )NrF   r   )r   mpf_logr1   mpf_pi)rJ   rX   s     rN   ln_sqrt2pi_fixedr   *  s.    	BGIfRj!4b946BBrQ   c                 ,    [        [        U 5      U 5      $ N)r   r   r~   s    rN   sqrtpi_fixedr   0  s    htnd++rQ   c           	         U u  pEpgUu  ppU(       a  U
S:  a  [        S5      eU
S:  a  [        U SU-  X-  -  X#5      $ U
S:X  a  U	S:X  a8  U(       a%  [        [        [	        XS-   [
        U   5      X#5      $ [	        XU5      $ U(       a"  [        [	        XS-   [
        U   5      U	* X#5      $ [        [	        XS-   U5      XU5      $ [        XS-   U5      n[        [        X5      X#5      $ )zJ
Compute s**t. Raises ComplexResult if s is negative and t is
fractional.
r   z,negative number raised to a fractional powerrD   r   rF   )	r   r3   r0   r"   r4   r5   r   mpf_expr/   )rw   trJ   rW   ssignsmansexpsbctsigntmantexptbccs                rN   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=$,,rQ   c                    US:X  a  X -  S4$ [        U 5      nSnSUS[        U5      -  -   S-   -  n[        u  pgp US-  (       aK  Xp-  nX-   nXS-
  -  n	U	[        [        Xy-	  5         -   n	X:  a  XyU-
  -	  nXU-
  -  nUn	US-  nU(       d   Xx4$ X -  n XD-   nX3-   S-
  nU[        [        X-	  5         -   nX5:  a  XU-
  -	  n XCU-
  -  nUnUS-  nM  )zun-th power of a fixed point number with precision prec

Returns the power in the form man, exp,
man * 2**exp ~= y**n
rB   r   r   r   )r   r"   r   rI   )
ynrJ   bcexpworkprec_pmpepbcs
             rN   int_pow_fixedr   Z  s    	Avax	!B
CD1Xa[=(1,-HNA2
q5BB6MCBI//C~L)Hn$FA 6M CgWq['#ag,''=k"A= CBF+ rQ   c                    Sn [        XX-  -
  5      n[        [        USU-  -  5      5      nSnUn	Un
[        XBU-   5       Ha  n[        XaS-
  U
5      u  p[        XS-
  U
-  U-
  U-
  U	-
  5      n[        U SU-  U-
  U	-   5      U-  nXS-
  [        XkU
-
  5      -  -   U-  nUn
Mc     U$ ! [         a=    [	        WU5      n[	        U5      n[        SXt5      n[        XWU5      n[        U5      n Nf = f)N2   g      ?r   rF   rB   )r   r   rI   OverflowErrorr   r2   r   r   r   r   r   )r   r   rJ   exp1starty1rr   fnextraextra1prevprq   r   r   rk   Bs                   rN   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   *B) )AC0/C0c                 2   U u  pEpgU(       a  [        S5      eU(       dW  U [        :X  a  [        $ U [        :X  a  US:  a  [        $ US:X  a  [        $ [        $ U(       d  [        $ US:  a  [        $ [        $ SnUS:  aI  US:X  a  [        $ US:X  a  [        XU5      $ US:X  a  [        [        XU5      $ [        U   nSnSn	X)-  nU* nUS	:  a~  US
:  d  U[        SSUS-  -  -   5      :  a`  US-   n
[        U5      n[        SX5      n[        XX5      n[        US   US   US   US   X#5      n U(       a  [        [        XW	-
  U5      $ U $ USU-  -   X!-  -
  n
US:  a  XS-  -  n
XU-  -
  n
Xz-
  nSnXn-   nUS:  a  SnU* nU(       a	  UUU-  -  nOUUU-  -  n[        X^5      nSnXn-   US-
  U
-  -
  U-  U-
  nSnU(       a  US:X  d  US:X  a  SnOUS:X  d  US:X  a  Sn[        UU-   XU5      n[        UUX#5      n U(       a  [        [        XW	-
  U5      $ U $ )zYnth-root of a positive number

Use the Newton method when faster, otherwise use x**(1/n)
znth root of a negative numberr   FrB   r   rD   Tr   r?   i N     gL<@gףp=
?rF   r^   ur   drM   )r   r'   r!   r"   r%   r+   r0   r5   rI   r   r2   r   r    r   r   r   )rw   r   rJ   rW   signmanr   r   flag_inverseextra_inverseprec2r   nthrr   shiftsign1esr   r   	rnd_shifts                       rN   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rQ   c                     [        U SX5      $ )zcubic root of a positive numberr^   )r   )rw   rJ   rW   s      rN   mpf_cbrtr     s    q!T''rQ   c                 >   U [         ;   a  [         U    u  p4XA:  a  X4U-
  -	  $ US-   nU[        ::  a1  Uc  [        U5      n[        U 5      nXU-
  -  n[	        Xu5      Xb-  -   nO"[        [        [        U 5      US-   5      U5      nU [        :  a
  X4[         U '   XU-
  -	  $ )zT
Fast computation of log(n), caching the value for small n,
intended for zeta sums.
rF   r   )	log_int_cacheLOG_TAYLOR_SHIFTr   r   log_taylor_cachedr   r   r   MAX_LOG_INT_CACHE)	r   rJ   ln2valuevprecrX   rr   xrY   s	            rN   log_int_fixedr     s    
 	M$Q'=T\**	B	;B-CQKQ$Ka$qu,WXa["Q$/47aD>rQ   c                 t    Sn X-   S-	  nUS:  a  [        X-
  5      S:  a  U $ [        X-  5      nUn US-  nM6  )zR
Fixed-point computation of agm(a,b), assuming
a, b both close to unit magnitude.
r   r   r   r=   )absr8   )ra   rb   rJ   ianews        rN   	agm_fixedr     sM    
 	
A
axq5S[1_HqsO	Q rQ   c                 d   X -  U-	  nU=n=pEU(       a  XR-  U-	  nXE-  U-	  nX4-  nU(       a  M  U[         U-  -  nX3-  US-
  -	  nU[        X-  5      -  U-	  nU =n=pEU(       a  XR-  U-	  nXE-  U-	  nXd-  nU(       a  M  [         U-  US-  -   nXf-  U-	  n[        X6U5      n[        U5      U-  U-  $ )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

rB   r   )r   r8   r   r   )r   rJ   x2rw   ra   rb   r   rq   s           rN   log_agmr   )  s    2 #$BNAN
TdNSTM	 ! '4-A	
QA	
:ag	%AMAM
TdNSTM	 ! 
$1a4 A	
tA!ATNd"q((rQ   c                 \   [        U5       H  n[        X-  5      n M     [        U-  nX-
  U-  X-   -  nUS:  nU(       a  U* nXU-  U-	  nXw-  U-	  nUn	US-  n
XX-  U-	  nSnU(       a(  XU-  -  n	US-  nXU-  -  n
XX-  U-	  nUS-  nU(       a  M(  X-  U-	  n
X-   SU-   -  nU(       a  U* $ U$ )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   r^   r   rB   r   )r   r8   r   )r   rJ   rr   r   onerY   r   v2v4s0s1krw   s                rN   
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	HrQ   c                    X[         -
  -	  n[        U   nX1-
  nX#4[        ;   a  [        X#4   u  pVO"X#[         -
  -  n[        XSS5      nXV4[        X#4'   XT-  nXd-  nX-
  U-  U-  nXq-  [        U-  U-   -  nX-  U-	  n	X-  U-	  n
UnUS-  nX-  U-	  nSnU(       a(  XU-  -  nUS-  nXU-  -  nX-  U-	  nUS-  nU(       a  M(  X-  U-	  nX-   S-  nXn-   $ )zX
Fixed-point computation of log(x), assuming x in (0.5, 2)
and prec <= LOG_TAYLOR_PREC.
r=   r^   r   rB   r   )r   cache_prec_stepslog_taylor_cacher   r	   )r   rJ   r   cached_precdprecra   log_ar   rY   r   r   r   r   r   rw   s                  rN   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rQ   c                 `   U u  p4pVU(       d0  U [         :X  a  [        $ U [        :X  a  [        $ U [        :X  a  [        $ U(       a  [	        S5      eUS-   nUS:X  a'  U(       d  [         $ [        U[        U5      -  U* X5      $ XV-   n[        U5      n	U	S::  a\  SU	-
  n
U
(       a  [        U-  U-
  nOU[        US-
  -  -
  n[        U5      nXl-
  nX:  a  [        XX-
  XS5      n[        XX5      $ X}-  nU	S:  a)  [        U	5      U:  a  [        U[        U5      -  U* X5      $ U[        ::  a0  [        [        XGU-
  5      U5      nU(       a  X[        U5      -  -  nOHU* [        -  nUU-
  n[!        U U5      n UU* -  n[#        [%        X5      U5      * nUU[        U5      -  -  n[        X* X5      $ )z^
Compute the natural logarithm of the mpf value x. If x is negative,
ComplexResult is raised.
zlogarithm of a negative numberr?   r   r   i'  )r!   r&   r%   r'   r   r   r   r   r   r   r    r7   LOG_TAYLOR_PRECr   r   LOG_AGM_MAG_PREC_RATIOr1   r   r   )r   rJ   rW   r   r   r   r   rX   magabs_magr   r   r   cancellationr   re   optimal_magr   s                     rN   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**rQ   c           
      "   US   (       d  XpU S   (       dn  US   (       d/  Xs=:X  a  [         :X  a   [        $   [        X4;   a  [        $ [        $ U [         :X  a  [	        [        U5      X#5      $ U [        :X  a  [        $ [        $ [        X 5      n[        X5      nSn[        XEX&-   5      n[        U[        S5      nUS   US   -   n	U[         :X  d	  X* S-  :  a!  [        XEX&-   [        US   US   5      -
  5      n[        [	        XrU5      S5      $ )z)
Computes log(sqrt(a^2+b^2)) accurately.
r   r?   rF   rB   r^   rD   )r!   r&   r'   r%   r   r*   r/   r-   r#   minr1   )
ra   rb   rJ   rW   a2b2r   h2	cancelledmag_cancelleds
             rN   mpf_log_hypotr    s    
 Q41Q4t 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0rQ   c                    US:  a(  [         R                  " [        XS-
  -	  5      S-  5      nO%[         R                  " [        U 5      SU-  -  5      nSn[        [        US-  5      SU-
  -	  5      nSn[	        X15       HO  nXT-  nX%U-
  -  n[        X%5      u  pgXu-  U-  nU[        XU-
  5      -
  U-  [        U-  US-  U-	  -   -  n	X)-
  nUnMQ     [        X#U-
  5      $ )Nd   5   g      @Cg       @r   rB   )rn   atanrI   r   r   cos_sin_fixedr   r   )
r   rJ   rr   r   extra_prX   cossintanra   s
             rN   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  rQ   c                     S[        US-
  5      -  S-   nX!-
  nX4[        ;   a  [        X4   u  pEO!X[        -
  -  n[        XB5      nXE4[        X4'   XC-	  XS-	  4$ )Nr   r?   )r   atan_taylor_cacheATAN_TAYLOR_SHIFTr  )r   rJ   r   r   ra   atan_as         rN   atan_taylor_get_cachedr  "  st    
 $q&!"b(ELE	z&&%ah/	6++,Q&'(k!(#J&/**rQ   c                 0   X[         -
  -	  n[        X!5      u  p4X-
  nXQ-  US-  U-	  X5-  U-	  -   [        U-  -   -  =pgUS-  U-	  nX-  U-	  n	US-  n
Xy-  U-	  nSnU(       a(  XgU-  -  nUS-  nXU-  -  n
Xy-  U-	  nUS-  nU(       a  M(  X-  U-	  n
Xj-
  nXL-   $ )NrB   r^   r   )r  r  r   )r   rJ   r   ra   r  r   r   rY   r   r   r   r   rw   s                rN   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:rQ   c           	          U (       d  [        [        X5      S5      $ [        [        [        U[        U   5      S5      5      $ )NrD   )r1   r   r,   r6   )r   rJ   rW   s      rN   atan_infr#  E  s5    *B//9VD,s*;<bABBrQ   c                 "   U u  p4pVU(       dB  U [         :X  a  [         $ U [        :X  a  [        SX5      $ U [        :X  a  [        SX5      $ [        $ XV-   nXqS-   :  a  [        X1U5      $ U* US-   :  a  [        U SU-
  X5      $ US-   [        U5      -   nUS:  a  [        SX5      n Sn	OSn	[        X5      n
U(       a  U
* n
U[        :  a  [        X5      nO[        X5      nU	(       a  [        U5      S-	  S-   U-
  nU(       a  U* n[        X* X5      $ )Nr   r   r?      rB   TF)r!   r%   r#  r&   r'   r7   r   r2   r   ATAN_TAYLOR_PRECr!  r  r   r   )r   rJ   rW   r   r   r   r   r  rX   
reciprocalr   ra   s               rN   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**rQ   c           	         Uu  pEpgU u  ppU	(       d  U [         :X  a*  U[        :w  a   [        U5      S:  a  [         $ [        X#5      $ U [        [
        4;   a[  U[        [
        4;   a  [        $ U [        :X  a  [        [        X#5      S5      $ [        [        [        U[        U   5      S5      5      $ [        $ U(       a&  [        [        [        U 5      X[        U   5      5      $ U(       dZ  U[        :X  a  [        $ U[        :X  a  [         $ U[
        :X  a  [        X#5      $ U [         :X  a  [         $ [        [        X#5      S5      $ [        [        XUS-   5      US-   5      nU(       a  [        [        US-   5      XU5      $ [        XU5      $ )Nr   rD   r   )r!   r'   r)   r   r%   r&   r1   r,   r6   	mpf_atan2r(  r0   r-   r+   )r   r   rJ   rW   xsignxmanxexpxbcysignymanyexpybctquos                rN   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''rQ   c           
         U u  p4pVXe-   S:  a  U [         [        4;  a  [        S5      eUS-   n[        X 5      n[	        [         [        [        [         X5      U5      U5      n	[        X	U5      n
[        [        XU5      S5      $ )Nr   z%asin(x) is real only for -1 <= x <= 1   r   )
r"   r#   r   r/   r-   r4   r.   r0   r1   r(  r   rJ   rW   r   r   r   r   rX   ra   rb   r   s              rN   mpf_asinr7    sz    Ds	vzae},CDD	BAhwtQ3R8"=AbAXas+Q//rQ   c                 2   U u  p4pVXe-   S:  a0  U [         [        4;  a  [        S5      eU [        :X  a  [        X5      $ US-   n[	        X 5      n[        [        [         X5      U5      n	[        U	[        [         X5      U5      n
[        [        XU5      S5      $ )Nr   z%acos(x) is real only for -1 <= x <= 1r5  r   )r"   r#   r   r   r/   r4   r.   r0   r-   r1   r(  r6  s              rN   mpf_acosr9    s    Ds	x!|T5M! GHH:$$$	BAq%r*A74',AXas+Q//rQ   c                 0   US-   nU u  pEpgXg-   nUS:  a  X* :  a  [        U SU-
  X5      $ X8* -  n[        [        [        X 5      [        U5      U5      n	[        [        U 5      X5      n	U(       a  [        [        X[        U   5      5      $ [        XU5      $ )Nr?   r   )	r7   r4   r-   r/   r"   r*   r,   r   r6   )
r   rJ   rW   rX   r   r   r   r   r  r`   s
             rN   	mpf_asinhr<    s    	BDs
&C
Rx9q!D&$44
t 	b126A
A"AwqS(9:;;q$$rQ   c                     US-   n[        U [        5      S:X  a  [        S5      e[        [	        [        X 5      [        U5      U5      n[        [	        XU5      X5      $ )Nr5  rD   z acosh(x) is real only for x >= 1)r(   r"   r   r4   r-   r/   r#   r   )r   rJ   rW   rX   r`   s        rN   	mpf_acoshr>    sS    	Bq$2>??ub126A71$d00rQ   c           	         U u  p4pVU(       d$  U(       a  U [         [        4;   a  U $ [        S5      eXe-   nUS:  a&  US:X  a  US:X  a  [        [        /U   $ [        S5      eUS-   nUS:  a  Xx* :  a  [        XX5      $ X* -  n[        U [        U5      n	[        [        X5      n
[        [        [        XU5      X5      S5      $ )Nz&atanh(x) is real only for -1 <= x <= 1r   r   r5  r;  rD   )r!   r'   r   r%   r&   r7   r-   r"   r.   r1   r   r0   )r   rJ   rW   r   r   r   r   r  rX   ra   rb   s              rN   	mpf_atanhr@    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??rQ   c                    U u  p4pVU(       d  U [         :X  a  [        $ U $ [        XV-   5      nUS:  a3  US:  d  U[        U5      ::  a  [	        [        [        U 5      5      X5      $ X-   S-   n[        U5      n	[        [        U	S5      [        U5      n
[        XU5      n[        X5      n[        XU5      n[        XU5      n[        XX5      nU$ )Nr   rF   r?   r   )r&   r'   r   r   r   r9   r   mpf_phir-   r1   r#   r   
mpf_cos_pir0   r.   )r   rJ   rW   r   r   r   r   sizerX   ra   rb   r   rY   s                rN   mpf_fibonaccirE    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HrQ   c                    U S:  a  U * n SnOSn[        SUS-  -  5      n[        U 5      U-
  n[        SXT-   5      nSS[        XE* 5      -  -   nX-   nXU-
  -  n [        U-  nUS:H  n	U[        :  aq  X -  U-	  =pX-  U-	  n[
        =pSnU(       a4  XS-
  U-  -  oU-  oS-  nXS-
  U-  -  oU-  oS-  nX-  U-	  nU(       a  M4  X-  U-	  nU	(       a  X-
  U-   nOX-   U-   nO[        SUS-  -  5      nX -  U-	  =pX/n[        SU5       H  nUR                  US   U
-  U-	  5        M     [
        /U-  nSnU(       aa  [        U5       H>  nXS-
  U-  -  nU	(       a  US-  (       a  UU==   U-  ss'   OUU==   U-  ss'   US-  nM@     UUS   -  U-	  nU(       a  Ma  [        SU5       H  nUU   UU   -  U-	  UU'   M     [        U5      U-   nUS:X  aD  [        UU-  X-  -
  5      nU(       a  UU-
  nOUU-   n[        U5       H  nUU-  U-	  nM     UU-	  $ US-
  n[        U5       H  nUU-  U-	  U-
  nM     [        [        X-  UU-  -
  5      5      nU(       a  U* nUU-	  UU-	  4$ )	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))
r   r         ?rF   rB   g333333?rm   rD   )rI   r   maxr   EXP_SERIES_U_CUTOFFr   r   appendsumr8   r   )r   rJ   typer   rr   xmagr   rX   r   altr   ra   x4r   r   r   r   r   xpowersr   sumsrw   rY   pshifts                           rN   exponential_seriesrS    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 a e]#A#AD$J#")1ANNGBKNR/0 zA~AYsAg1q5$q'Q,'"&q'Q,'Q	 
 72;2%A 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%%rQ   c                 2   U[         :  a  [        XS5      $ [        US-  5      nX-  n[        U-  =p4SnX -  U-	  =pgU(       a(  Xe-  ocU-  o5S-  nXe-  odU-  oES-  nXg-  U-	  nU(       a  M(  X@-  U-	  nX4-   nUn	U(       a  X-  U-	  nUS-  nU(       a  M  X-	  $ )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   rG  rB   r   )EXP_COSH_CUTOFFrS  rI   r   )
r   rJ   rr   r   r   r   ra   r   rw   r   s
             rN   exp_basecaserV  >  s     o!!1--D#IAID$B	Acd]A
	qq&!	qq&!TdN ! $4B
A	A
STM	Q ! 6MrQ   c                 z    U[         :  a  [        XS5      u  p#X#-   X#-
  4$ [        X5      n[        X-   -  U-  nXE4$ )z 
Computation of exp(x), exp(-x)
r   )rU  rS  rV  r   )r   rJ   coshsinhra   rb   s         rN   exp_expneg_basecaserZ  W  sK     o'3
y$)##QA	TY	A%A4KrQ   c                    U[         :  a  [        XS5      $ U[        -
  nX-	  n[        U5      nU[        ;  a:  US[         -   [        -
  -  n[        US[         -   S5      u  pgUS-	  US-	  4[        U'   [        U   u  pg[         U-
  nXh-  nXx-  nXU-  -  n [
        U-  n	U n
SnX -  U-	  * nU(       a0  X-  oU-  oS-  oU -  U-	  nX-  oU-  oS-  oU -  U-	  * nU(       a  M0  X-  X-  -
  U-	  X-  X-  -   U-	  4$ )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.
rB   rF   r   )COS_SIN_CACHE_PRECrS  COS_SIN_CACHE_STEPrI   cos_sin_cacher   )r   rJ   precsr   r   wcos_tsin_toffsetr  r  r   ra   s                rN   cos_sin_basecaserd  b  sB      !!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KrQ   c                    U u  p4pVU(       a  Xe-   nUS-   nU(       a  U* nUS:  a/  US:  a)  [        U[        SU-  5      -   5      n	[        XU-  X5      $ Xx* :  a  [        [        X1U5      $ US:  a@  X-   n
XZ-   nUS:  a  XK-  nOXK* -	  n[        U
5      n[        X5      u  p[        U5      nX-  nOXX-   nUS:  a  XK-  nOXK* -	  nSn[        X5      n[        XNU-
  X5      $ U(       d  [        $ U [        :X  a  [        $ U $ )N   r;   r   g333333?r   )mpf_erI   r3   r7   r"   r   divmodrV  r   r&   r!   )r   rJ   rW   r   r   r   r   r  rX   ewpmodrc  r   lg2r   s                  rN   r   r     s    Ds
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HrQ   c                    U u  pEpgU(       dl  U(       ae  U(       a&  U [         :X  a  [        $ U [        :X  a  [        $ [        $ U [         :X  a  [         [         4$ U [        :X  a  [         [        4$ [        [        4$ Xg-   nUS-   n	US:  aB  X* :  a7  U(       a  [        U SU-
  X5      $ [        [        SX5      n
[        XX5      nX4$ X* -  n	US:  ah  SSUS-
  -  -  U	:  aY  U(       a  [        [        [        /U   SU-
  X5      $ [        [        [        U 5      X5      S5      =pU(       a  [        U5      nX4$ US:  aC  X-   nXn-   nUS:  a  X_-  nOX_* -	  n[        U5      n[        UU5      u  nn[        U5      nUU-  nOXi-   nUS:  a  X_-  nOX_* -	  nSn[        UU	5      u  nnUUSU-  -	  -   n
UUSU-  -	  -
  nU(       a  U* nU(       a  X-  U
-  n[        XY* X5      $ [        U
UU	-
  S-
  X5      n
[        UUU	-
  S-
  X5      nX4$ )	z4Simultaneously compute (cosh(x), sinh(x)) for real xrf  r   r   rF   r^   rD   rB   )r%   r"   r&   r#   r'   r7   r1   r   r*   r,   r   rh  rI   rZ  r   )r   rJ   rW   tanhr   r   r   r   r  rX   rX  rY  r   rw   rj  rc  r   rk  r   ra   rb   s                        rN   mpf_cosh_sinhro    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rQ   c                 >   US:  ay  Sn SU-  nX2-   U-   n[        US-
  5      nUS-	  nXa-   n	U	S:  a  X	-  n
OX	* -	  n
[        X5      u  pX:  a  X|-
  nOUnXU-   S-
  -	  (       a  [        U5      nX-	  n
Xb-
  nO"US-  nMv  X2* -  nX-   n	U	S:  a  X	-  n
OX	* -	  n
SnXU4$ )Nr   r   r?   rF   )r   rh  rI   )r   r   r  rX   r   cancellation_precrj  pi2pi4rc  r   r   r   smalls                 rN   mod_pi2ru    s    
Qw "aH00E57#C(C[F{MG$!>DAwC#FH[FA) , 	tQ;A A8OrQ   c                    U u  pVpxU(       d?  U(       a  [         [         pO[        [        pUS:X  a  X4$ US:X  a  U	$ US:X  a  U
$ US:X  a  U
$ X-   nUS-   nUS:  an  X* :  ah  U(       a  [        U [	        U5      5      n [        [        SX5      n	[        U SU-
  X5      n
US:X  a  X4$ US:X  a  U	$ US:X  a  U
$ US:X  a  [        XX5      $ U(       a  US:  as  US:X  a%  [        n	[        [        4[        US-  5      U-     n
OUS:X  a  [        [        pO[        [        pUS:X  a  X4$ US:X  a  U	$ US:X  a  U
$ US:X  a  [        XX5      $ Xg* S-
  -	  S-   S-	  nXmU* S-
  -  -
  n[        U5      U-   nUS-   U-
  nX|-   nUS:  a  Xo-  nOXo* -	  nU[        U5      -  U-	  nO[        XgX5      u  np[        UU5      u  pUS-  nUS:X  a  U
* U	pOUS:X  a  U	* U
* pO	US:X  a  X* pU(       a  U
* n
US:X  a  [        X* X5      n	[        X* X5      n
X4$ US:X  a  [        X* X5      $ US:X  a  [        X* X5      $ US:X  a  [        XX5      $ g)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
r   r   rB   r^   rF   rD   N)r'   r"   r!   r/   r   r7   r#   boolr0   r   r   ru  rd  r   r   )r   rJ   rW   whichpir   r   r   r   r   rw   r  rX   r   mag2rc  r   re   s                     rN   mpf_cos_sinr{    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	"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-- rQ   c                     [        XUS5      $ Nr   r{  r   rJ   rW   s      rN   mpf_cosr  b      [#q-I&IrQ   c                     [        XUS5      $ )NrB   r~  r  s      rN   mpf_sinr  c  r  rQ   c                     [        XUS5      $ )Nr^   r~  r  s      rN   mpf_tanr  d  r  rQ   c                     [        XUSS5      $ )Nr   r   r~  r  s      rN   mpf_cos_sin_pir  e  s    KaQR4S-SrQ   c                     [        XUSS5      $ r}  r~  r  s      rN   rC  rC  f      AS!Q0O)OrQ   c                     [        XUSS5      $ )NrB   r   r~  r  s      rN   
mpf_sin_pir  g  r  rQ   c                      [        XU5      S   $ Nr   ro  r  s      rN   mpf_coshr  h      mAS.I!.L'LrQ   c                      [        XU5      S   $ r}  r  r  s      rN   mpf_sinhr  i  r  rQ   c                     [        XUSS9$ )Nr   )rn  r  r  s      rN   mpf_tanhr  j  s    mASq.Q'QrQ   c                     Uc  [        US-
  5      n[        X5      u  p4[        U5      n[        XA5      u  pVUS-  nUS:X  a  XV4$ US:X  a  U* U4$ US:X  a  U* U* 4$ US:X  a  Xe* 4$ g )Nr   r^   r   rB   )r   rh  rI   rd  )r   rJ   rr  r   r   r   rw   re   s           rN   r  r  o  s    
{tAv!>DAAAA$DA	AAAvad{Avqb!e|Avqb1"f}Avae|vrQ   c                     Uc  [        U5      n[        X5      u  p4[        U5      n[        XA5      nUS:  a  XS-  $ XS* -	  $ r  )r   rh  rI   rV  )r   rJ   r   r   r   rY   s         rN   	exp_fixedr  {  sF    
{o!>DAAAQAAvvRyrQ   sagez)Warning: Sage imports in libelefun failed)F)FNr   )r   )rS   rn   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/   r0   r1   r2   r3   r4   r5   r6   r7   r8   
libintmathr9   rU  rI  r\  r]  r^  r   r   r  r   r   r  r&  r  r  r   r   r
  rT   r\   r_   rs   rx   r   r   r   r   r   r   r   r   r   r   r   r   rB  r   rg  
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*  r7  r9  r<  r>  r@  rE  rS  rV  rZ  rd  r   ro  ru  r{  r  r  r  r  rC  r  r  r  r  r  r  sage.libs.mpmath.ext_libmplibsmpmath	ext_libmp_lbmpImportErrorAttributeErrorr    rQ   rN   <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=K? ?LL