test_rings.py 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. """Test sparse polynomials. """
  2. from functools import reduce
  3. from operator import add, mul
  4. from sympy.polys.rings import ring, xring, sring, PolyRing, PolyElement
  5. from sympy.polys.fields import field, FracField
  6. from sympy.polys.densebasic import ninf
  7. from sympy.polys.domains import ZZ, QQ, RR, FF, EX
  8. from sympy.polys.orderings import lex, grlex
  9. from sympy.polys.polyerrors import GeneratorsError, \
  10. ExactQuotientFailed, MultivariatePolynomialError, CoercionFailed
  11. from sympy.testing.pytest import raises
  12. from sympy.core import Symbol, symbols
  13. from sympy.core.singleton import S
  14. from sympy.core.numbers import pi
  15. from sympy.functions.elementary.exponential import exp
  16. from sympy.functions.elementary.miscellaneous import sqrt
  17. def test_PolyRing___init__():
  18. x, y, z, t = map(Symbol, "xyzt")
  19. assert len(PolyRing("x,y,z", ZZ, lex).gens) == 3
  20. assert len(PolyRing(x, ZZ, lex).gens) == 1
  21. assert len(PolyRing(("x", "y", "z"), ZZ, lex).gens) == 3
  22. assert len(PolyRing((x, y, z), ZZ, lex).gens) == 3
  23. assert len(PolyRing("", ZZ, lex).gens) == 0
  24. assert len(PolyRing([], ZZ, lex).gens) == 0
  25. raises(GeneratorsError, lambda: PolyRing(0, ZZ, lex))
  26. assert PolyRing("x", ZZ[t], lex).domain == ZZ[t]
  27. assert PolyRing("x", 'ZZ[t]', lex).domain == ZZ[t]
  28. assert PolyRing("x", PolyRing("t", ZZ, lex), lex).domain == ZZ[t]
  29. raises(GeneratorsError, lambda: PolyRing("x", PolyRing("x", ZZ, lex), lex))
  30. _lex = Symbol("lex")
  31. assert PolyRing("x", ZZ, lex).order == lex
  32. assert PolyRing("x", ZZ, _lex).order == lex
  33. assert PolyRing("x", ZZ, 'lex').order == lex
  34. R1 = PolyRing("x,y", ZZ, lex)
  35. R2 = PolyRing("x,y", ZZ, lex)
  36. R3 = PolyRing("x,y,z", ZZ, lex)
  37. assert R1.x == R1.gens[0]
  38. assert R1.y == R1.gens[1]
  39. assert R1.x == R2.x
  40. assert R1.y == R2.y
  41. assert R1.x != R3.x
  42. assert R1.y != R3.y
  43. def test_PolyRing___hash__():
  44. R, x, y, z = ring("x,y,z", QQ)
  45. assert hash(R)
  46. def test_PolyRing___eq__():
  47. assert ring("x,y,z", QQ)[0] == ring("x,y,z", QQ)[0]
  48. assert ring("x,y,z", QQ)[0] != ring("x,y,z", ZZ)[0]
  49. assert ring("x,y,z", ZZ)[0] != ring("x,y,z", QQ)[0]
  50. assert ring("x,y,z", QQ)[0] != ring("x,y", QQ)[0]
  51. assert ring("x,y", QQ)[0] != ring("x,y,z", QQ)[0]
  52. def test_PolyRing_ring_new():
  53. R, x, y, z = ring("x,y,z", QQ)
  54. assert R.ring_new(7) == R(7)
  55. assert R.ring_new(7*x*y*z) == 7*x*y*z
  56. f = x**2 + 2*x*y + 3*x + 4*z**2 + 5*z + 6
  57. assert R.ring_new([[[1]], [[2], [3]], [[4, 5, 6]]]) == f
  58. assert R.ring_new({(2, 0, 0): 1, (1, 1, 0): 2, (1, 0, 0): 3, (0, 0, 2): 4, (0, 0, 1): 5, (0, 0, 0): 6}) == f
  59. assert R.ring_new([((2, 0, 0), 1), ((1, 1, 0), 2), ((1, 0, 0), 3), ((0, 0, 2), 4), ((0, 0, 1), 5), ((0, 0, 0), 6)]) == f
  60. R, = ring("", QQ)
  61. assert R.ring_new([((), 7)]) == R(7)
  62. def test_PolyRing_drop():
  63. R, x,y,z = ring("x,y,z", ZZ)
  64. assert R.drop(x) == PolyRing("y,z", ZZ, lex)
  65. assert R.drop(y) == PolyRing("x,z", ZZ, lex)
  66. assert R.drop(z) == PolyRing("x,y", ZZ, lex)
  67. assert R.drop(0) == PolyRing("y,z", ZZ, lex)
  68. assert R.drop(0).drop(0) == PolyRing("z", ZZ, lex)
  69. assert R.drop(0).drop(0).drop(0) == ZZ
  70. assert R.drop(1) == PolyRing("x,z", ZZ, lex)
  71. assert R.drop(2) == PolyRing("x,y", ZZ, lex)
  72. assert R.drop(2).drop(1) == PolyRing("x", ZZ, lex)
  73. assert R.drop(2).drop(1).drop(0) == ZZ
  74. raises(ValueError, lambda: R.drop(3))
  75. raises(ValueError, lambda: R.drop(x).drop(y))
  76. def test_PolyRing___getitem__():
  77. R, x,y,z = ring("x,y,z", ZZ)
  78. assert R[0:] == PolyRing("x,y,z", ZZ, lex)
  79. assert R[1:] == PolyRing("y,z", ZZ, lex)
  80. assert R[2:] == PolyRing("z", ZZ, lex)
  81. assert R[3:] == ZZ
  82. def test_PolyRing_is_():
  83. R = PolyRing("x", QQ, lex)
  84. assert R.is_univariate is True
  85. assert R.is_multivariate is False
  86. R = PolyRing("x,y,z", QQ, lex)
  87. assert R.is_univariate is False
  88. assert R.is_multivariate is True
  89. R = PolyRing("", QQ, lex)
  90. assert R.is_univariate is False
  91. assert R.is_multivariate is False
  92. def test_PolyRing_add():
  93. R, x = ring("x", ZZ)
  94. F = [ x**2 + 2*i + 3 for i in range(4) ]
  95. assert R.add(F) == reduce(add, F) == 4*x**2 + 24
  96. R, = ring("", ZZ)
  97. assert R.add([2, 5, 7]) == 14
  98. def test_PolyRing_mul():
  99. R, x = ring("x", ZZ)
  100. F = [ x**2 + 2*i + 3 for i in range(4) ]
  101. assert R.mul(F) == reduce(mul, F) == x**8 + 24*x**6 + 206*x**4 + 744*x**2 + 945
  102. R, = ring("", ZZ)
  103. assert R.mul([2, 3, 5]) == 30
  104. def test_PolyRing_symmetric_poly():
  105. R, x, y, z, t = ring("x,y,z,t", ZZ)
  106. raises(ValueError, lambda: R.symmetric_poly(-1))
  107. raises(ValueError, lambda: R.symmetric_poly(5))
  108. assert R.symmetric_poly(0) == R.one
  109. assert R.symmetric_poly(1) == x + y + z + t
  110. assert R.symmetric_poly(2) == x*y + x*z + x*t + y*z + y*t + z*t
  111. assert R.symmetric_poly(3) == x*y*z + x*y*t + x*z*t + y*z*t
  112. assert R.symmetric_poly(4) == x*y*z*t
  113. def test_sring():
  114. x, y, z, t = symbols("x,y,z,t")
  115. R = PolyRing("x,y,z", ZZ, lex)
  116. assert sring(x + 2*y + 3*z) == (R, R.x + 2*R.y + 3*R.z)
  117. R = PolyRing("x,y,z", QQ, lex)
  118. assert sring(x + 2*y + z/3) == (R, R.x + 2*R.y + R.z/3)
  119. assert sring([x, 2*y, z/3]) == (R, [R.x, 2*R.y, R.z/3])
  120. Rt = PolyRing("t", ZZ, lex)
  121. R = PolyRing("x,y,z", Rt, lex)
  122. assert sring(x + 2*t*y + 3*t**2*z, x, y, z) == (R, R.x + 2*Rt.t*R.y + 3*Rt.t**2*R.z)
  123. Rt = PolyRing("t", QQ, lex)
  124. R = PolyRing("x,y,z", Rt, lex)
  125. assert sring(x + t*y/2 + t**2*z/3, x, y, z) == (R, R.x + Rt.t*R.y/2 + Rt.t**2*R.z/3)
  126. Rt = FracField("t", ZZ, lex)
  127. R = PolyRing("x,y,z", Rt, lex)
  128. assert sring(x + 2*y/t + t**2*z/3, x, y, z) == (R, R.x + 2*R.y/Rt.t + Rt.t**2*R.z/3)
  129. r = sqrt(2) - sqrt(3)
  130. R, a = sring(r, extension=True)
  131. assert R.domain == QQ.algebraic_field(sqrt(2) + sqrt(3))
  132. assert R.gens == ()
  133. assert a == R.domain.from_sympy(r)
  134. def test_PolyElement___hash__():
  135. R, x, y, z = ring("x,y,z", QQ)
  136. assert hash(x*y*z)
  137. def test_PolyElement___eq__():
  138. R, x, y = ring("x,y", ZZ, lex)
  139. assert ((x*y + 5*x*y) == 6) == False
  140. assert ((x*y + 5*x*y) == 6*x*y) == True
  141. assert (6 == (x*y + 5*x*y)) == False
  142. assert (6*x*y == (x*y + 5*x*y)) == True
  143. assert ((x*y - x*y) == 0) == True
  144. assert (0 == (x*y - x*y)) == True
  145. assert ((x*y - x*y) == 1) == False
  146. assert (1 == (x*y - x*y)) == False
  147. assert ((x*y - x*y) == 1) == False
  148. assert (1 == (x*y - x*y)) == False
  149. assert ((x*y + 5*x*y) != 6) == True
  150. assert ((x*y + 5*x*y) != 6*x*y) == False
  151. assert (6 != (x*y + 5*x*y)) == True
  152. assert (6*x*y != (x*y + 5*x*y)) == False
  153. assert ((x*y - x*y) != 0) == False
  154. assert (0 != (x*y - x*y)) == False
  155. assert ((x*y - x*y) != 1) == True
  156. assert (1 != (x*y - x*y)) == True
  157. assert R.one == QQ(1, 1) == R.one
  158. assert R.one == 1 == R.one
  159. Rt, t = ring("t", ZZ)
  160. R, x, y = ring("x,y", Rt)
  161. assert (t**3*x/x == t**3) == True
  162. assert (t**3*x/x == t**4) == False
  163. def test_PolyElement__lt_le_gt_ge__():
  164. R, x, y = ring("x,y", ZZ)
  165. assert R(1) < x < x**2 < x**3
  166. assert R(1) <= x <= x**2 <= x**3
  167. assert x**3 > x**2 > x > R(1)
  168. assert x**3 >= x**2 >= x >= R(1)
  169. def test_PolyElement__str__():
  170. x, y = symbols('x, y')
  171. for dom in [ZZ, QQ, ZZ[x], ZZ[x,y], ZZ[x][y]]:
  172. R, t = ring('t', dom)
  173. assert str(2*t**2 + 1) == '2*t**2 + 1'
  174. for dom in [EX, EX[x]]:
  175. R, t = ring('t', dom)
  176. assert str(2*t**2 + 1) == 'EX(2)*t**2 + EX(1)'
  177. def test_PolyElement_copy():
  178. R, x, y, z = ring("x,y,z", ZZ)
  179. f = x*y + 3*z
  180. g = f.copy()
  181. assert f == g
  182. g[(1, 1, 1)] = 7
  183. assert f != g
  184. def test_PolyElement_as_expr():
  185. R, x, y, z = ring("x,y,z", ZZ)
  186. f = 3*x**2*y - x*y*z + 7*z**3 + 1
  187. X, Y, Z = R.symbols
  188. g = 3*X**2*Y - X*Y*Z + 7*Z**3 + 1
  189. assert f != g
  190. assert f.as_expr() == g
  191. U, V, W = symbols("u,v,w")
  192. g = 3*U**2*V - U*V*W + 7*W**3 + 1
  193. assert f != g
  194. assert f.as_expr(U, V, W) == g
  195. raises(ValueError, lambda: f.as_expr(X))
  196. R, = ring("", ZZ)
  197. assert R(3).as_expr() == 3
  198. def test_PolyElement_from_expr():
  199. x, y, z = symbols("x,y,z")
  200. R, X, Y, Z = ring((x, y, z), ZZ)
  201. f = R.from_expr(1)
  202. assert f == 1 and R.is_element(f)
  203. f = R.from_expr(x)
  204. assert f == X and R.is_element(f)
  205. f = R.from_expr(x*y*z)
  206. assert f == X*Y*Z and R.is_element(f)
  207. f = R.from_expr(x*y*z + x*y + x)
  208. assert f == X*Y*Z + X*Y + X and R.is_element(f)
  209. f = R.from_expr(x**3*y*z + x**2*y**7 + 1)
  210. assert f == X**3*Y*Z + X**2*Y**7 + 1 and R.is_element(f)
  211. r, F = sring([exp(2)])
  212. f = r.from_expr(exp(2))
  213. assert f == F[0] and r.is_element(f)
  214. raises(ValueError, lambda: R.from_expr(1/x))
  215. raises(ValueError, lambda: R.from_expr(2**x))
  216. raises(ValueError, lambda: R.from_expr(7*x + sqrt(2)))
  217. R, = ring("", ZZ)
  218. f = R.from_expr(1)
  219. assert f == 1 and R.is_element(f)
  220. def test_PolyElement_degree():
  221. R, x,y,z = ring("x,y,z", ZZ)
  222. assert ninf == float('-inf')
  223. assert R(0).degree() is ninf
  224. assert R(1).degree() == 0
  225. assert (x + 1).degree() == 1
  226. assert (2*y**3 + z).degree() == 0
  227. assert (x*y**3 + z).degree() == 1
  228. assert (x**5*y**3 + z).degree() == 5
  229. assert R(0).degree(x) is ninf
  230. assert R(1).degree(x) == 0
  231. assert (x + 1).degree(x) == 1
  232. assert (2*y**3 + z).degree(x) == 0
  233. assert (x*y**3 + z).degree(x) == 1
  234. assert (7*x**5*y**3 + z).degree(x) == 5
  235. assert R(0).degree(y) is ninf
  236. assert R(1).degree(y) == 0
  237. assert (x + 1).degree(y) == 0
  238. assert (2*y**3 + z).degree(y) == 3
  239. assert (x*y**3 + z).degree(y) == 3
  240. assert (7*x**5*y**3 + z).degree(y) == 3
  241. assert R(0).degree(z) is ninf
  242. assert R(1).degree(z) == 0
  243. assert (x + 1).degree(z) == 0
  244. assert (2*y**3 + z).degree(z) == 1
  245. assert (x*y**3 + z).degree(z) == 1
  246. assert (7*x**5*y**3 + z).degree(z) == 1
  247. R, = ring("", ZZ)
  248. assert R(0).degree() is ninf
  249. assert R(1).degree() == 0
  250. def test_PolyElement_tail_degree():
  251. R, x,y,z = ring("x,y,z", ZZ)
  252. assert R(0).tail_degree() is ninf
  253. assert R(1).tail_degree() == 0
  254. assert (x + 1).tail_degree() == 0
  255. assert (2*y**3 + x**3*z).tail_degree() == 0
  256. assert (x*y**3 + x**3*z).tail_degree() == 1
  257. assert (x**5*y**3 + x**3*z).tail_degree() == 3
  258. assert R(0).tail_degree(x) is ninf
  259. assert R(1).tail_degree(x) == 0
  260. assert (x + 1).tail_degree(x) == 0
  261. assert (2*y**3 + x**3*z).tail_degree(x) == 0
  262. assert (x*y**3 + x**3*z).tail_degree(x) == 1
  263. assert (7*x**5*y**3 + x**3*z).tail_degree(x) == 3
  264. assert R(0).tail_degree(y) is ninf
  265. assert R(1).tail_degree(y) == 0
  266. assert (x + 1).tail_degree(y) == 0
  267. assert (2*y**3 + x**3*z).tail_degree(y) == 0
  268. assert (x*y**3 + x**3*z).tail_degree(y) == 0
  269. assert (7*x**5*y**3 + x**3*z).tail_degree(y) == 0
  270. assert R(0).tail_degree(z) is ninf
  271. assert R(1).tail_degree(z) == 0
  272. assert (x + 1).tail_degree(z) == 0
  273. assert (2*y**3 + x**3*z).tail_degree(z) == 0
  274. assert (x*y**3 + x**3*z).tail_degree(z) == 0
  275. assert (7*x**5*y**3 + x**3*z).tail_degree(z) == 0
  276. R, = ring("", ZZ)
  277. assert R(0).tail_degree() is ninf
  278. assert R(1).tail_degree() == 0
  279. def test_PolyElement_degrees():
  280. R, x,y,z = ring("x,y,z", ZZ)
  281. assert R(0).degrees() == (ninf, ninf, ninf)
  282. assert R(1).degrees() == (0, 0, 0)
  283. assert (x**2*y + x**3*z**2).degrees() == (3, 1, 2)
  284. def test_PolyElement_tail_degrees():
  285. R, x,y,z = ring("x,y,z", ZZ)
  286. assert R(0).tail_degrees() == (ninf, ninf, ninf)
  287. assert R(1).tail_degrees() == (0, 0, 0)
  288. assert (x**2*y + x**3*z**2).tail_degrees() == (2, 0, 0)
  289. def test_PolyElement_coeff():
  290. R, x, y, z = ring("x,y,z", ZZ, lex)
  291. f = 3*x**2*y - x*y*z + 7*z**3 + 23
  292. assert f.coeff(1) == 23
  293. raises(ValueError, lambda: f.coeff(3))
  294. assert f.coeff(x) == 0
  295. assert f.coeff(y) == 0
  296. assert f.coeff(z) == 0
  297. assert f.coeff(x**2*y) == 3
  298. assert f.coeff(x*y*z) == -1
  299. assert f.coeff(z**3) == 7
  300. raises(ValueError, lambda: f.coeff(3*x**2*y))
  301. raises(ValueError, lambda: f.coeff(-x*y*z))
  302. raises(ValueError, lambda: f.coeff(7*z**3))
  303. R, = ring("", ZZ)
  304. assert R(3).coeff(1) == 3
  305. def test_PolyElement_LC():
  306. R, x, y = ring("x,y", QQ, lex)
  307. assert R(0).LC == QQ(0)
  308. assert (QQ(1,2)*x).LC == QQ(1, 2)
  309. assert (QQ(1,4)*x*y + QQ(1,2)*x).LC == QQ(1, 4)
  310. def test_PolyElement_LM():
  311. R, x, y = ring("x,y", QQ, lex)
  312. assert R(0).LM == (0, 0)
  313. assert (QQ(1,2)*x).LM == (1, 0)
  314. assert (QQ(1,4)*x*y + QQ(1,2)*x).LM == (1, 1)
  315. def test_PolyElement_LT():
  316. R, x, y = ring("x,y", QQ, lex)
  317. assert R(0).LT == ((0, 0), QQ(0))
  318. assert (QQ(1,2)*x).LT == ((1, 0), QQ(1, 2))
  319. assert (QQ(1,4)*x*y + QQ(1,2)*x).LT == ((1, 1), QQ(1, 4))
  320. R, = ring("", ZZ)
  321. assert R(0).LT == ((), 0)
  322. assert R(1).LT == ((), 1)
  323. def test_PolyElement_leading_monom():
  324. R, x, y = ring("x,y", QQ, lex)
  325. assert R(0).leading_monom() == 0
  326. assert (QQ(1,2)*x).leading_monom() == x
  327. assert (QQ(1,4)*x*y + QQ(1,2)*x).leading_monom() == x*y
  328. def test_PolyElement_leading_term():
  329. R, x, y = ring("x,y", QQ, lex)
  330. assert R(0).leading_term() == 0
  331. assert (QQ(1,2)*x).leading_term() == QQ(1,2)*x
  332. assert (QQ(1,4)*x*y + QQ(1,2)*x).leading_term() == QQ(1,4)*x*y
  333. def test_PolyElement_terms():
  334. R, x,y,z = ring("x,y,z", QQ)
  335. terms = (x**2/3 + y**3/4 + z**4/5).terms()
  336. assert terms == [((2,0,0), QQ(1,3)), ((0,3,0), QQ(1,4)), ((0,0,4), QQ(1,5))]
  337. R, x,y = ring("x,y", ZZ, lex)
  338. f = x*y**7 + 2*x**2*y**3
  339. assert f.terms() == f.terms(lex) == f.terms('lex') == [((2, 3), 2), ((1, 7), 1)]
  340. assert f.terms(grlex) == f.terms('grlex') == [((1, 7), 1), ((2, 3), 2)]
  341. R, x,y = ring("x,y", ZZ, grlex)
  342. f = x*y**7 + 2*x**2*y**3
  343. assert f.terms() == f.terms(grlex) == f.terms('grlex') == [((1, 7), 1), ((2, 3), 2)]
  344. assert f.terms(lex) == f.terms('lex') == [((2, 3), 2), ((1, 7), 1)]
  345. R, = ring("", ZZ)
  346. assert R(3).terms() == [((), 3)]
  347. def test_PolyElement_monoms():
  348. R, x,y,z = ring("x,y,z", QQ)
  349. monoms = (x**2/3 + y**3/4 + z**4/5).monoms()
  350. assert monoms == [(2,0,0), (0,3,0), (0,0,4)]
  351. R, x,y = ring("x,y", ZZ, lex)
  352. f = x*y**7 + 2*x**2*y**3
  353. assert f.monoms() == f.monoms(lex) == f.monoms('lex') == [(2, 3), (1, 7)]
  354. assert f.monoms(grlex) == f.monoms('grlex') == [(1, 7), (2, 3)]
  355. R, x,y = ring("x,y", ZZ, grlex)
  356. f = x*y**7 + 2*x**2*y**3
  357. assert f.monoms() == f.monoms(grlex) == f.monoms('grlex') == [(1, 7), (2, 3)]
  358. assert f.monoms(lex) == f.monoms('lex') == [(2, 3), (1, 7)]
  359. def test_PolyElement_coeffs():
  360. R, x,y,z = ring("x,y,z", QQ)
  361. coeffs = (x**2/3 + y**3/4 + z**4/5).coeffs()
  362. assert coeffs == [QQ(1,3), QQ(1,4), QQ(1,5)]
  363. R, x,y = ring("x,y", ZZ, lex)
  364. f = x*y**7 + 2*x**2*y**3
  365. assert f.coeffs() == f.coeffs(lex) == f.coeffs('lex') == [2, 1]
  366. assert f.coeffs(grlex) == f.coeffs('grlex') == [1, 2]
  367. R, x,y = ring("x,y", ZZ, grlex)
  368. f = x*y**7 + 2*x**2*y**3
  369. assert f.coeffs() == f.coeffs(grlex) == f.coeffs('grlex') == [1, 2]
  370. assert f.coeffs(lex) == f.coeffs('lex') == [2, 1]
  371. def test_PolyElement___add__():
  372. Rt, t = ring("t", ZZ)
  373. Ruv, u,v = ring("u,v", ZZ)
  374. Rxyz, x,y,z = ring("x,y,z", Ruv)
  375. assert dict(x + 3*y) == {(1, 0, 0): 1, (0, 1, 0): 3}
  376. assert dict(u + x) == dict(x + u) == {(1, 0, 0): 1, (0, 0, 0): u}
  377. assert dict(u + x*y) == dict(x*y + u) == {(1, 1, 0): 1, (0, 0, 0): u}
  378. assert dict(u + x*y + z) == dict(x*y + z + u) == {(1, 1, 0): 1, (0, 0, 1): 1, (0, 0, 0): u}
  379. assert dict(u*x + x) == dict(x + u*x) == {(1, 0, 0): u + 1}
  380. assert dict(u*x + x*y) == dict(x*y + u*x) == {(1, 1, 0): 1, (1, 0, 0): u}
  381. assert dict(u*x + x*y + z) == dict(x*y + z + u*x) == {(1, 1, 0): 1, (0, 0, 1): 1, (1, 0, 0): u}
  382. raises(TypeError, lambda: t + x)
  383. raises(TypeError, lambda: x + t)
  384. raises(TypeError, lambda: t + u)
  385. raises(TypeError, lambda: u + t)
  386. Fuv, u,v = field("u,v", ZZ)
  387. Rxyz, x,y,z = ring("x,y,z", Fuv)
  388. assert dict(u + x) == dict(x + u) == {(1, 0, 0): 1, (0, 0, 0): u}
  389. Rxyz, x,y,z = ring("x,y,z", EX)
  390. assert dict(EX(pi) + x*y*z) == dict(x*y*z + EX(pi)) == {(1, 1, 1): EX(1), (0, 0, 0): EX(pi)}
  391. def test_PolyElement___sub__():
  392. Rt, t = ring("t", ZZ)
  393. Ruv, u,v = ring("u,v", ZZ)
  394. Rxyz, x,y,z = ring("x,y,z", Ruv)
  395. assert dict(x - 3*y) == {(1, 0, 0): 1, (0, 1, 0): -3}
  396. assert dict(-u + x) == dict(x - u) == {(1, 0, 0): 1, (0, 0, 0): -u}
  397. assert dict(-u + x*y) == dict(x*y - u) == {(1, 1, 0): 1, (0, 0, 0): -u}
  398. assert dict(-u + x*y + z) == dict(x*y + z - u) == {(1, 1, 0): 1, (0, 0, 1): 1, (0, 0, 0): -u}
  399. assert dict(-u*x + x) == dict(x - u*x) == {(1, 0, 0): -u + 1}
  400. assert dict(-u*x + x*y) == dict(x*y - u*x) == {(1, 1, 0): 1, (1, 0, 0): -u}
  401. assert dict(-u*x + x*y + z) == dict(x*y + z - u*x) == {(1, 1, 0): 1, (0, 0, 1): 1, (1, 0, 0): -u}
  402. raises(TypeError, lambda: t - x)
  403. raises(TypeError, lambda: x - t)
  404. raises(TypeError, lambda: t - u)
  405. raises(TypeError, lambda: u - t)
  406. Fuv, u,v = field("u,v", ZZ)
  407. Rxyz, x,y,z = ring("x,y,z", Fuv)
  408. assert dict(-u + x) == dict(x - u) == {(1, 0, 0): 1, (0, 0, 0): -u}
  409. Rxyz, x,y,z = ring("x,y,z", EX)
  410. assert dict(-EX(pi) + x*y*z) == dict(x*y*z - EX(pi)) == {(1, 1, 1): EX(1), (0, 0, 0): -EX(pi)}
  411. def test_PolyElement___mul__():
  412. Rt, t = ring("t", ZZ)
  413. Ruv, u,v = ring("u,v", ZZ)
  414. Rxyz, x,y,z = ring("x,y,z", Ruv)
  415. assert dict(u*x) == dict(x*u) == {(1, 0, 0): u}
  416. assert dict(2*u*x + z) == dict(x*2*u + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}
  417. assert dict(u*2*x + z) == dict(2*x*u + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}
  418. assert dict(2*u*x + z) == dict(x*2*u + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}
  419. assert dict(u*x*2 + z) == dict(x*u*2 + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}
  420. assert dict(2*u*x*y + z) == dict(x*y*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
  421. assert dict(u*2*x*y + z) == dict(2*x*y*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
  422. assert dict(2*u*x*y + z) == dict(x*y*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
  423. assert dict(u*x*y*2 + z) == dict(x*y*u*2 + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
  424. assert dict(2*u*y*x + z) == dict(y*x*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
  425. assert dict(u*2*y*x + z) == dict(2*y*x*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
  426. assert dict(2*u*y*x + z) == dict(y*x*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
  427. assert dict(u*y*x*2 + z) == dict(y*x*u*2 + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
  428. assert dict(3*u*(x + y) + z) == dict((x + y)*3*u + z) == {(1, 0, 0): 3*u, (0, 1, 0): 3*u, (0, 0, 1): 1}
  429. raises(TypeError, lambda: t*x + z)
  430. raises(TypeError, lambda: x*t + z)
  431. raises(TypeError, lambda: t*u + z)
  432. raises(TypeError, lambda: u*t + z)
  433. Fuv, u,v = field("u,v", ZZ)
  434. Rxyz, x,y,z = ring("x,y,z", Fuv)
  435. assert dict(u*x) == dict(x*u) == {(1, 0, 0): u}
  436. Rxyz, x,y,z = ring("x,y,z", EX)
  437. assert dict(EX(pi)*x*y*z) == dict(x*y*z*EX(pi)) == {(1, 1, 1): EX(pi)}
  438. def test_PolyElement___truediv__():
  439. R, x,y,z = ring("x,y,z", ZZ)
  440. assert (2*x**2 - 4)/2 == x**2 - 2
  441. assert (2*x**2 - 3)/2 == x**2
  442. assert (x**2 - 1).quo(x) == x
  443. assert (x**2 - x).quo(x) == x - 1
  444. raises(ExactQuotientFailed, lambda: (x**2 - 1)/x)
  445. assert (x**2 - x)/x == x - 1
  446. raises(ExactQuotientFailed, lambda: (x**2 - 1)/(2*x))
  447. assert (x**2 - 1).quo(2*x) == 0
  448. assert (x**2 - x)/(x - 1) == (x**2 - x).quo(x - 1) == x
  449. R, x,y,z = ring("x,y,z", ZZ)
  450. assert len((x**2/3 + y**3/4 + z**4/5).terms()) == 0
  451. R, x,y,z = ring("x,y,z", QQ)
  452. assert len((x**2/3 + y**3/4 + z**4/5).terms()) == 3
  453. Rt, t = ring("t", ZZ)
  454. Ruv, u,v = ring("u,v", ZZ)
  455. Rxyz, x,y,z = ring("x,y,z", Ruv)
  456. assert dict((u**2*x + u)/u) == {(1, 0, 0): u, (0, 0, 0): 1}
  457. raises(ExactQuotientFailed, lambda: u/(u**2*x + u))
  458. raises(TypeError, lambda: t/x)
  459. raises(TypeError, lambda: x/t)
  460. raises(TypeError, lambda: t/u)
  461. raises(TypeError, lambda: u/t)
  462. R, x = ring("x", ZZ)
  463. f, g = x**2 + 2*x + 3, R(0)
  464. raises(ZeroDivisionError, lambda: f.div(g))
  465. raises(ZeroDivisionError, lambda: divmod(f, g))
  466. raises(ZeroDivisionError, lambda: f.rem(g))
  467. raises(ZeroDivisionError, lambda: f % g)
  468. raises(ZeroDivisionError, lambda: f.quo(g))
  469. raises(ZeroDivisionError, lambda: f / g)
  470. raises(ZeroDivisionError, lambda: f.exquo(g))
  471. R, x, y = ring("x,y", ZZ)
  472. f, g = x*y + 2*x + 3, R(0)
  473. raises(ZeroDivisionError, lambda: f.div(g))
  474. raises(ZeroDivisionError, lambda: divmod(f, g))
  475. raises(ZeroDivisionError, lambda: f.rem(g))
  476. raises(ZeroDivisionError, lambda: f % g)
  477. raises(ZeroDivisionError, lambda: f.quo(g))
  478. raises(ZeroDivisionError, lambda: f / g)
  479. raises(ZeroDivisionError, lambda: f.exquo(g))
  480. R, x = ring("x", ZZ)
  481. f, g = x**2 + 1, 2*x - 4
  482. q, r = R(0), x**2 + 1
  483. assert f.div(g) == divmod(f, g) == (q, r)
  484. assert f.rem(g) == f % g == r
  485. assert f.quo(g) == q
  486. raises(ExactQuotientFailed, lambda: f / g)
  487. raises(ExactQuotientFailed, lambda: f.exquo(g))
  488. f, g = 3*x**3 + x**2 + x + 5, 5*x**2 - 3*x + 1
  489. q, r = R(0), f
  490. assert f.div(g) == divmod(f, g) == (q, r)
  491. assert f.rem(g) == f % g == r
  492. assert f.quo(g) == q
  493. raises(ExactQuotientFailed, lambda: f / g)
  494. raises(ExactQuotientFailed, lambda: f.exquo(g))
  495. f, g = 5*x**4 + 4*x**3 + 3*x**2 + 2*x + 1, x**2 + 2*x + 3
  496. q, r = 5*x**2 - 6*x, 20*x + 1
  497. assert f.div(g) == divmod(f, g) == (q, r)
  498. assert f.rem(g) == f % g == r
  499. assert f.quo(g) == q
  500. raises(ExactQuotientFailed, lambda: f / g)
  501. raises(ExactQuotientFailed, lambda: f.exquo(g))
  502. f, g = 5*x**5 + 4*x**4 + 3*x**3 + 2*x**2 + x, x**4 + 2*x**3 + 9
  503. q, r = 5*x - 6, 15*x**3 + 2*x**2 - 44*x + 54
  504. assert f.div(g) == divmod(f, g) == (q, r)
  505. assert f.rem(g) == f % g == r
  506. assert f.quo(g) == q
  507. raises(ExactQuotientFailed, lambda: f / g)
  508. raises(ExactQuotientFailed, lambda: f.exquo(g))
  509. R, x = ring("x", QQ)
  510. f, g = x**2 + 1, 2*x - 4
  511. q, r = x/2 + 1, R(5)
  512. assert f.div(g) == divmod(f, g) == (q, r)
  513. assert f.rem(g) == f % g == r
  514. assert f.quo(g) == q
  515. raises(ExactQuotientFailed, lambda: f / g)
  516. raises(ExactQuotientFailed, lambda: f.exquo(g))
  517. f, g = 3*x**3 + x**2 + x + 5, 5*x**2 - 3*x + 1
  518. q, r = QQ(3, 5)*x + QQ(14, 25), QQ(52, 25)*x + QQ(111, 25)
  519. assert f.div(g) == divmod(f, g) == (q, r)
  520. assert f.rem(g) == f % g == r
  521. assert f.quo(g) == q
  522. raises(ExactQuotientFailed, lambda: f / g)
  523. raises(ExactQuotientFailed, lambda: f.exquo(g))
  524. R, x,y = ring("x,y", ZZ)
  525. f, g = x**2 - y**2, x - y
  526. q, r = x + y, R(0)
  527. assert f.div(g) == divmod(f, g) == (q, r)
  528. assert f.rem(g) == f % g == r
  529. assert f.quo(g) == q
  530. assert f.exquo(g) == f / g == q
  531. f, g = x**2 + y**2, x - y
  532. q, r = x + y, 2*y**2
  533. assert f.div(g) == divmod(f, g) == (q, r)
  534. assert f.rem(g) == f % g == r
  535. assert f.quo(g) == q
  536. raises(ExactQuotientFailed, lambda: f / g)
  537. raises(ExactQuotientFailed, lambda: f.exquo(g))
  538. f, g = x**2 + y**2, -x + y
  539. q, r = -x - y, 2*y**2
  540. assert f.div(g) == divmod(f, g) == (q, r)
  541. assert f.rem(g) == f % g == r
  542. assert f.quo(g) == q
  543. raises(ExactQuotientFailed, lambda: f / g)
  544. raises(ExactQuotientFailed, lambda: f.exquo(g))
  545. f, g = x**2 + y**2, 2*x - 2*y
  546. q, r = R(0), f
  547. assert f.div(g) == divmod(f, g) == (q, r)
  548. assert f.rem(g) == f % g == r
  549. assert f.quo(g) == q
  550. raises(ExactQuotientFailed, lambda: f / g)
  551. raises(ExactQuotientFailed, lambda: f.exquo(g))
  552. R, x,y = ring("x,y", QQ)
  553. f, g = x**2 - y**2, x - y
  554. q, r = x + y, R(0)
  555. assert f.div(g) == divmod(f, g) == (q, r)
  556. assert f.rem(g) == f % g == r
  557. assert f.quo(g) == q
  558. assert f.exquo(g) == f / g == q
  559. f, g = x**2 + y**2, x - y
  560. q, r = x + y, 2*y**2
  561. assert f.div(g) == divmod(f, g) == (q, r)
  562. assert f.rem(g) == f % g == r
  563. assert f.quo(g) == q
  564. raises(ExactQuotientFailed, lambda: f / g)
  565. raises(ExactQuotientFailed, lambda: f.exquo(g))
  566. f, g = x**2 + y**2, -x + y
  567. q, r = -x - y, 2*y**2
  568. assert f.div(g) == divmod(f, g) == (q, r)
  569. assert f.rem(g) == f % g == r
  570. assert f.quo(g) == q
  571. raises(ExactQuotientFailed, lambda: f / g)
  572. raises(ExactQuotientFailed, lambda: f.exquo(g))
  573. f, g = x**2 + y**2, 2*x - 2*y
  574. q, r = x/2 + y/2, 2*y**2
  575. assert f.div(g) == divmod(f, g) == (q, r)
  576. assert f.rem(g) == f % g == r
  577. assert f.quo(g) == q
  578. raises(ExactQuotientFailed, lambda: f / g)
  579. raises(ExactQuotientFailed, lambda: f.exquo(g))
  580. def test_PolyElement___pow__():
  581. R, x = ring("x", ZZ, grlex)
  582. f = 2*x + 3
  583. assert f**0 == 1
  584. assert f**1 == f
  585. raises(ValueError, lambda: f**(-1))
  586. assert f**2 == f._pow_generic(2) == f._pow_multinomial(2) == 4*x**2 + 12*x + 9
  587. assert f**3 == f._pow_generic(3) == f._pow_multinomial(3) == 8*x**3 + 36*x**2 + 54*x + 27
  588. assert f**4 == f._pow_generic(4) == f._pow_multinomial(4) == 16*x**4 + 96*x**3 + 216*x**2 + 216*x + 81
  589. assert f**5 == f._pow_generic(5) == f._pow_multinomial(5) == 32*x**5 + 240*x**4 + 720*x**3 + 1080*x**2 + 810*x + 243
  590. R, x,y,z = ring("x,y,z", ZZ, grlex)
  591. f = x**3*y - 2*x*y**2 - 3*z + 1
  592. g = x**6*y**2 - 4*x**4*y**3 - 6*x**3*y*z + 2*x**3*y + 4*x**2*y**4 + 12*x*y**2*z - 4*x*y**2 + 9*z**2 - 6*z + 1
  593. assert f**2 == f._pow_generic(2) == f._pow_multinomial(2) == g
  594. R, t = ring("t", ZZ)
  595. f = -11200*t**4 - 2604*t**2 + 49
  596. g = 15735193600000000*t**16 + 14633730048000000*t**14 + 4828147466240000*t**12 \
  597. + 598976863027200*t**10 + 3130812416256*t**8 - 2620523775744*t**6 \
  598. + 92413760096*t**4 - 1225431984*t**2 + 5764801
  599. assert f**4 == f._pow_generic(4) == f._pow_multinomial(4) == g
  600. def test_PolyElement_div():
  601. R, x = ring("x", ZZ, grlex)
  602. f = x**3 - 12*x**2 - 42
  603. g = x - 3
  604. q = x**2 - 9*x - 27
  605. r = -123
  606. assert f.div([g]) == ([q], r)
  607. R, x = ring("x", ZZ, grlex)
  608. f = x**2 + 2*x + 2
  609. assert f.div([R(1)]) == ([f], 0)
  610. R, x = ring("x", QQ, grlex)
  611. f = x**2 + 2*x + 2
  612. assert f.div([R(2)]) == ([QQ(1,2)*x**2 + x + 1], 0)
  613. R, x,y = ring("x,y", ZZ, grlex)
  614. f = 4*x**2*y - 2*x*y + 4*x - 2*y + 8
  615. assert f.div([R(2)]) == ([2*x**2*y - x*y + 2*x - y + 4], 0)
  616. assert f.div([2*y]) == ([2*x**2 - x - 1], 4*x + 8)
  617. f = x - 1
  618. g = y - 1
  619. assert f.div([g]) == ([0], f)
  620. f = x*y**2 + 1
  621. G = [x*y + 1, y + 1]
  622. Q = [y, -1]
  623. r = 2
  624. assert f.div(G) == (Q, r)
  625. f = x**2*y + x*y**2 + y**2
  626. G = [x*y - 1, y**2 - 1]
  627. Q = [x + y, 1]
  628. r = x + y + 1
  629. assert f.div(G) == (Q, r)
  630. G = [y**2 - 1, x*y - 1]
  631. Q = [x + 1, x]
  632. r = 2*x + 1
  633. assert f.div(G) == (Q, r)
  634. R, = ring("", ZZ)
  635. assert R(3).div(R(2)) == (0, 3)
  636. R, = ring("", QQ)
  637. assert R(3).div(R(2)) == (QQ(3, 2), 0)
  638. def test_PolyElement_rem():
  639. R, x = ring("x", ZZ, grlex)
  640. f = x**3 - 12*x**2 - 42
  641. g = x - 3
  642. r = -123
  643. assert f.rem([g]) == f.div([g])[1] == r
  644. R, x,y = ring("x,y", ZZ, grlex)
  645. f = 4*x**2*y - 2*x*y + 4*x - 2*y + 8
  646. assert f.rem([R(2)]) == f.div([R(2)])[1] == 0
  647. assert f.rem([2*y]) == f.div([2*y])[1] == 4*x + 8
  648. f = x - 1
  649. g = y - 1
  650. assert f.rem([g]) == f.div([g])[1] == f
  651. f = x*y**2 + 1
  652. G = [x*y + 1, y + 1]
  653. r = 2
  654. assert f.rem(G) == f.div(G)[1] == r
  655. f = x**2*y + x*y**2 + y**2
  656. G = [x*y - 1, y**2 - 1]
  657. r = x + y + 1
  658. assert f.rem(G) == f.div(G)[1] == r
  659. G = [y**2 - 1, x*y - 1]
  660. r = 2*x + 1
  661. assert f.rem(G) == f.div(G)[1] == r
  662. def test_PolyElement_deflate():
  663. R, x = ring("x", ZZ)
  664. assert (2*x**2).deflate(x**4 + 4*x**2 + 1) == ((2,), [2*x, x**2 + 4*x + 1])
  665. R, x,y = ring("x,y", ZZ)
  666. assert R(0).deflate(R(0)) == ((1, 1), [0, 0])
  667. assert R(1).deflate(R(0)) == ((1, 1), [1, 0])
  668. assert R(1).deflate(R(2)) == ((1, 1), [1, 2])
  669. assert R(1).deflate(2*y) == ((1, 1), [1, 2*y])
  670. assert (2*y).deflate(2*y) == ((1, 1), [2*y, 2*y])
  671. assert R(2).deflate(2*y**2) == ((1, 2), [2, 2*y])
  672. assert (2*y**2).deflate(2*y**2) == ((1, 2), [2*y, 2*y])
  673. f = x**4*y**2 + x**2*y + 1
  674. g = x**2*y**3 + x**2*y + 1
  675. assert f.deflate(g) == ((2, 1), [x**2*y**2 + x*y + 1, x*y**3 + x*y + 1])
  676. def test_PolyElement_clear_denoms():
  677. R, x,y = ring("x,y", QQ)
  678. assert R(1).clear_denoms() == (ZZ(1), 1)
  679. assert R(7).clear_denoms() == (ZZ(1), 7)
  680. assert R(QQ(7,3)).clear_denoms() == (3, 7)
  681. assert R(QQ(7,3)).clear_denoms() == (3, 7)
  682. assert (3*x**2 + x).clear_denoms() == (1, 3*x**2 + x)
  683. assert (x**2 + QQ(1,2)*x).clear_denoms() == (2, 2*x**2 + x)
  684. rQQ, x,t = ring("x,t", QQ, lex)
  685. rZZ, X,T = ring("x,t", ZZ, lex)
  686. F = [x - QQ(17824537287975195925064602467992950991718052713078834557692023531499318507213727406844943097,413954288007559433755329699713866804710749652268151059918115348815925474842910720000)*t**7
  687. - QQ(4882321164854282623427463828745855894130208215961904469205260756604820743234704900167747753,12936071500236232304854053116058337647210926633379720622441104650497671088840960000)*t**6
  688. - QQ(36398103304520066098365558157422127347455927422509913596393052633155821154626830576085097433,25872143000472464609708106232116675294421853266759441244882209300995342177681920000)*t**5
  689. - QQ(168108082231614049052707339295479262031324376786405372698857619250210703675982492356828810819,58212321751063045371843239022262519412449169850208742800984970927239519899784320000)*t**4
  690. - QQ(5694176899498574510667890423110567593477487855183144378347226247962949388653159751849449037,1617008937529529038106756639507292205901365829172465077805138081312208886105120000)*t**3
  691. - QQ(154482622347268833757819824809033388503591365487934245386958884099214649755244381307907779,60637835157357338929003373981523457721301218593967440417692678049207833228942000)*t**2
  692. - QQ(2452813096069528207645703151222478123259511586701148682951852876484544822947007791153163,2425513406294293557160134959260938308852048743758697616707707121968313329157680)*t
  693. - QQ(34305265428126440542854669008203683099323146152358231964773310260498715579162112959703,202126117191191129763344579938411525737670728646558134725642260164026110763140),
  694. t**8 + QQ(693749860237914515552,67859264524169150569)*t**7
  695. + QQ(27761407182086143225024,610733380717522355121)*t**6
  696. + QQ(7785127652157884044288,67859264524169150569)*t**5
  697. + QQ(36567075214771261409792,203577793572507451707)*t**4
  698. + QQ(36336335165196147384320,203577793572507451707)*t**3
  699. + QQ(7452455676042754048000,67859264524169150569)*t**2
  700. + QQ(2593331082514399232000,67859264524169150569)*t
  701. + QQ(390399197427343360000,67859264524169150569)]
  702. G = [3725588592068034903797967297424801242396746870413359539263038139343329273586196480000*X -
  703. 160420835591776763325581422211936558925462474417709511019228211783493866564923546661604487873*T**7 -
  704. 1406108495478033395547109582678806497509499966197028487131115097902188374051595011248311352864*T**6 -
  705. 5241326875850889518164640374668786338033653548841427557880599579174438246266263602956254030352*T**5 -
  706. 10758917262823299139373269714910672770004760114329943852726887632013485035262879510837043892416*T**4 -
  707. 13119383576444715672578819534846747735372132018341964647712009275306635391456880068261130581248*T**3 -
  708. 9491412317016197146080450036267011389660653495578680036574753839055748080962214787557853941760*T**2 -
  709. 3767520915562795326943800040277726397326609797172964377014046018280260848046603967211258368000*T -
  710. 632314652371226552085897259159210286886724229880266931574701654721512325555116066073245696000,
  711. 610733380717522355121*T**8 +
  712. 6243748742141230639968*T**7 +
  713. 27761407182086143225024*T**6 +
  714. 70066148869420956398592*T**5 +
  715. 109701225644313784229376*T**4 +
  716. 109009005495588442152960*T**3 +
  717. 67072101084384786432000*T**2 +
  718. 23339979742629593088000*T +
  719. 3513592776846090240000]
  720. assert [ f.clear_denoms()[1].set_ring(rZZ) for f in F ] == G
  721. def test_PolyElement_cofactors():
  722. R, x, y = ring("x,y", ZZ)
  723. f, g = R(0), R(0)
  724. assert f.cofactors(g) == (0, 0, 0)
  725. f, g = R(2), R(0)
  726. assert f.cofactors(g) == (2, 1, 0)
  727. f, g = R(-2), R(0)
  728. assert f.cofactors(g) == (2, -1, 0)
  729. f, g = R(0), R(-2)
  730. assert f.cofactors(g) == (2, 0, -1)
  731. f, g = R(0), 2*x + 4
  732. assert f.cofactors(g) == (2*x + 4, 0, 1)
  733. f, g = 2*x + 4, R(0)
  734. assert f.cofactors(g) == (2*x + 4, 1, 0)
  735. f, g = R(2), R(2)
  736. assert f.cofactors(g) == (2, 1, 1)
  737. f, g = R(-2), R(2)
  738. assert f.cofactors(g) == (2, -1, 1)
  739. f, g = R(2), R(-2)
  740. assert f.cofactors(g) == (2, 1, -1)
  741. f, g = R(-2), R(-2)
  742. assert f.cofactors(g) == (2, -1, -1)
  743. f, g = x**2 + 2*x + 1, R(1)
  744. assert f.cofactors(g) == (1, x**2 + 2*x + 1, 1)
  745. f, g = x**2 + 2*x + 1, R(2)
  746. assert f.cofactors(g) == (1, x**2 + 2*x + 1, 2)
  747. f, g = 2*x**2 + 4*x + 2, R(2)
  748. assert f.cofactors(g) == (2, x**2 + 2*x + 1, 1)
  749. f, g = R(2), 2*x**2 + 4*x + 2
  750. assert f.cofactors(g) == (2, 1, x**2 + 2*x + 1)
  751. f, g = 2*x**2 + 4*x + 2, x + 1
  752. assert f.cofactors(g) == (x + 1, 2*x + 2, 1)
  753. f, g = x + 1, 2*x**2 + 4*x + 2
  754. assert f.cofactors(g) == (x + 1, 1, 2*x + 2)
  755. R, x, y, z, t = ring("x,y,z,t", ZZ)
  756. f, g = t**2 + 2*t + 1, 2*t + 2
  757. assert f.cofactors(g) == (t + 1, t + 1, 2)
  758. f, g = z**2*t**2 + 2*z**2*t + z**2 + z*t + z, t**2 + 2*t + 1
  759. h, cff, cfg = t + 1, z**2*t + z**2 + z, t + 1
  760. assert f.cofactors(g) == (h, cff, cfg)
  761. assert g.cofactors(f) == (h, cfg, cff)
  762. R, x, y = ring("x,y", QQ)
  763. f = QQ(1,2)*x**2 + x + QQ(1,2)
  764. g = QQ(1,2)*x + QQ(1,2)
  765. h = x + 1
  766. assert f.cofactors(g) == (h, g, QQ(1,2))
  767. assert g.cofactors(f) == (h, QQ(1,2), g)
  768. R, x, y = ring("x,y", RR)
  769. f = 2.1*x*y**2 - 2.1*x*y + 2.1*x
  770. g = 2.1*x**3
  771. h = 1.0*x
  772. assert f.cofactors(g) == (h, f/h, g/h)
  773. assert g.cofactors(f) == (h, g/h, f/h)
  774. def test_PolyElement_gcd():
  775. R, x, y = ring("x,y", QQ)
  776. f = QQ(1,2)*x**2 + x + QQ(1,2)
  777. g = QQ(1,2)*x + QQ(1,2)
  778. assert f.gcd(g) == x + 1
  779. def test_PolyElement_cancel():
  780. R, x, y = ring("x,y", ZZ)
  781. f = 2*x**3 + 4*x**2 + 2*x
  782. g = 3*x**2 + 3*x
  783. F = 2*x + 2
  784. G = 3
  785. assert f.cancel(g) == (F, G)
  786. assert (-f).cancel(g) == (-F, G)
  787. assert f.cancel(-g) == (-F, G)
  788. R, x, y = ring("x,y", QQ)
  789. f = QQ(1,2)*x**3 + x**2 + QQ(1,2)*x
  790. g = QQ(1,3)*x**2 + QQ(1,3)*x
  791. F = 3*x + 3
  792. G = 2
  793. assert f.cancel(g) == (F, G)
  794. assert (-f).cancel(g) == (-F, G)
  795. assert f.cancel(-g) == (-F, G)
  796. Fx, x = field("x", ZZ)
  797. Rt, t = ring("t", Fx)
  798. f = (-x**2 - 4)/4*t
  799. g = t**2 + (x**2 + 2)/2
  800. assert f.cancel(g) == ((-x**2 - 4)*t, 4*t**2 + 2*x**2 + 4)
  801. def test_PolyElement_max_norm():
  802. R, x, y = ring("x,y", ZZ)
  803. assert R(0).max_norm() == 0
  804. assert R(1).max_norm() == 1
  805. assert (x**3 + 4*x**2 + 2*x + 3).max_norm() == 4
  806. def test_PolyElement_l1_norm():
  807. R, x, y = ring("x,y", ZZ)
  808. assert R(0).l1_norm() == 0
  809. assert R(1).l1_norm() == 1
  810. assert (x**3 + 4*x**2 + 2*x + 3).l1_norm() == 10
  811. def test_PolyElement_diff():
  812. R, X = xring("x:11", QQ)
  813. f = QQ(288,5)*X[0]**8*X[1]**6*X[4]**3*X[10]**2 + 8*X[0]**2*X[2]**3*X[4]**3 +2*X[0]**2 - 2*X[1]**2
  814. assert f.diff(X[0]) == QQ(2304,5)*X[0]**7*X[1]**6*X[4]**3*X[10]**2 + 16*X[0]*X[2]**3*X[4]**3 + 4*X[0]
  815. assert f.diff(X[4]) == QQ(864,5)*X[0]**8*X[1]**6*X[4]**2*X[10]**2 + 24*X[0]**2*X[2]**3*X[4]**2
  816. assert f.diff(X[10]) == QQ(576,5)*X[0]**8*X[1]**6*X[4]**3*X[10]
  817. def test_PolyElement___call__():
  818. R, x = ring("x", ZZ)
  819. f = 3*x + 1
  820. assert f(0) == 1
  821. assert f(1) == 4
  822. raises(ValueError, lambda: f())
  823. raises(ValueError, lambda: f(0, 1))
  824. raises(CoercionFailed, lambda: f(QQ(1,7)))
  825. R, x,y = ring("x,y", ZZ)
  826. f = 3*x + y**2 + 1
  827. assert f(0, 0) == 1
  828. assert f(1, 7) == 53
  829. Ry = R.drop(x)
  830. assert f(0) == Ry.y**2 + 1
  831. assert f(1) == Ry.y**2 + 4
  832. raises(ValueError, lambda: f())
  833. raises(ValueError, lambda: f(0, 1, 2))
  834. raises(CoercionFailed, lambda: f(1, QQ(1,7)))
  835. raises(CoercionFailed, lambda: f(QQ(1,7), 1))
  836. raises(CoercionFailed, lambda: f(QQ(1,7), QQ(1,7)))
  837. def test_PolyElement_evaluate():
  838. R, x = ring("x", ZZ)
  839. f = x**3 + 4*x**2 + 2*x + 3
  840. r = f.evaluate(x, 0)
  841. assert r == 3 and not isinstance(r, PolyElement)
  842. raises(CoercionFailed, lambda: f.evaluate(x, QQ(1,7)))
  843. R, x, y, z = ring("x,y,z", ZZ)
  844. f = (x*y)**3 + 4*(x*y)**2 + 2*x*y + 3
  845. r = f.evaluate(x, 0)
  846. assert r == 3 and R.drop(x).is_element(r)
  847. r = f.evaluate([(x, 0), (y, 0)])
  848. assert r == 3 and R.drop(x, y).is_element(r)
  849. r = f.evaluate(y, 0)
  850. assert r == 3 and R.drop(y).is_element(r)
  851. r = f.evaluate([(y, 0), (x, 0)])
  852. assert r == 3 and R.drop(y, x).is_element(r)
  853. r = f.evaluate([(x, 0), (y, 0), (z, 0)])
  854. assert r == 3 and not isinstance(r, PolyElement)
  855. raises(CoercionFailed, lambda: f.evaluate([(x, 1), (y, QQ(1,7))]))
  856. raises(CoercionFailed, lambda: f.evaluate([(x, QQ(1,7)), (y, 1)]))
  857. raises(CoercionFailed, lambda: f.evaluate([(x, QQ(1,7)), (y, QQ(1,7))]))
  858. def test_PolyElement_subs():
  859. R, x = ring("x", ZZ)
  860. f = x**3 + 4*x**2 + 2*x + 3
  861. r = f.subs(x, 0)
  862. assert r == 3 and R.is_element(r)
  863. raises(CoercionFailed, lambda: f.subs(x, QQ(1,7)))
  864. R, x, y, z = ring("x,y,z", ZZ)
  865. f = x**3 + 4*x**2 + 2*x + 3
  866. r = f.subs(x, 0)
  867. assert r == 3 and R.is_element(r)
  868. r = f.subs([(x, 0), (y, 0)])
  869. assert r == 3 and R.is_element(r)
  870. raises(CoercionFailed, lambda: f.subs([(x, 1), (y, QQ(1,7))]))
  871. raises(CoercionFailed, lambda: f.subs([(x, QQ(1,7)), (y, 1)]))
  872. raises(CoercionFailed, lambda: f.subs([(x, QQ(1,7)), (y, QQ(1,7))]))
  873. def test_PolyElement_symmetrize():
  874. R, x, y = ring("x,y", ZZ)
  875. # Homogeneous, symmetric
  876. f = x**2 + y**2
  877. sym, rem, m = f.symmetrize()
  878. assert rem == 0
  879. assert sym.compose(m) + rem == f
  880. # Homogeneous, asymmetric
  881. f = x**2 - y**2
  882. sym, rem, m = f.symmetrize()
  883. assert rem != 0
  884. assert sym.compose(m) + rem == f
  885. # Inhomogeneous, symmetric
  886. f = x*y + 7
  887. sym, rem, m = f.symmetrize()
  888. assert rem == 0
  889. assert sym.compose(m) + rem == f
  890. # Inhomogeneous, asymmetric
  891. f = y + 7
  892. sym, rem, m = f.symmetrize()
  893. assert rem != 0
  894. assert sym.compose(m) + rem == f
  895. # Constant
  896. f = R.from_expr(3)
  897. sym, rem, m = f.symmetrize()
  898. assert rem == 0
  899. assert sym.compose(m) + rem == f
  900. # Constant constructed from sring
  901. R, f = sring(3)
  902. sym, rem, m = f.symmetrize()
  903. assert rem == 0
  904. assert sym.compose(m) + rem == f
  905. def test_PolyElement_compose():
  906. R, x = ring("x", ZZ)
  907. f = x**3 + 4*x**2 + 2*x + 3
  908. r = f.compose(x, 0)
  909. assert r == 3 and R.is_element(r)
  910. assert f.compose(x, x) == f
  911. assert f.compose(x, x**2) == x**6 + 4*x**4 + 2*x**2 + 3
  912. raises(CoercionFailed, lambda: f.compose(x, QQ(1,7)))
  913. R, x, y, z = ring("x,y,z", ZZ)
  914. f = x**3 + 4*x**2 + 2*x + 3
  915. r = f.compose(x, 0)
  916. assert r == 3 and R.is_element(r)
  917. r = f.compose([(x, 0), (y, 0)])
  918. assert r == 3 and R.is_element(r)
  919. r = (x**3 + 4*x**2 + 2*x*y*z + 3).compose(x, y*z**2 - 1)
  920. q = (y*z**2 - 1)**3 + 4*(y*z**2 - 1)**2 + 2*(y*z**2 - 1)*y*z + 3
  921. assert r == q and R.is_element(r)
  922. def test_PolyElement_is_():
  923. R, x,y,z = ring("x,y,z", QQ)
  924. assert (x - x).is_generator == False
  925. assert (x - x).is_ground == True
  926. assert (x - x).is_monomial == True
  927. assert (x - x).is_term == True
  928. assert (x - x + 1).is_generator == False
  929. assert (x - x + 1).is_ground == True
  930. assert (x - x + 1).is_monomial == True
  931. assert (x - x + 1).is_term == True
  932. assert x.is_generator == True
  933. assert x.is_ground == False
  934. assert x.is_monomial == True
  935. assert x.is_term == True
  936. assert (x*y).is_generator == False
  937. assert (x*y).is_ground == False
  938. assert (x*y).is_monomial == True
  939. assert (x*y).is_term == True
  940. assert (3*x).is_generator == False
  941. assert (3*x).is_ground == False
  942. assert (3*x).is_monomial == False
  943. assert (3*x).is_term == True
  944. assert (3*x + 1).is_generator == False
  945. assert (3*x + 1).is_ground == False
  946. assert (3*x + 1).is_monomial == False
  947. assert (3*x + 1).is_term == False
  948. assert R(0).is_zero is True
  949. assert R(1).is_zero is False
  950. assert R(0).is_one is False
  951. assert R(1).is_one is True
  952. assert (x - 1).is_monic is True
  953. assert (2*x - 1).is_monic is False
  954. assert (3*x + 2).is_primitive is True
  955. assert (4*x + 2).is_primitive is False
  956. assert (x + y + z + 1).is_linear is True
  957. assert (x*y*z + 1).is_linear is False
  958. assert (x*y + z + 1).is_quadratic is True
  959. assert (x*y*z + 1).is_quadratic is False
  960. assert (x - 1).is_squarefree is True
  961. assert ((x - 1)**2).is_squarefree is False
  962. assert (x**2 + x + 1).is_irreducible is True
  963. assert (x**2 + 2*x + 1).is_irreducible is False
  964. _, t = ring("t", FF(11))
  965. assert (7*t + 3).is_irreducible is True
  966. assert (7*t**2 + 3*t + 1).is_irreducible is False
  967. _, u = ring("u", ZZ)
  968. f = u**16 + u**14 - u**10 - u**8 - u**6 + u**2
  969. assert f.is_cyclotomic is False
  970. assert (f + 1).is_cyclotomic is True
  971. raises(MultivariatePolynomialError, lambda: x.is_cyclotomic)
  972. R, = ring("", ZZ)
  973. assert R(4).is_squarefree is True
  974. assert R(6).is_irreducible is True
  975. def test_PolyElement_drop():
  976. R, x,y,z = ring("x,y,z", ZZ)
  977. assert R(1).drop(0).ring == PolyRing("y,z", ZZ, lex)
  978. assert R(1).drop(0).drop(0).ring == PolyRing("z", ZZ, lex)
  979. assert R.is_element(R(1).drop(0).drop(0).drop(0)) is False
  980. raises(ValueError, lambda: z.drop(0).drop(0).drop(0))
  981. raises(ValueError, lambda: x.drop(0))
  982. def test_PolyElement_coeff_wrt():
  983. R, x, y, z = ring("x, y, z", ZZ)
  984. p = 4*x**3 + 5*y**2 + 6*y**2*z + 7
  985. assert p.coeff_wrt(1, 2) == 6*z + 5 # using generator index
  986. assert p.coeff_wrt(x, 3) == 4 # using generator
  987. p = 2*x**4 + 3*x*y**2*z + 10*y**2 + 10*x*z**2
  988. assert p.coeff_wrt(x, 1) == 3*y**2*z + 10*z**2
  989. assert p.coeff_wrt(y, 2) == 3*x*z + 10
  990. p = 4*x**2 + 2*x*y + 5
  991. assert p.coeff_wrt(z, 1) == R(0)
  992. assert p.coeff_wrt(y, 2) == R(0)
  993. def test_PolyElement_prem():
  994. R, x, y = ring("x, y", ZZ)
  995. f, g = x**2 + x*y, 2*x + 2
  996. assert f.prem(g) == -4*y + 4 # first generator is chosen by default if it is not given
  997. f, g = x**2 + 1, 2*x - 4
  998. assert f.prem(g) == f.prem(g, x) == 20
  999. assert f.prem(g, 1) == R(0)
  1000. f, g = x*y + 2*x + 1, x + y
  1001. assert f.prem(g) == -y**2 - 2*y + 1
  1002. assert f.prem(g, 1) == f.prem(g, y) == -x**2 + 2*x + 1
  1003. raises(ZeroDivisionError, lambda: f.prem(R(0)))
  1004. def test_PolyElement_pdiv():
  1005. R, x, y = ring("x,y", ZZ)
  1006. f, g = x**4 + 5*x**3 + 7*x**2, 2*x**2 + 3
  1007. assert f.pdiv(g) == f.pdiv(g, x) == (4*x**2 + 20*x + 22, -60*x - 66)
  1008. f, g = x**2 - y**2, x - y
  1009. assert f.pdiv(g) == f.pdiv(g, 0) == (x + y, 0)
  1010. f, g = x*y + 2*x + 1, x + y
  1011. assert f.pdiv(g) == (y + 2, -y**2 - 2*y + 1)
  1012. assert f.pdiv(g, y) == f.pdiv(g, 1) == (x + 1, -x**2 + 2*x + 1)
  1013. assert R(0).pdiv(g) == (0, 0)
  1014. raises(ZeroDivisionError, lambda: f.prem(R(0)))
  1015. def test_PolyElement_pquo():
  1016. R, x, y = ring("x, y", ZZ)
  1017. f, g = x**4 - 4*x**2*y + 4*y**2, x**2 - 2*y
  1018. assert f.pquo(g) == f.pquo(g, x) == x**2 - 2*y
  1019. assert f.pquo(g, y) == 4*x**2 - 8*y + 4
  1020. f, g = x**4 - y**4, x**2 - y**2
  1021. assert f.pquo(g) == f.pquo(g, 0) == x**2 + y**2
  1022. def test_PolyElement_pexquo():
  1023. R, x, y = ring("x, y", ZZ)
  1024. f, g = x**2 - y**2, x - y
  1025. assert f.pexquo(g) == f.pexquo(g, x) == x + y
  1026. assert f.pexquo(g, y) == f.pexquo(g, 1) == x + y + 1
  1027. f, g = x**2 + 3*x + 6, x + 2
  1028. raises(ExactQuotientFailed, lambda: f.pexquo(g))
  1029. def test_PolyElement_gcdex():
  1030. _, x = ring("x", QQ)
  1031. f, g = 2*x, x**2 - 16
  1032. s, t, h = x/32, -QQ(1, 16), 1
  1033. assert f.half_gcdex(g) == (s, h)
  1034. assert f.gcdex(g) == (s, t, h)
  1035. def test_PolyElement_subresultants():
  1036. R, x, y = ring("x, y", ZZ)
  1037. f, g = x**2*y + x*y, x + y # degree(f, x) > degree(g, x)
  1038. h = y**3 - y**2
  1039. assert f.subresultants(g) == [f, g, h] # first generator is chosen default
  1040. # generator index or generator is given
  1041. assert f.subresultants(g, 0) == f.subresultants(g, x) == [f, g, h]
  1042. assert f.subresultants(g, y) == [x**2*y + x*y, x + y, x**3 + x**2]
  1043. f, g = 2*x - y, x**2 + 2*y + x # degree(f, x) < degree(g, x)
  1044. assert f.subresultants(g) == [x**2 + x + 2*y, 2*x - y, y**2 + 10*y]
  1045. f, g = R(0), y**3 - y**2 # f = 0
  1046. assert f.subresultants(g) == [y**3 - y**2, 1]
  1047. f, g = x**2*y + x*y, R(0) # g = 0
  1048. assert f.subresultants(g) == [x**2*y + x*y, 1]
  1049. f, g = R(0), R(0) # f = 0 and g = 0
  1050. assert f.subresultants(g) == [0, 0]
  1051. f, g = x**2 + x, x**2 + x # f and g are same polynomial
  1052. assert f.subresultants(g) == [x**2 + x, x**2 + x]
  1053. def test_PolyElement_resultant():
  1054. _, x = ring("x", ZZ)
  1055. f, g, h = x**2 - 2*x + 1, x**2 - 1, 0
  1056. assert f.resultant(g) == h
  1057. def test_PolyElement_discriminant():
  1058. _, x = ring("x", ZZ)
  1059. f, g = x**3 + 3*x**2 + 9*x - 13, -11664
  1060. assert f.discriminant() == g
  1061. F, a, b, c = ring("a,b,c", ZZ)
  1062. _, x = ring("x", F)
  1063. f, g = a*x**2 + b*x + c, b**2 - 4*a*c
  1064. assert f.discriminant() == g
  1065. def test_PolyElement_decompose():
  1066. _, x = ring("x", ZZ)
  1067. f = x**12 + 20*x**10 + 150*x**8 + 500*x**6 + 625*x**4 - 2*x**3 - 10*x + 9
  1068. g = x**4 - 2*x + 9
  1069. h = x**3 + 5*x
  1070. assert g.compose(x, h) == f
  1071. assert f.decompose() == [g, h]
  1072. def test_PolyElement_shift():
  1073. _, x = ring("x", ZZ)
  1074. assert (x**2 - 2*x + 1).shift(2) == x**2 + 2*x + 1
  1075. assert (x**2 - 2*x + 1).shift_list([2]) == x**2 + 2*x + 1
  1076. R, x, y = ring("x, y", ZZ)
  1077. assert (x*y).shift_list([1, 2]) == (x+1)*(y+2)
  1078. raises(MultivariatePolynomialError, lambda: (x*y).shift(1))
  1079. def test_PolyElement_sturm():
  1080. F, t = field("t", ZZ)
  1081. _, x = ring("x", F)
  1082. f = 1024/(15625*t**8)*x**5 - 4096/(625*t**8)*x**4 + 32/(15625*t**4)*x**3 - 128/(625*t**4)*x**2 + F(1)/62500*x - F(1)/625
  1083. assert f.sturm() == [
  1084. x**3 - 100*x**2 + t**4/64*x - 25*t**4/16,
  1085. 3*x**2 - 200*x + t**4/64,
  1086. (-t**4/96 + F(20000)/9)*x + 25*t**4/18,
  1087. (-9*t**12 - 11520000*t**8 - 3686400000000*t**4)/(576*t**8 - 245760000*t**4 + 26214400000000),
  1088. ]
  1089. def test_PolyElement_gff_list():
  1090. _, x = ring("x", ZZ)
  1091. f = x**5 + 2*x**4 - x**3 - 2*x**2
  1092. assert f.gff_list() == [(x, 1), (x + 2, 4)]
  1093. f = x*(x - 1)**3*(x - 2)**2*(x - 4)**2*(x - 5)
  1094. assert f.gff_list() == [(x**2 - 5*x + 4, 1), (x**2 - 5*x + 4, 2), (x, 3)]
  1095. def test_PolyElement_norm():
  1096. k = QQ
  1097. K = QQ.algebraic_field(sqrt(2))
  1098. sqrt2 = K.unit
  1099. _, X, Y = ring("x,y", k)
  1100. _, x, y = ring("x,y", K)
  1101. assert (x*y + sqrt2).norm() == X**2*Y**2 - 2
  1102. def test_PolyElement_sqf_norm():
  1103. R, x = ring("x", QQ.algebraic_field(sqrt(3)))
  1104. X = R.to_ground().x
  1105. assert (x**2 - 2).sqf_norm() == ([1], x**2 - 2*sqrt(3)*x + 1, X**4 - 10*X**2 + 1)
  1106. R, x = ring("x", QQ.algebraic_field(sqrt(2)))
  1107. X = R.to_ground().x
  1108. assert (x**2 - 3).sqf_norm() == ([1], x**2 - 2*sqrt(2)*x - 1, X**4 - 10*X**2 + 1)
  1109. def test_PolyElement_sqf_list():
  1110. _, x = ring("x", ZZ)
  1111. f = x**5 - x**3 - x**2 + 1
  1112. g = x**3 + 2*x**2 + 2*x + 1
  1113. h = x - 1
  1114. p = x**4 + x**3 - x - 1
  1115. assert f.sqf_part() == p
  1116. assert f.sqf_list() == (1, [(g, 1), (h, 2)])
  1117. def test_issue_18894():
  1118. items = [S(3)/16 + sqrt(3*sqrt(3) + 10)/8, S(1)/8 + 3*sqrt(3)/16, S(1)/8 + 3*sqrt(3)/16, -S(3)/16 + sqrt(3*sqrt(3) + 10)/8]
  1119. R, a = sring(items, extension=True)
  1120. assert R.domain == QQ.algebraic_field(sqrt(3)+sqrt(3*sqrt(3)+10))
  1121. assert R.gens == ()
  1122. result = []
  1123. for item in items:
  1124. result.append(R.domain.from_sympy(item))
  1125. assert a == result
  1126. def test_PolyElement_factor_list():
  1127. _, x = ring("x", ZZ)
  1128. f = x**5 - x**3 - x**2 + 1
  1129. u = x + 1
  1130. v = x - 1
  1131. w = x**2 + x + 1
  1132. assert f.factor_list() == (1, [(u, 1), (v, 2), (w, 1)])
  1133. def test_issue_21410():
  1134. R, x = ring('x', FF(2))
  1135. p = x**6 + x**5 + x**4 + x**3 + 1
  1136. assert p._pow_multinomial(4) == x**24 + x**20 + x**16 + x**12 + 1
  1137. def test_zero_polynomial_primitive():
  1138. x = symbols('x')
  1139. R = ZZ[x]
  1140. zero_poly = R(0)
  1141. cont, prim = zero_poly.primitive()
  1142. assert cont == 0
  1143. assert prim == zero_poly
  1144. assert prim.is_primitive is False