Ë
    7^(h.[  ã                   óú   — 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)y)%zGroebner bases algorithms. é    )ÚDummy)Úmonomial_mulÚmonomial_lcmÚmonomial_dividesÚterm_div)Úlex)ÚDomainError)ÚqueryNc                 ó  — |€t        d«      }t        t        dœ}	 ||   }|j
                  d}}|j                  r|j                  sA	 ||j                  |j                  «       ¬«      }}| D �cg c]  }|j                  |«      ‘Œ } } || |«      }|�/|D �	cg c]$  }	|	j                  «       d   j                  |«      ‘Œ& }}	|S # t        $ r t	        d|z  «      ‚w xY wc c}w # t        $ r t        d|z  «      ‚w xY wc c}	w )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'))Ú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
             úW/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/polys/groebnertools.pyr   r   
   s#  € ð €~Ü�zÓ"ˆô "ÜñÐð
uØ% fÑ-ˆ	ð —;‘; ˆD€Fà�?Š? &×"8Ò"8ð	4Ø˜tŸz™z°×1AÑ1AÓ1C˜zÓD�$ˆDð /2Ö3¨�A—J‘J˜tÕ$Ð3ˆCÐ3á�#�tÓ€AàÐØ:;Ö=°Qˆa�n‰nÓ˜qÑ!×*Ñ*¨4Õ0Ð=ˆÐ=à€Høô% ò uÜÐjÐmsÑsÓtÐtðuüò 4øô ò 	RÜÐGÈ&ÑPÓQÐQð	Rüò >s#   œC Á"C$ Á.CÂ)C?ÃCÃ$C<c                 ó6  ‡ ‡‡‡‡‡— |j                   Š|j                  Š|j                  Š|j                  Šˆ ˆˆfd„}ˆˆ fd„}ˆ ˆˆˆfd„}‰ sg S ‰ dd }	 |dd Š g }t	        t        ‰ «      «      D ]=  }‰ |   }|j                  ‰ d| «      }|sŒ|j                  |j                  «       «       Œ? ‰ |k(  rnŒbi Št        «       }	t        «       }
t        «       }t        ‰ «      D ]  \  }}|‰|<   |	j                  |«       Œ |	rGt        |	D �cg c]  }‰ |   ‘Œ	 c}ˆfd„¬«      }‰|   }|	j                  |«        ||
||«      \  }
}|	rŒGd}|rf ||«      \  }}|j                  ||f«       t        ‰ |   ‰ |   |«      }t        |
ˆ ˆfd	„¬«      } |||«      }|r ||
||d
   «      \  }
}n|d
z  }|rŒft        «       }|
D ])  } |‰ |   |
|hz
  «      }|sŒ|j                  |d
   «       Œ+ |D �cg c]  }‰ |   ‘Œ	 }}t        |ˆfd„d¬«      }|S c c}w c c}w )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                 ó,   •— t        | ˆˆˆfd„¬«      }|S )Nc                 ób   •—  ‰ ‰‰| d      j                   ‰| d      j                   «      «      S )Nr   r   ©ÚLM)ÚpairÚfr   Úorders    €€€r$   ú<lambda>z-_buchberger.<locals>.select.<locals>.<lambda>d   s+   ø€ ¡U©<¸¸$¸q¹'¹
