euclidtools.py 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  1. """Euclidean algorithms, GCDs, LCMs and polynomial remainder sequences. """
  2. from sympy.ntheory import nextprime
  3. from sympy.polys.densearith import (
  4. dup_sub_mul,
  5. dup_neg, dmp_neg,
  6. dmp_add,
  7. dmp_sub,
  8. dup_mul, dmp_mul,
  9. dmp_pow,
  10. dup_div, dmp_div,
  11. dup_rem,
  12. dup_quo, dmp_quo,
  13. dup_prem, dmp_prem,
  14. dup_mul_ground, dmp_mul_ground,
  15. dmp_mul_term,
  16. dup_quo_ground, dmp_quo_ground,
  17. dup_max_norm, dmp_max_norm)
  18. from sympy.polys.densebasic import (
  19. dup_strip, dmp_raise,
  20. dmp_zero, dmp_one, dmp_ground,
  21. dmp_one_p, dmp_zero_p,
  22. dmp_zeros,
  23. dup_degree, dmp_degree, dmp_degree_in,
  24. dup_LC, dmp_LC, dmp_ground_LC,
  25. dmp_multi_deflate, dmp_inflate,
  26. dup_convert, dmp_convert,
  27. dmp_apply_pairs)
  28. from sympy.polys.densetools import (
  29. dup_clear_denoms, dmp_clear_denoms,
  30. dup_diff, dmp_diff,
  31. dup_eval, dmp_eval, dmp_eval_in,
  32. dup_trunc, dmp_ground_trunc,
  33. dup_monic, dmp_ground_monic,
  34. dup_primitive, dmp_ground_primitive,
  35. dup_extract, dmp_ground_extract)
  36. from sympy.polys.galoistools import (
  37. gf_int, gf_crt)
  38. from sympy.polys.polyconfig import query
  39. from sympy.polys.polyerrors import (
  40. MultivariatePolynomialError,
  41. HeuristicGCDFailed,
  42. HomomorphismFailed,
  43. NotInvertible,
  44. DomainError)
  45. def dup_half_gcdex(f, g, K):
  46. """
  47. Half extended Euclidean algorithm in `F[x]`.
  48. Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``.
  49. Examples
  50. ========
  51. >>> from sympy.polys import ring, QQ
  52. >>> R, x = ring("x", QQ)
  53. >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
  54. >>> g = x**3 + x**2 - 4*x - 4
  55. >>> R.dup_half_gcdex(f, g)
  56. (-1/5*x + 3/5, x + 1)
  57. """
  58. if not K.is_Field:
  59. raise DomainError("Cannot compute half extended GCD over %s" % K)
  60. a, b = [K.one], []
  61. while g:
  62. q, r = dup_div(f, g, K)
  63. f, g = g, r
  64. a, b = b, dup_sub_mul(a, q, b, K)
  65. a = dup_quo_ground(a, dup_LC(f, K), K)
  66. f = dup_monic(f, K)
  67. return a, f
  68. def dmp_half_gcdex(f, g, u, K):
  69. """
  70. Half extended Euclidean algorithm in `F[X]`.
  71. Examples
  72. ========
  73. >>> from sympy.polys import ring, ZZ
  74. >>> R, x,y = ring("x,y", ZZ)
  75. """
  76. if not u:
  77. return dup_half_gcdex(f, g, K)
  78. else:
  79. raise MultivariatePolynomialError(f, g)
  80. def dup_gcdex(f, g, K):
  81. """
  82. Extended Euclidean algorithm in `F[x]`.
  83. Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``.
  84. Examples
  85. ========
  86. >>> from sympy.polys import ring, QQ
  87. >>> R, x = ring("x", QQ)
  88. >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
  89. >>> g = x**3 + x**2 - 4*x - 4
  90. >>> R.dup_gcdex(f, g)
  91. (-1/5*x + 3/5, 1/5*x**2 - 6/5*x + 2, x + 1)
  92. """
  93. s, h = dup_half_gcdex(f, g, K)
  94. F = dup_sub_mul(h, s, f, K)
  95. t = dup_quo(F, g, K)
  96. return s, t, h
  97. def dmp_gcdex(f, g, u, K):
  98. """
  99. Extended Euclidean algorithm in `F[X]`.
  100. Examples
  101. ========
  102. >>> from sympy.polys import ring, ZZ
  103. >>> R, x,y = ring("x,y", ZZ)
  104. """
  105. if not u:
  106. return dup_gcdex(f, g, K)
  107. else:
  108. raise MultivariatePolynomialError(f, g)
  109. def dup_invert(f, g, K):
  110. """
  111. Compute multiplicative inverse of `f` modulo `g` in `F[x]`.
  112. Examples
  113. ========
  114. >>> from sympy.polys import ring, QQ
  115. >>> R, x = ring("x", QQ)
  116. >>> f = x**2 - 1
  117. >>> g = 2*x - 1
  118. >>> h = x - 1
  119. >>> R.dup_invert(f, g)
  120. -4/3
  121. >>> R.dup_invert(f, h)
  122. Traceback (most recent call last):
  123. ...
  124. NotInvertible: zero divisor
  125. """
  126. s, h = dup_half_gcdex(f, g, K)
  127. if h == [K.one]:
  128. return dup_rem(s, g, K)
  129. else:
  130. raise NotInvertible("zero divisor")
  131. def dmp_invert(f, g, u, K):
  132. """
  133. Compute multiplicative inverse of `f` modulo `g` in `F[X]`.
  134. Examples
  135. ========
  136. >>> from sympy.polys import ring, QQ
  137. >>> R, x = ring("x", QQ)
  138. """
  139. if not u:
  140. return dup_invert(f, g, K)
  141. else:
  142. raise MultivariatePolynomialError(f, g)
  143. def dup_euclidean_prs(f, g, K):
  144. """
  145. Euclidean polynomial remainder sequence (PRS) in `K[x]`.
  146. Examples
  147. ========
  148. >>> from sympy.polys import ring, QQ
  149. >>> R, x = ring("x", QQ)
  150. >>> f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
  151. >>> g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
  152. >>> prs = R.dup_euclidean_prs(f, g)
  153. >>> prs[0]
  154. x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
  155. >>> prs[1]
  156. 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
  157. >>> prs[2]
  158. -5/9*x**4 + 1/9*x**2 - 1/3
  159. >>> prs[3]
  160. -117/25*x**2 - 9*x + 441/25
  161. >>> prs[4]
  162. 233150/19773*x - 102500/6591
  163. >>> prs[5]
  164. -1288744821/543589225
  165. """
  166. prs = [f, g]
  167. h = dup_rem(f, g, K)
  168. while h:
  169. prs.append(h)
  170. f, g = g, h
  171. h = dup_rem(f, g, K)
  172. return prs
  173. def dmp_euclidean_prs(f, g, u, K):
  174. """
  175. Euclidean polynomial remainder sequence (PRS) in `K[X]`.
  176. Examples
  177. ========
  178. >>> from sympy.polys import ring, ZZ
  179. >>> R, x,y = ring("x,y", ZZ)
  180. """
  181. if not u:
  182. return dup_euclidean_prs(f, g, K)
  183. else:
  184. raise MultivariatePolynomialError(f, g)
  185. def dup_primitive_prs(f, g, K):
  186. """
  187. Primitive polynomial remainder sequence (PRS) in `K[x]`.
  188. Examples
  189. ========
  190. >>> from sympy.polys import ring, ZZ
  191. >>> R, x = ring("x", ZZ)
  192. >>> f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
  193. >>> g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
  194. >>> prs = R.dup_primitive_prs(f, g)
  195. >>> prs[0]
  196. x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
  197. >>> prs[1]
  198. 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
  199. >>> prs[2]
  200. -5*x**4 + x**2 - 3
  201. >>> prs[3]
  202. 13*x**2 + 25*x - 49
  203. >>> prs[4]
  204. 4663*x - 6150
  205. >>> prs[5]
  206. 1
  207. """
  208. prs = [f, g]
  209. _, h = dup_primitive(dup_prem(f, g, K), K)
  210. while h:
  211. prs.append(h)
  212. f, g = g, h
  213. _, h = dup_primitive(dup_prem(f, g, K), K)
  214. return prs
  215. def dmp_primitive_prs(f, g, u, K):
  216. """
  217. Primitive polynomial remainder sequence (PRS) in `K[X]`.
  218. Examples
  219. ========
  220. >>> from sympy.polys import ring, ZZ
  221. >>> R, x,y = ring("x,y", ZZ)
  222. """
  223. if not u:
  224. return dup_primitive_prs(f, g, K)
  225. else:
  226. raise MultivariatePolynomialError(f, g)
  227. def dup_inner_subresultants(f, g, K):
  228. """
  229. Subresultant PRS algorithm in `K[x]`.
  230. Computes the subresultant polynomial remainder sequence (PRS)
  231. and the non-zero scalar subresultants of `f` and `g`.
  232. By [1] Thm. 3, these are the constants '-c' (- to optimize
  233. computation of sign).
  234. The first subdeterminant is set to 1 by convention to match
  235. the polynomial and the scalar subdeterminants.
  236. If 'deg(f) < deg(g)', the subresultants of '(g,f)' are computed.
  237. Examples
  238. ========
  239. >>> from sympy.polys import ring, ZZ
  240. >>> R, x = ring("x", ZZ)
  241. >>> R.dup_inner_subresultants(x**2 + 1, x**2 - 1)
  242. ([x**2 + 1, x**2 - 1, -2], [1, 1, 4])
  243. References
  244. ==========
  245. .. [1] W.S. Brown, The Subresultant PRS Algorithm.
  246. ACM Transaction of Mathematical Software 4 (1978) 237-249
  247. """
  248. n = dup_degree(f)
  249. m = dup_degree(g)
  250. if n < m:
  251. f, g = g, f
  252. n, m = m, n
  253. if not f:
  254. return [], []
  255. if not g:
  256. return [f], [K.one]
  257. R = [f, g]
  258. d = n - m
  259. b = (-K.one)**(d + 1)
  260. h = dup_prem(f, g, K)
  261. h = dup_mul_ground(h, b, K)
  262. lc = dup_LC(g, K)
  263. c = lc**d
  264. # Conventional first scalar subdeterminant is 1
  265. S = [K.one, c]
  266. c = -c
  267. while h:
  268. k = dup_degree(h)
  269. R.append(h)
  270. f, g, m, d = g, h, k, m - k
  271. b = -lc * c**d
  272. h = dup_prem(f, g, K)
  273. h = dup_quo_ground(h, b, K)
  274. lc = dup_LC(g, K)
  275. if d > 1: # abnormal case
  276. q = c**(d - 1)
  277. c = K.quo((-lc)**d, q)
  278. else:
  279. c = -lc
  280. S.append(-c)
  281. return R, S
  282. def dup_subresultants(f, g, K):
  283. """
  284. Computes subresultant PRS of two polynomials in `K[x]`.
  285. Examples
  286. ========
  287. >>> from sympy.polys import ring, ZZ
  288. >>> R, x = ring("x", ZZ)
  289. >>> R.dup_subresultants(x**2 + 1, x**2 - 1)
  290. [x**2 + 1, x**2 - 1, -2]
  291. """
  292. return dup_inner_subresultants(f, g, K)[0]
  293. def dup_prs_resultant(f, g, K):
  294. """
  295. Resultant algorithm in `K[x]` using subresultant PRS.
  296. Examples
  297. ========
  298. >>> from sympy.polys import ring, ZZ
  299. >>> R, x = ring("x", ZZ)
  300. >>> R.dup_prs_resultant(x**2 + 1, x**2 - 1)
  301. (4, [x**2 + 1, x**2 - 1, -2])
  302. """
  303. if not f or not g:
  304. return (K.zero, [])
  305. R, S = dup_inner_subresultants(f, g, K)
  306. if dup_degree(R[-1]) > 0:
  307. return (K.zero, R)
  308. return S[-1], R
  309. def dup_resultant(f, g, K, includePRS=False):
  310. """
  311. Computes resultant of two polynomials in `K[x]`.
  312. Examples
  313. ========
  314. >>> from sympy.polys import ring, ZZ
  315. >>> R, x = ring("x", ZZ)
  316. >>> R.dup_resultant(x**2 + 1, x**2 - 1)
  317. 4
  318. """
  319. if includePRS:
  320. return dup_prs_resultant(f, g, K)
  321. return dup_prs_resultant(f, g, K)[0]
  322. def dmp_inner_subresultants(f, g, u, K):
  323. """
  324. Subresultant PRS algorithm in `K[X]`.
  325. Examples
  326. ========
  327. >>> from sympy.polys import ring, ZZ
  328. >>> R, x,y = ring("x,y", ZZ)
  329. >>> f = 3*x**2*y - y**3 - 4
  330. >>> g = x**2 + x*y**3 - 9
  331. >>> a = 3*x*y**4 + y**3 - 27*y + 4
  332. >>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16
  333. >>> prs = [f, g, a, b]
  334. >>> sres = [[1], [1], [3, 0, 0, 0, 0], [-3, 0, 0, -12, 1, 0, -54, 8, 729, -216, 16]]
  335. >>> R.dmp_inner_subresultants(f, g) == (prs, sres)
  336. True
  337. """
  338. if not u:
  339. return dup_inner_subresultants(f, g, K)
  340. n = dmp_degree(f, u)
  341. m = dmp_degree(g, u)
  342. if n < m:
  343. f, g = g, f
  344. n, m = m, n
  345. if dmp_zero_p(f, u):
  346. return [], []
  347. v = u - 1
  348. if dmp_zero_p(g, u):
  349. return [f], [dmp_ground(K.one, v)]
  350. R = [f, g]
  351. d = n - m
  352. b = dmp_pow(dmp_ground(-K.one, v), d + 1, v, K)
  353. h = dmp_prem(f, g, u, K)
  354. h = dmp_mul_term(h, b, 0, u, K)
  355. lc = dmp_LC(g, K)
  356. c = dmp_pow(lc, d, v, K)
  357. S = [dmp_ground(K.one, v), c]
  358. c = dmp_neg(c, v, K)
  359. while not dmp_zero_p(h, u):
  360. k = dmp_degree(h, u)
  361. R.append(h)
  362. f, g, m, d = g, h, k, m - k
  363. b = dmp_mul(dmp_neg(lc, v, K),
  364. dmp_pow(c, d, v, K), v, K)
  365. h = dmp_prem(f, g, u, K)
  366. h = [ dmp_quo(ch, b, v, K) for ch in h ]
  367. lc = dmp_LC(g, K)
  368. if d > 1:
  369. p = dmp_pow(dmp_neg(lc, v, K), d, v, K)
  370. q = dmp_pow(c, d - 1, v, K)
  371. c = dmp_quo(p, q, v, K)
  372. else:
  373. c = dmp_neg(lc, v, K)
  374. S.append(dmp_neg(c, v, K))
  375. return R, S
  376. def dmp_subresultants(f, g, u, K):
  377. """
  378. Computes subresultant PRS of two polynomials in `K[X]`.
  379. Examples
  380. ========
  381. >>> from sympy.polys import ring, ZZ
  382. >>> R, x,y = ring("x,y", ZZ)
  383. >>> f = 3*x**2*y - y**3 - 4
  384. >>> g = x**2 + x*y**3 - 9
  385. >>> a = 3*x*y**4 + y**3 - 27*y + 4
  386. >>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16
  387. >>> R.dmp_subresultants(f, g) == [f, g, a, b]
  388. True
  389. """
  390. return dmp_inner_subresultants(f, g, u, K)[0]
  391. def dmp_prs_resultant(f, g, u, K):
  392. """
  393. Resultant algorithm in `K[X]` using subresultant PRS.
  394. Examples
  395. ========
  396. >>> from sympy.polys import ring, ZZ
  397. >>> R, x,y = ring("x,y", ZZ)
  398. >>> f = 3*x**2*y - y**3 - 4
  399. >>> g = x**2 + x*y**3 - 9
  400. >>> a = 3*x*y**4 + y**3 - 27*y + 4
  401. >>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16
  402. >>> res, prs = R.dmp_prs_resultant(f, g)
  403. >>> res == b # resultant has n-1 variables
  404. False
  405. >>> res == b.drop(x)
  406. True
  407. >>> prs == [f, g, a, b]
  408. True
  409. """
  410. if not u:
  411. return dup_prs_resultant(f, g, K)
  412. if dmp_zero_p(f, u) or dmp_zero_p(g, u):
  413. return (dmp_zero(u - 1), [])
  414. R, S = dmp_inner_subresultants(f, g, u, K)
  415. if dmp_degree(R[-1], u) > 0:
  416. return (dmp_zero(u - 1), R)
  417. return S[-1], R
  418. def dmp_zz_modular_resultant(f, g, p, u, K):
  419. """
  420. Compute resultant of `f` and `g` modulo a prime `p`.
  421. Examples
  422. ========
  423. >>> from sympy.polys import ring, ZZ
  424. >>> R, x,y = ring("x,y", ZZ)
  425. >>> f = x + y + 2
  426. >>> g = 2*x*y + x + 3
  427. >>> R.dmp_zz_modular_resultant(f, g, 5)
  428. -2*y**2 + 1
  429. """
  430. if not u:
  431. return gf_int(dup_prs_resultant(f, g, K)[0] % p, p)
  432. v = u - 1
  433. n = dmp_degree(f, u)
  434. m = dmp_degree(g, u)
  435. N = dmp_degree_in(f, 1, u)
  436. M = dmp_degree_in(g, 1, u)
  437. B = n*M + m*N
  438. D, a = [K.one], -K.one
  439. r = dmp_zero(v)
  440. while dup_degree(D) <= B:
  441. while True:
  442. a += K.one
  443. if a == p:
  444. raise HomomorphismFailed('no luck')
  445. F = dmp_eval_in(f, gf_int(a, p), 1, u, K)
  446. if dmp_degree(F, v) == n:
  447. G = dmp_eval_in(g, gf_int(a, p), 1, u, K)
  448. if dmp_degree(G, v) == m:
  449. break
  450. R = dmp_zz_modular_resultant(F, G, p, v, K)
  451. e = dmp_eval(r, a, v, K)
  452. if not v:
  453. R = dup_strip([R])
  454. e = dup_strip([e])
  455. else:
  456. R = [R]
  457. e = [e]
  458. d = K.invert(dup_eval(D, a, K), p)
  459. d = dup_mul_ground(D, d, K)
  460. d = dmp_raise(d, v, 0, K)
  461. c = dmp_mul(d, dmp_sub(R, e, v, K), v, K)
  462. r = dmp_add(r, c, v, K)
  463. r = dmp_ground_trunc(r, p, v, K)
  464. D = dup_mul(D, [K.one, -a], K)
  465. D = dup_trunc(D, p, K)
  466. return r
  467. def _collins_crt(r, R, P, p, K):
  468. """Wrapper of CRT for Collins's resultant algorithm. """
  469. return gf_int(gf_crt([r, R], [P, p], K), P*p)
  470. def dmp_zz_collins_resultant(f, g, u, K):
  471. """
  472. Collins's modular resultant algorithm in `Z[X]`.
  473. Examples
  474. ========
  475. >>> from sympy.polys import ring, ZZ
  476. >>> R, x,y = ring("x,y", ZZ)
  477. >>> f = x + y + 2
  478. >>> g = 2*x*y + x + 3
  479. >>> R.dmp_zz_collins_resultant(f, g)
  480. -2*y**2 - 5*y + 1
  481. """
  482. n = dmp_degree(f, u)
  483. m = dmp_degree(g, u)
  484. if n < 0 or m < 0:
  485. return dmp_zero(u - 1)
  486. A = dmp_max_norm(f, u, K)
  487. B = dmp_max_norm(g, u, K)
  488. a = dmp_ground_LC(f, u, K)
  489. b = dmp_ground_LC(g, u, K)
  490. v = u - 1
  491. B = K(2)*K.factorial(K(n + m))*A**m*B**n
  492. r, p, P = dmp_zero(v), K.one, K.one
  493. while P <= B:
  494. p = K(nextprime(p))
  495. while not (a % p) or not (b % p):
  496. p = K(nextprime(p))
  497. F = dmp_ground_trunc(f, p, u, K)
  498. G = dmp_ground_trunc(g, p, u, K)
  499. try:
  500. R = dmp_zz_modular_resultant(F, G, p, u, K)
  501. except HomomorphismFailed:
  502. continue
  503. if K.is_one(P):
  504. r = R
  505. else:
  506. r = dmp_apply_pairs(r, R, _collins_crt, (P, p, K), v, K)
  507. P *= p
  508. return r
  509. def dmp_qq_collins_resultant(f, g, u, K0):
  510. """
  511. Collins's modular resultant algorithm in `Q[X]`.
  512. Examples
  513. ========
  514. >>> from sympy.polys import ring, QQ
  515. >>> R, x,y = ring("x,y", QQ)
  516. >>> f = QQ(1,2)*x + y + QQ(2,3)
  517. >>> g = 2*x*y + x + 3
  518. >>> R.dmp_qq_collins_resultant(f, g)
  519. -2*y**2 - 7/3*y + 5/6
  520. """
  521. n = dmp_degree(f, u)
  522. m = dmp_degree(g, u)
  523. if n < 0 or m < 0:
  524. return dmp_zero(u - 1)
  525. K1 = K0.get_ring()
  526. cf, f = dmp_clear_denoms(f, u, K0, K1)
  527. cg, g = dmp_clear_denoms(g, u, K0, K1)
  528. f = dmp_convert(f, u, K0, K1)
  529. g = dmp_convert(g, u, K0, K1)
  530. r = dmp_zz_collins_resultant(f, g, u, K1)
  531. r = dmp_convert(r, u - 1, K1, K0)
  532. c = K0.convert(cf**m * cg**n, K1)
  533. return dmp_quo_ground(r, c, u - 1, K0)
  534. def dmp_resultant(f, g, u, K, includePRS=False):
  535. """
  536. Computes resultant of two polynomials in `K[X]`.
  537. Examples
  538. ========
  539. >>> from sympy.polys import ring, ZZ
  540. >>> R, x,y = ring("x,y", ZZ)
  541. >>> f = 3*x**2*y - y**3 - 4
  542. >>> g = x**2 + x*y**3 - 9
  543. >>> R.dmp_resultant(f, g)
  544. -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16
  545. """
  546. if not u:
  547. return dup_resultant(f, g, K, includePRS=includePRS)
  548. if includePRS:
  549. return dmp_prs_resultant(f, g, u, K)
  550. if K.is_Field:
  551. if K.is_QQ and query('USE_COLLINS_RESULTANT'):
  552. return dmp_qq_collins_resultant(f, g, u, K)
  553. else:
  554. if K.is_ZZ and query('USE_COLLINS_RESULTANT'):
  555. return dmp_zz_collins_resultant(f, g, u, K)
  556. return dmp_prs_resultant(f, g, u, K)[0]
  557. def dup_discriminant(f, K):
  558. """
  559. Computes discriminant of a polynomial in `K[x]`.
  560. Examples
  561. ========
  562. >>> from sympy.polys import ring, ZZ
  563. >>> R, x = ring("x", ZZ)
  564. >>> R.dup_discriminant(x**2 + 2*x + 3)
  565. -8
  566. """
  567. d = dup_degree(f)
  568. if d <= 0:
  569. return K.zero
  570. else:
  571. s = (-1)**((d*(d - 1)) // 2)
  572. c = dup_LC(f, K)
  573. r = dup_resultant(f, dup_diff(f, 1, K), K)
  574. return K.quo(r, c*K(s))
  575. def dmp_discriminant(f, u, K):
  576. """
  577. Computes discriminant of a polynomial in `K[X]`.
  578. Examples
  579. ========
  580. >>> from sympy.polys import ring, ZZ
  581. >>> R, x,y,z,t = ring("x,y,z,t", ZZ)
  582. >>> R.dmp_discriminant(x**2*y + x*z + t)
  583. -4*y*t + z**2
  584. """
  585. if not u:
  586. return dup_discriminant(f, K)
  587. d, v = dmp_degree(f, u), u - 1
  588. if d <= 0:
  589. return dmp_zero(v)
  590. else:
  591. s = (-1)**((d*(d - 1)) // 2)
  592. c = dmp_LC(f, K)
  593. r = dmp_resultant(f, dmp_diff(f, 1, u, K), u, K)
  594. c = dmp_mul_ground(c, K(s), v, K)
  595. return dmp_quo(r, c, v, K)
  596. def _dup_rr_trivial_gcd(f, g, K):
  597. """Handle trivial cases in GCD algorithm over a ring. """
  598. if not (f or g):
  599. return [], [], []
  600. elif not f:
  601. if K.is_nonnegative(dup_LC(g, K)):
  602. return g, [], [K.one]
  603. else:
  604. return dup_neg(g, K), [], [-K.one]
  605. elif not g:
  606. if K.is_nonnegative(dup_LC(f, K)):
  607. return f, [K.one], []
  608. else:
  609. return dup_neg(f, K), [-K.one], []
  610. return None
  611. def _dup_ff_trivial_gcd(f, g, K):
  612. """Handle trivial cases in GCD algorithm over a field. """
  613. if not (f or g):
  614. return [], [], []
  615. elif not f:
  616. return dup_monic(g, K), [], [dup_LC(g, K)]
  617. elif not g:
  618. return dup_monic(f, K), [dup_LC(f, K)], []
  619. else:
  620. return None
  621. def _dmp_rr_trivial_gcd(f, g, u, K):
  622. """Handle trivial cases in GCD algorithm over a ring. """
  623. zero_f = dmp_zero_p(f, u)
  624. zero_g = dmp_zero_p(g, u)
  625. if_contain_one = dmp_one_p(f, u, K) or dmp_one_p(g, u, K)
  626. if zero_f and zero_g:
  627. return tuple(dmp_zeros(3, u, K))
  628. elif zero_f:
  629. if K.is_nonnegative(dmp_ground_LC(g, u, K)):
  630. return g, dmp_zero(u), dmp_one(u, K)
  631. else:
  632. return dmp_neg(g, u, K), dmp_zero(u), dmp_ground(-K.one, u)
  633. elif zero_g:
  634. if K.is_nonnegative(dmp_ground_LC(f, u, K)):
  635. return f, dmp_one(u, K), dmp_zero(u)
  636. else:
  637. return dmp_neg(f, u, K), dmp_ground(-K.one, u), dmp_zero(u)
  638. elif if_contain_one:
  639. return dmp_one(u, K), f, g
  640. elif query('USE_SIMPLIFY_GCD'):
  641. return _dmp_simplify_gcd(f, g, u, K)
  642. else:
  643. return None
  644. def _dmp_ff_trivial_gcd(f, g, u, K):
  645. """Handle trivial cases in GCD algorithm over a field. """
  646. zero_f = dmp_zero_p(f, u)
  647. zero_g = dmp_zero_p(g, u)
  648. if zero_f and zero_g:
  649. return tuple(dmp_zeros(3, u, K))
  650. elif zero_f:
  651. return (dmp_ground_monic(g, u, K),
  652. dmp_zero(u),
  653. dmp_ground(dmp_ground_LC(g, u, K), u))
  654. elif zero_g:
  655. return (dmp_ground_monic(f, u, K),
  656. dmp_ground(dmp_ground_LC(f, u, K), u),
  657. dmp_zero(u))
  658. elif query('USE_SIMPLIFY_GCD'):
  659. return _dmp_simplify_gcd(f, g, u, K)
  660. else:
  661. return None
  662. def _dmp_simplify_gcd(f, g, u, K):
  663. """Try to eliminate `x_0` from GCD computation in `K[X]`. """
  664. df = dmp_degree(f, u)
  665. dg = dmp_degree(g, u)
  666. if df > 0 and dg > 0:
  667. return None
  668. if not (df or dg):
  669. F = dmp_LC(f, K)
  670. G = dmp_LC(g, K)
  671. else:
  672. if not df:
  673. F = dmp_LC(f, K)
  674. G = dmp_content(g, u, K)
  675. else:
  676. F = dmp_content(f, u, K)
  677. G = dmp_LC(g, K)
  678. v = u - 1
  679. h = dmp_gcd(F, G, v, K)
  680. cff = [ dmp_quo(cf, h, v, K) for cf in f ]
  681. cfg = [ dmp_quo(cg, h, v, K) for cg in g ]
  682. return [h], cff, cfg
  683. def dup_rr_prs_gcd(f, g, K):
  684. """
  685. Computes polynomial GCD using subresultants over a ring.
  686. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
  687. and ``cfg = quo(g, h)``.
  688. Examples
  689. ========
  690. >>> from sympy.polys import ring, ZZ
  691. >>> R, x = ring("x", ZZ)
  692. >>> R.dup_rr_prs_gcd(x**2 - 1, x**2 - 3*x + 2)
  693. (x - 1, x + 1, x - 2)
  694. """
  695. result = _dup_rr_trivial_gcd(f, g, K)
  696. if result is not None:
  697. return result
  698. fc, F = dup_primitive(f, K)
  699. gc, G = dup_primitive(g, K)
  700. c = K.gcd(fc, gc)
  701. h = dup_subresultants(F, G, K)[-1]
  702. _, h = dup_primitive(h, K)
  703. c *= K.canonical_unit(dup_LC(h, K))
  704. h = dup_mul_ground(h, c, K)
  705. cff = dup_quo(f, h, K)
  706. cfg = dup_quo(g, h, K)
  707. return h, cff, cfg
  708. def dup_ff_prs_gcd(f, g, K):
  709. """
  710. Computes polynomial GCD using subresultants over a field.
  711. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
  712. and ``cfg = quo(g, h)``.
  713. Examples
  714. ========
  715. >>> from sympy.polys import ring, QQ
  716. >>> R, x = ring("x", QQ)
  717. >>> R.dup_ff_prs_gcd(x**2 - 1, x**2 - 3*x + 2)
  718. (x - 1, x + 1, x - 2)
  719. """
  720. result = _dup_ff_trivial_gcd(f, g, K)
  721. if result is not None:
  722. return result
  723. h = dup_subresultants(f, g, K)[-1]
  724. h = dup_monic(h, K)
  725. cff = dup_quo(f, h, K)
  726. cfg = dup_quo(g, h, K)
  727. return h, cff, cfg
  728. def dmp_rr_prs_gcd(f, g, u, K):
  729. """
  730. Computes polynomial GCD using subresultants over a ring.
  731. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
  732. and ``cfg = quo(g, h)``.
  733. Examples
  734. ========
  735. >>> from sympy.polys import ring, ZZ
  736. >>> R, x,y, = ring("x,y", ZZ)
  737. >>> f = x**2 + 2*x*y + y**2
  738. >>> g = x**2 + x*y
  739. >>> R.dmp_rr_prs_gcd(f, g)
  740. (x + y, x + y, x)
  741. """
  742. if not u:
  743. return dup_rr_prs_gcd(f, g, K)
  744. result = _dmp_rr_trivial_gcd(f, g, u, K)
  745. if result is not None:
  746. return result
  747. fc, F = dmp_primitive(f, u, K)
  748. gc, G = dmp_primitive(g, u, K)
  749. h = dmp_subresultants(F, G, u, K)[-1]
  750. c, _, _ = dmp_rr_prs_gcd(fc, gc, u - 1, K)
  751. if K.is_negative(dmp_ground_LC(h, u, K)):
  752. h = dmp_neg(h, u, K)
  753. _, h = dmp_primitive(h, u, K)
  754. h = dmp_mul_term(h, c, 0, u, K)
  755. cff = dmp_quo(f, h, u, K)
  756. cfg = dmp_quo(g, h, u, K)
  757. return h, cff, cfg
  758. def dmp_ff_prs_gcd(f, g, u, K):
  759. """
  760. Computes polynomial GCD using subresultants over a field.
  761. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``,
  762. and ``cfg = quo(g, h)``.
  763. Examples
  764. ========
  765. >>> from sympy.polys import ring, QQ
  766. >>> R, x,y, = ring("x,y", QQ)
  767. >>> f = QQ(1,2)*x**2 + x*y + QQ(1,2)*y**2
  768. >>> g = x**2 + x*y
  769. >>> R.dmp_ff_prs_gcd(f, g)
  770. (x + y, 1/2*x + 1/2*y, x)
  771. """
  772. if not u:
  773. return dup_ff_prs_gcd(f, g, K)
  774. result = _dmp_ff_trivial_gcd(f, g, u, K)
  775. if result is not None:
  776. return result
  777. fc, F = dmp_primitive(f, u, K)
  778. gc, G = dmp_primitive(g, u, K)
  779. h = dmp_subresultants(F, G, u, K)[-1]
  780. c, _, _ = dmp_ff_prs_gcd(fc, gc, u - 1, K)
  781. _, h = dmp_primitive(h, u, K)
  782. h = dmp_mul_term(h, c, 0, u, K)
  783. h = dmp_ground_monic(h, u, K)
  784. cff = dmp_quo(f, h, u, K)
  785. cfg = dmp_quo(g, h, u, K)
  786. return h, cff, cfg
  787. HEU_GCD_MAX = 6
  788. def _dup_zz_gcd_interpolate(h, x, K):
  789. """Interpolate polynomial GCD from integer GCD. """
  790. f = []
  791. while h:
  792. g = h % x
  793. if g > x // 2:
  794. g -= x
  795. f.insert(0, g)
  796. h = (h - g) // x
  797. return f
  798. def dup_zz_heu_gcd(f, g, K):
  799. """
  800. Heuristic polynomial GCD in `Z[x]`.
  801. Given univariate polynomials `f` and `g` in `Z[x]`, returns
  802. their GCD and cofactors, i.e. polynomials ``h``, ``cff`` and ``cfg``
  803. such that::
  804. h = gcd(f, g), cff = quo(f, h) and cfg = quo(g, h)
  805. The algorithm is purely heuristic which means it may fail to compute
  806. the GCD. This will be signaled by raising an exception. In this case
  807. you will need to switch to another GCD method.
  808. The algorithm computes the polynomial GCD by evaluating polynomials
  809. f and g at certain points and computing (fast) integer GCD of those
  810. evaluations. The polynomial GCD is recovered from the integer image
  811. by interpolation. The final step is to verify if the result is the
  812. correct GCD. This gives cofactors as a side effect.
  813. Examples
  814. ========
  815. >>> from sympy.polys import ring, ZZ
  816. >>> R, x = ring("x", ZZ)
  817. >>> R.dup_zz_heu_gcd(x**2 - 1, x**2 - 3*x + 2)
  818. (x - 1, x + 1, x - 2)
  819. References
  820. ==========
  821. .. [1] [Liao95]_
  822. """
  823. result = _dup_rr_trivial_gcd(f, g, K)
  824. if result is not None:
  825. return result
  826. df = dup_degree(f)
  827. dg = dup_degree(g)
  828. gcd, f, g = dup_extract(f, g, K)
  829. if df == 0 or dg == 0:
  830. return [gcd], f, g
  831. f_norm = dup_max_norm(f, K)
  832. g_norm = dup_max_norm(g, K)
  833. B = K(2*min(f_norm, g_norm) + 29)
  834. x = max(min(B, 99*K.sqrt(B)),
  835. 2*min(f_norm // abs(dup_LC(f, K)),
  836. g_norm // abs(dup_LC(g, K))) + 2)
  837. for i in range(0, HEU_GCD_MAX):
  838. ff = dup_eval(f, x, K)
  839. gg = dup_eval(g, x, K)
  840. if ff and gg:
  841. h = K.gcd(ff, gg)
  842. cff = ff // h
  843. cfg = gg // h
  844. h = _dup_zz_gcd_interpolate(h, x, K)
  845. h = dup_primitive(h, K)[1]
  846. cff_, r = dup_div(f, h, K)
  847. if not r:
  848. cfg_, r = dup_div(g, h, K)
  849. if not r:
  850. h = dup_mul_ground(h, gcd, K)
  851. return h, cff_, cfg_
  852. cff = _dup_zz_gcd_interpolate(cff, x, K)
  853. h, r = dup_div(f, cff, K)
  854. if not r:
  855. cfg_, r = dup_div(g, h, K)
  856. if not r:
  857. h = dup_mul_ground(h, gcd, K)
  858. return h, cff, cfg_
  859. cfg = _dup_zz_gcd_interpolate(cfg, x, K)
  860. h, r = dup_div(g, cfg, K)
  861. if not r:
  862. cff_, r = dup_div(f, h, K)
  863. if not r:
  864. h = dup_mul_ground(h, gcd, K)
  865. return h, cff_, cfg
  866. x = 73794*x * K.sqrt(K.sqrt(x)) // 27011
  867. raise HeuristicGCDFailed('no luck')
  868. def _dmp_zz_gcd_interpolate(h, x, v, K):
  869. """Interpolate polynomial GCD from integer GCD. """
  870. f = []
  871. while not dmp_zero_p(h, v):
  872. g = dmp_ground_trunc(h, x, v, K)
  873. f.insert(0, g)
  874. h = dmp_sub(h, g, v, K)
  875. h = dmp_quo_ground(h, x, v, K)
  876. if K.is_negative(dmp_ground_LC(f, v + 1, K)):
  877. return dmp_neg(f, v + 1, K)
  878. else:
  879. return f
  880. def dmp_zz_heu_gcd(f, g, u, K):
  881. """
  882. Heuristic polynomial GCD in `Z[X]`.
  883. Given univariate polynomials `f` and `g` in `Z[X]`, returns
  884. their GCD and cofactors, i.e. polynomials ``h``, ``cff`` and ``cfg``
  885. such that::
  886. h = gcd(f, g), cff = quo(f, h) and cfg = quo(g, h)
  887. The algorithm is purely heuristic which means it may fail to compute
  888. the GCD. This will be signaled by raising an exception. In this case
  889. you will need to switch to another GCD method.
  890. The algorithm computes the polynomial GCD by evaluating polynomials
  891. f and g at certain points and computing (fast) integer GCD of those
  892. evaluations. The polynomial GCD is recovered from the integer image
  893. by interpolation. The evaluation process reduces f and g variable by
  894. variable into a large integer. The final step is to verify if the
  895. interpolated polynomial is the correct GCD. This gives cofactors of
  896. the input polynomials as a side effect.
  897. Examples
  898. ========
  899. >>> from sympy.polys import ring, ZZ
  900. >>> R, x,y, = ring("x,y", ZZ)
  901. >>> f = x**2 + 2*x*y + y**2
  902. >>> g = x**2 + x*y
  903. >>> R.dmp_zz_heu_gcd(f, g)
  904. (x + y, x + y, x)
  905. References
  906. ==========
  907. .. [1] [Liao95]_
  908. """
  909. if not u:
  910. return dup_zz_heu_gcd(f, g, K)
  911. result = _dmp_rr_trivial_gcd(f, g, u, K)
  912. if result is not None:
  913. return result
  914. gcd, f, g = dmp_ground_extract(f, g, u, K)
  915. f_norm = dmp_max_norm(f, u, K)
  916. g_norm = dmp_max_norm(g, u, K)
  917. B = K(2*min(f_norm, g_norm) + 29)
  918. x = max(min(B, 99*K.sqrt(B)),
  919. 2*min(f_norm // abs(dmp_ground_LC(f, u, K)),
  920. g_norm // abs(dmp_ground_LC(g, u, K))) + 2)
  921. for i in range(0, HEU_GCD_MAX):
  922. ff = dmp_eval(f, x, u, K)
  923. gg = dmp_eval(g, x, u, K)
  924. v = u - 1
  925. if not (dmp_zero_p(ff, v) or dmp_zero_p(gg, v)):
  926. h, cff, cfg = dmp_zz_heu_gcd(ff, gg, v, K)
  927. h = _dmp_zz_gcd_interpolate(h, x, v, K)
  928. h = dmp_ground_primitive(h, u, K)[1]
  929. cff_, r = dmp_div(f, h, u, K)
  930. if dmp_zero_p(r, u):
  931. cfg_, r = dmp_div(g, h, u, K)
  932. if dmp_zero_p(r, u):
  933. h = dmp_mul_ground(h, gcd, u, K)
  934. return h, cff_, cfg_
  935. cff = _dmp_zz_gcd_interpolate(cff, x, v, K)
  936. h, r = dmp_div(f, cff, u, K)
  937. if dmp_zero_p(r, u):
  938. cfg_, r = dmp_div(g, h, u, K)
  939. if dmp_zero_p(r, u):
  940. h = dmp_mul_ground(h, gcd, u, K)
  941. return h, cff, cfg_
  942. cfg = _dmp_zz_gcd_interpolate(cfg, x, v, K)
  943. h, r = dmp_div(g, cfg, u, K)
  944. if dmp_zero_p(r, u):
  945. cff_, r = dmp_div(f, h, u, K)
  946. if dmp_zero_p(r, u):
  947. h = dmp_mul_ground(h, gcd, u, K)
  948. return h, cff_, cfg
  949. x = 73794*x * K.sqrt(K.sqrt(x)) // 27011
  950. raise HeuristicGCDFailed('no luck')
  951. def dup_qq_heu_gcd(f, g, K0):
  952. """
  953. Heuristic polynomial GCD in `Q[x]`.
  954. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
  955. ``cff = quo(f, h)``, and ``cfg = quo(g, h)``.
  956. Examples
  957. ========
  958. >>> from sympy.polys import ring, QQ
  959. >>> R, x = ring("x", QQ)
  960. >>> f = QQ(1,2)*x**2 + QQ(7,4)*x + QQ(3,2)
  961. >>> g = QQ(1,2)*x**2 + x
  962. >>> R.dup_qq_heu_gcd(f, g)
  963. (x + 2, 1/2*x + 3/4, 1/2*x)
  964. """
  965. result = _dup_ff_trivial_gcd(f, g, K0)
  966. if result is not None:
  967. return result
  968. K1 = K0.get_ring()
  969. cf, f = dup_clear_denoms(f, K0, K1)
  970. cg, g = dup_clear_denoms(g, K0, K1)
  971. f = dup_convert(f, K0, K1)
  972. g = dup_convert(g, K0, K1)
  973. h, cff, cfg = dup_zz_heu_gcd(f, g, K1)
  974. h = dup_convert(h, K1, K0)
  975. c = dup_LC(h, K0)
  976. h = dup_monic(h, K0)
  977. cff = dup_convert(cff, K1, K0)
  978. cfg = dup_convert(cfg, K1, K0)
  979. cff = dup_mul_ground(cff, K0.quo(c, cf), K0)
  980. cfg = dup_mul_ground(cfg, K0.quo(c, cg), K0)
  981. return h, cff, cfg
  982. def dmp_qq_heu_gcd(f, g, u, K0):
  983. """
  984. Heuristic polynomial GCD in `Q[X]`.
  985. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
  986. ``cff = quo(f, h)``, and ``cfg = quo(g, h)``.
  987. Examples
  988. ========
  989. >>> from sympy.polys import ring, QQ
  990. >>> R, x,y, = ring("x,y", QQ)
  991. >>> f = QQ(1,4)*x**2 + x*y + y**2
  992. >>> g = QQ(1,2)*x**2 + x*y
  993. >>> R.dmp_qq_heu_gcd(f, g)
  994. (x + 2*y, 1/4*x + 1/2*y, 1/2*x)
  995. """
  996. result = _dmp_ff_trivial_gcd(f, g, u, K0)
  997. if result is not None:
  998. return result
  999. K1 = K0.get_ring()
  1000. cf, f = dmp_clear_denoms(f, u, K0, K1)
  1001. cg, g = dmp_clear_denoms(g, u, K0, K1)
  1002. f = dmp_convert(f, u, K0, K1)
  1003. g = dmp_convert(g, u, K0, K1)
  1004. h, cff, cfg = dmp_zz_heu_gcd(f, g, u, K1)
  1005. h = dmp_convert(h, u, K1, K0)
  1006. c = dmp_ground_LC(h, u, K0)
  1007. h = dmp_ground_monic(h, u, K0)
  1008. cff = dmp_convert(cff, u, K1, K0)
  1009. cfg = dmp_convert(cfg, u, K1, K0)
  1010. cff = dmp_mul_ground(cff, K0.quo(c, cf), u, K0)
  1011. cfg = dmp_mul_ground(cfg, K0.quo(c, cg), u, K0)
  1012. return h, cff, cfg
  1013. def dup_inner_gcd(f, g, K):
  1014. """
  1015. Computes polynomial GCD and cofactors of `f` and `g` in `K[x]`.
  1016. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
  1017. ``cff = quo(f, h)``, and ``cfg = quo(g, h)``.
  1018. Examples
  1019. ========
  1020. >>> from sympy.polys import ring, ZZ
  1021. >>> R, x = ring("x", ZZ)
  1022. >>> R.dup_inner_gcd(x**2 - 1, x**2 - 3*x + 2)
  1023. (x - 1, x + 1, x - 2)
  1024. """
  1025. if not K.is_Exact:
  1026. try:
  1027. exact = K.get_exact()
  1028. except DomainError:
  1029. return [K.one], f, g
  1030. f = dup_convert(f, K, exact)
  1031. g = dup_convert(g, K, exact)
  1032. h, cff, cfg = dup_inner_gcd(f, g, exact)
  1033. h = dup_convert(h, exact, K)
  1034. cff = dup_convert(cff, exact, K)
  1035. cfg = dup_convert(cfg, exact, K)
  1036. return h, cff, cfg
  1037. elif K.is_Field:
  1038. if K.is_QQ and query('USE_HEU_GCD'):
  1039. try:
  1040. return dup_qq_heu_gcd(f, g, K)
  1041. except HeuristicGCDFailed:
  1042. pass
  1043. return dup_ff_prs_gcd(f, g, K)
  1044. else:
  1045. if K.is_ZZ and query('USE_HEU_GCD'):
  1046. try:
  1047. return dup_zz_heu_gcd(f, g, K)
  1048. except HeuristicGCDFailed:
  1049. pass
  1050. return dup_rr_prs_gcd(f, g, K)
  1051. def _dmp_inner_gcd(f, g, u, K):
  1052. """Helper function for `dmp_inner_gcd()`. """
  1053. if not K.is_Exact:
  1054. try:
  1055. exact = K.get_exact()
  1056. except DomainError:
  1057. return dmp_one(u, K), f, g
  1058. f = dmp_convert(f, u, K, exact)
  1059. g = dmp_convert(g, u, K, exact)
  1060. h, cff, cfg = _dmp_inner_gcd(f, g, u, exact)
  1061. h = dmp_convert(h, u, exact, K)
  1062. cff = dmp_convert(cff, u, exact, K)
  1063. cfg = dmp_convert(cfg, u, exact, K)
  1064. return h, cff, cfg
  1065. elif K.is_Field:
  1066. if K.is_QQ and query('USE_HEU_GCD'):
  1067. try:
  1068. return dmp_qq_heu_gcd(f, g, u, K)
  1069. except HeuristicGCDFailed:
  1070. pass
  1071. return dmp_ff_prs_gcd(f, g, u, K)
  1072. else:
  1073. if K.is_ZZ and query('USE_HEU_GCD'):
  1074. try:
  1075. return dmp_zz_heu_gcd(f, g, u, K)
  1076. except HeuristicGCDFailed:
  1077. pass
  1078. return dmp_rr_prs_gcd(f, g, u, K)
  1079. def dmp_inner_gcd(f, g, u, K):
  1080. """
  1081. Computes polynomial GCD and cofactors of `f` and `g` in `K[X]`.
  1082. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``,
  1083. ``cff = quo(f, h)``, and ``cfg = quo(g, h)``.
  1084. Examples
  1085. ========
  1086. >>> from sympy.polys import ring, ZZ
  1087. >>> R, x,y, = ring("x,y", ZZ)
  1088. >>> f = x**2 + 2*x*y + y**2
  1089. >>> g = x**2 + x*y
  1090. >>> R.dmp_inner_gcd(f, g)
  1091. (x + y, x + y, x)
  1092. """
  1093. if not u:
  1094. return dup_inner_gcd(f, g, K)
  1095. J, (f, g) = dmp_multi_deflate((f, g), u, K)
  1096. h, cff, cfg = _dmp_inner_gcd(f, g, u, K)
  1097. return (dmp_inflate(h, J, u, K),
  1098. dmp_inflate(cff, J, u, K),
  1099. dmp_inflate(cfg, J, u, K))
  1100. def dup_gcd(f, g, K):
  1101. """
  1102. Computes polynomial GCD of `f` and `g` in `K[x]`.
  1103. Examples
  1104. ========
  1105. >>> from sympy.polys import ring, ZZ
  1106. >>> R, x = ring("x", ZZ)
  1107. >>> R.dup_gcd(x**2 - 1, x**2 - 3*x + 2)
  1108. x - 1
  1109. """
  1110. return dup_inner_gcd(f, g, K)[0]
  1111. def dmp_gcd(f, g, u, K):
  1112. """
  1113. Computes polynomial GCD of `f` and `g` in `K[X]`.
  1114. Examples
  1115. ========
  1116. >>> from sympy.polys import ring, ZZ
  1117. >>> R, x,y, = ring("x,y", ZZ)
  1118. >>> f = x**2 + 2*x*y + y**2
  1119. >>> g = x**2 + x*y
  1120. >>> R.dmp_gcd(f, g)
  1121. x + y
  1122. """
  1123. return dmp_inner_gcd(f, g, u, K)[0]
  1124. def dup_rr_lcm(f, g, K):
  1125. """
  1126. Computes polynomial LCM over a ring in `K[x]`.
  1127. Examples
  1128. ========
  1129. >>> from sympy.polys import ring, ZZ
  1130. >>> R, x = ring("x", ZZ)
  1131. >>> R.dup_rr_lcm(x**2 - 1, x**2 - 3*x + 2)
  1132. x**3 - 2*x**2 - x + 2
  1133. """
  1134. fc, f = dup_primitive(f, K)
  1135. gc, g = dup_primitive(g, K)
  1136. c = K.lcm(fc, gc)
  1137. h = dup_quo(dup_mul(f, g, K),
  1138. dup_gcd(f, g, K), K)
  1139. return dup_mul_ground(h, c, K)
  1140. def dup_ff_lcm(f, g, K):
  1141. """
  1142. Computes polynomial LCM over a field in `K[x]`.
  1143. Examples
  1144. ========
  1145. >>> from sympy.polys import ring, QQ
  1146. >>> R, x = ring("x", QQ)
  1147. >>> f = QQ(1,2)*x**2 + QQ(7,4)*x + QQ(3,2)
  1148. >>> g = QQ(1,2)*x**2 + x
  1149. >>> R.dup_ff_lcm(f, g)
  1150. x**3 + 7/2*x**2 + 3*x
  1151. """
  1152. h = dup_quo(dup_mul(f, g, K),
  1153. dup_gcd(f, g, K), K)
  1154. return dup_monic(h, K)
  1155. def dup_lcm(f, g, K):
  1156. """
  1157. Computes polynomial LCM of `f` and `g` in `K[x]`.
  1158. Examples
  1159. ========
  1160. >>> from sympy.polys import ring, ZZ
  1161. >>> R, x = ring("x", ZZ)
  1162. >>> R.dup_lcm(x**2 - 1, x**2 - 3*x + 2)
  1163. x**3 - 2*x**2 - x + 2
  1164. """
  1165. if K.is_Field:
  1166. return dup_ff_lcm(f, g, K)
  1167. else:
  1168. return dup_rr_lcm(f, g, K)
  1169. def dmp_rr_lcm(f, g, u, K):
  1170. """
  1171. Computes polynomial LCM over a ring in `K[X]`.
  1172. Examples
  1173. ========
  1174. >>> from sympy.polys import ring, ZZ
  1175. >>> R, x,y, = ring("x,y", ZZ)
  1176. >>> f = x**2 + 2*x*y + y**2
  1177. >>> g = x**2 + x*y
  1178. >>> R.dmp_rr_lcm(f, g)
  1179. x**3 + 2*x**2*y + x*y**2
  1180. """
  1181. fc, f = dmp_ground_primitive(f, u, K)
  1182. gc, g = dmp_ground_primitive(g, u, K)
  1183. c = K.lcm(fc, gc)
  1184. h = dmp_quo(dmp_mul(f, g, u, K),
  1185. dmp_gcd(f, g, u, K), u, K)
  1186. return dmp_mul_ground(h, c, u, K)
  1187. def dmp_ff_lcm(f, g, u, K):
  1188. """
  1189. Computes polynomial LCM over a field in `K[X]`.
  1190. Examples
  1191. ========
  1192. >>> from sympy.polys import ring, QQ
  1193. >>> R, x,y, = ring("x,y", QQ)
  1194. >>> f = QQ(1,4)*x**2 + x*y + y**2
  1195. >>> g = QQ(1,2)*x**2 + x*y
  1196. >>> R.dmp_ff_lcm(f, g)
  1197. x**3 + 4*x**2*y + 4*x*y**2
  1198. """
  1199. h = dmp_quo(dmp_mul(f, g, u, K),
  1200. dmp_gcd(f, g, u, K), u, K)
  1201. return dmp_ground_monic(h, u, K)
  1202. def dmp_lcm(f, g, u, K):
  1203. """
  1204. Computes polynomial LCM of `f` and `g` in `K[X]`.
  1205. Examples
  1206. ========
  1207. >>> from sympy.polys import ring, ZZ
  1208. >>> R, x,y, = ring("x,y", ZZ)
  1209. >>> f = x**2 + 2*x*y + y**2
  1210. >>> g = x**2 + x*y
  1211. >>> R.dmp_lcm(f, g)
  1212. x**3 + 2*x**2*y + x*y**2
  1213. """
  1214. if not u:
  1215. return dup_lcm(f, g, K)
  1216. if K.is_Field:
  1217. return dmp_ff_lcm(f, g, u, K)
  1218. else:
  1219. return dmp_rr_lcm(f, g, u, K)
  1220. def dmp_content(f, u, K):
  1221. """
  1222. Returns GCD of multivariate coefficients.
  1223. Examples
  1224. ========
  1225. >>> from sympy.polys import ring, ZZ
  1226. >>> R, x,y, = ring("x,y", ZZ)
  1227. >>> R.dmp_content(2*x*y + 6*x + 4*y + 12)
  1228. 2*y + 6
  1229. """
  1230. cont, v = dmp_LC(f, K), u - 1
  1231. if dmp_zero_p(f, u):
  1232. return cont
  1233. for c in f[1:]:
  1234. cont = dmp_gcd(cont, c, v, K)
  1235. if dmp_one_p(cont, v, K):
  1236. break
  1237. if K.is_negative(dmp_ground_LC(cont, v, K)):
  1238. return dmp_neg(cont, v, K)
  1239. else:
  1240. return cont
  1241. def dmp_primitive(f, u, K):
  1242. """
  1243. Returns multivariate content and a primitive polynomial.
  1244. Examples
  1245. ========
  1246. >>> from sympy.polys import ring, ZZ
  1247. >>> R, x,y, = ring("x,y", ZZ)
  1248. >>> R.dmp_primitive(2*x*y + 6*x + 4*y + 12)
  1249. (2*y + 6, x + 2)
  1250. """
  1251. cont, v = dmp_content(f, u, K), u - 1
  1252. if dmp_zero_p(f, u) or dmp_one_p(cont, v, K):
  1253. return cont, f
  1254. else:
  1255. return cont, [ dmp_quo(c, cont, v, K) for c in f ]
  1256. def dup_cancel(f, g, K, include=True):
  1257. """
  1258. Cancel common factors in a rational function `f/g`.
  1259. Examples
  1260. ========
  1261. >>> from sympy.polys import ring, ZZ
  1262. >>> R, x = ring("x", ZZ)
  1263. >>> R.dup_cancel(2*x**2 - 2, x**2 - 2*x + 1)
  1264. (2*x + 2, x - 1)
  1265. """
  1266. return dmp_cancel(f, g, 0, K, include=include)
  1267. def dmp_cancel(f, g, u, K, include=True):
  1268. """
  1269. Cancel common factors in a rational function `f/g`.
  1270. Examples
  1271. ========
  1272. >>> from sympy.polys import ring, ZZ
  1273. >>> R, x,y = ring("x,y", ZZ)
  1274. >>> R.dmp_cancel(2*x**2 - 2, x**2 - 2*x + 1)
  1275. (2*x + 2, x - 1)
  1276. """
  1277. K0 = None
  1278. if K.is_Field and K.has_assoc_Ring:
  1279. K0, K = K, K.get_ring()
  1280. cq, f = dmp_clear_denoms(f, u, K0, K, convert=True)
  1281. cp, g = dmp_clear_denoms(g, u, K0, K, convert=True)
  1282. else:
  1283. cp, cq = K.one, K.one
  1284. _, p, q = dmp_inner_gcd(f, g, u, K)
  1285. if K0 is not None:
  1286. _, cp, cq = K.cofactors(cp, cq)
  1287. p = dmp_convert(p, u, K, K0)
  1288. q = dmp_convert(q, u, K, K0)
  1289. K = K0
  1290. p_neg = K.is_negative(dmp_ground_LC(p, u, K))
  1291. q_neg = K.is_negative(dmp_ground_LC(q, u, K))
  1292. if p_neg and q_neg:
  1293. p, q = dmp_neg(p, u, K), dmp_neg(q, u, K)
  1294. elif p_neg:
  1295. cp, p = -cp, dmp_neg(p, u, K)
  1296. elif q_neg:
  1297. cp, q = -cp, dmp_neg(q, u, K)
  1298. if not include:
  1299. return cp, cq, p, q
  1300. p = dmp_mul_ground(p, cp, u, K)
  1301. q = dmp_mul_ground(q, cq, u, K)
  1302. return p, q