K i.[dZddlmZddlmZmZmZmZddlm Z ddl m Z ddl m Z d$dZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%d Z&d!Z'd"Z(d#Z)y)%zGroebner bases algorithms. )Dummy) monomial_mul monomial_lcmmonomial_dividesterm_div)lex) DomainError)queryNc| td}ttd} ||}|j d}}|j r |jsA ||j|j}}|Dcgc]}|j|}}|||}|/|D cgc]$} | jdj|&}} |S#t$rt d|zwxYwcc}w#t$rtd|zwxYwcc} 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``. Ngroebner) buchbergerf5bzO'%s' is not a valid Groebner bases algorithm (valid are 'buchberger' and 'f5b'))domainz'Cannot compute a Groebner basis over %s) r _buchberger_f5bKeyError ValueErrorris_Fieldhas_assoc_Fieldclone get_fieldset_ringr clear_denoms) seqringmethod_groebner_methods _groebnerrorigsGgs _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/groebnertools.pyr r s#~z"" u%f- ;;DF ??&"8"8 4tzz1A1A1CzD$D/23AJJt$3C3#tA :; =Qannq!**40 = = H% ujmssttu4 RG&PQ Q R >s#C"C$.C)C?C$C<c6|j|j|j|jfd}fd}fd}sgSdd} |ddg}t t D]=}|}|j d|}|s|j|j?|k(rnbit} t} t} tD]\}} || <| j|| rGt| D cgc]} |  c} fd} | }| j||| | |\} } | rGd}| rf|| \}}| j||ft|||} t| fd }|| |}|r|| | |d \} } n|d z }| rft}| D])}||| |hz }|s|j|d +|Dcgc]}| }}t|fd d }|Scc} wcc}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|fd}|S)Ncb|dj|djS)NrrLM)pairfrorders r$z-_buchberger..select..ds+U<$q' qaz}}+U%Vkey)min)Pprr+rr,s r$selectz_buchberger..selectasV W r.c|j|Dcgc]}| c}}|sy|j}|vrt|<j||j|fScc}wN)remmoniclenappendr))r#JjhIr+s r$normalz_buchberger..normalgsg EE!%QAaD% & Az1v! 441: &s A+c|}|j|j}t}|r}|j}|}|j}|fd|k(s(t fd|Ds't fd|Ds|j ||f|r}t} |rM|j\}}|j}||k(s| j ||f|rMt} |rq|j\} } | j} | j}| |}|r| |k(s ||k(r| j | | f|rq| | z} t}|r<|j}|j}|s|j ||r<|j ||| fS)NcB|j}|Sr6r()ipmLCMhgr+mh monomial_divrs r$ lcm_dividesz0_buchberger..update..lcm_dividess$ QrUXX.#E1--r.c3.K|] }|ywr6).0ipxrGs r$ z._buchberger..update..s6S C(6sc34K|]}|dyw)rNrI)rJr3rGs r$rLz._buchberger..update..s;2K1.;s)r)copysetpopanyadd)r"Bihr=CDigr#mgEB_newig1ig2mg1mg2LCM12G_newrDrGrEr+rFrrs @@@r$updatez_buchberger..updateus bE TT FFH EB"AB R(E . . B#u,6A66;;;r2h!$ EUUWFB2B R(EB'50r2huuwHCC&))CC&))C c*E r*S"%. b)U2 3*%  B2BB' "   " e|r.NTc(|jSr6r(r+r,s r$r-z_buchberger..sqttr.r/rc.|jSr6r()r#r+r,s r$r-z_buchberger..sU1Q477^r.rc(|jSr6r(rcs r$r-z_buchberger..s%+r.r0reverse)r,rrFrranger9r7r:r8rO enumeraterRr1removespolysorted)r+rr4r?raf1iprFr"CPr=xrTreductions_to_zeror[r\G1htGrrWr>rFrrr,s` @@@@@r$rr2sMR JJE$$L$$L$$L EP   1B  qE s1v %A!Aae A !'')$  % 7   A A A B! 1! a q!!1!'< = qT  q"b!2  ":S 3* !C&!C&$ ' A3 4 Ar] 1b"Q%(EAr ! #  B AbE1t8 $ FF2a5M   B!B% B  -t v otherwise rrrI)uvr,s r$sig_cmpr0sE tad{tqt| 1;qt $ r.c$|d ||dfS)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 rrrI)r!r,s r$sig_keyrEsqTE51; r.c:tt|d||dS)z Multiply a signature by a monomial. The product of a signature (m, i) and a monomial n is defined as (m * t, i). rr)rr)r!rCs r$sig_multrQs  |AaD!$ad ++r.ctt|t|t|jjdkr|}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)rrrrr,rr)r+r#max_polyrets r$lbp_subr]s^tAwQq!4!459 (U1X C tH~sCM 22r.cttt||dt|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)rrrrmul_termr)r+cxs r$ lbp_mul_termrns5 xQA'q):):2)>A GGr.ctt|t|t|jjdk(ryt|t|k(rt |t |kDryy)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 rr)rrrrr,r)r+r#s r$lbp_cmprxsUtAwQq!4!45; Aw$q' q6CF? r.c~tt|t|jjt | fS)z4 Key for comparing two labeled polynomials. )rrrrr,rrs r$lbp_keyrs. DGU1X]]00 1CF7 ;;r.c d|j}t|j}t|j}t|d|d|jf}t |||}t |||}t tt|t|jt||} t tt|t|jt||} t| | dk(rt| ||t| ||fSt| ||t| ||fS)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. rr) rrLTronerrrr leading_termrr) r+r#rrltfltgltumvmfrgrs r$ critical_pairrs[[F (++C (++C s1vs1v & 3B "c6 "B "c6 "B c$q'58#8#8#:CFCR HB c$q'58#8#8#:CFCR HBr2"R"ab2q11R"ab2q11r.czt|djj}t|d|t |d}t|d|t |d}t ||}|dk(ry|dk(rJt|d|t |d}t|d|t |d}t ||}|dk(ryy)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 rrrr)rrzerorrr)cdrc0d0rpc1d1s r$cp_cmprs 1;   D QqT4QqT #B QqT4QqT #BBABwAv 1tS1Y ' 1tS1Y ' BO 7 r.c tt|d|jt|dtt|d|jt|dfS)z+ Key for comparing critical pairs. rrrr)rrrr)rrs r$cp_keyrsP C!diiQqT3 4gc!A$ SVWXYZW[S\>]6^ __r.cZtt|d|dt|d|dS)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]. rrr)rr)cps r$s_polyrs/ <1r!u-|BqE2a5/I JJr.c|D]{}|dt|dkr$tt|j|dry|dt|dk(sP|t |ks_tt|d|ds{yy)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]. rrTF)rrrr)r)signnumrSr=s r$is_rewritable_or_comparablers   7T!WQZ a T!W5 7d1gaj SV|#DGAJQ8   r.cHt|jj}t|jj}t|s|S |}|D]}t|st t|j t|j sBt t|jt|j|}ttt||dt||dkst||}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) rrr,rrr)rrrrrrr)r+rSr,rr#r=thps r$ f5_reducers6 !HMM  E 1X]] ! !F 8   AQx#E!HKKq= qeAhkk6BAxQ16QG!K*!Q/#ArN  6qH r.c j|} |}g}tt|D]/}||}|j|d|}|s|j |1||k(rnQtt|Dcgc],}t t j|dz|||dz.}}|jfddtt|Dcgc]2}t|dzt|D]}t||||4}}}|jfddt|}d} t|rW|j} t| dt| d|r9t| d t| d |rVt| } t| |}t t|t!|j#|dz}t!|rg} t%|D]c\}} t| dt| d|gr| j |5t| d t| d |gsS| j |et'| D]}||=|D]h} t!| st|| } t| dt| d|gr:t| d t| d |grX|j | j|jfd dt!|j(}|t!|d j(kr|j |nKt%|D]=\}}|t!|j(kDs+|j+||n|dz }n| dz } t|rW|D cgc]} t!| j#}} t-|}t/|fd dScc}wcc}}wcc} 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 TNrc:t|jSr6)rr)rcs r$r-z_f5b..qsuQx{{+r.rfct|Sr6rrrs r$r-z_f5b..us6"d+r.rrrrct|Sr6rrs r$r-z_f5b..s6"d#3r.rc(|jSr6r(rcs r$r-z_f5b..s5;r.)r,rhr9r7r:rr zero_monomsortrrPrrrrrrr8rireversedr)insert red_groebnerrl)rqrrSrnrorpr<rrkrtrr!indicesr#rCqHr,s ` @r$rr;sD JJE A   s1v A!Aae A   6  AFc!f N1S!a% (!A$A 6NANFF+TF:49Q= _a%PQTUPUWZ[\W]J^ _Q-!adD ) _ ) _B _GG+TG: AA b' VVX 'r!uc"Q%j! <  &r!uc"Q%j! <  2J aO Qq)1q5 1 8G"2 &2.r!uc"Q%j1#FNN1%0ABqE QCHNN1%  & g& qE  "8&q!T2B22a5#be*qcJ 4RUC1JL IIbM " GG3TG Ba AQx5qu11 %aLDAqQx%a "44A FA ! # o b't$%%aq %A%QA !. ==Q O `B &s91O7O! O'cd}|}g}|r;|jtfd||zDs|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. cg}t|D]6\}}|j|d|||dzdz}|s&|j|8|Dcgc]}|jc}Scc}w)z1 The actual reduction algorithm. Nr)rir7r:r8)r2Qrnror=s r$ reductionzred_groebner..reductionsm aL DAqaeaAi'(A   $%%a %%%s A%c3^K|]$}t|jj&ywr6)rr))rJr+f0s r$rLzred_groebner..s!@Q#ADD"%%0@s*-)rPrQr:)r"rrrqrrs @r$rrsP & A A UUW@!a%@@ HHRL  Q<r.ctt|D]H}t|dzt|D]+}t|||||}|j|}|s*yJy)z) Check if G is a Groebner basis. rFT)rhr9rkr7)r"rrnr<r!s r$ is_groebnerrsg3q6]q1uc!f% AadAaD$'AaA   r.c(|j|j}|jfdt|D]V\}}|j|j k7ry|d|||dzdzD]%}t |j|js$yXy)z2 Checks if G is a minimal Groebner basis. c(|jSr6r(r#r,s r$r-zis_minimal..qttr.r/FNrT)r,rrriLCrrr))r"rrrnr#r=r,s @r$ is_minimalrs JJE [[FFF$F%! 1 446:: 2A1q56" Aadd+   r.cF|j|j}|jfdt|D]e\}}|j|j k7ry|j D]2}|d|||dzdzD]}t|j|dsy4gy)z2 Checks if G is a reduced Groebner basis. c(|jSr6r(rs r$r-zis_reduced..rr.r/FNrrT) r,rrrirrtermsrr))r"rrrnr#termr=r,s @r$ is_reducedrs JJE [[FFF$F%! !1 446:: GGI !DrUQq1uvY& !#ADD$q'2  ! ! ! r.c|j|jk7r td|j}|j}|r|s |jSt |dkrft |dkrXt |j |j }|j|j|j}|j||S|j\}}|j\}}|j||}|jDcgc] \}}d|z|f} }}|jDcgc] \}}d|z|fc}}|jDcgc] \}}d|z| fc}}z} td} |j| f|jzt} | j!| } | j!| }t#| |g| }d}|Dcgc]}||ds |}}|djDcgc]\}}|dd ||zf}}}|j!|}|Scc}}wcc}}wcc}}wcc}wcc}}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)rr)symbolsr,cJtfd|jD S)Nc3(K|] }| ywr6rI)rJmonomr<s r$rLz7groebner_lcm..is_independent..As8EuQx8s)rQmonoms)r=r<s `r$is_independentz$groebner_lcm..is_independent@s8QXXZ8888r.rN)rrrrr9rr)lcmrterm_new primitiverrrrr from_termsr )r+r#rrrcoefffcgcrf_termsg_termsrt_ringrqr"basisrr=rh_termss r$ groebner_lcmrs  vv122 66D [[F Ayy 1v{s1v{QTT144( 144&}}UE** KKMEB KKMEB **R C:;'')E,%u%EGE:;'')E,%u%E:;'')E,%uf%EFG c A ZZt|| 33Z ?F'"A'"A aVV $E94nQ2!4A4;rs!$XX%.(& PRh  (+  *  ,3"H *< 2F! H`K60f}>@@ ((7 rr.