K iCUdZddlmZddlmZmZmZmZddlm Z ddl m Z ddl m Z ddlmZdZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZdZdZdZd dZ d dZ!dZ"d dZ#d dZ$dZ%d!dZ&y)"a Sparse distributed elements of free modules over multivariate (generalized) polynomial rings. This code and its data structures are very much like the distributed polynomials, except that the first "exponent" of the monomial is a module generator index. That is, the multi-exponent ``(i, e_1, ..., e_n)`` represents the "monomial" `x_1^{e_1} \cdots x_n^{e_n} f_i` of the free module `F` generated by `f_1, \ldots, f_r` over (a localization of) the ring `K[x_1, \ldots, x_n]`. A module element is simply stored as a list of terms ordered by the monomial order. Here a term is a pair of a multi-exponent and a coefficient. In general, this coefficient should never be zero (since it can then be omitted). The zero module element is stored as an empty list. The main routines are ``sdm_nf_mora`` and ``sdm_groebner`` which can be used to compute, respectively, weak normal forms and standard bases. They work with arbitrary (not necessarily global) monomial orders. In general, product orders have to be used to construct valid monomial orders for modules. However, ``lex`` can be used as-is. Note that the "level" (number of variables, i.e. parameter u+1 in distributedpolys.py) is never needed in this code. The main reference for this file is [SCA], "A Singular Introduction to Commutative Algebra". ) permutations) monomial_mul monomial_lcm monomial_div monomial_deg)Poly)parallel_dict_from_expr)S)sympifyc.|dft||ddzS)aN Multiply tuple ``X`` representing a monomial of `K[X]` into the tuple ``M`` representing a monomial of `F`. Examples ======== Multiplying `xy^3` into `x f_1` yields `x^2 y^3 f_1`: >>> from sympy.polys.distributedmodules import sdm_monomial_mul >>> sdm_monomial_mul((1, 1, 0), (1, 3)) (1, 2, 3) rN)r)MXs d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/distributedmodules.pysdm_monomial_mulr,s" aD7\!QqrU+ ++ct|ddS)z Return the total degree of ``M``. Examples ======== For example, the total degree of `x^2 y f_5` is 3: >>> from sympy.polys.distributedmodules import sdm_monomial_deg >>> sdm_monomial_deg((5, 2, 1)) 3 r N)r)rs rsdm_monomial_degr=s !" rc4|dft|dd|ddzS)a Return the "least common multiple" of ``A`` and ``B``. IF `A = M e_j` and `B = N e_j`, where `M` and `N` are polynomial monomials, this returns `\lcm(M, N) e_j`. Note that ``A`` and ``B`` involve distinct monomials. Otherwise the result is undefined. Examples ======== >>> from sympy.polys.distributedmodules import sdm_monomial_lcm >>> sdm_monomial_lcm((1, 2, 3), (1, 0, 5)) (1, 2, 5) rr N)rABs rsdm_monomial_lcmrMs(" aD7\!AB%12/ //rc `|d|dk(xr"tdt|dd|ddDS)a Does there exist a (polynomial) monomial X such that XA = B? Examples ======== Positive examples: In the following examples, the monomial is given in terms of x, y and the generator(s), f_1, f_2 etc. The tuple form of that monomial is used in the call to sdm_monomial_divides. Note: the generator appears last in the expression but first in the tuple and other factors appear in the same order that they appear in the monomial expression. `A = f_1` divides `B = f_1` >>> from sympy.polys.distributedmodules import sdm_monomial_divides >>> sdm_monomial_divides((1, 0, 0), (1, 0, 0)) True `A = f_1` divides `B = x^2 y f_1` >>> sdm_monomial_divides((1, 0, 0), (1, 2, 1)) True `A = xy f_5` divides `B = x^2 y f_5` >>> sdm_monomial_divides((5, 1, 1), (5, 2, 1)) True Negative examples: `A = f_1` does not divide `B = f_2` >>> sdm_monomial_divides((1, 0, 0), (2, 0, 0)) False `A = x f_1` does not divide `B = f_1` >>> sdm_monomial_divides((1, 1, 0), (1, 0, 0)) False `A = xy^2 f_5` does not divide `B = y f_5` >>> sdm_monomial_divides((5, 1, 2), (5, 0, 1)) False rc3,K|] \}}||kywN).0abs r z'sdm_monomial_divides..sE41aQEsr N)allziprs rsdm_monomial_dividesr$as:b Q41Q4< ECE3quae3DEEErc.|s |jS|ddS)z*Returns the leading coefficient of ``f``. rr )zero)fKs rsdm_LCr)s vv tAwrct|S)z1Make a dictionary from a distributed polynomial. )dictr's r sdm_to_dictr-s 7NrcZttt|j|S)ag Create an sdm from a dictionary. Here ``O`` is the monomial order to use. Examples ======== >>> from sympy.polys.distributedmodules import sdm_from_dict >>> from sympy.polys import QQ, lex >>> dic = {(1, 1, 0): QQ(1), (1, 0, 0): QQ(2), (0, 1, 0): QQ(0)} >>> sdm_from_dict(dic, lex) [((1, 1, 0), 1), ((1, 0, 0), 2)] ) sdm_stripsdm_sortlistitems)dOs r sdm_from_dictr5s  Xd1779oq1 22rc&t|fddS)z:Sort terms in ``f`` using the given monomial order ``O``. c|dSNrr)termr4s rzsdm_sort..saQjrTkeyreverse)sorted)r'r4s `rr0r0s !0$ ??rcB|Dcgc] \}}|s ||fc}}Scc}}w)z>> from sympy.polys.distributedmodules import sdm_add >>> from sympy.polys import lex, QQ >>> sdm_add([((1, 1, 1), QQ(1))], [((2, 0, 0), QQ(1))], lex, QQ) [((2, 0, 0), 1), ((1, 1, 1), 1)] `(xy f_1) + (-xy f_1)` = 0` >>> sdm_add([((1, 1, 1), QQ(1))], [((1, 1, 1), QQ(-1))], lex, QQ) [] `(f_1) + (2f_1) = 3f_1` >>> sdm_add([((1, 0, 0), QQ(1))], [((1, 0, 0), QQ(2))], lex, QQ) [((1, 0, 0), 3)] `(yf_1) + (xf_1) = xf_1 + yf_1` >>> sdm_add([((1, 0, 1), QQ(1))], [((1, 1, 0), QQ(1))], lex, QQ) [((1, 1, 0), 1), ((1, 0, 1), 1)] )r+r5)r'gr4r(hr@crAs rsdm_addrFs`D QA q A:eHqLEeH %AeH  A rc|ddS)aV Returns the leading monomial of ``f``. Only valid if `f \ne 0`. Examples ======== >>> from sympy.polys.distributedmodules import sdm_LM, sdm_from_dict >>> from sympy.polys import QQ, lex >>> dic = {(1, 2, 3): QQ(1), (4, 0, 0): QQ(1), (4, 0, 1): QQ(1)} >>> sdm_LM(sdm_from_dict(dic, lex)) (4, 0, 1) rrr,s rsdm_LMrHs Q47Nrc |dS)aW Returns the leading term of ``f``. Only valid if `f \ne 0`. Examples ======== >>> from sympy.polys.distributedmodules import sdm_LT, sdm_from_dict >>> from sympy.polys import QQ, lex >>> dic = {(1, 2, 3): QQ(1), (4, 0, 0): QQ(2), (4, 0, 1): QQ(3)} >>> sdm_LT(sdm_from_dict(dic, lex)) ((4, 0, 1), 3) rrr,s rsdm_LTrJs  Q4Krc|\}}|r|sgS|j|r!|Dcgc]\}}t|||fc}}S|Dcgc]\}}t||||zfc}}Scc}}wcc}}w)a3 Multiply a distributed module element ``f`` by a (polynomial) term ``term``. Multiplication of coefficients is done over the ground field ``K``, and monomials are ordered according to ``O``. Examples ======== `0 f_1 = 0` >>> from sympy.polys.distributedmodules import sdm_mul_term >>> from sympy.polys import lex, QQ >>> sdm_mul_term([((1, 0, 0), QQ(1))], ((0, 0), QQ(0)), lex, QQ) [] `x 0 = 0` >>> sdm_mul_term([], ((1, 0), QQ(1)), lex, QQ) [] `(x) (f_1) = xf_1` >>> sdm_mul_term([((1, 0, 0), QQ(1))], ((1, 0), QQ(1)), lex, QQ) [((1, 1, 0), 1)] `(2xy) (3x f_1 + 4y f_2) = 8xy^2 f_2 + 6x^2y f_1` >>> f = [((2, 0, 1), QQ(4)), ((1, 1, 0), QQ(3))] >>> sdm_mul_term(f, ((1, 1), QQ(2)), lex, QQ) [((2, 1, 2), 8), ((1, 2, 1), 6)] )is_oner)r'r9r4r(rrEf_Mf_cs r sdm_mul_termrOstB DAq A 88A;EFHc&sA.4H HIJLXS#&sA.a8L LILs A"A(cgS)zReturn the zero module element.rrrrsdm_zerorQBs Irc&td|DS)a Degree of ``f``. This is the maximum of the degrees of all its monomials. Invalid if ``f`` is zero. Examples ======== >>> from sympy.polys.distributedmodules import sdm_deg >>> sdm_deg([((1, 2, 3), 1), ((10, 0, 1), 1), ((2, 3, 4), 4)]) 7 c38K|]}t|dywrN)r)rrs rr!zsdm_deg..Us1!!%1s)maxr,s rsdm_degrVGs 1q1 11rc tt|fi|\}}i}t|D]5\}}|jD]\} } |j | ||f| z<7t ||S)a> Create an sdm from an iterable of expressions. Coefficients are created in the ground field ``K``, and terms are ordered according to monomial order ``O``. Named arguments are passed on to the polys conversion code and can be used to specify for example generators. Examples ======== >>> from sympy.polys.distributedmodules import sdm_from_vector >>> from sympy.abc import x, y, z >>> from sympy.polys import QQ, lex >>> sdm_from_vector([x**2+y**2, 2*z], lex, QQ) [((1, 0, 0, 1), 2), ((0, 2, 0, 0), 1), ((0, 0, 2, 0), 1)] )r r enumerater2convertr5) vecr4r(optsdicsgensdicir3kvs rsdm_from_vectorrbZsv")>>JD$ C$)1GGI )DAqIIaLCqM )) a  rNc t|}i}|jD].\}}|j|dgj|dd|f0|xs t |}g}t |D]]}||vr8|jt t||||j?|jtj_|S)a* Convert sdm ``f`` into a list of polynomial expressions. The generators for the polynomial ring are specified via ``gens``. The rank of the module is guessed, or passed via ``n``. The ground field is assumed to be ``K``. Examples ======== >>> from sympy.polys.distributedmodules import sdm_to_vector >>> from sympy.abc import x, y, z >>> from sympy.polys import QQ >>> f = [((1, 0, 0, 1), QQ(2)), ((0, 2, 0, 0), QQ(1)), ((0, 0, 2, 0), QQ(1))] >>> sdm_to_vector(f, [x, y, z], QQ) [x**2 + y**2, 2*z] rr N)r]domain) r-r2 setdefaultappendlenrangerr+as_exprr Zero) r'r]r(nr^r\r`raress r sdm_to_vectorrmss$ a.C D 51 !b!((!AB%45 SYA C 1X 9 JJtDaMQ?GGI J JJqvv   Jrc  |r|s tSt|}t|}|d|dk7r tS|dd}|dd}t||}t||}t||} |j t || t ||} t t|||jf||t|| | f||||} || St t|d||jf||t|d| | f||||} | | fS)a Compute the generalized s-polynomial of ``f`` and ``g``. The ground field is assumed to be ``K``, and monomials ordered according to ``O``. This is invalid if either of ``f`` or ``g`` is zero. If the leading terms of `f` and `g` involve different basis elements of `F`, their s-poly is defined to be zero. Otherwise it is a certain linear combination of `f` and `g` in which the leading terms cancel. See [SCA, defn 2.3.6] for details. If ``phantom`` is not ``None``, it should be a pair of module elements on which to perform the same operation(s) as on ``f`` and ``g``. The in this case both results are returned. Examples ======== >>> from sympy.polys.distributedmodules import sdm_spoly >>> from sympy.polys import QQ, lex >>> f = [((2, 1, 1), QQ(1)), ((1, 0, 1), QQ(1))] >>> g = [((2, 3, 0), QQ(1))] >>> h = [((1, 2, 3), QQ(1))] >>> sdm_spoly(f, h, lex, QQ) [] >>> sdm_spoly(f, g, lex, QQ) [((1, 2, 1), 1)] rr N) rQrHrrquor)rFrOone) r'rCr4r(phantomLM1LM2lcmm1m2rEr1r2s r sdm_spolyrys> Az )C )C 1vQz ab'C ab'C sC C c3 B c3 B va|mVAq\*A a"aeea3a"a!Q/A 7B gaj2quu+q!<gaj2q'1a8!Q @B r6MrcBt|tt|z S)a Compute the ecart of ``f``. This is defined to be the difference of the total degree of `f` and the total degree of the leading monomial of `f` [SCA, defn 2.3.7]. Invalid if f is zero. Examples ======== >>> from sympy.polys.distributedmodules import sdm_ecart >>> sdm_ecart([((1, 2, 3), 1), ((1, 0, 1), 1)]) 0 >>> sdm_ecart([((2, 2, 1), 1), ((1, 5, 1), 1)]) 3 )rVrrHr,s r sdm_ecartr{s$ 1:(3 33rc ddlm}|}t|}||d}t|d} d}n |g} d}|rt|| D cgc]1\} } t t | t |r| t | | f3} } } | snvt| d\} } } t | t |kDr$|j||r| j|rt|| ||| f\}}nt|| ||}|r|r|fS|Scc} } w) a6 Compute a weak normal form of ``f`` with respect to ``G`` and order ``O``. The ground field is assumed to be ``K``, and monomials ordered according to ``O``. Weak normal forms are defined in [SCA, defn 2.3.3]. They are not unique. This function deterministically computes a weak normal form, depending on the order of `G`. The most important property of a weak normal form is the following: if `R` is the ring associated with the monomial ordering (if the ordering is global, we just have `R = K[x_1, \ldots, x_n]`, otherwise it is a certain localization thereof), `I` any ideal of `R` and `G` a standard basis for `I`, then for any `f \in R`, we have `f \in I` if and only if `NF(f | G) = 0`. This is the generalized Mora algorithm for computing weak normal forms with respect to arbitrary monomial orders [SCA, algorithm 2.3.9]. If ``phantom`` is not ``None``, it should be a pair of "phantom" arguments on which to perform the same computations as on ``f``, ``G``, both results are then returned. rrepeatr TFc |dS)Nr r)xs rr:zsdm_nf_mora..s 1rr<rq) itertoolsr~r1r#r$rHr{minrfry)r'Gr4r(rqr~rDThpTprCgpTh_s r sdm_nf_morars2! A QA QZ '!*  BZ 14Q=2%fQi;)A,#== r~.1b Q<)A, & HHQK " aAq2r(;EAr!Q1%A "u H=s6Dc6 ddlm}| t|}||d}t|d}d}n |g}d} rK t fdt ||D\} } |rt | ||| f\ }nt | || rK|r fS S#t $rYwxYw)a Compute a weak normal form of ``f`` with respect to ``G`` and order ``O``. The ground field is assumed to be ``K``, and monomials ordered according to ``O``. This is the standard Buchberger algorithm for computing weak normal forms with respect to *global* monomial orders [SCA, algorithm 1.6.10]. If ``phantom`` is not ``None``, it should be a pair of "phantom" arguments on which to perform the same computations as on ``f``, ``G``, both results are then returned. rr}r TFc3hK|])\}}tt|tr||f+ywr)r$rH)rrCrrDs rr!z$sdm_nf_buchberger..1s4HUQ0F1IFRHs/2r)rr~r1nextr# StopIterationry) r'rr4r(rqr~rrrrCrrDs @rsdm_nf_buchbergerrs! A QA QZ '!*  BZ  Hc!RjHHEAr aAq2r(;EAr!Q1%A "u H   s!B BBct}|}|r0t||||}|rt|t|g||}|dd}|r0|S)aN Compute a reduced normal form of ``f`` with respect to ``G`` and order ``O``. The ground field is assumed to be ``K``, and monomials ordered according to ``O``. In contrast to weak normal forms, reduced normal forms *are* unique, but their computation is more expensive. This is the standard Buchberger algorithm for computing reduced normal forms with respect to *global* monomial orders [SCA, algorithm 1.6.11]. The ``pantom`` option is not supported, so this normal form cannot be used as a normal form for the "extended" groebner algorithm. r N)rQrrFrJ)r'rr4r(rDrCs rsdm_nf_buchberger_reducedr>sS  A A aAq ) F1I;1-A!"A Hrc D g}ggfdfd  fd} ttd|Dddz }g}t|D]H\} } || t | |}|s| s|j t | fd|zz|diJ|r|j\} } } } | | }} |rAt| |||| || f\}}|||||f\}}|r)|j |n|t| |||}||| | |}|rtD chc]\} } t| | fc} } td D]P\\}}\}}t|}t|}t||s/||fvs6||fvs=j||fRtd Dfd d }|Dcgc]}|d }}|r||D cgc] \}} ||  c} }fS|S#t$r |rggfcYSgcYSwxYwcc} } wcc}wcc} }w)a Compute a minimal standard basis of ``G`` with respect to order ``O``. The algorithm uses a normal form ``NF``, for example ``sdm_nf_mora``. The ground field is assumed to be ``K``, and monomials ordered according to ``O``. Let `N` denote the submodule generated by elements of `G`. A standard basis for `N` is a subset `S` of `N`, such that `in(S) = in(N)`, where for any subset `X` of `F`, `in(X)` denotes the submodule generated by the initial forms of elements of `X`. [SCA, defn 2.3.2] A standard basis is called minimal if no subset of it is a standard basis. One may show that standard bases are always generating sets. Minimal standard bases are not unique. This algorithm computes a deterministic result, depending on the particular order of `G`. If ``extended=True``, also compute the transition matrix from the initial generators to the groebner basis. That is, return a list of coefficient vectors, expressing the elements of the groebner basis in terms of the elements of ``G``. This functions implements the "sugar" strategy, see Giovini et al: "One sugar cube, please" OR Selection strategies in Buchberger algorithm. ct|}t|}t|t|z |t|z tt||zS)z8Compute the sugar of the S-poly corresponding to (i, j).)rHrUrr)r_jLMiLMjr Sugarss rSsugarzsdm_groebner..SsugarsbQqTlQqTl6!9/44!9/446/S9:; ;rc,|d|d|dfS)Nr rpr4s rr:zsdm_groebner..s!a!gqt,rc  |s|St } j| j|t| fd}|Dcgc] }||r |}}t|Dcgc]<} dt |dk(r#|| ||t t |f>}}|j t }t|D]I\}}t|dzt|D])} t|d|| ds|j| +K|jtt|Dcgc] \}}||vs |c}}|j d|Scc}wcc}wcc}}w)z*Add f with sugar ``sugar`` to S, update P.c|\}}}}d|dk7rytt|}tt|}||k7xr!||k7xrt||xr t||S)NrF)rrHr$) pairr_rsttiktjkLMfr s r removethisz0sdm_groebner..update..removethissJAq!Q1v1~"3qt 5C"3qt 5C!8-q--A#q-I-$S!, -rrrr rTr;) rgrfrHrhrsortsetrXr$addextendreversed)r'sugarPr`rrr_Nremoverrr rrourkeys @rupdatezsdm_groebner..updatesmH F  eQi - /1AQ / /Ah =#a&F1Q4LO";F1aL"23qt "E F = = 6aL "DAq1q5#a&) "'!ad1g6JJqM " " 1IA&1IJK 64(# 0 =Js E1E1+AE6? E; E;c3,K|] }|s|dywrTr)rrs rr!zsdm_groebner..s0Aa1Q40s rr )rrrc3<K|]\}}t||fywr)r1)rr'r_s rr!zsdm_groebner..s+Aa! +sc,t|dSr8)rHrs rr:zsdm_groebner..s1VAaD\?rTr;)rgrrrXrVrfr5poprytuplerrHr$rr>)!rNFr4r(extendedrrnumgens coefficientsr_r'rrrrCsprArDhcoeffrair birrLrrlrr rrrs! ` @@@@r sdm_groebnerrXsrF A A F;-F$N d00034q8L! O1 1gaj! $     td7l/BAaD.I1 M NO UUW 1atQqT1 !!Q1+7?LO*LNIB2q!Q 0EFIAv##F+9Q1a(!Q2A 1fQlA & $-Q<041a%(A0A(A.B!R 1I 1I 1 %1b'Q,Ar7a< AHHaW   ++1J A A1Q4 C 3A\!_333 JY  r6M @ 1 3s)!G86H H"H8H H Hr)F)'__doc__rrsympy.polys.monomialsrrrrsympy.polys.polytoolsrsympy.polys.polyutilsr sympy.core.singletonr sympy.core.sympifyr rrrr$r)r-r5r0r/rFrHrJrOrQrVrbrmryr{rrrrrrrrs:#'9"& ,"  0(1Fl 3$@ = /d$$)MX 2&!2D1h4*5 p% P 4Kr