
    \hX                       S r SSKJr  SSKJr  SSKJrJr  SSKJ	r	  SSK
Jr  SSKJr  SS	KJr  SS
KJr  SSKJrJrJrJrJr  SSKJr  SSKJrJr  SSKJr  SSKJr  SSK J!r!  SSK"J#r#J$r$  SSK%J&r&  SSK'J(r(J)r)J*r*J+r+J,r,  SSK-J.r.  \" SS9r/S r0S r1S&S jr2S'S jr3 " S S5      r4 " S S5      r5S(S jr6S)S  jr7S! r8S*S+S" jjr9S,S$ jr:S% r;g#)-z9Tools for manipulating of large commutative expressions.     )annotations   )Add)Mul_keep_coeff)Pow)Basic)Expr)expand_power_expsympify)RationalIntegerNumberIequal_valued)S)default_sort_keyorderedDummy)preorder_traversal)NonCommutativeExpression)TupleDict)
SYMPY_INTS)common_prefixcommon_suffix
variationsiterableis_sequence)defaultdictTpositivec                T    [        U [        [        45      =(       d    U R                  $ N)
isinstancer   float	is_Number)is    L/var/www/auris/envauris/lib/python3.13/site-packages/sympy/core/exprtools.py	_isnumberr,      s    a*e,-<<    c                  ^ U R                   (       d  gU * R                  (       a  [        U * 5      nUc  U$ U* $ U R                  (       Gd  U R	                  5       S   R
                  (       Gay  U nUR                  (       a'  UR                  (       a  [        S5      $ [        S5      $ UR                  (       a'  UR                  (       a  [        S5      $ [        S5      $ UR                  (       a]  UR                  (       a%  UR                  SL a  [        S5      $ [        S5      $ UR                  (       a  [        R                  $ [        $ UR                   (       aD  UR                  (       a  [        S5      $ UR                  (       a  [        R"                  $ [        * $ UR$                  (       d"  UR&                  (       d  UR(                  (       a  [        R*                  $ gU R,                  n[/        U5      S:X  Ga	  U R1                  5       (       Ga  S	S
KJn  S	SKJn  S	SKJn  UR?                  5       n[        U5      mT[        [        * 4;   a  [        R*                  mTGb  U RA                  U5      nUR
                  (       a  / n	O	 U" U5      n	U RE                  UT5      nURF                  (       a  [I        U4S jU	 5       5      (       a  URF                  (       a?  UR                  (       a.  U(       a  UR                  (       a  U$ [K        SSS9$ [K        SSS9$ URL                  (       a?  URN                  (       a.  U(       a  URN                  (       a  U$ [K        SSS9$ [K        SSS9$ gURL                  (       a  [I        U4S jU	 5       5      (       az  URF                  (       a,  URN                  (       a  U(       a
  [K        SSS9$ [K        SSS9$ URL                  (       a,  UR                  (       a  U(       a
  [K        SSS9$ [K        SSS9$ gU R	                  5       u  pSnUR
                  (       a  [        U5      nO(UR
                  (       d  [        U5      b  [        U5      nUb  UR                  (       d  URN                  (       ap  X-  nUR                  (       a
  [K        SSS9$ URF                  (       a
  [K        SSS9$ URN                  (       a
  [K        SSS9$ URL                  (       a
  [K        SSS9$ gU RQ                  5       u  nnSnUR1                  5       (       Gd:  UR	                  5       u  pUR
                  (       d  UR
                  (       d  gURR                  (       d  URT                  (       a  URV                  (       a  [I        S URY                  [Z        5       5       5      (       a  UR                  (       d  URN                  (       a  [        R                  n[\        R^                  " U5       H[  nUR
                  (       a  UU-  nM  0 nUR,                   H  n[        U5      UU'   UU   b  M      g   UURE                  U5      -  nM]     OU(       a  [a        S URY                  [Z        5       5       5      (       d  URL                  (       d  URF                  (       ak  [c        UR,                  5      n0 nU H=  n[        U5      nUc    gU=(       d    URF                  (       a  [        O[        * UU'   M?     URe                  U5      nUbv  X-   nURF                  (       a'  UR                  (       a  URE                  [        S	5      $ URL                  (       a(  URN                  (       a  URE                  [        S	5      $ ggg! U[B        4 a4    U" X5       V
s/ s H  oR                   (       d  M  U
PM     Os  sn
f n	n
 GNOf = f)aj  Return the value closest to 0 that ``self`` may have if all symbols
are signed and the result is uniformly the same sign for all values of symbols.
If a symbol is only signed but not known to be an
integer or the result is 0 then a symbol representative of the sign of self
will be returned. Otherwise, None is returned if a) the sign could be positive
or negative or b) self is not in one of the following forms:

- L(x, y, ...) + A: a function linear in all symbols x, y, ... with an
  additive constant; if A is zero then the function can be a monomial whose
  sign is monotonic over the range of the variables, e.g. (x + 1)**3 if x is
  nonnegative.
- A/L(x, y, ...) + B: the inverse of a function linear in all symbols x, y, ...
  that does not have a sign change from positive to negative for any set
  of values for the variables.
- M(x, y, ...) + A: a monomial M whose factors are all signed and a constant, A.
- A/M(x, y, ...) + B: the inverse of a monomial and constants A and B.
- P(x): a univariate polynomial

Examples
========

>>> from sympy.core.exprtools import _monotonic_sign as F
>>> from sympy import Dummy
>>> nn = Dummy(integer=True, nonnegative=True)
>>> p = Dummy(integer=True, positive=True)
>>> p2 = Dummy(integer=True, positive=True)
>>> F(nn + 1)
1
>>> F(p - 1)
_nneg
>>> F(nn*p + 1)
1
>>> F(p2*p + 1)
2
>>> F(nn - 1)  # could be negative, zero or positive
Nr         	      Fr   )
real_roots)roots)PolynomialErrorc              3  @   >#    U  H  oT-
  R                   v   M     g 7fr&   )is_nonpositive.0rx0s     r+   	<genexpr>"_monotonic_sign.<locals>.<genexpr>   s      ,G9EAR//   posTr#   nneg)nonnegativeneg)negativenpos)nonpositivec              3  @   >#    U  H  oT-
  R                   v   M     g 7fr&   )is_nonnegativer9   s     r+   r=   r>      s      .G9EAR//r?   c              3  r   #    U  H-  oR                   (       d  M  UR                  R                  v   M/     g 7fr&   )is_Powexp
is_Integerr:   ps     r+   r=   r>      s!     Glhh$AEE$$ls   77c              3  J   #    U  H  oR                   (       a  M  Uv   M     g 7fr&   )	is_numberrM   s     r+   r=   r>      s     >l++11ls   #	#)3is_extended_real	is_Symbol_monotonic_signis_Addas_numer_denomrP   is_primeis_oddr   is_compositeis_positiveis_even
is_integerr   One_epsis_extended_negativeNegativeOneis_zerois_extended_nonpositiveis_extended_nonnegativeZerofree_symbolslenis_polynomialsympy.polys.polytoolsr4   sympy.polys.polyrootsr5   sympy.polys.polyerrorsr6   popdiffNotImplementedErrorsubsrH   allr   r8   is_negativeas_coeff_Addis_MulrJ   is_rationalatomsr   r   	make_argsanylistxreplace)selfrvsfreer4   r5   r6   xdcurrentrootsr;   yndenvcaairepsrN   r*   r<   s                        @r+   rS   rS   !   s   J   	dU#Zr(bS(;;;4..03===::xxqz!qz!^^xxqz!qz!]]yy::&"1:%"1:%uu##yyr{"}}$u9911Q5N5N66M D
4yA~83>
A #BdTE]"VV~IIaL;;#%LV'1!} IIa$## ,G9E,G )G )G''AMM()1V5QU;VV#(T#BB''AMM()1V5QU;VV#(T#BB> = %%# .G9E.G +G +G''AMM#(#>>#(T#BB''AMM#(#>>#(T#BB& # &&(DAC{{%a([["1%1)!,CCOOsE== 66%% T::]] 66%% T:: DAqA??!q{{AHHGaggclGGG!--AmmA&<<GAA-a0DGAw ) RWWT]" ' 
>aggcl>>>ADTDTXYXhXh'DA#A&9AQ%5%5TD5!	 
 

1A}U774##774## !/	 e ,-@A V38;'U;aBTBT;'U'UVs$   ^* *_* ___*)_*c                   U R                  5       u  pUR                  (       aX  UR                  (       a@  UR                  (       d   [	        U[        SUR                  5      5      nUR                  nX4$ U Sp1 X4$ UR                  SS9u  p$U[        R                  L a  [	        X5      Sp1X4$ U[        R                  La9  [        [        SUR                  5      U5      n[	        X5      UR                  p1X4$ U Sp1X4$ )a+  
Decompose power into symbolic base and integer exponent.

Examples
========

>>> from sympy.core.exprtools import decompose_power
>>> from sympy.abc import x, y
>>> from sympy import exp

>>> decompose_power(x)
(x, 1)
>>> decompose_power(x**2)
(x, 2)
>>> decompose_power(exp(2*y/3))
(exp(y/3), 2)

r   T)rational)as_base_expr)   is_RationalrL   r   r   qrN   as_coeff_Mulr   r_   r\   r   )exprbaserK   etails        r+   decompose_powerr      s    &   "ID
}}??>>4!SUU!34A  7N A! 7N $$d$3	!--$or! 7N  x355148D$osuu! 7N A!7Nr-   c                ~    U R                  5       u  pUR                  (       d  [        U 5      u  p[        U5      nX4$ )as  
Decompose power into symbolic base and rational exponent;
if the exponent is not a Rational, then separate only the
integer coefficient.

Examples
========

>>> from sympy.core.exprtools import decompose_power_rat
>>> from sympy.abc import x
>>> from sympy import sqrt, exp

>>> decompose_power_rat(sqrt(x))
(x, 1/2)
>>> decompose_power_rat(exp(-3*x/2))
(exp(x/2), -3)

)r   r   r   r   )r   r   rK   exp_is       r+   decompose_power_ratr     s8    &   "ID??%d+en9r-   c                      \ rS rSrSrSrSS jrS rS r\	S 5       r
\	S	 5       rS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rSrg)Factorsi   z0Efficient representation of ``f_1*f_2*...*f_n``.)factorsgensNc                   [        U[        [        45      (       a  [        U5      n[        U[        5      (       a  UR
                  R                  5       nGO2US[        R                  4;   a  0 nGOU[        R                  L d  US:X  a"  [        R                  [        R                  0nGO[        U[        5      (       Ga	  Un0 nUS:  a$  [        R                  U[        R                  '   U* nU[        R                  La  UR                  (       d$  UR                  (       d  U[        R                  L a  [        R                  X'   GO=UR                  (       a^  UR                  S:w  a&  [        R                  U[!        UR                  5      '   [        R                  U[!        UR"                  5      '   GO[%        SU-  5      eGO[        U[&        5      (       a%  UR(                  (       d  U[        R                  0nGO[        U[*        5      (       Ga  UR-                  5       u  p4UR/                  [0        5      n[3        U5       H  nUR5                  [0        5        M     [7        [8        R:                  " U5      R=                  5       5      n[?        URA                  5       5       H  n[        U[B        5      (       d  M  [        U[         5      (       a  M1  [!        UR                  5      [!        UR"                  5      pX;   a  X   O[        R                  X   -   X'   X;   a  X   O[        R                  X   -
  X'   URE                  U5        M     U(       a.  URG                  [0        [        R                  5      U-   U[0        '   U(       a  [        R                  U[9        USS06'   GOUR                  5       nU V
s/ s H  o[0        L d  U
S;   d  M  U
PM     nn
U(       Ga  [        R                  nU H-  n
[I        X   5      (       d  M  XURE                  U
5      -  -  nM/     U[        R                  LGa@  URJ                  (       a  UR(                  OU/ GH  nU[        R                  L a  [        R                  X'   M+  U[0        L a  [        R                  U[0        '   MM  URL                  (       aE  URG                  URN                  [        R                  5      URP                  -   XRN                  '   M  [S        US5      (       a  [        R                  X'   M  [S        US5      (       a7  [        R                  X* '   [        R                  U[        R                  '   GM  [%        S	U-  5      e   Xl        [U        US
S5      nUc  [W        S5      e[Y        U" 5       5      U l-        gs  sn
f )a4  Initialize Factors from dict or expr.

Examples
========

>>> from sympy.core.exprtools import Factors
>>> from sympy.abc import x
>>> from sympy import I
>>> e = 2*x**3
>>> Factors(e)
Factors({2: 1, x: 3})
>>> Factors(e.as_powers_dict())
Factors({2: 1, x: 3})
>>> f = _
>>> f.factors  # underlying dictionary
{2: 1, x: 3}
>>> f.gens  # base of each factor
frozenset({2, x})
>>> Factors(0)
Factors({0: 1})
>>> Factors(I)
Factors({I: 1})

Notes
=====

Although a dictionary can be passed, only minimal checking is
performed: powers of -1 and I are made canonical.

Nr   r   z'Expected Float|Rational|Integer, not %sevaluateF)r   r   r   zunexpected factor in i1: %skeyszexpecting Expr or dictionary).r'   r   r(   r   r   r   copyr\   rc   r   r_   is_FloatrL   Infinityr   rN   r   r   
ValueErrorr	   argsr
   args_cnccountr   rangeremovedictr   
_from_argsas_powers_dictrv   r   r   rj   getr,   rq   rJ   r   rK   r   getattr	TypeError	frozensetr   )rx   r   r   r   ncr*   _frN   r   khandlei1r   r   s                  r+   __init__Factors.__init__%  s   > g
E233jGgw''oo**,Gquu%G'Q,vvquuoG((AG1u)*&B~::ajj!"GJ]] ssax01-,-MMGGACCL)$%NQR%RSS  ''&G&&$$&EA
A1X 3>>!,;;=>G',,.)a**:a3I3I"133<q01'*!&&GJ!VGJ01'*!&&GJ!VGJKKN * $[[AFF3a7
45EER0%01llnG ")CAFa7laFCUUA$WZ00 W[[^++B   QUU?(*		RWWt;-)*GJ!V)*GAJXX.5kk!&&!&&.IAEE.QGFFO)!Q//)*GJ)!R00*+%%GBK56UUGAMM2",-JQ-N"OO < w-<:;;df%	7 Ds   W#Wc                    [        [        U R                  R                  5       5      5      nU Vs/ s H  o R                  U   PM     nn[	        X45      $ s  snf r&   )tupler   r   r   hash)rx   r   r   valuess       r+   __hash__Factors.__hash__  sJ    WT\\..012+/04a,,q/40TN## 1s   Ac           
         SSR                  [        U R                  R                  5       5       VVs/ s H  u  pU< SU< 3PM     snn5      -  $ s  snnf )NzFactors({%s}), z: )joinr   r   items)rx   r   r   s      r+   __repr__Factors.__repr__  sM    +24<<3E3E3G+HI+H41A+HI"K K 	KIs   Ac                j    U R                   n[        U5      S:H  =(       a    [        R                  U;   $ )zJ
>>> from sympy.core.exprtools import Factors
>>> Factors(0).is_zero
True
r   )r   re   r   rc   )rx   r   s     r+   r`   Factors.is_zero  s(     LL1v{*qvv{*r-   c                $    U R                   (       + $ )zI
>>> from sympy.core.exprtools import Factors
>>> Factors(1).is_one
True
)r   rx   s    r+   is_oneFactors.is_one  s     <<r-   c                B   / nU R                   R                  5        Hx  u  p#US:w  a\  [        U[        5      (       a2  UR	                  5       u  pE[        X55      nUR                  XE-  5        MR  UR                  X#-  5        Mg  UR                  U5        Mz     [        U6 $ )zReturn the underlying expression.

Examples
========

>>> from sympy.core.exprtools import Factors
>>> from sympy.abc import x, y
>>> Factors((x*y**2).as_powers_dict()).as_expr()
x*y**2

r   )r   r   r'   r   r   r   appendr   )rx   r   factorrK   br   s         r+   as_exprFactors.as_expr  s     <<--/KFaxc7++!--/DA#C+AKK%KK,F# 0 Dzr-   c                `   [        U[        5      (       d  [        U5      n[        S X4 5       5      (       a  [        [        R                  5      $ [        U R                  5      nUR                  R                  5        H   u  p4X2;   a  X#   U-   nU(       d  X#	 M  XBU'   M"     [        U5      $ )a+  Return Factors of ``self * other``.

Examples
========

>>> from sympy.core.exprtools import Factors
>>> from sympy.abc import x, y, z
>>> a = Factors((x*y**2).as_powers_dict())
>>> b = Factors((x*y/z).as_powers_dict())
>>> a.mul(b)
Factors({x: 2, y: 3, z: -1})
>>> a*b
Factors({x: 2, y: 3, z: -1})
c              3  8   #    U  H  oR                   v   M     g 7fr&   r`   r:   r   s     r+   r=   Factors.mul.<locals>.<genexpr>  s     0-Qyy-   )r'   r   ru   r   rc   r   r   r   rx   otherr   r   rK   s        r+   mulFactors.mul  s     %))ENE04-000166?"t||$ ==..0KF o+!FO 1 wr-   c                   [        U[        5      (       ds  [        U5      nUR                  (       a#  [        5       [        [        R                  5      4$ U R                  (       a#  [        [        R                  5      [        5       4$ [        U R                  5      n[        UR                  5      nU R                  R                  5        H  u  pE UR                  U   nXV-
  nU(       d  X$	 X4	 M&  [        U5      (       a  US:  a  XrU'   X4	 MD  X$	 U* X4'   MM  UR                  U5      nUb  U(       a  XU'   X4	 Mp  X$	 X4	 Mv  UR                  5       u  pU	(       aT  UR                  5       u  pX-
  nUS:  a  X$==   U-  ss'   UnO)US:  a  X$==   U	-  ss'   X4==   U	-  ss'   X-
  nOXU'   UnU(       a  XcU'   M  X4	 M     [        U5      [        U5      4$ ! [         a     GM  f = f)a  Return ``self`` and ``other`` with ``gcd`` removed from each.
The only differences between this and method ``div`` is that this
is 1) optimized for the case when there are few factors in common and
2) this does not raise an error if ``other`` is zero.

See Also
========
div

r   )r'   r   r`   r   rc   r   r   r   KeyErrorr,   extract_additivelyrp   )rx   r   self_factorsother_factorsr   self_exp	other_exprK   r;   scsaocoark   s                 r+   normalFactors.normal  s    %))ENE}}	7166?33||33DLL)U]]+ $ 2 2 4F!MM&1	 &C (!)37+.(%-$,-0DM)//	:=/0V,)1(0)1%224FB!*!7!7!9!w!8(0B60(*I!AX(0B60)1R71(*	I350(*I 09f-)1[ !5^ |$gm&<<<Y  s   G
G('G(c                2   [        U R                  5      0 p2[        U[        5      (       dV  [        U5      nUR                  (       a  [
        eU R                  (       a#  [        [        R                  5      [        5       4$ UR                  R                  5        H  u  pEXB;   a  X$   U-
  n[        U5      (       a  US::  a  X$	 US:  a  U(       a  XbU'   M<  U* nOX$   R                  U5      nUb  U(       a  XrU'   O{X$	 OxUnX$   R                  5       u  pU	(       aH  UR                  5       u  pX-
  nUS:  a  X$==   U-  ss'   UnOUS:  a  X$==   U	-  ss'   X-
  nOXU'   UnU(       a  XU'   OXC;  d   eM  XSU'   M     [        U5      [        U5      4$ )a  Return ``self`` and ``other`` with ``gcd`` removed from each.
This is optimized for the case when there are many factors in common.

Examples
========

>>> from sympy.core.exprtools import Factors
>>> from sympy.abc import x, y, z
>>> from sympy import S

>>> a = Factors((x*y**2).as_powers_dict())
>>> a.div(a)
(Factors({}), Factors({}))
>>> a.div(x*z)
(Factors({y: 2}), Factors({z: 1}))

The ``/`` operator only gives ``quo``:

>>> a/x
Factors({y: 2})

Factors treats its factors as though they are all in the numerator, so
if you violate this assumption the results will be correct but will
not strictly correspond to the numerator and denominator of the ratio:

>>> a.div(x/z)
(Factors({y: 2}), Factors({z: -1}))

Factors is also naive about bases: it does not attempt any denesting
of Rational-base terms, for example the following does not become
2**(2*x)/2.

>>> Factors(2**(2*x + 2)).div(S(8))
(Factors({2: 2*x + 2}), Factors({8: 1}))

factor_terms can clean up such Rational-bases powers:

>>> from sympy import factor_terms
>>> n, d = Factors(2**(2*x + 2)).div(S(8))
>>> n.as_expr()/d.as_expr()
2**(2*x + 2)/8
>>> factor_terms(_)
2**(2*x)/2

r   )r   r   r'   r   r`   ZeroDivisionErrorr   rc   r   r,   r   rp   )rx   r   quoremr   rK   r}   r;   r   r   r   r   r   rk   s                 r+   divFactors.div0  s|   \ %rS%))ENE}}''||33 ==..0KF}K#%Q<<AvKAv*+K "C 66s;A}*+K #$'	!$!9!9!;%.%;%;%=FB#%7D#ax #r 1,.	!% #r 1,.I	.0F,.	$*3K#)#44#4KW 1Z s|WS\))r-   c                *    U R                  U5      S   $ )a  Return numerator Factor of ``self / other``.

Examples
========

>>> from sympy.core.exprtools import Factors
>>> from sympy.abc import x, y, z
>>> a = Factors((x*y**2).as_powers_dict())
>>> b = Factors((x*y/z).as_powers_dict())
>>> a.quo(b)  # same as a/b
Factors({y: 1})
r   r   rx   r   s     r+   r   Factors.quo  s     xxq!!r-   c                *    U R                  U5      S   $ )a  Return denominator Factors of ``self / other``.

Examples
========

>>> from sympy.core.exprtools import Factors
>>> from sympy.abc import x, y, z
>>> a = Factors((x*y**2).as_powers_dict())
>>> b = Factors((x*y/z).as_powers_dict())
>>> a.rem(b)
Factors({z: -1})
>>> a.rem(a)
Factors({})
r   r   r   s     r+   r   Factors.rem  s     xxq!!r-   c                P   [        U[        5      (       a,  UR                  5       nUR                  (       a  [	        U5      n[        U[
        5      (       aC  US:  a=  0 nU(       a)  U R                  R                  5        H  u  p4XA-  X#'   M     [        U5      $ [        SU-  5      e)zReturn self raised to a non-negative integer power.

Examples
========

>>> from sympy.core.exprtools import Factors
>>> from sympy.abc import x, y
>>> a = Factors((x*y**2).as_powers_dict())
>>> a**2
Factors({x: 2, y: 4})

r   z%expected non-negative integer, got %s)	r'   r   r   rL   intr   r   r   r   r   s        r+   powFactors.pow  s     eW%%MMOEE
eZ((UaZG#'<<#5#5#7KF&)iGO $8 7##DuLMMr-   c                   [        U[        5      (       d1  [        U5      nUR                  (       a  [        U R                  5      $ 0 nU R                  R	                  5        Hk  u  p4[        U5      [        U5      pCX1R                  ;   d  M+  XAR                  U   -
  R                  nUS:X  a  XBU'   MR  US:X  d  MZ  UR                  U   X#'   Mm     [        U5      $ )aW  Return Factors of ``gcd(self, other)``. The keys are
the intersection of factors with the minimum exponent for
each factor.

Examples
========

>>> from sympy.core.exprtools import Factors
>>> from sympy.abc import x, y, z
>>> a = Factors((x*y**2).as_powers_dict())
>>> b = Factors((x*y/z).as_powers_dict())
>>> a.gcd(b)
Factors({x: 1, y: 1})
TF)r'   r   r`   r   r   r   ro   )rx   r   r   r   rK   lts         r+   gcdFactors.gcd  s     %))ENE}}t||,,<<--/KF!&/73<C&MM&11>>:&)FO5[&+mmF&;GO 0 wr-   c                X   [        U[        5      (       d<  [        U5      n[        S X4 5       5      (       a  [        [        R                  5      $ [        U R                  5      nUR                  R                  5        H  u  p4X2;   a  [        XBU   5      nXBU'   M     [        U5      $ )aS  Return Factors of ``lcm(self, other)`` which are
the union of factors with the maximum exponent for
each factor.

Examples
========

>>> from sympy.core.exprtools import Factors
>>> from sympy.abc import x, y, z
>>> a = Factors((x*y**2).as_powers_dict())
>>> b = Factors((x*y/z).as_powers_dict())
>>> a.lcm(b)
Factors({x: 1, y: 2, z: -1})
c              3  8   #    U  H  oR                   v   M     g 7fr&   r   r   s     r+   r=   Factors.lcm.<locals>.<genexpr>  s     4m99mr   )	r'   r   ru   r   rc   r   r   r   maxr   s        r+   lcmFactors.lcm  s     %))ENE4tm444qvv&t||$ ==..0KF #v/!FO	 1 wr-   c                $    U R                  U5      $ r&   )r   r   s     r+   __mul__Factors.__mul__      xxr-   c                $    U R                  U5      $ r&   r   r   s     r+   
__divmod__Factors.__divmod__  r  r-   c                $    U R                  U5      $ r&   )r   r   s     r+   __truediv__Factors.__truediv__  r  r-   c                $    U R                  U5      $ r&   )r   r   s     r+   __mod__Factors.__mod__  r  r-   c                $    U R                  U5      $ r&   )r   r   s     r+   __pow__Factors.__pow__  r  r-   c                t    [        U[        5      (       d  [        U5      nU R                  UR                  :H  $ r&   )r'   r   r   r   s     r+   __eq__Factors.__eq__   s+    %))ENE||u}},,r-   c                    X:X  + $ r&    r   s     r+   __ne__Factors.__ne__%        r-   r&   )__name__
__module____qualname____firstlineno____doc__	__slots__r   r   r   propertyr`   r   r   r   r   r   r   r   r   r   r   r   r  r  r	  r  r  r  __static_attributes__r  r-   r+   r   r      s    :#Il&\$
K + +    4 BD=Ld*L"""N8 B <-
!r-   r   c                  z    \ rS rSrSrSrSS jrS rS rS r	S	 r
S
 rS rS rS rS rS rS rS rS rS rSrg)Termi)  z5Efficient representation of ``coeff*(numer/denom)``. coeffnumerdenomNc                   Uc  Uc  UR                   (       d  [        S5      eUR                  5       u  pE[        [        5      [        [        5      p2U H[  n[        U5      u  pxUR                  (       a  UR                  5       u  pXIU-  -  nUS:  a  X'==   U-  ss'   MN  X7==   U* -  ss'   M]     [        U5      n[        U5      nOUnUc
  [        5       nUc
  [        5       nX@l	        X l
        X0l        g )Nzcommutative expression expectedr   )is_commutativer   as_coeff_mulr"   r   r   rT   	primitiver   r!  r"  r#  )
rx   termr"  r#  r!  r   r   r   rK   conts
             r+   r   Term.__init__.  s    =U]&&.57 7 "..0NE&s+[-=5!+F3	;;!%!1JD3Y&E7K3&KKC4'K " ENEENEE}	}	


r-   c                Z    [        U R                  U R                  U R                  45      $ r&   )r   r!  r"  r#  r   s    r+   r   Term.__hash__R  s     TZZTZZ899r-   c                \    SU R                   < SU R                  < SU R                  < S3$ )NzTerm(r   )r   r   s    r+   r   Term.__repr__U  s    %)ZZTZZHHr-   c                    U R                   U R                  R                  5       U R                  R                  5       -  -  $ r&   )r!  r"  r   r#  r   s    r+   r   Term.as_exprX  s0    zz4::--/

0B0B0DDEEr-   c                   U R                   UR                   -  nU R                  R                  UR                  5      nU R                  R                  UR                  5      nUR	                  U5      u  p4[        X#U5      $ r&   )r!  r"  r   r#  r   r  )rx   r   r!  r"  r#  s        r+   r   Term.mul[  s]    

5;;&

u{{+

u{{+||E*E%((r-   c                ^    [        SU R                  -  U R                  U R                  5      $ )Nr   )r  r!  r#  r"  r   s    r+   invTerm.invd  s!    AdjjL$**djj99r-   c                @    U R                  UR                  5       5      $ r&   )r   r5  r   s     r+   r   Term.quog  s    xx		$$r-   c                    US:  a   U R                  5       R                  U* 5      $ [        U R                  U-  U R                  R                  U5      U R
                  R                  U5      5      $ )Nr   )r5  r   r  r!  r"  r#  r   s     r+   r   Term.powj  sX    1988:>>5&))

e+

u-

u-/ /r-   c                    [        U R                  R                  UR                  5      U R                  R                  UR                  5      U R                  R                  UR                  5      5      $ r&   )r  r!  r   r"  r#  r   s     r+   r   Term.gcdr  J    DJJNN5;;/JJNN5;;/JJNN5;;/1 	1r-   c                    [        U R                  R                  UR                  5      U R                  R                  UR                  5      U R                  R                  UR                  5      5      $ r&   )r  r!  r   r"  r#  r   s     r+   r   Term.lcmw  r=  r-   c                Z    [        U[        5      (       a  U R                  U5      $ [        $ r&   )r'   r  r   NotImplementedr   s     r+   r   Term.__mul__|  #    eT""88E?"!!r-   c                Z    [        U[        5      (       a  U R                  U5      $ [        $ r&   )r'   r  r   rA  r   s     r+   r  Term.__truediv__  rC  r-   c                Z    [        U[        5      (       a  U R                  U5      $ [        $ r&   )r'   r   r   rA  r   s     r+   r  Term.__pow__  s#    eZ((88E?"!!r-   c                    U R                   UR                   :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r&   r   r   s     r+   r  Term.__eq__  sA    

ekk) *

ekk)*

ekk)	+r-   c                    X:X  + $ r&   r  r   s     r+   r  Term.__ne__  r  r-   )r!  r#  r"  )NN)r  r  r  r  r  r  r   r   r   r   r   r5  r   r   r   r   r   r  r  r  r  r  r  r-   r+   r  r  )  sX    ?+I"H:IF):%/1
1
"""+
!r-   r  c           	     F   [        U [        5      (       a+  [        U [        5      (       d  [        R                  " U 5      n [        [        [        U  Vs/ s H  o3(       d  M  UPM     sn5      5      n [        U 5      S:X  a/  [        R                  [        R                  [        R                  4$ [        U 5      S:X  aK  U S   R                  nU S   R                  R                  5       nU S   R                  R                  5       nGO\U S   nU SS  H  nUR!                  U5      nM     [#        U 5       H  u  pUR%                  U5      X'   M     U(       a  U S   R                  nU SS  H  nUR'                  UR                  5      nM      / n	U  Hc  nUR                  R)                  UR%                  UR                  5      5      nU	R+                  UR                  UR                  5       -  5        Me     OBU  Vs/ s H  o3R                  5       PM     n	n[        [        R                  5      R                  nUR                  5       n[        U	6 nUR                  5       nU(       d'  UR,                  (       a  UR/                  5       u  pXJ-  nXEU4$ s  snf s  snf )a  Helper function for :func:`gcd_terms`.

Parameters
==========

isprimitive : boolean, optional
    If ``isprimitive`` is True then the call to primitive
    for an Add will be skipped. This is useful when the
    content has already been extracted.

fraction : boolean, optional
    If ``fraction`` is True then the expression will appear over a common
    denominator, the lcm of all term denominators.
r   r   N)r'   r	   r   r   rt   rv   mapr  re   r   rc   r\   r!  r"  r   r#  r   	enumerater   r   r   r   rT   r'  )termsisprimitivefractiontr)  r"  r#  r(  r*   numers_conts              r+   
_gcd_termsrU    s     %
5%(@(@e$Tu2u!Au234E
 5zQvvqvvquu$$
5zQQx~~a&&(a&&( Qx!"ID88D>D  !'GAxx~EH ( !HNNEab			$**- " F

uyy'<=djj89  ,115aiik5F1K%%E||~V5<<(W 3@ 2s   
J"JJc                  ^^^^ S n[        U [        5      nU=(       dF    [        U [        5      (       + =(       a*    [        U [        S9=(       a    [        U [
        5      (       + nU(       a  U(       a  [        U R                  5      n O[        U 5      n U" U 5      u  p[        U TT5      u  pn
U	R                  U5      n	UR                  5       u  pT(       dz  UR                  5       u  pUR                  (       dW  T(       dP  U	R                  (       a?  UR                  5       u  nnU	U-  n[        S UR                   5       5      (       a  Un	X-  n[!        XU	-  U
-  TS9$ [        U [        5      (       d  U $ U R"                  (       a  U $ U R$                  (       aA  U R'                  5       u  nn[!        U[)        U Vs/ s H  n[+        UTTT5      PM     sn6 TS9$ UUUU4S jm[        U [
        5      (       a0  [        U R                   VVs/ s H  u  nnUT" U5      4PM     snn6 $ U R,                  " U R                   Vs/ s H  nT" U5      PM     sn6 $ s  snf s  snnf s  snf )a  Compute the GCD of ``terms`` and put them together.

Parameters
==========

terms : Expr
    Can be an expression or a non-Basic sequence of expressions
    which will be handled as though they are terms from a sum.

isprimitive : bool, optional
    If ``isprimitive`` is True the _gcd_terms will not run the primitive
    method on the terms.

clear : bool, optional
    It controls the removal of integers from the denominator of an Add
    expression. When True (default), all numerical denominator will be cleared;
    when False the denominators will be cleared only if all terms had numerical
    denominators other than 1.

fraction : bool, optional
    When True (default), will put the expression over a common
    denominator.

Examples
========

>>> from sympy import gcd_terms
>>> from sympy.abc import x, y

>>> gcd_terms((x + 1)**2*y + (x + 1)*y**2)
y*(x + 1)*(x + y + 1)
>>> gcd_terms(x/2 + 1)
(x + 2)/2
>>> gcd_terms(x/2 + 1, clear=False)
x/2 + 1
>>> gcd_terms(x/2 + y/2, clear=False)
(x + y)/2
>>> gcd_terms(x/2 + 1/x)
(x**2 + 2)/(2*x)
>>> gcd_terms(x/2 + 1/x, fraction=False)
(x + 2/x)/2
>>> gcd_terms(x/2 + 1/x, fraction=False, clear=False)
x/2 + 1/x

>>> gcd_terms(x/2/y + 1/x/y)
(x**2 + 2)/(2*x*y)
>>> gcd_terms(x/2/y + 1/x/y, clear=False)
(x**2/2 + 1)/(x*y)
>>> gcd_terms(x/2/y + 1/x/y, clear=False, fraction=False)
(x/2 + 1/x)/y

The ``clear`` flag was ignored in this case because the returned
expression was a rational expression, not a simple sum.

See Also
========

factor_terms, sympy.polys.polytools.terms_gcd

c                X   U  Vs/ s H'  oR                   (       a  U/ 4OUR                  5       PM)     nn/ n[        U5       HT  u  nu  pVU(       aA  [        U6 n[	        5       nUR                  Xv45        UR                  U5        [        U6 X$'   MP  XRU'   MV     U[        U5      4$ s  snf )zhreplace nc portions of each term with a unique Dummy symbols
and return the replacements to restore them)r%  r   rN  r   r   r   r   )rO  r   r   r   r*   r   r   r}   s           r+   maskgcd_terms.<locals>.mask  s     HMMu!++B=uM#D/JAw"XGQG$q'Q * T$Z Ns   .B')includec              3  \   #    U  H"  nUR                  5       S    R                  v   M$     g7fr   N)r   rL   r:   r   s     r+   r=   gcd_terms.<locals>.<genexpr>8  s)      .!,A ~~'*55!,s   *,)clearc                ^  > [        U [        5      (       d  [        U [        5      (       aC  U R                  (       d  U $ U R                  " U R                   Vs/ s H  nT" U5      PM     sn6 $ [        U 5      " U  Vs/ s H  nT" U5      PM     sn5      $ [        U TTT5      $ s  snf s  snf r&   )r'   r
   r	   r   functype	gcd_terms)r   r*   r_  rQ  r   rP  s     r+   r   gcd_terms.<locals>.handleI  s    !T""!U##vvHvv166:6aq	6:;;7q1q!F1Iq122K99  ;1s   B%>B*)r'   r   r	   r!   setr   rv   r   r   rU  rw   r   rL   rT   rU   ru   r   is_Atomrq   r&  r   rc  ra  )rO  rP  r_  rQ  rX  isaddaddliker   r)  r"  r#  r!  r   r   _coeffr   r}   _numerr   r*   r   r   r   s    ```                  @r+   rc  rc    s   z   uc"E $:eU33 $E3'$ud##  $EENE5k'{HEUt$**,**,IA<<%,,'')1q .!'. . ."EHE5%-"5UCCeU##}}||$$&41c$ %.aeX$N$ #(* 	*: : %<Aq&)n<==::5::6:aq	:677$ =6s   II#
I)c                   U R                   nUS:X  a  [        R                  $ U R                  nU Vs1 s H  oDR                  S   iM     nn[        U40 UD6nUR                  " U6 u  pG[        U[        5      (       a)  X@R                  " S/UQ76 -  U R                  " U/UQ76 -   $ X@R                  " U/UQ76 -  $ s  snf )a;  Return Sum or Integral object with factors that are not
in the wrt variables removed. In cases where there are additive
terms in the function of the object that are independent, the
object will be separated into two objects.

Examples
========

>>> from sympy import Sum, factor_terms
>>> from sympy.abc import x, y
>>> factor_terms(Sum(x + y, (x, 1, 3)))
y*Sum(1, (x, 1, 3)) + Sum(x, (x, 1, 3))
>>> factor_terms(Sum(x*y, (x, 1, 3)))
y*Sum(x, (x, 1, 3))

Notes
=====

If a function in the summand or integrand is replaced
with a symbol, then this simplification should not be
done or else an incorrect result will be obtained when
the symbol is replaced with an expression that depends
on the variables of summation/integration:

>>> eq = Sum(y, (x, 1, 3))
>>> factor_terms(eq).subs(y, x).doit()
3*x
>>> eq.subs(y, x).doit()
6
r   r   )
functionr   rc   limitsr   factor_termsas_independentr'   r   ra  )r   kwargsresultrm  r*   wrtr   r}   s           r+   _factor_sum_intrs  X  s    > ]]F{vv[[F %
%f66!9fC
% 	V&v&AS!DA!S99Q(((499Q+@+@@@99Q(((( &s   B=c                F   ^^^^^ UUUUU4S jm[        U 5      nT" U5      $ )aS  Remove common factors from terms in all arguments without
changing the underlying structure of the expr. No expansion or
simplification (and no processing of non-commutatives) is performed.

Parameters
==========

radical: bool, optional
    If radical=True then a radical common to all terms will be factored
    out of any Add sub-expressions of the expr.

clear : bool, optional
    If clear=False (default) then coefficients will not be separated
    from a single Add if they can be distributed to leave one or more
    terms with integer coefficients.

fraction : bool, optional
    If fraction=True (default is False) then a common denominator will be
    constructed for the expression.

sign : bool, optional
    If sign=True (default) then even if the only factor in common is a -1,
    it will be factored out of the expression.

Examples
========

>>> from sympy import factor_terms, Symbol
>>> from sympy.abc import x, y
>>> factor_terms(x + x*(2 + 4*y)**3)
x*(8*(2*y + 1)**3 + 1)
>>> A = Symbol('A', commutative=False)
>>> factor_terms(x*A + x*A + x*y*A)
x*(y*A + 2*A)

When ``clear`` is False, a rational will only be factored out of an
Add expression if all terms of the Add have coefficients that are
fractions:

>>> factor_terms(x/2 + 1, clear=False)
x/2 + 1
>>> factor_terms(x/2 + 1, clear=True)
(x + 2)/2

If a -1 is all that can be factored out, to *not* factor it out, the
flag ``sign`` must be False:

>>> factor_terms(-x - y)
-(x + y)
>>> factor_terms(-x - y, sign=False)
-x - y
>>> factor_terms(-2*x - 2*y, sign=False)
-2*(x + y)

See Also
========

gcd_terms, sympy.polys.polytools.terms_gcd

c                  > SSK Jn  SSKJn  [	        U 5      n[        U [        5      (       a  U R                  (       a1  U(       a(  [        U 5      " U  Vs/ s H  nT" U5      PM     sn5      $ U $ U R                  (       d)  U R                  (       d  U(       d  [        U S5      (       dD  U R                  n[        U Vs/ s H  nT" U5      PM     sn5      nXe:X  a  U $ U R                  " U6 $ [        XU45      (       a  [        U TTTTS9$ U R!                  TTS9u  pxUR"                  (       a  [$        R&                  " U5       V	s/ s H  n	T" U	5      PM     n
n	[)        S U
 5       5      (       d  U* nU
 V	s/ s H  o* PM     n
n	0 n[+        U
5       HT  u  pIU	R-                  5       u  pUR.                  (       d  M*  U[1        UR                  6 :w  d  MB  [3        5       X'   XX   '   MV     [$        R4                  " U
5      n[7        USTTS	9R9                  U5      nOAUR                  (       a0  UR                  " UR                   V	s/ s H  n	T" U	5      PM     sn	6 n[;        XxTTS
9nU$ s  snf s  snf s  sn	f s  sn	f s  sn	f )Nr   )Sum)Integralr   )radicalr_  rQ  sign)rx  r_  c              3  j   #    U  H)  nUR                  5       S    R                  S5      SL v   M+     g7f)r   r   N)r   extract_multiplicativelyr]  s     r+   r=   +factor_terms.<locals>.do.<locals>.<genexpr>  s3      + )1 ~~'*CCBG4O )s   13T)rP  r_  rQ  )r_  ry  )sympy.concrete.summationsrv  sympy.integrals.integralsrw  r    r'   r	   rf  rb  rJ   is_Functionhasattrr   r   ra  rs  as_content_primitiverT   r   rt   ru   rN  r   rq   r   r   r   rc  rw   r   )r   rv  rw  is_iterabler*   r   newargsr)  rN   r   	list_argsspecialr   r   ry   r_  dorQ  rx  ry  s                  r+   r  factor_terms.<locals>.do  s,   16tn$&&$,,Dz$"7$Q2a5$"788K;;$**74#<#<99DD1DqRUD12G99g&&d(O,,"4u!. . ++G5+I88(+a(89(81A(8I9 + )+ + +u)23AR	3G!),}}888S!&&\ 1#(7IL,-IL)	 - y)A! !# $,8G#4  VV!"(A"Q%(*AD9	S #8 2 :
 4  )s   I-?I2I7:I<
Jr   )r   rx  r_  rQ  ry  expr2r  s    ```` @r+   rn  rn    s$    z0 0b DMEe9r-   Nc                v  ^^^ T=(       d    SmU4S jnU" 5       mU4S jnU nUR                   (       a  U 0 / 4$ / n[        5       n[        5       n[        U[        S9nU H  m[	        U4S jU 5       5      (       a  UR                  5         M/  TR                   (       a  MB  TR                  (       a#  UR                  T5        UR                  5         Mv  TR                  (       a  M  TR                  (       a  M  TR                  (       a  M  UR                  T5        UR                  5         M     [        U5      S:X  a.  U(       d'  UR                  UR                  5       U" 5       45        O<[        U5      S:X  a-  U(       d&  UR                  UR                  5       U" 5       45        [        U[        S9nU H,  n	U" SS	9n
UR                  X45        UR                  U
5        M.     UR                  U5      n[!        U5      nUR#                  [        S9  XE VVs0 s H  u  pX_M	     snnU4$ s  snnf )
a  
Return ``eq`` with non-commutative objects replaced with Dummy
symbols. A dictionary that can be used to restore the original
values is returned: if it is None, the expression is noncommutative
and cannot be made commutative. The third value returned is a list
of any non-commutative symbols that appear in the returned equation.

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

All non-commutative objects other than Symbols are replaced with
a non-commutative Symbol. Identical objects will be identified
by identical symbols.

If there is only 1 non-commutative object in an expression it will
be replaced with a commutative symbol. Otherwise, the non-commutative
entities are retained and the calling routine should handle
replacements in this case since some care must be taken to keep
track of the ordering of symbols when they occur within Muls.

Parameters
==========

name : str
    ``name``, if given, is the name that will be used with numbered Dummy
    variables that will replace the non-commutative objects and is mainly
    used for doctesting purposes.

Examples
========

>>> from sympy.physics.secondquant import Commutator, NO, F, Fd
>>> from sympy import symbols
>>> from sympy.core.exprtools import _mask_nc
>>> from sympy.abc import x, y
>>> A, B, C = symbols('A,B,C', commutative=False)

One nc-symbol:

>>> _mask_nc(A**2 - x**2, 'd')
(_d0**2 - x**2, {_d0: A}, [])

Multiple nc-symbols:

>>> _mask_nc(A**2 - B**2, 'd')
(A**2 - B**2, {}, [A, B])

An nc-object with nc-symbols but no others outside of it:

>>> _mask_nc(1 + x*Commutator(A, B), 'd')
(_d0*x + 1, {_d0: Commutator(A, B)}, [])
>>> _mask_nc(NO(Fd(x)*F(y)), 'd')
(_d0, {_d0: NO(CreateFermion(x)*AnnihilateFermion(y))}, [])

Multiple nc-objects:

>>> eq = x*Commutator(A, B) + x*Commutator(A, C)*Commutator(A, B)
>>> _mask_nc(eq, 'd')
(x*_d0 + x*_d1*_d0, {_d0: Commutator(A, B), _d1: Commutator(A, C)}, [_d0, _d1])

Multiple nc-objects and nc-symbols:

>>> eq = A*Commutator(A, B) + B*Commutator(A, C)
>>> _mask_nc(eq, 'd')
(A*_d0 + B*_d1, {_d0: Commutator(A, B), _d1: Commutator(A, C)}, [_d0, _d1, A, B])

rX  c               3  @   >#    Sn  T[        U 5      -   v   U S-  n M  7f)Nr   r   )str)r*   names    r+   numbered_names _mask_nc.<locals>.numbered_namesA  s*     Q-FA r?   c                 :   > SSK Jn  U" [        T5      /U Q70 UD6$ )Nr   r   )symbolr   next)r   rp  r   namess      r+   r   _mask_nc.<locals>.DummyI  s    !T%[242622r-   )r   c              3  4   >#    U  H  nTUS    :H  v   M     g7fr\  r  )r:   r;   r   s     r+   r=   _mask_nc.<locals>.<genexpr>W  s     &#QqAaDy#s   r   keyF)commutative)r%  re  r   r   ru   skip	is_symboladdrT   rq   rJ   re   r   rj   sortedrm   rv   sort)eqr  r  r   r   repnc_objnc_symspotr   r   r   r   r   r  s    `           @@r+   _mask_ncr    s   H >6D E3 D2rz CUFeG
T(8
9C&#&&&HHJ!!!{{A
hhh!(((ahhh

1
  6{a

FJJL%'*+	W	6

GKKM57+, F 01Fu%

A7B  99S>D7mGLL%L&3'341!$3'00's   !H5c           
        [        U 5      n [        U [        5      (       a  U R                  (       d  U $ U R                  (       d3  U R
                  " U R                   Vs/ s H  n[        U5      PM     sn6 $ U R
                  " U R                   Vs/ s H  n[        U5      PM     sn6 n SSKJ	nJ
n  [        U 5      u  pnU(       a  U" U 5      R                  U5      $ [        R                  " U 5       Vs/ s H  oR                  5       PM     nn[         R"                  =n=n	=pSn[%        U5       Hb  u  p!US:X  a  [&        R(                  " US   5      nM&  US   (       a"  U" U[&        R(                  " US   5      5      nMR  [         R"                  nMd     U[         R"                  La  SnUR+                  5       u  pU	[         R"                  La]  [%        U5       HN  u  nu  p[-        [&        R                  " [&        R(                  " [-        U5      5      U	-  5      5      nXU   S'   MP     [%        U5       H(  u  nu  pU(       a  US   U-  US'   OSU-  /nXU   S'   M*     [%        U5       H  u  p!US:X  a	  US   SS nO[/        WUS   5      nU(       a  M,  US   S   (       d    OUS   S   S   R1                  5       u  nnSnUR2                  (       a~  U HM  nUS   (       d    OmUS   S   R1                  5       u  nnUR2                  (       a  UU:X  a  [5        UU5      nMM    O-   S=nnUU-  n
UU* -  nU H  nUUS   S   -  US   S'   M       O6U(       a  M    O+   Sn[7        W5      n['        U6 n
U H  nUS   US US'   M     [%        U5       H  u  p!US:X  a	  US   SS nO[9        XS   5      nU(       a  M+  US   S   (       d    OUS   S   S   R1                  5       u  nnSnUR2                  (       a~  U HM  nUS   (       d    OmUS   S   R1                  5       u  nnUR2                  (       a  UU:X  a  [5        UU5      nMM    O-   S=nnUU-  nUU* -  nU H  nUS   S   U-  US   S'   M       OEU(       a  M    O:   Sn[7        U5      n['        U6 nU H  nUS   S[7        US   5      U-
   US'   M     U(       a.  [        U VVs/ s H  u  nn['        U6 ['        U6 -  PM     snn6 nOU nSSKJn  [?        U[@        S	9 Vs/ s H  o[C        5       4PM     nnU VVs/ s H
  u  nnUU4PM     nnnURE                  5         [        UR                  U5      5      u  nn nU" U" U5      5      nUR                  U 5      R                  U5      nURF                  (       a  [I        XU
-  U-  U-  5      $ URJ                  (       a  S
 n!/ n"/ n#UR                   Hs  n$U$RL                  (       a  U"RO                  U$5        M'  U$R1                  5       u  nnUR2                  (       a  U#RQ                  U/U-  5        Mb  U#RO                  U$5        Mu     U	['        U"6 -  U
-  n%U!" X-  5      n&[S        U#[7        U#5      5       H+  n'U%['        U'6 -  U-  nU!" U5      U&:X  d  M  [I        UU5      s  $    [I        XU
-  U-  U-  5      $ s  snf s  snf s  snf s  snnf s  snf s  snnf )aj  Return the factored form of ``expr`` while handling non-commutative
expressions.

Examples
========

>>> from sympy import factor_nc, Symbol
>>> from sympy.abc import x
>>> A = Symbol('A', commutative=False)
>>> B = Symbol('B', commutative=False)
>>> factor_nc((x**2 + 2*A*x + A**2).expand())
(x + A)**2
>>> factor_nc(((x + A)*(x + B)).expand())
(x + A)*(x + B)
r   )r   r   FTr   Nr   )powsimpr  c           
     ,    U R                  SSSSSSSS9$ )zCExpand with the minimal set of hints necessary to check the result.TF)deepr   	power_exp
power_basebasicmultinomiallog)expand)r   s    r+   
_pemexpandfactor_nc.<locals>._pemexpand  s)    {{$$$Et # P Pr-   )*r   r'   r
   r   rT   ra  	factor_ncr   rg   r   r   r  rm   r   rt   r   r   r\   rN  r   r   r   rv   r   r   rL   minre   r   sympy.simplify.powsimpr  r  r   r   reverserJ   r   rq   r%  r   extendr   )(r   r   r*   r   r   r  
nc_symbolsr   r   glr;   hitccr   r   r   r   okrR  btetillennr   midr  rep1r   r   unrep1new_midr2r  cfacncfacr   pre_midtargetrz   s(                                           r+   r  r  v  sK     4=DdD!!;;yy;A9Q<;<<99DII>Iq'*I>?D1$TNDz
d|  %%&)mmD&9:&9

&9:AdODAAvNN1Q4(13>>!A$/0EE $ AEE>C>>#DA~"+D/JAwcmmCNN48,DQ,FGHB!#GAJ #2 (o
7BqE!GBqEA#BQ
 . dODAAvaDG!!QqT*1AwqzAwqz!}0021<<! t!!"1a!4!4!6B==R1W #Ar
A! " $(SqDU!%A&(1ajAaDG "&r9 $< Cq6DQAtDE{!  dODAAvaDG!!qT*1AwqzAwqz"~1131<<! t!!"1b!5!5!7B==R1W #Ar
A! " $(SqDU!%A'(tBx{AaDH "&r9 $< Cq6DQAt-S1Y-.! =fb"Rb)=>CC2 '-Z=M&NO&NEG&NO%)*TTQ1a&T*!#((4.1Q&/*,,r"''/>>qA#g+a-00>>P DE\\##KKN==?DAq||aSU+Q " T
l1nG'Fs5z2S!W_Q&b>V+&q"-- 3 1c#gai((w <> ;B > P*s$   [[7[[ 
[&1[+)r   r
   returnztuple[Expr, int])r   r
   r  ztuple[Expr, Rational])FT)FTT)FFFT)r   zExpr | complexr  r
   r&   )<r  
__future__r   r  r   r   r   r   powerr   r  r	   r   r
   rl  r   r   numbersr   r   r   r   r   	singletonr   sortingr   r   r  r   	traversalr   
coreerrorsr   
containersr   r   sympy.external.gmpyr   sympy.utilities.iterablesr   r   r   r    r!   collectionsr"   r]   r,   rS   r   r   r   r  rU  rc  rs  rn  r  r  r  r-   r+   <module>r     s    ? "  !    &  ? ?  .  ) 0 # *+ + $ d=v$r)X4F! F!Rk! k!\>B}8@-)`ody1xo)r-   