
    wgݯ              
          d Z dZddlZddlmZ ddlZda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 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! edk(  rd	 Z"nd
 Z"d Z# G d de$      Z%	 e&  e&d      Z( e&d      Z) e&d      Z* e&d      Z+ e&d      Z,e,Z-d Z.d Z/d Z0de	ddfZ1de	ddfZ2de
ddfZ3de
ddfZ4de
ddfZ5deddfZ6de
ddfZ7de	ddfZ8de	ddfZ9de	ddfZ:dZ;d Z< G d  d!      Z=dg e>dd"      D  cg c]  } e
| dz
  z  dz
   c} z   Z? e=       e?gZ@e)d#e*d$e,d%e+d&iZAd' ZBd( ZC	 eDeEfZFd) ZGd* ZHed+k(  r"d, eIe      v rej                  ZBej                  ZCedk(  rej                  xZBZCereGZKeHZLneBZKeCZLde-fd-ZM eNd.  e>d/d0      D              ZOed+k(  rd1 eIe      v rej                  ZMedk(  rej                  ZMde-fd2ZQd3 ZRdhd4ZSd5 ZTde-fd6ZUde-fd7ZVde-fd8ZWde-fd9ZXd:e-fd;ZYd<e-fd=ZZde-fd>Z[d?e-fd@Z\e-fdAZ]dB Z^dC Z_dD Z`dE ZadF ZbdG ZcdH ZddI ZedJ ZfdK ZgdL Zhde-fdMZide-fdNZjde-fdOZkdP Zlde-dfdQZmde-fdRZnde-d?fdSZode-fdTZpe-fdUZqde-fdVZre-fdWZsed+k(  repZteqZunerZtesZudX ZvdY Zwe-fdZZxe-fd[Zye-fd\Zze,e+e+e,e)e*e*e)e(e(iZ{e,e,e+e+e)e*e*e)e(e(iZ|e-fd]Z}d^ Z~d_ Z	 	 did`ZdjdaZe9e9e:e8dbZe-fdcZdd Zde Ze-fdfZe-fdgZedk(  rK	 ddlmc mc mZ ej                  Zmej                  Znej                  Ztej                  Zxej                  Zyy# e'$ r d Z&Y w xY wc c} w # e'$ r eDfZFY w xY w# e$ r Y yw xY w)kzH
Low-level functions for arbitrary-precision floating-point arithmetic.
	plaintext    N)bisect   )MPZMPZ_TYPEMPZ_ZEROMPZ_ONEMPZ_TWOMPZ_FIVEBACKENDSTRICTHASH_MODULUS	HASH_BITSgmpysage
sage_utils)giant_steps
trailtablebctablelshiftrshiftbitcounttrailing
sqrt_fixednumeralisqrt
isqrt_fastsqrtrembin_to_radixr   c                 .    | \  }}}}|t        |      ||fS Nhexxsignmanexpbcs        X/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/mpmath/libmp/libmpf.pyto_pickabler+      s#    c3SXsB&&    c                 4    | \  }}}}|t        |      dd  ||fS )N   r"   r$   s        r*   r+   r+   #   s)    c3SXab\3**r,   c                 0    | \  }}}}|t        |d      ||fS )N   )r   r$   s        r*   from_pickabler1   '   s%    D#sB#c2,R((r,   c                       e Zd Zy)ComplexResultN)__name__
__module____qualname__ r,   r*   r3   r3   +   s    r,   r3   c                     | S r!   r7   )r%   s    r*   <lambda>r9   1   s    q r,   nfcudc           
      \    t        dt        t        t        |       dz        dz
              S )zZReturn number of accurate decimals that can be represented
    with a precision of n bits.r   ry	O
@maxintroundr:   s    r*   prec_to_dpsrF   ;   s*     q#eCF#556q89::r,   c           
      \    t        dt        t        t        |       dz   dz                    S )zJReturn the number of bits required to represent n decimals
    accurately.r   r@   rA   rE   s    r*   dps_to_precrH   @   s)     q#eSVAX'99:;<<r,   c                 .    t        |       }|dk(  ry|dz   S )zReturn the number of decimal digits required to represent
    a number with n-bit precision so that it can be uniquely
    reconstructed from the representation.         )rF   )r:   dpss     r*   repr_dpsrN   E   s      a.C