¿¹ÀqÈÈaÉÁzÇ}Á}Ó+UÓ%V€ ó    ©Úkey)Úmin)ÚPÚprr+   r   r,   s     €€€r$   Úselectz_buchberger.<locals>.selecta   s   ø€ ô �ÕVÔWˆØˆ	r.   c                 óà   •— | j                  |D �cg c]  }‰|   ‘Œ	 c}«      }|sy |j                  «       }|‰vrt        ‰«      ‰|<   ‰j                  |«       |j                  ‰|   fS c c}w ©N)ÚremÚmonicÚlenÚappendr)   )r#   ÚJÚjÚhÚIr+   s       €€r$   Únormalz_buchberger.<locals>.normalg   sg   ø€ Ø�E‰E !Ö%˜Q�A�a“DÒ%Ó&ˆáØà—‘“	ˆAà˜‰zÜ˜1“v��!‘Ø—‘˜”à—4‘4˜˜1™�:Ðùò &s   ‘A+c                 óÎ  •‡‡‡— ‰|   }|j                   Š| j                  «       }t        «       }|r}|j                  «       }‰|   }|j                   } ‰‰|«      Šˆˆˆˆˆfd„Š ‰‰|«      ‰k(  s(t	        ˆfd„|D «       «      s't	        ˆfd„|D «       «      s|j                  ||f«       |rŒ}t        «       }	|rM|j                  «       \  }}‰|   j                   } ‰‰|«      Š ‰‰|«      ‰k(  s|	j                  ||f«       |rŒMt        «       }
|rq|j                  «       \  }}‰|   j                   }‰|   j                   } ‰||«      } ‰|‰«      r ‰|‰«      |k(  s ‰|‰«      |k(  r|
j                  ||f«       |rŒq|
|	z  }
t        «       }| r<| j                  «       }‰|   j                   } ‰|‰«      s|j                  |«       | rŒ<|j                  |«       ||
fS )Nc                 óB   •—  ‰‰‰|    j                   «      } ‰‰|«      S r6   r(   )ÚipÚmÚLCMhgr+   ÚmhÚmonomial_divr   s     €€€€€r$   Úlcm_dividesz0_buchberger.<locals>.update.<locals>.lcm_divides†   s$   ø€ á   Q r¡U§X¡XÓ.�Ù# E¨1Ó-Ð-r.   c              3   ó.   •K  — | ]  } ‰|«      –— Œ y ­wr6   © )Ú.0ÚipxrG   s     €r$   ú	<genexpr>z._buchberger.<locals>.update.<locals>.<genexpr>�   s   øè ø€ Ò6¨S™ C×(Ñ6ùs   ƒc              3   ó4   •K  — | ]  } ‰|d    «      –— Œ y­w)r   NrI   )rJ   r3   rG   s     €r$   rL   z._buchberger.<locals>.update.<locals>.<genexpr>Ž   s   øè ø€ Ò;°2™K¨¨1©×.Ñ;ùs   ƒ)r)   ÚcopyÚsetÚpopÚanyÚadd)r"   ÚBÚihr=   ÚCÚDÚigr#   ÚmgÚEÚB_newÚig1Úig2Úmg1Úmg2ÚLCM12ÚG_newrD   rG   rE   r+   rF   r   r   s                    @@@€€€€r$   Úupdatez_buchberger.<locals>.updateu   sÙ  û€ ð ˆb‰EˆØ�T‰Tˆð �F‰F‹HˆÜ‹Eˆáà—‘“ˆBØ�"‘ˆAØ—‘ˆBÙ   RÓ(ˆE÷.ð .ñ ˜B Ó# uÒ,ÜÓ6°AÔ6Ô6ÜÓ;¸Ô;Ô;Ø—‘�r˜2�h”ò! ô$ ‹Eˆáà—U‘U“W‰FˆB�Ø�2‘—‘ˆBÙ   RÓ(ˆEá  BÓ'¨5Ò0Ø—‘�r˜2�h”ò ô “ˆáà—u‘u“w‰HˆC�Ø�C‘&—)‘)ˆCØ�C‘&—)‘)ˆCÙ   cÓ*ˆEñ    rÔ*Ù˜S "Ó%¨Ò.Ù   bÓ)¨UÒ2Ø—	‘	˜3 ˜*Ô%ò ð 	�‰
ˆô “ˆáØ—‘“ˆBØ�2‘—‘ˆBá  BÔ'Ø—	‘	˜"”ò ð 	�	‰	�"Œà�eˆ|Ðr.   NTc                 ó(   •—  ‰| j                   «      S r6   r(   ©r+   r,   s    €r$   r-   z_buchberger.<locals>.<lambda>ß   s   ø€ ±°q·t±t³€ r.   r/   r   c                 ó.   •—  ‰‰|    j                   «      S r6   r(   )r#   r+   r,   s    €€r$   r-   z_buchberger.<locals>.<lambda>í   s   ø€ ¡U¨1¨Q©4¯7©7£^€ r.   r   c                 ó(   •—  ‰| j                   «      S r6   r(   rc   s    €r$   r-   z_buchberger.<locals>.<lambda>  s   ø€ ¡%¨¯©£+€ r.   ©r0   Úreverse)r,   r   rF   r   Úranger9   r7   r:   r8   rO   Ú	enumeraterR   r1   ÚremoveÚspolyÚsorted)r+   r   r4   r?   ra   Úf1ÚiÚpÚrÚFr"   ÚCPr=   ÚxrT   Úreductions_to_zeror[   r\   ÚG1ÚhtÚGrrW   r>   rF   r   r   r,   s   `                     @@@@@r$   r   r   2   sM  ý€ ðR �J‰J€Eà×$Ñ$€LØ×$Ñ$€LØ×$Ñ$€Löõ÷EñP Øˆ	ð 
‰1ˆ€Bà
Ø‰qˆEˆØˆä”s˜1“v“ò 	%ˆAØ�!‘ˆAØ—‘�a˜˜�e“ˆAâØ—	‘	˜!Ÿ'™'›)Õ$ð	%ð �Š7Øð ð 	€AÜ‹€AÜ‹€AÜ	‹€Bä˜!“ò ‰ˆˆ1Øˆˆ!‰Ø	�‰ˆa�ðñ ä˜qÖ!˜!��1“Ò!Ó'<Ô=ˆØˆq‰TˆØ	�‰�ŒÙ�q˜"˜bÓ!‰ˆˆ2ò ð Ðá
Ù˜"“:‰ˆˆSØ
�	‰	�3˜�*Ôä�!�C‘&˜!˜C™& $Ó'ˆä�AÔ3Ô4ˆÙ�A�r‹]ˆáÙ˜1˜b " Q¡%Ó(‰EˆA‰rà !Ñ#Ðò ô  
‹€Bàò ˆÙ�A�b‘E˜1 ˜t™8Ó$ˆâØ�F‰F�2�a‘5�Mð	ð Ö	�Bˆ!ˆB‹%Ð	€BÐ	ô 
�Ó-°tÔ	<€Bà€IùòK "ùò@ 
s   ÄHÇ0Hc                 óð   — | j                   }|j                   }|j                  ||«      }|j                  ||«      }|j                  ||«      }| 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
    )r)   r   rF   Ú	mul_monom)Úp1Úp2r   ÚLM1ÚLM2r_   Úm1Úm2Ús1Ús2r!   s              r$   rk   rk     sv   € ð
 �%‰%€CØ
