m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1411 lines
42 KiB

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