
    MZdT                         d dl mZmZmZmZmZ d dlmZmZm	Z	 d dl
mZ d dlmZ d dlmZmZmZ d dlmZmZ d Zd Zd	 Zd
 ZddZd Z G d de      Zd Zd ZddZd Z d Z!d Z"d Z#d Z$y)    )AddExprMulSsympify)_mexpand	count_ops
expand_mul)default_sort_key)Dummy)rootsignsqrt)PolyPolynomialErrorc                     | j                   xr= | j                  j                  xr% t        | j                        t        j
                  u S )z/Return True if expr is a sqrt, otherwise False.)is_Powexpis_Rationalabsr   Half)exprs    ;/usr/lib/python3/dist-packages/sympy/simplify/sqrtdenest.pyis_sqrtr   	   s3     ;;K488//KCMQVV4KK    c                 (   | t         j                  u ry| j                  ry| j                  s| j                  r2t        | j                  D cg c]  }t        |       c}t              S t        |       rt        | j                        dz   S yc c}w )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   key)r   ImaginaryUnitis_Atomis_Addis_Mulmaxargs
sqrt_depthr   r   base)pxs     r   r&   r&      sn    $ 	AOOyy	
QXX1662aJqM28HII	!&&!A%%	 3s   Bc                 $   | j                   ry| j                  ryt        |       s"| j                  r+| j                  j
                  rt        | j                        S | j                  s| j                  rt        d | j                  D              S y)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   2   K   | ]  }t        |        y wN)is_algebraic.0r)   s     r   	<genexpr>zis_algebraic.<locals>.<genexpr>D   s     3q<?3   )r   r!   r   r   r   
is_Integerr-   r'   r"   r#   allr%   )r(   s    r   r-   r-   -   se      	}}	
	qxxAEE$4$4AFF##	
QXX3AFF333r   c                     | dk(  rdgg}|S | dk(  rddgddgddgg}|S | dk(  rg dg dg dg dg d	g d
g dg}|S t        | dz
        }|D cg c]  }|dgz   
 }}|D cg c]  }|dgz   
 }}|dg| dz
  z  dgz   gz   |z   }|S c c}w c c}w )ag  
    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   r7   r7   I   s     	AvSE H 
aVaVaV$ H 
a	9	9i9 H	 QUO !!a1#g!! !!a1#g!!1#q1u+#$$r)H "!s   B!Bc                 h    t        |       } t        |      D ]  }t        |       }| |k(  r| c S |}  | S )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   
sqrtdenestrC   f   sC    6 dD8_ 19K	
 Kr   c                    ddl m} t        |       } | j                  r,| t        j
                  t        j
                  f}t        |      S | j                  r=t        | j                  t              }|D cg c]  }|dz  	 }}t        d |D              r ||       \  }}}|||f}t        |      S t        |      D 	cg c]  \  }	}t        |      ||	f }
}	}t        |
t              }|d   dk(  rg }t        |      S |\  }}}	|j                  |	      }|
j                  |	       t        j                   }|j"                  rpg }g }|j                  D ]3  }t        |      |k  r|j%                  |       #|j%                  |       5 t'        j(                  |      }t'        j(                  |      }g }|g}|
D ]  }|d   |k  r|j%                  |d           |d   }||k(  r|j%                  d       <|j"                  rWt        |j                        }||v r)|j+                  |       |j%                  t'        |        |j%                  |d          |j%                  |d           t-        | }t-        | }|||dz  f}t        |      S | j/                         \  }}t1        |      r!t        j
                  ||dz  f}t        |      S g }t        |      S c c}w c c}}	w )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_surdsr   r5   c              3   P   K   | ]  }|j                   xr |j                     y wr,   )r   is_positive)r/   sqs     r   r0   z_sqrt_match.<locals>.<genexpr>   s      BRr~~0"..0Bs   $&r   )sympy.simplify.radsimprE   r   	is_Numberr   Zeror"   sortedr%   r   r3   list	enumerater&   r$   popOner#   appendr   
