test_rings.py 44 KB

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