�%‰%€CØ×Ñ˜c 3Ó'€EØ	×	Ñ	˜5 #Ó	&€BØ	×	Ñ	˜5 #Ó	&€BØ	�‰�bÓ	€BØ	�‰�bÓ	€BØ
ˆR‰€AØ€Hr.   c                 ó   — | d   S )Nr   rI   ©r+   s    r$   ÚSignr„     ó   € ØˆQ‰4€Kr.   c                 ó   — | d   S )Nr   rI   rƒ   s    r$   ÚPolynr‡     r…   r.   c                 ó   — | d   S )Né   rI   rƒ   s    r$   ÚNumrŠ   "  r…   r.   c                 ó
   — | |fS r6   rI   )ÚmonomialÚindexs     r$   ÚsigrŽ   &  s   € Ø�eÐÐr.   c                 ó   — | ||fS r6   rI   )Ú	signatureÚ
polynomialÚnumbers      r$   Úlbpr“   *  s   € Ø�z 6Ð*Ð*r.   c                 ób   — | d   |d   kD  ry| d   |d   k(  r || d   «       ||d   «      k  ryy)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   rI   )ÚuÚvr,   s      r$   Úsig_cmpr˜   0  sE   € ð 	ˆ�tˆa�‰d‚{ØØˆ�tˆq�‰t‚|ñ ��1‘‹;™˜q ™t›Ò$ØØr.   c                 ó$   — | 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   rI   )r!   r,   s     r$   Úsig_keyrš   E  s   € ð ˆq‰TˆE‘5˜˜1™“;ÐÐr.   c                 ó:   — 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!   rC   s     r$   Úsig_multrœ   Q  s    € ô Œ|˜A˜a™D !Ó$ 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   r,   r“   rŠ   )r+   r#   Úmax_polyÚrets       r$   Úlbp_subr    ]  s^   € ô Œt�A‹wœ˜Q›¤ q£§¡×!4Ñ!4Ó5¸Ò9Ø‰àˆä
�‹(”U˜1“XÑ
€CäŒt�H‹~˜s¤C¨£MÓ2Ð2r.   c                 óŠ   — t        t        t        | «      |d   «      t        | «      j	                  |«      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Š   )r+   Úcxs     r$   Úlbp_mul_termr¤   n  s5   € ô Œxœ˜Q›  A¡Ó'¬¨q«×):Ñ):¸2Ó)>ÄÀAÃÓGÐGr.   c                 óâ   — t        t        | «      t        |«      t        | «      j                  j                  «      dk(  ryt        | «      t        |«      k(  rt        | «      t        |«      kD  ryy)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   € ô Œt�A‹wœ˜Q›¤ q£§¡×!4Ñ!4Ó5¸Ò;ØÜˆAƒw”$�q“'ÒÜˆq‹6”C˜“FŠ?Øð r.   c                 ó~   — t        t        | «      t        | «      j                  j                  «      t        | «       fS )z4
    Key for comparing two labeled polynomials.
    )rš   r„   r‡   r   r,   rŠ   rƒ   s    r$   Úlbp_keyr¨   �  s.   € ô ”D˜“GœU 1›XŸ]™]×0Ñ0Ó1´C¸³F°7Ð;Ð;r.   c           	      ód  — |j                   }t        | «      j                  }t        |«      j                  }t        |d   |d   «      |j                  f}t        |||«      }t        |||«      }t        t        t        | «      t        | «      j                  «       t        | «      «      |«      }	t        t        t        |«      t        |«      j                  «       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¦   )r+   r#   r   r   ÚltfÚltgÚltÚumÚvmÚfrÚgrs              r$   Úcritical_pairr´   –  sü   € ð �[‰[€Fä