by7Nr,   rL   ii8ig      c                 D   |t         k(  rE| dk\  r1| |dz
  z	  }|dz  r|dz  s| t        |dk     |   z  r|dz	  dz   S |dz	  S t        |  ||       S |t        k(  r| |z	  S |t        k(  r|  |z	   S |t
        k(  r| dk\  r| |z	  S |  |z	   S |t        k(  r| dk\  r|  |z	   S | |z	  S y )Nr   r   r.   ,  )round_nearesth_mask	round_intround_floorround_ceiling
round_downround_up)r%   r:   rndts       r*   rV   rV   k   s    
m6ac
A1u1q5a&3-*:&:1ax!tqb!S)))
kAv
m"|
j66M"|
h6bQY<Av r,   c                       e Zd Zd Zy)
h_mask_bigc                      t         |dz
  z  dz
  S )Nr   )r	   )selfr:   s     r*   __getitem__zh_mask_big.__getitem__   s    !A#!!r,   N)r4   r5   r6   ra   r7   r,   r*   r^   r^      s    "r,   r^   rS   )r   r   )r   r   )r   r   )r   r   c                    |st         S ||z
  }|dkD  r\|t        k(  r3||dz
  z	  }|dz  r |dz  s|t        |dk     |   z  r	|dz	  dz   }n|dz	  }nt        |   |    r||z  }n| |z	   }||z  }|}|dz  sUt        t        |dz           }|s/|dz  s|dz  }|dz  }|dz  }|dz  st        t        |dz           }||z  }||z  }||z  }|dk(  rd}| |||fS )a  
    Create a raw mpf tuple with value (-1)**sign * man * 2**exp and
    normalized mantissa. The mantissa is rounded in the specified
    direction if its size exceeds the precision. Trailing zero bits
    are also stripped from the mantissa to ensure that the
    representation is canonical.

    Conditions on the input:
    * The input must represent a regular (finite) number
    * The sign bit must be 0 or 1
    * The mantissa must be positive
    * The exponent must be an integer
    * The bitcount must be exact

    If these conditions are not met, use from_man_exp, mpf_pos, or any
    of the conversion functions to create normalized raw mpf tuples.
    r   r   r.   rS         fzerorT   rU   shifts_downr   rC   r&   r'   r(   r)   precr[   r:   r\   s           r*   
_normalizerj      s,   $ 
T	A1u-!A1u1q5cF1S5M!,<&<!tQhdd#AICTAI,Cq7s39~&Ci	qa Ci 3sSy>*A	q
a
 axc2r,   c                    |st         S ||k  r| |||fS ||z
  }|t        k(  r3||dz
  z	  }|dz  r |dz  s|t        |dk     |   z  r	|dz	  dz   }n|dz	  }nt        |   |    r||z  }n| |z	   }||z  }|}|dz  sUt        t        |dz           }|s/|dz  s|dz  }|dz  }|dz  }|dz  st        t        |dz           }||z  }||z  }||z  }|dk(  rd}| |||fS )zSsame as normalize, but with the added condition that
       man is odd or zero
    r   r.   rS   rc   rd   re   rh   s           r*   _normalize1rl      s;    	TzS#r!!
T	A
mAaCLq5q1u#qua(8"8a4(CQ$C	S	$			l1HC	B7s39~&Ci	qa Ci 3sSy>*A	q
a
 axc2r,   c                     t        |      t        k(  sJ t        |      t        v sJ t        |      t        v sJ |t        |      k(  sJ t	        | |||||      S )zAdditional checks on the components of an mpf. Enable tests by setting
       the environment variable MPMATH_STRICT to Y.)typer   
_exp_typesr   rj   r&   r'   r(   r)   ri   r[   s         r*   strict_normalizerr      sa     9   8z!!!9
"""#dCb$44r,   c                     t        |      t        k(  sJ t        |      t        v sJ t        |      t        v sJ |t        |      k(  sJ |r|dz  sJ t	        | |||||      S )rn   r   )ro   r   rp   r   rl   rq   s         r*   strict_normalize1rt     sp     9   8z!!!9
"""#q!!tS#r455r,   r   _mpmath_normalizec                    t        |       } d}| dk  rd}|  } | dk  rt        t        |          }nt        |       }|s|| st        S | dz  si| dz  r|| dz	  |dz   |dz
  fS t
        t        | dz           }|s/| dz  s| dz  } |dz  }|dz  }| dz  st
        t        | dz           }| |z  } ||z  }||z  }|| ||fS t        || ||||      S )zCreate raw mpf from (man, exp) pair. The mantissa may be signed.
    If no precision is specified, the mantissa is stored exactly.r   r      r.   rc   rd   )r   r   rC   r   rf   r   	normalize)r'   r(   ri   r[   r&   r)   r\   s          r*   from_man_expry   #  s
    c(CD
Qwd
TzSXc]LQwQwcQhaa883sSy>*A)AIC1HC!GB ) s39~.AIC1HC!GBc3##T3Rs33r,   c              #   8   K   | ]  }|t        |d       f  yw)r   N)ry   ).0r:   s     r*   	<genexpr>r|   B  s     BQ!\!Q'(Bs   ii  _mpmath_createc                 D    |s| t         v r	t         |    S t        | d||      S )zcCreate a raw mpf from an integer. If no precision is specified,
    the mantissa is stored exactly.r   )	int_cachery   )r:   ri   r[   s      r*   from_intr   J  s)     	>Q<1dC((r,   c                 <    | \  }}}}|s|rt        d|z        ||fS )z:Return (man, exp) of a raw mpf. Raise an error if inf/nan.z*mantissa and exponent are undefined for %s
