applied-ai-018 commited on
Commit
f357b24
·
verified ·
1 Parent(s): ac5fea4

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/__init__.cpython-310.pyc +0 -0
  2. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/appellseqs.cpython-310.pyc +0 -0
  3. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/compatibility.cpython-310.pyc +0 -0
  4. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/euclidtools.cpython-310.pyc +0 -0
  5. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/factortools.cpython-310.pyc +0 -0
  6. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/fglmtools.cpython-310.pyc +0 -0
  7. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/fields.cpython-310.pyc +0 -0
  8. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/heuristicgcd.cpython-310.pyc +0 -0
  9. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/modulargcd.cpython-310.pyc +0 -0
  10. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/orderings.cpython-310.pyc +0 -0
  11. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/orthopolys.cpython-310.pyc +0 -0
  12. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyclasses.cpython-310.pyc +0 -0
  13. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyconfig.cpython-310.pyc +0 -0
  14. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyfuncs.cpython-310.pyc +0 -0
  15. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polymatrix.cpython-310.pyc +0 -0
  16. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyutils.cpython-310.pyc +0 -0
  17. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/rationaltools.cpython-310.pyc +0 -0
  18. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/ring_series.cpython-310.pyc +0 -0
  19. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/rings.cpython-310.pyc +0 -0
  20. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/solvers.cpython-310.pyc +0 -0
  21. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/sqfreetools.cpython-310.pyc +0 -0
  22. env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/subresultants_qq_zz.cpython-310.pyc +0 -0
  23. env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/__init__.py +0 -0
  24. env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/__pycache__/__init__.cpython-310.pyc +0 -0
  25. env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/__pycache__/bench_galoispolys.cpython-310.pyc +0 -0
  26. env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/__pycache__/bench_groebnertools.cpython-310.pyc +0 -0
  27. env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/__pycache__/bench_solvers.cpython-310.pyc +0 -0
  28. env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/bench_galoispolys.py +66 -0
  29. env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/bench_groebnertools.py +25 -0
  30. env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/bench_solvers.py +0 -0
  31. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__init__.py +0 -0
  32. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_factortools.cpython-310.pyc +0 -0
  33. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_heuristicgcd.cpython-310.pyc +0 -0
  34. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_multivariate_resultants.cpython-310.pyc +0 -0
  35. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_partfrac.cpython-310.pyc +0 -0
  36. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_polyclasses.cpython-310.pyc +0 -0
  37. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_polyoptions.cpython-310.pyc +0 -0
  38. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_pythonrational.cpython-310.pyc +0 -0
  39. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_ring_series.cpython-310.pyc +0 -0
  40. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_rootisolation.cpython-310.pyc +0 -0
  41. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_sqfreetools.cpython-310.pyc +0 -0
  42. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_appellseqs.py +91 -0
  43. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_constructor.py +208 -0
  44. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_densearith.py +996 -0
  45. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_densetools.py +668 -0
  46. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_dispersion.py +95 -0
  47. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_distributedmodules.py +208 -0
  48. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_euclidtools.py +712 -0
  49. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_factortools.py +771 -0
  50. env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_fields.py +362 -0
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (5.28 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/appellseqs.cpython-310.pyc ADDED
Binary file (8.7 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/compatibility.cpython-310.pyc ADDED
Binary file (62 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/euclidtools.cpython-310.pyc ADDED
Binary file (37.2 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/factortools.cpython-310.pyc ADDED
Binary file (33.3 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/fglmtools.cpython-310.pyc ADDED
Binary file (6.93 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/fields.cpython-310.pyc ADDED
Binary file (18.8 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/heuristicgcd.cpython-310.pyc ADDED
Binary file (3.16 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/modulargcd.cpython-310.pyc ADDED
Binary file (49.7 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/orderings.cpython-310.pyc ADDED
Binary file (11.8 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/orthopolys.cpython-310.pyc ADDED
Binary file (8.96 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyclasses.cpython-310.pyc ADDED
Binary file (55.1 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyconfig.cpython-310.pyc ADDED
Binary file (1.66 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyfuncs.cpython-310.pyc ADDED
Binary file (8.74 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polymatrix.cpython-310.pyc ADDED
Binary file (11.7 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/polyutils.cpython-310.pyc ADDED
Binary file (15.6 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/rationaltools.cpython-310.pyc ADDED
Binary file (3.12 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/ring_series.cpython-310.pyc ADDED
Binary file (47.9 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/rings.cpython-310.pyc ADDED
Binary file (69.5 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/solvers.cpython-310.pyc ADDED
Binary file (13.9 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/sqfreetools.cpython-310.pyc ADDED
Binary file (11 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/__pycache__/subresultants_qq_zz.cpython-310.pyc ADDED
Binary file (68.2 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/__init__.py ADDED
File without changes
env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (187 Bytes). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/__pycache__/bench_galoispolys.cpython-310.pyc ADDED
Binary file (2.28 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/__pycache__/bench_groebnertools.cpython-310.pyc ADDED
Binary file (1.37 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/__pycache__/bench_solvers.cpython-310.pyc ADDED
Binary file (335 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/bench_galoispolys.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Benchmarks for polynomials over Galois fields. """
2
+
3
+
4
+ from sympy.polys.galoistools import gf_from_dict, gf_factor_sqf
5
+ from sympy.polys.domains import ZZ
6
+ from sympy.core.numbers import pi
7
+ from sympy.ntheory.generate import nextprime
8
+
9
+
10
+ def gathen_poly(n, p, K):
11
+ return gf_from_dict({n: K.one, 1: K.one, 0: K.one}, p, K)
12
+
13
+
14
+ def shoup_poly(n, p, K):
15
+ f = [K.one] * (n + 1)
16
+ for i in range(1, n + 1):
17
+ f[i] = (f[i - 1]**2 + K.one) % p
18
+ return f
19
+
20
+
21
+ def genprime(n, K):
22
+ return K(nextprime(int((2**n * pi).evalf())))
23
+
24
+ p_10 = genprime(10, ZZ)
25
+ f_10 = gathen_poly(10, p_10, ZZ)
26
+
27
+ p_20 = genprime(20, ZZ)
28
+ f_20 = gathen_poly(20, p_20, ZZ)
29
+
30
+
31
+ def timeit_gathen_poly_f10_zassenhaus():
32
+ gf_factor_sqf(f_10, p_10, ZZ, method='zassenhaus')
33
+
34
+
35
+ def timeit_gathen_poly_f10_shoup():
36
+ gf_factor_sqf(f_10, p_10, ZZ, method='shoup')
37
+
38
+
39
+ def timeit_gathen_poly_f20_zassenhaus():
40
+ gf_factor_sqf(f_20, p_20, ZZ, method='zassenhaus')
41
+
42
+
43
+ def timeit_gathen_poly_f20_shoup():
44
+ gf_factor_sqf(f_20, p_20, ZZ, method='shoup')
45
+
46
+ P_08 = genprime(8, ZZ)
47
+ F_10 = shoup_poly(10, P_08, ZZ)
48
+
49
+ P_18 = genprime(18, ZZ)
50
+ F_20 = shoup_poly(20, P_18, ZZ)
51
+
52
+
53
+ def timeit_shoup_poly_F10_zassenhaus():
54
+ gf_factor_sqf(F_10, P_08, ZZ, method='zassenhaus')
55
+
56
+
57
+ def timeit_shoup_poly_F10_shoup():
58
+ gf_factor_sqf(F_10, P_08, ZZ, method='shoup')
59
+
60
+
61
+ def timeit_shoup_poly_F20_zassenhaus():
62
+ gf_factor_sqf(F_20, P_18, ZZ, method='zassenhaus')
63
+
64
+
65
+ def timeit_shoup_poly_F20_shoup():
66
+ gf_factor_sqf(F_20, P_18, ZZ, method='shoup')
env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/bench_groebnertools.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Benchmark of the Groebner bases algorithms. """
2
+
3
+
4
+ from sympy.polys.rings import ring
5
+ from sympy.polys.domains import QQ
6
+ from sympy.polys.groebnertools import groebner
7
+
8
+ R, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12 = ring("x1:13", QQ)
9
+
10
+ V = R.gens
11
+ E = [(x1, x2), (x2, x3), (x1, x4), (x1, x6), (x1, x12), (x2, x5), (x2, x7), (x3, x8),
12
+ (x3, x10), (x4, x11), (x4, x9), (x5, x6), (x6, x7), (x7, x8), (x8, x9), (x9, x10),
13
+ (x10, x11), (x11, x12), (x5, x12), (x5, x9), (x6, x10), (x7, x11), (x8, x12)]
14
+
15
+ F3 = [ x**3 - 1 for x in V ]
16
+ Fg = [ x**2 + x*y + y**2 for x, y in E ]
17
+
18
+ F_1 = F3 + Fg
19
+ F_2 = F3 + Fg + [x3**2 + x3*x4 + x4**2]
20
+
21
+ def time_vertex_color_12_vertices_23_edges():
22
+ assert groebner(F_1, R) != [1]
23
+
24
+ def time_vertex_color_12_vertices_24_edges():
25
+ assert groebner(F_2, R) == [1]
env-llmeval/lib/python3.10/site-packages/sympy/polys/benchmarks/bench_solvers.py ADDED
The diff for this file is too large to render. See raw diff
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__init__.py ADDED
File without changes
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_factortools.cpython-310.pyc ADDED
Binary file (24.4 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_heuristicgcd.cpython-310.pyc ADDED
Binary file (3.94 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_multivariate_resultants.cpython-310.pyc ADDED
Binary file (8.76 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_partfrac.cpython-310.pyc ADDED
Binary file (8.79 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_polyclasses.cpython-310.pyc ADDED
Binary file (15.3 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_polyoptions.cpython-310.pyc ADDED
Binary file (14.7 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_pythonrational.cpython-310.pyc ADDED
Binary file (5.14 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_ring_series.cpython-310.pyc ADDED
Binary file (30.1 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_rootisolation.cpython-310.pyc ADDED
Binary file (26.5 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_sqfreetools.cpython-310.pyc ADDED
Binary file (4.61 kB). View file
 
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_appellseqs.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for efficient functions for generating Appell sequences."""
2
+ from sympy.core.numbers import Rational as Q
3
+ from sympy.polys.polytools import Poly
4
+ from sympy.testing.pytest import raises
5
+ from sympy.polys.appellseqs import (bernoulli_poly, bernoulli_c_poly,
6
+ euler_poly, genocchi_poly, andre_poly)
7
+ from sympy.abc import x
8
+
9
+ def test_bernoulli_poly():
10
+ raises(ValueError, lambda: bernoulli_poly(-1, x))
11
+ assert bernoulli_poly(1, x, polys=True) == Poly(x - Q(1,2))
12
+
13
+ assert bernoulli_poly(0, x) == 1
14
+ assert bernoulli_poly(1, x) == x - Q(1,2)
15
+ assert bernoulli_poly(2, x) == x**2 - x + Q(1,6)
16
+ assert bernoulli_poly(3, x) == x**3 - Q(3,2)*x**2 + Q(1,2)*x
17
+ assert bernoulli_poly(4, x) == x**4 - 2*x**3 + x**2 - Q(1,30)
18
+ assert bernoulli_poly(5, x) == x**5 - Q(5,2)*x**4 + Q(5,3)*x**3 - Q(1,6)*x
19
+ assert bernoulli_poly(6, x) == x**6 - 3*x**5 + Q(5,2)*x**4 - Q(1,2)*x**2 + Q(1,42)
20
+
21
+ assert bernoulli_poly(1).dummy_eq(x - Q(1,2))
22
+ assert bernoulli_poly(1, polys=True) == Poly(x - Q(1,2))
23
+
24
+ def test_bernoulli_c_poly():
25
+ raises(ValueError, lambda: bernoulli_c_poly(-1, x))
26
+ assert bernoulli_c_poly(1, x, polys=True) == Poly(x, domain='QQ')
27
+
28
+ assert bernoulli_c_poly(0, x) == 1
29
+ assert bernoulli_c_poly(1, x) == x
30
+ assert bernoulli_c_poly(2, x) == x**2 - Q(1,3)
31
+ assert bernoulli_c_poly(3, x) == x**3 - x
32
+ assert bernoulli_c_poly(4, x) == x**4 - 2*x**2 + Q(7,15)
33
+ assert bernoulli_c_poly(5, x) == x**5 - Q(10,3)*x**3 + Q(7,3)*x
34
+ assert bernoulli_c_poly(6, x) == x**6 - 5*x**4 + 7*x**2 - Q(31,21)
35
+
36
+ assert bernoulli_c_poly(1).dummy_eq(x)
37
+ assert bernoulli_c_poly(1, polys=True) == Poly(x, domain='QQ')
38
+
39
+ assert 2**8 * bernoulli_poly(8, (x+1)/2).expand() == bernoulli_c_poly(8, x)
40
+ assert 2**9 * bernoulli_poly(9, (x+1)/2).expand() == bernoulli_c_poly(9, x)
41
+
42
+ def test_genocchi_poly():
43
+ raises(ValueError, lambda: genocchi_poly(-1, x))
44
+ assert genocchi_poly(2, x, polys=True) == Poly(-2*x + 1)
45
+
46
+ assert genocchi_poly(0, x) == 0
47
+ assert genocchi_poly(1, x) == -1
48
+ assert genocchi_poly(2, x) == 1 - 2*x
49
+ assert genocchi_poly(3, x) == 3*x - 3*x**2
50
+ assert genocchi_poly(4, x) == -1 + 6*x**2 - 4*x**3
51
+ assert genocchi_poly(5, x) == -5*x + 10*x**3 - 5*x**4
52
+ assert genocchi_poly(6, x) == 3 - 15*x**2 + 15*x**4 - 6*x**5
53
+
54
+ assert genocchi_poly(2).dummy_eq(-2*x + 1)
55
+ assert genocchi_poly(2, polys=True) == Poly(-2*x + 1)
56
+
57
+ assert 2 * (bernoulli_poly(8, x) - bernoulli_c_poly(8, x)) == genocchi_poly(8, x)
58
+ assert 2 * (bernoulli_poly(9, x) - bernoulli_c_poly(9, x)) == genocchi_poly(9, x)
59
+
60
+ def test_euler_poly():
61
+ raises(ValueError, lambda: euler_poly(-1, x))
62
+ assert euler_poly(1, x, polys=True) == Poly(x - Q(1,2))
63
+
64
+ assert euler_poly(0, x) == 1
65
+ assert euler_poly(1, x) == x - Q(1,2)
66
+ assert euler_poly(2, x) == x**2 - x
67
+ assert euler_poly(3, x) == x**3 - Q(3,2)*x**2 + Q(1,4)
68
+ assert euler_poly(4, x) == x**4 - 2*x**3 + x
69
+ assert euler_poly(5, x) == x**5 - Q(5,2)*x**4 + Q(5,2)*x**2 - Q(1,2)
70
+ assert euler_poly(6, x) == x**6 - 3*x**5 + 5*x**3 - 3*x
71
+
72
+ assert euler_poly(1).dummy_eq(x - Q(1,2))
73
+ assert euler_poly(1, polys=True) == Poly(x - Q(1,2))
74
+
75
+ assert genocchi_poly(9, x) == euler_poly(8, x) * -9
76
+ assert genocchi_poly(10, x) == euler_poly(9, x) * -10
77
+
78
+ def test_andre_poly():
79
+ raises(ValueError, lambda: andre_poly(-1, x))
80
+ assert andre_poly(1, x, polys=True) == Poly(x)
81
+
82
+ assert andre_poly(0, x) == 1
83
+ assert andre_poly(1, x) == x
84
+ assert andre_poly(2, x) == x**2 - 1
85
+ assert andre_poly(3, x) == x**3 - 3*x
86
+ assert andre_poly(4, x) == x**4 - 6*x**2 + 5
87
+ assert andre_poly(5, x) == x**5 - 10*x**3 + 25*x
88
+ assert andre_poly(6, x) == x**6 - 15*x**4 + 75*x**2 - 61
89
+
90
+ assert andre_poly(1).dummy_eq(x)
91
+ assert andre_poly(1, polys=True) == Poly(x)
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_constructor.py ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for tools for constructing domains for expressions. """
2
+
3
+ from sympy.polys.constructor import construct_domain
4
+ from sympy.polys.domains import ZZ, QQ, ZZ_I, QQ_I, RR, CC, EX
5
+ from sympy.polys.domains.realfield import RealField
6
+ from sympy.polys.domains.complexfield import ComplexField
7
+
8
+ from sympy.core import (Catalan, GoldenRatio)
9
+ from sympy.core.numbers import (E, Float, I, Rational, pi)
10
+ from sympy.core.singleton import S
11
+ from sympy.functions.elementary.exponential import exp
12
+ from sympy.functions.elementary.miscellaneous import sqrt
13
+ from sympy.functions.elementary.trigonometric import sin
14
+ from sympy.abc import x, y
15
+
16
+
17
+ def test_construct_domain():
18
+
19
+ assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
20
+ assert construct_domain([1, 2, 3], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])
21
+
22
+ assert construct_domain([S.One, S(2), S(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
23
+ assert construct_domain([S.One, S(2), S(3)], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])
24
+
25
+ assert construct_domain([S.Half, S(2)]) == (QQ, [QQ(1, 2), QQ(2)])
26
+ result = construct_domain([3.14, 1, S.Half])
27
+ assert isinstance(result[0], RealField)
28
+ assert result[1] == [RR(3.14), RR(1.0), RR(0.5)]
29
+
30
+ result = construct_domain([3.14, I, S.Half])
31
+ assert isinstance(result[0], ComplexField)
32
+ assert result[1] == [CC(3.14), CC(1.0j), CC(0.5)]
33
+
34
+ assert construct_domain([1.0+I]) == (CC, [CC(1.0, 1.0)])
35
+ assert construct_domain([2.0+3.0*I]) == (CC, [CC(2.0, 3.0)])
36
+
37
+ assert construct_domain([1, I]) == (ZZ_I, [ZZ_I(1, 0), ZZ_I(0, 1)])
38
+ assert construct_domain([1, I/2]) == (QQ_I, [QQ_I(1, 0), QQ_I(0, S.Half)])
39
+
40
+ assert construct_domain([3.14, sqrt(2)], extension=None) == (EX, [EX(3.14), EX(sqrt(2))])
41
+ assert construct_domain([3.14, sqrt(2)], extension=True) == (EX, [EX(3.14), EX(sqrt(2))])
42
+
43
+ assert construct_domain([1, sqrt(2)], extension=None) == (EX, [EX(1), EX(sqrt(2))])
44
+
45
+ assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))])
46
+ assert construct_domain([x, sqrt(x), sqrt(y)]) == (EX, [EX(x), EX(sqrt(x)), EX(sqrt(y))])
47
+
48
+ alg = QQ.algebraic_field(sqrt(2))
49
+
50
+ assert construct_domain([7, S.Half, sqrt(2)], extension=True) == \
51
+ (alg, [alg.convert(7), alg.convert(S.Half), alg.convert(sqrt(2))])
52
+
53
+ alg = QQ.algebraic_field(sqrt(2) + sqrt(3))
54
+
55
+ assert construct_domain([7, sqrt(2), sqrt(3)], extension=True) == \
56
+ (alg, [alg.convert(7), alg.convert(sqrt(2)), alg.convert(sqrt(3))])
57
+
58
+ dom = ZZ[x]
59
+
60
+ assert construct_domain([2*x, 3]) == \
61
+ (dom, [dom.convert(2*x), dom.convert(3)])
62
+
63
+ dom = ZZ[x, y]
64
+
65
+ assert construct_domain([2*x, 3*y]) == \
66
+ (dom, [dom.convert(2*x), dom.convert(3*y)])
67
+
68
+ dom = QQ[x]
69
+
70
+ assert construct_domain([x/2, 3]) == \
71
+ (dom, [dom.convert(x/2), dom.convert(3)])
72
+
73
+ dom = QQ[x, y]
74
+
75
+ assert construct_domain([x/2, 3*y]) == \
76
+ (dom, [dom.convert(x/2), dom.convert(3*y)])
77
+
78
+ dom = ZZ_I[x]
79
+
80
+ assert construct_domain([2*x, I]) == \
81
+ (dom, [dom.convert(2*x), dom.convert(I)])
82
+
83
+ dom = ZZ_I[x, y]
84
+
85
+ assert construct_domain([2*x, I*y]) == \
86
+ (dom, [dom.convert(2*x), dom.convert(I*y)])
87
+
88
+ dom = QQ_I[x]
89
+
90
+ assert construct_domain([x/2, I]) == \
91
+ (dom, [dom.convert(x/2), dom.convert(I)])
92
+
93
+ dom = QQ_I[x, y]
94
+
95
+ assert construct_domain([x/2, I*y]) == \
96
+ (dom, [dom.convert(x/2), dom.convert(I*y)])
97
+
98
+ dom = RR[x]
99
+
100
+ assert construct_domain([x/2, 3.5]) == \
101
+ (dom, [dom.convert(x/2), dom.convert(3.5)])
102
+
103
+ dom = RR[x, y]
104
+
105
+ assert construct_domain([x/2, 3.5*y]) == \
106
+ (dom, [dom.convert(x/2), dom.convert(3.5*y)])
107
+
108
+ dom = CC[x]
109
+
110
+ assert construct_domain([I*x/2, 3.5]) == \
111
+ (dom, [dom.convert(I*x/2), dom.convert(3.5)])
112
+
113
+ dom = CC[x, y]
114
+
115
+ assert construct_domain([I*x/2, 3.5*y]) == \
116
+ (dom, [dom.convert(I*x/2), dom.convert(3.5*y)])
117
+
118
+ dom = CC[x]
119
+
120
+ assert construct_domain([x/2, I*3.5]) == \
121
+ (dom, [dom.convert(x/2), dom.convert(I*3.5)])
122
+
123
+ dom = CC[x, y]
124
+
125
+ assert construct_domain([x/2, I*3.5*y]) == \
126
+ (dom, [dom.convert(x/2), dom.convert(I*3.5*y)])
127
+
128
+ dom = ZZ.frac_field(x)
129
+
130
+ assert construct_domain([2/x, 3]) == \
131
+ (dom, [dom.convert(2/x), dom.convert(3)])
132
+
133
+ dom = ZZ.frac_field(x, y)
134
+
135
+ assert construct_domain([2/x, 3*y]) == \
136
+ (dom, [dom.convert(2/x), dom.convert(3*y)])
137
+
138
+ dom = RR.frac_field(x)
139
+
140
+ assert construct_domain([2/x, 3.5]) == \
141
+ (dom, [dom.convert(2/x), dom.convert(3.5)])
142
+
143
+ dom = RR.frac_field(x, y)
144
+
145
+ assert construct_domain([2/x, 3.5*y]) == \
146
+ (dom, [dom.convert(2/x), dom.convert(3.5*y)])
147
+
148
+ dom = RealField(prec=336)[x]
149
+
150
+ assert construct_domain([pi.evalf(100)*x]) == \
151
+ (dom, [dom.convert(pi.evalf(100)*x)])
152
+
153
+ assert construct_domain(2) == (ZZ, ZZ(2))
154
+ assert construct_domain(S(2)/3) == (QQ, QQ(2, 3))
155
+ assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3))
156
+
157
+ assert construct_domain({}) == (ZZ, {})
158
+
159
+
160
+ def test_complex_exponential():
161
+ w = exp(-I*2*pi/3, evaluate=False)
162
+ alg = QQ.algebraic_field(w)
163
+ assert construct_domain([w**2, w, 1], extension=True) == (
164
+ alg,
165
+ [alg.convert(w**2),
166
+ alg.convert(w),
167
+ alg.convert(1)]
168
+ )
169
+
170
+
171
+ def test_composite_option():
172
+ assert construct_domain({(1,): sin(y)}, composite=False) == \
173
+ (EX, {(1,): EX(sin(y))})
174
+
175
+ assert construct_domain({(1,): y}, composite=False) == \
176
+ (EX, {(1,): EX(y)})
177
+
178
+ assert construct_domain({(1, 1): 1}, composite=False) == \
179
+ (ZZ, {(1, 1): 1})
180
+
181
+ assert construct_domain({(1, 0): y}, composite=False) == \
182
+ (EX, {(1, 0): EX(y)})
183
+
184
+
185
+ def test_precision():
186
+ f1 = Float("1.01")
187
+ f2 = Float("1.0000000000000000000001")
188
+ for u in [1, 1e-2, 1e-6, 1e-13, 1e-14, 1e-16, 1e-20, 1e-100, 1e-300,
189
+ f1, f2]:
190
+ result = construct_domain([u])
191
+ v = float(result[1][0])
192
+ assert abs(u - v) / u < 1e-14 # Test relative accuracy
193
+
194
+ result = construct_domain([f1])
195
+ y = result[1][0]
196
+ assert y-1 > 1e-50
197
+
198
+ result = construct_domain([f2])
199
+ y = result[1][0]
200
+ assert y-1 > 1e-50
201
+
202
+
203
+ def test_issue_11538():
204
+ for n in [E, pi, Catalan]:
205
+ assert construct_domain(n)[0] == ZZ[n]
206
+ assert construct_domain(x + n)[0] == ZZ[x, n]
207
+ assert construct_domain(GoldenRatio)[0] == EX
208
+ assert construct_domain(x + GoldenRatio)[0] == EX
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_densearith.py ADDED
@@ -0,0 +1,996 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for dense recursive polynomials' arithmetics. """
2
+
3
+ from sympy.polys.densebasic import (
4
+ dup_normal, dmp_normal,
5
+ )
6
+
7
+ from sympy.polys.densearith import (
8
+ dup_add_term, dmp_add_term,
9
+ dup_sub_term, dmp_sub_term,
10
+ dup_mul_term, dmp_mul_term,
11
+ dup_add_ground, dmp_add_ground,
12
+ dup_sub_ground, dmp_sub_ground,
13
+ dup_mul_ground, dmp_mul_ground,
14
+ dup_quo_ground, dmp_quo_ground,
15
+ dup_exquo_ground, dmp_exquo_ground,
16
+ dup_lshift, dup_rshift,
17
+ dup_abs, dmp_abs,
18
+ dup_neg, dmp_neg,
19
+ dup_add, dmp_add,
20
+ dup_sub, dmp_sub,
21
+ dup_mul, dmp_mul,
22
+ dup_sqr, dmp_sqr,
23
+ dup_pow, dmp_pow,
24
+ dup_add_mul, dmp_add_mul,
25
+ dup_sub_mul, dmp_sub_mul,
26
+ dup_pdiv, dup_prem, dup_pquo, dup_pexquo,
27
+ dmp_pdiv, dmp_prem, dmp_pquo, dmp_pexquo,
28
+ dup_rr_div, dmp_rr_div,
29
+ dup_ff_div, dmp_ff_div,
30
+ dup_div, dup_rem, dup_quo, dup_exquo,
31
+ dmp_div, dmp_rem, dmp_quo, dmp_exquo,
32
+ dup_max_norm, dmp_max_norm,
33
+ dup_l1_norm, dmp_l1_norm,
34
+ dup_l2_norm_squared, dmp_l2_norm_squared,
35
+ dup_expand, dmp_expand,
36
+ )
37
+
38
+ from sympy.polys.polyerrors import (
39
+ ExactQuotientFailed,
40
+ )
41
+
42
+ from sympy.polys.specialpolys import f_polys
43
+ from sympy.polys.domains import FF, ZZ, QQ
44
+
45
+ from sympy.testing.pytest import raises
46
+
47
+ f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
48
+ F_0 = dmp_mul_ground(dmp_normal(f_0, 2, QQ), QQ(1, 7), 2, QQ)
49
+
50
+ def test_dup_add_term():
51
+ f = dup_normal([], ZZ)
52
+
53
+ assert dup_add_term(f, ZZ(0), 0, ZZ) == dup_normal([], ZZ)
54
+
55
+ assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1], ZZ)
56
+ assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 0], ZZ)
57
+ assert dup_add_term(f, ZZ(1), 2, ZZ) == dup_normal([1, 0, 0], ZZ)
58
+
59
+ f = dup_normal([1, 1, 1], ZZ)
60
+
61
+ assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1, 1, 2], ZZ)
62
+ assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 2, 1], ZZ)
63
+ assert dup_add_term(f, ZZ(1), 2, ZZ) == dup_normal([2, 1, 1], ZZ)
64
+
65
+ assert dup_add_term(f, ZZ(1), 3, ZZ) == dup_normal([1, 1, 1, 1], ZZ)
66
+ assert dup_add_term(f, ZZ(1), 4, ZZ) == dup_normal([1, 0, 1, 1, 1], ZZ)
67
+ assert dup_add_term(f, ZZ(1), 5, ZZ) == dup_normal([1, 0, 0, 1, 1, 1], ZZ)
68
+ assert dup_add_term(
69
+ f, ZZ(1), 6, ZZ) == dup_normal([1, 0, 0, 0, 1, 1, 1], ZZ)
70
+
71
+ assert dup_add_term(f, ZZ(-1), 2, ZZ) == dup_normal([1, 1], ZZ)
72
+
73
+
74
+ def test_dmp_add_term():
75
+ assert dmp_add_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, 0, ZZ) == \
76
+ dup_add_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, ZZ)
77
+ assert dmp_add_term(f_0, [[]], 3, 2, ZZ) == f_0
78
+ assert dmp_add_term(F_0, [[]], 3, 2, QQ) == F_0
79
+
80
+
81
+ def test_dup_sub_term():
82
+ f = dup_normal([], ZZ)
83
+
84
+ assert dup_sub_term(f, ZZ(0), 0, ZZ) == dup_normal([], ZZ)
85
+
86
+ assert dup_sub_term(f, ZZ(1), 0, ZZ) == dup_normal([-1], ZZ)
87
+ assert dup_sub_term(f, ZZ(1), 1, ZZ) == dup_normal([-1, 0], ZZ)
88
+ assert dup_sub_term(f, ZZ(1), 2, ZZ) == dup_normal([-1, 0, 0], ZZ)
89
+
90
+ f = dup_normal([1, 1, 1], ZZ)
91
+
92
+ assert dup_sub_term(f, ZZ(2), 0, ZZ) == dup_normal([ 1, 1, -1], ZZ)
93
+ assert dup_sub_term(f, ZZ(2), 1, ZZ) == dup_normal([ 1, -1, 1], ZZ)
94
+ assert dup_sub_term(f, ZZ(2), 2, ZZ) == dup_normal([-1, 1, 1], ZZ)
95
+
96
+ assert dup_sub_term(f, ZZ(1), 3, ZZ) == dup_normal([-1, 1, 1, 1], ZZ)
97
+ assert dup_sub_term(f, ZZ(1), 4, ZZ) == dup_normal([-1, 0, 1, 1, 1], ZZ)
98
+ assert dup_sub_term(f, ZZ(1), 5, ZZ) == dup_normal([-1, 0, 0, 1, 1, 1], ZZ)
99
+ assert dup_sub_term(
100
+ f, ZZ(1), 6, ZZ) == dup_normal([-1, 0, 0, 0, 1, 1, 1], ZZ)
101
+
102
+ assert dup_sub_term(f, ZZ(1), 2, ZZ) == dup_normal([1, 1], ZZ)
103
+
104
+
105
+ def test_dmp_sub_term():
106
+ assert dmp_sub_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, 0, ZZ) == \
107
+ dup_sub_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, ZZ)
108
+ assert dmp_sub_term(f_0, [[]], 3, 2, ZZ) == f_0
109
+ assert dmp_sub_term(F_0, [[]], 3, 2, QQ) == F_0
110
+
111
+
112
+ def test_dup_mul_term():
113
+ f = dup_normal([], ZZ)
114
+
115
+ assert dup_mul_term(f, ZZ(2), 3, ZZ) == dup_normal([], ZZ)
116
+
117
+ f = dup_normal([1, 1], ZZ)
118
+
119
+ assert dup_mul_term(f, ZZ(0), 3, ZZ) == dup_normal([], ZZ)
120
+
121
+ f = dup_normal([1, 2, 3], ZZ)
122
+
123
+ assert dup_mul_term(f, ZZ(2), 0, ZZ) == dup_normal([2, 4, 6], ZZ)
124
+ assert dup_mul_term(f, ZZ(2), 1, ZZ) == dup_normal([2, 4, 6, 0], ZZ)
125
+ assert dup_mul_term(f, ZZ(2), 2, ZZ) == dup_normal([2, 4, 6, 0, 0], ZZ)
126
+ assert dup_mul_term(f, ZZ(2), 3, ZZ) == dup_normal([2, 4, 6, 0, 0, 0], ZZ)
127
+
128
+
129
+ def test_dmp_mul_term():
130
+ assert dmp_mul_term([ZZ(1), ZZ(2), ZZ(3)], ZZ(2), 1, 0, ZZ) == \
131
+ dup_mul_term([ZZ(1), ZZ(2), ZZ(3)], ZZ(2), 1, ZZ)
132
+
133
+ assert dmp_mul_term([[]], [ZZ(2)], 3, 1, ZZ) == [[]]
134
+ assert dmp_mul_term([[ZZ(1)]], [], 3, 1, ZZ) == [[]]
135
+
136
+ assert dmp_mul_term([[ZZ(1), ZZ(2)], [ZZ(3)]], [ZZ(2)], 2, 1, ZZ) == \
137
+ [[ZZ(2), ZZ(4)], [ZZ(6)], [], []]
138
+
139
+ assert dmp_mul_term([[]], [QQ(2, 3)], 3, 1, QQ) == [[]]
140
+ assert dmp_mul_term([[QQ(1, 2)]], [], 3, 1, QQ) == [[]]
141
+
142
+ assert dmp_mul_term([[QQ(1, 5), QQ(2, 5)], [QQ(3, 5)]], [QQ(2, 3)], 2, 1, QQ) == \
143
+ [[QQ(2, 15), QQ(4, 15)], [QQ(6, 15)], [], []]
144
+
145
+
146
+ def test_dup_add_ground():
147
+ f = ZZ.map([1, 2, 3, 4])
148
+ g = ZZ.map([1, 2, 3, 8])
149
+
150
+ assert dup_add_ground(f, ZZ(4), ZZ) == g
151
+
152
+
153
+ def test_dmp_add_ground():
154
+ f = ZZ.map([[1], [2], [3], [4]])
155
+ g = ZZ.map([[1], [2], [3], [8]])
156
+
157
+ assert dmp_add_ground(f, ZZ(4), 1, ZZ) == g
158
+
159
+
160
+ def test_dup_sub_ground():
161
+ f = ZZ.map([1, 2, 3, 4])
162
+ g = ZZ.map([1, 2, 3, 0])
163
+
164
+ assert dup_sub_ground(f, ZZ(4), ZZ) == g
165
+
166
+
167
+ def test_dmp_sub_ground():
168
+ f = ZZ.map([[1], [2], [3], [4]])
169
+ g = ZZ.map([[1], [2], [3], []])
170
+
171
+ assert dmp_sub_ground(f, ZZ(4), 1, ZZ) == g
172
+
173
+
174
+ def test_dup_mul_ground():
175
+ f = dup_normal([], ZZ)
176
+
177
+ assert dup_mul_ground(f, ZZ(2), ZZ) == dup_normal([], ZZ)
178
+
179
+ f = dup_normal([1, 2, 3], ZZ)
180
+
181
+ assert dup_mul_ground(f, ZZ(0), ZZ) == dup_normal([], ZZ)
182
+ assert dup_mul_ground(f, ZZ(2), ZZ) == dup_normal([2, 4, 6], ZZ)
183
+
184
+
185
+ def test_dmp_mul_ground():
186
+ assert dmp_mul_ground(f_0, ZZ(2), 2, ZZ) == [
187
+ [[ZZ(2), ZZ(4), ZZ(6)], [ZZ(4)]],
188
+ [[ZZ(6)]],
189
+ [[ZZ(8), ZZ(10), ZZ(12)], [ZZ(2), ZZ(4), ZZ(2)], [ZZ(2)]]
190
+ ]
191
+
192
+ assert dmp_mul_ground(F_0, QQ(1, 2), 2, QQ) == [
193
+ [[QQ(1, 14), QQ(2, 14), QQ(3, 14)], [QQ(2, 14)]],
194
+ [[QQ(3, 14)]],
195
+ [[QQ(4, 14), QQ(5, 14), QQ(6, 14)], [QQ(1, 14), QQ(2, 14),
196
+ QQ(1, 14)], [QQ(1, 14)]]
197
+ ]
198
+
199
+
200
+ def test_dup_quo_ground():
201
+ raises(ZeroDivisionError, lambda: dup_quo_ground(dup_normal([1, 2,
202
+ 3], ZZ), ZZ(0), ZZ))
203
+
204
+ f = dup_normal([], ZZ)
205
+
206
+ assert dup_quo_ground(f, ZZ(3), ZZ) == dup_normal([], ZZ)
207
+
208
+ f = dup_normal([6, 2, 8], ZZ)
209
+
210
+ assert dup_quo_ground(f, ZZ(1), ZZ) == f
211
+ assert dup_quo_ground(f, ZZ(2), ZZ) == dup_normal([3, 1, 4], ZZ)
212
+
213
+ assert dup_quo_ground(f, ZZ(3), ZZ) == dup_normal([2, 0, 2], ZZ)
214
+
215
+ f = dup_normal([6, 2, 8], QQ)
216
+
217
+ assert dup_quo_ground(f, QQ(1), QQ) == f
218
+ assert dup_quo_ground(f, QQ(2), QQ) == [QQ(3), QQ(1), QQ(4)]
219
+ assert dup_quo_ground(f, QQ(7), QQ) == [QQ(6, 7), QQ(2, 7), QQ(8, 7)]
220
+
221
+
222
+ def test_dup_exquo_ground():
223
+ raises(ZeroDivisionError, lambda: dup_exquo_ground(dup_normal([1,
224
+ 2, 3], ZZ), ZZ(0), ZZ))
225
+ raises(ExactQuotientFailed, lambda: dup_exquo_ground(dup_normal([1,
226
+ 2, 3], ZZ), ZZ(3), ZZ))
227
+
228
+ f = dup_normal([], ZZ)
229
+
230
+ assert dup_exquo_ground(f, ZZ(3), ZZ) == dup_normal([], ZZ)
231
+
232
+ f = dup_normal([6, 2, 8], ZZ)
233
+
234
+ assert dup_exquo_ground(f, ZZ(1), ZZ) == f
235
+ assert dup_exquo_ground(f, ZZ(2), ZZ) == dup_normal([3, 1, 4], ZZ)
236
+
237
+ f = dup_normal([6, 2, 8], QQ)
238
+
239
+ assert dup_exquo_ground(f, QQ(1), QQ) == f
240
+ assert dup_exquo_ground(f, QQ(2), QQ) == [QQ(3), QQ(1), QQ(4)]
241
+ assert dup_exquo_ground(f, QQ(7), QQ) == [QQ(6, 7), QQ(2, 7), QQ(8, 7)]
242
+
243
+
244
+ def test_dmp_quo_ground():
245
+ f = dmp_normal([[6], [2], [8]], 1, ZZ)
246
+
247
+ assert dmp_quo_ground(f, ZZ(1), 1, ZZ) == f
248
+ assert dmp_quo_ground(
249
+ f, ZZ(2), 1, ZZ) == dmp_normal([[3], [1], [4]], 1, ZZ)
250
+
251
+ assert dmp_normal(dmp_quo_ground(
252
+ f, ZZ(3), 1, ZZ), 1, ZZ) == dmp_normal([[2], [], [2]], 1, ZZ)
253
+
254
+
255
+ def test_dmp_exquo_ground():
256
+ f = dmp_normal([[6], [2], [8]], 1, ZZ)
257
+
258
+ assert dmp_exquo_ground(f, ZZ(1), 1, ZZ) == f
259
+ assert dmp_exquo_ground(
260
+ f, ZZ(2), 1, ZZ) == dmp_normal([[3], [1], [4]], 1, ZZ)
261
+
262
+
263
+ def test_dup_lshift():
264
+ assert dup_lshift([], 3, ZZ) == []
265
+ assert dup_lshift([1], 3, ZZ) == [1, 0, 0, 0]
266
+
267
+
268
+ def test_dup_rshift():
269
+ assert dup_rshift([], 3, ZZ) == []
270
+ assert dup_rshift([1, 0, 0, 0], 3, ZZ) == [1]
271
+
272
+
273
+ def test_dup_abs():
274
+ assert dup_abs([], ZZ) == []
275
+ assert dup_abs([ZZ( 1)], ZZ) == [ZZ(1)]
276
+ assert dup_abs([ZZ(-7)], ZZ) == [ZZ(7)]
277
+ assert dup_abs([ZZ(-1), ZZ(2), ZZ(3)], ZZ) == [ZZ(1), ZZ(2), ZZ(3)]
278
+
279
+ assert dup_abs([], QQ) == []
280
+ assert dup_abs([QQ( 1, 2)], QQ) == [QQ(1, 2)]
281
+ assert dup_abs([QQ(-7, 3)], QQ) == [QQ(7, 3)]
282
+ assert dup_abs(
283
+ [QQ(-1, 7), QQ(2, 7), QQ(3, 7)], QQ) == [QQ(1, 7), QQ(2, 7), QQ(3, 7)]
284
+
285
+
286
+ def test_dmp_abs():
287
+ assert dmp_abs([ZZ(-1)], 0, ZZ) == [ZZ(1)]
288
+ assert dmp_abs([QQ(-1, 2)], 0, QQ) == [QQ(1, 2)]
289
+
290
+ assert dmp_abs([[[]]], 2, ZZ) == [[[]]]
291
+ assert dmp_abs([[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
292
+ assert dmp_abs([[[ZZ(-7)]]], 2, ZZ) == [[[ZZ(7)]]]
293
+
294
+ assert dmp_abs([[[]]], 2, QQ) == [[[]]]
295
+ assert dmp_abs([[[QQ(1, 2)]]], 2, QQ) == [[[QQ(1, 2)]]]
296
+ assert dmp_abs([[[QQ(-7, 9)]]], 2, QQ) == [[[QQ(7, 9)]]]
297
+
298
+
299
+ def test_dup_neg():
300
+ assert dup_neg([], ZZ) == []
301
+ assert dup_neg([ZZ(1)], ZZ) == [ZZ(-1)]
302
+ assert dup_neg([ZZ(-7)], ZZ) == [ZZ(7)]
303
+ assert dup_neg([ZZ(-1), ZZ(2), ZZ(3)], ZZ) == [ZZ(1), ZZ(-2), ZZ(-3)]
304
+
305
+ assert dup_neg([], QQ) == []
306
+ assert dup_neg([QQ(1, 2)], QQ) == [QQ(-1, 2)]
307
+ assert dup_neg([QQ(-7, 9)], QQ) == [QQ(7, 9)]
308
+ assert dup_neg([QQ(
309
+ -1, 7), QQ(2, 7), QQ(3, 7)], QQ) == [QQ(1, 7), QQ(-2, 7), QQ(-3, 7)]
310
+
311
+
312
+ def test_dmp_neg():
313
+ assert dmp_neg([ZZ(-1)], 0, ZZ) == [ZZ(1)]
314
+ assert dmp_neg([QQ(-1, 2)], 0, QQ) == [QQ(1, 2)]
315
+
316
+ assert dmp_neg([[[]]], 2, ZZ) == [[[]]]
317
+ assert dmp_neg([[[ZZ(1)]]], 2, ZZ) == [[[ZZ(-1)]]]
318
+ assert dmp_neg([[[ZZ(-7)]]], 2, ZZ) == [[[ZZ(7)]]]
319
+
320
+ assert dmp_neg([[[]]], 2, QQ) == [[[]]]
321
+ assert dmp_neg([[[QQ(1, 9)]]], 2, QQ) == [[[QQ(-1, 9)]]]
322
+ assert dmp_neg([[[QQ(-7, 9)]]], 2, QQ) == [[[QQ(7, 9)]]]
323
+
324
+
325
+ def test_dup_add():
326
+ assert dup_add([], [], ZZ) == []
327
+ assert dup_add([ZZ(1)], [], ZZ) == [ZZ(1)]
328
+ assert dup_add([], [ZZ(1)], ZZ) == [ZZ(1)]
329
+ assert dup_add([ZZ(1)], [ZZ(1)], ZZ) == [ZZ(2)]
330
+ assert dup_add([ZZ(1)], [ZZ(2)], ZZ) == [ZZ(3)]
331
+
332
+ assert dup_add([ZZ(1), ZZ(2)], [ZZ(1)], ZZ) == [ZZ(1), ZZ(3)]
333
+ assert dup_add([ZZ(1)], [ZZ(1), ZZ(2)], ZZ) == [ZZ(1), ZZ(3)]
334
+
335
+ assert dup_add([ZZ(1), ZZ(
336
+ 2), ZZ(3)], [ZZ(8), ZZ(9), ZZ(10)], ZZ) == [ZZ(9), ZZ(11), ZZ(13)]
337
+
338
+ assert dup_add([], [], QQ) == []
339
+ assert dup_add([QQ(1, 2)], [], QQ) == [QQ(1, 2)]
340
+ assert dup_add([], [QQ(1, 2)], QQ) == [QQ(1, 2)]
341
+ assert dup_add([QQ(1, 4)], [QQ(1, 4)], QQ) == [QQ(1, 2)]
342
+ assert dup_add([QQ(1, 4)], [QQ(1, 2)], QQ) == [QQ(3, 4)]
343
+
344
+ assert dup_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ) == [QQ(1, 2), QQ(5, 3)]
345
+ assert dup_add([QQ(1)], [QQ(1, 2), QQ(2, 3)], QQ) == [QQ(1, 2), QQ(5, 3)]
346
+
347
+ assert dup_add([QQ(1, 7), QQ(2, 7), QQ(3, 7)], [QQ(
348
+ 8, 7), QQ(9, 7), QQ(10, 7)], QQ) == [QQ(9, 7), QQ(11, 7), QQ(13, 7)]
349
+
350
+
351
+ def test_dmp_add():
352
+ assert dmp_add([ZZ(1), ZZ(2)], [ZZ(1)], 0, ZZ) == \
353
+ dup_add([ZZ(1), ZZ(2)], [ZZ(1)], ZZ)
354
+ assert dmp_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], 0, QQ) == \
355
+ dup_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ)
356
+
357
+ assert dmp_add([[[]]], [[[]]], 2, ZZ) == [[[]]]
358
+ assert dmp_add([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[ZZ(1)]]]
359
+ assert dmp_add([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
360
+ assert dmp_add([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(3)]]]
361
+ assert dmp_add([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(3)]]]
362
+
363
+ assert dmp_add([[[]]], [[[]]], 2, QQ) == [[[]]]
364
+ assert dmp_add([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[QQ(1, 2)]]]
365
+ assert dmp_add([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[QQ(1, 2)]]]
366
+ assert dmp_add([[[QQ(2, 7)]]], [[[QQ(1, 7)]]], 2, QQ) == [[[QQ(3, 7)]]]
367
+ assert dmp_add([[[QQ(1, 7)]]], [[[QQ(2, 7)]]], 2, QQ) == [[[QQ(3, 7)]]]
368
+
369
+
370
+ def test_dup_sub():
371
+ assert dup_sub([], [], ZZ) == []
372
+ assert dup_sub([ZZ(1)], [], ZZ) == [ZZ(1)]
373
+ assert dup_sub([], [ZZ(1)], ZZ) == [ZZ(-1)]
374
+ assert dup_sub([ZZ(1)], [ZZ(1)], ZZ) == []
375
+ assert dup_sub([ZZ(1)], [ZZ(2)], ZZ) == [ZZ(-1)]
376
+
377
+ assert dup_sub([ZZ(1), ZZ(2)], [ZZ(1)], ZZ) == [ZZ(1), ZZ(1)]
378
+ assert dup_sub([ZZ(1)], [ZZ(1), ZZ(2)], ZZ) == [ZZ(-1), ZZ(-1)]
379
+
380
+ assert dup_sub([ZZ(3), ZZ(
381
+ 2), ZZ(1)], [ZZ(8), ZZ(9), ZZ(10)], ZZ) == [ZZ(-5), ZZ(-7), ZZ(-9)]
382
+
383
+ assert dup_sub([], [], QQ) == []
384
+ assert dup_sub([QQ(1, 2)], [], QQ) == [QQ(1, 2)]
385
+ assert dup_sub([], [QQ(1, 2)], QQ) == [QQ(-1, 2)]
386
+ assert dup_sub([QQ(1, 3)], [QQ(1, 3)], QQ) == []
387
+ assert dup_sub([QQ(1, 3)], [QQ(2, 3)], QQ) == [QQ(-1, 3)]
388
+
389
+ assert dup_sub([QQ(1, 7), QQ(2, 7)], [QQ(1)], QQ) == [QQ(1, 7), QQ(-5, 7)]
390
+ assert dup_sub([QQ(1)], [QQ(1, 7), QQ(2, 7)], QQ) == [QQ(-1, 7), QQ(5, 7)]
391
+
392
+ assert dup_sub([QQ(3, 7), QQ(2, 7), QQ(1, 7)], [QQ(
393
+ 8, 7), QQ(9, 7), QQ(10, 7)], QQ) == [QQ(-5, 7), QQ(-7, 7), QQ(-9, 7)]
394
+
395
+
396
+ def test_dmp_sub():
397
+ assert dmp_sub([ZZ(1), ZZ(2)], [ZZ(1)], 0, ZZ) == \
398
+ dup_sub([ZZ(1), ZZ(2)], [ZZ(1)], ZZ)
399
+ assert dmp_sub([QQ(1, 2), QQ(2, 3)], [QQ(1)], 0, QQ) == \
400
+ dup_sub([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ)
401
+
402
+ assert dmp_sub([[[]]], [[[]]], 2, ZZ) == [[[]]]
403
+ assert dmp_sub([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[ZZ(1)]]]
404
+ assert dmp_sub([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(-1)]]]
405
+ assert dmp_sub([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
406
+ assert dmp_sub([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(-1)]]]
407
+
408
+ assert dmp_sub([[[]]], [[[]]], 2, QQ) == [[[]]]
409
+ assert dmp_sub([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[QQ(1, 2)]]]
410
+ assert dmp_sub([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[QQ(-1, 2)]]]
411
+ assert dmp_sub([[[QQ(2, 7)]]], [[[QQ(1, 7)]]], 2, QQ) == [[[QQ(1, 7)]]]
412
+ assert dmp_sub([[[QQ(1, 7)]]], [[[QQ(2, 7)]]], 2, QQ) == [[[QQ(-1, 7)]]]
413
+
414
+
415
+ def test_dup_add_mul():
416
+ assert dup_add_mul([ZZ(1), ZZ(2), ZZ(3)], [ZZ(3), ZZ(2), ZZ(1)],
417
+ [ZZ(1), ZZ(2)], ZZ) == [ZZ(3), ZZ(9), ZZ(7), ZZ(5)]
418
+ assert dmp_add_mul([[ZZ(1), ZZ(2)], [ZZ(3)]], [[ZZ(3)], [ZZ(2), ZZ(1)]],
419
+ [[ZZ(1)], [ZZ(2)]], 1, ZZ) == [[ZZ(3)], [ZZ(3), ZZ(9)], [ZZ(4), ZZ(5)]]
420
+
421
+
422
+ def test_dup_sub_mul():
423
+ assert dup_sub_mul([ZZ(1), ZZ(2), ZZ(3)], [ZZ(3), ZZ(2), ZZ(1)],
424
+ [ZZ(1), ZZ(2)], ZZ) == [ZZ(-3), ZZ(-7), ZZ(-3), ZZ(1)]
425
+ assert dmp_sub_mul([[ZZ(1), ZZ(2)], [ZZ(3)]], [[ZZ(3)], [ZZ(2), ZZ(1)]],
426
+ [[ZZ(1)], [ZZ(2)]], 1, ZZ) == [[ZZ(-3)], [ZZ(-1), ZZ(-5)], [ZZ(-4), ZZ(1)]]
427
+
428
+
429
+ def test_dup_mul():
430
+ assert dup_mul([], [], ZZ) == []
431
+ assert dup_mul([], [ZZ(1)], ZZ) == []
432
+ assert dup_mul([ZZ(1)], [], ZZ) == []
433
+ assert dup_mul([ZZ(1)], [ZZ(1)], ZZ) == [ZZ(1)]
434
+ assert dup_mul([ZZ(5)], [ZZ(7)], ZZ) == [ZZ(35)]
435
+
436
+ assert dup_mul([], [], QQ) == []
437
+ assert dup_mul([], [QQ(1, 2)], QQ) == []
438
+ assert dup_mul([QQ(1, 2)], [], QQ) == []
439
+ assert dup_mul([QQ(1, 2)], [QQ(4, 7)], QQ) == [QQ(2, 7)]
440
+ assert dup_mul([QQ(5, 7)], [QQ(3, 7)], QQ) == [QQ(15, 49)]
441
+
442
+ f = dup_normal([3, 0, 0, 6, 1, 2], ZZ)
443
+ g = dup_normal([4, 0, 1, 0], ZZ)
444
+ h = dup_normal([12, 0, 3, 24, 4, 14, 1, 2, 0], ZZ)
445
+
446
+ assert dup_mul(f, g, ZZ) == h
447
+ assert dup_mul(g, f, ZZ) == h
448
+
449
+ f = dup_normal([2, 0, 0, 1, 7], ZZ)
450
+ h = dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
451
+
452
+ assert dup_mul(f, f, ZZ) == h
453
+
454
+ K = FF(6)
455
+
456
+ assert dup_mul([K(2), K(1)], [K(3), K(4)], K) == [K(5), K(4)]
457
+
458
+ p1 = dup_normal([79, -1, 78, -94, -10, 11, 32, -19, 78, 2, -89, 30, 73, 42,
459
+ 85, 77, 83, -30, -34, -2, 95, -81, 37, -49, -46, -58, -16, 37, 35, -11,
460
+ -57, -15, -31, 67, -20, 27, 76, 2, 70, 67, -65, 65, -26, -93, -44, -12,
461
+ -92, 57, -90, -57, -11, -67, -98, -69, 97, -41, 89, 33, 89, -50, 81,
462
+ -31, 60, -27, 43, 29, -77, 44, 21, -91, 32, -57, 33, 3, 53, -51, -38,
463
+ -99, -84, 23, -50, 66, -100, 1, -75, -25, 27, -60, 98, -51, -87, 6, 8,
464
+ 78, -28, -95, -88, 12, -35, 26, -9, 16, -92, 55, -7, -86, 68, -39, -46,
465
+ 84, 94, 45, 60, 92, 68, -75, -74, -19, 8, 75, 78, 91, 57, 34, 14, -3,
466
+ -49, 65, 78, -18, 6, -29, -80, -98, 17, 13, 58, 21, 20, 9, 37, 7, -30,
467
+ -53, -20, 34, 67, -42, 89, -22, 73, 43, -6, 5, 51, -8, -15, -52, -22,
468
+ -58, -72, -3, 43, -92, 82, 83, -2, -13, -23, -60, 16, -94, -8, -28,
469
+ -95, -72, 63, -90, 76, 6, -43, -100, -59, 76, 3, 3, 46, -85, 75, 62,
470
+ -71, -76, 88, 97, -72, -1, 30, -64, 72, -48, 14, -78, 58, 63, -91, 24,
471
+ -87, -27, -80, -100, -44, 98, 70, 100, -29, -38, 11, 77, 100, 52, 86,
472
+ 65, -5, -42, -81, -38, -42, 43, -2, -70, -63, -52], ZZ)
473
+ p2 = dup_normal([65, -19, -47, 1, 90, 81, -15, -34, 25, -75, 9, -83, 50, -5,
474
+ -44, 31, 1, 70, -7, 78, 74, 80, 85, 65, 21, 41, 66, 19, -40, 63, -21,
475
+ -27, 32, 69, 83, 34, -35, 14, 81, 57, -75, 32, -67, -89, -100, -61, 46,
476
+ 84, -78, -29, -50, -94, -24, -32, -68, -16, 100, -7, -72, -89, 35, 82,
477
+ 58, 81, -92, 62, 5, -47, -39, -58, -72, -13, 84, 44, 55, -25, 48, -54,
478
+ -31, -56, -11, -50, -84, 10, 67, 17, 13, -14, 61, 76, -64, -44, -40,
479
+ -96, 11, -11, -94, 2, 6, 27, -6, 68, -54, 66, -74, -14, -1, -24, -73,
480
+ 96, 89, -11, -89, 56, -53, 72, -43, 96, 25, 63, -31, 29, 68, 83, 91,
481
+ -93, -19, -38, -40, 40, -12, -19, -79, 44, 100, -66, -29, -77, 62, 39,
482
+ -8, 11, -97, 14, 87, 64, 21, -18, 13, 15, -59, -75, -99, -88, 57, 54,
483
+ 56, -67, 6, -63, -59, -14, 28, 87, -20, -39, 84, -91, -2, 49, -75, 11,
484
+ -24, -95, 36, 66, 5, 25, -72, -40, 86, 90, 37, -33, 57, -35, 29, -18,
485
+ 4, -79, 64, -17, -27, 21, 29, -5, -44, -87, -24, 52, 78, 11, -23, -53,
486
+ 36, 42, 21, -68, 94, -91, -51, -21, 51, -76, 72, 31, 24, -48, -80, -9,
487
+ 37, -47, -6, -8, -63, -91, 79, -79, -100, 38, -20, 38, 100, 83, -90,
488
+ 87, 63, -36, 82, -19, 18, -98, -38, 26, 98, -70, 79, 92, 12, 12, 70,
489
+ 74, 36, 48, -13, 31, 31, -47, -71, -12, -64, 36, -42, 32, -86, 60, 83,
490
+ 70, 55, 0, 1, 29, -35, 8, -82, 8, -73, -46, -50, 43, 48, -5, -86, -72,
491
+ 44, -90, 19, 19, 5, -20, 97, -13, -66, -5, 5, -69, 64, -30, 41, 51, 36,
492
+ 13, -99, -61, 94, -12, 74, 98, 68, 24, 46, -97, -87, -6, -27, 82, 62,
493
+ -11, -77, 86, 66, -47, -49, -50, 13, 18, 89, -89, 46, -80, 13, 98, -35,
494
+ -36, -25, 12, 20, 26, -52, 79, 27, 79, 100, 8, 62, -58, -28, 37], ZZ)
495
+ res = dup_normal([5135, -1566, 1376, -7466, 4579, 11710, 8001, -7183,
496
+ -3737, -7439, 345, -10084, 24522, -1201, 1070, -10245, 9582, 9264,
497
+ 1903, 23312, 18953, 10037, -15268, -5450, 6442, -6243, -3777, 5110,
498
+ 10936, -16649, -6022, 16255, 31300, 24818, 31922, 32760, 7854, 27080,
499
+ 15766, 29596, 7139, 31945, -19810, 465, -38026, -3971, 9641, 465,
500
+ -19375, 5524, -30112, -11960, -12813, 13535, 30670, 5925, -43725,
501
+ -14089, 11503, -22782, 6371, 43881, 37465, -33529, -33590, -39798,
502
+ -37854, -18466, -7908, -35825, -26020, -36923, -11332, -5699, 25166,
503
+ -3147, 19885, 12962, -20659, -1642, 27723, -56331, -24580, -11010,
504
+ -20206, 20087, -23772, -16038, 38580, 20901, -50731, 32037, -4299,
505
+ 26508, 18038, -28357, 31846, -7405, -20172, -15894, 2096, 25110,
506
+ -45786, 45918, -55333, -31928, -49428, -29824, -58796, -24609, -15408,
507
+ 69, -35415, -18439, 10123, -20360, -65949, 33356, -20333, 26476,
508
+ -32073, 33621, 930, 28803, -42791, 44716, 38164, 12302, -1739, 11421,
509
+ 73385, -7613, 14297, 38155, -414, 77587, 24338, -21415, 29367, 42639,
510
+ 13901, -288, 51027, -11827, 91260, 43407, 88521, -15186, 70572, -12049,
511
+ 5090, -12208, -56374, 15520, -623, -7742, 50825, 11199, -14894, 40892,
512
+ 59591, -31356, -28696, -57842, -87751, -33744, -28436, -28945, -40287,
513
+ 37957, -35638, 33401, -61534, 14870, 40292, 70366, -10803, 102290,
514
+ -71719, -85251, 7902, -22409, 75009, 99927, 35298, -1175, -762, -34744,
515
+ -10587, -47574, -62629, -19581, -43659, -54369, -32250, -39545, 15225,
516
+ -24454, 11241, -67308, -30148, 39929, 37639, 14383, -73475, -77636,
517
+ -81048, -35992, 41601, -90143, 76937, -8112, 56588, 9124, -40094,
518
+ -32340, 13253, 10898, -51639, 36390, 12086, -1885, 100714, -28561,
519
+ -23784, -18735, 18916, 16286, 10742, -87360, -13697, 10689, -19477,
520
+ -29770, 5060, 20189, -8297, 112407, 47071, 47743, 45519, -4109, 17468,
521
+ -68831, 78325, -6481, -21641, -19459, 30919, 96115, 8607, 53341, 32105,
522
+ -16211, 23538, 57259, -76272, -40583, 62093, 38511, -34255, -40665,
523
+ -40604, -37606, -15274, 33156, -13885, 103636, 118678, -14101, -92682,
524
+ -100791, 2634, 63791, 98266, 19286, -34590, -21067, -71130, 25380,
525
+ -40839, -27614, -26060, 52358, -15537, 27138, -6749, 36269, -33306,
526
+ 13207, -91084, -5540, -57116, 69548, 44169, -57742, -41234, -103327,
527
+ -62904, -8566, 41149, -12866, 71188, 23980, 1838, 58230, 73950, 5594,
528
+ 43113, -8159, -15925, 6911, 85598, -75016, -16214, -62726, -39016,
529
+ 8618, -63882, -4299, 23182, 49959, 49342, -3238, -24913, -37138, 78361,
530
+ 32451, 6337, -11438, -36241, -37737, 8169, -3077, -24829, 57953, 53016,
531
+ -31511, -91168, 12599, -41849, 41576, 55275, -62539, 47814, -62319,
532
+ 12300, -32076, -55137, -84881, -27546, 4312, -3433, -54382, 113288,
533
+ -30157, 74469, 18219, 79880, -2124, 98911, 17655, -33499, -32861,
534
+ 47242, -37393, 99765, 14831, -44483, 10800, -31617, -52710, 37406,
535
+ 22105, 29704, -20050, 13778, 43683, 36628, 8494, 60964, -22644, 31550,
536
+ -17693, 33805, -124879, -12302, 19343, 20400, -30937, -21574, -34037,
537
+ -33380, 56539, -24993, -75513, -1527, 53563, 65407, -101, 53577, 37991,
538
+ 18717, -23795, -8090, -47987, -94717, 41967, 5170, -14815, -94311,
539
+ 17896, -17734, -57718, -774, -38410, 24830, 29682, 76480, 58802,
540
+ -46416, -20348, -61353, -68225, -68306, 23822, -31598, 42972, 36327,
541
+ 28968, -65638, -21638, 24354, -8356, 26777, 52982, -11783, -44051,
542
+ -26467, -44721, -28435, -53265, -25574, -2669, 44155, 22946, -18454,
543
+ -30718, -11252, 58420, 8711, 67447, 4425, 41749, 67543, 43162, 11793,
544
+ -41907, 20477, -13080, 6559, -6104, -13244, 42853, 42935, 29793, 36730,
545
+ -28087, 28657, 17946, 7503, 7204, 21491, -27450, -24241, -98156,
546
+ -18082, -42613, -24928, 10775, -14842, -44127, 55910, 14777, 31151, -2194,
547
+ 39206, -2100, -4211, 11827, -8918, -19471, 72567, 36447, -65590, -34861,
548
+ -17147, -45303, 9025, -7333, -35473, 11101, 11638, 3441, 6626, -41800,
549
+ 9416, 13679, 33508, 40502, -60542, 16358, 8392, -43242, -35864, -34127,
550
+ -48721, 35878, 30598, 28630, 20279, -19983, -14638, -24455, -1851, -11344,
551
+ 45150, 42051, 26034, -28889, -32382, -3527, -14532, 22564, -22346, 477,
552
+ 11706, 28338, -25972, -9185, -22867, -12522, 32120, -4424, 11339, -33913,
553
+ -7184, 5101, -23552, -17115, -31401, -6104, 21906, 25708, 8406, 6317,
554
+ -7525, 5014, 20750, 20179, 22724, 11692, 13297, 2493, -253, -16841, -17339,
555
+ -6753, -4808, 2976, -10881, -10228, -13816, -12686, 1385, 2316, 2190, -875,
556
+ -1924], ZZ)
557
+
558
+ assert dup_mul(p1, p2, ZZ) == res
559
+
560
+ p1 = dup_normal([83, -61, -86, -24, 12, 43, -88, -9, 42, 55, -66, 74, 95,
561
+ -25, -12, 68, -99, 4, 45, 6, -15, -19, 78, 65, -55, 47, -13, 17, 86,
562
+ 81, -58, -27, 50, -40, -24, 39, -41, -92, 75, 90, -1, 40, -15, -27,
563
+ -35, 68, 70, -64, -40, 78, -88, -58, -39, 69, 46, 12, 28, -94, -37,
564
+ -50, -80, -96, -61, 25, 1, 71, 4, 12, 48, 4, 34, -47, -75, 5, 48, 82,
565
+ 88, 23, 98, 35, 17, -10, 48, -61, -95, 47, 65, -19, -66, -57, -6, -51,
566
+ -42, -89, 66, -13, 18, 37, 90, -23, 72, 96, -53, 0, 40, -73, -52, -68,
567
+ 32, -25, -53, 79, -52, 18, 44, 73, -81, 31, -90, 70, 3, 36, 48, 76,
568
+ -24, -44, 23, 98, -4, 73, 69, 88, -70, 14, -68, 94, -78, -15, -64, -97,
569
+ -70, -35, 65, 88, 49, -53, -7, 12, -45, -7, 59, -94, 99, -2, 67, -60,
570
+ -71, 29, -62, -77, 1, 51, 17, 80, -20, -47, -19, 24, -9, 39, -23, 21,
571
+ -84, 10, 84, 56, -17, -21, -66, 85, 70, 46, -51, -22, -95, 78, -60,
572
+ -96, -97, -45, 72, 35, 30, -61, -92, -93, -60, -61, 4, -4, -81, -73,
573
+ 46, 53, -11, 26, 94, 45, 14, -78, 55, 84, -68, 98, 60, 23, 100, -63,
574
+ 68, 96, -16, 3, 56, 21, -58, 62, -67, 66, 85, 41, -79, -22, 97, -67,
575
+ 82, 82, -96, -20, -7, 48, -67, 48, -9, -39, 78], ZZ)
576
+ p2 = dup_normal([52, 88, 76, 66, 9, -64, 46, -20, -28, 69, 60, 96, -36,
577
+ -92, -30, -11, -35, 35, 55, 63, -92, -7, 25, -58, 74, 55, -6, 4, 47,
578
+ -92, -65, 67, -45, 74, -76, 59, -6, 69, 39, 24, -71, -7, 39, -45, 60,
579
+ -68, 98, 97, -79, 17, 4, 94, -64, 68, -100, -96, -2, 3, 22, 96, 54,
580
+ -77, -86, 67, 6, 57, 37, 40, 89, -78, 64, -94, -45, -92, 57, 87, -26,
581
+ 36, 19, 97, 25, 77, -87, 24, 43, -5, 35, 57, 83, 71, 35, 63, 61, 96,
582
+ -22, 8, -1, 96, 43, 45, 94, -93, 36, 71, -41, -99, 85, -48, 59, 52,
583
+ -17, 5, 87, -16, -68, -54, 76, -18, 100, 91, -42, -70, -66, -88, -12,
584
+ 1, 95, -82, 52, 43, -29, 3, 12, 72, -99, -43, -32, -93, -51, 16, -20,
585
+ -12, -11, 5, 33, -38, 93, -5, -74, 25, 74, -58, 93, 59, -63, -86, 63,
586
+ -20, -4, -74, -73, -95, 29, -28, 93, -91, -2, -38, -62, 77, -58, -85,
587
+ -28, 95, 38, 19, -69, 86, 94, 25, -2, -4, 47, 34, -59, 35, -48, 29,
588
+ -63, -53, 34, 29, 66, 73, 6, 92, -84, 89, 15, 81, 93, 97, 51, -72, -78,
589
+ 25, 60, 90, -45, 39, 67, -84, -62, 57, 26, -32, -56, -14, -83, 76, 5,
590
+ -2, 99, -100, 28, 46, 94, -7, 53, -25, 16, -23, -36, 89, -78, -63, 31,
591
+ 1, 84, -99, -52, 76, 48, 90, -76, 44, -19, 54, -36, -9, -73, -100, -69,
592
+ 31, 42, 25, -39, 76, -26, -8, -14, 51, 3, 37, 45, 2, -54, 13, -34, -92,
593
+ 17, -25, -65, 53, -63, 30, 4, -70, -67, 90, 52, 51, 18, -3, 31, -45,
594
+ -9, 59, 63, -87, 22, -32, 29, -38, 21, 36, -82, 27, -11], ZZ)
595
+ res = dup_normal([4316, 4132, -3532, -7974, -11303, -10069, 5484, -3330,
596
+ -5874, 7734, 4673, 11327, -9884, -8031, 17343, 21035, -10570, -9285,
597
+ 15893, 3780, -14083, 8819, 17592, 10159, 7174, -11587, 8598, -16479,
598
+ 3602, 25596, 9781, 12163, 150, 18749, -21782, -12307, 27578, -2757,
599
+ -12573, 12565, 6345, -18956, 19503, -15617, 1443, -16778, 36851, 23588,
600
+ -28474, 5749, 40695, -7521, -53669, -2497, -18530, 6770, 57038, 3926,
601
+ -6927, -15399, 1848, -64649, -27728, 3644, 49608, 15187, -8902, -9480,
602
+ -7398, -40425, 4824, 23767, -7594, -6905, 33089, 18786, 12192, 24670,
603
+ 31114, 35334, -4501, -14676, 7107, -59018, -21352, 20777, 19661, 20653,
604
+ 33754, -885, -43758, 6269, 51897, -28719, -97488, -9527, 13746, 11644,
605
+ 17644, -21720, 23782, -10481, 47867, 20752, 33810, -1875, 39918, -7710,
606
+ -40840, 19808, -47075, 23066, 46616, 25201, 9287, 35436, -1602, 9645,
607
+ -11978, 13273, 15544, 33465, 20063, 44539, 11687, 27314, -6538, -37467,
608
+ 14031, 32970, -27086, 41323, 29551, 65910, -39027, -37800, -22232,
609
+ 8212, 46316, -28981, -55282, 50417, -44929, -44062, 73879, 37573,
610
+ -2596, -10877, -21893, -133218, -33707, -25753, -9531, 17530, 61126,
611
+ 2748, -56235, 43874, -10872, -90459, -30387, 115267, -7264, -44452,
612
+ 122626, 14839, -599, 10337, 57166, -67467, -54957, 63669, 1202, 18488,
613
+ 52594, 7205, -97822, 612, 78069, -5403, -63562, 47236, 36873, -154827,
614
+ -26188, 82427, -39521, 5628, 7416, 5276, -53095, 47050, 26121, -42207,
615
+ 79021, -13035, 2499, -66943, 29040, -72355, -23480, 23416, -12885,
616
+ -44225, -42688, -4224, 19858, 55299, 15735, 11465, 101876, -39169,
617
+ 51786, 14723, 43280, -68697, 16410, 92295, 56767, 7183, 111850, 4550,
618
+ 115451, -38443, -19642, -35058, 10230, 93829, 8925, 63047, 3146, 29250,
619
+ 8530, 5255, -98117, -115517, -76817, -8724, 41044, 1312, -35974, 79333,
620
+ -28567, 7547, -10580, -24559, -16238, 10794, -3867, 24848, 57770,
621
+ -51536, -35040, 71033, 29853, 62029, -7125, -125585, -32169, -47907,
622
+ 156811, -65176, -58006, -15757, -57861, 11963, 30225, -41901, -41681,
623
+ 31310, 27982, 18613, 61760, 60746, -59096, 33499, 30097, -17997, 24032,
624
+ 56442, -83042, 23747, -20931, -21978, -158752, -9883, -73598, -7987,
625
+ -7333, -125403, -116329, 30585, 53281, 51018, -29193, 88575, 8264,
626
+ -40147, -16289, 113088, 12810, -6508, 101552, -13037, 34440, -41840,
627
+ 101643, 24263, 80532, 61748, 65574, 6423, -20672, 6591, -10834, -71716,
628
+ 86919, -92626, 39161, 28490, 81319, 46676, 106720, 43530, 26998, 57456,
629
+ -8862, 60989, 13982, 3119, -2224, 14743, 55415, -49093, -29303, 28999,
630
+ 1789, 55953, -84043, -7780, -65013, 57129, -47251, 61484, 61994,
631
+ -78361, -82778, 22487, -26894, 9756, -74637, -15519, -4360, 30115,
632
+ 42433, 35475, 15286, 69768, 21509, -20214, 78675, -21163, 13596, 11443,
633
+ -10698, -53621, -53867, -24155, 64500, -42784, -33077, -16500, 873,
634
+ -52788, 14546, -38011, 36974, -39849, -34029, -94311, 83068, -50437,
635
+ -26169, -46746, 59185, 42259, -101379, -12943, 30089, -59086, 36271,
636
+ 22723, -30253, -52472, -70826, -23289, 3331, -31687, 14183, -857,
637
+ -28627, 35246, -51284, 5636, -6933, 66539, 36654, 50927, 24783, 3457,
638
+ 33276, 45281, 45650, -4938, -9968, -22590, 47995, 69229, 5214, -58365,
639
+ -17907, -14651, 18668, 18009, 12649, -11851, -13387, 20339, 52472,
640
+ -1087, -21458, -68647, 52295, 15849, 40608, 15323, 25164, -29368,
641
+ 10352, -7055, 7159, 21695, -5373, -54849, 101103, -24963, -10511,
642
+ 33227, 7659, 41042, -69588, 26718, -20515, 6441, 38135, -63, 24088,
643
+ -35364, -12785, -18709, 47843, 48533, -48575, 17251, -19394, 32878,
644
+ -9010, -9050, 504, -12407, 28076, -3429, 25324, -4210, -26119, 752,
645
+ -29203, 28251, -11324, -32140, -3366, -25135, 18702, -31588, -7047,
646
+ -24267, 49987, -14975, -33169, 37744, -7720, -9035, 16964, -2807, -421,
647
+ 14114, -17097, -13662, 40628, -12139, -9427, 5369, 17551, -13232, -16211,
648
+ 9804, -7422, 2677, 28635, -8280, -4906, 2908, -22558, 5604, 12459, 8756,
649
+ -3980, -4745, -18525, 7913, 5970, -16457, 20230, -6247, -13812, 2505,
650
+ 11899, 1409, -15094, 22540, -18863, 137, 11123, -4516, 2290, -8594, 12150,
651
+ -10380, 3005, 5235, -7350, 2535, -858], ZZ)
652
+
653
+ assert dup_mul(p1, p2, ZZ) == res
654
+
655
+
656
+ def test_dmp_mul():
657
+ assert dmp_mul([ZZ(5)], [ZZ(7)], 0, ZZ) == \
658
+ dup_mul([ZZ(5)], [ZZ(7)], ZZ)
659
+ assert dmp_mul([QQ(5, 7)], [QQ(3, 7)], 0, QQ) == \
660
+ dup_mul([QQ(5, 7)], [QQ(3, 7)], QQ)
661
+
662
+ assert dmp_mul([[[]]], [[[]]], 2, ZZ) == [[[]]]
663
+ assert dmp_mul([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[]]]
664
+ assert dmp_mul([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[]]]
665
+ assert dmp_mul([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(2)]]]
666
+ assert dmp_mul([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(2)]]]
667
+
668
+ assert dmp_mul([[[]]], [[[]]], 2, QQ) == [[[]]]
669
+ assert dmp_mul([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[]]]
670
+ assert dmp_mul([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[]]]
671
+ assert dmp_mul([[[QQ(2, 7)]]], [[[QQ(1, 3)]]], 2, QQ) == [[[QQ(2, 21)]]]
672
+ assert dmp_mul([[[QQ(1, 7)]]], [[[QQ(2, 3)]]], 2, QQ) == [[[QQ(2, 21)]]]
673
+
674
+ K = FF(6)
675
+
676
+ assert dmp_mul(
677
+ [[K(2)], [K(1)]], [[K(3)], [K(4)]], 1, K) == [[K(5)], [K(4)]]
678
+
679
+
680
+ def test_dup_sqr():
681
+ assert dup_sqr([], ZZ) == []
682
+ assert dup_sqr([ZZ(2)], ZZ) == [ZZ(4)]
683
+ assert dup_sqr([ZZ(1), ZZ(2)], ZZ) == [ZZ(1), ZZ(4), ZZ(4)]
684
+
685
+ assert dup_sqr([], QQ) == []
686
+ assert dup_sqr([QQ(2, 3)], QQ) == [QQ(4, 9)]
687
+ assert dup_sqr([QQ(1, 3), QQ(2, 3)], QQ) == [QQ(1, 9), QQ(4, 9), QQ(4, 9)]
688
+
689
+ f = dup_normal([2, 0, 0, 1, 7], ZZ)
690
+
691
+ assert dup_sqr(f, ZZ) == dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
692
+
693
+ K = FF(9)
694
+
695
+ assert dup_sqr([K(3), K(4)], K) == [K(6), K(7)]
696
+
697
+
698
+ def test_dmp_sqr():
699
+ assert dmp_sqr([ZZ(1), ZZ(2)], 0, ZZ) == \
700
+ dup_sqr([ZZ(1), ZZ(2)], ZZ)
701
+
702
+ assert dmp_sqr([[[]]], 2, ZZ) == [[[]]]
703
+ assert dmp_sqr([[[ZZ(2)]]], 2, ZZ) == [[[ZZ(4)]]]
704
+
705
+ assert dmp_sqr([[[]]], 2, QQ) == [[[]]]
706
+ assert dmp_sqr([[[QQ(2, 3)]]], 2, QQ) == [[[QQ(4, 9)]]]
707
+
708
+ K = FF(9)
709
+
710
+ assert dmp_sqr([[K(3)], [K(4)]], 1, K) == [[K(6)], [K(7)]]
711
+
712
+
713
+ def test_dup_pow():
714
+ assert dup_pow([], 0, ZZ) == [ZZ(1)]
715
+ assert dup_pow([], 0, QQ) == [QQ(1)]
716
+
717
+ assert dup_pow([], 1, ZZ) == []
718
+ assert dup_pow([], 7, ZZ) == []
719
+
720
+ assert dup_pow([ZZ(1)], 0, ZZ) == [ZZ(1)]
721
+ assert dup_pow([ZZ(1)], 1, ZZ) == [ZZ(1)]
722
+ assert dup_pow([ZZ(1)], 7, ZZ) == [ZZ(1)]
723
+
724
+ assert dup_pow([ZZ(3)], 0, ZZ) == [ZZ(1)]
725
+ assert dup_pow([ZZ(3)], 1, ZZ) == [ZZ(3)]
726
+ assert dup_pow([ZZ(3)], 7, ZZ) == [ZZ(2187)]
727
+
728
+ assert dup_pow([QQ(1, 1)], 0, QQ) == [QQ(1, 1)]
729
+ assert dup_pow([QQ(1, 1)], 1, QQ) == [QQ(1, 1)]
730
+ assert dup_pow([QQ(1, 1)], 7, QQ) == [QQ(1, 1)]
731
+
732
+ assert dup_pow([QQ(3, 7)], 0, QQ) == [QQ(1, 1)]
733
+ assert dup_pow([QQ(3, 7)], 1, QQ) == [QQ(3, 7)]
734
+ assert dup_pow([QQ(3, 7)], 7, QQ) == [QQ(2187, 823543)]
735
+
736
+ f = dup_normal([2, 0, 0, 1, 7], ZZ)
737
+
738
+ assert dup_pow(f, 0, ZZ) == dup_normal([1], ZZ)
739
+ assert dup_pow(f, 1, ZZ) == dup_normal([2, 0, 0, 1, 7], ZZ)
740
+ assert dup_pow(f, 2, ZZ) == dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
741
+ assert dup_pow(f, 3, ZZ) == dup_normal(
742
+ [8, 0, 0, 12, 84, 0, 6, 84, 294, 1, 21, 147, 343], ZZ)
743
+
744
+
745
+ def test_dmp_pow():
746
+ assert dmp_pow([[]], 0, 1, ZZ) == [[ZZ(1)]]
747
+ assert dmp_pow([[]], 0, 1, QQ) == [[QQ(1)]]
748
+
749
+ assert dmp_pow([[]], 1, 1, ZZ) == [[]]
750
+ assert dmp_pow([[]], 7, 1, ZZ) == [[]]
751
+
752
+ assert dmp_pow([[ZZ(1)]], 0, 1, ZZ) == [[ZZ(1)]]
753
+ assert dmp_pow([[ZZ(1)]], 1, 1, ZZ) == [[ZZ(1)]]
754
+ assert dmp_pow([[ZZ(1)]], 7, 1, ZZ) == [[ZZ(1)]]
755
+
756
+ assert dmp_pow([[QQ(3, 7)]], 0, 1, QQ) == [[QQ(1, 1)]]
757
+ assert dmp_pow([[QQ(3, 7)]], 1, 1, QQ) == [[QQ(3, 7)]]
758
+ assert dmp_pow([[QQ(3, 7)]], 7, 1, QQ) == [[QQ(2187, 823543)]]
759
+
760
+ f = dup_normal([2, 0, 0, 1, 7], ZZ)
761
+
762
+ assert dmp_pow(f, 2, 0, ZZ) == dup_pow(f, 2, ZZ)
763
+
764
+
765
+ def test_dup_pdiv():
766
+ f = dup_normal([3, 1, 1, 5], ZZ)
767
+ g = dup_normal([5, -3, 1], ZZ)
768
+
769
+ q = dup_normal([15, 14], ZZ)
770
+ r = dup_normal([52, 111], ZZ)
771
+
772
+ assert dup_pdiv(f, g, ZZ) == (q, r)
773
+ assert dup_pquo(f, g, ZZ) == q
774
+ assert dup_prem(f, g, ZZ) == r
775
+
776
+ raises(ExactQuotientFailed, lambda: dup_pexquo(f, g, ZZ))
777
+
778
+ f = dup_normal([3, 1, 1, 5], QQ)
779
+ g = dup_normal([5, -3, 1], QQ)
780
+
781
+ q = dup_normal([15, 14], QQ)
782
+ r = dup_normal([52, 111], QQ)
783
+
784
+ assert dup_pdiv(f, g, QQ) == (q, r)
785
+ assert dup_pquo(f, g, QQ) == q
786
+ assert dup_prem(f, g, QQ) == r
787
+
788
+ raises(ExactQuotientFailed, lambda: dup_pexquo(f, g, QQ))
789
+
790
+
791
+ def test_dmp_pdiv():
792
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
793
+ g = dmp_normal([[1], [-1, 0]], 1, ZZ)
794
+
795
+ q = dmp_normal([[1], [1, 0]], 1, ZZ)
796
+ r = dmp_normal([[2, 0, 0]], 1, ZZ)
797
+
798
+ assert dmp_pdiv(f, g, 1, ZZ) == (q, r)
799
+ assert dmp_pquo(f, g, 1, ZZ) == q
800
+ assert dmp_prem(f, g, 1, ZZ) == r
801
+
802
+ raises(ExactQuotientFailed, lambda: dmp_pexquo(f, g, 1, ZZ))
803
+
804
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
805
+ g = dmp_normal([[2], [-2, 0]], 1, ZZ)
806
+
807
+ q = dmp_normal([[2], [2, 0]], 1, ZZ)
808
+ r = dmp_normal([[8, 0, 0]], 1, ZZ)
809
+
810
+ assert dmp_pdiv(f, g, 1, ZZ) == (q, r)
811
+ assert dmp_pquo(f, g, 1, ZZ) == q
812
+ assert dmp_prem(f, g, 1, ZZ) == r
813
+
814
+ raises(ExactQuotientFailed, lambda: dmp_pexquo(f, g, 1, ZZ))
815
+
816
+
817
+ def test_dup_rr_div():
818
+ raises(ZeroDivisionError, lambda: dup_rr_div([1, 2, 3], [], ZZ))
819
+
820
+ f = dup_normal([3, 1, 1, 5], ZZ)
821
+ g = dup_normal([5, -3, 1], ZZ)
822
+
823
+ q, r = [], f
824
+
825
+ assert dup_rr_div(f, g, ZZ) == (q, r)
826
+
827
+
828
+ def test_dmp_rr_div():
829
+ raises(ZeroDivisionError, lambda: dmp_rr_div([[1, 2], [3]], [[]], 1, ZZ))
830
+
831
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
832
+ g = dmp_normal([[1], [-1, 0]], 1, ZZ)
833
+
834
+ q = dmp_normal([[1], [1, 0]], 1, ZZ)
835
+ r = dmp_normal([[2, 0, 0]], 1, ZZ)
836
+
837
+ assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
838
+
839
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
840
+ g = dmp_normal([[-1], [1, 0]], 1, ZZ)
841
+
842
+ q = dmp_normal([[-1], [-1, 0]], 1, ZZ)
843
+ r = dmp_normal([[2, 0, 0]], 1, ZZ)
844
+
845
+ assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
846
+
847
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
848
+ g = dmp_normal([[2], [-2, 0]], 1, ZZ)
849
+
850
+ q, r = [[]], f
851
+
852
+ assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
853
+
854
+
855
+ def test_dup_ff_div():
856
+ raises(ZeroDivisionError, lambda: dup_ff_div([1, 2, 3], [], QQ))
857
+
858
+ f = dup_normal([3, 1, 1, 5], QQ)
859
+ g = dup_normal([5, -3, 1], QQ)
860
+
861
+ q = [QQ(3, 5), QQ(14, 25)]
862
+ r = [QQ(52, 25), QQ(111, 25)]
863
+
864
+ assert dup_ff_div(f, g, QQ) == (q, r)
865
+
866
+ def test_dup_ff_div_gmpy2():
867
+ try:
868
+ from gmpy2 import mpq
869
+ except ImportError:
870
+ return
871
+
872
+ from sympy.polys.domains import GMPYRationalField
873
+ K = GMPYRationalField()
874
+
875
+ f = [mpq(1,3), mpq(3,2)]
876
+ g = [mpq(2,1)]
877
+ assert dmp_ff_div(f, g, 0, K) == ([mpq(1,6), mpq(3,4)], [])
878
+
879
+ f = [mpq(1,2), mpq(1,3), mpq(1,4), mpq(1,5)]
880
+ g = [mpq(-1,1), mpq(1,1), mpq(-1,1)]
881
+ assert dmp_ff_div(f, g, 0, K) == ([mpq(-1,2), mpq(-5,6)], [mpq(7,12), mpq(-19,30)])
882
+
883
+ def test_dmp_ff_div():
884
+ raises(ZeroDivisionError, lambda: dmp_ff_div([[1, 2], [3]], [[]], 1, QQ))
885
+
886
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
887
+ g = dmp_normal([[1], [-1, 0]], 1, QQ)
888
+
889
+ q = [[QQ(1, 1)], [QQ(1, 1), QQ(0, 1)]]
890
+ r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
891
+
892
+ assert dmp_ff_div(f, g, 1, QQ) == (q, r)
893
+
894
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
895
+ g = dmp_normal([[-1], [1, 0]], 1, QQ)
896
+
897
+ q = [[QQ(-1, 1)], [QQ(-1, 1), QQ(0, 1)]]
898
+ r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
899
+
900
+ assert dmp_ff_div(f, g, 1, QQ) == (q, r)
901
+
902
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
903
+ g = dmp_normal([[2], [-2, 0]], 1, QQ)
904
+
905
+ q = [[QQ(1, 2)], [QQ(1, 2), QQ(0, 1)]]
906
+ r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
907
+
908
+ assert dmp_ff_div(f, g, 1, QQ) == (q, r)
909
+
910
+
911
+ def test_dup_div():
912
+ f, g, q, r = [5, 4, 3, 2, 1], [1, 2, 3], [5, -6, 0], [20, 1]
913
+
914
+ assert dup_div(f, g, ZZ) == (q, r)
915
+ assert dup_quo(f, g, ZZ) == q
916
+ assert dup_rem(f, g, ZZ) == r
917
+
918
+ raises(ExactQuotientFailed, lambda: dup_exquo(f, g, ZZ))
919
+
920
+ f, g, q, r = [5, 4, 3, 2, 1, 0], [1, 2, 0, 0, 9], [5, -6], [15, 2, -44, 54]
921
+
922
+ assert dup_div(f, g, ZZ) == (q, r)
923
+ assert dup_quo(f, g, ZZ) == q
924
+ assert dup_rem(f, g, ZZ) == r
925
+
926
+ raises(ExactQuotientFailed, lambda: dup_exquo(f, g, ZZ))
927
+
928
+
929
+ def test_dmp_div():
930
+ f, g, q, r = [5, 4, 3, 2, 1], [1, 2, 3], [5, -6, 0], [20, 1]
931
+
932
+ assert dmp_div(f, g, 0, ZZ) == (q, r)
933
+ assert dmp_quo(f, g, 0, ZZ) == q
934
+ assert dmp_rem(f, g, 0, ZZ) == r
935
+
936
+ raises(ExactQuotientFailed, lambda: dmp_exquo(f, g, 0, ZZ))
937
+
938
+ f, g, q, r = [[[1]]], [[[2]], [1]], [[[]]], [[[1]]]
939
+
940
+ assert dmp_div(f, g, 2, ZZ) == (q, r)
941
+ assert dmp_quo(f, g, 2, ZZ) == q
942
+ assert dmp_rem(f, g, 2, ZZ) == r
943
+
944
+ raises(ExactQuotientFailed, lambda: dmp_exquo(f, g, 2, ZZ))
945
+
946
+
947
+ def test_dup_max_norm():
948
+ assert dup_max_norm([], ZZ) == 0
949
+ assert dup_max_norm([1], ZZ) == 1
950
+
951
+ assert dup_max_norm([1, 4, 2, 3], ZZ) == 4
952
+
953
+
954
+ def test_dmp_max_norm():
955
+ assert dmp_max_norm([[[]]], 2, ZZ) == 0
956
+ assert dmp_max_norm([[[1]]], 2, ZZ) == 1
957
+
958
+ assert dmp_max_norm(f_0, 2, ZZ) == 6
959
+
960
+
961
+ def test_dup_l1_norm():
962
+ assert dup_l1_norm([], ZZ) == 0
963
+ assert dup_l1_norm([1], ZZ) == 1
964
+ assert dup_l1_norm([1, 4, 2, 3], ZZ) == 10
965
+
966
+
967
+ def test_dmp_l1_norm():
968
+ assert dmp_l1_norm([[[]]], 2, ZZ) == 0
969
+ assert dmp_l1_norm([[[1]]], 2, ZZ) == 1
970
+
971
+ assert dmp_l1_norm(f_0, 2, ZZ) == 31
972
+
973
+
974
+ def test_dup_l2_norm_squared():
975
+ assert dup_l2_norm_squared([], ZZ) == 0
976
+ assert dup_l2_norm_squared([1], ZZ) == 1
977
+ assert dup_l2_norm_squared([1, 4, 2, 3], ZZ) == 30
978
+
979
+
980
+ def test_dmp_l2_norm_squared():
981
+ assert dmp_l2_norm_squared([[[]]], 2, ZZ) == 0
982
+ assert dmp_l2_norm_squared([[[1]]], 2, ZZ) == 1
983
+ assert dmp_l2_norm_squared(f_0, 2, ZZ) == 111
984
+
985
+
986
+ def test_dup_expand():
987
+ assert dup_expand((), ZZ) == [1]
988
+ assert dup_expand(([1, 2, 3], [1, 2], [7, 5, 4, 3]), ZZ) == \
989
+ dup_mul([1, 2, 3], dup_mul([1, 2], [7, 5, 4, 3], ZZ), ZZ)
990
+
991
+
992
+ def test_dmp_expand():
993
+ assert dmp_expand((), 1, ZZ) == [[1]]
994
+ assert dmp_expand(([[1], [2], [3]], [[1], [2]], [[7], [5], [4], [3]]), 1, ZZ) == \
995
+ dmp_mul([[1], [2], [3]], dmp_mul([[1], [2]], [[7], [5], [
996
+ 4], [3]], 1, ZZ), 1, ZZ)
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_densetools.py ADDED
@@ -0,0 +1,668 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for dense recursive polynomials' tools. """
2
+
3
+ from sympy.polys.densebasic import (
4
+ dup_normal, dmp_normal,
5
+ dup_from_raw_dict,
6
+ dmp_convert, dmp_swap,
7
+ )
8
+
9
+ from sympy.polys.densearith import dmp_mul_ground
10
+
11
+ from sympy.polys.densetools import (
12
+ dup_clear_denoms, dmp_clear_denoms,
13
+ dup_integrate, dmp_integrate, dmp_integrate_in,
14
+ dup_diff, dmp_diff, dmp_diff_in,
15
+ dup_eval, dmp_eval, dmp_eval_in,
16
+ dmp_eval_tail, dmp_diff_eval_in,
17
+ dup_trunc, dmp_trunc, dmp_ground_trunc,
18
+ dup_monic, dmp_ground_monic,
19
+ dup_content, dmp_ground_content,
20
+ dup_primitive, dmp_ground_primitive,
21
+ dup_extract, dmp_ground_extract,
22
+ dup_real_imag,
23
+ dup_mirror, dup_scale, dup_shift,
24
+ dup_transform,
25
+ dup_compose, dmp_compose,
26
+ dup_decompose,
27
+ dmp_lift,
28
+ dup_sign_variations,
29
+ dup_revert, dmp_revert,
30
+ )
31
+
32
+ from sympy.polys.polyclasses import ANP
33
+
34
+ from sympy.polys.polyerrors import (
35
+ MultivariatePolynomialError,
36
+ ExactQuotientFailed,
37
+ NotReversible,
38
+ DomainError,
39
+ )
40
+
41
+ from sympy.polys.specialpolys import f_polys
42
+
43
+ from sympy.polys.domains import FF, ZZ, QQ, EX
44
+ from sympy.polys.rings import ring
45
+
46
+ from sympy.core.numbers import I
47
+ from sympy.core.singleton import S
48
+ from sympy.functions.elementary.trigonometric import sin
49
+
50
+ from sympy.abc import x
51
+
52
+ from sympy.testing.pytest import raises
53
+
54
+ f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
55
+
56
+ def test_dup_integrate():
57
+ assert dup_integrate([], 1, QQ) == []
58
+ assert dup_integrate([], 2, QQ) == []
59
+
60
+ assert dup_integrate([QQ(1)], 1, QQ) == [QQ(1), QQ(0)]
61
+ assert dup_integrate([QQ(1)], 2, QQ) == [QQ(1, 2), QQ(0), QQ(0)]
62
+
63
+ assert dup_integrate([QQ(1), QQ(2), QQ(3)], 0, QQ) == \
64
+ [QQ(1), QQ(2), QQ(3)]
65
+ assert dup_integrate([QQ(1), QQ(2), QQ(3)], 1, QQ) == \
66
+ [QQ(1, 3), QQ(1), QQ(3), QQ(0)]
67
+ assert dup_integrate([QQ(1), QQ(2), QQ(3)], 2, QQ) == \
68
+ [QQ(1, 12), QQ(1, 3), QQ(3, 2), QQ(0), QQ(0)]
69
+ assert dup_integrate([QQ(1), QQ(2), QQ(3)], 3, QQ) == \
70
+ [QQ(1, 60), QQ(1, 12), QQ(1, 2), QQ(0), QQ(0), QQ(0)]
71
+
72
+ assert dup_integrate(dup_from_raw_dict({29: QQ(17)}, QQ), 3, QQ) == \
73
+ dup_from_raw_dict({32: QQ(17, 29760)}, QQ)
74
+
75
+ assert dup_integrate(dup_from_raw_dict({29: QQ(17), 5: QQ(1, 2)}, QQ), 3, QQ) == \
76
+ dup_from_raw_dict({32: QQ(17, 29760), 8: QQ(1, 672)}, QQ)
77
+
78
+
79
+ def test_dmp_integrate():
80
+ assert dmp_integrate([[[]]], 1, 2, QQ) == [[[]]]
81
+ assert dmp_integrate([[[]]], 2, 2, QQ) == [[[]]]
82
+
83
+ assert dmp_integrate([[[QQ(1)]]], 1, 2, QQ) == [[[QQ(1)]], [[]]]
84
+ assert dmp_integrate([[[QQ(1)]]], 2, 2, QQ) == [[[QQ(1, 2)]], [[]], [[]]]
85
+
86
+ assert dmp_integrate([[QQ(1)], [QQ(2)], [QQ(3)]], 0, 1, QQ) == \
87
+ [[QQ(1)], [QQ(2)], [QQ(3)]]
88
+ assert dmp_integrate([[QQ(1)], [QQ(2)], [QQ(3)]], 1, 1, QQ) == \
89
+ [[QQ(1, 3)], [QQ(1)], [QQ(3)], []]
90
+ assert dmp_integrate([[QQ(1)], [QQ(2)], [QQ(3)]], 2, 1, QQ) == \
91
+ [[QQ(1, 12)], [QQ(1, 3)], [QQ(3, 2)], [], []]
92
+ assert dmp_integrate([[QQ(1)], [QQ(2)], [QQ(3)]], 3, 1, QQ) == \
93
+ [[QQ(1, 60)], [QQ(1, 12)], [QQ(1, 2)], [], [], []]
94
+
95
+
96
+ def test_dmp_integrate_in():
97
+ f = dmp_convert(f_6, 3, ZZ, QQ)
98
+
99
+ assert dmp_integrate_in(f, 2, 1, 3, QQ) == \
100
+ dmp_swap(
101
+ dmp_integrate(dmp_swap(f, 0, 1, 3, QQ), 2, 3, QQ), 0, 1, 3, QQ)
102
+ assert dmp_integrate_in(f, 3, 1, 3, QQ) == \
103
+ dmp_swap(
104
+ dmp_integrate(dmp_swap(f, 0, 1, 3, QQ), 3, 3, QQ), 0, 1, 3, QQ)
105
+ assert dmp_integrate_in(f, 2, 2, 3, QQ) == \
106
+ dmp_swap(
107
+ dmp_integrate(dmp_swap(f, 0, 2, 3, QQ), 2, 3, QQ), 0, 2, 3, QQ)
108
+ assert dmp_integrate_in(f, 3, 2, 3, QQ) == \
109
+ dmp_swap(
110
+ dmp_integrate(dmp_swap(f, 0, 2, 3, QQ), 3, 3, QQ), 0, 2, 3, QQ)
111
+
112
+
113
+ def test_dup_diff():
114
+ assert dup_diff([], 1, ZZ) == []
115
+ assert dup_diff([7], 1, ZZ) == []
116
+ assert dup_diff([2, 7], 1, ZZ) == [2]
117
+ assert dup_diff([1, 2, 1], 1, ZZ) == [2, 2]
118
+ assert dup_diff([1, 2, 3, 4], 1, ZZ) == [3, 4, 3]
119
+ assert dup_diff([1, -1, 0, 0, 2], 1, ZZ) == [4, -3, 0, 0]
120
+
121
+ f = dup_normal([17, 34, 56, -345, 23, 76, 0, 0, 12, 3, 7], ZZ)
122
+
123
+ assert dup_diff(f, 0, ZZ) == f
124
+ assert dup_diff(f, 1, ZZ) == [170, 306, 448, -2415, 138, 380, 0, 0, 24, 3]
125
+ assert dup_diff(f, 2, ZZ) == dup_diff(dup_diff(f, 1, ZZ), 1, ZZ)
126
+ assert dup_diff(
127
+ f, 3, ZZ) == dup_diff(dup_diff(dup_diff(f, 1, ZZ), 1, ZZ), 1, ZZ)
128
+
129
+ K = FF(3)
130
+ f = dup_normal([17, 34, 56, -345, 23, 76, 0, 0, 12, 3, 7], K)
131
+
132
+ assert dup_diff(f, 1, K) == dup_normal([2, 0, 1, 0, 0, 2, 0, 0, 0, 0], K)
133
+ assert dup_diff(f, 2, K) == dup_normal([1, 0, 0, 2, 0, 0, 0], K)
134
+ assert dup_diff(f, 3, K) == dup_normal([], K)
135
+
136
+ assert dup_diff(f, 0, K) == f
137
+ assert dup_diff(f, 2, K) == dup_diff(dup_diff(f, 1, K), 1, K)
138
+ assert dup_diff(
139
+ f, 3, K) == dup_diff(dup_diff(dup_diff(f, 1, K), 1, K), 1, K)
140
+
141
+
142
+ def test_dmp_diff():
143
+ assert dmp_diff([], 1, 0, ZZ) == []
144
+ assert dmp_diff([[]], 1, 1, ZZ) == [[]]
145
+ assert dmp_diff([[[]]], 1, 2, ZZ) == [[[]]]
146
+
147
+ assert dmp_diff([[[1], [2]]], 1, 2, ZZ) == [[[]]]
148
+
149
+ assert dmp_diff([[[1]], [[]]], 1, 2, ZZ) == [[[1]]]
150
+ assert dmp_diff([[[3]], [[1]], [[]]], 1, 2, ZZ) == [[[6]], [[1]]]
151
+
152
+ assert dmp_diff([1, -1, 0, 0, 2], 1, 0, ZZ) == \
153
+ dup_diff([1, -1, 0, 0, 2], 1, ZZ)
154
+
155
+ assert dmp_diff(f_6, 0, 3, ZZ) == f_6
156
+ assert dmp_diff(f_6, 1, 3, ZZ) == [[[[8460]], [[]]],
157
+ [[[135, 0, 0], [], [], [-135, 0, 0]]],
158
+ [[[]]],
159
+ [[[-423]], [[-47]], [[]], [[141], [], [94, 0], []], [[]]]]
160
+ assert dmp_diff(
161
+ f_6, 2, 3, ZZ) == dmp_diff(dmp_diff(f_6, 1, 3, ZZ), 1, 3, ZZ)
162
+ assert dmp_diff(f_6, 3, 3, ZZ) == dmp_diff(
163
+ dmp_diff(dmp_diff(f_6, 1, 3, ZZ), 1, 3, ZZ), 1, 3, ZZ)
164
+
165
+ K = FF(23)
166
+ F_6 = dmp_normal(f_6, 3, K)
167
+
168
+ assert dmp_diff(F_6, 0, 3, K) == F_6
169
+ assert dmp_diff(F_6, 1, 3, K) == dmp_diff(F_6, 1, 3, K)
170
+ assert dmp_diff(F_6, 2, 3, K) == dmp_diff(dmp_diff(F_6, 1, 3, K), 1, 3, K)
171
+ assert dmp_diff(F_6, 3, 3, K) == dmp_diff(
172
+ dmp_diff(dmp_diff(F_6, 1, 3, K), 1, 3, K), 1, 3, K)
173
+
174
+
175
+ def test_dmp_diff_in():
176
+ assert dmp_diff_in(f_6, 2, 1, 3, ZZ) == \
177
+ dmp_swap(dmp_diff(dmp_swap(f_6, 0, 1, 3, ZZ), 2, 3, ZZ), 0, 1, 3, ZZ)
178
+ assert dmp_diff_in(f_6, 3, 1, 3, ZZ) == \
179
+ dmp_swap(dmp_diff(dmp_swap(f_6, 0, 1, 3, ZZ), 3, 3, ZZ), 0, 1, 3, ZZ)
180
+ assert dmp_diff_in(f_6, 2, 2, 3, ZZ) == \
181
+ dmp_swap(dmp_diff(dmp_swap(f_6, 0, 2, 3, ZZ), 2, 3, ZZ), 0, 2, 3, ZZ)
182
+ assert dmp_diff_in(f_6, 3, 2, 3, ZZ) == \
183
+ dmp_swap(dmp_diff(dmp_swap(f_6, 0, 2, 3, ZZ), 3, 3, ZZ), 0, 2, 3, ZZ)
184
+
185
+
186
+ def test_dup_eval():
187
+ assert dup_eval([], 7, ZZ) == 0
188
+ assert dup_eval([1, 2], 0, ZZ) == 2
189
+ assert dup_eval([1, 2, 3], 7, ZZ) == 66
190
+
191
+
192
+ def test_dmp_eval():
193
+ assert dmp_eval([], 3, 0, ZZ) == 0
194
+
195
+ assert dmp_eval([[]], 3, 1, ZZ) == []
196
+ assert dmp_eval([[[]]], 3, 2, ZZ) == [[]]
197
+
198
+ assert dmp_eval([[1, 2]], 0, 1, ZZ) == [1, 2]
199
+
200
+ assert dmp_eval([[[1]]], 3, 2, ZZ) == [[1]]
201
+ assert dmp_eval([[[1, 2]]], 3, 2, ZZ) == [[1, 2]]
202
+
203
+ assert dmp_eval([[3, 2], [1, 2]], 3, 1, ZZ) == [10, 8]
204
+ assert dmp_eval([[[3, 2]], [[1, 2]]], 3, 2, ZZ) == [[10, 8]]
205
+
206
+
207
+ def test_dmp_eval_in():
208
+ assert dmp_eval_in(
209
+ f_6, -2, 1, 3, ZZ) == dmp_eval(dmp_swap(f_6, 0, 1, 3, ZZ), -2, 3, ZZ)
210
+ assert dmp_eval_in(
211
+ f_6, 7, 1, 3, ZZ) == dmp_eval(dmp_swap(f_6, 0, 1, 3, ZZ), 7, 3, ZZ)
212
+ assert dmp_eval_in(f_6, -2, 2, 3, ZZ) == dmp_swap(
213
+ dmp_eval(dmp_swap(f_6, 0, 2, 3, ZZ), -2, 3, ZZ), 0, 1, 2, ZZ)
214
+ assert dmp_eval_in(f_6, 7, 2, 3, ZZ) == dmp_swap(
215
+ dmp_eval(dmp_swap(f_6, 0, 2, 3, ZZ), 7, 3, ZZ), 0, 1, 2, ZZ)
216
+
217
+ f = [[[int(45)]], [[]], [[]], [[int(-9)], [-1], [], [int(3), int(0), int(10), int(0)]]]
218
+
219
+ assert dmp_eval_in(f, -2, 2, 2, ZZ) == \
220
+ [[45], [], [], [-9, -1, 0, -44]]
221
+
222
+
223
+ def test_dmp_eval_tail():
224
+ assert dmp_eval_tail([[]], [1], 1, ZZ) == []
225
+ assert dmp_eval_tail([[[]]], [1], 2, ZZ) == [[]]
226
+ assert dmp_eval_tail([[[]]], [1, 2], 2, ZZ) == []
227
+
228
+ assert dmp_eval_tail(f_0, [], 2, ZZ) == f_0
229
+
230
+ assert dmp_eval_tail(f_0, [1, -17, 8], 2, ZZ) == 84496
231
+ assert dmp_eval_tail(f_0, [-17, 8], 2, ZZ) == [-1409, 3, 85902]
232
+ assert dmp_eval_tail(f_0, [8], 2, ZZ) == [[83, 2], [3], [302, 81, 1]]
233
+
234
+ assert dmp_eval_tail(f_1, [-17, 8], 2, ZZ) == [-136, 15699, 9166, -27144]
235
+
236
+ assert dmp_eval_tail(
237
+ f_2, [-12, 3], 2, ZZ) == [-1377, 0, -702, -1224, 0, -624]
238
+ assert dmp_eval_tail(
239
+ f_3, [-12, 3], 2, ZZ) == [144, 82, -5181, -28872, -14868, -540]
240
+
241
+ assert dmp_eval_tail(
242
+ f_4, [25, -1], 2, ZZ) == [152587890625, 9765625, -59605407714843750,
243
+ -3839159765625, -1562475, 9536712644531250, 610349546750, -4, 24414375000, 1562520]
244
+ assert dmp_eval_tail(f_5, [25, -1], 2, ZZ) == [-1, -78, -2028, -17576]
245
+
246
+ assert dmp_eval_tail(f_6, [0, 2, 4], 3, ZZ) == [5040, 0, 0, 4480]
247
+
248
+
249
+ def test_dmp_diff_eval_in():
250
+ assert dmp_diff_eval_in(f_6, 2, 7, 1, 3, ZZ) == \
251
+ dmp_eval(dmp_diff(dmp_swap(f_6, 0, 1, 3, ZZ), 2, 3, ZZ), 7, 3, ZZ)
252
+
253
+
254
+ def test_dup_revert():
255
+ f = [-QQ(1, 720), QQ(0), QQ(1, 24), QQ(0), -QQ(1, 2), QQ(0), QQ(1)]
256
+ g = [QQ(61, 720), QQ(0), QQ(5, 24), QQ(0), QQ(1, 2), QQ(0), QQ(1)]
257
+
258
+ assert dup_revert(f, 8, QQ) == g
259
+
260
+ raises(NotReversible, lambda: dup_revert([QQ(1), QQ(0)], 3, QQ))
261
+
262
+
263
+ def test_dmp_revert():
264
+ f = [-QQ(1, 720), QQ(0), QQ(1, 24), QQ(0), -QQ(1, 2), QQ(0), QQ(1)]
265
+ g = [QQ(61, 720), QQ(0), QQ(5, 24), QQ(0), QQ(1, 2), QQ(0), QQ(1)]
266
+
267
+ assert dmp_revert(f, 8, 0, QQ) == g
268
+
269
+ raises(MultivariatePolynomialError, lambda: dmp_revert([[1]], 2, 1, QQ))
270
+
271
+
272
+ def test_dup_trunc():
273
+ assert dup_trunc([1, 2, 3, 4, 5, 6], ZZ(3), ZZ) == [1, -1, 0, 1, -1, 0]
274
+ assert dup_trunc([6, 5, 4, 3, 2, 1], ZZ(3), ZZ) == [-1, 1, 0, -1, 1]
275
+
276
+
277
+ def test_dmp_trunc():
278
+ assert dmp_trunc([[]], [1, 2], 2, ZZ) == [[]]
279
+ assert dmp_trunc([[1, 2], [1, 4, 1], [1]], [1, 2], 1, ZZ) == [[-3], [1]]
280
+
281
+
282
+ def test_dmp_ground_trunc():
283
+ assert dmp_ground_trunc(f_0, ZZ(3), 2, ZZ) == \
284
+ dmp_normal(
285
+ [[[1, -1, 0], [-1]], [[]], [[1, -1, 0], [1, -1, 1], [1]]], 2, ZZ)
286
+
287
+
288
+ def test_dup_monic():
289
+ assert dup_monic([3, 6, 9], ZZ) == [1, 2, 3]
290
+
291
+ raises(ExactQuotientFailed, lambda: dup_monic([3, 4, 5], ZZ))
292
+
293
+ assert dup_monic([], QQ) == []
294
+ assert dup_monic([QQ(1)], QQ) == [QQ(1)]
295
+ assert dup_monic([QQ(7), QQ(1), QQ(21)], QQ) == [QQ(1), QQ(1, 7), QQ(3)]
296
+
297
+
298
+ def test_dmp_ground_monic():
299
+ assert dmp_ground_monic([[3], [6], [9]], 1, ZZ) == [[1], [2], [3]]
300
+
301
+ raises(
302
+ ExactQuotientFailed, lambda: dmp_ground_monic([[3], [4], [5]], 1, ZZ))
303
+
304
+ assert dmp_ground_monic([[]], 1, QQ) == [[]]
305
+ assert dmp_ground_monic([[QQ(1)]], 1, QQ) == [[QQ(1)]]
306
+ assert dmp_ground_monic(
307
+ [[QQ(7)], [QQ(1)], [QQ(21)]], 1, QQ) == [[QQ(1)], [QQ(1, 7)], [QQ(3)]]
308
+
309
+
310
+ def test_dup_content():
311
+ assert dup_content([], ZZ) == ZZ(0)
312
+ assert dup_content([1], ZZ) == ZZ(1)
313
+ assert dup_content([-1], ZZ) == ZZ(1)
314
+ assert dup_content([1, 1], ZZ) == ZZ(1)
315
+ assert dup_content([2, 2], ZZ) == ZZ(2)
316
+ assert dup_content([1, 2, 1], ZZ) == ZZ(1)
317
+ assert dup_content([2, 4, 2], ZZ) == ZZ(2)
318
+
319
+ assert dup_content([QQ(2, 3), QQ(4, 9)], QQ) == QQ(2, 9)
320
+ assert dup_content([QQ(2, 3), QQ(4, 5)], QQ) == QQ(2, 15)
321
+
322
+
323
+ def test_dmp_ground_content():
324
+ assert dmp_ground_content([[]], 1, ZZ) == ZZ(0)
325
+ assert dmp_ground_content([[]], 1, QQ) == QQ(0)
326
+ assert dmp_ground_content([[1]], 1, ZZ) == ZZ(1)
327
+ assert dmp_ground_content([[-1]], 1, ZZ) == ZZ(1)
328
+ assert dmp_ground_content([[1], [1]], 1, ZZ) == ZZ(1)
329
+ assert dmp_ground_content([[2], [2]], 1, ZZ) == ZZ(2)
330
+ assert dmp_ground_content([[1], [2], [1]], 1, ZZ) == ZZ(1)
331
+ assert dmp_ground_content([[2], [4], [2]], 1, ZZ) == ZZ(2)
332
+
333
+ assert dmp_ground_content([[QQ(2, 3)], [QQ(4, 9)]], 1, QQ) == QQ(2, 9)
334
+ assert dmp_ground_content([[QQ(2, 3)], [QQ(4, 5)]], 1, QQ) == QQ(2, 15)
335
+
336
+ assert dmp_ground_content(f_0, 2, ZZ) == ZZ(1)
337
+ assert dmp_ground_content(
338
+ dmp_mul_ground(f_0, ZZ(2), 2, ZZ), 2, ZZ) == ZZ(2)
339
+
340
+ assert dmp_ground_content(f_1, 2, ZZ) == ZZ(1)
341
+ assert dmp_ground_content(
342
+ dmp_mul_ground(f_1, ZZ(3), 2, ZZ), 2, ZZ) == ZZ(3)
343
+
344
+ assert dmp_ground_content(f_2, 2, ZZ) == ZZ(1)
345
+ assert dmp_ground_content(
346
+ dmp_mul_ground(f_2, ZZ(4), 2, ZZ), 2, ZZ) == ZZ(4)
347
+
348
+ assert dmp_ground_content(f_3, 2, ZZ) == ZZ(1)
349
+ assert dmp_ground_content(
350
+ dmp_mul_ground(f_3, ZZ(5), 2, ZZ), 2, ZZ) == ZZ(5)
351
+
352
+ assert dmp_ground_content(f_4, 2, ZZ) == ZZ(1)
353
+ assert dmp_ground_content(
354
+ dmp_mul_ground(f_4, ZZ(6), 2, ZZ), 2, ZZ) == ZZ(6)
355
+
356
+ assert dmp_ground_content(f_5, 2, ZZ) == ZZ(1)
357
+ assert dmp_ground_content(
358
+ dmp_mul_ground(f_5, ZZ(7), 2, ZZ), 2, ZZ) == ZZ(7)
359
+
360
+ assert dmp_ground_content(f_6, 3, ZZ) == ZZ(1)
361
+ assert dmp_ground_content(
362
+ dmp_mul_ground(f_6, ZZ(8), 3, ZZ), 3, ZZ) == ZZ(8)
363
+
364
+
365
+ def test_dup_primitive():
366
+ assert dup_primitive([], ZZ) == (ZZ(0), [])
367
+ assert dup_primitive([ZZ(1)], ZZ) == (ZZ(1), [ZZ(1)])
368
+ assert dup_primitive([ZZ(1), ZZ(1)], ZZ) == (ZZ(1), [ZZ(1), ZZ(1)])
369
+ assert dup_primitive([ZZ(2), ZZ(2)], ZZ) == (ZZ(2), [ZZ(1), ZZ(1)])
370
+ assert dup_primitive(
371
+ [ZZ(1), ZZ(2), ZZ(1)], ZZ) == (ZZ(1), [ZZ(1), ZZ(2), ZZ(1)])
372
+ assert dup_primitive(
373
+ [ZZ(2), ZZ(4), ZZ(2)], ZZ) == (ZZ(2), [ZZ(1), ZZ(2), ZZ(1)])
374
+
375
+ assert dup_primitive([], QQ) == (QQ(0), [])
376
+ assert dup_primitive([QQ(1)], QQ) == (QQ(1), [QQ(1)])
377
+ assert dup_primitive([QQ(1), QQ(1)], QQ) == (QQ(1), [QQ(1), QQ(1)])
378
+ assert dup_primitive([QQ(2), QQ(2)], QQ) == (QQ(2), [QQ(1), QQ(1)])
379
+ assert dup_primitive(
380
+ [QQ(1), QQ(2), QQ(1)], QQ) == (QQ(1), [QQ(1), QQ(2), QQ(1)])
381
+ assert dup_primitive(
382
+ [QQ(2), QQ(4), QQ(2)], QQ) == (QQ(2), [QQ(1), QQ(2), QQ(1)])
383
+
384
+ assert dup_primitive(
385
+ [QQ(2, 3), QQ(4, 9)], QQ) == (QQ(2, 9), [QQ(3), QQ(2)])
386
+ assert dup_primitive(
387
+ [QQ(2, 3), QQ(4, 5)], QQ) == (QQ(2, 15), [QQ(5), QQ(6)])
388
+
389
+
390
+ def test_dmp_ground_primitive():
391
+ assert dmp_ground_primitive([[]], 1, ZZ) == (ZZ(0), [[]])
392
+
393
+ assert dmp_ground_primitive(f_0, 2, ZZ) == (ZZ(1), f_0)
394
+ assert dmp_ground_primitive(
395
+ dmp_mul_ground(f_0, ZZ(2), 2, ZZ), 2, ZZ) == (ZZ(2), f_0)
396
+
397
+ assert dmp_ground_primitive(f_1, 2, ZZ) == (ZZ(1), f_1)
398
+ assert dmp_ground_primitive(
399
+ dmp_mul_ground(f_1, ZZ(3), 2, ZZ), 2, ZZ) == (ZZ(3), f_1)
400
+
401
+ assert dmp_ground_primitive(f_2, 2, ZZ) == (ZZ(1), f_2)
402
+ assert dmp_ground_primitive(
403
+ dmp_mul_ground(f_2, ZZ(4), 2, ZZ), 2, ZZ) == (ZZ(4), f_2)
404
+
405
+ assert dmp_ground_primitive(f_3, 2, ZZ) == (ZZ(1), f_3)
406
+ assert dmp_ground_primitive(
407
+ dmp_mul_ground(f_3, ZZ(5), 2, ZZ), 2, ZZ) == (ZZ(5), f_3)
408
+
409
+ assert dmp_ground_primitive(f_4, 2, ZZ) == (ZZ(1), f_4)
410
+ assert dmp_ground_primitive(
411
+ dmp_mul_ground(f_4, ZZ(6), 2, ZZ), 2, ZZ) == (ZZ(6), f_4)
412
+
413
+ assert dmp_ground_primitive(f_5, 2, ZZ) == (ZZ(1), f_5)
414
+ assert dmp_ground_primitive(
415
+ dmp_mul_ground(f_5, ZZ(7), 2, ZZ), 2, ZZ) == (ZZ(7), f_5)
416
+
417
+ assert dmp_ground_primitive(f_6, 3, ZZ) == (ZZ(1), f_6)
418
+ assert dmp_ground_primitive(
419
+ dmp_mul_ground(f_6, ZZ(8), 3, ZZ), 3, ZZ) == (ZZ(8), f_6)
420
+
421
+ assert dmp_ground_primitive([[ZZ(2)]], 1, ZZ) == (ZZ(2), [[ZZ(1)]])
422
+ assert dmp_ground_primitive([[QQ(2)]], 1, QQ) == (QQ(2), [[QQ(1)]])
423
+
424
+ assert dmp_ground_primitive(
425
+ [[QQ(2, 3)], [QQ(4, 9)]], 1, QQ) == (QQ(2, 9), [[QQ(3)], [QQ(2)]])
426
+ assert dmp_ground_primitive(
427
+ [[QQ(2, 3)], [QQ(4, 5)]], 1, QQ) == (QQ(2, 15), [[QQ(5)], [QQ(6)]])
428
+
429
+
430
+ def test_dup_extract():
431
+ f = dup_normal([2930944, 0, 2198208, 0, 549552, 0, 45796], ZZ)
432
+ g = dup_normal([17585664, 0, 8792832, 0, 1099104, 0], ZZ)
433
+
434
+ F = dup_normal([64, 0, 48, 0, 12, 0, 1], ZZ)
435
+ G = dup_normal([384, 0, 192, 0, 24, 0], ZZ)
436
+
437
+ assert dup_extract(f, g, ZZ) == (45796, F, G)
438
+
439
+
440
+ def test_dmp_ground_extract():
441
+ f = dmp_normal(
442
+ [[2930944], [], [2198208], [], [549552], [], [45796]], 1, ZZ)
443
+ g = dmp_normal([[17585664], [], [8792832], [], [1099104], []], 1, ZZ)
444
+
445
+ F = dmp_normal([[64], [], [48], [], [12], [], [1]], 1, ZZ)
446
+ G = dmp_normal([[384], [], [192], [], [24], []], 1, ZZ)
447
+
448
+ assert dmp_ground_extract(f, g, 1, ZZ) == (45796, F, G)
449
+
450
+
451
+ def test_dup_real_imag():
452
+ assert dup_real_imag([], ZZ) == ([[]], [[]])
453
+ assert dup_real_imag([1], ZZ) == ([[1]], [[]])
454
+
455
+ assert dup_real_imag([1, 1], ZZ) == ([[1], [1]], [[1, 0]])
456
+ assert dup_real_imag([1, 2], ZZ) == ([[1], [2]], [[1, 0]])
457
+
458
+ assert dup_real_imag(
459
+ [1, 2, 3], ZZ) == ([[1], [2], [-1, 0, 3]], [[2, 0], [2, 0]])
460
+
461
+ raises(DomainError, lambda: dup_real_imag([EX(1), EX(2)], EX))
462
+
463
+
464
+ def test_dup_mirror():
465
+ assert dup_mirror([], ZZ) == []
466
+ assert dup_mirror([1], ZZ) == [1]
467
+
468
+ assert dup_mirror([1, 2, 3, 4, 5], ZZ) == [1, -2, 3, -4, 5]
469
+ assert dup_mirror([1, 2, 3, 4, 5, 6], ZZ) == [-1, 2, -3, 4, -5, 6]
470
+
471
+
472
+ def test_dup_scale():
473
+ assert dup_scale([], -1, ZZ) == []
474
+ assert dup_scale([1], -1, ZZ) == [1]
475
+
476
+ assert dup_scale([1, 2, 3, 4, 5], -1, ZZ) == [1, -2, 3, -4, 5]
477
+ assert dup_scale([1, 2, 3, 4, 5], -7, ZZ) == [2401, -686, 147, -28, 5]
478
+
479
+
480
+ def test_dup_shift():
481
+ assert dup_shift([], 1, ZZ) == []
482
+ assert dup_shift([1], 1, ZZ) == [1]
483
+
484
+ assert dup_shift([1, 2, 3, 4, 5], 1, ZZ) == [1, 6, 15, 20, 15]
485
+ assert dup_shift([1, 2, 3, 4, 5], 7, ZZ) == [1, 30, 339, 1712, 3267]
486
+
487
+
488
+ def test_dup_transform():
489
+ assert dup_transform([], [], [1, 1], ZZ) == []
490
+ assert dup_transform([], [1], [1, 1], ZZ) == []
491
+ assert dup_transform([], [1, 2], [1, 1], ZZ) == []
492
+
493
+ assert dup_transform([6, -5, 4, -3, 17], [1, -3, 4], [2, -3], ZZ) == \
494
+ [6, -82, 541, -2205, 6277, -12723, 17191, -13603, 4773]
495
+
496
+
497
+ def test_dup_compose():
498
+ assert dup_compose([], [], ZZ) == []
499
+ assert dup_compose([], [1], ZZ) == []
500
+ assert dup_compose([], [1, 2], ZZ) == []
501
+
502
+ assert dup_compose([1], [], ZZ) == [1]
503
+
504
+ assert dup_compose([1, 2, 0], [], ZZ) == []
505
+ assert dup_compose([1, 2, 1], [], ZZ) == [1]
506
+
507
+ assert dup_compose([1, 2, 1], [1], ZZ) == [4]
508
+ assert dup_compose([1, 2, 1], [7], ZZ) == [64]
509
+
510
+ assert dup_compose([1, 2, 1], [1, -1], ZZ) == [1, 0, 0]
511
+ assert dup_compose([1, 2, 1], [1, 1], ZZ) == [1, 4, 4]
512
+ assert dup_compose([1, 2, 1], [1, 2, 1], ZZ) == [1, 4, 8, 8, 4]
513
+
514
+
515
+ def test_dmp_compose():
516
+ assert dmp_compose([1, 2, 1], [1, 2, 1], 0, ZZ) == [1, 4, 8, 8, 4]
517
+
518
+ assert dmp_compose([[[]]], [[[]]], 2, ZZ) == [[[]]]
519
+ assert dmp_compose([[[]]], [[[1]]], 2, ZZ) == [[[]]]
520
+ assert dmp_compose([[[]]], [[[1]], [[2]]], 2, ZZ) == [[[]]]
521
+
522
+ assert dmp_compose([[[1]]], [], 2, ZZ) == [[[1]]]
523
+
524
+ assert dmp_compose([[1], [2], [ ]], [[]], 1, ZZ) == [[]]
525
+ assert dmp_compose([[1], [2], [1]], [[]], 1, ZZ) == [[1]]
526
+
527
+ assert dmp_compose([[1], [2], [1]], [[1]], 1, ZZ) == [[4]]
528
+ assert dmp_compose([[1], [2], [1]], [[7]], 1, ZZ) == [[64]]
529
+
530
+ assert dmp_compose([[1], [2], [1]], [[1], [-1]], 1, ZZ) == [[1], [ ], [ ]]
531
+ assert dmp_compose([[1], [2], [1]], [[1], [ 1]], 1, ZZ) == [[1], [4], [4]]
532
+
533
+ assert dmp_compose(
534
+ [[1], [2], [1]], [[1], [2], [1]], 1, ZZ) == [[1], [4], [8], [8], [4]]
535
+
536
+
537
+ def test_dup_decompose():
538
+ assert dup_decompose([1], ZZ) == [[1]]
539
+
540
+ assert dup_decompose([1, 0], ZZ) == [[1, 0]]
541
+ assert dup_decompose([1, 0, 0, 0], ZZ) == [[1, 0, 0, 0]]
542
+
543
+ assert dup_decompose([1, 0, 0, 0, 0], ZZ) == [[1, 0, 0], [1, 0, 0]]
544
+ assert dup_decompose(
545
+ [1, 0, 0, 0, 0, 0, 0], ZZ) == [[1, 0, 0, 0], [1, 0, 0]]
546
+
547
+ assert dup_decompose([7, 0, 0, 0, 1], ZZ) == [[7, 0, 1], [1, 0, 0]]
548
+ assert dup_decompose([4, 0, 3, 0, 2], ZZ) == [[4, 3, 2], [1, 0, 0]]
549
+
550
+ f = [1, 0, 20, 0, 150, 0, 500, 0, 625, -2, 0, -10, 9]
551
+
552
+ assert dup_decompose(f, ZZ) == [[1, 0, 0, -2, 9], [1, 0, 5, 0]]
553
+
554
+ f = [2, 0, 40, 0, 300, 0, 1000, 0, 1250, -4, 0, -20, 18]
555
+
556
+ assert dup_decompose(f, ZZ) == [[2, 0, 0, -4, 18], [1, 0, 5, 0]]
557
+
558
+ f = [1, 0, 20, -8, 150, -120, 524, -600, 865, -1034, 600, -170, 29]
559
+
560
+ assert dup_decompose(f, ZZ) == [[1, -8, 24, -34, 29], [1, 0, 5, 0]]
561
+
562
+ R, t = ring("t", ZZ)
563
+ f = [6*t**2 - 42,
564
+ 48*t**2 + 96,
565
+ 144*t**2 + 648*t + 288,
566
+ 624*t**2 + 864*t + 384,
567
+ 108*t**3 + 312*t**2 + 432*t + 192]
568
+
569
+ assert dup_decompose(f, R.to_domain()) == [f]
570
+
571
+
572
+ def test_dmp_lift():
573
+ q = [QQ(1, 1), QQ(0, 1), QQ(1, 1)]
574
+
575
+ f = [ANP([QQ(1, 1)], q, QQ), ANP([], q, QQ), ANP([], q, QQ),
576
+ ANP([QQ(1, 1), QQ(0, 1)], q, QQ), ANP([QQ(17, 1), QQ(0, 1)], q, QQ)]
577
+
578
+ assert dmp_lift(f, 0, QQ.algebraic_field(I)) == \
579
+ [QQ(1), QQ(0), QQ(0), QQ(0), QQ(0), QQ(0), QQ(2), QQ(0), QQ(578),
580
+ QQ(0), QQ(0), QQ(0), QQ(1), QQ(0), QQ(-578), QQ(0), QQ(83521)]
581
+
582
+ raises(DomainError, lambda: dmp_lift([EX(1), EX(2)], 0, EX))
583
+
584
+
585
+ def test_dup_sign_variations():
586
+ assert dup_sign_variations([], ZZ) == 0
587
+ assert dup_sign_variations([1, 0], ZZ) == 0
588
+ assert dup_sign_variations([1, 0, 2], ZZ) == 0
589
+ assert dup_sign_variations([1, 0, 3, 0], ZZ) == 0
590
+ assert dup_sign_variations([1, 0, 4, 0, 5], ZZ) == 0
591
+
592
+ assert dup_sign_variations([-1, 0, 2], ZZ) == 1
593
+ assert dup_sign_variations([-1, 0, 3, 0], ZZ) == 1
594
+ assert dup_sign_variations([-1, 0, 4, 0, 5], ZZ) == 1
595
+
596
+ assert dup_sign_variations([-1, -4, -5], ZZ) == 0
597
+ assert dup_sign_variations([ 1, -4, -5], ZZ) == 1
598
+ assert dup_sign_variations([ 1, 4, -5], ZZ) == 1
599
+ assert dup_sign_variations([ 1, -4, 5], ZZ) == 2
600
+ assert dup_sign_variations([-1, 4, -5], ZZ) == 2
601
+ assert dup_sign_variations([-1, 4, 5], ZZ) == 1
602
+ assert dup_sign_variations([-1, -4, 5], ZZ) == 1
603
+ assert dup_sign_variations([ 1, 4, 5], ZZ) == 0
604
+
605
+ assert dup_sign_variations([-1, 0, -4, 0, -5], ZZ) == 0
606
+ assert dup_sign_variations([ 1, 0, -4, 0, -5], ZZ) == 1
607
+ assert dup_sign_variations([ 1, 0, 4, 0, -5], ZZ) == 1
608
+ assert dup_sign_variations([ 1, 0, -4, 0, 5], ZZ) == 2
609
+ assert dup_sign_variations([-1, 0, 4, 0, -5], ZZ) == 2
610
+ assert dup_sign_variations([-1, 0, 4, 0, 5], ZZ) == 1
611
+ assert dup_sign_variations([-1, 0, -4, 0, 5], ZZ) == 1
612
+ assert dup_sign_variations([ 1, 0, 4, 0, 5], ZZ) == 0
613
+
614
+
615
+ def test_dup_clear_denoms():
616
+ assert dup_clear_denoms([], QQ, ZZ) == (ZZ(1), [])
617
+
618
+ assert dup_clear_denoms([QQ(1)], QQ, ZZ) == (ZZ(1), [QQ(1)])
619
+ assert dup_clear_denoms([QQ(7)], QQ, ZZ) == (ZZ(1), [QQ(7)])
620
+
621
+ assert dup_clear_denoms([QQ(7, 3)], QQ) == (ZZ(3), [QQ(7)])
622
+ assert dup_clear_denoms([QQ(7, 3)], QQ, ZZ) == (ZZ(3), [QQ(7)])
623
+
624
+ assert dup_clear_denoms(
625
+ [QQ(3), QQ(1), QQ(0)], QQ, ZZ) == (ZZ(1), [QQ(3), QQ(1), QQ(0)])
626
+ assert dup_clear_denoms(
627
+ [QQ(1), QQ(1, 2), QQ(0)], QQ, ZZ) == (ZZ(2), [QQ(2), QQ(1), QQ(0)])
628
+
629
+ assert dup_clear_denoms([QQ(3), QQ(
630
+ 1), QQ(0)], QQ, ZZ, convert=True) == (ZZ(1), [ZZ(3), ZZ(1), ZZ(0)])
631
+ assert dup_clear_denoms([QQ(1), QQ(
632
+ 1, 2), QQ(0)], QQ, ZZ, convert=True) == (ZZ(2), [ZZ(2), ZZ(1), ZZ(0)])
633
+
634
+ assert dup_clear_denoms(
635
+ [EX(S(3)/2), EX(S(9)/4)], EX) == (EX(4), [EX(6), EX(9)])
636
+
637
+ assert dup_clear_denoms([EX(7)], EX) == (EX(1), [EX(7)])
638
+ assert dup_clear_denoms([EX(sin(x)/x), EX(0)], EX) == (EX(x), [EX(sin(x)), EX(0)])
639
+
640
+
641
+ def test_dmp_clear_denoms():
642
+ assert dmp_clear_denoms([[]], 1, QQ, ZZ) == (ZZ(1), [[]])
643
+
644
+ assert dmp_clear_denoms([[QQ(1)]], 1, QQ, ZZ) == (ZZ(1), [[QQ(1)]])
645
+ assert dmp_clear_denoms([[QQ(7)]], 1, QQ, ZZ) == (ZZ(1), [[QQ(7)]])
646
+
647
+ assert dmp_clear_denoms([[QQ(7, 3)]], 1, QQ) == (ZZ(3), [[QQ(7)]])
648
+ assert dmp_clear_denoms([[QQ(7, 3)]], 1, QQ, ZZ) == (ZZ(3), [[QQ(7)]])
649
+
650
+ assert dmp_clear_denoms(
651
+ [[QQ(3)], [QQ(1)], []], 1, QQ, ZZ) == (ZZ(1), [[QQ(3)], [QQ(1)], []])
652
+ assert dmp_clear_denoms([[QQ(
653
+ 1)], [QQ(1, 2)], []], 1, QQ, ZZ) == (ZZ(2), [[QQ(2)], [QQ(1)], []])
654
+
655
+ assert dmp_clear_denoms([QQ(3), QQ(
656
+ 1), QQ(0)], 0, QQ, ZZ, convert=True) == (ZZ(1), [ZZ(3), ZZ(1), ZZ(0)])
657
+ assert dmp_clear_denoms([QQ(1), QQ(1, 2), QQ(
658
+ 0)], 0, QQ, ZZ, convert=True) == (ZZ(2), [ZZ(2), ZZ(1), ZZ(0)])
659
+
660
+ assert dmp_clear_denoms([[QQ(3)], [QQ(
661
+ 1)], []], 1, QQ, ZZ, convert=True) == (ZZ(1), [[QQ(3)], [QQ(1)], []])
662
+ assert dmp_clear_denoms([[QQ(1)], [QQ(1, 2)], []], 1, QQ, ZZ,
663
+ convert=True) == (ZZ(2), [[QQ(2)], [QQ(1)], []])
664
+
665
+ assert dmp_clear_denoms(
666
+ [[EX(S(3)/2)], [EX(S(9)/4)]], 1, EX) == (EX(4), [[EX(6)], [EX(9)]])
667
+ assert dmp_clear_denoms([[EX(7)]], 1, EX) == (EX(1), [[EX(7)]])
668
+ assert dmp_clear_denoms([[EX(sin(x)/x), EX(0)]], 1, EX) == (EX(x), [[EX(sin(x)), EX(0)]])
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_dispersion.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core import Symbol, S, oo
2
+ from sympy.functions.elementary.miscellaneous import sqrt
3
+ from sympy.polys import poly
4
+ from sympy.polys.dispersion import dispersion, dispersionset
5
+
6
+
7
+ def test_dispersion():
8
+ x = Symbol("x")
9
+ a = Symbol("a")
10
+
11
+ fp = poly(S.Zero, x)
12
+ assert sorted(dispersionset(fp)) == [0]
13
+
14
+ fp = poly(S(2), x)
15
+ assert sorted(dispersionset(fp)) == [0]
16
+
17
+ fp = poly(x + 1, x)
18
+ assert sorted(dispersionset(fp)) == [0]
19
+ assert dispersion(fp) == 0
20
+
21
+ fp = poly((x + 1)*(x + 2), x)
22
+ assert sorted(dispersionset(fp)) == [0, 1]
23
+ assert dispersion(fp) == 1
24
+
25
+ fp = poly(x*(x + 3), x)
26
+ assert sorted(dispersionset(fp)) == [0, 3]
27
+ assert dispersion(fp) == 3
28
+
29
+ fp = poly((x - 3)*(x + 3), x)
30
+ assert sorted(dispersionset(fp)) == [0, 6]
31
+ assert dispersion(fp) == 6
32
+
33
+ fp = poly(x**4 - 3*x**2 + 1, x)
34
+ gp = fp.shift(-3)
35
+ assert sorted(dispersionset(fp, gp)) == [2, 3, 4]
36
+ assert dispersion(fp, gp) == 4
37
+ assert sorted(dispersionset(gp, fp)) == []
38
+ assert dispersion(gp, fp) is -oo
39
+
40
+ fp = poly(x*(3*x**2+a)*(x-2536)*(x**3+a), x)
41
+ gp = fp.as_expr().subs(x, x-345).as_poly(x)
42
+ assert sorted(dispersionset(fp, gp)) == [345, 2881]
43
+ assert sorted(dispersionset(gp, fp)) == [2191]
44
+
45
+ gp = poly((x-2)**2*(x-3)**3*(x-5)**3, x)
46
+ assert sorted(dispersionset(gp)) == [0, 1, 2, 3]
47
+ assert sorted(dispersionset(gp, (gp+4)**2)) == [1, 2]
48
+
49
+ fp = poly(x*(x+2)*(x-1), x)
50
+ assert sorted(dispersionset(fp)) == [0, 1, 2, 3]
51
+
52
+ fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>')
53
+ gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>')
54
+ assert sorted(dispersionset(fp, gp)) == [2]
55
+ assert sorted(dispersionset(gp, fp)) == [1, 4]
56
+
57
+ # There are some difficulties if we compute over Z[a]
58
+ # and alpha happenes to lie in Z[a] instead of simply Z.
59
+ # Hence we can not decide if alpha is indeed integral
60
+ # in general.
61
+
62
+ fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x)
63
+ assert sorted(dispersionset(fp)) == [0, 1]
64
+
65
+ # For any specific value of a, the dispersion is 3*a
66
+ # but the algorithm can not find this in general.
67
+ # This is the point where the resultant based Ansatz
68
+ # is superior to the current one.
69
+ fp = poly(a**2*x**3 + (a**3 + a**2 + a + 1)*x, x)
70
+ gp = fp.as_expr().subs(x, x - 3*a).as_poly(x)
71
+ assert sorted(dispersionset(fp, gp)) == []
72
+
73
+ fpa = fp.as_expr().subs(a, 2).as_poly(x)
74
+ gpa = gp.as_expr().subs(a, 2).as_poly(x)
75
+ assert sorted(dispersionset(fpa, gpa)) == [6]
76
+
77
+ # Work with Expr instead of Poly
78
+ f = (x + 1)*(x + 2)
79
+ assert sorted(dispersionset(f)) == [0, 1]
80
+ assert dispersion(f) == 1
81
+
82
+ f = x**4 - 3*x**2 + 1
83
+ g = x**4 - 12*x**3 + 51*x**2 - 90*x + 55
84
+ assert sorted(dispersionset(f, g)) == [2, 3, 4]
85
+ assert dispersion(f, g) == 4
86
+
87
+ # Work with Expr and specify a generator
88
+ f = (x + 1)*(x + 2)
89
+ assert sorted(dispersionset(f, None, x)) == [0, 1]
90
+ assert dispersion(f, None, x) == 1
91
+
92
+ f = x**4 - 3*x**2 + 1
93
+ g = x**4 - 12*x**3 + 51*x**2 - 90*x + 55
94
+ assert sorted(dispersionset(f, g, x)) == [2, 3, 4]
95
+ assert dispersion(f, g, x) == 4
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_distributedmodules.py ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for sparse distributed modules. """
2
+
3
+ from sympy.polys.distributedmodules import (
4
+ sdm_monomial_mul, sdm_monomial_deg, sdm_monomial_divides,
5
+ sdm_add, sdm_LM, sdm_LT, sdm_mul_term, sdm_zero, sdm_deg,
6
+ sdm_LC, sdm_from_dict,
7
+ sdm_spoly, sdm_ecart, sdm_nf_mora, sdm_groebner,
8
+ sdm_from_vector, sdm_to_vector, sdm_monomial_lcm
9
+ )
10
+
11
+ from sympy.polys.orderings import lex, grlex, InverseOrder
12
+ from sympy.polys.domains import QQ
13
+
14
+ from sympy.abc import x, y, z
15
+
16
+
17
+ def test_sdm_monomial_mul():
18
+ assert sdm_monomial_mul((1, 1, 0), (1, 3)) == (1, 2, 3)
19
+
20
+
21
+ def test_sdm_monomial_deg():
22
+ assert sdm_monomial_deg((5, 2, 1)) == 3
23
+
24
+
25
+ def test_sdm_monomial_lcm():
26
+ assert sdm_monomial_lcm((1, 2, 3), (1, 5, 0)) == (1, 5, 3)
27
+
28
+
29
+ def test_sdm_monomial_divides():
30
+ assert sdm_monomial_divides((1, 0, 0), (1, 0, 0)) is True
31
+ assert sdm_monomial_divides((1, 0, 0), (1, 2, 1)) is True
32
+ assert sdm_monomial_divides((5, 1, 1), (5, 2, 1)) is True
33
+
34
+ assert sdm_monomial_divides((1, 0, 0), (2, 0, 0)) is False
35
+ assert sdm_monomial_divides((1, 1, 0), (1, 0, 0)) is False
36
+ assert sdm_monomial_divides((5, 1, 2), (5, 0, 1)) is False
37
+
38
+
39
+ def test_sdm_LC():
40
+ assert sdm_LC([((1, 2, 3), QQ(5))], QQ) == QQ(5)
41
+
42
+
43
+ def test_sdm_from_dict():
44
+ dic = {(1, 2, 1, 1): QQ(1), (1, 1, 2, 1): QQ(1), (1, 0, 2, 1): QQ(1),
45
+ (1, 0, 0, 3): QQ(1), (1, 1, 1, 0): QQ(1)}
46
+ assert sdm_from_dict(dic, grlex) == \
47
+ [((1, 2, 1, 1), QQ(1)), ((1, 1, 2, 1), QQ(1)),
48
+ ((1, 0, 2, 1), QQ(1)), ((1, 0, 0, 3), QQ(1)), ((1, 1, 1, 0), QQ(1))]
49
+
50
+ # TODO test to_dict?
51
+
52
+
53
+ def test_sdm_add():
54
+ assert sdm_add([((1, 1, 1), QQ(1))], [((2, 0, 0), QQ(1))], lex, QQ) == \
55
+ [((2, 0, 0), QQ(1)), ((1, 1, 1), QQ(1))]
56
+ assert sdm_add([((1, 1, 1), QQ(1))], [((1, 1, 1), QQ(-1))], lex, QQ) == []
57
+ assert sdm_add([((1, 0, 0), QQ(1))], [((1, 0, 0), QQ(2))], lex, QQ) == \
58
+ [((1, 0, 0), QQ(3))]
59
+ assert sdm_add([((1, 0, 1), QQ(1))], [((1, 1, 0), QQ(1))], lex, QQ) == \
60
+ [((1, 1, 0), QQ(1)), ((1, 0, 1), QQ(1))]
61
+
62
+
63
+ def test_sdm_LM():
64
+ dic = {(1, 2, 3): QQ(1), (4, 0, 0): QQ(1), (4, 0, 1): QQ(1)}
65
+ assert sdm_LM(sdm_from_dict(dic, lex)) == (4, 0, 1)
66
+
67
+
68
+ def test_sdm_LT():
69
+ dic = {(1, 2, 3): QQ(1), (4, 0, 0): QQ(2), (4, 0, 1): QQ(3)}
70
+ assert sdm_LT(sdm_from_dict(dic, lex)) == ((4, 0, 1), QQ(3))
71
+
72
+
73
+ def test_sdm_mul_term():
74
+ assert sdm_mul_term([((1, 0, 0), QQ(1))], ((0, 0), QQ(0)), lex, QQ) == []
75
+ assert sdm_mul_term([], ((1, 0), QQ(1)), lex, QQ) == []
76
+ assert sdm_mul_term([((1, 0, 0), QQ(1))], ((1, 0), QQ(1)), lex, QQ) == \
77
+ [((1, 1, 0), QQ(1))]
78
+ f = [((2, 0, 1), QQ(4)), ((1, 1, 0), QQ(3))]
79
+ assert sdm_mul_term(f, ((1, 1), QQ(2)), lex, QQ) == \
80
+ [((2, 1, 2), QQ(8)), ((1, 2, 1), QQ(6))]
81
+
82
+
83
+ def test_sdm_zero():
84
+ assert sdm_zero() == []
85
+
86
+
87
+ def test_sdm_deg():
88
+ assert sdm_deg([((1, 2, 3), 1), ((10, 0, 1), 1), ((2, 3, 4), 4)]) == 7
89
+
90
+
91
+ def test_sdm_spoly():
92
+ f = [((2, 1, 1), QQ(1)), ((1, 0, 1), QQ(1))]
93
+ g = [((2, 3, 0), QQ(1))]
94
+ h = [((1, 2, 3), QQ(1))]
95
+ assert sdm_spoly(f, h, lex, QQ) == []
96
+ assert sdm_spoly(f, g, lex, QQ) == [((1, 2, 1), QQ(1))]
97
+
98
+
99
+ def test_sdm_ecart():
100
+ assert sdm_ecart([((1, 2, 3), 1), ((1, 0, 1), 1)]) == 0
101
+ assert sdm_ecart([((2, 2, 1), 1), ((1, 5, 1), 1)]) == 3
102
+
103
+
104
+ def test_sdm_nf_mora():
105
+ f = sdm_from_dict({(1, 2, 1, 1): QQ(1), (1, 1, 2, 1): QQ(1),
106
+ (1, 0, 2, 1): QQ(1), (1, 0, 0, 3): QQ(1), (1, 1, 1, 0): QQ(1)},
107
+ grlex)
108
+ f1 = sdm_from_dict({(1, 1, 1, 0): QQ(1), (1, 0, 2, 0): QQ(1),
109
+ (1, 0, 0, 0): QQ(-1)}, grlex)
110
+ f2 = sdm_from_dict({(1, 1, 1, 0): QQ(1)}, grlex)
111
+ (id0, id1, id2) = [sdm_from_dict({(i, 0, 0, 0): QQ(1)}, grlex)
112
+ for i in range(3)]
113
+
114
+ assert sdm_nf_mora(f, [f1, f2], grlex, QQ, phantom=(id0, [id1, id2])) == \
115
+ ([((1, 0, 2, 1), QQ(1)), ((1, 0, 0, 3), QQ(1)), ((1, 1, 1, 0), QQ(1)),
116
+ ((1, 1, 0, 1), QQ(1))],
117
+ [((1, 1, 0, 1), QQ(-1)), ((0, 0, 0, 0), QQ(1))])
118
+ assert sdm_nf_mora(f, [f2, f1], grlex, QQ, phantom=(id0, [id2, id1])) == \
119
+ ([((1, 0, 2, 1), QQ(1)), ((1, 0, 0, 3), QQ(1)), ((1, 1, 1, 0), QQ(1))],
120
+ [((2, 1, 0, 1), QQ(-1)), ((2, 0, 1, 1), QQ(-1)), ((0, 0, 0, 0), QQ(1))])
121
+
122
+ f = sdm_from_vector([x*z, y**2 + y*z - z, y], lex, QQ, gens=[x, y, z])
123
+ f1 = sdm_from_vector([x, y, 1], lex, QQ, gens=[x, y, z])
124
+ f2 = sdm_from_vector([x*y, z, z**2], lex, QQ, gens=[x, y, z])
125
+ assert sdm_nf_mora(f, [f1, f2], lex, QQ) == \
126
+ sdm_nf_mora(f, [f2, f1], lex, QQ) == \
127
+ [((1, 0, 1, 1), QQ(1)), ((1, 0, 0, 1), QQ(-1)), ((0, 1, 1, 0), QQ(-1)),
128
+ ((0, 1, 0, 1), QQ(1))]
129
+
130
+
131
+ def test_conversion():
132
+ f = [x**2 + y**2, 2*z]
133
+ g = [((1, 0, 0, 1), QQ(2)), ((0, 2, 0, 0), QQ(1)), ((0, 0, 2, 0), QQ(1))]
134
+ assert sdm_to_vector(g, [x, y, z], QQ) == f
135
+ assert sdm_from_vector(f, lex, QQ) == g
136
+ assert sdm_from_vector(
137
+ [x, 1], lex, QQ) == [((1, 0), QQ(1)), ((0, 1), QQ(1))]
138
+ assert sdm_to_vector([((1, 1, 0, 0), 1)], [x, y, z], QQ, n=3) == [0, x, 0]
139
+ assert sdm_from_vector([0, 0], lex, QQ, gens=[x, y]) == sdm_zero()
140
+
141
+
142
+ def test_nontrivial():
143
+ gens = [x, y, z]
144
+
145
+ def contains(I, f):
146
+ S = [sdm_from_vector([g], lex, QQ, gens=gens) for g in I]
147
+ G = sdm_groebner(S, sdm_nf_mora, lex, QQ)
148
+ return sdm_nf_mora(sdm_from_vector([f], lex, QQ, gens=gens),
149
+ G, lex, QQ) == sdm_zero()
150
+
151
+ assert contains([x, y], x)
152
+ assert contains([x, y], x + y)
153
+ assert not contains([x, y], 1)
154
+ assert not contains([x, y], z)
155
+ assert contains([x**2 + y, x**2 + x], x - y)
156
+ assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
157
+ assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**3)
158
+ assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4)
159
+ assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y**2)
160
+ assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4 + y**3 + 2*z*y*x)
161
+ assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y*z)
162
+ assert contains([x, 1 + x + y, 5 - 7*y], 1)
163
+ assert contains(
164
+ [x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
165
+ x**3)
166
+ assert not contains(
167
+ [x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
168
+ x**2 + y**2)
169
+
170
+ # compare local order
171
+ assert not contains([x*(1 + x + y), y*(1 + z)], x)
172
+ assert not contains([x*(1 + x + y), y*(1 + z)], x + y)
173
+
174
+
175
+ def test_local():
176
+ igrlex = InverseOrder(grlex)
177
+ gens = [x, y, z]
178
+
179
+ def contains(I, f):
180
+ S = [sdm_from_vector([g], igrlex, QQ, gens=gens) for g in I]
181
+ G = sdm_groebner(S, sdm_nf_mora, igrlex, QQ)
182
+ return sdm_nf_mora(sdm_from_vector([f], lex, QQ, gens=gens),
183
+ G, lex, QQ) == sdm_zero()
184
+ assert contains([x, y], x)
185
+ assert contains([x, y], x + y)
186
+ assert not contains([x, y], 1)
187
+ assert not contains([x, y], z)
188
+ assert contains([x**2 + y, x**2 + x], x - y)
189
+ assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
190
+ assert contains([x*(1 + x + y), y*(1 + z)], x)
191
+ assert contains([x*(1 + x + y), y*(1 + z)], x + y)
192
+
193
+
194
+ def test_uncovered_line():
195
+ gens = [x, y]
196
+ f1 = sdm_zero()
197
+ f2 = sdm_from_vector([x, 0], lex, QQ, gens=gens)
198
+ f3 = sdm_from_vector([0, y], lex, QQ, gens=gens)
199
+
200
+ assert sdm_spoly(f1, f2, lex, QQ) == sdm_zero()
201
+ assert sdm_spoly(f3, f2, lex, QQ) == sdm_zero()
202
+
203
+
204
+ def test_chain_criterion():
205
+ gens = [x]
206
+ f1 = sdm_from_vector([1, x], grlex, QQ, gens=gens)
207
+ f2 = sdm_from_vector([0, x - 2], grlex, QQ, gens=gens)
208
+ assert len(sdm_groebner([f1, f2], sdm_nf_mora, grlex, QQ)) == 2
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_euclidtools.py ADDED
@@ -0,0 +1,712 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for Euclidean algorithms, GCDs, LCMs and polynomial remainder sequences. """
2
+
3
+ from sympy.polys.rings import ring
4
+ from sympy.polys.domains import ZZ, QQ, RR
5
+
6
+ from sympy.polys.specialpolys import (
7
+ f_polys,
8
+ dmp_fateman_poly_F_1,
9
+ dmp_fateman_poly_F_2,
10
+ dmp_fateman_poly_F_3)
11
+
12
+ f_0, f_1, f_2, f_3, f_4, f_5, f_6 = f_polys()
13
+
14
+ def test_dup_gcdex():
15
+ R, x = ring("x", QQ)
16
+
17
+ f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
18
+ g = x**3 + x**2 - 4*x - 4
19
+
20
+ s = -QQ(1,5)*x + QQ(3,5)
21
+ t = QQ(1,5)*x**2 - QQ(6,5)*x + 2
22
+ h = x + 1
23
+
24
+ assert R.dup_half_gcdex(f, g) == (s, h)
25
+ assert R.dup_gcdex(f, g) == (s, t, h)
26
+
27
+ f = x**4 + 4*x**3 - x + 1
28
+ g = x**3 - x + 1
29
+
30
+ s, t, h = R.dup_gcdex(f, g)
31
+ S, T, H = R.dup_gcdex(g, f)
32
+
33
+ assert R.dup_add(R.dup_mul(s, f),
34
+ R.dup_mul(t, g)) == h
35
+ assert R.dup_add(R.dup_mul(S, g),
36
+ R.dup_mul(T, f)) == H
37
+
38
+ f = 2*x
39
+ g = x**2 - 16
40
+
41
+ s = QQ(1,32)*x
42
+ t = -QQ(1,16)
43
+ h = 1
44
+
45
+ assert R.dup_half_gcdex(f, g) == (s, h)
46
+ assert R.dup_gcdex(f, g) == (s, t, h)
47
+
48
+
49
+ def test_dup_invert():
50
+ R, x = ring("x", QQ)
51
+ assert R.dup_invert(2*x, x**2 - 16) == QQ(1,32)*x
52
+
53
+
54
+ def test_dup_euclidean_prs():
55
+ R, x = ring("x", QQ)
56
+
57
+ f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
58
+ g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
59
+
60
+ assert R.dup_euclidean_prs(f, g) == [
61
+ f,
62
+ g,
63
+ -QQ(5,9)*x**4 + QQ(1,9)*x**2 - QQ(1,3),
64
+ -QQ(117,25)*x**2 - 9*x + QQ(441,25),
65
+ QQ(233150,19773)*x - QQ(102500,6591),
66
+ -QQ(1288744821,543589225)]
67
+
68
+
69
+ def test_dup_primitive_prs():
70
+ R, x = ring("x", ZZ)
71
+
72
+ f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
73
+ g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
74
+
75
+ assert R.dup_primitive_prs(f, g) == [
76
+ f,
77
+ g,
78
+ -5*x**4 + x**2 - 3,
79
+ 13*x**2 + 25*x - 49,
80
+ 4663*x - 6150,
81
+ 1]
82
+
83
+
84
+ def test_dup_subresultants():
85
+ R, x = ring("x", ZZ)
86
+
87
+ assert R.dup_resultant(0, 0) == 0
88
+
89
+ assert R.dup_resultant(1, 0) == 0
90
+ assert R.dup_resultant(0, 1) == 0
91
+
92
+ f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
93
+ g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
94
+
95
+ a = 15*x**4 - 3*x**2 + 9
96
+ b = 65*x**2 + 125*x - 245
97
+ c = 9326*x - 12300
98
+ d = 260708
99
+
100
+ assert R.dup_subresultants(f, g) == [f, g, a, b, c, d]
101
+ assert R.dup_resultant(f, g) == R.dup_LC(d)
102
+
103
+ f = x**2 - 2*x + 1
104
+ g = x**2 - 1
105
+
106
+ a = 2*x - 2
107
+
108
+ assert R.dup_subresultants(f, g) == [f, g, a]
109
+ assert R.dup_resultant(f, g) == 0
110
+
111
+ f = x**2 + 1
112
+ g = x**2 - 1
113
+
114
+ a = -2
115
+
116
+ assert R.dup_subresultants(f, g) == [f, g, a]
117
+ assert R.dup_resultant(f, g) == 4
118
+
119
+ f = x**2 - 1
120
+ g = x**3 - x**2 + 2
121
+
122
+ assert R.dup_resultant(f, g) == 0
123
+
124
+ f = 3*x**3 - x
125
+ g = 5*x**2 + 1
126
+
127
+ assert R.dup_resultant(f, g) == 64
128
+
129
+ f = x**2 - 2*x + 7
130
+ g = x**3 - x + 5
131
+
132
+ assert R.dup_resultant(f, g) == 265
133
+
134
+ f = x**3 - 6*x**2 + 11*x - 6
135
+ g = x**3 - 15*x**2 + 74*x - 120
136
+
137
+ assert R.dup_resultant(f, g) == -8640
138
+
139
+ f = x**3 - 6*x**2 + 11*x - 6
140
+ g = x**3 - 10*x**2 + 29*x - 20
141
+
142
+ assert R.dup_resultant(f, g) == 0
143
+
144
+ f = x**3 - 1
145
+ g = x**3 + 2*x**2 + 2*x - 1
146
+
147
+ assert R.dup_resultant(f, g) == 16
148
+
149
+ f = x**8 - 2
150
+ g = x - 1
151
+
152
+ assert R.dup_resultant(f, g) == -1
153
+
154
+
155
+ def test_dmp_subresultants():
156
+ R, x, y = ring("x,y", ZZ)
157
+
158
+ assert R.dmp_resultant(0, 0) == 0
159
+ assert R.dmp_prs_resultant(0, 0)[0] == 0
160
+ assert R.dmp_zz_collins_resultant(0, 0) == 0
161
+ assert R.dmp_qq_collins_resultant(0, 0) == 0
162
+
163
+ assert R.dmp_resultant(1, 0) == 0
164
+ assert R.dmp_resultant(1, 0) == 0
165
+ assert R.dmp_resultant(1, 0) == 0
166
+
167
+ assert R.dmp_resultant(0, 1) == 0
168
+ assert R.dmp_prs_resultant(0, 1)[0] == 0
169
+ assert R.dmp_zz_collins_resultant(0, 1) == 0
170
+ assert R.dmp_qq_collins_resultant(0, 1) == 0
171
+
172
+ f = 3*x**2*y - y**3 - 4
173
+ g = x**2 + x*y**3 - 9
174
+
175
+ a = 3*x*y**4 + y**3 - 27*y + 4
176
+ b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16
177
+
178
+ r = R.dmp_LC(b)
179
+
180
+ assert R.dmp_subresultants(f, g) == [f, g, a, b]
181
+
182
+ assert R.dmp_resultant(f, g) == r
183
+ assert R.dmp_prs_resultant(f, g)[0] == r
184
+ assert R.dmp_zz_collins_resultant(f, g) == r
185
+ assert R.dmp_qq_collins_resultant(f, g) == r
186
+
187
+ f = -x**3 + 5
188
+ g = 3*x**2*y + x**2
189
+
190
+ a = 45*y**2 + 30*y + 5
191
+ b = 675*y**3 + 675*y**2 + 225*y + 25
192
+
193
+ r = R.dmp_LC(b)
194
+
195
+ assert R.dmp_subresultants(f, g) == [f, g, a]
196
+ assert R.dmp_resultant(f, g) == r
197
+ assert R.dmp_prs_resultant(f, g)[0] == r
198
+ assert R.dmp_zz_collins_resultant(f, g) == r
199
+ assert R.dmp_qq_collins_resultant(f, g) == r
200
+
201
+ R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
202
+
203
+ f = 6*x**2 - 3*x*y - 2*x*z + y*z
204
+ g = x**2 - x*u - x*v + u*v
205
+
206
+ r = y**2*z**2 - 3*y**2*z*u - 3*y**2*z*v + 9*y**2*u*v - 2*y*z**2*u \
207
+ - 2*y*z**2*v + 6*y*z*u**2 + 12*y*z*u*v + 6*y*z*v**2 - 18*y*u**2*v \
208
+ - 18*y*u*v**2 + 4*z**2*u*v - 12*z*u**2*v - 12*z*u*v**2 + 36*u**2*v**2
209
+
210
+ assert R.dmp_zz_collins_resultant(f, g) == r.drop(x)
211
+
212
+ R, x, y, z, u, v = ring("x,y,z,u,v", QQ)
213
+
214
+ f = x**2 - QQ(1,2)*x*y - QQ(1,3)*x*z + QQ(1,6)*y*z
215
+ g = x**2 - x*u - x*v + u*v
216
+
217
+ r = QQ(1,36)*y**2*z**2 - QQ(1,12)*y**2*z*u - QQ(1,12)*y**2*z*v + QQ(1,4)*y**2*u*v \
218
+ - QQ(1,18)*y*z**2*u - QQ(1,18)*y*z**2*v + QQ(1,6)*y*z*u**2 + QQ(1,3)*y*z*u*v \
219
+ + QQ(1,6)*y*z*v**2 - QQ(1,2)*y*u**2*v - QQ(1,2)*y*u*v**2 + QQ(1,9)*z**2*u*v \
220
+ - QQ(1,3)*z*u**2*v - QQ(1,3)*z*u*v**2 + u**2*v**2
221
+
222
+ assert R.dmp_qq_collins_resultant(f, g) == r.drop(x)
223
+
224
+ Rt, t = ring("t", ZZ)
225
+ Rx, x = ring("x", Rt)
226
+
227
+ f = x**6 - 5*x**4 + 5*x**2 + 4
228
+ g = -6*t*x**5 + x**4 + 20*t*x**3 - 3*x**2 - 10*t*x + 6
229
+
230
+ assert Rx.dup_resultant(f, g) == 2930944*t**6 + 2198208*t**4 + 549552*t**2 + 45796
231
+
232
+
233
+ def test_dup_discriminant():
234
+ R, x = ring("x", ZZ)
235
+
236
+ assert R.dup_discriminant(0) == 0
237
+ assert R.dup_discriminant(x) == 1
238
+
239
+ assert R.dup_discriminant(x**3 + 3*x**2 + 9*x - 13) == -11664
240
+ assert R.dup_discriminant(5*x**5 + x**3 + 2) == 31252160
241
+ assert R.dup_discriminant(x**4 + 2*x**3 + 6*x**2 - 22*x + 13) == 0
242
+ assert R.dup_discriminant(12*x**7 + 15*x**4 + 30*x**3 + x**2 + 1) == -220289699947514112
243
+
244
+
245
+ def test_dmp_discriminant():
246
+ R, x = ring("x", ZZ)
247
+
248
+ assert R.dmp_discriminant(0) == 0
249
+
250
+ R, x, y = ring("x,y", ZZ)
251
+
252
+ assert R.dmp_discriminant(0) == 0
253
+ assert R.dmp_discriminant(y) == 0
254
+
255
+ assert R.dmp_discriminant(x**3 + 3*x**2 + 9*x - 13) == -11664
256
+ assert R.dmp_discriminant(5*x**5 + x**3 + 2) == 31252160
257
+ assert R.dmp_discriminant(x**4 + 2*x**3 + 6*x**2 - 22*x + 13) == 0
258
+ assert R.dmp_discriminant(12*x**7 + 15*x**4 + 30*x**3 + x**2 + 1) == -220289699947514112
259
+
260
+ assert R.dmp_discriminant(x**2*y + 2*y) == (-8*y**2).drop(x)
261
+ assert R.dmp_discriminant(x*y**2 + 2*x) == 1
262
+
263
+ R, x, y, z = ring("x,y,z", ZZ)
264
+ assert R.dmp_discriminant(x*y + z) == 1
265
+
266
+ R, x, y, z, u = ring("x,y,z,u", ZZ)
267
+ assert R.dmp_discriminant(x**2*y + x*z + u) == (-4*y*u + z**2).drop(x)
268
+
269
+ R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
270
+ assert R.dmp_discriminant(x**3*y + x**2*z + x*u + v) == \
271
+ (-27*y**2*v**2 + 18*y*z*u*v - 4*y*u**3 - 4*z**3*v + z**2*u**2).drop(x)
272
+
273
+
274
+ def test_dup_gcd():
275
+ R, x = ring("x", ZZ)
276
+
277
+ f, g = 0, 0
278
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (0, 0, 0)
279
+
280
+ f, g = 2, 0
281
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 0)
282
+
283
+ f, g = -2, 0
284
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 0)
285
+
286
+ f, g = 0, -2
287
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 0, -1)
288
+
289
+ f, g = 0, 2*x + 4
290
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 0, 1)
291
+
292
+ f, g = 2*x + 4, 0
293
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 1, 0)
294
+
295
+ f, g = 2, 2
296
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 1)
297
+
298
+ f, g = -2, 2
299
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 1)
300
+
301
+ f, g = 2, -2
302
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, -1)
303
+
304
+ f, g = -2, -2
305
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, -1)
306
+
307
+ f, g = x**2 + 2*x + 1, 1
308
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1)
309
+
310
+ f, g = x**2 + 2*x + 1, 2
311
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2)
312
+
313
+ f, g = 2*x**2 + 4*x + 2, 2
314
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1)
315
+
316
+ f, g = 2, 2*x**2 + 4*x + 2
317
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1)
318
+
319
+ f, g = 2*x**2 + 4*x + 2, x + 1
320
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1)
321
+
322
+ f, g = x + 1, 2*x**2 + 4*x + 2
323
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2)
324
+
325
+ f, g = x - 31, x
326
+ assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, f, g)
327
+
328
+ f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
329
+ g = x**3 + 6*x**2 + 11*x + 6
330
+
331
+ h = x**2 + 3*x + 2
332
+
333
+ cff = x**2 + 5*x + 4
334
+ cfg = x + 3
335
+
336
+ assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
337
+ assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)
338
+
339
+ f = x**4 - 4
340
+ g = x**4 + 4*x**2 + 4
341
+
342
+ h = x**2 + 2
343
+
344
+ cff = x**2 - 2
345
+ cfg = x**2 + 2
346
+
347
+ assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
348
+ assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)
349
+
350
+ f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
351
+ g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
352
+
353
+ h = 1
354
+
355
+ cff = f
356
+ cfg = g
357
+
358
+ assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
359
+ assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)
360
+
361
+ R, x = ring("x", QQ)
362
+
363
+ f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
364
+ g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
365
+
366
+ h = 1
367
+
368
+ cff = f
369
+ cfg = g
370
+
371
+ assert R.dup_qq_heu_gcd(f, g) == (h, cff, cfg)
372
+ assert R.dup_ff_prs_gcd(f, g) == (h, cff, cfg)
373
+
374
+ R, x = ring("x", ZZ)
375
+
376
+ f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \
377
+ + 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \
378
+ + 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \
379
+ + 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \
380
+ - 12278371209708240950316872681744825481125965781519138077173235712*x**21 \
381
+ + 289127344604779611146960547954288113529690984687482920704*x**14 \
382
+ + 19007977035740498977629742919480623972236450681*x**7 \
383
+ + 311973482284542371301330321821976049
384
+
385
+ g = 365431878023781158602430064717380211405897160759702125019136*x**21 \
386
+ + 197599133478719444145775798221171663643171734081650688*x**14 \
387
+ - 9504116979659010018253915765478924103928886144*x**7 \
388
+ - 311973482284542371301330321821976049
389
+
390
+ assert R.dup_zz_heu_gcd(f, R.dup_diff(f, 1))[0] == g
391
+ assert R.dup_rr_prs_gcd(f, R.dup_diff(f, 1))[0] == g
392
+
393
+ R, x = ring("x", QQ)
394
+
395
+ f = QQ(1,2)*x**2 + x + QQ(1,2)
396
+ g = QQ(1,2)*x + QQ(1,2)
397
+
398
+ h = x + 1
399
+
400
+ assert R.dup_qq_heu_gcd(f, g) == (h, g, QQ(1,2))
401
+ assert R.dup_ff_prs_gcd(f, g) == (h, g, QQ(1,2))
402
+
403
+ R, x = ring("x", ZZ)
404
+
405
+ f = 1317378933230047068160*x + 2945748836994210856960
406
+ g = 120352542776360960*x + 269116466014453760
407
+
408
+ h = 120352542776360960*x + 269116466014453760
409
+ cff = 10946
410
+ cfg = 1
411
+
412
+ assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
413
+
414
+
415
+ def test_dmp_gcd():
416
+ R, x, y = ring("x,y", ZZ)
417
+
418
+ f, g = 0, 0
419
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (0, 0, 0)
420
+
421
+ f, g = 2, 0
422
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 0)
423
+
424
+ f, g = -2, 0
425
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 0)
426
+
427
+ f, g = 0, -2
428
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 0, -1)
429
+
430
+ f, g = 0, 2*x + 4
431
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 0, 1)
432
+
433
+ f, g = 2*x + 4, 0
434
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 1, 0)
435
+
436
+ f, g = 2, 2
437
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 1)
438
+
439
+ f, g = -2, 2
440
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 1)
441
+
442
+ f, g = 2, -2
443
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, -1)
444
+
445
+ f, g = -2, -2
446
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, -1)
447
+
448
+ f, g = x**2 + 2*x + 1, 1
449
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1)
450
+
451
+ f, g = x**2 + 2*x + 1, 2
452
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2)
453
+
454
+ f, g = 2*x**2 + 4*x + 2, 2
455
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1)
456
+
457
+ f, g = 2, 2*x**2 + 4*x + 2
458
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1)
459
+
460
+ f, g = 2*x**2 + 4*x + 2, x + 1
461
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1)
462
+
463
+ f, g = x + 1, 2*x**2 + 4*x + 2
464
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2)
465
+
466
+ R, x, y, z, u = ring("x,y,z,u", ZZ)
467
+
468
+ f, g = u**2 + 2*u + 1, 2*u + 2
469
+ assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (u + 1, u + 1, 2)
470
+
471
+ f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1
472
+ h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1
473
+
474
+ assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg)
475
+ assert R.dmp_rr_prs_gcd(f, g) == (h, cff, cfg)
476
+
477
+ assert R.dmp_zz_heu_gcd(g, f) == (h, cfg, cff)
478
+ assert R.dmp_rr_prs_gcd(g, f) == (h, cfg, cff)
479
+
480
+ R, x, y, z = ring("x,y,z", ZZ)
481
+
482
+ f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(2, ZZ))
483
+ H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
484
+
485
+ assert H == h and R.dmp_mul(H, cff) == f \
486
+ and R.dmp_mul(H, cfg) == g
487
+
488
+ H, cff, cfg = R.dmp_rr_prs_gcd(f, g)
489
+
490
+ assert H == h and R.dmp_mul(H, cff) == f \
491
+ and R.dmp_mul(H, cfg) == g
492
+
493
+ R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
494
+
495
+ f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(4, ZZ))
496
+ H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
497
+
498
+ assert H == h and R.dmp_mul(H, cff) == f \
499
+ and R.dmp_mul(H, cfg) == g
500
+
501
+ R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ)
502
+
503
+ f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(6, ZZ))
504
+ H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
505
+
506
+ assert H == h and R.dmp_mul(H, cff) == f \
507
+ and R.dmp_mul(H, cfg) == g
508
+
509
+ R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ)
510
+
511
+ f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(8, ZZ))
512
+ H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
513
+
514
+ assert H == h and R.dmp_mul(H, cff) == f \
515
+ and R.dmp_mul(H, cfg) == g
516
+
517
+ R, x, y, z = ring("x,y,z", ZZ)
518
+
519
+ f, g, h = map(R.from_dense, dmp_fateman_poly_F_2(2, ZZ))
520
+ H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
521
+
522
+ assert H == h and R.dmp_mul(H, cff) == f \
523
+ and R.dmp_mul(H, cfg) == g
524
+
525
+ H, cff, cfg = R.dmp_rr_prs_gcd(f, g)
526
+
527
+ assert H == h and R.dmp_mul(H, cff) == f \
528
+ and R.dmp_mul(H, cfg) == g
529
+
530
+ f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(2, ZZ))
531
+ H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
532
+
533
+ assert H == h and R.dmp_mul(H, cff) == f \
534
+ and R.dmp_mul(H, cfg) == g
535
+
536
+ H, cff, cfg = R.dmp_rr_prs_gcd(f, g)
537
+
538
+ assert H == h and R.dmp_mul(H, cff) == f \
539
+ and R.dmp_mul(H, cfg) == g
540
+
541
+ R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
542
+
543
+ f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(4, ZZ))
544
+ H, cff, cfg = R.dmp_inner_gcd(f, g)
545
+
546
+ assert H == h and R.dmp_mul(H, cff) == f \
547
+ and R.dmp_mul(H, cfg) == g
548
+
549
+ R, x, y = ring("x,y", QQ)
550
+
551
+ f = QQ(1,2)*x**2 + x + QQ(1,2)
552
+ g = QQ(1,2)*x + QQ(1,2)
553
+
554
+ h = x + 1
555
+
556
+ assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1,2))
557
+ assert R.dmp_ff_prs_gcd(f, g) == (h, g, QQ(1,2))
558
+
559
+ R, x, y = ring("x,y", RR)
560
+
561
+ f = 2.1*x*y**2 - 2.2*x*y + 2.1*x
562
+ g = 1.0*x**3
563
+
564
+ assert R.dmp_ff_prs_gcd(f, g) == \
565
+ (1.0*x, 2.1*y**2 - 2.2*y + 2.1, 1.0*x**2)
566
+
567
+
568
+ def test_dup_lcm():
569
+ R, x = ring("x", ZZ)
570
+
571
+ assert R.dup_lcm(2, 6) == 6
572
+
573
+ assert R.dup_lcm(2*x**3, 6*x) == 6*x**3
574
+ assert R.dup_lcm(2*x**3, 3*x) == 6*x**3
575
+
576
+ assert R.dup_lcm(x**2 + x, x) == x**2 + x
577
+ assert R.dup_lcm(x**2 + x, 2*x) == 2*x**2 + 2*x
578
+ assert R.dup_lcm(x**2 + 2*x, x) == x**2 + 2*x
579
+ assert R.dup_lcm(2*x**2 + x, x) == 2*x**2 + x
580
+ assert R.dup_lcm(2*x**2 + x, 2*x) == 4*x**2 + 2*x
581
+
582
+
583
+ def test_dmp_lcm():
584
+ R, x, y = ring("x,y", ZZ)
585
+
586
+ assert R.dmp_lcm(2, 6) == 6
587
+ assert R.dmp_lcm(x, y) == x*y
588
+
589
+ assert R.dmp_lcm(2*x**3, 6*x*y**2) == 6*x**3*y**2
590
+ assert R.dmp_lcm(2*x**3, 3*x*y**2) == 6*x**3*y**2
591
+
592
+ assert R.dmp_lcm(x**2*y, x*y**2) == x**2*y**2
593
+
594
+ f = 2*x*y**5 - 3*x*y**4 - 2*x*y**3 + 3*x*y**2
595
+ g = y**5 - 2*y**3 + y
596
+ h = 2*x*y**7 - 3*x*y**6 - 4*x*y**5 + 6*x*y**4 + 2*x*y**3 - 3*x*y**2
597
+
598
+ assert R.dmp_lcm(f, g) == h
599
+
600
+ f = x**3 - 3*x**2*y - 9*x*y**2 - 5*y**3
601
+ g = x**4 + 6*x**3*y + 12*x**2*y**2 + 10*x*y**3 + 3*y**4
602
+ h = x**5 + x**4*y - 18*x**3*y**2 - 50*x**2*y**3 - 47*x*y**4 - 15*y**5
603
+
604
+ assert R.dmp_lcm(f, g) == h
605
+
606
+
607
+ def test_dmp_content():
608
+ R, x,y = ring("x,y", ZZ)
609
+
610
+ assert R.dmp_content(-2) == 2
611
+
612
+ f, g, F = 3*y**2 + 2*y + 1, 1, 0
613
+
614
+ for i in range(0, 5):
615
+ g *= f
616
+ F += x**i*g
617
+
618
+ assert R.dmp_content(F) == f.drop(x)
619
+
620
+ R, x,y,z = ring("x,y,z", ZZ)
621
+
622
+ assert R.dmp_content(f_4) == 1
623
+ assert R.dmp_content(f_5) == 1
624
+
625
+ R, x,y,z,t = ring("x,y,z,t", ZZ)
626
+ assert R.dmp_content(f_6) == 1
627
+
628
+
629
+ def test_dmp_primitive():
630
+ R, x,y = ring("x,y", ZZ)
631
+
632
+ assert R.dmp_primitive(0) == (0, 0)
633
+ assert R.dmp_primitive(1) == (1, 1)
634
+
635
+ f, g, F = 3*y**2 + 2*y + 1, 1, 0
636
+
637
+ for i in range(0, 5):
638
+ g *= f
639
+ F += x**i*g
640
+
641
+ assert R.dmp_primitive(F) == (f.drop(x), F / f)
642
+
643
+ R, x,y,z = ring("x,y,z", ZZ)
644
+
645
+ cont, f = R.dmp_primitive(f_4)
646
+ assert cont == 1 and f == f_4
647
+ cont, f = R.dmp_primitive(f_5)
648
+ assert cont == 1 and f == f_5
649
+
650
+ R, x,y,z,t = ring("x,y,z,t", ZZ)
651
+
652
+ cont, f = R.dmp_primitive(f_6)
653
+ assert cont == 1 and f == f_6
654
+
655
+
656
+ def test_dup_cancel():
657
+ R, x = ring("x", ZZ)
658
+
659
+ f = 2*x**2 - 2
660
+ g = x**2 - 2*x + 1
661
+
662
+ p = 2*x + 2
663
+ q = x - 1
664
+
665
+ assert R.dup_cancel(f, g) == (p, q)
666
+ assert R.dup_cancel(f, g, include=False) == (1, 1, p, q)
667
+
668
+ f = -x - 2
669
+ g = 3*x - 4
670
+
671
+ F = x + 2
672
+ G = -3*x + 4
673
+
674
+ assert R.dup_cancel(f, g) == (f, g)
675
+ assert R.dup_cancel(F, G) == (f, g)
676
+
677
+ assert R.dup_cancel(0, 0) == (0, 0)
678
+ assert R.dup_cancel(0, 0, include=False) == (1, 1, 0, 0)
679
+
680
+ assert R.dup_cancel(x, 0) == (1, 0)
681
+ assert R.dup_cancel(x, 0, include=False) == (1, 1, 1, 0)
682
+
683
+ assert R.dup_cancel(0, x) == (0, 1)
684
+ assert R.dup_cancel(0, x, include=False) == (1, 1, 0, 1)
685
+
686
+ f = 0
687
+ g = x
688
+ one = 1
689
+
690
+ assert R.dup_cancel(f, g, include=True) == (f, one)
691
+
692
+
693
+ def test_dmp_cancel():
694
+ R, x, y = ring("x,y", ZZ)
695
+
696
+ f = 2*x**2 - 2
697
+ g = x**2 - 2*x + 1
698
+
699
+ p = 2*x + 2
700
+ q = x - 1
701
+
702
+ assert R.dmp_cancel(f, g) == (p, q)
703
+ assert R.dmp_cancel(f, g, include=False) == (1, 1, p, q)
704
+
705
+ assert R.dmp_cancel(0, 0) == (0, 0)
706
+ assert R.dmp_cancel(0, 0, include=False) == (1, 1, 0, 0)
707
+
708
+ assert R.dmp_cancel(y, 0) == (1, 0)
709
+ assert R.dmp_cancel(y, 0, include=False) == (1, 1, 1, 0)
710
+
711
+ assert R.dmp_cancel(0, y) == (0, 1)
712
+ assert R.dmp_cancel(0, y, include=False) == (1, 1, 0, 1)
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_factortools.py ADDED
@@ -0,0 +1,771 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tools for polynomial factorization routines in characteristic zero. """
2
+
3
+ from sympy.polys.rings import ring, xring
4
+ from sympy.polys.domains import FF, ZZ, QQ, ZZ_I, QQ_I, RR, EX
5
+
6
+ from sympy.polys import polyconfig as config
7
+ from sympy.polys.polyerrors import DomainError
8
+ from sympy.polys.polyclasses import ANP
9
+ from sympy.polys.specialpolys import f_polys, w_polys
10
+
11
+ from sympy.core.numbers import I
12
+ from sympy.functions.elementary.miscellaneous import sqrt
13
+ from sympy.functions.elementary.trigonometric import sin
14
+ from sympy.ntheory.generate import nextprime
15
+ from sympy.testing.pytest import raises, XFAIL
16
+
17
+
18
+ f_0, f_1, f_2, f_3, f_4, f_5, f_6 = f_polys()
19
+ w_1, w_2 = w_polys()
20
+
21
+ def test_dup_trial_division():
22
+ R, x = ring("x", ZZ)
23
+ assert R.dup_trial_division(x**5 + 8*x**4 + 25*x**3 + 38*x**2 + 28*x + 8, (x + 1, x + 2)) == [(x + 1, 2), (x + 2, 3)]
24
+
25
+
26
+ def test_dmp_trial_division():
27
+ R, x, y = ring("x,y", ZZ)
28
+ assert R.dmp_trial_division(x**5 + 8*x**4 + 25*x**3 + 38*x**2 + 28*x + 8, (x + 1, x + 2)) == [(x + 1, 2), (x + 2, 3)]
29
+
30
+
31
+ def test_dup_zz_mignotte_bound():
32
+ R, x = ring("x", ZZ)
33
+ assert R.dup_zz_mignotte_bound(2*x**2 + 3*x + 4) == 6
34
+ assert R.dup_zz_mignotte_bound(x**3 + 14*x**2 + 56*x + 64) == 152
35
+
36
+
37
+ def test_dmp_zz_mignotte_bound():
38
+ R, x, y = ring("x,y", ZZ)
39
+ assert R.dmp_zz_mignotte_bound(2*x**2 + 3*x + 4) == 32
40
+
41
+
42
+ def test_dup_zz_hensel_step():
43
+ R, x = ring("x", ZZ)
44
+
45
+ f = x**4 - 1
46
+ g = x**3 + 2*x**2 - x - 2
47
+ h = x - 2
48
+ s = -2
49
+ t = 2*x**2 - 2*x - 1
50
+
51
+ G, H, S, T = R.dup_zz_hensel_step(5, f, g, h, s, t)
52
+
53
+ assert G == x**3 + 7*x**2 - x - 7
54
+ assert H == x - 7
55
+ assert S == 8
56
+ assert T == -8*x**2 - 12*x - 1
57
+
58
+
59
+ def test_dup_zz_hensel_lift():
60
+ R, x = ring("x", ZZ)
61
+
62
+ f = x**4 - 1
63
+ F = [x - 1, x - 2, x + 2, x + 1]
64
+
65
+ assert R.dup_zz_hensel_lift(ZZ(5), f, F, 4) == \
66
+ [x - 1, x - 182, x + 182, x + 1]
67
+
68
+
69
+ def test_dup_zz_irreducible_p():
70
+ R, x = ring("x", ZZ)
71
+
72
+ assert R.dup_zz_irreducible_p(3*x**4 + 2*x**3 + 6*x**2 + 8*x + 7) is None
73
+ assert R.dup_zz_irreducible_p(3*x**4 + 2*x**3 + 6*x**2 + 8*x + 4) is None
74
+
75
+ assert R.dup_zz_irreducible_p(3*x**4 + 2*x**3 + 6*x**2 + 8*x + 10) is True
76
+ assert R.dup_zz_irreducible_p(3*x**4 + 2*x**3 + 6*x**2 + 8*x + 14) is True
77
+
78
+
79
+ def test_dup_cyclotomic_p():
80
+ R, x = ring("x", ZZ)
81
+
82
+ assert R.dup_cyclotomic_p(x - 1) is True
83
+ assert R.dup_cyclotomic_p(x + 1) is True
84
+ assert R.dup_cyclotomic_p(x**2 + x + 1) is True
85
+ assert R.dup_cyclotomic_p(x**2 + 1) is True
86
+ assert R.dup_cyclotomic_p(x**4 + x**3 + x**2 + x + 1) is True
87
+ assert R.dup_cyclotomic_p(x**2 - x + 1) is True
88
+ assert R.dup_cyclotomic_p(x**6 + x**5 + x**4 + x**3 + x**2 + x + 1) is True
89
+ assert R.dup_cyclotomic_p(x**4 + 1) is True
90
+ assert R.dup_cyclotomic_p(x**6 + x**3 + 1) is True
91
+
92
+ assert R.dup_cyclotomic_p(0) is False
93
+ assert R.dup_cyclotomic_p(1) is False
94
+ assert R.dup_cyclotomic_p(x) is False
95
+ assert R.dup_cyclotomic_p(x + 2) is False
96
+ assert R.dup_cyclotomic_p(3*x + 1) is False
97
+ assert R.dup_cyclotomic_p(x**2 - 1) is False
98
+
99
+ f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1
100
+ assert R.dup_cyclotomic_p(f) is False
101
+
102
+ g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1
103
+ assert R.dup_cyclotomic_p(g) is True
104
+
105
+ R, x = ring("x", QQ)
106
+ assert R.dup_cyclotomic_p(x**2 + x + 1) is True
107
+ assert R.dup_cyclotomic_p(QQ(1,2)*x**2 + x + 1) is False
108
+
109
+ R, x = ring("x", ZZ["y"])
110
+ assert R.dup_cyclotomic_p(x**2 + x + 1) is False
111
+
112
+
113
+ def test_dup_zz_cyclotomic_poly():
114
+ R, x = ring("x", ZZ)
115
+
116
+ assert R.dup_zz_cyclotomic_poly(1) == x - 1
117
+ assert R.dup_zz_cyclotomic_poly(2) == x + 1
118
+ assert R.dup_zz_cyclotomic_poly(3) == x**2 + x + 1
119
+ assert R.dup_zz_cyclotomic_poly(4) == x**2 + 1
120
+ assert R.dup_zz_cyclotomic_poly(5) == x**4 + x**3 + x**2 + x + 1
121
+ assert R.dup_zz_cyclotomic_poly(6) == x**2 - x + 1
122
+ assert R.dup_zz_cyclotomic_poly(7) == x**6 + x**5 + x**4 + x**3 + x**2 + x + 1
123
+ assert R.dup_zz_cyclotomic_poly(8) == x**4 + 1
124
+ assert R.dup_zz_cyclotomic_poly(9) == x**6 + x**3 + 1
125
+
126
+
127
+ def test_dup_zz_cyclotomic_factor():
128
+ R, x = ring("x", ZZ)
129
+
130
+ assert R.dup_zz_cyclotomic_factor(0) is None
131
+ assert R.dup_zz_cyclotomic_factor(1) is None
132
+
133
+ assert R.dup_zz_cyclotomic_factor(2*x**10 - 1) is None
134
+ assert R.dup_zz_cyclotomic_factor(x**10 - 3) is None
135
+ assert R.dup_zz_cyclotomic_factor(x**10 + x**5 - 1) is None
136
+
137
+ assert R.dup_zz_cyclotomic_factor(x + 1) == [x + 1]
138
+ assert R.dup_zz_cyclotomic_factor(x - 1) == [x - 1]
139
+
140
+ assert R.dup_zz_cyclotomic_factor(x**2 + 1) == [x**2 + 1]
141
+ assert R.dup_zz_cyclotomic_factor(x**2 - 1) == [x - 1, x + 1]
142
+
143
+ assert R.dup_zz_cyclotomic_factor(x**27 + 1) == \
144
+ [x + 1, x**2 - x + 1, x**6 - x**3 + 1, x**18 - x**9 + 1]
145
+ assert R.dup_zz_cyclotomic_factor(x**27 - 1) == \
146
+ [x - 1, x**2 + x + 1, x**6 + x**3 + 1, x**18 + x**9 + 1]
147
+
148
+
149
+ def test_dup_zz_factor():
150
+ R, x = ring("x", ZZ)
151
+
152
+ assert R.dup_zz_factor(0) == (0, [])
153
+ assert R.dup_zz_factor(7) == (7, [])
154
+ assert R.dup_zz_factor(-7) == (-7, [])
155
+
156
+ assert R.dup_zz_factor_sqf(0) == (0, [])
157
+ assert R.dup_zz_factor_sqf(7) == (7, [])
158
+ assert R.dup_zz_factor_sqf(-7) == (-7, [])
159
+
160
+ assert R.dup_zz_factor(2*x + 4) == (2, [(x + 2, 1)])
161
+ assert R.dup_zz_factor_sqf(2*x + 4) == (2, [x + 2])
162
+
163
+ f = x**4 + x + 1
164
+
165
+ for i in range(0, 20):
166
+ assert R.dup_zz_factor(f) == (1, [(f, 1)])
167
+
168
+ assert R.dup_zz_factor(x**2 + 2*x + 2) == \
169
+ (1, [(x**2 + 2*x + 2, 1)])
170
+
171
+ assert R.dup_zz_factor(18*x**2 + 12*x + 2) == \
172
+ (2, [(3*x + 1, 2)])
173
+
174
+ assert R.dup_zz_factor(-9*x**2 + 1) == \
175
+ (-1, [(3*x - 1, 1),
176
+ (3*x + 1, 1)])
177
+
178
+ assert R.dup_zz_factor_sqf(-9*x**2 + 1) == \
179
+ (-1, [3*x - 1,
180
+ 3*x + 1])
181
+
182
+ assert R.dup_zz_factor(x**3 - 6*x**2 + 11*x - 6) == \
183
+ (1, [(x - 3, 1),
184
+ (x - 2, 1),
185
+ (x - 1, 1)])
186
+
187
+ assert R.dup_zz_factor_sqf(x**3 - 6*x**2 + 11*x - 6) == \
188
+ (1, [x - 3,
189
+ x - 2,
190
+ x - 1])
191
+
192
+ assert R.dup_zz_factor(3*x**3 + 10*x**2 + 13*x + 10) == \
193
+ (1, [(x + 2, 1),
194
+ (3*x**2 + 4*x + 5, 1)])
195
+
196
+ assert R.dup_zz_factor_sqf(3*x**3 + 10*x**2 + 13*x + 10) == \
197
+ (1, [x + 2,
198
+ 3*x**2 + 4*x + 5])
199
+
200
+ assert R.dup_zz_factor(-x**6 + x**2) == \
201
+ (-1, [(x - 1, 1),
202
+ (x + 1, 1),
203
+ (x, 2),
204
+ (x**2 + 1, 1)])
205
+
206
+ f = 1080*x**8 + 5184*x**7 + 2099*x**6 + 744*x**5 + 2736*x**4 - 648*x**3 + 129*x**2 - 324
207
+
208
+ assert R.dup_zz_factor(f) == \
209
+ (1, [(5*x**4 + 24*x**3 + 9*x**2 + 12, 1),
210
+ (216*x**4 + 31*x**2 - 27, 1)])
211
+
212
+ f = -29802322387695312500000000000000000000*x**25 \
213
+ + 2980232238769531250000000000000000*x**20 \
214
+ + 1743435859680175781250000000000*x**15 \
215
+ + 114142894744873046875000000*x**10 \
216
+ - 210106372833251953125*x**5 \
217
+ + 95367431640625
218
+
219
+ assert R.dup_zz_factor(f) == \
220
+ (-95367431640625, [(5*x - 1, 1),
221
+ (100*x**2 + 10*x - 1, 2),
222
+ (625*x**4 + 125*x**3 + 25*x**2 + 5*x + 1, 1),
223
+ (10000*x**4 - 3000*x**3 + 400*x**2 - 20*x + 1, 2),
224
+ (10000*x**4 + 2000*x**3 + 400*x**2 + 30*x + 1, 2)])
225
+
226
+ f = x**10 - 1
227
+
228
+ config.setup('USE_CYCLOTOMIC_FACTOR', True)
229
+ F_0 = R.dup_zz_factor(f)
230
+
231
+ config.setup('USE_CYCLOTOMIC_FACTOR', False)
232
+ F_1 = R.dup_zz_factor(f)
233
+
234
+ assert F_0 == F_1 == \
235
+ (1, [(x - 1, 1),
236
+ (x + 1, 1),
237
+ (x**4 - x**3 + x**2 - x + 1, 1),
238
+ (x**4 + x**3 + x**2 + x + 1, 1)])
239
+
240
+ config.setup('USE_CYCLOTOMIC_FACTOR')
241
+
242
+ f = x**10 + 1
243
+
244
+ config.setup('USE_CYCLOTOMIC_FACTOR', True)
245
+ F_0 = R.dup_zz_factor(f)
246
+
247
+ config.setup('USE_CYCLOTOMIC_FACTOR', False)
248
+ F_1 = R.dup_zz_factor(f)
249
+
250
+ assert F_0 == F_1 == \
251
+ (1, [(x**2 + 1, 1),
252
+ (x**8 - x**6 + x**4 - x**2 + 1, 1)])
253
+
254
+ config.setup('USE_CYCLOTOMIC_FACTOR')
255
+
256
+ def test_dmp_zz_wang():
257
+ R, x,y,z = ring("x,y,z", ZZ)
258
+ UV, _x = ring("x", ZZ)
259
+
260
+ p = ZZ(nextprime(R.dmp_zz_mignotte_bound(w_1)))
261
+ assert p == 6291469
262
+
263
+ t_1, k_1, e_1 = y, 1, ZZ(-14)
264
+ t_2, k_2, e_2 = z, 2, ZZ(3)
265
+ t_3, k_3, e_3 = y + z, 2, ZZ(-11)
266
+ t_4, k_4, e_4 = y - z, 1, ZZ(-17)
267
+
268
+ T = [t_1, t_2, t_3, t_4]
269
+ K = [k_1, k_2, k_3, k_4]
270
+ E = [e_1, e_2, e_3, e_4]
271
+
272
+ T = zip([ t.drop(x) for t in T ], K)
273
+
274
+ A = [ZZ(-14), ZZ(3)]
275
+
276
+ S = R.dmp_eval_tail(w_1, A)
277
+ cs, s = UV.dup_primitive(S)
278
+
279
+ assert cs == 1 and s == S == \
280
+ 1036728*_x**6 + 915552*_x**5 + 55748*_x**4 + 105621*_x**3 - 17304*_x**2 - 26841*_x - 644
281
+
282
+ assert R.dmp_zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17]
283
+ assert UV.dup_sqf_p(s) and UV.dup_degree(s) == R.dmp_degree(w_1)
284
+
285
+ _, H = UV.dup_zz_factor_sqf(s)
286
+
287
+ h_1 = 44*_x**2 + 42*_x + 1
288
+ h_2 = 126*_x**2 - 9*_x + 28
289
+ h_3 = 187*_x**2 - 23
290
+
291
+ assert H == [h_1, h_2, h_3]
292
+
293
+ LC = [ lc.drop(x) for lc in [-4*y - 4*z, -y*z**2, y**2 - z**2] ]
294
+
295
+ assert R.dmp_zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC)
296
+
297
+ factors = R.dmp_zz_wang_hensel_lifting(w_1, H, LC, A, p)
298
+ assert R.dmp_expand(factors) == w_1
299
+
300
+
301
+ @XFAIL
302
+ def test_dmp_zz_wang_fail():
303
+ R, x,y,z = ring("x,y,z", ZZ)
304
+ UV, _x = ring("x", ZZ)
305
+
306
+ p = ZZ(nextprime(R.dmp_zz_mignotte_bound(w_1)))
307
+ assert p == 6291469
308
+
309
+ H_1 = [44*x**2 + 42*x + 1, 126*x**2 - 9*x + 28, 187*x**2 - 23]
310
+ H_2 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9]
311
+ H_3 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9]
312
+
313
+ c_1 = -70686*x**5 - 5863*x**4 - 17826*x**3 + 2009*x**2 + 5031*x + 74
314
+ c_2 = 9*x**5*y**4 + 12*x**5*y**3 - 45*x**5*y**2 - 108*x**5*y - 324*x**5 + 18*x**4*y**3 - 216*x**4*y**2 - 810*x**4*y + 2*x**3*y**4 + 9*x**3*y**3 - 252*x**3*y**2 - 288*x**3*y - 945*x**3 - 30*x**2*y**2 - 414*x**2*y + 2*x*y**3 - 54*x*y**2 - 3*x*y + 81*x + 12*y
315
+ c_3 = -36*x**4*y**2 - 108*x**4*y - 27*x**3*y**2 - 36*x**3*y - 108*x**3 - 8*x**2*y**2 - 42*x**2*y - 6*x*y**2 + 9*x + 2*y
316
+
317
+ assert R.dmp_zz_diophantine(H_1, c_1, [], 5, p) == [-3*x, -2, 1]
318
+ assert R.dmp_zz_diophantine(H_2, c_2, [ZZ(-14)], 5, p) == [-x*y, -3*x, -6]
319
+ assert R.dmp_zz_diophantine(H_3, c_3, [ZZ(-14)], 5, p) == [0, 0, -1]
320
+
321
+
322
+ def test_issue_6355():
323
+ # This tests a bug in the Wang algorithm that occurred only with a very
324
+ # specific set of random numbers.
325
+ random_sequence = [-1, -1, 0, 0, 0, 0, -1, -1, 0, -1, 3, -1, 3, 3, 3, 3, -1, 3]
326
+
327
+ R, x, y, z = ring("x,y,z", ZZ)
328
+ f = 2*x**2 + y*z - y - z**2 + z
329
+
330
+ assert R.dmp_zz_wang(f, seed=random_sequence) == [f]
331
+
332
+
333
+ def test_dmp_zz_factor():
334
+ R, x = ring("x", ZZ)
335
+ assert R.dmp_zz_factor(0) == (0, [])
336
+ assert R.dmp_zz_factor(7) == (7, [])
337
+ assert R.dmp_zz_factor(-7) == (-7, [])
338
+
339
+ assert R.dmp_zz_factor(x**2 - 9) == (1, [(x - 3, 1), (x + 3, 1)])
340
+
341
+ R, x, y = ring("x,y", ZZ)
342
+ assert R.dmp_zz_factor(0) == (0, [])
343
+ assert R.dmp_zz_factor(7) == (7, [])
344
+ assert R.dmp_zz_factor(-7) == (-7, [])
345
+
346
+ assert R.dmp_zz_factor(x) == (1, [(x, 1)])
347
+ assert R.dmp_zz_factor(4*x) == (4, [(x, 1)])
348
+ assert R.dmp_zz_factor(4*x + 2) == (2, [(2*x + 1, 1)])
349
+ assert R.dmp_zz_factor(x*y + 1) == (1, [(x*y + 1, 1)])
350
+ assert R.dmp_zz_factor(y**2 + 1) == (1, [(y**2 + 1, 1)])
351
+ assert R.dmp_zz_factor(y**2 - 1) == (1, [(y - 1, 1), (y + 1, 1)])
352
+
353
+ assert R.dmp_zz_factor(x**2*y**2 + 6*x**2*y + 9*x**2 - 1) == (1, [(x*y + 3*x - 1, 1), (x*y + 3*x + 1, 1)])
354
+ assert R.dmp_zz_factor(x**2*y**2 - 9) == (1, [(x*y - 3, 1), (x*y + 3, 1)])
355
+
356
+ R, x, y, z = ring("x,y,z", ZZ)
357
+ assert R.dmp_zz_factor(x**2*y**2*z**2 - 9) == \
358
+ (1, [(x*y*z - 3, 1),
359
+ (x*y*z + 3, 1)])
360
+
361
+ R, x, y, z, u = ring("x,y,z,u", ZZ)
362
+ assert R.dmp_zz_factor(x**2*y**2*z**2*u**2 - 9) == \
363
+ (1, [(x*y*z*u - 3, 1),
364
+ (x*y*z*u + 3, 1)])
365
+
366
+ R, x, y, z = ring("x,y,z", ZZ)
367
+ assert R.dmp_zz_factor(f_1) == \
368
+ (1, [(x + y*z + 20, 1),
369
+ (x*y + z + 10, 1),
370
+ (x*z + y + 30, 1)])
371
+
372
+ assert R.dmp_zz_factor(f_2) == \
373
+ (1, [(x**2*y**2 + x**2*z**2 + y + 90, 1),
374
+ (x**3*y + x**3*z + z - 11, 1)])
375
+
376
+ assert R.dmp_zz_factor(f_3) == \
377
+ (1, [(x**2*y**2 + x*z**4 + x + z, 1),
378
+ (x**3 + x*y*z + y**2 + y*z**3, 1)])
379
+
380
+ assert R.dmp_zz_factor(f_4) == \
381
+ (-1, [(x*y**3 + z**2, 1),
382
+ (x**2*z + y**4*z**2 + 5, 1),
383
+ (x**3*y - z**2 - 3, 1),
384
+ (x**3*y**4 + z**2, 1)])
385
+
386
+ assert R.dmp_zz_factor(f_5) == \
387
+ (-1, [(x + y - z, 3)])
388
+
389
+ R, x, y, z, t = ring("x,y,z,t", ZZ)
390
+ assert R.dmp_zz_factor(f_6) == \
391
+ (1, [(47*x*y + z**3*t**2 - t**2, 1),
392
+ (45*x**3 - 9*y**3 - y**2 + 3*z**3 + 2*z*t, 1)])
393
+
394
+ R, x, y, z = ring("x,y,z", ZZ)
395
+ assert R.dmp_zz_factor(w_1) == \
396
+ (1, [(x**2*y**2 - x**2*z**2 + y - z**2, 1),
397
+ (x**2*y*z**2 + 3*x*z + 2*y, 1),
398
+ (4*x**2*y + 4*x**2*z + x*y*z - 1, 1)])
399
+
400
+ R, x, y = ring("x,y", ZZ)
401
+ f = -12*x**16*y + 240*x**12*y**3 - 768*x**10*y**4 + 1080*x**8*y**5 - 768*x**6*y**6 + 240*x**4*y**7 - 12*y**9
402
+
403
+ assert R.dmp_zz_factor(f) == \
404
+ (-12, [(y, 1),
405
+ (x**2 - y, 6),
406
+ (x**4 + 6*x**2*y + y**2, 1)])
407
+
408
+
409
+ def test_dup_qq_i_factor():
410
+ R, x = ring("x", QQ_I)
411
+ i = QQ_I(0, 1)
412
+
413
+ assert R.dup_qq_i_factor(x**2 - 2) == (QQ_I(1, 0), [(x**2 - 2, 1)])
414
+
415
+ assert R.dup_qq_i_factor(x**2 - 1) == (QQ_I(1, 0), [(x - 1, 1), (x + 1, 1)])
416
+
417
+ assert R.dup_qq_i_factor(x**2 + 1) == (QQ_I(1, 0), [(x - i, 1), (x + i, 1)])
418
+
419
+ assert R.dup_qq_i_factor(x**2/4 + 1) == \
420
+ (QQ_I(QQ(1, 4), 0), [(x - 2*i, 1), (x + 2*i, 1)])
421
+
422
+ assert R.dup_qq_i_factor(x**2 + 4) == \
423
+ (QQ_I(1, 0), [(x - 2*i, 1), (x + 2*i, 1)])
424
+
425
+ assert R.dup_qq_i_factor(x**2 + 2*x + 1) == \
426
+ (QQ_I(1, 0), [(x + 1, 2)])
427
+
428
+ assert R.dup_qq_i_factor(x**2 + 2*i*x - 1) == \
429
+ (QQ_I(1, 0), [(x + i, 2)])
430
+
431
+ f = 8192*x**2 + x*(22656 + 175232*i) - 921416 + 242313*i
432
+
433
+ assert R.dup_qq_i_factor(f) == \
434
+ (QQ_I(8192, 0), [(x + QQ_I(QQ(177, 128), QQ(1369, 128)), 2)])
435
+
436
+
437
+ def test_dmp_qq_i_factor():
438
+ R, x, y = ring("x, y", QQ_I)
439
+ i = QQ_I(0, 1)
440
+
441
+ assert R.dmp_qq_i_factor(x**2 + 2*y**2) == \
442
+ (QQ_I(1, 0), [(x**2 + 2*y**2, 1)])
443
+
444
+ assert R.dmp_qq_i_factor(x**2 + y**2) == \
445
+ (QQ_I(1, 0), [(x - i*y, 1), (x + i*y, 1)])
446
+
447
+ assert R.dmp_qq_i_factor(x**2 + y**2/4) == \
448
+ (QQ_I(1, 0), [(x - i*y/2, 1), (x + i*y/2, 1)])
449
+
450
+ assert R.dmp_qq_i_factor(4*x**2 + y**2) == \
451
+ (QQ_I(4, 0), [(x - i*y/2, 1), (x + i*y/2, 1)])
452
+
453
+
454
+ def test_dup_zz_i_factor():
455
+ R, x = ring("x", ZZ_I)
456
+ i = ZZ_I(0, 1)
457
+
458
+ assert R.dup_zz_i_factor(x**2 - 2) == (ZZ_I(1, 0), [(x**2 - 2, 1)])
459
+
460
+ assert R.dup_zz_i_factor(x**2 - 1) == (ZZ_I(1, 0), [(x - 1, 1), (x + 1, 1)])
461
+
462
+ assert R.dup_zz_i_factor(x**2 + 1) == (ZZ_I(1, 0), [(x - i, 1), (x + i, 1)])
463
+
464
+ assert R.dup_zz_i_factor(x**2 + 4) == \
465
+ (ZZ_I(1, 0), [(x - 2*i, 1), (x + 2*i, 1)])
466
+
467
+ assert R.dup_zz_i_factor(x**2 + 2*x + 1) == \
468
+ (ZZ_I(1, 0), [(x + 1, 2)])
469
+
470
+ assert R.dup_zz_i_factor(x**2 + 2*i*x - 1) == \
471
+ (ZZ_I(1, 0), [(x + i, 2)])
472
+
473
+ f = 8192*x**2 + x*(22656 + 175232*i) - 921416 + 242313*i
474
+
475
+ assert R.dup_zz_i_factor(f) == \
476
+ (ZZ_I(0, 1), [((64 - 64*i)*x + (773 + 596*i), 2)])
477
+
478
+
479
+ def test_dmp_zz_i_factor():
480
+ R, x, y = ring("x, y", ZZ_I)
481
+ i = ZZ_I(0, 1)
482
+
483
+ assert R.dmp_zz_i_factor(x**2 + 2*y**2) == \
484
+ (ZZ_I(1, 0), [(x**2 + 2*y**2, 1)])
485
+
486
+ assert R.dmp_zz_i_factor(x**2 + y**2) == \
487
+ (ZZ_I(1, 0), [(x - i*y, 1), (x + i*y, 1)])
488
+
489
+ assert R.dmp_zz_i_factor(4*x**2 + y**2) == \
490
+ (ZZ_I(1, 0), [(2*x - i*y, 1), (2*x + i*y, 1)])
491
+
492
+
493
+ def test_dup_ext_factor():
494
+ R, x = ring("x", QQ.algebraic_field(I))
495
+ def anp(element):
496
+ return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)
497
+
498
+ assert R.dup_ext_factor(0) == (anp([]), [])
499
+
500
+ f = anp([QQ(1)])*x + anp([QQ(1)])
501
+
502
+ assert R.dup_ext_factor(f) == (anp([QQ(1)]), [(f, 1)])
503
+
504
+ g = anp([QQ(2)])*x + anp([QQ(2)])
505
+
506
+ assert R.dup_ext_factor(g) == (anp([QQ(2)]), [(f, 1)])
507
+
508
+ f = anp([QQ(7)])*x**4 + anp([QQ(1, 1)])
509
+ g = anp([QQ(1)])*x**4 + anp([QQ(1, 7)])
510
+
511
+ assert R.dup_ext_factor(f) == (anp([QQ(7)]), [(g, 1)])
512
+
513
+ f = anp([QQ(1)])*x**4 + anp([QQ(1)])
514
+
515
+ assert R.dup_ext_factor(f) == \
516
+ (anp([QQ(1, 1)]), [(anp([QQ(1)])*x**2 + anp([QQ(-1), QQ(0)]), 1),
517
+ (anp([QQ(1)])*x**2 + anp([QQ( 1), QQ(0)]), 1)])
518
+
519
+ f = anp([QQ(4, 1)])*x**2 + anp([QQ(9, 1)])
520
+
521
+ assert R.dup_ext_factor(f) == \
522
+ (anp([QQ(4, 1)]), [(anp([QQ(1, 1)])*x + anp([-QQ(3, 2), QQ(0, 1)]), 1),
523
+ (anp([QQ(1, 1)])*x + anp([ QQ(3, 2), QQ(0, 1)]), 1)])
524
+
525
+ f = anp([QQ(4, 1)])*x**4 + anp([QQ(8, 1)])*x**3 + anp([QQ(77, 1)])*x**2 + anp([QQ(18, 1)])*x + anp([QQ(153, 1)])
526
+
527
+ assert R.dup_ext_factor(f) == \
528
+ (anp([QQ(4, 1)]), [(anp([QQ(1, 1)])*x + anp([-QQ(4, 1), QQ(1, 1)]), 1),
529
+ (anp([QQ(1, 1)])*x + anp([-QQ(3, 2), QQ(0, 1)]), 1),
530
+ (anp([QQ(1, 1)])*x + anp([ QQ(3, 2), QQ(0, 1)]), 1),
531
+ (anp([QQ(1, 1)])*x + anp([ QQ(4, 1), QQ(1, 1)]), 1)])
532
+
533
+ R, x = ring("x", QQ.algebraic_field(sqrt(2)))
534
+ def anp(element):
535
+ return ANP(element, [QQ(1), QQ(0), QQ(-2)], QQ)
536
+
537
+ f = anp([QQ(1)])*x**4 + anp([QQ(1, 1)])
538
+
539
+ assert R.dup_ext_factor(f) == \
540
+ (anp([QQ(1)]), [(anp([QQ(1)])*x**2 + anp([QQ(-1), QQ(0)])*x + anp([QQ(1)]), 1),
541
+ (anp([QQ(1)])*x**2 + anp([QQ( 1), QQ(0)])*x + anp([QQ(1)]), 1)])
542
+
543
+ f = anp([QQ(1, 1)])*x**2 + anp([QQ(2), QQ(0)])*x + anp([QQ(2, 1)])
544
+
545
+ assert R.dup_ext_factor(f) == \
546
+ (anp([QQ(1, 1)]), [(anp([1])*x + anp([1, 0]), 2)])
547
+
548
+ assert R.dup_ext_factor(f**3) == \
549
+ (anp([QQ(1, 1)]), [(anp([1])*x + anp([1, 0]), 6)])
550
+
551
+ f *= anp([QQ(2, 1)])
552
+
553
+ assert R.dup_ext_factor(f) == \
554
+ (anp([QQ(2, 1)]), [(anp([1])*x + anp([1, 0]), 2)])
555
+
556
+ assert R.dup_ext_factor(f**3) == \
557
+ (anp([QQ(8, 1)]), [(anp([1])*x + anp([1, 0]), 6)])
558
+
559
+
560
+ def test_dmp_ext_factor():
561
+ R, x,y = ring("x,y", QQ.algebraic_field(sqrt(2)))
562
+ def anp(x):
563
+ return ANP(x, [QQ(1), QQ(0), QQ(-2)], QQ)
564
+
565
+ assert R.dmp_ext_factor(0) == (anp([]), [])
566
+
567
+ f = anp([QQ(1)])*x + anp([QQ(1)])
568
+
569
+ assert R.dmp_ext_factor(f) == (anp([QQ(1)]), [(f, 1)])
570
+
571
+ g = anp([QQ(2)])*x + anp([QQ(2)])
572
+
573
+ assert R.dmp_ext_factor(g) == (anp([QQ(2)]), [(f, 1)])
574
+
575
+ f = anp([QQ(1)])*x**2 + anp([QQ(-2)])*y**2
576
+
577
+ assert R.dmp_ext_factor(f) == \
578
+ (anp([QQ(1)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1),
579
+ (anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)])
580
+
581
+ f = anp([QQ(2)])*x**2 + anp([QQ(-4)])*y**2
582
+
583
+ assert R.dmp_ext_factor(f) == \
584
+ (anp([QQ(2)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1),
585
+ (anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)])
586
+
587
+
588
+ def test_dup_factor_list():
589
+ R, x = ring("x", ZZ)
590
+ assert R.dup_factor_list(0) == (0, [])
591
+ assert R.dup_factor_list(7) == (7, [])
592
+
593
+ R, x = ring("x", QQ)
594
+ assert R.dup_factor_list(0) == (0, [])
595
+ assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
596
+
597
+ R, x = ring("x", ZZ['t'])
598
+ assert R.dup_factor_list(0) == (0, [])
599
+ assert R.dup_factor_list(7) == (7, [])
600
+
601
+ R, x = ring("x", QQ['t'])
602
+ assert R.dup_factor_list(0) == (0, [])
603
+ assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
604
+
605
+ R, x = ring("x", ZZ)
606
+ assert R.dup_factor_list_include(0) == [(0, 1)]
607
+ assert R.dup_factor_list_include(7) == [(7, 1)]
608
+
609
+ assert R.dup_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
610
+ assert R.dup_factor_list_include(x**2 + 2*x + 1) == [(x + 1, 2)]
611
+ # issue 8037
612
+ assert R.dup_factor_list(6*x**2 - 5*x - 6) == (1, [(2*x - 3, 1), (3*x + 2, 1)])
613
+
614
+ R, x = ring("x", QQ)
615
+ assert R.dup_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1, 2), [(x + 1, 2)])
616
+
617
+ R, x = ring("x", FF(2))
618
+ assert R.dup_factor_list(x**2 + 1) == (1, [(x + 1, 2)])
619
+
620
+ R, x = ring("x", RR)
621
+ assert R.dup_factor_list(1.0*x**2 + 2.0*x + 1.0) == (1.0, [(1.0*x + 1.0, 2)])
622
+ assert R.dup_factor_list(2.0*x**2 + 4.0*x + 2.0) == (2.0, [(1.0*x + 1.0, 2)])
623
+
624
+ f = 6.7225336055071*x**2 - 10.6463972754741*x - 0.33469524022264
625
+ coeff, factors = R.dup_factor_list(f)
626
+ assert coeff == RR(10.6463972754741)
627
+ assert len(factors) == 1
628
+ assert factors[0][0].max_norm() == RR(1.0)
629
+ assert factors[0][1] == 1
630
+
631
+ Rt, t = ring("t", ZZ)
632
+ R, x = ring("x", Rt)
633
+
634
+ f = 4*t*x**2 + 4*t**2*x
635
+
636
+ assert R.dup_factor_list(f) == \
637
+ (4*t, [(x, 1),
638
+ (x + t, 1)])
639
+
640
+ Rt, t = ring("t", QQ)
641
+ R, x = ring("x", Rt)
642
+
643
+ f = QQ(1, 2)*t*x**2 + QQ(1, 2)*t**2*x
644
+
645
+ assert R.dup_factor_list(f) == \
646
+ (QQ(1, 2)*t, [(x, 1),
647
+ (x + t, 1)])
648
+
649
+ R, x = ring("x", QQ.algebraic_field(I))
650
+ def anp(element):
651
+ return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)
652
+
653
+ f = anp([QQ(1, 1)])*x**4 + anp([QQ(2, 1)])*x**2
654
+
655
+ assert R.dup_factor_list(f) == \
656
+ (anp([QQ(1, 1)]), [(anp([QQ(1, 1)])*x, 2),
657
+ (anp([QQ(1, 1)])*x**2 + anp([])*x + anp([QQ(2, 1)]), 1)])
658
+
659
+ R, x = ring("x", EX)
660
+ raises(DomainError, lambda: R.dup_factor_list(EX(sin(1))))
661
+
662
+
663
+ def test_dmp_factor_list():
664
+ R, x, y = ring("x,y", ZZ)
665
+ assert R.dmp_factor_list(0) == (ZZ(0), [])
666
+ assert R.dmp_factor_list(7) == (7, [])
667
+
668
+ R, x, y = ring("x,y", QQ)
669
+ assert R.dmp_factor_list(0) == (QQ(0), [])
670
+ assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
671
+
672
+ Rt, t = ring("t", ZZ)
673
+ R, x, y = ring("x,y", Rt)
674
+ assert R.dmp_factor_list(0) == (0, [])
675
+ assert R.dmp_factor_list(7) == (ZZ(7), [])
676
+
677
+ Rt, t = ring("t", QQ)
678
+ R, x, y = ring("x,y", Rt)
679
+ assert R.dmp_factor_list(0) == (0, [])
680
+ assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
681
+
682
+ R, x, y = ring("x,y", ZZ)
683
+ assert R.dmp_factor_list_include(0) == [(0, 1)]
684
+ assert R.dmp_factor_list_include(7) == [(7, 1)]
685
+
686
+ R, X = xring("x:200", ZZ)
687
+
688
+ f, g = X[0]**2 + 2*X[0] + 1, X[0] + 1
689
+ assert R.dmp_factor_list(f) == (1, [(g, 2)])
690
+
691
+ f, g = X[-1]**2 + 2*X[-1] + 1, X[-1] + 1
692
+ assert R.dmp_factor_list(f) == (1, [(g, 2)])
693
+
694
+ R, x = ring("x", ZZ)
695
+ assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
696
+ R, x = ring("x", QQ)
697
+ assert R.dmp_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1,2), [(x + 1, 2)])
698
+
699
+ R, x, y = ring("x,y", ZZ)
700
+ assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
701
+ R, x, y = ring("x,y", QQ)
702
+ assert R.dmp_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1,2), [(x + 1, 2)])
703
+
704
+ R, x, y = ring("x,y", ZZ)
705
+ f = 4*x**2*y + 4*x*y**2
706
+
707
+ assert R.dmp_factor_list(f) == \
708
+ (4, [(y, 1),
709
+ (x, 1),
710
+ (x + y, 1)])
711
+
712
+ assert R.dmp_factor_list_include(f) == \
713
+ [(4*y, 1),
714
+ (x, 1),
715
+ (x + y, 1)]
716
+
717
+ R, x, y = ring("x,y", QQ)
718
+ f = QQ(1,2)*x**2*y + QQ(1,2)*x*y**2
719
+
720
+ assert R.dmp_factor_list(f) == \
721
+ (QQ(1,2), [(y, 1),
722
+ (x, 1),
723
+ (x + y, 1)])
724
+
725
+ R, x, y = ring("x,y", RR)
726
+ f = 2.0*x**2 - 8.0*y**2
727
+
728
+ assert R.dmp_factor_list(f) == \
729
+ (RR(8.0), [(0.5*x - y, 1),
730
+ (0.5*x + y, 1)])
731
+
732
+ f = 6.7225336055071*x**2*y**2 - 10.6463972754741*x*y - 0.33469524022264
733
+ coeff, factors = R.dmp_factor_list(f)
734
+ assert coeff == RR(10.6463972754741)
735
+ assert len(factors) == 1
736
+ assert factors[0][0].max_norm() == RR(1.0)
737
+ assert factors[0][1] == 1
738
+
739
+ Rt, t = ring("t", ZZ)
740
+ R, x, y = ring("x,y", Rt)
741
+ f = 4*t*x**2 + 4*t**2*x
742
+
743
+ assert R.dmp_factor_list(f) == \
744
+ (4*t, [(x, 1),
745
+ (x + t, 1)])
746
+
747
+ Rt, t = ring("t", QQ)
748
+ R, x, y = ring("x,y", Rt)
749
+ f = QQ(1, 2)*t*x**2 + QQ(1, 2)*t**2*x
750
+
751
+ assert R.dmp_factor_list(f) == \
752
+ (QQ(1, 2)*t, [(x, 1),
753
+ (x + t, 1)])
754
+
755
+ R, x, y = ring("x,y", FF(2))
756
+ raises(NotImplementedError, lambda: R.dmp_factor_list(x**2 + y**2))
757
+
758
+ R, x, y = ring("x,y", EX)
759
+ raises(DomainError, lambda: R.dmp_factor_list(EX(sin(1))))
760
+
761
+
762
+ def test_dup_irreducible_p():
763
+ R, x = ring("x", ZZ)
764
+ assert R.dup_irreducible_p(x**2 + x + 1) is True
765
+ assert R.dup_irreducible_p(x**2 + 2*x + 1) is False
766
+
767
+
768
+ def test_dmp_irreducible_p():
769
+ R, x, y = ring("x,y", ZZ)
770
+ assert R.dmp_irreducible_p(x**2 + x + 1) is True
771
+ assert R.dmp_irreducible_p(x**2 + 2*x + 1) is False
env-llmeval/lib/python3.10/site-packages/sympy/polys/tests/test_fields.py ADDED
@@ -0,0 +1,362 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Test sparse rational functions. """
2
+
3
+ from sympy.polys.fields import field, sfield, FracField, FracElement
4
+ from sympy.polys.rings import ring
5
+ from sympy.polys.domains import ZZ, QQ
6
+ from sympy.polys.orderings import lex
7
+
8
+ from sympy.testing.pytest import raises, XFAIL
9
+ from sympy.core import symbols, E
10
+ from sympy.core.numbers import Rational
11
+ from sympy.functions.elementary.exponential import (exp, log)
12
+ from sympy.functions.elementary.miscellaneous import sqrt
13
+
14
+ def test_FracField___init__():
15
+ F1 = FracField("x,y", ZZ, lex)
16
+ F2 = FracField("x,y", ZZ, lex)
17
+ F3 = FracField("x,y,z", ZZ, lex)
18
+
19
+ assert F1.x == F1.gens[0]
20
+ assert F1.y == F1.gens[1]
21
+ assert F1.x == F2.x
22
+ assert F1.y == F2.y
23
+ assert F1.x != F3.x
24
+ assert F1.y != F3.y
25
+
26
+ def test_FracField___hash__():
27
+ F, x, y, z = field("x,y,z", QQ)
28
+ assert hash(F)
29
+
30
+ def test_FracField___eq__():
31
+ assert field("x,y,z", QQ)[0] == field("x,y,z", QQ)[0]
32
+ assert field("x,y,z", QQ)[0] is field("x,y,z", QQ)[0]
33
+
34
+ assert field("x,y,z", QQ)[0] != field("x,y,z", ZZ)[0]
35
+ assert field("x,y,z", QQ)[0] is not field("x,y,z", ZZ)[0]
36
+
37
+ assert field("x,y,z", ZZ)[0] != field("x,y,z", QQ)[0]
38
+ assert field("x,y,z", ZZ)[0] is not field("x,y,z", QQ)[0]
39
+
40
+ assert field("x,y,z", QQ)[0] != field("x,y", QQ)[0]
41
+ assert field("x,y,z", QQ)[0] is not field("x,y", QQ)[0]
42
+
43
+ assert field("x,y", QQ)[0] != field("x,y,z", QQ)[0]
44
+ assert field("x,y", QQ)[0] is not field("x,y,z", QQ)[0]
45
+
46
+ def test_sfield():
47
+ x = symbols("x")
48
+
49
+ F = FracField((E, exp(exp(x)), exp(x)), ZZ, lex)
50
+ e, exex, ex = F.gens
51
+ assert sfield(exp(x)*exp(exp(x) + 1 + log(exp(x) + 3)/2)**2/(exp(x) + 3)) \
52
+ == (F, e**2*exex**2*ex)
53
+
54
+ F = FracField((x, exp(1/x), log(x), x**QQ(1, 3)), ZZ, lex)
55
+ _, ex, lg, x3 = F.gens
56
+ assert sfield(((x-3)*log(x)+4*x**2)*exp(1/x+log(x)/3)/x**2) == \
57
+ (F, (4*F.x**2*ex + F.x*ex*lg - 3*ex*lg)/x3**5)
58
+
59
+ F = FracField((x, log(x), sqrt(x + log(x))), ZZ, lex)
60
+ _, lg, srt = F.gens
61
+ assert sfield((x + 1) / (x * (x + log(x))**QQ(3, 2)) - 1/(x * log(x)**2)) \
62
+ == (F, (F.x*lg**2 - F.x*srt + lg**2 - lg*srt)/
63
+ (F.x**2*lg**2*srt + F.x*lg**3*srt))
64
+
65
+ def test_FracElement___hash__():
66
+ F, x, y, z = field("x,y,z", QQ)
67
+ assert hash(x*y/z)
68
+
69
+ def test_FracElement_copy():
70
+ F, x, y, z = field("x,y,z", ZZ)
71
+
72
+ f = x*y/3*z
73
+ g = f.copy()
74
+
75
+ assert f == g
76
+ g.numer[(1, 1, 1)] = 7
77
+ assert f != g
78
+
79
+ def test_FracElement_as_expr():
80
+ F, x, y, z = field("x,y,z", ZZ)
81
+ f = (3*x**2*y - x*y*z)/(7*z**3 + 1)
82
+
83
+ X, Y, Z = F.symbols
84
+ g = (3*X**2*Y - X*Y*Z)/(7*Z**3 + 1)
85
+
86
+ assert f != g
87
+ assert f.as_expr() == g
88
+
89
+ X, Y, Z = symbols("x,y,z")
90
+ g = (3*X**2*Y - X*Y*Z)/(7*Z**3 + 1)
91
+
92
+ assert f != g
93
+ assert f.as_expr(X, Y, Z) == g
94
+
95
+ raises(ValueError, lambda: f.as_expr(X))
96
+
97
+ def test_FracElement_from_expr():
98
+ x, y, z = symbols("x,y,z")
99
+ F, X, Y, Z = field((x, y, z), ZZ)
100
+
101
+ f = F.from_expr(1)
102
+ assert f == 1 and isinstance(f, F.dtype)
103
+
104
+ f = F.from_expr(Rational(3, 7))
105
+ assert f == F(3)/7 and isinstance(f, F.dtype)
106
+
107
+ f = F.from_expr(x)
108
+ assert f == X and isinstance(f, F.dtype)
109
+
110
+ f = F.from_expr(Rational(3,7)*x)
111
+ assert f == X*Rational(3, 7) and isinstance(f, F.dtype)
112
+
113
+ f = F.from_expr(1/x)
114
+ assert f == 1/X and isinstance(f, F.dtype)
115
+
116
+ f = F.from_expr(x*y*z)
117
+ assert f == X*Y*Z and isinstance(f, F.dtype)
118
+
119
+ f = F.from_expr(x*y/z)
120
+ assert f == X*Y/Z and isinstance(f, F.dtype)
121
+
122
+ f = F.from_expr(x*y*z + x*y + x)
123
+ assert f == X*Y*Z + X*Y + X and isinstance(f, F.dtype)
124
+
125
+ f = F.from_expr((x*y*z + x*y + x)/(x*y + 7))
126
+ assert f == (X*Y*Z + X*Y + X)/(X*Y + 7) and isinstance(f, F.dtype)
127
+
128
+ f = F.from_expr(x**3*y*z + x**2*y**7 + 1)
129
+ assert f == X**3*Y*Z + X**2*Y**7 + 1 and isinstance(f, F.dtype)
130
+
131
+ raises(ValueError, lambda: F.from_expr(2**x))
132
+ raises(ValueError, lambda: F.from_expr(7*x + sqrt(2)))
133
+
134
+ assert isinstance(ZZ[2**x].get_field().convert(2**(-x)),
135
+ FracElement)
136
+ assert isinstance(ZZ[x**2].get_field().convert(x**(-6)),
137
+ FracElement)
138
+ assert isinstance(ZZ[exp(Rational(1, 3))].get_field().convert(E),
139
+ FracElement)
140
+
141
+
142
+ def test_FracField_nested():
143
+ a, b, x = symbols('a b x')
144
+ F1 = ZZ.frac_field(a, b)
145
+ F2 = F1.frac_field(x)
146
+ frac = F2(a + b)
147
+ assert frac.numer == F1.poly_ring(x)(a + b)
148
+ assert frac.numer.coeffs() == [F1(a + b)]
149
+ assert frac.denom == F1.poly_ring(x)(1)
150
+
151
+ F3 = ZZ.poly_ring(a, b)
152
+ F4 = F3.frac_field(x)
153
+ frac = F4(a + b)
154
+ assert frac.numer == F3.poly_ring(x)(a + b)
155
+ assert frac.numer.coeffs() == [F3(a + b)]
156
+ assert frac.denom == F3.poly_ring(x)(1)
157
+
158
+ frac = F2(F3(a + b))
159
+ assert frac.numer == F1.poly_ring(x)(a + b)
160
+ assert frac.numer.coeffs() == [F1(a + b)]
161
+ assert frac.denom == F1.poly_ring(x)(1)
162
+
163
+ frac = F4(F1(a + b))
164
+ assert frac.numer == F3.poly_ring(x)(a + b)
165
+ assert frac.numer.coeffs() == [F3(a + b)]
166
+ assert frac.denom == F3.poly_ring(x)(1)
167
+
168
+
169
+ def test_FracElement__lt_le_gt_ge__():
170
+ F, x, y = field("x,y", ZZ)
171
+
172
+ assert F(1) < 1/x < 1/x**2 < 1/x**3
173
+ assert F(1) <= 1/x <= 1/x**2 <= 1/x**3
174
+
175
+ assert -7/x < 1/x < 3/x < y/x < 1/x**2
176
+ assert -7/x <= 1/x <= 3/x <= y/x <= 1/x**2
177
+
178
+ assert 1/x**3 > 1/x**2 > 1/x > F(1)
179
+ assert 1/x**3 >= 1/x**2 >= 1/x >= F(1)
180
+
181
+ assert 1/x**2 > y/x > 3/x > 1/x > -7/x
182
+ assert 1/x**2 >= y/x >= 3/x >= 1/x >= -7/x
183
+
184
+ def test_FracElement___neg__():
185
+ F, x,y = field("x,y", QQ)
186
+
187
+ f = (7*x - 9)/y
188
+ g = (-7*x + 9)/y
189
+
190
+ assert -f == g
191
+ assert -g == f
192
+
193
+ def test_FracElement___add__():
194
+ F, x,y = field("x,y", QQ)
195
+
196
+ f, g = 1/x, 1/y
197
+ assert f + g == g + f == (x + y)/(x*y)
198
+
199
+ assert x + F.ring.gens[0] == F.ring.gens[0] + x == 2*x
200
+
201
+ F, x,y = field("x,y", ZZ)
202
+ assert x + 3 == 3 + x
203
+ assert x + QQ(3,7) == QQ(3,7) + x == (7*x + 3)/7
204
+
205
+ Fuv, u,v = field("u,v", ZZ)
206
+ Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
207
+
208
+ f = (u*v + x)/(y + u*v)
209
+ assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u*v}
210
+ assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u*v}
211
+
212
+ Ruv, u,v = ring("u,v", ZZ)
213
+ Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
214
+
215
+ f = (u*v + x)/(y + u*v)
216
+ assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u*v}
217
+ assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u*v}
218
+
219
+ def test_FracElement___sub__():
220
+ F, x,y = field("x,y", QQ)
221
+
222
+ f, g = 1/x, 1/y
223
+ assert f - g == (-x + y)/(x*y)
224
+
225
+ assert x - F.ring.gens[0] == F.ring.gens[0] - x == 0
226
+
227
+ F, x,y = field("x,y", ZZ)
228
+ assert x - 3 == -(3 - x)
229
+ assert x - QQ(3,7) == -(QQ(3,7) - x) == (7*x - 3)/7
230
+
231
+ Fuv, u,v = field("u,v", ZZ)
232
+ Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
233
+
234
+ f = (u*v - x)/(y - u*v)
235
+ assert dict(f.numer) == {(1, 0, 0, 0):-1, (0, 0, 0, 0): u*v}
236
+ assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0):-u*v}
237
+
238
+ Ruv, u,v = ring("u,v", ZZ)
239
+ Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
240
+
241
+ f = (u*v - x)/(y - u*v)
242
+ assert dict(f.numer) == {(1, 0, 0, 0):-1, (0, 0, 0, 0): u*v}
243
+ assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0):-u*v}
244
+
245
+ def test_FracElement___mul__():
246
+ F, x,y = field("x,y", QQ)
247
+
248
+ f, g = 1/x, 1/y
249
+ assert f*g == g*f == 1/(x*y)
250
+
251
+ assert x*F.ring.gens[0] == F.ring.gens[0]*x == x**2
252
+
253
+ F, x,y = field("x,y", ZZ)
254
+ assert x*3 == 3*x
255
+ assert x*QQ(3,7) == QQ(3,7)*x == x*Rational(3, 7)
256
+
257
+ Fuv, u,v = field("u,v", ZZ)
258
+ Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
259
+
260
+ f = ((u + 1)*x*y + 1)/((v - 1)*z - t*u*v - 1)
261
+ assert dict(f.numer) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
262
+ assert dict(f.denom) == {(0, 0, 1, 0): v - 1, (0, 0, 0, 1): -u*v, (0, 0, 0, 0): -1}
263
+
264
+ Ruv, u,v = ring("u,v", ZZ)
265
+ Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
266
+
267
+ f = ((u + 1)*x*y + 1)/((v - 1)*z - t*u*v - 1)
268
+ assert dict(f.numer) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
269
+ assert dict(f.denom) == {(0, 0, 1, 0): v - 1, (0, 0, 0, 1): -u*v, (0, 0, 0, 0): -1}
270
+
271
+ def test_FracElement___truediv__():
272
+ F, x,y = field("x,y", QQ)
273
+
274
+ f, g = 1/x, 1/y
275
+ assert f/g == y/x
276
+
277
+ assert x/F.ring.gens[0] == F.ring.gens[0]/x == 1
278
+
279
+ F, x,y = field("x,y", ZZ)
280
+ assert x*3 == 3*x
281
+ assert x/QQ(3,7) == (QQ(3,7)/x)**-1 == x*Rational(7, 3)
282
+
283
+ raises(ZeroDivisionError, lambda: x/0)
284
+ raises(ZeroDivisionError, lambda: 1/(x - x))
285
+ raises(ZeroDivisionError, lambda: x/(x - x))
286
+
287
+ Fuv, u,v = field("u,v", ZZ)
288
+ Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
289
+
290
+ f = (u*v)/(x*y)
291
+ assert dict(f.numer) == {(0, 0, 0, 0): u*v}
292
+ assert dict(f.denom) == {(1, 1, 0, 0): 1}
293
+
294
+ g = (x*y)/(u*v)
295
+ assert dict(g.numer) == {(1, 1, 0, 0): 1}
296
+ assert dict(g.denom) == {(0, 0, 0, 0): u*v}
297
+
298
+ Ruv, u,v = ring("u,v", ZZ)
299
+ Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
300
+
301
+ f = (u*v)/(x*y)
302
+ assert dict(f.numer) == {(0, 0, 0, 0): u*v}
303
+ assert dict(f.denom) == {(1, 1, 0, 0): 1}
304
+
305
+ g = (x*y)/(u*v)
306
+ assert dict(g.numer) == {(1, 1, 0, 0): 1}
307
+ assert dict(g.denom) == {(0, 0, 0, 0): u*v}
308
+
309
+ def test_FracElement___pow__():
310
+ F, x,y = field("x,y", QQ)
311
+
312
+ f, g = 1/x, 1/y
313
+
314
+ assert f**3 == 1/x**3
315
+ assert g**3 == 1/y**3
316
+
317
+ assert (f*g)**3 == 1/(x**3*y**3)
318
+ assert (f*g)**-3 == (x*y)**3
319
+
320
+ raises(ZeroDivisionError, lambda: (x - x)**-3)
321
+
322
+ def test_FracElement_diff():
323
+ F, x,y,z = field("x,y,z", ZZ)
324
+
325
+ assert ((x**2 + y)/(z + 1)).diff(x) == 2*x/(z + 1)
326
+
327
+ @XFAIL
328
+ def test_FracElement___call__():
329
+ F, x,y,z = field("x,y,z", ZZ)
330
+ f = (x**2 + 3*y)/z
331
+
332
+ r = f(1, 1, 1)
333
+ assert r == 4 and not isinstance(r, FracElement)
334
+ raises(ZeroDivisionError, lambda: f(1, 1, 0))
335
+
336
+ def test_FracElement_evaluate():
337
+ F, x,y,z = field("x,y,z", ZZ)
338
+ Fyz = field("y,z", ZZ)[0]
339
+ f = (x**2 + 3*y)/z
340
+
341
+ assert f.evaluate(x, 0) == 3*Fyz.y/Fyz.z
342
+ raises(ZeroDivisionError, lambda: f.evaluate(z, 0))
343
+
344
+ def test_FracElement_subs():
345
+ F, x,y,z = field("x,y,z", ZZ)
346
+ f = (x**2 + 3*y)/z
347
+
348
+ assert f.subs(x, 0) == 3*y/z
349
+ raises(ZeroDivisionError, lambda: f.subs(z, 0))
350
+
351
+ def test_FracElement_compose():
352
+ pass
353
+
354
+ def test_FracField_index():
355
+ a = symbols("a")
356
+ F, x, y, z = field('x y z', QQ)
357
+ assert F.index(x) == 0
358
+ assert F.index(y) == 1
359
+
360
+ raises(ValueError, lambda: F.index(1))
361
+ raises(ValueError, lambda: F.index(a))
362
+ pass