�‹(�+‰+€CÜ
�‹(�+‰+€CÜ
�s˜1‘v˜s 1™vÓ
&¨¯
©
Ð	3€Bä	�"�c˜6Ó	"€BÜ	�"�c˜6Ó	"€Bô 
”cœ$˜q›'¤5¨£8×#8Ñ#8Ó#:¼CÀ»FÓCÀRÓ	H€BÜ	”cœ$˜q›'¤5¨£8×#8Ñ#8Ó#:¼CÀ»FÓCÀRÓ	H€Bô ˆr�2ƒ˜"ÒÜ�R“˜"˜a¤ b£¨2¨qÐ1Ð1ä�R“˜"˜a¤ b£¨2¨qÐ1Ð1r.   c                 óz  — t        | d   «      j                  j                  }t        | d   |t	        | d   «      «      }t        |d   |t	        |d   «      «      }t        ||«      }|dk(  ry|dk(  rJt        | d   |t	        | d   «      «      }t        |d   |t	        |d   «      «      }t        ||«      }|dk(  ryy)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Úd0rp   Úc1Úd1s           r$   Úcp_cmpr¿   ¹  s»   € ô ��1‘‹;×Ñ× Ñ €Dä	ˆQˆq‰T�4œ˜Q˜q™T›Ó	#€BÜ	ˆQˆq‰T�4œ˜Q˜q™T›Ó	#€Bä��B‹€AàˆB‚wØØˆA‚vÜ��1‘�tœS  1¡›YÓ'ˆÜ��1‘�tœS  1¡›YÓ'ˆä�B˜‹Oˆà�Š7Øð r.   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Á   Ý  sP   € ô ”C˜˜!™˜dŸi™i¬¨Q¨q©T«Ó3Ó4´g¼cÀ!ÀAÁ$ÈÏ	É	ÔSVÐWXÐYZÑW[ÓS\Ó>]Ó6^Ð_Ð_r.   c                 óZ   — 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Å   ä  s/   € ô ”<  1¡ r¨!¡uÓ-¬|¸B¸q¹EÀ2ÀaÁ5Ó/IÓJÐJr.   c                 ó  — |D ]{  }| d   t        |«      d   k  r$t        t        |«      j                  | d   «      r y| d   t        |«      d   k(  sŒP|t	        |«      k  sŒ_t        t        |«      d   | d   «      sŒ{ y y)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ÚnumrS   r=   s       r$   Úis_rewritable_or_comparablerÉ   í  s   € ð ò 
 ˆà�‰7”T˜!“W˜Q‘ZÒÜ¤ a£§¡¨T°!©WÔ5Ùð �‰7”d˜1“g˜a‘jÓ Ø”S˜“V‹|Ü#¤D¨£G¨A¡J°°Q±Õ8Ùð
 ð r.   c                 óH  — t        | «      j                  j                  }t        | «      j                  j                  }t        | «      s| S 	 | }|D ]½  }t        |«      sŒt	        t        |«      j
                  t        | «      j
                  «      sŒBt        t        | «      j                  t        |«      j                  |«      }t        t        t        |«      |d   «      t        | «      |«      dk  sŒ¥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)

    r   )r‡   r   r,   r   r   r)   r   rª   r˜   rœ   r„   r¤   r    )r+   rS   r,   r   r#   r=   ÚtÚhps           r$   Ú	f5_reducerÍ     så   € ô6 �!‹H�M‰M×Ñ€EÜ�1‹X�]‰]×!Ñ!€Fä�Œ8Øˆà