ValueErrorsr&   r'   r(   r)   s        r*   
to_man_expr   R  s/    D#sBSEKLL8Or,   c                     | \  }}}}|s|rt        d      |dk\  r|r| |z  S ||z  S |s|r|| z	   S || z	  S |rt        | | |      S t        || |      S )zConvert a raw mpf to the nearest int. Rounding is done down by
    default (same as int(float) in Python), but can be changed. If the
    input is inf/nan, an exception is raised.z cannot convert inf or nan to intr   )r   rV   )r   r[   r&   r'   r(   r)   s         r*   to_intr   Y  s     D#sBS;<<
axDS= czcT]##C4= #tS))sdC((r,   c                 *   | \  }}}}|s|r| S |dk\  r| S ||z   }|dk  r_|t         k(  r|rt        S t        S |t        k(  r|rt        S t        S |t
        k(  r"|dk  s	|t        k(  rt        S |rt        S t        S t        t        | t        ||      |      S Nr   r   )
rX   rf   fonerW   fnonerT   r	   NotImplementedErrormpf_posmin)r   r[   r&   r'   r(   r)   mags          r*   mpf_round_intr   o  s    D#sBS
ax
b&C
Qw-E\ [KE\!\M!Qw#.,e|"{%%1c"clC((r,   c                 D    t        | t              }|rt        |||      }|S r!   )r   rW   r   r   ri   r[   vs       r*   	mpf_floorr     s$    a%AAtS!Hr,   c                 D    t        | t              }|rt        |||      }|S r!   )r   rX   r   r   s       r*   mpf_ceilr     $    a'AAtS!Hr,   c                 D    t        | t              }|rt        |||      }|S r!   )r   rT   r   r   s       r*   mpf_nintr     r   r,   c                 0    t        | t        |       ||      S r!   )mpf_subr   )r   ri   r[   s      r*   mpf_fracr     s    1ilD#..r,   5   c                 "   | | k7  rt         S 	 t        j                  |       \  }}| t        k(  rt        S | t         k(  rt
        S t        t        |dz        |dz
  ||      S #  | t        k(  rt        cY S | t         k(  rt
        cY S t         cY S xY w)zCreate a raw mpf from a Python float, rounding if necessary.
    If prec >= 53, the result is guaranteed to represent exactly the
    same number as the input. If prec is not specified, use prec=53.l          r   )fnanmathfrexpmath_float_inffinffninfry   rC   )r%   ri   r[   mes        r*   
from_floatr     s    
 	Avzz!}1
 	N4K^OE\AuI"dC88ts   A! !B4BBq   c                 \   t        |       }| |k(  rt        |||      S ddl}|j                  |       rG|j	                  |       \  }}t        t        |j                  |d            t        |dz
        ||      S |j                  |       rt        S |j                  |       rt        S t        S )zCreate a raw mpf from a numpy float, rounding if necessary.
    If prec >= 113, the result is guaranteed to represent exactly the
    same number as the input. If prec is not specified, use prec=113.r   Nr   )floatr   numpyisfiniter   ry   rC   ldexpisposinfr   isneginfr   r   )r%   ri   r[   ynpr   r   s          r*   from_npfloatr     s     	aAAv!T3''	{{1~xx{1CC 013qu:tSII	{{1~d{	{{1~e|Kr,   c                    | j                         rt        S | j                         r| j                         rt        S t
        S |(t        t        | j                         d         dz        }t        t        |       ||      S )zCreate a raw mpf from a Decimal, rounding if necessary.
    If prec is not specified, use the equivalent bit precision
    of the number of significant digits in x.r   r@   )is_nanr   is_infinite	is_signedr   r   rC   lenas_tuplefrom_strstr)r%   ri   r[   s      r*   from_Decimalr     sc     	xxz$;}}u?4?|3qzz|A'(::;CFD#&&r,   Fc                 F   | \  }}}}|s6| t         k(  ry| t        k(  rt        S | t        k(  rt         S t        t        z  S |dkD  rt	        ||||d|      \  }}}}|r| }	 t        j                  ||      S # t        $ r! |r ||z   dkD  r|r	t         cY S t        cY S Y yw xY w)a  
    Convert a raw mpf to a Python float. The result is exact if the
    bitcount of s is <= 53 and no underflow/overflow occurs.

    If the number is too large or too small to represent as a regular
    float, it will be converted to inf or 0.0. Setting strict=True
    forces an OverflowError to be raised instead.

    Warning: with a directed rounding mode, the correct nearest representable
    floating-point number in the specified direction might not be computed
    in case of overflow or (gradual) underflow.
    g        r   r   )rf   r   r   r   
normalize1r   r   OverflowError)r   strictr[   r&   r'   r(   r)   s          r*   to_floatr     s     D#sB:c9^+:~o-n,,	Bw'c3BDc3dzz#s## 
8a<&&%%
s    A6 6B B B c                 B    t        t        |       t        |      ||      S )zDCreate a raw mpf from a rational number p/q, round if
    necessary.)mpf_divr   )pqri   r[   s       r*   from_rationalr     s     8A;T377r,   c                 n    | \  }}}}|r| }|dk(  rt        d|z        |dk\  r
|d|z  z  dfS |d| z  fS )z]Convert a raw mpf to a rational number. Return integers (p, q)
    such that s = p/q exactly.rO   z&cannot convert %s to a rational numberr   r   r   r   s        r*   to_rationalr     s^     D#sBd	RxACGHH
