
    Ed-[                         d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ d$dZd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d  Z&d! Z'd" Z(d# Z)dS )%zGroebner bases algorithms.     )Dummy)monomial_mulmonomial_lcmmonomial_dividesterm_div)lex)DomainError)queryNc                    |t          d          }t          t          d}	 ||         }n # t          $ r t	          d|z            w xY wj        dc}|j        r|j        s[	                     |	                                          cfd| D             } n # t          $ r t          d|z            w xY w ||           }fd|D             }|S )	aa  
    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'))domainc                 :    g | ]}|                               S  )set_ring).0srings     9lib/python3.11/site-packages/sympy/polys/groebnertools.py
<listcomp>zgroebner.<locals>.<listcomp>)   s%    333AJJt$$333    z'Cannot compute a Groebner basis over %sc                 j    g | ]/}|                                 d                                        0S    )clear_denomsr   )r   gorigs     r   r   zgroebner.<locals>.<listcomp>.   s6    ===Qannq!**400===r   )r
   _buchberger_f5bKeyError
ValueErrorr   is_Fieldhas_assoc_Fieldclone	get_fieldr	   )seqr   method_groebner_methods	_groebnerr   Gr   s    `     @r   r   r   
   sN     #z"" " 
u%f-		 u u ujmsstttu ;LFD? 4&"8 4	4tzz1A1A1C1CzDDJD$ 4333c333CC  	R 	R 	RG&PQQQ	R
 		#tA >====!===Hs   - A
&+B   B=c                     |j         |j        |j        |j         fd} fd} fd} sg S  dd         }	 |dd          g }t	          t                               D ]P} |         }|                     d|                   }|r'|                    |                                           Q |k    rni t                      }	t                      }
t                      }t                     D ]\  }}||<   |	                    |            |	rOt           fd|	D             fd          }|         }|	                    |            ||
||          \  }
}|	Od	}|r ||          \  }}|                    ||f           t           |          |         |          }t          |
 fd
          } |||          }|r ||
||d                   \  }
}n|dz  }|t                      }|
D ]5} | |         |
|hz
            }|r|                    |d                    6 fd|D             }t          |fdd          }|S )aK  
    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                 4    t          | fd          }|S )Nc                 r      | d                  j         | d                  j                             S )Nr   r   LM)pairfr   orders    r   <lambda>z-_buchberger.<locals>.select.<locals>.<lambda>d   s1    UU<<$q'
qaz}+U+U%V%V r   key)min)Pprr2   r   r3   s     r   selectz_buchberger.<locals>.selecta   s,     VVVVVVWWW	r   c                     |                      fd|D                       }|sd S |                                }|vr't                    |<                       |           |j        |         fS )Nc                      g | ]
}|         S r   r   )r   jr2   s     r   r   z/_buchberger.<locals>.normal.<locals>.<listcomp>h   s    %%%QAaD%%%r   )remmoniclenappendr0   )r   JhIr2   s      r   normalz_buchberger.<locals>.normalg   s}    EE%%%%!%%%&& 		4		Az 1vv!41:r   c                 x   |         }|j         |                                 }t                      }|r|                                }|         }|j         } |          fd |          k    s6t	          fd|D                       s2t	          fd|D                       s|                    ||f           |t                      }	|rY|                                \  }}|         j         } |           |          k    s|	                    ||f           |Yt                      }
|r|                                \  }}|         j         }|         j         } ||          } |          r  |          |k    s |          |k    r|
                    ||f           ||
|	z  }
t                      }| rD|                                 }|         j         } |          s|                    |           | D|                    |           ||
fS )Nc                 J     |          j                   } |          S Nr/   )ipmLCMhgr2   mhmonomial_divr   s     r   lcm_dividesz0_buchberger.<locals>.update.<locals>.lcm_divides   s,     LQrUX..#|E1---r   c              3   .   K   | ]} |          V  d S rH   r   )r   ipxrN   s     r   	<genexpr>z._buchberger.<locals>.update.<locals>.<genexpr>   s-      66SC((666666r   c              3   :   K   | ]} |d                    V  dS r   Nr   )r   r9   rN   s     r   rQ   z._buchberger.<locals>.update.<locals>.<genexpr>   s1      ;;2KK1..;;;;;;r   )r0   copysetpopanyadd)r+   BihrC   CDigr   mgEB_newig1ig2mg1mg2LCM12G_newrK   rN   rL   r2   rM   r   r   s                    @@@r   updatez_buchberger.<locals>.updateu   s    bET FFHHEE 	 B"AB LR((E. . . . . . . . . |B##u,  6666A66666 ;;;;;;;;;  r2h!  	 $ EE 	 UUWWFB2B LR((E<B''50  r2h  	   	&uuwwHCC&)CC&)C Lc**E  <r** &S"%%.& Lb))U2& 		3*%%%  	& 	
  	B2B<B'' 		"  	 			"e|r   NTc                      g | ]
}|         S r   r   )r   xr2   s     r   r   z_buchberger.<locals>.<listcomp>   s    !!!!1!!!r   c                 $     | j                   S rH   r/   r2   r3   s    r   r4   z_buchberger.<locals>.<lambda>   s    qt r   r5   r   c                 0     |          j                   S rH   r/   )r   r2   r3   s    r   r4   z_buchberger.<locals>.<lambda>   s    UU1Q47^^ r   r   c                      g | ]
}|         S r   r   )r   r]   r2   s     r   r   z_buchberger.<locals>.<listcomp>   s    			B!B%			r   c                 $     | j                   S rH   r/   rk   s    r   r4   z_buchberger.<locals>.<lambda>  s    %%++ r   r6   reverse)r3   r   rM   r   ranger@   r>   rA   r?   rU   	enumeraterX   r7   removespolysorted)r2   r   r:   rE   rg   f1iprFr+   CPrC   rZ   reductions_to_zerora   rb   G1htGrr]   rD   rM   r   r   r3   s   `                    @@@@@r   r   r   2   sS   R JE$L$L$L           E E E E E E E EP  	 
111BqqqEs1vv 	% 	%A!AaeA %		!''))$$$7 	 	AAA	B!  1!	a
  "!!!!q!!!'<'<'<'<===qT	q"b!!2  " 
 $6"::S
		3*!C&!C&$''A33333444VAr]] 	$F1b"Q%((EArr!#  $  
