
    \h`T                         S SK JrJrJrJrJr  S SKJrJrJ	r	  S SK
Jr  S SKJr  S SKJrJrJr  S SKJrJr  S rS\4S	 jrS
 rS rSS jrS r " S S\5      rS rS rSS jr S r!S r"S r#S r$S r%g)    )AddExprMulSsympify)_mexpand	count_ops
expand_mul)default_sort_key)Dummy)rootsignsqrt)PolyPolynomialErrorc                     U R                   =(       aB    U R                  R                  =(       a%    [        U R                  5      [        R
                  L $ )z/Return True if expr is a sqrt, otherwise False.)is_Powexpis_Rationalabsr   Half)exprs    Q/var/www/auris/envauris/lib/python3.13/site-packages/sympy/simplify/sqrtdenest.pyis_sqrtr   	   s3     ;;K488//KCMQVV4KK    returnc                    U [         R                  L a  gU R                  (       a  gU R                  (       d  U R                  (       a  [        S U R                   5       5      $ [        U 5      (       a  [        U R                  5      S-   $ g)a  Return the maximum depth of any square root argument of p.

>>> from sympy.functions.elementary.miscellaneous import sqrt
>>> from sympy.simplify.sqrtdenest import sqrt_depth

Neither of these square roots contains any other square roots
so the depth is 1:

>>> sqrt_depth(1 + sqrt(2)*(1 + sqrt(3)))
1

The sqrt(3) is contained within a square root so the depth is
2:

>>> sqrt_depth(1 + sqrt(2)*sqrt(1 + sqrt(3)))
2
   r   c              3   8   #    U  H  n[        U5      v   M     g 7fN)
sqrt_depth.0xs     r   	<genexpr>sqrt_depth.<locals>.<genexpr>&   s     1&Q:a==&   )
r   ImaginaryUnitis_Atomis_Addis_Mulmaxargsr   r!   baseps    r   r!   r!      s_    $ 	AOOyyxx1881!&&111qzz!&&!A%%r   c                 j   U R                   (       a  gU R                  (       a  g[        U 5      (       d,  U R                  (       a0  U R                  R
                  (       a  [        U R                  5      $ U R                  (       d  U R                  (       a  [        S U R                   5       5      $ g)a  Return True if p is comprised of only Rationals or square roots
of Rationals and algebraic operations.

Examples
========

>>> from sympy.functions.elementary.miscellaneous import sqrt
>>> from sympy.simplify.sqrtdenest import is_algebraic
>>> from sympy import cos
>>> is_algebraic(sqrt(2)*(3/(sqrt(7) + sqrt(5)*sqrt(2))))
True
>>> is_algebraic(sqrt(2)*(3/(sqrt(7) + sqrt(5)*cos(2))))
False
TFc              3   8   #    U  H  n[        U5      v   M     g 7fr    )is_algebraicr"   s     r   r%   is_algebraic.<locals>.<genexpr>C   s     3Fq<??Fr'   )r   r)   r   r   r   
is_Integerr3   r.   r*   r+   allr-   r/   s    r   r3   r3   ,   sg      	}}	
	qxxAEE$4$4AFF##	
QXX3AFF333r   c                 .   U S:X  a  S//nU$ U S:X  a  SS/SS/SS//nU$ U S:X  a  / SQ/ SQ/ SQ/ SQ/ S	Q/ S
Q/ SQ/nU$ [        U S-
  5      nU Vs/ s H  o3S/-   PM
     nnU Vs/ s H  o3S/-   PM
     nnUS/U S-
  -  S/-   /-   U-   nU$ s  snf s  snf )aC  
Returns all possible subsets of the set (0, 1, ..., n-1) except the
empty set, listed in reversed lexicographical order according to binary
representation, so that the case of the fourth root is treated last.

Examples
========

>>> from sympy.simplify.sqrtdenest import _subsets
>>> _subsets(2)
[[1, 0], [0, 1], [1, 1]]

r      r      )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )_subsets)nabr$   a0a1s         r   r:   r:   H   s     	AvSE H 
aVaVaV$ H 
a	9	9i9 H	 QUO !q!1#gq! !q!1#gq!1#q1u+#$$r)H "!s   B&Bc                 l    [        U 5      n [        U5       H  n[        U 5      nX:X  a  U s  $ Un M     U $ )a  Denests sqrts in an expression that contain other square roots
if possible, otherwise returns the expr unchanged. This is based on the
algorithms of [1].

Examples
========

>>> from sympy.simplify.sqrtdenest import sqrtdenest
>>> from sympy import sqrt
>>> sqrtdenest(sqrt(5 + 2 * sqrt(6)))
sqrt(2) + sqrt(3)

See Also
========

sympy.solvers.solvers.unrad

References
==========

.. [1] https://web.archive.org/web/20210806201615/https://researcher.watson.ibm.com/researcher/files/us-fagin/symb85.pdf

.. [2] D. J. Jeffrey and A. D. Rich, 'Symplifying Square Roots of Square Roots
       by Denesting' (available at https://www.cybertester.com/data/denest.pdf)

)r
   range_sqrtdenest0)r   max_iterizs       r   
sqrtdenestrF   e   s<    6 dD8_9K	 
 Kr   c                    SSK Jn  [        U 5      n U R                  (       a#  U [        R
                  [        R
                  4nGOU R                  (       GaC  [        U R                  [        S9nU Vs/ s H  oDS-  PM	     nn[        S U 5       5      (       a  U" U 5      u  pgnXU4n[        U5      $ [        U5       V	Vs/ s H  u  p[        U5      XI4PM     n
n	n[        U
[        S9nUS   S:X  a  / nGOUu  pn	UR                  U	5      nU
R                  U	5        [        R                   nUR"                  (       av  / n/ nUR                   H6  n[        U5      U:  a  UR%                  U5        M%  UR%                  U5        M8     [&        R(                  " U5      n[&        R(                  " U5      n/ nU/nU
 H  nUS   U:  a  UR%                  US   5        M"  US   nUU:X  a  UR%                  S5        M@  UR"                  (       a[  [        UR                  5      nUU;   a*  UR+                  U5        UR%                  ['        U6 5        M  UR%                  US   5        M  UR%                  US   5        M     [-        U6 n[-        U6 nXUS-  4nO:U R/                  5       u  pv[1        U5      (       a  [        R
                  XvS-  4nO/ n[        U5      $ s  snf s  snn	f )a}  Return [a, b, r] for p.match(a + b*sqrt(r)) where, in addition to
matching, sqrt(r) also has then maximal sqrt_depth among addends of p.

Examples
========

>>> from sympy.functions.elementary.miscellaneous import sqrt
>>> from sympy.simplify.sqrtdenest import _sqrt_match
>>> _sqrt_match(1 + sqrt(2) + sqrt(2)*sqrt(3) +  2*sqrt(1+sqrt(5)))
[1 + sqrt(2) + sqrt(6), 2, 1 + sqrt(5)]
r   )split_surdskeyr8   c              3   ^   #    U  H#  oR                   =(       a    UR                  v   M%     g 7fr    )r   is_positive)r#   sqs     r   r%   _sqrt_match.<locals>.<genexpr>   s     B6R~~0"..06s   +-r   )sympy.simplify.radsimprH   r   	is_Numberr   Zeror*   sortedr-   r   r6   list	enumerater!   r,   popOner+   appendr   
_from_argsremover   as_coeff_Mulr   )r0   rH   respargsr$   sqargsrr=   r<   rD   vnmaxdepth_bvrvr?   b1x1x1argss                       r   _sqrt_matchrh      sa    3A{{!&&!&&!	
qvv#34 %&1Q$&B6BBB!!nGA!'C9 09/?@/?tqjmQ"/?@1*+7a<C KEa		!AEE!HAxxA!!}u,		!		!	  
 NN2&NN2&BBQ4%<IIadO1BQw		!99%)"'']F F{ &a 0 "		#v, 7 "		!A$IIadO! " RARAA,C~~1::661d#CC9s ' As   4K K%c                       \ rS rSrSrg)SqrtdenestStopIteration    N)__name__
__module____qualname____firstlineno____static_attributes__rl   r   r   rj   rj      s    r   rj   c                 &   [        U 5      (       a  U R                  5       u  pU[        R                  L a  UR                  R
                  (       a  [        UR                  R                  [        S9n[        U5      S:  a#  [        S U 5       5      (       a   [        U5      $ [        [        [        U Vs/ s H  n[!        U5      PM     sn6 5      5      n [#        U 5      $ X4 Vs/ s H  n[!        U5      PM     snu  pX-  $ [%        U [        5      (       a  / n/ nU R                   H7  nUR'                  5       u  pUR)                  U5        UR)                  U	5        M9     [        S U 5       5      (       a"  [        S U 5       5      (       a  [+        Xc5      $ [%        U [,        5      (       a<  U R                  nU(       a)  U R.                  " U V	s/ s H  n	[!        U	5      PM     sn	6 $ U $ ! [         a     GN[f = fs  snf s  snf s  sn	f )z+Returns expr after denesting its arguments.rI   r8   c              3   >   #    U  H  oS -  R                   v   M     g7f)r8   N)r5   r"   s     r   r%   _sqrtdenest0.<locals>.<genexpr>   s     (IDqQ$):):Ds   c              3   8   #    U  H  oR                   v   M     g 7fr    )r   )r#   cs     r   r%   rt      s     )b}}br'   c              3   8   #    U  H  n[        U5      v   M     g 7fr    )r   )r#   args     r   r%   rt      s     1O$3'#,,$r'   )r   as_numer_denomr   rV   r.   r*   rR   r-   r   lenr6   _sqrtdenest_recrj   r   r   r   rB   _sqrtdenest1
isinstancerZ   rW   _sqrt_ratcombr   func)
r   r;   dr-   r$   rD   csrx   rv   r<   s
             r   rB   rB      s    t}}""$:vv}}affkk/?@t9q=S(ID(I%I%I.q11 HSD*IDq<?D*I%JKL%%./V4VLOV4DA3J$99C##%DAIIaLKKN 
 )b)))c1O$1O.O.O **$yy99=1|A=>>K/ 3 *I 5" >s$   
G3 8H)H	H3
H Hc                 b   SSK JnJnJn  U R                  (       d  [        U 5      $ U R                  S:  a+  [        S5      [        [        U R                  * 5      5      -  $ U" U R                  5      u  pEnU[        U5      -  nXV:  a  Xepe[        US-  US-  -
  5      n[        UR                  5      S:  a  U" U5      u  pHn	U[        U5      -  nX:  a  Xp[        US-  U	S-  -
  5      n
[        [        U
5      5      n[        [        X-   5      5      nU" X5      u  p[        U[        S5      -  X[        S5      -  -  -   5      nO[        [        U5      5      n[        U5      S:  a  [        eX_-   n[        UR                  5      [        U R                  5      :  a(  [        U5      [        U R                  5      :  a  [        e[        [        U5      5      n[        U5      S:  a  [        eU" UU5      u  pU[        S5      -  X[        S5      -  -  -   nU" U5      n[        U5      $ )a   Helper that denests the square root of three or more surds.

Explanation
===========

It returns the denested expression; if it cannot be denested it
throws SqrtdenestStopIteration

Algorithm: expr.base is in the extension Q_m = Q(sqrt(r_1),..,sqrt(r_k));
split expr.base = a + b*sqrt(r_k), where `a` and `b` are on
Q_(m-1) = Q(sqrt(r_1),..,sqrt(r_(k-1))); then a**2 - b**2*r_k is
on Q_(m-1); denest sqrt(a**2 - b**2*r_k) and so on.
See [1], section 6.

Examples
========

>>> from sympy import sqrt
>>> from sympy.simplify.sqrtdenest import _sqrtdenest_rec
>>> _sqrtdenest_rec(sqrt(-72*sqrt(2) + 158*sqrt(5) + 498))
-sqrt(10) + sqrt(2) + 9 + 9*sqrt(5)
>>> w=-6*sqrt(55)-6*sqrt(35)-2*sqrt(22)-2*sqrt(14)+2*sqrt(77)+6*sqrt(10)+65
>>> _sqrtdenest_rec(sqrt(w))
-sqrt(11) - sqrt(7) + sqrt(2) + 3*sqrt(5)
r   )radsimprad_rationalizerH   r8   r   )rO   r   r   rH   r   rF   r.   r   r{   r   rz   r-   r|   r!   rj   r	   )r   r   r   rH   gr<   r=   c2r?   re   c2_1c_1d_1numdenrv   acr   r^   s                      r   r{   r{     s   4 ML;;$yy1}BxdiiZ(8999$))$GA!	$q'	Au1	!Q$A+	B
277|aO	rQZ7AA&d4j)d28n-"2+Sa[3DG#445b"!}q%%	
B
277|s499~%R=Idii00))48A!}q%%q!$HC	$q'	CT!W%%A
AA;r   c                 6   SSK Jn  [        U 5      (       d  U $ U R                  nUR                  (       a  U $ [        U5      nU(       d  U $ Uu  p5n[        US-  US-  U-  -
  5      nUR                  (       ay  UR                  (       a  [        X5Xg5      nUb  U$ Og[        U* U-  5      n	[        U	5      n
U
R                  (       a*  [        [        XV-  5      X6U	5      nUb  U[        US5      -  $ O[        X5U5      nUb  U$ U(       a  [        U 5      (       d  U $ [        XXVU5      nU(       a  U$ X5Xg/n[        U" U S-  5      /US[!        U 5      5      S   nUS   c  U $ Ub4  [!        U5      [!        U 5      :X  a  [#        U5      [#        U 5      :  a  U $ U$ U $ )z_Return denested expr after denesting with simpler methods or, that
failing, using the denester.r   r   r8      r   )sympy.simplify.simplifyr   r   r.   r)   rh   r   r   rL   _sqrt_numeric_denestr   r   _sqrt_symbolic_denestr3   sqrt_biquadratic_denest	_denesterr!   r	   )r   denesterr   r<   valr=   r^   d2rE   dr2drr[   av0s                r   r|   r|   A  s    04==		Ayy
a.CGA!	!Q$Aa-	 B	~~>>$Q11A}  B3q5/CcB~~(!#cB=T!QZ<' "!*=H<--
!$1
4C

 -C747#$c1j.>?BA
1v~}a=Jt,,1	$1OKKr   c                 t   [        [        XU45      u  pn[        U5      nU(       d  gUu  pEnU(       a  [        SSS9n [	        U R                  [        U5      US-  U-
  U-  5      U5      nUR                  5       S:X  a  UR                  5       u  pnX-  n
[        U
S-  SU	-  U-  -
  5      R                  S5      (       ac  [        U	[        U5      U
SU	-  -  -   S-  -  5      nUR                  (       a-  [        [        R                  " UR                  5       5      5      nU$ ggg! [         a     gf = f)a  Given an expression, sqrt(a + b*sqrt(b)), return the denested
expression or None.

Explanation
===========

If r = ra + rb*sqrt(rr), try replacing sqrt(rr) in ``a`` with
(y**2 - ra)/rb, and if the result is a quadratic, ca*y**2 + cb*y + cc, and
(cb + b)**2 - 4*ca*cc is 0, then sqrt(a + b*sqrt(r)) can be rewritten as
sqrt(ca*(sqrt(r) + (cb + b)/(2*ca))**2).

Examples
========

>>> from sympy.simplify.sqrtdenest import _sqrt_symbolic_denest, sqrtdenest
>>> from sympy import sqrt, Symbol
>>> from sympy.abc import x

>>> a, b, r = 16 - 2*sqrt(29), 2, -10*sqrt(29) + 55
>>> _sqrt_symbolic_denest(a, b, r)
sqrt(11 - 2*sqrt(29)) + sqrt(5)

If the expression is numeric, it will be simplified:

>>> w = sqrt(sqrt(sqrt(3) + 1) + 1) + 1 + sqrt(2)
>>> sqrtdenest(sqrt((w**2).expand()))
1 + sqrt(2) + sqrt(1 + sqrt(1 + sqrt(3)))

Otherwise, it will only be simplified if assumptions allow:

>>> w = w.subs(sqrt(3), sqrt(x + 3))
>>> sqrtdenest(sqrt((w**2).expand()))
sqrt((sqrt(sqrt(sqrt(x + 3) + 1) + 1) + 1 + sqrt(2))**2)

Notice that the argument of the sqrt is a square. If x is made positive
then the sqrt of the square is resolved:

>>> _.subs(x, Symbol('x', positive=True))
sqrt(sqrt(sqrt(x + 3) + 1) + 1) + 1 + sqrt(2)
NyT)positiver8   r   r   )mapr   rh   r   r   subsr   r   degree
all_coeffsr   equals	is_numberr   rX   as_content_primitive)r<   r=   r^   rvalrarbrrr   newacacbccrE   s                r   r   r   |  s)   T 'A!9%GA!q>DJBB	#%	tBx!Q$)R8!<D ;;=A*JBBGBA"R(//22T!Wr1R4y01445;; 0F0F0H!IJA	 3  
  		s    .D* *
D76D7c                 N   [        U5      nX-   n[        U5      [        U5      S-   :  d  US-  R                  (       ag  [        U5      [        U5      pvXgs=:X  a  S:X  a  O  OS=pgU[        X-   5      -  U[        X-
  5      -  -   [        S5      -  S-  nUR	                  5       $ g)zrHelper that denest
$\sqrt{a + b \sqrt{r}}, d^2 = a^2 - b^2 r > 0$

If it cannot be denested, it returns ``None``.
r   r8   r   N)r   r!   r   r   expand)	r<   r=   r^   r   r   ss1s2r[   s	            r   r   r     s     	RA	A
 !}z!}q((QT,>,>a$q'B>r>KBDK"tAE{"22d1g=Azz| -?r   c           	      r   SSK JnJn  US::  d&  US:  d   U(       a  [        U R                  5      S:  a  gXU4 H@  nUR
                   H-  nUS-  n	U	R                  (       a  U	R                  (       a  M,      g   MB     [        [        [        U" U5      5      5      5      n
[        U
5      S:  a  gUS-  U
S-  -   US-  U
S-  -
  /u  pX4 Hh  n[        [        U5      5      n[        U5      S:  a  M(  U" U[        SU-  5      5      u  pX-  nUU[        U5      -  -   nUS:  a  U* n[        U5      s  $    g)a.  denest expr = sqrt(a + b*sqrt(r))
where a, b, r are linear combinations of square roots of
positive rationals on the rationals (SQRR) and r > 0, b != 0,
d2 = a**2 - b**2*r > 0

If it cannot denest it returns None.

Explanation
===========

Search for a solution A of type SQRR of the biquadratic equation
4*A**4 - 4*a*A**2 + b**2*r = 0                               (1)
sqd = sqrt(a**2 - b**2*r)
Choosing the sqrt to be positive, the possible solutions are
A = sqrt(a/2 +/- sqd/2)
Since a, b, r are SQRR, then a**2 - b**2*r is a SQRR,
so if sqd can be denested, it is done by
_sqrtdenest_rec, and the result is a SQRR.
Similarly for A.
Examples of solutions (in both cases a and sqd are positive):

  Example of expr with solution sqrt(a/2 + sqd/2) but not
  solution sqrt(a/2 - sqd/2):
  expr = sqrt(-sqrt(15) - sqrt(2)*sqrt(-sqrt(5) + 5) - sqrt(3) + 8)
  a = -sqrt(15) - sqrt(3) + 8; sqd = -2*sqrt(5) - 2 + 4*sqrt(3)

  Example of expr with solution sqrt(a/2 - sqd/2) but not
  solution sqrt(a/2 + sqd/2):
  w = 2 + r2 + r3 + (1 + r3)*sqrt(2 + r2 + 5*r3)
  expr = sqrt((w**2).expand())
  a = 4*sqrt(6) + 8*sqrt(2) + 47 + 28*sqrt(3)
  sqd = 29 + 20*sqrt(3)

Define B = b/2*A; eq.(1) implies a = A**2 + B**2*r; then
expr**2 = a + b*sqrt(r) = (A + B*sqrt(r))**2

Examples
========

>>> from sympy import sqrt
>>> from sympy.simplify.sqrtdenest import _sqrt_match, sqrt_biquadratic_denest
>>> z = sqrt((2*sqrt(2) + 4)*sqrt(2 + sqrt(2)) + 5*sqrt(2) + 8)
>>> a, b, r = _sqrt_match(z**2)
>>> d2 = a**2 - b**2*r
>>> sqrt_biquadratic_denest(z, a, b, r, d2)
sqrt(2) + sqrt(sqrt(2) + 2) + 2
r   )r   r   r8   Nr   )rO   r   r   r!   r.   r-   r5   rL   r   rF   r   )r   r<   r=   r^   r   r   r   r$   r   y2sqdrf   x2ABnBdBrE   s                     r   r   r     s#   ` @AvaqJtyy$9A$=AYAAB==  
 :d72;/0