axaf~q  AI~r,   c                 b    | \  }}}}||z   }|r|dk\  r| |z  S | | z	  S |dk\  r||z  S || z	  S )z.Convert a raw mpf to a fixed-point big integerr   r7   )r   ri   r&   r'   r(   r)   offsets          r*   to_fixedr     s[    D#sB4ZFQ;//!$6'22Q;sf},"w//r,   c                 f    t         sddl}|j                   a t        t        |       |  | t              S )zQReturn a raw mpf chosen randomly from [0, 1), with prec bits
    in the mantissa.r   N)getrandbitsrandomry   rW   )ri   r   s     r*   mpf_randr     s-     ((D)D5$DDr,   c                 F    | d   r|d   s| t         k(  s	|t         k(  ry| |k(  S )zTest equality of two raw mpfs. This is simply tuple comparison
    unless either number is nan, in which case the result is False.r   F)r   r   r\   s     r*   mpf_eqr     s*     Q4qt9T	6Mr,   c                    t         j                  dk\  r| \  }}}}|sj| t        k(  rt         j                  j                  S | t
        k(  rt         j                  j                  S | t        k(  rt         j                  j                   S |t        z  }|dk\  r
|t        z  }nt        dz
  d|z
  t        z  z
  }||z  t        z  }|r| }|dk(  rd}t        |      S 	 t        t        | d            S # t        $ r t        |       cY S w xY w)N)rL   r.   r   r   rO   rP   )r   )sysversion_infor   	hash_infonanr   infr   r   r   rC   hashr   r   )r   ssignsmansexpsbchs         r*   mpf_hashr   !  s    
6!!"tT3 Dy!2!22Dy!2!22Ez3==#4#4"44<19)#Dq=R$Y)$;<D$Y,&qb!7A1v	1-.. 	 7N		s   C/ /DDc                 ~   | \  }}}}|\  }}}}	|r|sN| t         k(  rt        |       S |t         k(  rt        |       S | |k(  ry|t        k(  ry| t        k(  ry|t        k(  ryy||k7  r|syy||k(  r||k(  ry||kD  r|ryy|ryy||z   }
|	|z   }|r|
|k  ry|
|kD  ry|
|k  ry|
|kD  ryt        | |dt              }|d   ryy)zCompare the raw mpfs s and t. Return -1 if s < t, 0 if s == t,
    and 1 if s > t. (Same convention as Python's cmp() function.)r   r   rO      )rf   mpf_signr   r   r   r   rW   )r   r\   r   r   r   r   tsigntmantexptbcabdeltas                r*   mpf_cmpr   >  s    E4sE4s t:x{l*:hqk)6!9Q9Q:a~Qt|4<$;RQ 	d
Ad
Aq5q5q5q5 Aq![)EQxr,   c                 F    | t         k(  s	|t         k(  ryt        | |      dk  S NFr   r   r   r   s     r*   mpf_ltr   r  #    DyAI1a=1r,   c                 F    | t         k(  s	|t         k(  ryt        | |      dk  S r   r   r   s     r*   mpf_ler   w  #    DyAI1a=Ar,   c                 F    | t         k(  s	|t         k(  ryt        | |      dkD  S r   r   r   s     r*   mpf_gtr   |  r   r,   c                 F    | t         k(  s	|t         k(  ryt        | |      dk\  S r   r   r   s     r*   mpf_ger     r   r,   c                 f    | d   x}}| dd  D ]  }t        ||      r|}t        ||      s|}! ||fS r   )r   r   )seqr   rB   r%   s       r*   mpf_min_maxr     sJ    AC#W #!S>3!S>3# 8Or,   c                 D    |r| \  }}}}|s|r| S t        ||||||      S | S )zPCalculate 0+s for a raw mpf (i.e., just round s to the specified
    precision).)r   r   ri   r[   r&   r'   r(   r)   s          r*   r   r     s9     c3H$S"dC88Hr,   c                     | \  }}}}|s"|r| t         k(  rt        S | t        k(  rt         S | S |s	d|z
  |||fS t        d|z
  |||||      S )zNegate a raw mpf (return -s), rounding the result to the
    specified precision. The prec argument can be omitted to do the
    operation exactly.r   )r   r   r   r   s          r*   mpf_negr     sb     D#sBDy,Ez$;$S"%%afc3D#66r,   c                 r    | \  }}}}|s|r| t         k(  rt        S | S |s
|rd|||fS | S t        d|||||      S )zReturn abs(s) of the raw mpf s, rounded to the specified
    precision. The prec argument can be omitted to generate an
    exact result.r   )r   r   r   r   s          r*   mpf_absr     sW     D#sBS:KsC$$ac2tS11r,   c                 H    | \  }}}}|s| t         k(  ry| t        k(  ryyd|z  S )zReturn -1, 0, or 1 (as a Python int, not a raw mpf) depending on
    whether s is negative, zero, or positive. (Nan is taken to give 0.)r   rO   r   )r   r   r   s        r*   r   r     s4     D#sB9Q:b4<r,   c                    | \  }}}}|\  }	}
}}|	|z  }	|r|
r||z
  }|rG|dkD  r|dkD  rK|rI||z   |z
  |z
  }||dz   kD  r6|dz   }||z  }|	|k(  r|dz  }n|dz  }t        ||||z
  t        |      ||      S ||	k(  r	|
||z  z   }n |r	|
||z  z
  }n||z  |