B  VAbE1t8$$ 	FF2a5MMM				"			B 
----t	<	<	<BIr   c                    | j         }|j         }|                    ||          }|                    ||          }|                    ||          }|                     |          }|                    |          }	||	z
  }
|
S )z
    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
    )r0   r   rM   	mul_monom)p1p2r   LM1LM2re   m1m2s1s2r   s              r   rt   rt     s    
 %C
%Cc3''E			5#	&	&B			5#	&	&B	b		B	b		B
RAHr   c                     | d         S )Nr   r   r2   s    r   Signr         Q4Kr   c                     | d         S )Nr   r   r   s    r   Polynr     r   r   c                     | d         S )N   r   r   s    r   Numr   "  r   r   c                 
    | |fS rH   r   )monomialindexs     r   sigr   &  s    er   c                     | ||fS rH   r   )	signature
polynomialnumbers      r   lbpr   *  s    z6**r   c                     | d         |d         k    rdS | d         |d         k    r& || d                    ||d                   k     rdS dS )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   r   )uvr3   s      r   sig_cmpr   0  sb     	tad{ rtqt|  51;;qt$ 	21r   c                 6    | d           || d                   fS )z
    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   r   )r   r3   s     r   sig_keyr   E  s!     qTE551;;r   c                 V    t          t          | d         |          | d                   S )z
    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   rJ   s     r   sig_multr   Q  s&     |AaD!$$ad+++r   c                 ,   t          t          |           t          |          t          |           j        j                  dk     r|}n| }t          |           t          |          z
  }t          t          |          |t          |                    S )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   r3   r   r   )r2   r   max_polyrets       r   lbp_subr   ]  st     tAwwQq!4559 
((U1XX
CtH~~sCMM222r   c                     t          t          t          |           |d                   t          |                               |          t          |                     S )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   )r2   cxs     r   lbp_mul_termr   n  sD     xQA''q):):2)>)>AGGGr   c                    t          t          |           t          |          t          |           j        j                  dk    rdS t          |           t          |          k    r"t          |           t          |          k    rdS dS )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   r3   r   )r2   r   s     r   lbp_cmpr   x  sv     tAwwQq!455; rAww$q'' q66CFF? 	2 1r   c                     t          t          |           t          |           j        j                  t          |            fS )z4
    Key for comparing two labeled polynomials.
    )r   r   r   r   r3   r   r   s    r   lbp_keyr     s2     DGGU1XX]011CFF7;;r   c           	         |j         }t          |           j        }t          |          j        }t          |d         |d                   |j        f}t          |||          }t          |||          }t          t          t          |           t          |           	                                t          |                     |          }	t          t          t          |          t          |          	                                t          |                    |          }