Øˆàò 		ˆAÜ�Q�xÜ#¤E¨!£H§K¡K´°q³·±Õ=Ü ¤ q£§¡¬e°A«h¯k©k¸6ÓB�AÜœx¬¨Q«°°1±Ó6¼¸Q»ÀÓGÈ!ÓKô *¨!¨QÓ/˜Ü# A r›N˜Ùð		ð �Š6œ˜qœØˆHð r.   c           
      óØ  ‡‡— ‰j                   Š| }	 |} g }t        t        | «      «      D ]/  }| |   }|j                  | d| «      }|sŒ|j	                  |«       Œ1 | |k(  rnŒQt        t        | «      «      D �cg c],  }t        t        ‰j                  |dz   «      | |   |dz   «      ‘Œ. }}|j                  ˆfd„d¬«       t        t        |«      «      D ��cg c]2  }t        |dz   t        |«      «      D ]  }t        ||   ||   ‰«      ‘Œ Œ4 }}}|j                  ˆfd„d¬«       t        |«      }d}	t        |«      �rW|j                  «       }
t        |
d   t        |
d   «      |«      rŒ9t        |
d	   t        |
d
   «      |«      rŒVt        |
«      }t        ||«      }t        t        |«      t!        |«      j#                  «       |dz   «      }t!        |«      �r¨g }t%        |«      D ]c  \  }}
t        |
d   t        |
d   «      |g«      r|j	                  |«       Œ5t        |
d	   t        |
d
   «      |g«      sŒS|j	                  |«       Œe t'        |«      D ]  }||= Œ |D ]h  }t!        |«      sŒt        ||‰«      }
t        |
d   t        |
d   «      |g«      rŒ:t        |
d	   t        |
d
   «      |g«      rŒX|j	                  |