z
  }|dk\  rd}n| }d}t        |      }t        |||||xs ||      S |dk  r|dk  rK|rI||z   |z
  |z
  }||dz   kD  r6|dz   }|
|z  }
||	k(  r|
dz  }
n|
dz  }
t        |	|
||z
  t        |
      ||      S ||	k(  r
||
| z  z   }n"|	r
||
| z  z
  }n	|
| z  |z
  }|dk\  rd}n| }d}t        |      }t        |||||xs ||      S ||	k(  r|
|z   }n|r|
|z
  }n||
z
  }|dk\  rd}n| }d}t        |      }t        |||||xs ||      S |rt        |      }|s+|r| |k(  s|
s|s| S t        S |
rt        |	|
|||xs ||      S |S |r|S |rt        |||||xs ||      S | S )z
    Add the two raw mpf values s and t.

    With prec=0, no rounding is performed. Note that this can
    produce a very large mantissa (potentially too large to fit
    in memory) if exponents are far apart.
    r   d      r   i)r   r   rx   r   r   )r   r\   ri   r[   _subr   r   r   r   r   r   r   r   r   r   r'   r)   s                    r*   mpf_addr    s    E4sE4s	TMEzC<D$J,t3Etax'!% E>4194+/194)%tF{$TND# 7 7 E>$&.1C DDFN$;c%)V^t$;cax !"d !c]!%dB
CHH!D=T$J,t3Etax'!% E>4194+/194)%tF{$TND# 7 7 E>$6'/2C DDVGO$<c%)fW_$<cax !"d !c]!%dB
CHHE>+CD4Kc 4Kcaxdc]T2tzr3??AJAvTKeT4dkc3GG%tS$+#sCCHr,   c                      t        | |||d      S )zxReturn the difference of two raw mpfs, s-t. This function is
    simply a wrapper of mpf_add that changes the sign of t.r   )r  )r   r\   ri   r[   s       r*   r   r     s     1asA&&r,   c                 j   d}d}|dz  xs d}d}| D ]  }|\  }	}
}}|
r^|	r|s|
 }
||z
  }||k\  r/||kD  r!|r|t        t        |            z
  |kD  r|
}|}C||
|z  z  }L| }||z
  |kD  r|rZ|
|}}_||z  |
z   }|}j|sm|rt        |      }t        |xs t        |d      } |r|S t        ||||      S )a>  
    Sum a list of mpf values efficiently and accurately
    (typically no temporary roundoff occurs). If prec=0,
    the final result will not be rounded either.

    There may be roundoff error or cancellation if extremely
    large exponent differences occur.

    With absolute=True, sums the absolute values.
    r   r.   i@B Nr   )r   absr   r  rf   ry   )xsri   r[   absoluter'   r(   max_extra_precspecialr%   xsignxmanxexpxbcr   s                 r*   mpf_sumr  "  s    C
C!V&wNG 6!"tT3Xu3JEs{ N*%S(:":^"KCCDEM*C9~-#'S%<4/CCAJg.15G76: S$,,r,   c                 P   | \  }}}}|\  }}	}
}||z  }||	z  }|r)t        |      }|rt        ||||
z   |||      S ||||
z   |fS | xr |}|	 xr |
}|s|st        S t        | |fv rt        S |	s|
r|| }} |t        k(  rt        S t        t
        dt        |       t        |      z     S )Multiply two raw mpfsr   rO   )r   r   rf   r   r   r   r   r   r\   ri   r[   r   r   r   r   r   r   r   r   r&   r'   r)   	s_special	t_specials                    r*   gmpy_mpf_mulr  R  s    E4sE4s5=D
t)C
c]dCdBcBB#tDy"--#tI#tIY1v~d{d1aqAEz$;uhqkHQK788r,   c                     | \  }}}}|st        | t        |      ||      S |st        S |dk  r|dz  }| }||z  }t        |||t	        |      ||      S )Multiply by a Python integer.r   r   )mpf_mulr   rf   rx   r   r   r:   ri   r[   r&   r'   r(   r)   s           r*   gmpy_mpf_mul_intr  g  si    D#sBq(1+tS111u	B1HCT3Xc]D#>>r,   c                 l   | \  }}}}|\  }}	}
}||z  }||	z  }|r7||z   dz
  }|t        ||z	        z  }|rt        ||||
z   |||      S ||||
z   |fS | xr |}|	 xr |
}|s|st        S t        | |fv rt        S |	s|
r|| }} |t        k(  rt        S t        t
        dt        |       t        |      z     S )r  r   r  )rC   r   rf   r   r   r   r   r  s                    r*   python_mpf_mulr  t  s    E4sE4s5=D