t          |	|
          dk    r"t          |
          ||t          |	          || fS t          |	          || t          |
          ||fS )a7  
    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   )r2   r   r   r   ltfltgltumvmfrgrs              r   critical_pairr     s.    [F
((+C
((+C
s1vs1v
&
&
	3B	"c6	"	"B	"c6	"	"B 
c$q''588#8#8#:#:CFFCCR	H	HB	c$q''588#8#8#:#:CFFCCR	H	HB r2" 2R"ab2q11R"ab2q11r   c                     t          | d                   j        j        }t          | d         |t	          | d                             }t          |d         |t	          |d                             }t          ||          }|dk    rdS |dk    rlt          | d         |t	          | d                             }t          |d         |t	          |d                             }t          ||          }|dk    rdS dS )a}  
    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d0ry   c1d1s           r   cp_cmpr     s     1;; D	QqT4QqT	#	#B	QqT4QqT	#	#BBABw rAv 1tS1YY''1tS1YY''BOO7 	2 1r   c                     t          t          | d         |j        t          | d                                       t          t          | d         |j        t          | d                                       fS )z+
    Key for comparing critical pairs.
    r   r   r   r   )r   r   r   r   )r   r   s     r   cp_keyr     s]     C!diQqT3344gc!A$	SVWXYZW[S\S\>]>]6^6^__r   c                     t          t          | d         | d                   t          | d         | d                             S )z
    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     s8     <1r!u--|BqE2a5/I/IJJJr   c                 `   |D ]}| d         t          |          d         k     r+t          t          |          j        | d                   r dS | d         t          |          d         k    r?|t	          |          k     r,t          t          |          d         | d                   r dS dS )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   r0   r   )signnumrY   rC   s       r   is_rewritable_or_comparabler     s      
  
 7T!WWQZ 	aT!W55 tt 7d1ggaj  	 SVV|  #DGGAJQ88  445r   c                    t          |           j        j        }t          |           j        j        }t          |           s| S 	 | }|D ]}t          |          rt	          t          |          j        t          |           j                  rt          t          |           j        t          |          j        |          }t          t          t          |          |d                   t          |           |          dk     r"t          ||          }t          | |          }  n|| k    st          |           s| S )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)

    Tr   )r   r   r3   r   r   r0   r   r   r   r   r   r   r   )r2   rY   r3   r   r   rC   thps           r   	f5_reducer     s   6 !HHME1XX]!F88  		 		AQxx #E!HHKq==  qeAhhk6BBAxQ166QGG!K  *!Q//#ArNN6 	q 	Hr   c                     j          	  g t          t                               D ]>} |         }|                     d|                   }|r                    |           ? k    rng fdt          t                               D                                 fdd           fdt          t                              D             }|                    fdd           t                    }d}t          |          r|                                }t          |d         t          |d	                             rOt          |d
         t          |d                             rzt          |          }	t          |	          }t          t          |          t          |                                          |dz             }t          |          rg }
t          |          D ]\  }}t          |d         t          |d	                   |g          r|
                    |           Ft          |d
         t          |d                   |g          r|
                    |           t!          |
          D ]}||= D ]}t          |          r~t#          ||          }t          |d         t          |d	                   |g          rNt          |d
         t          |d                   |g          rz|                    |           |                    fdd           t          |          j        } |           t          d                   j                  k    r                    |           nWt                    D ]G\  }} |           t          |          j                  k    r                    ||            nH|dz  }n|dz  }t          |          d D             }t)          |          }t+          |fdd          S )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", http://arxiv.org/abs/1004.0084 (specifically
           v4)

    .. [2] Thomas Becker, Volker Weispfenning, Groebner bases: A computational
           approach to commutative algebra, 1993, p. 203, 216
    TNc           	      p    g | ]2}t          t          j        |d z             |         |d z             3S r   )r   r   
zero_monom)r   rw   rz   r   s     r   r   z_f5b.<locals>.<listcomp>p  s=    NNN1S!a%((!A$A	6	6NNNr   c                 >     t          |           j                  S rH   )r   r0   rk   s    r   r4   z_f5b.<locals>.<lambda>q  s    uQxx{++ r   ro   c                     g | ]B}t          |d z   t                              D ]}t          |         |                    CS r   )rq   r@   r   )r   rw   r=   rY   r   s      r   r   z_f5b.<locals>.<listcomp>t  sU    	_	_	_a%PQTUPUWZ[\W]W]J^J^	_	_Q-!adD
)
)	_	_	_	_r   c                 $    t          |           S rH   r   r   r   s    r   r4   z_f5b.<locals>.<lambda>u  s    6"d++ r   r   r   r   r   r   c                 $    t          |           S rH   r   r   s    r   r4   z_f5b.<locals>.<lambda>  s    6"d#3#3 r   r   c                 P    g | ]#}t          |                                          $S r   )r   r?   )r   r   s     r   r   z_f5b.<locals>.<listcomp>  s(    %%%aq		%%%r   c                 $     | j                   S rH   r/   rk   s    r   r4   z_f5b.<locals>.<lambda>  s    55;; r   )r3   rq   r@   r>   rA   sortrV   r   r   r   r   r   r   r   r?   rr   reversedr   r0   insertred_groebnerru   )rz   r   rw   rx   ry   r{   kr|   r   r   indicesr   rJ   qHrY   r3   s   ``             @@r   r    r    ;  s_   D JE 	
As1vv 	 	A!AaeA 6 	 	ONNNNc!ffNNNAFF++++TF::: 
`	_	_	_	_5Q==	_	_	_BGG++++TG:::AA
b'' 7$VVXX 'r!uc"Q%jj!<< 	&r!uc"Q%jj!<< 	2JJaOOQq))1q51188 (	$G"2 & &2.r!uc"Q%jj1#FF &NN1%%%%0ABqE

