groebnertools.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. """Groebner bases algorithms. """
  2. from sympy.core.symbol import Dummy
  3. from sympy.polys.monomials import monomial_mul, monomial_lcm, monomial_divides, term_div
  4. from sympy.polys.orderings import lex
  5. from sympy.polys.polyerrors import DomainError
  6. from sympy.polys.polyconfig import query
  7. def groebner(seq, ring, method=None):
  8. """
  9. Computes Groebner basis for a set of polynomials in `K[X]`.
  10. Wrapper around the (default) improved Buchberger and the other algorithms
  11. for computing Groebner bases. The choice of algorithm can be changed via
  12. ``method`` argument or :func:`sympy.polys.polyconfig.setup`, where
  13. ``method`` can be either ``buchberger`` or ``f5b``.
  14. """
  15. if method is None:
  16. method = query('groebner')
  17. _groebner_methods = {
  18. 'buchberger': _buchberger,
  19. 'f5b': _f5b,
  20. }
  21. try:
  22. _groebner = _groebner_methods[method]
  23. except KeyError:
  24. raise ValueError("'%s' is not a valid Groebner bases algorithm (valid are 'buchberger' and 'f5b')" % method)
  25. domain, orig = ring.domain, None
  26. if not domain.is_Field or not domain.has_assoc_Field:
  27. try:
  28. orig, ring = ring, ring.clone(domain=domain.get_field())
  29. except DomainError:
  30. raise DomainError("Cannot compute a Groebner basis over %s" % domain)
  31. else:
  32. seq = [ s.set_ring(ring) for s in seq ]
  33. G = _groebner(seq, ring)
  34. if orig is not None:
  35. G = [ g.clear_denoms()[1].set_ring(orig) for g in G ]
  36. return G
  37. def _buchberger(f, ring):
  38. """
  39. Computes Groebner basis for a set of polynomials in `K[X]`.
  40. Given a set of multivariate polynomials `F`, finds another
  41. set `G`, such that Ideal `F = Ideal G` and `G` is a reduced
  42. Groebner basis.
  43. The resulting basis is unique and has monic generators if the
  44. ground domains is a field. Otherwise the result is non-unique
  45. but Groebner bases over e.g. integers can be computed (if the
  46. input polynomials are monic).
  47. Groebner bases can be used to choose specific generators for a
  48. polynomial ideal. Because these bases are unique you can check
  49. for ideal equality by comparing the Groebner bases. To see if
  50. one polynomial lies in an ideal, divide by the elements in the
  51. base and see if the remainder vanishes.
  52. They can also be used to solve systems of polynomial equations
  53. as, by choosing lexicographic ordering, you can eliminate one
  54. variable at a time, provided that the ideal is zero-dimensional
  55. (finite number of solutions).
  56. Notes
  57. =====
  58. Algorithm used: an improved version of Buchberger's algorithm
  59. as presented in T. Becker, V. Weispfenning, Groebner Bases: A
  60. Computational Approach to Commutative Algebra, Springer, 1993,
  61. page 232.
  62. References
  63. ==========
  64. .. [1] [Bose03]_
  65. .. [2] [Giovini91]_
  66. .. [3] [Ajwa95]_
  67. .. [4] [Cox97]_
  68. """
  69. order = ring.order
  70. monomial_mul = ring.monomial_mul
  71. monomial_div = ring.monomial_div
  72. monomial_lcm = ring.monomial_lcm
  73. def select(P):
  74. # normal selection strategy
  75. # select the pair with minimum LCM(LM(f), LM(g))
  76. pr = min(P, key=lambda pair: order(monomial_lcm(f[pair[0]].LM, f[pair[1]].LM)))
  77. return pr
  78. def normal(g, J):
  79. h = g.rem([ f[j] for j in J ])
  80. if not h:
  81. return None
  82. else:
  83. h = h.monic()
  84. if h not in I:
  85. I[h] = len(f)
  86. f.append(h)
  87. return h.LM, I[h]
  88. def update(G, B, ih):
  89. # update G using the set of critical pairs B and h
  90. # [BW] page 230
  91. h = f[ih]
  92. mh = h.LM
  93. # filter new pairs (h, g), g in G
  94. C = G.copy()
  95. D = set()
  96. while C:
  97. # select a pair (h, g) by popping an element from C
  98. ig = C.pop()
  99. g = f[ig]
  100. mg = g.LM
  101. LCMhg = monomial_lcm(mh, mg)
  102. def lcm_divides(ip):
  103. # LCM(LM(h), LM(p)) divides LCM(LM(h), LM(g))
  104. m = monomial_lcm(mh, f[ip].LM)
  105. return monomial_div(LCMhg, m)
  106. # HT(h) and HT(g) disjoint: mh*mg == LCMhg
  107. if monomial_mul(mh, mg) == LCMhg or (
  108. not any(lcm_divides(ipx) for ipx in C) and
  109. not any(lcm_divides(pr[1]) for pr in D)):
  110. D.add((ih, ig))
  111. E = set()
  112. while D:
  113. # select h, g from D (h the same as above)
  114. ih, ig = D.pop()
  115. mg = f[ig].LM
  116. LCMhg = monomial_lcm(mh, mg)
  117. if not monomial_mul(mh, mg) == LCMhg:
  118. E.add((ih, ig))
  119. # filter old pairs
  120. B_new = set()
  121. while B:
  122. # select g1, g2 from B (-> CP)
  123. ig1, ig2 = B.pop()
  124. mg1 = f[ig1].LM
  125. mg2 = f[ig2].LM
  126. LCM12 = monomial_lcm(mg1, mg2)
  127. # if HT(h) does not divide lcm(HT(g1), HT(g2))
  128. if not monomial_div(LCM12, mh) or \
  129. monomial_lcm(mg1, mh) == LCM12 or \
  130. monomial_lcm(mg2, mh) == LCM12:
  131. B_new.add((ig1, ig2))
  132. B_new |= E
  133. # filter polynomials
  134. G_new = set()
  135. while G:
  136. ig = G.pop()
  137. mg = f[ig].LM
  138. if not monomial_div(mg, mh):
  139. G_new.add(ig)
  140. G_new.add(ih)
  141. return G_new, B_new
  142. # end of update ################################
  143. if not f:
  144. return []
  145. # replace f with a reduced list of initial polynomials; see [BW] page 203
  146. f1 = f[:]
  147. while True:
  148. f = f1[:]
  149. f1 = []
  150. for i in range(len(f)):
  151. p = f[i]
  152. r = p.rem(f[:i])
  153. if r:
  154. f1.append(r.monic())
  155. if f == f1:
  156. break
  157. I = {} # ip = I[p]; p = f[ip]
  158. F = set() # set of indices of polynomials
  159. G = set() # set of indices of intermediate would-be Groebner basis
  160. CP = set() # set of pairs of indices of critical pairs
  161. for i, h in enumerate(f):
  162. I[h] = i
  163. F.add(i)
  164. #####################################
  165. # algorithm GROEBNERNEWS2 in [BW] page 232
  166. while F:
  167. # select p with minimum monomial according to the monomial ordering
  168. h = min([f[x] for x in F], key=lambda f: order(f.LM))
  169. ih = I[h]
  170. F.remove(ih)
  171. G, CP = update(G, CP, ih)
  172. # count the number of critical pairs which reduce to zero
  173. reductions_to_zero = 0
  174. while CP:
  175. ig1, ig2 = select(CP)
  176. CP.remove((ig1, ig2))
  177. h = spoly(f[ig1], f[ig2], ring)
  178. # ordering divisors is on average more efficient [Cox] page 111
  179. G1 = sorted(G, key=lambda g: order(f[g].LM))
  180. ht = normal(h, G1)
  181. if ht:
  182. G, CP = update(G, CP, ht[1])
  183. else:
  184. reductions_to_zero += 1
  185. ######################################
  186. # now G is a Groebner basis; reduce it
  187. Gr = set()
  188. for ig in G:
  189. ht = normal(f[ig], G - {ig})
  190. if ht:
  191. Gr.add(ht[1])
  192. Gr = [f[ig] for ig in Gr]
  193. # order according to the monomial ordering
  194. Gr = sorted(Gr, key=lambda f: order(f.LM), reverse=True)
  195. return Gr
  196. def spoly(p1, p2, ring):
  197. """
  198. Compute LCM(LM(p1), LM(p2))/LM(p1)*p1 - LCM(LM(p1), LM(p2))/LM(p2)*p2
  199. This is the S-poly provided p1 and p2 are monic
  200. """
  201. LM1 = p1.LM
  202. LM2 = p2.LM
  203. LCM12 = ring.monomial_lcm(LM1, LM2)
  204. m1 = ring.monomial_div(LCM12, LM1)
  205. m2 = ring.monomial_div(LCM12, LM2)
  206. s1 = p1.mul_monom(m1)
  207. s2 = p2.mul_monom(m2)
  208. s = s1 - s2
  209. return s
  210. # F5B
  211. # convenience functions
  212. def Sign(f):
  213. return f[0]
  214. def Polyn(f):
  215. return f[1]
  216. def Num(f):
  217. return f[2]
  218. def sig(monomial, index):
  219. return (monomial, index)
  220. def lbp(signature, polynomial, number):
  221. return (signature, polynomial, number)
  222. # signature functions
  223. def sig_cmp(u, v, order):
  224. """
  225. Compare two signatures by extending the term order to K[X]^n.
  226. u < v iff
  227. - the index of v is greater than the index of u
  228. or
  229. - the index of v is equal to the index of u and u[0] < v[0] w.r.t. order
  230. u > v otherwise
  231. """
  232. if u[1] > v[1]:
  233. return -1
  234. if u[1] == v[1]:
  235. #if u[0] == v[0]:
  236. # return 0
  237. if order(u[0]) < order(v[0]):
  238. return -1
  239. return 1
  240. def sig_key(s, order):
  241. """
  242. Key for comparing two signatures.
  243. s = (m, k), t = (n, l)
  244. s < t iff [k > l] or [k == l and m < n]
  245. s > t otherwise
  246. """
  247. return (-s[1], order(s[0]))
  248. def sig_mult(s, m):
  249. """
  250. Multiply a signature by a monomial.
  251. The product of a signature (m, i) and a monomial n is defined as
  252. (m * t, i).
  253. """
  254. return sig(monomial_mul(s[0], m), s[1])
  255. # labeled polynomial functions
  256. def lbp_sub(f, g):
  257. """
  258. Subtract labeled polynomial g from f.
  259. The signature and number of the difference of f and g are signature
  260. and number of the maximum of f and g, w.r.t. lbp_cmp.
  261. """
  262. if sig_cmp(Sign(f), Sign(g), Polyn(f).ring.order) < 0:
  263. max_poly = g
  264. else:
  265. max_poly = f
  266. ret = Polyn(f) - Polyn(g)
  267. return lbp(Sign(max_poly), ret, Num(max_poly))
  268. def lbp_mul_term(f, cx):
  269. """
  270. Multiply a labeled polynomial with a term.
  271. The product of a labeled polynomial (s, p, k) by a monomial is
  272. defined as (m * s, m * p, k).
  273. """
  274. return lbp(sig_mult(Sign(f), cx[0]), Polyn(f).mul_term(cx), Num(f))
  275. def lbp_cmp(f, g):
  276. """
  277. Compare two labeled polynomials.
  278. f < g iff
  279. - Sign(f) < Sign(g)
  280. or
  281. - Sign(f) == Sign(g) and Num(f) > Num(g)
  282. f > g otherwise
  283. """
  284. if sig_cmp(Sign(f), Sign(g), Polyn(f).ring.order) == -1:
  285. return -1
  286. if Sign(f) == Sign(g):
  287. if Num(f) > Num(g):
  288. return -1
  289. #if Num(f) == Num(g):
  290. # return 0
  291. return 1
  292. def lbp_key(f):
  293. """
  294. Key for comparing two labeled polynomials.
  295. """
  296. return (sig_key(Sign(f), Polyn(f).ring.order), -Num(f))
  297. # algorithm and helper functions
  298. def critical_pair(f, g, ring):
  299. """
  300. Compute the critical pair corresponding to two labeled polynomials.
  301. A critical pair is a tuple (um, f, vm, g), where um and vm are
  302. terms such that um * f - vm * g is the S-polynomial of f and g (so,
  303. wlog assume um * f > vm * g).
  304. For performance sake, a critical pair is represented as a tuple
  305. (Sign(um * f), um, f, Sign(vm * g), vm, g), since um * f creates
  306. a new, relatively expensive object in memory, whereas Sign(um *
  307. f) and um are lightweight and f (in the tuple) is a reference to
  308. an already existing object in memory.
  309. """
  310. domain = ring.domain
  311. ltf = Polyn(f).LT
  312. ltg = Polyn(g).LT
  313. lt = (monomial_lcm(ltf[0], ltg[0]), domain.one)
  314. um = term_div(lt, ltf, domain)
  315. vm = term_div(lt, ltg, domain)
  316. # The full information is not needed (now), so only the product
  317. # with the leading term is considered:
  318. fr = lbp_mul_term(lbp(Sign(f), Polyn(f).leading_term(), Num(f)), um)
  319. gr = lbp_mul_term(lbp(Sign(g), Polyn(g).leading_term(), Num(g)), vm)
  320. # return in proper order, such that the S-polynomial is just
  321. # u_first * f_first - u_second * f_second:
  322. if lbp_cmp(fr, gr) == -1:
  323. return (Sign(gr), vm, g, Sign(fr), um, f)
  324. else:
  325. return (Sign(fr), um, f, Sign(gr), vm, g)
  326. def cp_cmp(c, d):
  327. """
  328. Compare two critical pairs c and d.
  329. c < d iff
  330. - lbp(c[0], _, Num(c[2]) < lbp(d[0], _, Num(d[2])) (this
  331. corresponds to um_c * f_c and um_d * f_d)
  332. or
  333. - lbp(c[0], _, Num(c[2]) >< lbp(d[0], _, Num(d[2])) and
  334. lbp(c[3], _, Num(c[5])) < lbp(d[3], _, Num(d[5])) (this
  335. corresponds to vm_c * g_c and vm_d * g_d)
  336. c > d otherwise
  337. """
  338. zero = Polyn(c[2]).ring.zero
  339. c0 = lbp(c[0], zero, Num(c[2]))
  340. d0 = lbp(d[0], zero, Num(d[2]))
  341. r = lbp_cmp(c0, d0)
  342. if r == -1:
  343. return -1
  344. if r == 0:
  345. c1 = lbp(c[3], zero, Num(c[5]))
  346. d1 = lbp(d[3], zero, Num(d[5]))
  347. r = lbp_cmp(c1, d1)
  348. if r == -1:
  349. return -1
  350. #if r == 0:
  351. # return 0
  352. return 1
  353. def cp_key(c, ring):
  354. """
  355. Key for comparing critical pairs.
  356. """
  357. return (lbp_key(lbp(c[0], ring.zero, Num(c[2]))), lbp_key(lbp(c[3], ring.zero, Num(c[5]))))
  358. def s_poly(cp):
  359. """
  360. Compute the S-polynomial of a critical pair.
  361. The S-polynomial of a critical pair cp is cp[1] * cp[2] - cp[4] * cp[5].
  362. """
  363. return lbp_sub(lbp_mul_term(cp[2], cp[1]), lbp_mul_term(cp[5], cp[4]))
  364. def is_rewritable_or_comparable(sign, num, B):
  365. """
  366. Check if a labeled polynomial is redundant by checking if its
  367. signature and number imply rewritability or comparability.
  368. (sign, num) is comparable if there exists a labeled polynomial
  369. h in B, such that sign[1] (the index) is less than Sign(h)[1]
  370. and sign[0] is divisible by the leading monomial of h.
  371. (sign, num) is rewritable if there exists a labeled polynomial
  372. h in B, such thatsign[1] is equal to Sign(h)[1], num < Num(h)
  373. and sign[0] is divisible by Sign(h)[0].
  374. """
  375. for h in B:
  376. # comparable
  377. if sign[1] < Sign(h)[1]:
  378. if monomial_divides(Polyn(h).LM, sign[0]):
  379. return True
  380. # rewritable
  381. if sign[1] == Sign(h)[1]:
  382. if num < Num(h):
  383. if monomial_divides(Sign(h)[0], sign[0]):
  384. return True
  385. return False
  386. def f5_reduce(f, B):
  387. """
  388. F5-reduce a labeled polynomial f by B.
  389. Continuously searches for non-zero labeled polynomial h in B, such
  390. that the leading term lt_h of h divides the leading term lt_f of
  391. f and Sign(lt_h * h) < Sign(f). If such a labeled polynomial h is
  392. found, f gets replaced by f - lt_f / lt_h * h. If no such h can be
  393. found or f is 0, f is no further F5-reducible and f gets returned.
  394. A polynomial that is reducible in the usual sense need not be
  395. F5-reducible, e.g.:
  396. >>> from sympy.polys.groebnertools import lbp, sig, f5_reduce, Polyn
  397. >>> from sympy.polys import ring, QQ, lex
  398. >>> R, x,y,z = ring("x,y,z", QQ, lex)
  399. >>> f = lbp(sig((1, 1, 1), 4), x, 3)
  400. >>> g = lbp(sig((0, 0, 0), 2), x, 2)
  401. >>> Polyn(f).rem([Polyn(g)])
  402. 0
  403. >>> f5_reduce(f, [g])
  404. (((1, 1, 1), 4), x, 3)
  405. """
  406. order = Polyn(f).ring.order
  407. domain = Polyn(f).ring.domain
  408. if not Polyn(f):
  409. return f
  410. while True:
  411. g = f
  412. for h in B:
  413. if Polyn(h):
  414. if monomial_divides(Polyn(h).LM, Polyn(f).LM):
  415. t = term_div(Polyn(f).LT, Polyn(h).LT, domain)
  416. if sig_cmp(sig_mult(Sign(h), t[0]), Sign(f), order) < 0:
  417. # The following check need not be done and is in general slower than without.
  418. #if not is_rewritable_or_comparable(Sign(gp), Num(gp), B):
  419. hp = lbp_mul_term(h, t)
  420. f = lbp_sub(f, hp)
  421. break
  422. if g == f or not Polyn(f):
  423. return f
  424. def _f5b(F, ring):
  425. """
  426. Computes a reduced Groebner basis for the ideal generated by F.
  427. f5b is an implementation of the F5B algorithm by Yao Sun and
  428. Dingkang Wang. Similarly to Buchberger's algorithm, the algorithm
  429. proceeds by computing critical pairs, computing the S-polynomial,
  430. reducing it and adjoining the reduced S-polynomial if it is not 0.
  431. Unlike Buchberger's algorithm, each polynomial contains additional
  432. information, namely a signature and a number. The signature
  433. specifies the path of computation (i.e. from which polynomial in
  434. the original basis was it derived and how), the number says when
  435. the polynomial was added to the basis. With this information it
  436. is (often) possible to decide if an S-polynomial will reduce to
  437. 0 and can be discarded.
  438. Optimizations include: Reducing the generators before computing
  439. a Groebner basis, removing redundant critical pairs when a new
  440. polynomial enters the basis and sorting the critical pairs and
  441. the current basis.
  442. Once a Groebner basis has been found, it gets reduced.
  443. References
  444. ==========
  445. .. [1] Yao Sun, Dingkang Wang: "A New Proof for the Correctness of F5
  446. (F5-Like) Algorithm", http://arxiv.org/abs/1004.0084 (specifically
  447. v4)
  448. .. [2] Thomas Becker, Volker Weispfenning, Groebner bases: A computational
  449. approach to commutative algebra, 1993, p. 203, 216
  450. """
  451. order = ring.order
  452. # reduce polynomials (like in Mario Pernici's implementation) (Becker, Weispfenning, p. 203)
  453. B = F
  454. while True:
  455. F = B
  456. B = []
  457. for i in range(len(F)):
  458. p = F[i]
  459. r = p.rem(F[:i])
  460. if r:
  461. B.append(r)
  462. if F == B:
  463. break
  464. # basis
  465. B = [lbp(sig(ring.zero_monom, i + 1), F[i], i + 1) for i in range(len(F))]
  466. B.sort(key=lambda f: order(Polyn(f).LM), reverse=True)
  467. # critical pairs
  468. CP = [critical_pair(B[i], B[j], ring) for i in range(len(B)) for j in range(i + 1, len(B))]
  469. CP.sort(key=lambda cp: cp_key(cp, ring), reverse=True)
  470. k = len(B)
  471. reductions_to_zero = 0
  472. while len(CP):
  473. cp = CP.pop()
  474. # discard redundant critical pairs:
  475. if is_rewritable_or_comparable(cp[0], Num(cp[2]), B):
  476. continue
  477. if is_rewritable_or_comparable(cp[3], Num(cp[5]), B):
  478. continue
  479. s = s_poly(cp)
  480. p = f5_reduce(s, B)
  481. p = lbp(Sign(p), Polyn(p).monic(), k + 1)
  482. if Polyn(p):
  483. # remove old critical pairs, that become redundant when adding p:
  484. indices = []
  485. for i, cp in enumerate(CP):
  486. if is_rewritable_or_comparable(cp[0], Num(cp[2]), [p]):
  487. indices.append(i)
  488. elif is_rewritable_or_comparable(cp[3], Num(cp[5]), [p]):
  489. indices.append(i)
  490. for i in reversed(indices):
  491. del CP[i]
  492. # only add new critical pairs that are not made redundant by p:
  493. for g in B:
  494. if Polyn(g):
  495. cp = critical_pair(p, g, ring)
  496. if is_rewritable_or_comparable(cp[0], Num(cp[2]), [p]):
  497. continue
  498. elif is_rewritable_or_comparable(cp[3], Num(cp[5]), [p]):
  499. continue
  500. CP.append(cp)
  501. # sort (other sorting methods/selection strategies were not as successful)
  502. CP.sort(key=lambda cp: cp_key(cp, ring), reverse=True)
  503. # insert p into B:
  504. m = Polyn(p).LM
  505. if order(m) <= order(Polyn(B[-1]).LM):
  506. B.append(p)
  507. else:
  508. for i, q in enumerate(B):
  509. if order(m) > order(Polyn(q).LM):
  510. B.insert(i, p)
  511. break
  512. k += 1
  513. #print(len(B), len(CP), "%d critical pairs removed" % len(indices))
  514. else:
  515. reductions_to_zero += 1
  516. # reduce Groebner basis:
  517. H = [Polyn(g).monic() for g in B]
  518. H = red_groebner(H, ring)
  519. return sorted(H, key=lambda f: order(f.LM), reverse=True)
  520. def red_groebner(G, ring):
  521. """
  522. Compute reduced Groebner basis, from BeckerWeispfenning93, p. 216
  523. Selects a subset of generators, that already generate the ideal
  524. and computes a reduced Groebner basis for them.
  525. """
  526. def reduction(P):
  527. """
  528. The actual reduction algorithm.
  529. """
  530. Q = []
  531. for i, p in enumerate(P):
  532. h = p.rem(P[:i] + P[i + 1:])
  533. if h:
  534. Q.append(h)
  535. return [p.monic() for p in Q]
  536. F = G
  537. H = []
  538. while F:
  539. f0 = F.pop()
  540. if not any(monomial_divides(f.LM, f0.LM) for f in F + H):
  541. H.append(f0)
  542. # Becker, Weispfenning, p. 217: H is Groebner basis of the ideal generated by G.
  543. return reduction(H)
  544. def is_groebner(G, ring):
  545. """
  546. Check if G is a Groebner basis.
  547. """
  548. for i in range(len(G)):
  549. for j in range(i + 1, len(G)):
  550. s = spoly(G[i], G[j], ring)
  551. s = s.rem(G)
  552. if s:
  553. return False
  554. return True
  555. def is_minimal(G, ring):
  556. """
  557. Checks if G is a minimal Groebner basis.
  558. """
  559. order = ring.order
  560. domain = ring.domain
  561. G.sort(key=lambda g: order(g.LM))
  562. for i, g in enumerate(G):
  563. if g.LC != domain.one:
  564. return False
  565. for h in G[:i] + G[i + 1:]:
  566. if monomial_divides(h.LM, g.LM):
  567. return False
  568. return True
  569. def is_reduced(G, ring):
  570. """
  571. Checks if G is a reduced Groebner basis.
  572. """
  573. order = ring.order
  574. domain = ring.domain
  575. G.sort(key=lambda g: order(g.LM))
  576. for i, g in enumerate(G):
  577. if g.LC != domain.one:
  578. return False
  579. for term in g.terms():
  580. for h in G[:i] + G[i + 1:]:
  581. if monomial_divides(h.LM, term[0]):
  582. return False
  583. return True
  584. def groebner_lcm(f, g):
  585. """
  586. Computes LCM of two polynomials using Groebner bases.
  587. The LCM is computed as the unique generator of the intersection
  588. of the two ideals generated by `f` and `g`. The approach is to
  589. compute a Groebner basis with respect to lexicographic ordering
  590. of `t*f` and `(1 - t)*g`, where `t` is an unrelated variable and
  591. then filtering out the solution that doesn't contain `t`.
  592. References
  593. ==========
  594. .. [1] [Cox97]_
  595. """
  596. if f.ring != g.ring:
  597. raise ValueError("Values should be equal")
  598. ring = f.ring
  599. domain = ring.domain
  600. if not f or not g:
  601. return ring.zero
  602. if len(f) <= 1 and len(g) <= 1:
  603. monom = monomial_lcm(f.LM, g.LM)
  604. coeff = domain.lcm(f.LC, g.LC)
  605. return ring.term_new(monom, coeff)
  606. fc, f = f.primitive()
  607. gc, g = g.primitive()
  608. lcm = domain.lcm(fc, gc)
  609. f_terms = [ ((1,) + monom, coeff) for monom, coeff in f.terms() ]
  610. g_terms = [ ((0,) + monom, coeff) for monom, coeff in g.terms() ] \
  611. + [ ((1,) + monom,-coeff) for monom, coeff in g.terms() ]
  612. t = Dummy("t")
  613. t_ring = ring.clone(symbols=(t,) + ring.symbols, order=lex)
  614. F = t_ring.from_terms(f_terms)
  615. G = t_ring.from_terms(g_terms)
  616. basis = groebner([F, G], t_ring)
  617. def is_independent(h, j):
  618. return not any(monom[j] for monom in h.monoms())
  619. H = [ h for h in basis if is_independent(h, 0) ]
  620. h_terms = [ (monom[1:], coeff*lcm) for monom, coeff in H[0].terms() ]
  621. h = ring.from_terms(h_terms)
  622. return h
  623. def groebner_gcd(f, g):
  624. """Computes GCD of two polynomials using Groebner bases. """
  625. if f.ring != g.ring:
  626. raise ValueError("Values should be equal")
  627. domain = f.ring.domain
  628. if not domain.is_Field:
  629. fc, f = f.primitive()
  630. gc, g = g.primitive()
  631. gcd = domain.gcd(fc, gc)
  632. H = (f*g).quo([groebner_lcm(f, g)])
  633. if len(H) != 1:
  634. raise ValueError("Length should be 1")
  635. h = H[0]
  636. if not domain.is_Field:
  637. return gcd*h
  638. else:
  639. return h.monic()