t)C
3Y]
c#r'ldCdBcBB#tDy"--#tI#tIY1v~d{d1aqAEz$;uhqkHQK788r,   c                    | \  }}}}|st        | t        |      ||      S |st        S |dk  r|dz  }| }||z  }|dk  r|t        t	        |         dz
  z  }n|t        |      dz
  z  }|t	        ||z	        z  }t        ||||||      S )r  r   r   rw   )r  r   rf   r   rC   r   rx   r  s           r*   python_mpf_mul_intr!    s    D#sBq(1+tS111u	B1HC4x
gc!fo!!
hqkAo#c2g,BT3Rs33r,   c                 *    | \  }}}}|s| S ||||z   |fS )z8Quickly multiply the raw mpf s by 2**n without rounding.r7   )r   r:   r&   r'   r(   r)   s         r*   	mpf_shiftr#    s+    D#sBc!eRr,   c                 l    | \  }}}}|s| t         k(  rt         dfS t        t        | | |z
        ||z   fS )z?Convert x = y*2**n to (y, n) with abs(y) in [0.5, 1) if nonzeror   )rf   r   r#  r$   s        r*   	mpf_frexpr%    sF    D#sB:1:QC "S&((r,   c                    | \  }}}}|\  }}	}
}|r|	s| t         k(  r$|t         k(  rt        |t        k(  rt        S t         S |t         k(  rt        | xr |}|	 xr |
}|r|rt        S | t        k(  s	|t        k(  rt        S |s4|t         k(  rt        S t        t        dt        |       t        |      z     S t         S ||z  }|	dk(  rt        ||||
z
  |||      S ||z
  |z   dz   }|dk  rd}t        ||z  |	      \  }}|r,|dz  dz   }|dz  }t        ||||
z
  |z
  t        |      ||      S t        ||||
z
  |z
  t        |      ||      S )zFloating-point divisionr  r   r   )
rf   ZeroDivisionErrorr   r   r   r   r   divmodr   rx   )r   r\   ri   r[   r   r   r   r   r   r   r   r   r  r  r&   extraquotrems                     r*   r   r     sr   E4sE4st:Ez!22Dy+L:##X'4	X'4	K9T	KEzu%hqkHQK&?@@5=Dqy$d4idC@@ 3Jq EqytU{D)ID#
a1}
$d4iox~tSQQT4d5(4.$LLr,   c                 &   |\  }}}}| r|st        t        |       |||      S | dk  r|dz  }|  } ||z   dz   }t        | |z  |      \  }	}
|
r*|	dz  dz   }	|dz  }t        ||	| |z
  t	        |	      ||      S t        ||	| |z
  t	        |	      ||      S )z>Floating-point division n/t with a Python integer as numeratorr   r   r   )r   r   r(  r   r   rx   )r:   r\   ri   r[   r&   r'   r(   r)   r)  r*  r+  s              r*   mpf_rdiv_intr-    s    D#sBCx{AtS111u	B2IMEq%x%ID#
a1}
$sd5j(4.$LLT4#eXd^T3GGr,   c                 $   | \  }}}}|\  }}	}
}|s|s|	s|
rt         S ||k(  r
|
||z   kD  r| S |	dk(  r||
|z   kD  rt        S t        ||
      }d|z  |z  }d|z  |	z  }	|||z
  z  |	|
|z
  z  z  }|dk\  rd}n| }d}t        |||t	        |      ||      S )Nr   rO   r   )r   rf   r   rx   r   )r   r\   ri   r[   r   r   r   r   r   r   r   r   baser'   r&   s                  r*   mpf_modr0    s    E4sE4std~$c/ qyTDH_tT?D;D;DDI4DI#6
7C
axdT3hsmT3??r,   c                    | \  }}}}|sZ|rX| t         k(  r|dkD  r| S |dk(  rt        S t        S | t        k(  r(|dkD  rt         t        g|dz     S |dk(  rt        S t        S t        S t	        |      }|dk(  rt
        S |dk(  rt        | ||      S |dk(  rY| \  }}}}|st        S ||z  }|dk(  rdt        ||z   dfS ||z   dz
  }|t        t	        ||z	           z  }t        d|||z   |||      S |dk(  rt        t
        | ||      S |dk  r+t        | | |dz   t        |         }	t        t
        |	||      S ||z  }
|dk(  r|
t        ||z  dfS ||z  dk  r!||z  }t        |
|||z  t        |      ||      S |t        k(  xs t        |   |
   }|dt        |      z  z   dz   }t
        \  }}}}	 |dz  rV||z  }||z   }||dz
  z  }|t        t	        ||z	           z   }||kD  r|r	|||z
  z	  }n
| ||z
  z	   }|||z
  z  }|}|dz  }|snT||z  }||z   }||z   dz
  }|t        t	        ||z	           z   }||kD  r|r	|||z
  z	  }n
| ||z
  z	   }|||z
  z  }|}|dz  }t!        |