QCHH &NN1%%%g&&  qEE  " "88 "&q!T22B22a5#be**qcJJ ! 4RUC1JJLL ! IIbMMM GG3333TGBBB aAuQxx55qu111 %aLL  DAquQxx%%a"4"44 A FAA !#o b'' 7$t 	&%1%%%AQA!....====r   c                     d }| }g }|rI|                                 t          fd||z   D                       s|                               |I ||          S )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                     g }t          |           D ]G\  }}|                    | d|         | |dz   d         z             }|r|                    |           Hd |D             S )z1
        The actual reduction algorithm.
        Nr   c                 6    g | ]}|                                 S r   )r?   )r   rx   s     r   r   z3red_groebner.<locals>.reduction.<locals>.<listcomp>  s     %%%a		%%%r   )rr   r>   rA   )r8   Qrw   rx   rC   s        r   	reductionzred_groebner.<locals>.reduction  sx     aLL 	 	DAqaeaAi'((A %%1%%%%r   c              3   L   K   | ]}t          |j        j                  V  d S rH   )r   r0   )r   r2   f0s     r   rQ   zred_groebner.<locals>.<genexpr>  s2      @@Q#AD"%00@@@@@@r   )rV   rW   rA   )r+   r   r   rz   r   r   s        @r   r   r     s    
& 
& 
& 	
A
A
 UUWW@@@@!a%@@@@@ 	HHRLLL	   9Q<<r   c                     t          t          |                     D ]]}t          |dz   t          |                     D ]:}t          | |         | |         |          }|                    |           }|r  dS ;^dS )z)
    Check if G is a Groebner basis.
    r   FT)rq   r@   rt   r>   )r+   r   rw   r=   r   s        r   is_groebnerr     s     3q66]]  q1uc!ff%% 	 	AadAaD$''AaA uuu	 4r   c                    |j         |j        }|                     fd           t          |           D ]Q\  }}|j        |j        k    r dS | d|         | |dz   d         z   D ] }t          |j        |j                  r  dS !RdS )z2
    Checks if G is a minimal Groebner basis.
    c                 $     | j                   S rH   r/   r   r3   s    r   r4   zis_minimal.<locals>.<lambda>      qt r   r5   FNr   T)r3   r   r   rr   LCr   r   r0   )r+   r   r   rw   r   rC   r3   s         @r   
