
    \h.[                         S r SSKJr  SSKJrJrJrJr  SSKJ	r	  SSK
Jr  SSKJ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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)%zGroebner bases algorithms.     )Dummy)monomial_mulmonomial_lcmmonomial_dividesterm_div)lex)DomainError)queryNc                    Uc  [        S5      n[        [        S.n X2   nUR
                  SpeUR                  (       a  UR                  (       d>   XR                  UR                  5       S9pU  Vs/ s H  owR                  U5      PM     n nU" X5      nUb1  U V	s/ s H$  oR                  5       S   R                  U5      PM&     nn	U$ ! [         a    [	        SU-  5      ef = fs  snf ! [         a    [        SU-  5      ef = fs  sn	f )aI  
Computes Groebner basis for a set of polynomials in `K[X]`.

Wrapper around the (default) improved Buchberger and the other algorithms
for computing Groebner bases. The choice of algorithm can be changed via
``method`` argument or :func:`sympy.polys.polyconfig.setup`, where
``method`` can be either ``buchberger`` or ``f5b``.

Ngroebner)
buchbergerf5bzO'%s' is not a valid Groebner bases algorithm (valid are 'buchberger' and 'f5b'))domainz'Cannot compute a Groebner basis over %s   )r
   _buchberger_f5bKeyError
ValueErrorr   is_Fieldhas_assoc_Fieldclone	get_fieldset_ringr	   clear_denoms)
seqringmethod_groebner_methods	_groebnerr   origsGgs
             Q/var/www/auris/envauris/lib/python3.13/site-packages/sympy/polys/groebnertools.pyr   r   
   s    ~z" "
u%-	 ;;D??&"8"8	4zz1A1A1CzD$ /23cJJt$cC3#A:;=!Qnnq!**40!=H%  ujmssttu 4  	RG&PQQ	R >s#   C C- 2C(+D	C%-Dc                   ^ ^^^^^ UR                   mUR                  mUR                  mUR                  mU UU4S jnUU 4S jnU UUU4S jnT (       d  / $ T SS n USS m / n[	        [        T 5      5       HD  nT U   nUR                  T SU 5      nU(       d  M%  UR                  UR                  5       5        MF     T U:X  a  OMl  0 m[        5       n	[        5       n
[        5       n[        T 5       H  u  plUTU'   U	R                  U5        M     U	(       aL  [        U	 Vs/ s H  nT U   PM
     snU4S jS9nTU   nU	R                  U5        U" XU5      u  pU	(       a  ML  SnU(       am  U" U5      u  nnUR                  UU45        [        T U   T U   U5      n[        U
U U4S	 jS9nU" UU5      nU(       a  U" XUS
   5      u  pOUS
-  nU(       a  Mm  [        5       nU
 H0  nU" T U   U
U1-
  5      nU(       d  M  UR                  US
   5        M2     U Vs/ s H  nT U   PM
     nn[        UU4S jSS9nU$ s  snf s  snf )a  
Computes Groebner basis for a set of polynomials in `K[X]`.

Given a set of multivariate polynomials `F`, finds another
set `G`, such that Ideal `F = Ideal G` and `G` is a reduced
Groebner basis.

The resulting basis is unique and has monic generators if the
ground domains is a field. Otherwise the result is non-unique
but Groebner bases over e.g. integers can be computed (if the
input polynomials are monic).

Groebner bases can be used to choose specific generators for a
polynomial ideal. Because these bases are unique you can check
for ideal equality by comparing the Groebner bases.  To see if
one polynomial lies in an ideal, divide by the elements in the
base and see if the remainder vanishes.

They can also be used to solve systems of polynomial equations
as,  by choosing lexicographic ordering,  you can eliminate one
variable at a time, provided that the ideal is zero-dimensional
(finite number of solutions).

Notes
=====

Algorithm used: an improved version of Buchberger's algorithm
as presented in T. Becker, V. Weispfenning, Groebner Bases: A
Computational Approach to Commutative Algebra, Springer, 1993,
page 232.

References
==========

.. [1] [Bose03]_
.. [2] [Giovini91]_
.. [3] [Ajwa95]_
.. [4] [Cox97]_

c                 (   > [        U UUU4S jS9nU$ )Nc                 b   > T" T" TU S      R                   TU S      R                   5      5      $ )Nr   r   LM)pairfr   orders    r$   <lambda>-_buchberger.<locals>.select.<locals>.<lambda>d   s)    U<$q'
qaz}}+U%V    key)min)Pprr+   r   r,   s     r$   select_buchberger.<locals>.selecta   s     VW	r/   c                    > U R                  U Vs/ s H  nTU   PM
     sn5      nU(       d  g UR                  5       nUT;  a  [        T5      TU'   TR                  U5        UR                  TU   4$ s  snf N)remmoniclenappendr)   )r#   JjhIr+   s       r$   normal_buchberger.<locals>.normalg   sk    EE!%!QAaD!%&	Az1v!441: &s   A5c                 N  >^^^ TU   nUR                   mU R                  5       n[        5       nU(       a  UR                  5       nTU   nUR                   nT" TU5      mUUUUU4S jmT" TU5      T:X  d4  [	        U4S jU 5       5      (       d,  [	        U4S jU 5       5      (       d  UR                  X&45        U(       a  M  [        5       n	U(       aR  UR                  5       u  p&TU   R                   nT" TU5      mT" TU5      T:X  d  U	R                  X&45        U(       a  MR  [        5       n
U(       a{  UR                  5       u  pTU   R                   nTU   R                   nT" X5      nT" UT5      (       a  T" UT5      U:X  d  T" UT5      U:X  a  U
R                  X45        U(       a  M{  X-  n
[        5       nU (       aG  U R                  5       nTU   R                   nT" UT5      (       d  UR                  U5        U (       a  MG  UR                  U5        UU
4$ )Nc                 B   > T" TTU    R                   5      nT" TU5      $ r8   r(   )ipmLCMhgr+   mhmonomial_divr   s     r$   lcm_divides0_buchberger.<locals>.update.<locals>.lcm_divides   s$     QrUXX.#E1--r/   c              3   4   >#    U  H  nT" U5      v   M     g 7fr8    ).0ipxrJ   s     r$   	<genexpr>._buchberger.<locals>.update.<locals>.<genexpr>   s     6ASC((As   c              3   :   >#    U  H  nT" US    5      v   M     g7f)r   NrM   )rN   r4   rJ   s     r$   rP   rQ      s     ;2K1..s   )r)   copysetpopanyadd)r"   Bihr?   CDigr#   mgEB_newig1ig2mg1mg2LCM12G_newrG   rJ   rH   r+   rI   r   r   s                    @@@r$   update_buchberger.<locals>.updateu   s    bETT FFHEB"AB R(E. . B#u,6A666;;;;rh! a$ EUUWFB2B R(EB'50rh a uuwHCC&))CC&))C *E  r**S"%. b)U2		3*% a 	
 B2BB''		" a 			"e|r/   NTc                 (   > T" U R                   5      $ r8   r(   r+   r,   s    r$   r-   _buchberger.<locals>.<lambda>   s    qttr/   r0   r   c                 .   > T" TU    R                   5      $ r8   r(   )r#   r+   r,   s    r$   r-   rj      s    U1Q477^r/   r   c                 (   > T" U R                   5      $ r8   r(   ri   s    r$   r-   rj     s    %+r/   r1   reverse)r,   r   rI   r   ranger;   r9   r<   r:   rT   	enumeraterW   r2   removespolysorted)r+   r   r5   rA   rf   f1iprFr"   CPr?   xrY   reductions_to_zeror`   ra   G1htGrr\   r@   rI   r   r   r,   s   `                     @@@@@r$   r   r   2   sI   R JJE$$L$$L$$LE EP 	 
1B
qEs1vA!AaeAq		!'')$  7  	AAA	B!!	a  q!q!1q!'<=qT	qb! ! 
":S
		3*!C&!C&$'A34Ar]1"Q%(EAr!# "  
BAbE1t8$2FF2a5M	  	"B!B%"B	 
-t	<BIK "@ 
s   $I!Ic                     U R                   nUR                   nUR                  X45      nUR                  XS5      nUR                  XT5      nU R                  U5      nUR                  U5      n	X-
  n
U
$ )zw
Compute LCM(LM(p1), LM(p2))/LM(p1)*p1 - LCM(LM(p1), LM(p2))/LM(p2)*p2
This is the S-poly provided p1 and p2 are monic
)r)   r   rI   	mul_monom)p1p2r   LM1LM2rd   m1m2s1s2r!   s              r$   rr   rr     sn    
 %%C
%%Cc'E			5	&B			5	&B	b	B	b	B
AHr/   c                     U S   $ )Nr   rM   r+   s    r$   Signr         Q4Kr/   c                     U S   $ )Nr   rM   r   s    r$   Polynr     r   r/   c                     U S   $ )N   rM   r   s    r$   Numr   "  r   r/   c                     X4$ r8   rM   )monomialindexs     r$   sigr   &  s    r/   c                 
    XU4$ r8   rM   )	signature
polynomialnumbers      r$   lbpr   *  s    6**r/   c                 h    U S   US   :  a  gU S   US   :X  a  U" U S   5      U" US   5      :  a  gg)z
Compare two signatures by extending the term order to K[X]^n.

u < v iff
    - the index of v is greater than the index of u
or
    - the index of v is equal to the index of u and u[0] < v[0] w.r.t. order

u > v otherwise
r   r   rM   )uvr,   s      r$   sig_cmpr   0  sE     	tad{tqt| 1;qt$r/   c                 $    U S   * U" U S   5      4$ )zt
Key for comparing two signatures.

s = (m, k), t = (n, l)

s < t iff [k > l] or [k == l and m < n]
s > t otherwise
r   r   rM   )r!   r,   s     r$   sig_keyr   E  s     qTE51;r/   c                 :    [        [        U S   U5      U S   5      $ )zs
Multiply a signature by a monomial.

The product of a signature (m, i) and a monomial n is defined as
(m * t, i).
r   r   )r   r   )r!   rF   s     r$   sig_multr   Q  s      |AaD!$ad++r/   c                     [        [        U 5      [        U5      [        U 5      R                  R                  5      S:  a  UnOU n[        U 5      [        U5      -
  n[        [        U5      U[        U5      5      $ )z
Subtract labeled polynomial g from f.

The signature and number of the difference of f and g are signature
and number of the maximum of f and g, w.r.t. lbp_cmp.
r   )r   r   r   r   r,   r   r   )r+   r#   max_polyrets       r$   lbp_subr   ]  s^     tAwQq!4!459
(U1X
CtH~sCM22r/   c                     [        [        [        U 5      US   5      [        U 5      R	                  U5      [        U 5      5      $ )z
Multiply a labeled polynomial with a term.

The product of a labeled polynomial (s, p, k) by a monomial is
defined as (m * s, m * p, k).
r   )r   r   r   r   mul_termr   )r+   cxs     r$   lbp_mul_termr   n  s5     xQA'q):):2)>AGGr/   c                     [        [        U 5      [        U5      [        U 5      R                  R                  5      S:X  a  g[        U 5      [        U5      :X  a  [        U 5      [        U5      :  a  gg)z
Compare two labeled polynomials.

f < g iff
    - Sign(f) < Sign(g)
or
    - Sign(f) == Sign(g) and Num(f) > Num(g)

f > g otherwise
r   r   )r   r   r   r   r,   r   )r+   r#   s     r$   lbp_cmpr   x  sU     tAwQq!4!45;Aw$q'q6CF? r/   c                 ~    [        [        U 5      [        U 5      R                  R                  5      [        U 5      * 4$ )z,
Key for comparing two labeled polynomials.
)r   r   r   r   r,   r   r   s    r$   lbp_keyr     s.     DGU1X]]001CF7;;r/   c           	      X   UR                   n[        U 5      R                  n[        U5      R                  n[        US   US   5      UR                  4n[        XdU5      n[        XeU5      n[        [        [        U 5      [        U 5      R                  5       [        U 5      5      U5      n	[        [        [        U5      [        U5      R                  5       [        U5      5      U5      n
[        X5      S:X  a  [        U
5      X[        U	5      Xp4$ [        U	5      Xp[        U
5      X4$ )a  
Compute the critical pair corresponding to two labeled polynomials.

A critical pair is a tuple (um, f, vm, g), where um and vm are
terms such that um * f - vm * g is the S-polynomial of f and g (so,
wlog assume um * f > vm * g).
For performance sake, a critical pair is represented as a tuple
(Sign(um * f), um, f, Sign(vm * g), vm, g), since um * f creates
a new, relatively expensive object in memory, whereas Sign(um *
f) and um are lightweight and f (in the tuple) is a reference to
an already existing object in memory.
r   r   )r   r   LTr   oner   r   r   r   leading_termr   r   )r+   r#   r   r   ltfltgltumvmfrgrs              r$   critical_pairr     s     [[F
(++C
(++C
s1vs1v
&

	3B	"6	"B	"6	"B 
c$q'58#8#8#:CFCR	HB	c$q'58#8#8#:CFCR	HB r"R"b211R"b211r/   c                 |   [        U S   5      R                  R                  n[        U S   U[	        U S   5      5      n[        US   U[	        US   5      5      n[        X45      nUS:X  a  gUS:X  aJ  [        U S   U[	        U S   5      5      n[        US   U[	        US   5      5      n[        Xg5      nUS:X  a  gg)aU  
Compare two critical pairs c and d.

c < d iff
    - lbp(c[0], _, Num(c[2]) < lbp(d[0], _, Num(d[2])) (this
    corresponds to um_c * f_c and um_d * f_d)
or
    - lbp(c[0], _, Num(c[2]) >< lbp(d[0], _, Num(d[2])) and
    lbp(c[3], _, Num(c[5])) < lbp(d[3], _, Num(d[5])) (this
    corresponds to vm_c * g_c and vm_d * g_d)

c > d otherwise
r   r   r         r   )r   r   zeror   r   r   )cdr   c0d0rw   c1d1s           r$   cp_cmpr     s     1;  D	QqT4QqT	#B	QqT4QqT	#BABwAv1tS1Y'1tS1Y'BO7 r/   c                     [        [        U S   UR                  [        U S   5      5      5      [        [        U S   UR                  [        U S   5      5      5      4$ )z#
Key for comparing critical pairs.
r   r   r   r   )r   r   r   r   )r   r   s     r$   cp_keyr     sP     C!diiQqT34gc!A$		SVWXYZW[S\>]6^__r/   c                 Z    [        [        U S   U S   5      [        U S   U S   5      5      $ )zx
Compute the S-polynomial of a critical pair.

The S-polynomial of a critical pair cp is cp[1] * cp[2] - cp[4] * cp[5].
r   r   r      )r   r   )cps    r$   s_polyr     s/     <1r!u-|BqE2a5/IJJr/   c                 &   U H  nU S   [        U5      S   :  a)  [        [        U5      R                  U S   5      (       a    gU S   [        U5      S   :X  d  MX  U[	        U5      :  d  Mi  [        [        U5      S   U S   5      (       d  M    g   g)a  
Check if a labeled polynomial is redundant by checking if its
signature and number imply rewritability or comparability.

(sign, num) is comparable if there exists a labeled polynomial
h in B, such that sign[1] (the index) is less than Sign(h)[1]
and sign[0] is divisible by the leading monomial of h.

(sign, num) is rewritable if there exists a labeled polynomial
h in B, such thatsign[1] is equal to Sign(h)[1], num < Num(h)
and sign[0] is divisible by Sign(h)[0].
r   r   TF)r   r   r   r)   r   )signnumrX   r?   s       r$   is_rewritable_or_comparabler     s     7T!WQZaT!W55 7d1gaj SV|#DGAJQ88  r/   c                 x   [        U 5      R                  R                  n[        U 5      R                  R                  n[        U 5      (       d  U $  U nU H  n[        U5      (       d  M  [	        [        U5      R
                  [        U 5      R
                  5      (       d  MN  [        [        U 5      R                  [        U5      R                  U5      n[        [        [        U5      US   5      [        U 5      U5      S:  d  M  [        XV5      n[        X5      n   O   X@:X  d  [        U 5      (       d  U $ M  )a  
F5-reduce a labeled polynomial f by B.

Continuously searches for non-zero labeled polynomial h in B, such
that the leading term lt_h of h divides the leading term lt_f of
f and Sign(lt_h * h) < Sign(f). If such a labeled polynomial h is
found, f gets replaced by f - lt_f / lt_h * h. If no such h can be
found or f is 0, f is no further F5-reducible and f gets returned.

A polynomial that is reducible in the usual sense need not be
F5-reducible, e.g.:

>>> from sympy.polys.groebnertools import lbp, sig, f5_reduce, Polyn
>>> from sympy.polys import ring, QQ, lex

>>> R, x,y,z = ring("x,y,z", QQ, lex)

>>> f = lbp(sig((1, 1, 1), 4), x, 3)
>>> g = lbp(sig((0, 0, 0), 2), x, 2)

>>> Polyn(f).rem([Polyn(g)])
0
>>> f5_reduce(f, [g])
(((1, 1, 1), 4), x, 3)

r   )r   r   r,   r   r   r)   r   r   r   r   r   r   r   )r+   rX   r,   r   r#   r?   thps           r$   	f5_reducer     s    6 !HMME1X]]!!F88
AQxx#E!HKKq== qeAhkk6BAxQ16QG!K *!/#AN  6qH r/   c           
      f  ^^ TR                   mU n Un / n[        [        U 5      5       H5  nX   nUR                  U SU 5      nU(       d  M$  UR	                  U5        M7     X:X  a  OMY  [        [        U 5      5       Vs/ s H,  n[        [        TR                  US-   5      X   US-   5      PM.     nnUR                  U4S jSS9  [        [        U5      5       VVs/ s H4  n[        US-   [        U5      5        H  n[        X#   X&   T5      PM     M6     nnnUR                  U4S jSS9  [        U5      nSn	[        U5      (       Ga  UR                  5       n
[        U
S   [        U
S   5      U5      (       a  MD  [        U
S	   [        U
S
   5      U5      (       a  Mg  [        U
5      n[        X5      n[        [        U5      [!        U5      R#                  5       US-   5      n[!        U5      (       Ga  / n[%        U5       Ho  u  p:[        U
S   [        U
S   5      U/5      (       a  UR	                  U5        M:  [        U
S	   [        U
S
   5      U/5      (       d  M^  UR	                  U5        Mq     ['        U5       H  nXs	 M     U Hz  n[!        U5      (       d  M  [        XMT5      n
[        U
S   [        U
S   5      U/5      (       a  ME  [        U
S	   [        U
S
   5      U/5      (       a  Mi  UR	                  U
5        M|     UR                  U4S jSS9  [!        U5      R(                  nT" U5      T" [!        US   5      R(                  5      ::  a  UR	                  U5        OL[%        U5       H=  u  p?T" U5      T" [!        U5      R(                  5      :  d  M,  UR+                  X45          O   US-  nOU	S-  n	[        U5      (       a  GM  U Vs/ s H  n[!        U5      R#                  5       PM     nn[-        UT5      n[/        UU4S jSS9$ s  snf s  snnf s  snf )a%  
Computes a reduced Groebner basis for the ideal generated by F.

f5b is an implementation of the F5B algorithm by Yao Sun and
Dingkang Wang. Similarly to Buchberger's algorithm, the algorithm
proceeds by computing critical pairs, computing the S-polynomial,
reducing it and adjoining the reduced S-polynomial if it is not 0.

Unlike Buchberger's algorithm, each polynomial contains additional
information, namely a signature and a number. The signature
specifies the path of computation (i.e. from which polynomial in
the original basis was it derived and how), the number says when
the polynomial was added to the basis.  With this information it
is (often) possible to decide if an S-polynomial will reduce to
0 and can be discarded.

Optimizations include: Reducing the generators before computing
a Groebner basis, removing redundant critical pairs when a new
polynomial enters the basis and sorting the critical pairs and
the current basis.

Once a Groebner basis has been found, it gets reduced.

References
==========

.. [1] Yao Sun, Dingkang Wang: "A New Proof for the Correctness of F5
       (F5-Like) Algorithm", https://arxiv.org/abs/1004.0084 (specifically
       v4)

.. [2] Thomas Becker, Volker Weispfenning, Groebner bases: A computational
       approach to commutative algebra, 1993, p. 203, 216
TNr   c                 :   > T" [        U 5      R                  5      $ r8   )r   r)   ri   s    r$   r-   _f5b.<locals>.<lambda>q  s    uQx{{+r/   rm   c                    > [        U T5      $ r8   r   r   r   s    r$   r-   r   u  s    6"d+r/   r   r   r   r   c                    > [        U T5      $ r8   r   r   s    r$   r-   r     s    6"d#3r/   r   c                 (   > T" U R                   5      $ r8   r(   ri   s    r$   r-   r     s    5;r/   )r,   ro   r;   r9   r<   r   r   
zero_monomsortr   rU   r   r   r   r   r   r   r:   rp   reversedr)   insertred_groebnerrs   )rx   r   rX   ru   rv   rw   r>   ry   kr{   r   r!   indicesr#   rF   qHr,   s    `               @r$   r   r   ;  s   D JJE 	
A
s1vAAaeAq  6  AFc!fN1S!a%(!$A	6ANFF+TF: 49Q=	_=a%PQTUPUWZ[\W]J^Q-adD
)J^
)=B	_GG+TG:AA
b''VVX 'r!uc"Q%j!<<&r!uc"Q%j!<<2JaOQq)1q5188G"2.r!uc"Q%j1#FFNN1%0ABqE
QCHHNN1%	 ' g&E ' 88&qT2B22a5#be*qcJJ 4RUC1JLL IIbM  GG3TGB aAQx5qu11%aLDAQx%a"44 )
 FA !#o b''t $%%1aq	1A%QA!.==Q 	O 
`B 	&s   3P#!;P(##P.c                    ^ S nU n/ nU(       aF  UR                  5       m[        U4S jX4-    5       5      (       d  UR                  T5        U(       a  MF  U" U5      $ )z
Compute reduced Groebner basis, from BeckerWeispfenning93, p. 216

Selects a subset of generators, that already generate the ideal
and computes a reduced Groebner basis for them.
c                     / n[        U 5       H;  u  p#UR                  U SU XS-   S -   5      nU(       d  M*  UR                  U5        M=     U Vs/ s H  o3R                  5       PM     sn$ s  snf )z!
The actual reduction algorithm.
Nr   )rp   r9   r<   r:   )r3   Qru   rv   r?   s        r$   	reductionred_groebner.<locals>.reduction  sh     aLDAaeaAi'(Aq !
 $%%1a	1%%%s   A-c              3   d   >#    U  H%  n[        UR                  TR                  5      v   M'     g 7fr8   )r   r)   )rN   r+   f0s     r$   rP   red_groebner.<locals>.<genexpr>  s$     @%Q#ADD"%%00%s   -0)rU   rV   r<   )r"   r   r   rx   r   r   s        @r$   r   r     sS    
& 	
A
A
UUW@!%@@@HHRL	 ! Q<r/   c                     [        [        U 5      5       HN  n[        US-   [        U 5      5       H/  n[        X   X   U5      nUR                  U 5      nU(       d  M.      g   MP     g)z!
Check if G is a Groebner basis.
r   FT)ro   r;   rr   r9   )r"   r   ru   r>   r!   s        r$   is_groebnerr     s[     3q6]q1uc!f%AadAD$'AaAq	 &  r/   c                 4  ^ UR                   mUR                  nU R                  U4S jS9  [        U 5       H]  u  p4UR                  UR
                  :w  a    gU SU XS-   S -    H+  n[        UR                  UR                  5      (       d  M*      g   M_     g)z*
Checks if G is a minimal Groebner basis.
c                 (   > T" U R                   5      $ r8   r(   r#   r,   s    r$   r-   is_minimal.<locals>.<lambda>      qttr/   r0   FNr   T)r,   r   r   rp   LCr   r   r)   )r"   r   r   ru   r#   r?   r,   s         @r$   
is_minimalr     s     JJE[[FFF$F%!446::2Aq56"Aadd++ #	  r/   c                 V  ^ UR                   mUR                  nU R                  U4S jS9  [        U 5       Hn  u  p4UR                  UR
                  :w  a    gUR                  5        H9  nU SU XS-   S -    H%  n[        UR                  US   5      (       d  M#        g   M;     Mp     g)z*
Checks if G is a reduced Groebner basis.
c                 (   > T" U R                   5      $ r8   r(   r   s    r$   r-   is_reduced.<locals>.<lambda>  r   r/   r0   FNr   r   T)	r,   r   r   rp   r   r   termsr   r)   )r"   r   r   ru   r#   termr?   r,   s          @r$   
is_reducedr     s     JJE[[FFF$F%!446::GGIDrUQ1uvY&#ADD$q'22  ' 	  r/   c                    U R                   UR                   :w  a  [        S5      eU R                   nUR                  nU (       a  U(       d  UR                  $ [	        U 5      S::  af  [	        U5      S::  aW  [        U R                  UR                  5      nUR                  U R                  UR                  5      nUR                  XE5      $ U R                  5       u  p`UR                  5       u  pqUR                  Xg5      nU R                  5        VVs/ s H  u  pESU-   U4PM     n	nnUR                  5        VVs/ s H  u  pESU-   U4PM     snnUR                  5        VVs/ s H  u  pESU-   U* 4PM     snn-   n
[        S5      nUR                  U4UR                  -   [        S9nUR!                  U	5      nUR!                  U
5      n[#        X/U5      nS nU Vs/ s H  nU" US5      (       d  M  UPM     nnUS   R                  5        VVs/ s H  u  pEUSS	 XX-  4PM     nnnUR!                  U5      nU$ s  snnf s  snnf s  snnf s  snf s  snnf )
a  
Computes LCM of two polynomials using Groebner bases.

The LCM is computed as the unique generator of the intersection
of the two ideals generated by `f` and `g`. The approach is to
compute a Groebner basis with respect to lexicographic ordering
of `t*f` and `(1 - t)*g`, where `t` is an unrelated variable and
then filtering out the solution that does not contain `t`.

References
==========

.. [1] [Cox97]_

Values should be equalr   )r   )r   r   )symbolsr,   c                 T   ^ [        U4S jU R                  5        5       5      (       + $ )Nc              3   ,   >#    U  H	  oT   v   M     g 7fr8   rM   )rN   monomr>   s     r$   rP   7groebner_lcm.<locals>.is_independent.<locals>.<genexpr>A  s     8ZEQxZs   )rV   monoms)r?   r>   s    `r$   is_independent$groebner_lcm.<locals>.is_independent@  s    8QXXZ8888r/   r   N)r   r   r   r   r;   r   r)   lcmr   term_new	primitiver   r   r   r   r   
from_termsr   )r+   r#   r   r   r  coefffcgcr  f_termsg_termsr   t_ringrx   r"   basisr  r?   r   h_termss                       r$   groebner_lcmr    s     	vv12266D[[FAyy
1v{s1v{QTT144(

144&}}U**KKMEBKKMEB
**R
C:;'')E),%u%)GE:;'')E),%u%)E:;'')E),%uf%)EFG 	c
AZZt|| 33Z?F'"A'"AaVV$E9 4UnQ2!UA4;<Q4::<I<<5qrEI&<GI AH) FEE 	5Is$   I>I	'I$I:IIc                    U R                   UR                   :w  a  [        S5      eU R                   R                  nUR                  (       d5  U R	                  5       u  p0UR	                  5       u  pAUR                  X45      nX-  R                  [        X5      /5      n[        U5      S:w  a  [        S5      eUS   nUR                  (       d  WU-  $ UR                  5       $ )z6Computes GCD of two polynomials using Groebner bases. r   r   zLength should be 1r   )
r   r   r   r   r	  gcdquor  r;   r:   )r+   r#   r   r  r  r  r   r?   s           r$   groebner_gcdr  J  s    vv122VV]]F??jj 	
		<%&'A
1v{-..	!A??1uwwyr/   r8   )*__doc__sympy.core.symbolr   sympy.polys.monomialsr   r   r   r   sympy.polys.orderingsr   sympy.polys.polyerrorsr	   sympy.polys.polyconfigr
   r   r   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  rM   r/   r$   <module>r     s    ! $ X X % . (&PRh(+*	 ,3"H*< 2F!H`K60f}>@@((7rr/   