
    \h                        S SK Jr  S SKJrJrJrJrJr  S SKJ	r	J
r
  S SKJr  S SKJrJr  S SKJrJrJr  S SK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J r   S SK!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.  S SK/J0r0J1r1  S#S jr2S r3S$S jr4S r5SS.S jr6S%S jr7S r8S&S jr9S&S jr:S&S jr;S r<S r=S  r>\=r?\>r@\<rAS! rBS" rCg)'    )defaultdict)sympifySMul
DerivativePow)_unevaluated_AddAdd)assumptions)Factors	gcd_terms)_mexpand
expand_mulexpand_power_base)_keep_coeff_unevaluated_Mul_mulsort)Rationalzoonan)global_parameters)ordereddefault_sort_key)DummyWildsymbols)expsqrtlog)Abs)gcd)
sqrtdenest)iterablesiftNTc                   ^^^.^/ [        U 5      n [        T5      (       a  TOT/ Vs/ s H  n[        U5      PM     snmS n[        TUSS9u  pU	(       Ga  [        [	        X Vs/ s H  n[        S0 [        U5      D6PM     sn5      5      n
T Vs/ s H  oR                  X5      PM     snm[        U R                  U
5      TX#TUS9nU
R                  5        VVs0 s H  u  pX_M	     nnn[        U[        5      (       d  UR                  U5      $ UR                  5        VVs0 s H5  u  pUR                  X5      R                  U5      UR                  U5      _M7     snn$ Tc  [        5       n[        R                  " U 5       H  nUR                   " T6 (       a  UT;   a  M  UR"                  (       d  UT;  a  UR%                  U5        MI  UR&                  " UR(                  " T6 S   6 nUT;  d  Mp  UR%                  U5        M     [+        U4S jU 5       5      nT[-        [/        U5      5      -   mU(       d  [        U TX#SUS9$ Uc  [0        R2                  nS	 nS
 m.U.4S jm/UU/4S jnU(       a  U R4                  (       a]  U R7                  5       =(       d    SnU R8                  " U R:                   Vs/ s H  nUU:w  d  M  [        UTUSTU5      PM     sn6 U-   n OU R"                  (       a8  U R8                  " U R:                   Vs/ s H  n[        UTUSTU5      PM     sn6 $ U R<                  (       a0  [        U R>                  TUSTU5      n[A        UU RB                  5      $ T Vs/ s H  n[E        USS9PM     snmSnU(       a:  U R7                  5       nUb'  URF                  " T6 (       a  SnOU RI                  5       n [        R                  " U 5       Vs/ s H  n[E        USS9PM     nn[K        [,        5      [L        RN                  nnU GHE  nURQ                  SS9u  nn[-        [/        U5      5      U-   n U  Vs/ s H  nT/" U5      PM     n!nSn"T H  n#[        U#[R        5      (       a"  U"(       a  [-        [U        U!5      5      n!U"(       + n"U" U!U#5      n$U$c  MH  U#RV                  (       d  [Y        S5      eU$u  n!n%n&n'U'(       dI  / n(U% H8  n)U)S   c  U)S   n*OU)S   U)S   -  n*U(R[                  [A        U)S   U*5      5        M:     []        U(6 n+OU" U%5      n+[E        U" U!5      SS9n![E        U+SS9n+UU+   R[                  U!5          GM>     UU-  nGMH     UR                  5        VVs0 s H  u  pU[        U6 _M     nnnU[L        RN                  La  UU[L        R^                  '   Ub"  UR                  5        H  u  n,n-U-U-   UU,'   M     Ub,  UR                  5        V,V-s0 s H  u  n,n-U,U" U-5      _M     nn,n-U(       a/  [        UR                  5        V,V-s/ s H  u  n,n-U,U--  PM     sn-n,6 $ U$ s  snf s  snf s  snf s  snnf s  snnf s  snf s  snf s  snf s  snf s  snf s  snnf s  sn-n,f s  sn-n,f )a  
Collect additive terms of an expression.

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

This function collects additive terms of an expression with respect
to a list of expression up to powers with rational exponents. By the
term symbol here are meant arbitrary expressions, which can contain
powers, products, sums etc. In other words symbol is a pattern which
will be searched for in the expression's terms.

The input expression is not expanded by :func:`collect`, so user is
expected to provide an expression in an appropriate form. This makes
:func:`collect` more predictable as there is no magic happening behind the
scenes. However, it is important to note, that powers of products are
converted to products of powers using the :func:`~.expand_power_base`
function.

There are two possible types of output. First, if ``evaluate`` flag is
set, this function will return an expression with collected terms or
else it will return a dictionary with expressions up to rational powers
as keys and collected coefficients as values.

Examples
========

>>> from sympy import S, collect, expand, factor, Wild
>>> from sympy.abc import a, b, c, x, y

This function can collect symbolic coefficients in polynomials or
rational expressions. It will manage to find all integer or rational
powers of collection variable::

    >>> collect(a*x**2 + b*x**2 + a*x - b*x + c, x)
    c + x**2*(a + b) + x*(a - b)

The same result can be achieved in dictionary form::

    >>> d = collect(a*x**2 + b*x**2 + a*x - b*x + c, x, evaluate=False)
    >>> d[x**2]
    a + b
    >>> d[x]
    a - b
    >>> d[S.One]
    c

You can also work with multivariate polynomials. However, remember that
this function is greedy so it will care only about a single symbol at time,
in specification order::

    >>> collect(x**2 + y*x**2 + x*y + y + a*y, [x, y])
    x**2*(y + 1) + x*y + y*(a + 1)

Also more complicated expressions can be used as patterns::

    >>> from sympy import sin, log
    >>> collect(a*sin(2*x) + b*sin(2*x), sin(2*x))
    (a + b)*sin(2*x)

    >>> collect(a*x*log(x) + b*(x*log(x)), x*log(x))
    x*(a + b)*log(x)

You can use wildcards in the pattern::

    >>> w = Wild('w1')
    >>> collect(a*x**y - b*x**y, w**y)
    x**y*(a - b)

It is also possible to work with symbolic powers, although it has more
complicated behavior, because in this case power's base and symbolic part
of the exponent are treated as a single symbol::

    >>> collect(a*x**c + b*x**c, x)
    a*x**c + b*x**c
    >>> collect(a*x**c + b*x**c, x**c)
    x**c*(a + b)

However if you incorporate rationals to the exponents, then you will get
well known behavior::

    >>> collect(a*x**(2*c) + b*x**(2*c), x**c)
    x**(2*c)*(a + b)

Note also that all previously stated facts about :func:`collect` function
apply to the exponential function, so you can get::

    >>> from sympy import exp
    >>> collect(a*exp(2*x) + b*exp(2*x), exp(x))
    (a + b)*exp(2*x)

If you are interested only in collecting specific powers of some symbols
then set ``exact`` flag to True::

    >>> collect(a*x**7 + b*x**7, x, exact=True)
    a*x**7 + b*x**7
    >>> collect(a*x**7 + b*x**7, x**7, exact=True)
    x**7*(a + b)

If you want to collect on any object containing symbols, set
``exact`` to None:

    >>> collect(x*exp(x) + sin(x)*y + sin(x)*2 + 3*x, x, exact=None)
    x*exp(x) + 3*x + (y + 2)*sin(x)
    >>> collect(a*x*y + x*y + b*x + x, [x, y], exact=None)
    x*y*(a + 1) + x*(b + 1)

You can also apply this function to differential equations, where
derivatives of arbitrary order can be collected. Note that if you
collect with respect to a function or a derivative of a function, all
derivatives of that function will also be collected. Use
``exact=True`` to prevent this from happening::

    >>> from sympy import Derivative as D, collect, Function
    >>> f = Function('f') (x)

    >>> collect(a*D(f,x) + b*D(f,x), D(f,x))
    (a + b)*Derivative(f(x), x)

    >>> collect(a*D(D(f,x),x) + b*D(D(f,x),x), f)
    (a + b)*Derivative(f(x), (x, 2))

    >>> collect(a*D(D(f,x),x) + b*D(D(f,x),x), D(f,x), exact=True)
    a*Derivative(f(x), (x, 2)) + b*Derivative(f(x), (x, 2))

    >>> collect(a*D(f,x) + b*D(f,x) + a*f + b*f, f)
    (a + b)*f(x) + (a + b)*Derivative(f(x), x)

Or you can even match both derivative order and exponent at the same time::

    >>> collect(a*D(D(f,x),x)**2 + b*D(D(f,x),x)**2, D(f,x))
    (a + b)*Derivative(f(x), (x, 2))**2

Finally, you can apply a function to each of the collected coefficients.
For example you can factorize symbolic coefficients of polynomial::

    >>> f = expand((x + a + 1)**3)

    >>> collect(f, x, factor)
    x**3 + 3*x**2*(a + 1) + 3*x*(a + 1)**2 + (a + 1)**3

.. note:: Arguments are expected to be in expanded form, so you might have
          to call :func:`~.expand` prior to calling this function.

See Also
========

collect_const, collect_sqrt, rcollect
c                     U R                   =(       d2    U * R                   =(       d    [        U R                  [        5      5      $ N)	is_Symbolboolatomsr   xs    N/var/www/auris/envauris/lib/python3.13/site-packages/sympy/simplify/radsimp.py<lambda>collect.<locals>.<lambda>   s1    Q[[ aRNN d	7     T)binary)funcevaluateexactdistribute_order_termN   c              3   f   >#    U  H&  oR                   =(       a    UR                  T;   v   M(     g 7fr'   )is_Powbase).0isymss     r-   	<genexpr>collect.<locals>.<genexpr>   s#     @%QXX0!&&D.0%s   .1Fc                 :   / nU  H  u  p#pEUb!  Uu  pg[        U5       H  n[        X&5      nM     UcB  U[        R                  L a  UR	                  U5        MS  UR	                  [        X#5      5        Mo  UR	                  [        X#U-  5      5        M     [        U6 $ r'   )ranger   r   Oneappendr   r   )	termsproducttermratsymderivvarorder_s	            r-   make_expression collect.<locals>.make_expression   s    %*!Ds "
uA%d0D & {!%%<NN4(NN3t>2s4S12 &+ G}r0   c                   ^ U R                   U R                  S   Sp2nU R                  SS   H  nXB:X  a  US-  nM  [        S5      e   [        U[        5      (       a  UR                  S   m[        U4S jUR                   5       5      (       a  [        S5      eTU:X  a$  UR                   U[        UR                  5      -   p1OO[        U[        5      (       a  M  X[        U5      44$ )Nr   r6   z(Improve MV Derivative support in collectc              3   ,   >#    U  H	  oT:g  v   M     g 7fr'    )r:   ss0s     r-   r=   4collect.<locals>.parse_derivative.<locals>.<genexpr>   s     3Nq7Ns   )expr	variablesNotImplementedError
isinstancer   anylenr   )rH   rT   rG   rJ   rQ   rR   s        @r-   parse_derivative!collect.<locals>.parse_derivative   s     !::uq'915$Ax
)>@ @	 % z**"B3DNN333)>@ @ Sy"iiT^^1D)De z** 8E?+++r0   c                   > [         R                  Sp!U SpCU R                  (       Ga"  [        U R                  [
        5      (       a  T" U R                  5      u  p4OU R                  nU R                  [         R                  :X  a_  U R                  nUR                  (       a  [         R                  UpGO&UR                  (       a  UR                  SS9u  pg[        U5      UpOU R                  R                  (       a  U R                  nOU R                  R                  5       u  pgUR                  (       a  Xgp!OU R                  nO[        U [        5      (       a^  U R                  nUR                  (       a  [         R                  UpONUR                  (       a  UR                  SS9u  pg[        U5      UpO[        U [
        5      (       a
  T" U 5      u  p4X1X$4$ )a  Parses expression expr and outputs tuple (sexpr, rat_expo,
sym_expo, deriv)
where:
 - sexpr is the base expression
 - rat_expo is the rational exponent that sexpr is raised to
 - sym_expo is the symbolic exponent that sexpr is raised to
 - deriv contains the derivatives of the expression

 For example, the output of x would be (x, 1, None, None)
 the output of 2**x would be (2, 1, x, None).
NT)rational)r   rA   r8   rW   r9   r   Exp1r   is_Rationalis_Mulas_coeff_Mul	is_Number)	rT   rat_exposym_exposexprrH   argcoefftailrZ   s	           r-   
parse_termcollect.<locals>.parse_term  sR    UUD(Tu;;;$))Z00/		:u		yyAFF"hh??&'ffc8ZZ"%"2"2D"2"AKE&)$i8##88"hh335??).h#xxHc""((C"#&&#x!...="%d)Uxj))+D1LE//r0   c                   > [         R                  " U5      n[        U 5      [        U5      :  a  gU Vs/ s H  nT" U5      PM     nnU SS n / SSpTnU H  u  p&pxUR                  (       a  US:X  a  Uc  M"  [	        [        U 5      5       H  n	X	   c  M
  X	   u  ppUb  SnU
R                  U5      c  M*  X:X  d  Uc  M4  Uc  M9  UR                  U5      c  MM  TSL a  X-  nUc  UnOXN:w  a  SnOXk:w  d  X:w  a  Mp  UR                  X	   5        SX	'     M       g   U  Vs/ s H  o(       d  M  UPM     snX4U4$ s  snf s  snf )zParse terms searching for a pattern.
Terms is a list of tuples as returned by parse_terms;
Pattern is an expression treated as a product of factors.
NFr6   T)r   	make_argsrY   rb   r@   matchrB   )rC   patternelemelemscommon_expo	has_derive_rate_syme_ordjrE   t_ratt_symt_ordexpo_fr4   ri   s                   r-   parse_expression!collect.<locals>.parse_expression6  sc   
 --(u:G$ 4;<GDz$'GG<!HE,.e	E-4)U>>eqjU]s5z*Ax' 05-D
 ($(	

4(4"^u/@!-!KK.: E> $)=D*2.2
 $/#623K  %~ ( UX.#'S +Z  g .5j "'-2"B-u9LLu =t .s   D9
D>-D>r   )deep)split_1z%Can not collect noncommutative symbol   rP   )0r   r#   r$   dictzipr   r   getcollectsubsitemsrW   xreplacesetr
   rl   has_freer`   add_new_rawargsas_coeff_mulalllistr   r   r3   is_AddgetOr2   argsr8   r9   r   r   r   hasremoveOr   r   Zeroargs_cncr   reversedis_commutativeAttributeErrorrB   r   rA   )0rT   r<   r2   r3   r4   r5   r;   condrK   nonsymsrepsrQ   rvkvurep_symsgsimplerL   r|   oarE   b
order_termsumma	collecteddislikedrD   cncr   rC   small_firstsymbolresultrp   rq   rr   margsro   eindexkeyvalrZ   ri   s0    `  `                                         @@r-   r   r      s   l 4=D)1$dV!CE!C1GAJ!CEDDdD.JAC7!K7a%"9+a."97!KLM(,-1-TYYt_d"79 "&.."d##;;t$$ !#
, * HHQN++D11::d3CC *, , }t$A::t$T	88		! NNANND$9!$<=D=IIaL % @%@@d75>**4"79 9 $--&,400dFMP ;;		 qA99!YY2&!q& OGAtT48MN&2 3567D [[99 II'%D dD$7LM%' ( ( [[		4tU4IKAq$((##6:;dae,d;DJYY[
!~~t$!
||~7:}}T7JK7J!qu-7JEK%d+QVVxI   /2GAJ"$(,-1A-F&*--+Xe_-"-o%eV4F!,,()PQQ7=4uk9 !E %7? $QA $QQASa!_5 !&  KE+E2E)/%*@uM)%e<% ''.9 > HK N )2(9:(9CG(9I:qvv#	!%%!)HC :-IcN * +4??+<>+<xsCCcN+< 	 > Y__->?->cSW->?@@O	 F "L- /,H2' < L .H ;> @sS   Y	%YYY(<Y
Y$Y$Y)9Y.+Y3Y8?Y=8Z0Z	
c           	          U R                   (       d  U R                  " U6 (       d  U $ U R                  " U R                   Vs/ s H  n[	        U/UQ76 PM     sn6 n U R
                  (       a  [        X5      $ U $ s  snf )a!  
Recursively collect sums in an expression.

Examples
========

>>> from sympy.simplify import rcollect
>>> from sympy.abc import x, y

>>> expr = (x**2*y + x*y + x + y)/(x + y)

>>> rcollect(expr, y)
(x + y*(x**2 + x + 1))/(x + y)

See Also
========

collect, collect_const, collect_sqrt
)is_Atomr   	__class__r   rcollectr   r   )rT   varsrf   s      r-   r   r     sb    ( ||488T?~~		J	 4t 4	JK;;4&&K  Ks   A:c                    Uc  [         R                  nU R                  5       u  p [        5       n[        R
                  " U 5       H  nUR                  5       S    H  nUR                  (       d  M  UR                  (       a5  UR                  R                  (       a  UR                  R                  S:X  d  U[        R                  L d  Mq  UR                  U5        M     M     [        U /UQ7SS06nX:g  nU(       d  Sn[!        [#        [        R
                  " U5      5      5      n	[%        U	5       H  u  pUR                  5       u  pU Hc  nUR                  (       a5  UR                  R                  (       a  UR                  R                  S:X  d  U[        R                  L d  M^  US-  n  O   X==   U-  ss'   M     U(       d  U(       d	  [	        U	6 /n	['        U	5      U4$ X&-  $ )a  Return expr with terms having common square roots collected together.
If ``evaluate`` is False a count indicating the number of sqrt-containing
terms will be returned and, if non-zero, the terms of the Add will be
returned, else the expression itself will be returned as a single term.
If ``evaluate`` is True, the expression with any collected terms will be
returned.

Note: since I = sqrt(-1), it is collected, too.

Examples
========

>>> from sympy import sqrt
>>> from sympy.simplify.radsimp import collect_sqrt
>>> from sympy.abc import a, b

>>> r2, r3, r5 = [sqrt(i) for i in [2, 3, 5]]
>>> collect_sqrt(a*r2 + b*r2)
sqrt(2)*(a + b)
>>> collect_sqrt(a*r2 + b*r2 + a*r3 + b*r3)
sqrt(2)*(a + b) + sqrt(3)*(a + b)
>>> collect_sqrt(a*r2 + b*r2 + a*r3 + b*r5)
sqrt(3)*a + sqrt(5)*b + sqrt(2)*(a + b)

If evaluate is False then the arguments will be sorted and
returned as a list and a count of the number of sqrt-containing
terms will be returned:

>>> collect_sqrt(a*r2 + b*r2 + a*r3 + b*r5, evaluate=False)
((sqrt(3)*a, sqrt(5)*b, sqrt(2)*(a + b)), 3)
>>> collect_sqrt(a*sqrt(2) + b, evaluate=False)
((b, sqrt(2)*a), 1)
>>> collect_sqrt(a + b, evaluate=False)
((a + b,), 0)

See Also
========

collect, collect_const, rcollect
r   r   NumbersFr6   )r   r3   as_content_primitiver   r
   rl   r   	is_numberr8   r   r_   qr   ImaginaryUnitr   collect_constr   r   	enumeratetuple)rT   r3   rg   r   r   mdhitnradr   r;   r   r   cis                 r-   collect_sqrtr     sj   R $-- ++-KE5D]]4 aA{{{HH!2!2quuww!|(	 ! ! 	d1T151A
)CGCMM!,-.dODAJJLEA99!3!3Aaoo-AID  GuG $ tJ<DT{D  7Nr0   c                 b   ^ S mU R                  S U4S j5      R                  S U4S j5      $ )a$  Return ``expr`` with arguments of multiple Abs in a term collected
under a single instance.

Examples
========

>>> from sympy.simplify.radsimp import collect_abs
>>> from sympy.abc import x
>>> collect_abs(abs(x + 1)/abs(x**2 - 1))
Abs((x + 1)/(x**2 - 1))
>>> collect_abs(abs(1/x))
Abs(1/x)
c                    U R                  5       u  p/ n/ nU H  n[        U[        5      (       a   UR                  UR                  S   5        M8  [        U[
        5      (       aq  [        UR                  [        5      (       aR  UR                  R                  (       a7  UR                  UR                  R                  S   UR                  -  5        M  UR                  U5        M     [        U5      S:  a  [        S U 5       5      (       d  U $ [        U6 n[        U5      nU/nUR                  U5        UR                  [        5      (       d  UR                  U5        [        U6 $ [        U[        5      (       d
  [        USS9nXxS'   [        U5        UR                  U5        [        R                  " X(       + S9$ )Nr   r   c              3   |   #    U  H2  n[        U[        5      (       d  M  UR                  R                  v   M4     g 7fr'   )rW   r   r   is_negative)r:   r;   s     r-   r=   ,collect_abs.<locals>._abs.<locals>.<genexpr>^  s'     !UQ*QPSBT"3!%%"3"3Qs   <<Fr3   )r   )r   rW   r    rB   r   r   r9   r   is_realrY   rX   r   extendr   r   
_from_args)	mulr   r   r   r   r;   absargAr   s	            r-   _abscollect_abs.<locals>._absS  sB   A!S!!#As##
1663(?(?AEEMMQ./  q6A:c!UQ!UUUJaKsAuuSzzKKO:!S!!FU+AQB~~d6::r0   c                 "    [        U [        5      $ r'   )rW   r   r+   s    r-   r.   collect_abs.<locals>.<lambda>p  s    *Q$r0   c                    > T" U 5      $ r'   rP   r,   r   s    r-   r.   r   q  s	    $q'r0   c                 "    [        U [        5      $ r'   )rW   r   r+   s    r-   r.   r   r  s    jC(r0   c                    > T" U 5      $ r'   rP   r   s    r-   r.   r   s  s	    d1gr0   )replace)rT   r   s    @r-   collect_absr   E  s1    ;8 <<$"7(r0   )r   c          
        ^^ U R                   (       d  U $ SnU(       da  Sn[        5       nU R                   HD  n[        R                  " U5       H'  nUR
                  (       d  M  UR                  U5        M)     MF     O[        U5      nU(       d$  U Vs/ s H  ofR                  (       a  M  UPM     nn[        [        U5      5      nU GH  n[        [        5      n[        U5      n[        R                  " U 5       H  n[        U5      n	U	R                  U5      u  pUR                  (       ad  U	R                   R#                  5       mU
R                   m[%        UU4S jT 5       5      (       d#  Xv   R'                  U
R)                  5       5        M  U[*        R,                     R'                  U5        M     / nSnSn[        U5       H  nX   nU[*        R,                  L a  UR/                  U5        M-  [1        U5      S:  a(  [        U6 nSnU(       a  X`:w  a  UR'                  U5        OUS   nU(       a?  UR                  (       a.  UR                   (       a  UR'                  [3        XSS95        SnM  UR'                  X-  5        M     U(       d  GM  U(       a	  [5        U6 n O[        U6 n U R                   (       a  GM    U $    U $ s  snf )a  A non-greedy collection of terms with similar number coefficients in
an Add expr. If ``vars`` is given then only those constants will be
targeted. Although any Number can also be targeted, if this is not
desired set ``Numbers=False`` and no Float or Rational will be collected.

Parameters
==========

expr : SymPy expression
    This parameter defines the expression the expression from which
    terms with similar coefficients are to be collected. A non-Add
    expression is returned as it is.

vars : variable length collection of Numbers, optional
    Specifies the constants to target for collection. Can be multiple in
    number.

Numbers : bool
    Specifies to target all instance of
    :class:`sympy.core.numbers.Number` class. If ``Numbers=False``, then
    no Float or Rational will be collected.

Returns
=======

expr : Expr
    Returns an expression with similar coefficient terms collected.

Examples
========

>>> from sympy import sqrt
>>> from sympy.abc import s, x, y, z
>>> from sympy.simplify.radsimp import collect_const
>>> collect_const(sqrt(3) + sqrt(3)*(1 + sqrt(2)))
sqrt(3)*(sqrt(2) + 2)
>>> collect_const(sqrt(3)*s + sqrt(7)*s + sqrt(3) + sqrt(7))
(sqrt(3) + sqrt(7))*(s + 1)
>>> s = sqrt(2) + 2
>>> collect_const(sqrt(3)*s + sqrt(3) + sqrt(7)*s + sqrt(7))
(sqrt(2) + 3)*(sqrt(3) + sqrt(7))
>>> collect_const(sqrt(3)*s + sqrt(3) + sqrt(7)*s + sqrt(7), sqrt(3))
sqrt(7) + sqrt(3)*(sqrt(2) + 3) + sqrt(7)*(sqrt(2) + 2)

The collection is sign-sensitive, giving higher precedence to the
unsigned values:

>>> collect_const(x - y - z)
x - (y + z)
>>> collect_const(-y - z)
-(y + z)
>>> collect_const(2*x - 2*y - 2*z, 2)
2*(x - y - z)
>>> collect_const(2*x - 2*y - 2*z, -2)
2*x - 2*(y + z)

See Also
========

collect, collect_sqrt, rcollect
FTc              3      >#    U  H;  nUT;   =(       a*    TU   R                   =(       a    TU   R                   (       + v   M=     g 7fr'   )
is_Integer)r:   r   fnowfwass     r-   r=    collect_const.<locals>.<genexpr>  sJ      :48q 9 +a);); +Q**A+ +48s   AAr6   r   )sign)r   r   r   r   rl   r   r   r   rb   r   r   r   r   r
   divis_onefactorscopyrX   rB   as_exprr   rA   r   rY   r   r	   )rT   r   r   recurser   r   r   rC   Fvfr   rr   r   unevalr   r   r   s                   @@r-   r   r   v  s(   | ;;GuA]]1%;;;HHQK & 
 t}34a{{43DD!QZt$A
A559DAxx
 yy~~'yy :48: : :HOOAIIK0!%%L" %  AAAEEzA1vzGqyKKNaD
 1;;188K489AC +  . 3'.Dz;;;Kk j Kq 4s   K.Kc                   ^^^^^^^ SSK Jm  SSKJn  [	        S5      mU4S jmSU4S jjmUUUUUU4S jm[        U T5      (       d3  U R                  " U R                   Vs/ s H  n[        UTTS	9PM     sn6 $ U R                  5       u  pPU R                  5       n [        U 5      n[        T" U 5      5      u  pxXgU4:w  Gao  UR                  (       dx  Xx4n	U" USS
9nU" USS
9n[        [        USU-  5      5      n
[        U
R                  R!                  5        VVs/ s H	  u  pX-  PM     snn6 n
[        U
5      u  pxXgU4:X  a  U	u  px[#        U5      nUR$                  (       d  UR&                  (       a  [        [)        [        USU-  5      5      5      u  pUR$                  (       d"  UR+                  5       UR+                  5       ::  ac  X4 Vs/ s H
  o" U5      PM     snu  pxUR,                  (       a7  UR                  S   R$                  (       a  UR                  " UR                  6 nU[        USU-  5      -   $ s  snf s  snnf s  snf )a	  
Rationalize the denominator by removing square roots.

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

The expression returned from radsimp must be used with caution
since if the denominator contains symbols, it will be possible to make
substitutions that violate the assumptions of the simplification process:
that for a denominator matching a + b*sqrt(c), a != +/-b*sqrt(c). (If
there are no symbols, this assumptions is made valid by collecting terms
of sqrt(c) so the match variable ``a`` does not contain ``sqrt(c)``.) If
you do not want the simplification to occur for symbolic denominators, set
``symbolic`` to False.

If there are more than ``max_terms`` radical terms then the expression is
returned unchanged.

Examples
========

>>> from sympy import radsimp, sqrt, Symbol, pprint
>>> from sympy import factor_terms, fraction, signsimp
>>> from sympy.simplify.radsimp import collect_sqrt
>>> from sympy.abc import a, b, c

>>> radsimp(1/(2 + sqrt(2)))
(2 - sqrt(2))/2
>>> x,y = map(Symbol, 'xy')
>>> e = ((2 + 2*sqrt(2))*x + (2 + sqrt(8))*y)/(2 + sqrt(2))
>>> radsimp(e)
sqrt(2)*(x + y)

No simplification beyond removal of the gcd is done. One might
want to polish the result a little, however, by collecting
square root terms:

>>> r2 = sqrt(2)
>>> r5 = sqrt(5)
>>> ans = radsimp(1/(y*r2 + x*r2 + a*r5 + b*r5)); pprint(ans)
    ___       ___       ___       ___
  \/ 5 *a + \/ 5 *b - \/ 2 *x - \/ 2 *y
------------------------------------------
   2               2      2              2
5*a  + 10*a*b + 5*b  - 2*x  - 4*x*y - 2*y

>>> n, d = fraction(ans)
>>> pprint(factor_terms(signsimp(collect_sqrt(n))/d, radical=True))
        ___             ___
      \/ 5 *(a + b) - \/ 2 *(x + y)
------------------------------------------
   2               2      2              2
5*a  + 10*a*b + 5*b  - 2*x  - 4*x*y - 2*y

If radicals in the denominator cannot be removed or there is no denominator,
the original expression will be returned.

>>> radsimp(sqrt(2)*x + sqrt(2))
sqrt(2)*x + sqrt(2)

Results with symbols will not always be valid for all substitutions:

>>> eq = 1/(a + b*sqrt(c))
>>> eq.subs(a, b*sqrt(c))
1/(2*b*sqrt(c))
>>> radsimp(eq).subs(a, b*sqrt(c))
nan

If ``symbolic=False``, symbolic denominators will not be transformed (but
numeric denominators will still be processed):

>>> radsimp(eq, symbolic=False)
1/(a + b*sqrt(c))

r   )Expr)signsimpza:d A:Dc                   > Tu  pp4pVpx[        U 5      S:X  ag  [        [        [        XQXb/U  V	V
s/ s H  o  H  oPM     M     sn
n	5      5      5      n[	        U5      U-  [	        U5      U-  -
  R                  U5      $ [        U 5      S:X  a  [        [        [        XQXbXs/U  V	V
s/ s H  o  H  oPM     M     sn
n	5      5      5      n[	        U5      U-  [	        U5      U-  -   [	        U5      U-  -
  S[	        U5      -  [	        U5      -  U-  U-  XQS-  -  -
  XbS-  -  -
  XsS-  -  -   -  R                  U5      $ [        U 5      S:X  Ga  [        [        [        XQXbXsX/U  V	V
s/ s H  o  H  oPM     M     sn
n	5      5      5      n[	        U5      U-  [	        U5      U-  -   [	        U5      U-  -
  [	        U5      U-  -
  S[	        U5      -  [	        U5      -  U-  U-  XQS-  -  -
  XbS-  -  -
  S[	        U5      -  [	        U5      -  U-  U-  -
  XsS-  -  -   XS-  -  -   -  S[	        U5      -  [	        U5      -  [	        U5      -  [	        U5      -  U-  U-  U-  U-  US-  US-  -  -   SU-  U-  US-  -  US-  -  -
  SU-  U-  US-  -  US-  -  -
  SU-  U-  US-  -  US-  -  -
  US-  US-  -  -   SU-  U-  US-  -  US-  -  -
  SU-  U-  US-  -  US-  -  -
  US-  US-  -  -   SU-  U-  US-  -  US-  -  -
  US-  US-  -  -   -  R                  U5      $ [        U 5      S:X  a  [	        U S   S   5      $ [        es  sn
n	f s  sn
n	f s  sn
n	f )Nr         ir6   r   )rY   r   r   r   r   r   rV   )rtermsr   r   r   r   r   BCDr;   rv   r   r<   s               r-   _numradsimp.<locals>._numN  s    "&aA!v;!S!6/M6a1a16/MNOPDGAIQ	!88D>2v;!S!a!35SAQRAaQRa5STUVD!WQYa"T!WQY.47471B11DQ1Fa41OdF2!tV2 &htn- [AS!aA!9v;Yv!WXRSAWXAv;YZ[\D!WQYa*T!WQY6aBQtAwYtTUwEVWXEXYZEZqD&FT6F"$%d1gId1g$5a$7$9F:<=dFFCQ$F DGDG+DG3DG;A=a?A!Cad1a4iO!Aad
1a4 "#A#a%1*QT/245aCE!Q$Jq!tODFGd1a4iP!Aad
1a4 "#A#a%1*QT/245qDAI>@A!Aad
1a4P 1QT	
 %HTN+ [Aq	!%%%%' 0N 6T
 <Zs   M
&M'MFc                   > U R                   (       d  gU R                  nUR                  (       a  UR                  S:X  d  T(       a  [	        U5      S:X  a  gU(       ag  SnUR                  (       a  UR                  nO%T(       a  [	        U5      n U R
                  (       a  U nUS:w  a  [        US5      R
                  (       a  gg)NFr   Tr6   )r8   r   r_   r   denomr   r   )r   log2r   r   symbolics       r-   ispow2radsimp.<locals>.ispow2h  s    xxEE==QSSAXeAh!mA}}CC!H<<AAv#a)..r0   c                 ^  > SSK Jn  U R                  (       a  U $ [        U T5      (       d0  U R                  " U R
                   Vs/ s H  nT" U5      PM     sn6 $ [        U 5      u  p4UR                  (       a  UR                  (       a  U $ UR                  (       dE  UR                  " UR
                   Vs/ s H  nT" U5      PM     sn6 n[        UT" SU-  5      5      $ U[        R                  La  [        UT" SU-  5      5      $ UR                  (       a,  [        UR
                   Vs/ s H  nT" SU-  5      PM     sn6 $ T(       d  UR                  (       a  U $ T" U5      (       aE  [        [        UR                  5      5      [        UR                   5      -  nXT:w  a  T" SU-  5      $ OiUR"                  (       aX  UR                   R$                  (       d  UR                  R&                  (       a"  T" SUR                  -  5      UR                   -  $ UR(                  (       d@  T" U5      (       d3  SUR                  " UR
                   Vs/ s H  nT" U5      PM     sn6 -  $ Sn[+        U5      nUR                  (       a  SU-  $ UR,                  (       a4  U" U5      nUR.                  (       a  UR1                  U5      (       a  SU-  $  [3        [4        5      n[6        R8                  " U5       H  n	/ n
/ n[:        R8                  " U	5       H  nT" USS9(       aV  U
R=                  UR                   [        R>                  L a  UR                  OUR                  SUR                   -  -  5        Me  U[        R@                  L a!  U
R=                  [        RB                  5        M  UR=                  U5        M     U[E        [G        U
5      5         R=                  [;        U6 5        M     [5        [G        [5        URI                  5       5      5      5      nU VVs/ s H  u  p[;        U6 [7        U6 4PM     nnn[K        U5      US   S   [        R                  L a  SOS-
  nUS:  a  GO UT:  a  SnO[K        U5      S:  as  [M        S	 U 5       5      (       aY  [O        [        R                  [6        RP                  " U VVs/ s H  u  nn[        U5      U-  PM     snn5      5      u  nnUU-  nOSnOuSS
K)J*nJ+n  U" T" U5      5      nUU-  nUU-  nU" [+        U5      TS9nURY                  [        RZ                  [\        [^        5      (       a  U $ UR                  (       a  OGM  U(       d  U $ [        USU-  5      $ s  snf s  snf s  snf s  snf s  snnf s  snnf )Nr   )	nsimplifyr6   T)r   r   Fr   c              3   j   #    U  H)  u  pUR                   =(       a    US -  R                  v   M+     g7f)r   N)r   r_   )r:   r,   ys      r-   r=   *radsimp.<locals>.handle.<locals>.<genexpr>  s(     Nvtqq||:A(:(::vs   13)powsimp	powdenest)force)0sympy.simplify.simplifyr  r   rW   r2   r   fractionr   r   rA   r`   free_symbolsr"   r   r9   numerr   r8   
is_integeris_positiver   r   r   rb   equalsr   r   r
   rl   r   rB   Halfr   NegativeOner   r   r   rY   r   rad_rationalizer   sympy.simplify.powsimpr  r  r   r   r   r   )rT   r  r   nr   d2keep_dr   r   p2otherr;   r   rv   r   r,   r  ndr  r  numr   r   handler   	max_termsr   s                         r-   r  radsimp.<locals>.handlez  sS    	6<<KD$''99$))<)Qvay)<==~99KAFF3FqF34A#Avac{33aee^#Avac{33XX#166%B6afQqSk6%BCC ANNK!99DL)5<7Bwad|# XX155++qvv/A/A!AFF(#QUU**F1IIQVV8AfQi8999  QK 99Q3J ;;1B||		!t#D)I]]1%q)Aad+		AEEQVVO!&&!AEE'ARSaoo-		!--0Q * %,-44S%[A & '$y'8"9:;F5;<VTQsAwQ(VF<v;vay|quu'<!!DDax	! 6{Q NvNNN+AEE3>>/56vtq!av648 9EBGA !DA$v,'CHAHA(1+X6AuuQVVS#&&yya d K1Q3''G = 4
 &C 9F =$ 7s$   V8V/VV
'V#V))r   r  r   r6   F)sympy.core.exprr   r	  r   r   rW   r2   r   radsimpas_coeff_Addnormalr
  r   r   r   r   r   r   rb   r   r   	count_opsr`   )rT   r   r  r   r   rg   oldr  r   wasur   r   n2r  r;   r   r   r  r   r<   s    ``             @@@@@r-   r!  r!    s   X %09D&4$l( l(\ dD!!yyW[W`W`aW`RS71x9MW`abb##%KE;;=D
4.CF4L!DA
!f}yy&CU+AU+A(AaC01A AIIOO4E"F4EDA144E"FGAA;DA!f}qM;;!((i(8AaC(@ABFB||!++- ?.0X6XX688q	 3 3A#Aqs+++1 b #G 7s   #I I

Ic                     UR                   (       d  X4$ [        U5      u  p#nU[        U5      -  n[        X4-
  U -  5      n [        US-  US-  -
  5      n[	        X5      $ )a8  
Rationalize ``num/den`` by removing square roots in the denominator;
num and den are sum of terms whose squares are positive rationals.

Examples
========

>>> from sympy import sqrt
>>> from sympy.simplify.radsimp import rad_rationalize
>>> rad_rationalize(sqrt(3), 1 + sqrt(2)/3)
(-sqrt(3) + sqrt(6)/3, -7/9)
r   )r   split_surdsr   r   r  )r  denr   r   r   s        r-   r  r    s^     ::x#GA!	$q'	A
AE3;
C
1a4!Q$;
C3$$r0   c                    [        U 5      n / / p2[        R                  " U 5       GH  nUR                  (       Gag  UR                  (       d  [        U[        5      (       Ga@  UR                  5       u  pVUR                  (       a  U[        R                  L a  UR                  U5        M  U(       aE  UR                  5       (       a  UR                  [        XV* 5      5        M  UR                  U5        M  UR                  [        XV* 5      5        M  UR                  (       a  UR                  U5        GM  U(       dN  UR                  (       a=  UR!                  5       u  pxUS:w  a  UR                  U5        UR                  U5        GMi  UR                  U5        GM}  UR"                  (       aZ  UR$                  (       dI  UR&                  S:w  a  UR                  UR&                  5        UR                  UR(                  5        GM  UR                  U5        GM     [        USU(       + 06[        USU(       + 064$ )a  Returns a pair with expression's numerator and denominator.
If the given expression is not a fraction then this function
will return the tuple (expr, 1).

This function will not make any attempt to simplify nested
fractions or to do any term rewriting at all.

If only one of the numerator/denominator pair is needed then
use numer(expr) or denom(expr) functions respectively.

>>> from sympy import fraction, Rational, Symbol
>>> from sympy.abc import x, y

>>> fraction(x/y)
(x, y)
>>> fraction(x)
(x, 1)

>>> fraction(1/y**2)
(1, y**2)

>>> fraction(x*y/2)
(x*y, 2)
>>> fraction(Rational(1, 2))
(1, 2)

This function will also work fine with assumptions:

>>> k = Symbol('k', negative=True)
>>> fraction(x * y**k)
(x, y**(-k))

If we know nothing about sign of some exponent and ``exact``
flag is unset, then the exponent's structure will
be analyzed and pretty fraction will be returned:

>>> from sympy import exp, Mul
>>> fraction(2*x**(-y))
(2, x**y)

>>> fraction(exp(-x))
(1, exp(x))

>>> fraction(exp(-x), exact=True)
(exp(-x), 1)

The ``exact`` flag will also keep any unevaluated Muls from
being evaluated:

>>> u = Mul(2, x + 1, evaluate=False)
>>> fraction(u)
(2*x + 2, 1)
>>> fraction(u, exact=True)
(2*(x  + 1), 1)
r6   r3   )r   r   rl   r   r8   rW   r   as_base_expr   r   r  rB   is_constantr   r  r`   as_numer_denomr_   r   pr   )	rT   r4   r  r   rE   r   exr  r   s	            r-   r
  r
    st   p 4=Dr5d#DKK:dC3H3H$$&EA~~&LLO~~''SC[1T*LLQ-T"ryy**,6LLOQT"doovv{TVV$LL LL7 $8 *E	*C,KU,KKKr0   c                     [        XS9S   $ )Nr4   r   r
  rT   r4   s     r-   r  r  u      D&q))r0   c                     [        XS9S   $ )Nr3  r6   r4  r5  s     r-   r   r   y  r6  r0   c                 *    U R                   " SSS0UD6$ )NfracTrP   )expand)rT   hintss     r-   fraction_expandr<  }  s    ;;*D*E**r0   c                 f    [        XR                  SS5      S9u  p#UR                  " SSS0UD6U-  $ )Nr4   Fr3  r  TrP   r
  r   r:  rT   r;  r   r   s       r-   numer_expandr@    s5    D		'5 9:DA88($(%(1,,r0   c                 d    [        XR                  SS5      S9u  p#X#R                  " SSS0UD6-  $ )Nr4   Fr3  r   TrP   r>  r?  s       r-   denom_expandrB    s3    D		'5 9:DAxx,d,e,,,r0   c                 .   [        U R                  [        S9nU Vs/ s H  o"R                  5       PM     nnU Vs/ s H   o"S   R                  (       d  M  US   S-  PM"     nnUR                  [        S9  [        U6 u  pVnUnU(       dK  [        U5      S:  a<  U Vs/ s H  o"U-  PM	     n	nU	 Vs/ s H  o"S:w  d  M
  UPM     n	n[        U	6 u  pnXZ-  n/ / pU H  u  pUR                  (       ae  UR                  [        R                  :X  aG  UR                  nX;   a!  UR                  U[        X-  5      -  5        Mf  UR                  X-  5        M{  UR                  X-  5        M     [        U6 n[        U6 nUUU4$ s  snf s  snf s  snf s  snf )a  
Split an expression with terms whose squares are positive rationals
into a sum of terms whose surds squared have gcd equal to g
and a sum of terms with surds squared prime with g.

Examples
========

>>> from sympy import sqrt
>>> from sympy.simplify.radsimp import split_surds
>>> split_surds(3*sqrt(3) + sqrt(5)/7 + sqrt(6) + sqrt(10) + sqrt(15))
(3, sqrt(2) + sqrt(5) + 3, sqrt(5)/7 + sqrt(10))
)r   r6   r   )sortedr   r   ra   r8   sort
_split_gcdrY   r   r   r  r9   rB   r   r
   )rT   r   r,   
coeff_mulssurdsr   b1b2g2b1ng1a1va2vr   rQ   s1r   r   s                     r-   r*  r*    s[    $))!12D,01Dq.."DJ1(8jaDKKWQqT1WjE8	JJ#J$E"IA2	
B#b'Q,BqsB(#Qaq#( #&T288Bx

1T"%[=)

13JJqsO  	S	AS	Aq!8O/ 28
  (s"   FFFF3	F Fc                      U S   nU/n/ nU SS  H:  n[        X5      nUS:X  a  UR                  U5        M'  UnUR                  U5        M<     XU4$ )a<  
Split the list of integers ``a`` into a list of integers, ``a1`` having
``g = gcd(a1)``, and a list ``a2`` whose elements are not divisible by
``g``.  Returns ``g, a1, a2``.

Examples
========

>>> from sympy.simplify.radsimp import _split_gcd
>>> _split_gcd(55, 35, 22, 14, 77, 10)
(5, [55, 35, 10], [22, 14, 77])
r   r6   N)r!   rB   )r   r   rI  rJ  r,   rM  s         r-   rF  rF    s`     	
!A
B	BqrUY7IIaLAIIaL  "9r0   )NNFTr'   )Tr   r  )Dcollectionsr   
sympy.corer   r   r   r   r   sympy.core.addr	   r
   sympy.core.assumptionsr   sympy.core.exprtoolsr   r   sympy.core.functionr   r   r   sympy.core.mulr   r   r   sympy.core.numbersr   r   r   sympy.core.parametersr   sympy.core.sortingr   r   sympy.core.symbolr   r   r   sympy.functionsr   r   r   $sympy.functions.elementary.complexesr    sympy.polysr!   sympy.simplify.sqrtdenestr"   sympy.utilities.iterablesr#   r$   r   r   r   r   r   r!  r  r
  r  r   r<  r@  rB  expand_numerexpand_denomexpand_fractionr*  rF  rP   r0   r-   <module>re     s    # 7 7 0 . 3 G G B B 1 1 3 8 2 2 * * 4  0 4
~B>L^.b (, EPC,L%,XLv**+-- !&Rr0   