is_minimalr     s     JE[FFF$$$$F%%%!  146: 	552A21q566" 	 	Aad++ uuu	 4r   c                 J   |j         |j        }|                     fd           t          |           D ]j\  }}|j        |j        k    r dS |                                D ]=}| d|         | |dz   d         z   D ]"}t          |j        |d                   r   dS #>kdS )z2
    Checks if G is a reduced Groebner basis.
    c                 $     | j                   S rH   r/   r   s    r   r4   zis_reduced.<locals>.<lambda>  r   r   r5   FNr   r   T)	r3   r   r   rr   r   r   termsr   r0   )r+   r   r   rw   r   termrC   r3   s          @r   
is_reducedr     s     JE[FFF$$$$F%%%! ! !146: 	55GGII 	! 	!DrrUQq1uvvY& ! !#AD$q'22 ! 5555!!	!
 4r   c                    | j         |j         k    rt          d          | j         }|j        }| r|s|j        S t	          |           dk    rct	          |          dk    rPt          | j        |j                  }|                    | j        |j                  }|	                    ||          S | 
                                \  }} |
                                \  }}|                    ||          d |                                 D             }d |                                D             d |                                D             z   }	t          d          }
|                    |
f|j        z   t                    }|                    |          }|                    |	          }t#          ||g|          }d fd	|D             }fd
|d                                         D             }|                    |          }|S )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   c                 "    g | ]\  }}d |z   |fS r   r   r   monomcoeffs      r   r   z groebner_lcm.<locals>.<listcomp>4  %    EEE,%u%EEEr   c                 "    g | ]\  }}d |z   |fS )r   r   r  s      r   r   z groebner_lcm.<locals>.<listcomp>5  r  r   c                 $    g | ]\  }}d |z   | fS r   r   r  s      r   r   z groebner_lcm.<locals>.<listcomp>6  s'    EEE,%uf%EEEr   r   )symbolsr3   c                 `    t          fd|                                 D                        S )Nc              3   (   K   | ]}|         V  d S rH   r   )r   r  r=   s     r   rQ   z7groebner_lcm.<locals>.is_independent.<locals>.<genexpr>A  s'      88EuQx888888r   )rW   monoms)rC   r=   s    `r   is_independentz$groebner_lcm.<locals>.is_independent@  s1    8888QXXZZ8888888r   c                 ,    g | ]} |d           |S r  r   )r   rC   r  s     r   r   z groebner_lcm.<locals>.<listcomp>C  s*    444nnQ224!444r   c                 4    g | ]\  }}|d d         |z  fS rS   r   )r   r  r  lcms      r   r   z groebner_lcm.<locals>.<listcomp>E  s.    III<5%qrrE#I&IIIr   r   )r   r"   r   r   r@   r   r0   r  r   term_new	primitiver   r   r%   r  r   
from_termsr   )r2   r   r   r   r  r  fcgcf_termsg_termsr   t_ringrz   r+   basisr   h_termsrC   r  r  s                     @@r   groebner_lcmr    s     	v 312226D[F A y
1vv{ +s1vv{ +QT14((

14&&}}UE***KKMMEBKKMMEB
**R

CEE!''))EEEGEE!''))EEEEE!''))EEEFG 	c

AZZt| 33Z??F'""A'""AaVV$$E9 9 9 	5444U444AIIII1Q4::<<IIIG  AHr   c                    | j         |j         k    rt          d          | j         j        }|j        sD|                                 \  }} |                                \  }}|                    ||          }| |z                      t          | |          g          }t          |          dk    rt          d          |d         }|j        s||z  S |	                                S )z6Computes GCD of two polynomials using Groebner bases. r   r   zLength should be 1r   )
r   r"   r   r#   r  gcdquor  r@   r?   )r2   r   r   r  r  r  r   rC   s           r   groebner_gcdr  J  s    v 31222V]F? !AAjjR  	
1		<1%%&''A
1vv{ /-...	!A? 1uwwyyr   rH   )*__doc__sympy.core.symbolr   sympy.polys.monomialsr   r   r   r   sympy.polys.orderingsr   sympy.polys.polyerrorsr	   sympy.polys.polyconfigr
   r   r   rt   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  r   r   r   <module>r%     sC   ! ! $ # # # # # X X X X X X X X X X X X % % % % % % . . . . . . ( ( ( ( ( (& & & &PR R Rh  (        + + +  *	  	  	 , , ,3 3 3"H H H  *< < < 2  2  2F! ! !H` ` `K K K  60 0 0f}> }> }>@  @    (  (7 7 7r    r   