«       Œj |j                  ˆfd„d¬«       t!        |«      j(                  } ‰|«       ‰t!        |d   «      j(                  «      k  r|j	                  |«       nKt%        |«      D ]=  \  }} ‰|«       ‰t!        |«      j(                  «      kD  sŒ+|j+                  ||«        n |dz  }n|	dz  }	t        |«      r�ŒW|D �cg c]  }t!        |«      j#                  «       ‘Œ }}t-        |‰«      }t/        |ˆfd„d¬«      S c c}w c c}}w c c}w )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        | «      j                  «      S r6   )r‡   r)   rc   s    €r$   r-   z_f5b.<locals>.<lambda>q  s   ø€ ™œu Q›xŸ{™{Ó+€ r.   rf   c                 ó   •— t        | ‰«      S r6   ©rÁ   ©rÄ   r   s    €r$   r-   z_f5b.<locals>.<lambda>u  s   ø€ œ6 " dÓ+€ r.   r   r‰   r¶   r·   c                 ó   •— t        | ‰«      S r6   rÑ   rÒ   s    €r$   r-   z_f5b.<locals>.<lambda>¢  s   ø€ ¤6¨"¨dÓ#3€ r.   r•   c                 ó(   •—  ‰| j                   «      S r6   r(   rc   s    €r$   r-   z_f5b.<locals>.<lambda>¸  s   ø€ ¡5¨¯©£;€ r.   )r,   rh   r9   r7   r:   r“   rŽ   Ú
zero_monomÚsortr´   rP   rÉ   rŠ   rÅ   rÍ   r„   r‡   r8   ri   Úreversedr)   ÚinsertÚred_groebnerrl   )rq   r   rS   rn   ro   rp   r<   rr   Úkrt   rÄ   r!   Úindicesr#   rC   ÚqÚHr,   s    `               @r$   r   r   ;  s§  ù€ ðD �J‰J€Eð 	
€AØ
ØˆØˆä”s˜1“v“ò 	ˆAØ�!‘ˆAØ—‘�a˜˜�e“ˆAâØ—‘˜•ð	ð �Š6Øð ô AFÄcÈ!ÃfÃÖN¸1ŒŒS�—‘ ! a¡%Ó(¨!¨A©$°°A±Õ	6ÐN€AÐNØ‡F�FÓ+°T€FÔ:ô 49¼¸Q»³=×	_¨aÌ%ÐPQÐTUÑPUÔWZÐ[\ÓW]ÓJ^Ò	_ÀQŒ-˜˜!™˜a ™d DÕ
)Ð	_Ð
)Ð	_€BÑ	_Ø‡G�GÓ+°T€GÔ:äˆA‹€AàÐä
ˆb�'Ø�V‰V‹Xˆô ' r¨!¡u¬c°"°Q±%«j¸!Ô<ØÜ& r¨!¡u¬c°"°Q±%«j¸!Ô<Øä�2‹Jˆä�a˜‹Oˆä”�Q“œ˜q›Ÿ™Ó)¨1¨q©5Ó1ˆä��8àˆGÜ" 2›ò &‘��2Ü.¨r°!©u´c¸"¸Q¹%³jÀ1À#ÔFØ—N‘N 1Õ%Ü0°°A±¼¸B¸q¹E»
ÀQÀCÕHØ—N‘N 1Õ%ð	&ô ˜gÓ&ò �Ø�q‘Eðð ò "�Ü˜•8Ü& q¨!¨TÓ2�BÜ2°2°a±5¼#¸bÀ¹e»*ÀqÀcÔJØ Ü4°R¸±U¼CÀÀ1Á»JÈÈÔLØ à—I‘I˜b•Mð"ð �G‰GÓ3¸TˆGÔBô �a“—‘ˆAÙ�Q‹x™5¤ q¨¡u£§¡Ó1Ò1Ø—‘˜•ä% a›Lò ‘D�A�qÙ˜Q“x¡%¬¨a«¯©Ó"4Ó4ØŸ™  AœÙðð
 �‰F‰Að  !Ñ#Ðôo ˆbŽ'ðt $%Ö%˜aŒˆq‹�‰Õ	Ð%€AÐ%Ü�Q˜Ó€Aä�!Ó.¸Ô=Ð=ùòQ 	Oùó 