_from_argsremover   as_coeff_Mulr   )r(   rE   respargsr)   sqargsrr:   r9   rA   vnmaxdepth_bvrvr<   b1x1x1argss                       r   _sqrt_matchrb      s    3A{{!&&!&&!x 9w 
qvv#34 %&1!Q$&&B6BB!!nGAq!Q'C9 09/?@tq!jmQ"@@1*+7a<C\ 9W KE1a		!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A,C 9 ~~11:661ad#C 9 C9s ' As   6K  K%c                       e Zd Zy)SqrtdenestStopIterationN)__name__
__module____qualname__ r   r   rd   rd      s    r   rd   c                    t        |       r| j                         \  }}|t        j                  u r|j                  j
                  rt        |j                  j                  t              }t        |      dkD  rt        d |D              r	 t        |      S t        t        t        |D cg c]  }t!        |       c}             } t#        |       S ||fD cg c]  }t!        |       c}\  }}||z  S t%        | t              rzg }g }| j                  D ]7  }|j'                         \  }}	|j)                  |       |j)                  |	       9 t        d |D              rt        d |D              rt+        ||      S t%        | t,              r4| j                  }|r& | j.                  |D 	cg c]  }	t!        |	       c}	 S | S # t        $ r Y ;w xY wc c}w c c}w c c}	w )z+Returns expr after denesting its arguments.r   r5   c              3   :   K   | ]  }|d z  j                     yw)r5   N)r2   r.   s     r   r0   z_sqrtdenest0.<locals>.<genexpr>   s     (Iq!Q$):):(Is   c              3   4   K   | ]  }|j                     y wr,   )r   )r/   cs     r   r0   z_sqrtdenest0.<locals>.<genexpr>   s     )q}})   c              3   2   K   | ]  }t        |        y wr,   )r   )r/   args     r   r0   z_sqrtdenest0.<locals>.<genexpr>   s     1O3'#,1Or1   )r   as_numer_denomr   rP   r'   r"   rL   r%   r   lenr3   _sqrtdenest_recrd   r   r   r   r?   _sqrtdenest1
isinstancerT   rQ   _sqrt_ratcombr   func)
r   r8   dr%   r)   rA   csro   rl   r9   s
             r   r?   r?      s    t}""$1:vv}}affkk/?@t9q=S(ID(I%I.q11 HSD*Iq<?*I%JKL%%./V4LO4DAqQ3J$99 	C##%DAqIIaLKKN	
 )b))c1O$1O.O T**$yy499=1|A=>>K/ 3 *I 5" >s$   
G +GG/G!	GGc                 f   ddl m}m}m} | j                  st        |       S | j                  dk  r+t        d      t        t        | j                               z  S  || j                        \  }}}|t        |      z  }||k  r||}}t        |dz  |dz  z
        }t        |j                        dkD  r ||      \  }}}	|t        |      z  }||	k  r|	|}	}t        |dz  |	dz  z
        }
t        t        |
            }t        t        ||z               } ||	|      \  }}t        |t        d      z  ||t        d      z  z  z         }nt        t        |            }t        |      dkD  rt        ||z   }t        |j                        t        | j                        k\  r't        |      t        | j                        k\  rt        t        t        |            }t        |      dkD  rt         |||      \  }}|t        d      z  ||t        d      z  z  z   } ||      }t        |      S )al  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_rationalizerE   r5   r   )rI   rz   r{   rE   r   rC   r'   r   rr   r   rq   r%   rs   r&   rd   r	   )r   rz   r{   rE   gr9   r:   c2r<   r_   c2_1c_1d_1numdenrl   acrw   rX   s                      r   rr   rr     s   4 ML;;$yy1}BxdiiZ(8999$))$GAq!	$q'	A1u!1	!Q$A+	B