|||||      S )z=Compute s**n, where s is a raw mpf and n is a Python integer.r   r   r.   rO   r   i  r  )r   r   rf   r   rC   r   r   r	   r   r   r   mpf_pow_intreciprocal_rndr   rT   rg   rx   )r   r:   ri   r[   r&   r'   r(   r)   _inverseresult_signrounds_downworkprecpmpepbcs                   r*   r2  r2    s   D#sBS91uQhAvd{L:1udE]1q511Avd{LAAAvd{Avgas++Av3RL#g!8wC++"Wq[
gc#r'l##!S#c'2tS99BwwtQc221ua!T!V^C-@AtWdC00(K axWc!eQ//	!td{	+sCE8C=$LL -' &C%  am#a'HNAr2s
q5CBCB26MCB#I//CX~H-BCS\23BcHn$FA#g#g"Wq['#cRi.))=bk*2h;/02= CBF7 : ["b#tS99r,   c                     |t         k(  rt        | ||      S | \  }}}}|t        ||z   |z
  dz
  df}|r|t        t        fv |z  }	n|t
        t        fv |z  }	|	rt        | |||      S t        | ||      S )a  
    For nonzero x, calculate x + eps with directed rounding, where
    eps < prec relatively and eps has the given sign (0 for
    positive, 1 for negative).

    With rounding to nearest, this is taken to simply normalize
    x to the given precision.
    r   )rT   r   r	   rY   rX   rZ   r  )
r%   eps_signri   r[   r&   r'   r(   r)   epsaways
             r*   mpf_perturbr@  f  s     mq$$$D#sBWc"fTk!mQ
/C
M22h>-00H<q#tS))q$$$r,   c                    | d   rd}t        |       } nd}| \  }}}}|syt        |t        j                  dd      z        dz   }||z   }t	        |      dkD  rdd	lm}	m}
 t        t	        |            d
z   }t        |      }t        | |	|            }t        | |
|      |      }t        |      }t        | t        t        ||      |      } | \  }}}}|}nd}t        ||z
  |z
  d      }t        |t        j                  dd      z  dz         }t!        | |      }t#        ||d|      }t%        |d|      }|t'        |      |z
  dz
  z  }|||fS )a0  Helper function for representing the floating-point number s as
    a decimal with dps digits. Returns (sign, string, exponent) where
    sign is '' or '-', string is the digit string, and exponent is
    the decimal exponent as an int.

    If inexact, the decimal representation is rounded toward zero.r   - )rC  0r   
   r.   i  r   )mpf_ln2mpf_ln10r   g      ?)r/  size)r   rC   r   logr  	libelefunrF  rG  r   r   r  r   r   r2  ftenrB   r   r   r   r   )r   rM   r&   _signr'   r(   r)   bitprec
exp_from_1rF  rG  expprectmpr   exponentfixprecfixdpssfsddigitss                       r*   to_digits_exprW    sh    	tAJE3R#A&'",G rJ
:0 3s8$q(smc77+,c8G,g63KA{4G4g>sC
 'C-"$a(G488Bq>)C/0F	!W	B	b'2v	.BRbs+FFf$q((H!!r,   c                 j   | d   s=| t         k(  r|rd}nd}|r|dz  }|S | t        k(  ry| t        k(  ry| t        k(  ryt        |t        |d	z   d
      }||}t        | |d	z         \  }}}	|s|d   dv r|	dz  }	d}nt        |      |kD  rt||   dv rm|d| }|dz
  }