`ùòB 	&s   Á91OÃ7O!Î O'c                 óœ   ‡— d„ }| }g }|r;|j                  «       Št        ˆfd„||z   D «       «      s|j                  ‰«       |rŒ; ||«      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 ]6  \  }}|j                  | d| | |dz   d z   «      }|sŒ&|j                  |«       Œ8 |D �cg c]  }|j                  «       ‘Œ c}S c c}w )z1
        The actual reduction algorithm.
        Nr   )ri   r7   r:   r8   )r2   ÚQrn   ro   r=   s        r$   Ú	reductionzred_groebner.<locals>.reductionÂ  sm   € ð ˆÜ˜a“Lò 	‰DˆAˆqØ—‘�a˜˜�e˜a  A¡ ˜iÑ'Ó(ˆAÚØ—‘˜•ð	ð
 $%Ö%˜a�—‘•	Ò%Ð%ùÒ%s   ÁA%c              3   ó^   •K  — | ]$  }t        |j                  ‰j                  «      –— Œ& y ­wr6   )r   r)   )rJ   r+   Úf0s     €r$   rL   zred_groebner.<locals>.<genexpr>Ô  s!   øè ø€ Ò@°QÔ# A§D¡D¨"¯%©%×0Ñ@ùs   ƒ*-)rP   rQ   r:   )r"   r   rá   rq   rÝ   rã   s        @r$   rÙ   rÙ   »  sP   ø€ ò
&ð 	
€AØ
€Aá
Ø�U‰U‹WˆäÓ@¸!¸a¹%Ô@Ô@Ø�H‰H�RŒLò	 ñ �Q‹<Ðr.   c                 óÂ   — t        t        | «      «      D ]H  }t        |dz   t        | «      «      D ]+  }t        | |   | |   |«      }|j                  | «      }|sŒ*  y ŒJ y)z)
    Check if G is a Groebner basis.
    r   FT)rh   r9   rk   r7   )r"   r   rn   r<   r!   s        r$   Úis_groebnerrå   Û  sg   € ô ”3�q“6‹]ò ˆÜ�q˜1‘uœc !›fÓ%ò 	ˆAÜ�a˜‘d˜A˜a™D $Ó'ˆAØ—‘�a“ˆAÚÚñ		ðð r.   c                 ó(  ‡— |j                   Š|j                  }| j                  ˆfd„¬«       t        | «      D ]V  \  }}|j                  |j
                  k7  r y| d| | |dz   d z   D ]%  }t        |j                  |j                  «      sŒ$  y ŒX y)z2
    Checks if G is a minimal Groebner basis.
    c                 ó(   •—  ‰| j                   «      S r6   r(   ©r#   r,   s    €r$   r-   zis_minimal.<locals>.<lambda>ð  ó   ø€ ™˜qŸt™t›€ r.   r/   FNr   T)r,   r   rÖ   ri   ÚLCr«   r   r)   )r"   r   r   rn   r#   r=   r,   s         @r$   Ú
is_minimalrë   é  sŽ   ø€ ð �J‰J€EØ�[‰[€Fà‡F�FÓ$€FÔ%ä˜!“ò ‰ˆˆ1Ø�4‰4�6—:‘:ÒÙà�2�A�˜˜1˜q™5˜6˜Ñ"ò 	ˆAÜ §¡ a§d¡dÕ+Úñ	ð	ð r.   c                 óF  ‡— |j                   Š|j                  }| j                  ˆfd„¬«       t        | «      D ]e  \  }}|j                  |j
                  k7  r y|j                  «       D ]2  }| d| | |dz   d z   D ]  }t        |j                  |d   «      sŒ   y Œ4 Œg y)z2
    Checks if G is a reduced Groebner basis.
    c                 ó(   •—  ‰| j                   «      S r6   r(   rè   s    €r$   r-   zis_reduced.<locals>.<lambda>  ré   r.   r/   FNr   r   T)	r,   r   rÖ   ri   rê   r«   Útermsr   r)   )r"   r   r   rn   r#   Útermr=   r,   s          @r$   Ú
is_reducedrð   ý  s£   ø€ ð �J‰J€EØ�[‰[€Fà‡F�FÓ$€FÔ%ä˜!“ò !‰ˆˆ1Ø�4‰4�6—:‘:ÒÙà—G‘G“Iò 	!ˆDØ�r˜�U˜Q˜q 1™u˜v˜YÑ&ò !�Ü# A§D¡D¨$¨q©'Õ2Û ñ!ñ	!ð	!ð r.   c                 ó–  — | j                   |j                   k7  rt        d«      ‚| j                   }|j                  }| r|s|j                  S t	        | «      dk  rft	        |«      dk  rXt        | j                  |j                  «      }|j                  | j                  |j                  «      }|j                  ||«      S | j                  «       \  }} |j                  «       \  }}|j                  ||«      }| j                  «       D ��cg c]  \  }}d|z   |f‘Œ }	}}|j                  «       D ��cg c]  \  }}d|z   |f‘Œ c}}|j                  «       D ��cg c]  \  }}d|z   | f‘Œ c}}z   }
t        d«      }|j                  |f|j                  z   t        ¬«      }|j!                  |	«      }|j!                  |
«      }t#        ||g|«      }d„ }|D �cg c]  } ||d«      sŒ|‘Œ }}|d   j                  «       D ��cg c]  \  }}|dd	 ||z  f‘Œ }}}|j!                  |«      }|S c c}}w c c}}w c c}}w c c}w c c}}w )
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                 óJ   ‡— t        ˆfd„| j                  «       D «       «       S )Nc              3   ó(   •K  — | ]	  }|‰   –— Œ y ­wr6   rI   )rJ   Úmonomr<   s     €r$   rL   z7groebner_lcm.<locals>.is_independent.<locals>.<genexpr>A  s   øè ø€ Ò8 E�u˜Q•xÑ8ùs   ƒ)rQ   Úmonoms)r=   r<   s    `r$   Úis_independentz$groebner_lcm.<locals>.is_independent@  s   ø€ ÜÓ8¨Q¯X©X«ZÔ8Ó8Ð8Ð8r.   r   N)r   r   r   r¸   r9   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_ringrq   r"   Úbasisrø   r=   rÝ   Úh_termss                       r$   Úgroebner_lcmr    s  € ð  	‡v�v�—‘ÒÜÐ1Ó2Ð2à�6‰6€DØ�[‰[€Fá‘AØ�y‰yÐä
ˆ1ƒv�‚{”s˜1“v ’{Ü˜QŸT™T 1§4¡4Ó(ˆØ—
‘
˜1Ÿ4™4 §¡Ó&ˆØ�}‰}˜U EÓ*Ð*à�K‰K‹M�E€BˆØ�K‰K‹M�E€Bˆà
�*‰*�R˜Ó
€Cà:;¿'¹'»)×E©,¨%°�˜‘˜uÒ%ÐE€GÑEØ:;¿'¹'»)×E©,¨%°�˜‘˜uÒ%ÓEØ:;¿'¹'»)×E©,¨%°�˜‘˜u˜fÒ%ÓEñF€Gô 	ˆc‹
€AØ�Z‰Z   t§|¡|Ñ 3¼3ˆZÓ?€Fà×Ñ˜'Ó"€AØ×Ñ˜'Ó"€Aä�a˜�V˜VÓ$€Eò9ð Ö4�™n¨Q°Õ2Š!Ð4€AÐ4à;<¸Q¹4¿:¹:»<×I©<¨5°%��q�r�˜E #™IÒ&ÐI€GÑIØ�‰˜Ó €Aà€Hùó) FùÛEùÛEùò 	5ùãIs$   ÄH.Ä3H4ÅH:ÇI Ç(I ÈIc                 óº  — | j                   |j                   k7  rt        d«      ‚| j                   j                  }|j                  s8| j	                  «       \  }} |j	                  «       \  }}|j                  ||«      }| |z  j                  t        | |«      g«      }t        |«      dk7  rt        d«      ‚|d   }|j                  s|z  S |j                  «       S )z6Computes GCD of two polynomials using Groebner bases. rò   r   zLength should be 1r   )
r   r   r   r   rû   ÚgcdÚquor  r9   r8   )r+   r#   r   rþ   rÿ   r  rÝ   r=   s           r$   Úgroebner_gcdr	  J  s¸   € à‡v�v�—‘ÒÜÐ1Ó2Ð2Ø�V‰V�]‰]€Fà�?Š?Ø—‘“‰ˆˆAØ—‘“‰ˆˆAØ�j‰j˜˜RÓ ˆà	
ˆ1‰�	‰	”<  1Ó%Ð&Ó'€Aä
ˆ1ƒv�‚{ÜÐ-Ó.Ð.Ø	ˆ!‰€Aà�?Š?Ø�1‰uˆà�w‰w‹yÐr.   r6   )*Ú__doc__Úsympy.core.symbolr   Úsympy.polys.monomialsr   r   r   r   Úsympy.polys.orderingsr   Úsympy.polys.polyerrorsr	   Úsympy.polys.polyconfigr
   r   r   rk   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	  rI   r.   r$   ú<module>r     sµ   ðÙ !õ $ß XÓ XÝ %Ý .Ý (ó&òPRòhò(òòòò+òò*	 ò,ò3ò"Hòò*<ò 2òF!òH`òKòò60òf}>ò@ò@òò(ò(7órr.   