277|aO	2rQZ7BAA&d4j)d28n-"2s+SSa[3DG#445b"!}q%%	
QB
277|s499~%R=Idii00))48A!}q%%q!$HC	$q'	CT!W%%A
AA;r   c                    ddl m} t        |       s| S | j                  }|j                  r| S t        |      }|s| S |\  }}}t        |dz  |dz  |z  z
        }|j                  ro|j                  rt        ||||      }|d|S t        | |z        }	t        |	      }
|
j                  r<t        t        ||z        |||	      }| |t        |d      z  S t        |||      }||S |rt        |       s| S t        | ||||      }|r|S ||||g}t         || dz        g|dt!        |             d   }|d   | S |2t!        |      t!        |       k(  rt#        |      t#        |       kD  r| S |S | S )zcReturn denested expr after denesting with simpler methods or, that
    failing, using the denester.r   rz   r5      r   )sympy.simplify.simplifyrz   r   r'   r!   rb   r   r   rG   _sqrt_numeric_denestr   r   _sqrt_symbolic_denestr-   sqrt_biquadratic_denest	_denesterr&   r	   )r   denesterrz   r9   valr:   rX   d2rB   dr2drrU   av0s                r   rs   rs   B  s    04=		Ayy
a.CGAq!	!Q$Aa-	 B	~~>>$Q1b1A}
 B3q5/CcB~~(!A#1cB=T!QZ<' "!Q*=H<-
!$1a
4C

 aB-C747#$c1j.>?BA
1v~}a=Jt,,1	$1OKKr   c                 V   t        t        | ||f      \  } }}t        |      }|sy|\  }}}|rt        dd      }	 t	        | j                  t        |      |dz  |z
  |z        |      }|j                         dk(  r|j                         \  }	}
}|
|z  }
t        |
dz  d|	z  |z  z
        j                  d      r]t        |	t        |      |
d|	z  z  z   dz  z        }|j                  r,t        t        j                  |j                                     }|S yyy# t        $ r Y yw xY w)a4  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)positiver5   r   r   )mapr   rb   r   r   subsr   r   degree
all_coeffsr   equals	is_numberr   rR   as_content_primitive)r9   r:   rX   rvalrarbrrr   newacacbccrB   s                r   r   r   }  s0   T 'Aq!9%GAq!q>DJBB	#%	tBx!Q$)R8!<D ;;=A*JBB!GBA"R(//2T!Wr1R4y01445;; 0F0F0H!IJA	 3  
  		s   .D 	D('D(c                 J   t        |      }| |z   }t        |      t        |      dz   k  s|dz  j                  rjt        |      t        |      }}||cxk(  rdk(  rn ndx}}|t        | |z         z  |t        | |z
        z  z   t        d      z  dz  }|j	                         S y)z~Helper that denest
    $\sqrt{a + b \sqrt{r}}, d^2 = a^2 - b^2 r > 0$

    If it cannot be denested, it returns ``None``.
    r   r5   r|   N)r   r&   r   r   expand)	r9   r:   rX   r   rw   ss1s2rU   s	            r   r   r     s     	RA	AA
 !}z!}q((QT,>,>a$q'B>r>KBDQK"tAE{"22d1g=Azz| -?r   c           	      F   ddl m}m} |dk  s|dk  s|rt        | j                        dk  ry|||fD ]3  }|j
                  D ]"  }|dz  }	|	j                  r|	j                  r!  y 5 t        t        t         ||                        }
t        |
      dkD  ry|dz  |
dz  z   |dz  |
dz  z
  g\  }}||fD ]g  }t        t        |            }t        |      dkD  r& ||t        d|z              \  }}||z  }||t        |      z  z   }|dk  r| }t        |      c S  y)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   )rz   r{   r5   Nr   )rI   rz   r{   r&   r'   r%   r2   rG   r   rC   r   )r   r9   r:   rX   r   rz   r{   r)   r   y2sqdr`   x2ABnBdBrB   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BrE$q'	Mq5A{	 r   c                    ddl m} ||kD  ry|d   y|d   t        d | D              rt        t	        |             D ]u  t        t        t        t	                    D cg c]  }|   s	| |    c}       }j                  d      dkD  rd   r| }t        |      }|j                  sq|fc S  t        | d         dgt	        |       z  fS d}|d   |dd g}	|d   }|d	   |g}