|
dk\  r||
   dk(  r|
dz  }
|
dk\  r	||
   dk(  r|
dk\  r-|d|
 t        t        ||
         dz         z   d||
z
  dz
  z  z   }ndd|dz
  z  z   }|	dz  }	n|d| }||	cxk  r|k  r5n n2|	dk  rdt        |	       z  |z   }d}n|	dz   }||kD  r|d||z
  z  z  }d}	nd}|d| dz   ||d z   }|r|j                  d      }|d   dk(  r|dz  }|	dk(  r	|r|s||z   S |	dk\  r||z   dz   t        |	      z   S |	dk  r||z   dz   t        |	      z   S y)a{  
    Convert a raw mpf to a decimal floating-point literal with at
    most `dps` decimal digits in the mantissa (not counting extra zeros
    that may be inserted for visual purposes).

    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.

    The literal is formatted so that it can be parsed back to a number
    by to_str, float() or Decimal().
    r   z0.0z.0ze+0+inf-infr   NrL   r   567899rD  1.rO   ze+r   )rf   r   r   r   r   r   rW  r   r   rC   rstrip)r   rM   strip_zeros	min_fixed	max_fixedshow_zero_exponentr\   r&   rV  rQ  isplits               r*   to_strrg    sW   ( Q4:AA!U
H9V:f9UcCF)R&8)c) +1c!e4D&( !9MH v;!7DS\FaAq&VAY#-Q q&VAY#-Avc#fQi.1*<&==sQwQR{@SSscAg.ADS\F x+)+!|c8)n,6 13;c59o-FHE%.3&7]]3'FbzS #1}%7v9M1}TF]T1CMAA!|D6MC/#h-??|r,   c                    | j                         j                  d      } t        |        | j                  d      }t	        |      dk(  rd}n|d   } t        |d         }| j                  d      }t	        |      dk(  r,|d   |d   j                  d      }}|t	        |      z  }||z   } t        t        | |            } | |fS )zHelper function for from_str.lr   r   r   r_  r.   rD  )lowerr`  r   rf  r   rC   r   )r%   r/  partsr(   r   r   s         r*   str_to_man_exprl  
  s    		A	!HGGCLE
5zQ!H%(mGGCLE
5zQQxq-1s1vEC4LAc6Mr,   )r   rY  rZ  r   c           	         | j                         j                         } | t        v r	t        |    S d| v rV| j                  d      \  }}|j	                  d      |j	                  d      }}t        t        |      t        |      ||      S t        | d      \  }}t        |      dkD  r1t        ||dz         }t        |t        t        ||dz         ||      }|S |dk\  rt        |d|z  z  ||      }|S t        |d| z  ||      }|S )aV  Create a raw mpf from a decimal literal, rounding in the
    specified direction if the input number cannot be represented
    exactly as a binary floating-point number with the given number of
    bits. The literal syntax accepted is the same as for Python
    floats.

    TODO: the rounding does not work properly for large exponents.
    /ri  rE  r/  i  r   )rj  stripspecial_strrf  r`  r   rC   rl  r  r   r  r2  rK  )r%   ri   r[   r   r   r'   r(   r   s           r*   r   r   !  s    	
	AK1~
axwws|1xx}ahhsm1SVSVT377ab)HC 3x#~S$r'"A{4d2g6cB H	 !8r3wc2A H c2t8T37AHr,   c                     t        | d      \  }}t        |      }d}|dk  r| }d}t        |      }t        |||||t              S )Nr.   ro  r   r   )rl  r   r   rx   rW   )r%   r'   r(   r&   r)   s        r*   	from_bstrrs  D  sQ    aa(HC
c(CD
Qwd	#BT3R[99r,   c                 Z    | \  }}}}ddg|   t        |t        |      d      z   d|z  z   S )NrC  rB  r.   )rH  r/  ze%i)r   r   r$   s        r*   to_bstrru  N  s:    D#sBs8D>GChsm!DDPSTTr,   c                 T   | \  }}}}|rt        d      |s| S |dz  r|dz  }|dz  }|dz  }n|dk(  rt        |||dz  |||      S t        dd|z  |z
  dz         }||dz  z  }|dv rt        ||z        }n t	        ||z        \  }}|r|dz  dz   }|dz  }t        |||z
  dz  ||      S )zb
    Compute the square root of a nonnegative mpf value. The
    result is correctly rounded.
    z square root of a negative numberr   r.   r  fd)r3   r   rB   r   r   ry   )	r   ri   r[   r&   r'   r(   r)   shiftr+  s	            r*   mpf_sqrtry  X  s    
 D#sB>??
Qwq	
a	$S!VRs;;1T6"9Q;E	UQYE
d{CJ3:&S61*CQJEc%i!^T377r,   c                     |t         k(  rt        | ||      S | t         k(  rt        |||      S t        t        | |       t        ||      |dz         }t	        |||      S )zICompute the Euclidean norm sqrt(x**2 + y**2) of two raw mpfs
    x and y.r  )rf   r   r  r  ry  )r%   r   ri   r[   hypot2s        r*   	mpf_hypotr|  t  s[     	Ez'!T3//Ez'!T3//WQq\71Q<a8FFD#&&r,   r!   )TNNF)rE  )__doc____docformat__r   r   r   r   backendr   r   r   r	   r
   r   r   r   r   r   r   r   r   
libintmathr   r   r   r   r   r   r   r   r   r   r   r   r   r+   r1   r   r3   intern	NameErrorrT   rW   rX   rZ   rY   
round_fastrF   rH   rN   rf   fnzeror   r   ftworK  fhalfr   r   r   r   rV   r^   rangeh_mask_smallrU   rg   rj   rl   rC   longrp   rr   rt   dirru   rx   r   ry   dictr   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  mpf_mul_intr#  r%  r   r-  r0  r3  negative_rndr2  r@  rW  rg  rl  rq  r   rs  ru  ry  r|  sage.libs.mpmath.ext_libmplibsmpmath	ext_libmpext_libImportError)r4  s   0r*   <module>r     sO      
 F F F F    f'+)	J 	

 sSks#;C[

;
=
 
Ha
Xq!		7Aq	
GQ	7Aq	8Q	
GR 	
8T2	8T2	
HdB 2" " s%3-@Qg!na'@@
,	%
 5-uhu&5n'RtJ56 f,D	9''J((K
f)333J	 I"JIJ !%* 4> B%S/BB	
f)SY6&&L
f**LJ )),), Z  J  J  J / z 9& *  : ' * #J #- 8
	0&E:2h



 :  j 7 j 2 jq ]~ j '
 J .-` : 9* &0 ?  Z 9, (2 4( fG"KG$K ) ' #MJ ", H  ' @0 x
Z+- z
X+- !+ Q:h%62"h @DS@j* $uDA$ F:U % 88 ) ' f44////////## [*  Fp Af  JT$  s=   ,L# 9L2*L7 A	M #L/.L/7MMMM