1C#cCEk1Q3Q;'FBXtAwa=1 HQqSM2E$q'	Mq5A{  r   c                   ^ SSK Jn  X#:  a  gUS   c  gUS   c  [        S U  5       5      (       a  [        [	        U 5      5       H  m[        [        [        [	        T5      5       Vs/ s H  nTU   (       d  M  X   PM     sn6 5      nTR                  S5      S:  a  TS   (       a  U* n[        U5      nUR                  (       d  M  UT4s  $    [        U S   5      S/[	        U 5      -  4$ SnUS   b  USS /n	US   nUS	   U/n
SUS'   O[        [        SU  Vs/ s H  n[        U5      PM     sn5      5      n	U	 H(  nUS   (       d  M  Ub  XS   :w  a  SUS'     gM#  US   nM*     Uc  [        U S   5      S/[	        U 5      -  4$ U	 Vs/ s H(  n[        US   S-  5      [        XS   S-  -  5      -
  PM*     snU/-   n
[        XUS-   U5      u  nmT(       d  g[        U4S
 j[        [	        U 5      5       5       5      (       d'  U	S   n[        US   [        US   U-  5      -   5      T4$ [        [        [	        U 5      5       Vs/ s H  nTU   (       d  M  X   PM     sn6 n[        U5      nST;   aI  TR!                  S5      [	        U 5      S-
  :  a(  T[	        U 5      S-
     (       a  US   * US'   US   * US'   T[	        U 5         (       d  [        US   U-   5      nUS::  a  [        U S   5      S/[	        U 5      -  4$ [#        U5      [#        U5      S-   ::  d  US-  R$                  (       d  SUS'   g['        [        U5      SS9n[#        U5      [#        U5      S-   ::  d  SUS'   gU" SU-  5      n[        U[        S5      -  US   [        U5      -  U-  [        S5      -  -   5      nUT4$ [        US   U-  5      U-   nUS::  a  [        U S   5      S/[	        U 5      -  4$ [)        [        U5      S5      [        U5      nn[        U[        S5      U-  -  US   U-  [        S5      U-  -  -   5      T4$ s  snf s  snf s  snf s  snf )aH  Denests a list of expressions that contain nested square roots.

Explanation
===========

Algorithm based on <http://www.almaden.ibm.com/cs/people/fagin/symb85.pdf>.

It is assumed that all of the elements of 'nested' share the same
bottom-level radicand. (This is stated in the paper, on page 177, in
the paragraph immediately preceding the algorithm.)

When evaluating all of the arguments in parallel, the bottom-level
radicand only needs to be denested once. This means that calling
_denester with x arguments results in a recursive invocation with x+1
arguments; hence _denester has polynomial complexity.

However, if the arguments were evaluated separately, each call would
result in two recursive invocations, and the algorithm would have
exponential complexity.

This is discussed in the paper in the middle paragraph of page 179.
r   r   )NNr   Nc              3   8   #    U  H  oR                   v   M     g 7fr    )rP   )r#   r;   s     r   r%   _denester.<locals>.<genexpr>6  s     ,VVr'   r   r8   r9   c              3   .   >#    U  H
  nTU   v   M     g 7fr    rl   )r#   rD   fs     r   r%   r   Y  s     4!3A1Q4!3s   F)r   r   )r   r   r6   r:   rz   r   r   rA   countr   r   rS   filterrh   r   anyindexr!   rP   r|   r   )nestedr   hmax_depth_levelr   rD   r0   sqpRvaluesnested2r   r_   r   vadsqvadsqvad1r[   r   FRr   r   s                        @r   r   r     sT   . 0
1v~A,V,,,#f+&A%A-H-Q1Q4yvy-HIJAwwqzA~!B%Bq'CAv ' F2J!S[00q6"1gYFAA1vqkGCF&f'MfdD(9f'MNOFQ44}!9%)CF#- % aD  yF2J'!S[884:<4:q  !a(A$'	*+4:<?@cBGq1uo>14s6{!3444r
A!x!Q//0!33s6{);D);Aqtifi);DEAAAAv!''!*s6{Q61S[1_;M!u!!u!S[>qtax(!8r
+aSV_<<!#*Q-!*;;Q))!CF%$T#Y?"5)Z]Q->>!CF% 5)uT!W}!T!WV0CDG0KLMAv ad1f%)7r
+aSV_<<Xa[!,d2hA472:1b$q'!)1D DEqHHE I  (N< Es$   )Q
;Q
Q/QQ"Qc                 4   SSK Jn  S nU" U5      nUc(  [        [        X5       VVs/ s H	  u  pVXV-  PM     snn6 $ Uu  pxn	U R	                  U	5      n
UR	                  U	5        X   nX==   U" X-  UR
                  -  5      -  ss'   [        X5      $ s  snnf )zDenest rational combinations of radicals.

Based on section 5 of [1].

Examples
========

>>> from sympy import sqrt
>>> from sympy.simplify.sqrtdenest import sqrtdenest
>>> z = sqrt(1+sqrt(3)) + sqrt(3+3*sqrt(3)) - sqrt(10+6*sqrt(3))
>>> sqrtdenest(z)
0
r   r   c                    [        U 5      n[        US-
  5       Hm  n[        US-   U5       HW  nX   R                  nX   R                  n[        XE-  5      n[	        [        U5      5      nU[        U5      :w  d  MQ  XrU4s  s  $    Mo     g )Nr   )rz   rA   r.   r   rF   r   )r<   r;   rD   jr   r   r0   r   s           r   find_sqrt_ratcomb.<locals>.find  su    Fq1uA1q5!_TYYTYYRW%tAw'Q<7N % r   )rO   r   r   ziprU   r.   r~   )r   r-   r   r   indicesrv   rx   r   i1i2r   r?   s               r   r~   r~   }  s     /	# 4jG3r=9=QW=9::IA2	BHHRL	B Fgbfrww&''F"" :s   B
N)r9   )T)&
sympy.corer   r   r   r   r   sympy.core.functionr   r	   r
   sympy.core.sortingr   sympy.core.symbolr   sympy.functionsr   r   r   sympy.polysr   r   r   intr!   r3   r:   rF   rh   StopIterationrj   rB   r{   r|   r   r   r   r   r~   rl   r   r   <module>r      s    1 1 ? ? / # , , -LS :8:!HL^	m 	"J=@8v<~(GTaIH)#r   