d|d<   nt        t        d| D cg c]  }t        |       c}            }	|	D ]  }|d   s	|||d   k7  sd|d<    y|d   }! |t        | d         dgt	        |       z  fS |	D cg c](  }t        |d   dz        t        ||d   dz  z        z
  * c}|gz   }
t        |
||dz   |      \  }syt        fd
t        t	        |             D              s'|	d   }t        |d   t        |d   |z        z         fS t        t        t	        |             D cg c]  }|   s	| |    c} }t        |      }dv rCj!                  d      t	        |       dz
  k  r#t	        |       dz
     r|d    |d<   |d    |d<   t	        |          st        |d   |z         }|dk  rt        | d         dgt	        |       z  fS t#        |      t#        |      dz   k  s|dz  j$                  sd|d<   yt'        t        |      d      }t#        |      t#        |      dz   k  sd|d<   y |d|z        }t        |t        d      z  |d   t        |      z  |z  t        d      z  z         }|fS t        |d   |z        |z   }|dk  rt        | d         dgt	        |       z  fS t)        t        |      d      t        |      }}t        |t        d      |z  z  |d   |z  t        d      |z  z  z         fS c c}w c c}w c c}w c c}w )a  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   4   K   | ]  }|j                     y wr,   )rJ   )r/   r8   s     r   r0   z_denester.<locals>.<genexpr>7  s     ,,rm   r|   r5   r6   c              3   (   K   | ]	  }|     y wr,   rh   )r/   rA   fs     r   r0   z_denester.<locals>.<genexpr>Z  s     4A1Q44s   F)r   r   )r   rz   r3   r7   rq   r   r   r>   countr   r   rM   filterrb   r   anyindexr&   rJ   rs   r   )nestedr   hmax_depth_levelrz   rA   r(   sqpRvaluesnested2r   rY   rw   vadsqvadsqvad1rU   r   FRr   r   s                        @r   r   r     sB   . 0?
1v~A,V,,#f+& 	A%A-HQ1Q4vayHIJAwwqzA~!B%Bq'CAv	 F2J!S[00q6"1gYFAA1vqkGCF&f'MdD(9'MNOF !Q4}!9%)CF#-aD! yF2J'!S[884:</0  !a(!A$'	*+ <?@cBG#q1uo>14s6{!344r
A!x!Q//0!33s6{);DAqtfQi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$   "
O8-O8O=&-P
P$Pc                 <   ddl m} d } ||      }|(t        t        | |      D cg c]
  \  }}||z   c}} S |\  }}}	| j	                  |	      }
|j	                  |	       ||   }| |xx    ||
|z  |j
                  z        z  cc<   t        | |      S c c}}w )a#  Denest 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                    t        |       }t        |dz
        D ]m  }t        |dz   |      D ]Y  }| |   j                  }| |   j                  }t        ||z        }t	        t        |            }|t        |      k7  sR|||fc c S  o y )Nr   )rq   r>   r'   r   rC   r   )r9   r8   rA   jr   r   r(   r   s           r   findz_sqrt_ratcomb.<locals>.find  s    Fq1u 	#A1q5!_ #qTYYqTYYR"W%tAw'Q<a7N#	#r   )rI   rz   r   ziprO   r'   ru   )rx   r%   rz   r   indicesrl   ro   r   i1i2r~   r<   s               r   ru   ru   ~  s     /	# 4jG3r4=9CQW9::IAr2	BHHRL	bB rFgb1frww&''FT"" :s   B
N)r6   )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   r&   r-   r7   rC   rb   StopIterationrd   r?   rr   rs   r   r   r   r   ru   rh   r   r   <module>r      s    1 1 ? ? / # , , -L<8:!HL^	m 	"J=@8v<~(GTaIH)#r   