jasonfan commited on
Commit
c5deb2c
·
verified ·
1 Parent(s): 8ab5764

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. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/galoisgroups.py +623 -0
  2. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/minpoly.py +882 -0
  3. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/modules.py +2114 -0
  4. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/primes.py +784 -0
  5. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/resolvent_lookup.py +456 -0
  6. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/subfield.py +516 -0
  7. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/__init__.py +0 -0
  8. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_basis.py +85 -0
  9. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_galoisgroups.py +143 -0
  10. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_minpoly.py +490 -0
  11. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_modules.py +752 -0
  12. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_numbers.py +202 -0
  13. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_primes.py +296 -0
  14. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_subfield.py +317 -0
  15. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_utilities.py +113 -0
  16. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/utilities.py +474 -0
  17. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/__init__.py +0 -0
  18. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_appellseqs.py +91 -0
  19. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_constructor.py +236 -0
  20. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_densearith.py +1007 -0
  21. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_densebasic.py +730 -0
  22. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_densetools.py +714 -0
  23. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_dispersion.py +95 -0
  24. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_distributedmodules.py +208 -0
  25. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_euclidtools.py +712 -0
  26. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_factortools.py +784 -0
  27. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_fields.py +353 -0
  28. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_galoistools.py +875 -0
  29. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_groebnertools.py +533 -0
  30. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_heuristicgcd.py +152 -0
  31. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_hypothesis.py +36 -0
  32. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_injections.py +39 -0
  33. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_modulargcd.py +325 -0
  34. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_monomials.py +269 -0
  35. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_multivariate_resultants.py +294 -0
  36. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_orderings.py +124 -0
  37. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_orthopolys.py +175 -0
  38. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_partfrac.py +249 -0
  39. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polyclasses.py +601 -0
  40. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polyfuncs.py +126 -0
  41. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polymatrix.py +185 -0
  42. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polyoptions.py +485 -0
  43. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polyroots.py +758 -0
  44. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polytools.py +0 -0
  45. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polyutils.py +300 -0
  46. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_puiseux.py +204 -0
  47. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_pythonrational.py +139 -0
  48. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_rationaltools.py +63 -0
  49. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_ring_series.py +831 -0
  50. miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_rings.py +1591 -0
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/galoisgroups.py ADDED
@@ -0,0 +1,623 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Compute Galois groups of polynomials.
3
+
4
+ We use algorithms from [1], with some modifications to use lookup tables for
5
+ resolvents.
6
+
7
+ References
8
+ ==========
9
+
10
+ .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory*.
11
+
12
+ """
13
+
14
+ from collections import defaultdict
15
+ import random
16
+
17
+ from sympy.core.symbol import Dummy, symbols
18
+ from sympy.ntheory.primetest import is_square
19
+ from sympy.polys.domains import ZZ
20
+ from sympy.polys.densebasic import dup_random
21
+ from sympy.polys.densetools import dup_eval
22
+ from sympy.polys.euclidtools import dup_discriminant
23
+ from sympy.polys.factortools import dup_factor_list, dup_irreducible_p
24
+ from sympy.polys.numberfields.galois_resolvents import (
25
+ GaloisGroupException, get_resolvent_by_lookup, define_resolvents,
26
+ Resolvent,
27
+ )
28
+ from sympy.polys.numberfields.utilities import coeff_search
29
+ from sympy.polys.polytools import (Poly, poly_from_expr,
30
+ PolificationFailed, ComputationFailed)
31
+ from sympy.polys.sqfreetools import dup_sqf_p
32
+ from sympy.utilities import public
33
+
34
+
35
+ class MaxTriesException(GaloisGroupException):
36
+ ...
37
+
38
+
39
+ def tschirnhausen_transformation(T, max_coeff=10, max_tries=30, history=None,
40
+ fixed_order=True):
41
+ r"""
42
+ Given a univariate, monic, irreducible polynomial over the integers, find
43
+ another such polynomial defining the same number field.
44
+
45
+ Explanation
46
+ ===========
47
+
48
+ See Alg 6.3.4 of [1].
49
+
50
+ Parameters
51
+ ==========
52
+
53
+ T : Poly
54
+ The given polynomial
55
+ max_coeff : int
56
+ When choosing a transformation as part of the process,
57
+ keep the coeffs between plus and minus this.
58
+ max_tries : int
59
+ Consider at most this many transformations.
60
+ history : set, None, optional (default=None)
61
+ Pass a set of ``Poly.rep``'s in order to prevent any of these
62
+ polynomials from being returned as the polynomial ``U`` i.e. the
63
+ transformation of the given polynomial *T*. The given poly *T* will
64
+ automatically be added to this set, before we try to find a new one.
65
+ fixed_order : bool, default True
66
+ If ``True``, work through candidate transformations A(x) in a fixed
67
+ order, from small coeffs to large, resulting in deterministic behavior.
68
+ If ``False``, the A(x) are chosen randomly, while still working our way
69
+ up from small coefficients to larger ones.
70
+
71
+ Returns
72
+ =======
73
+
74
+ Pair ``(A, U)``
75
+
76
+ ``A`` and ``U`` are ``Poly``, ``A`` is the
77
+ transformation, and ``U`` is the transformed polynomial that defines
78
+ the same number field as *T*. The polynomial ``A`` maps the roots of
79
+ *T* to the roots of ``U``.
80
+
81
+ Raises
82
+ ======
83
+
84
+ MaxTriesException
85
+ if could not find a polynomial before exceeding *max_tries*.
86
+
87
+ """
88
+ X = Dummy('X')
89
+ n = T.degree()
90
+ if history is None:
91
+ history = set()
92
+ history.add(T.rep)
93
+
94
+ if fixed_order:
95
+ coeff_generators = {}
96
+ deg_coeff_sum = 3
97
+ current_degree = 2
98
+
99
+ def get_coeff_generator(degree):
100
+ gen = coeff_generators.get(degree, coeff_search(degree, 1))
101
+ coeff_generators[degree] = gen
102
+ return gen
103
+
104
+ for i in range(max_tries):
105
+
106
+ # We never use linear A(x), since applying a fixed linear transformation
107
+ # to all roots will only multiply the discriminant of T by a square
108
+ # integer. This will change nothing important. In particular, if disc(T)
109
+ # was zero before, it will still be zero now, and typically we apply
110
+ # the transformation in hopes of replacing T by a squarefree poly.
111
+
112
+ if fixed_order:
113
+ # If d is degree and c max coeff, we move through the dc-space
114
+ # along lines of constant sum. First d + c = 3 with (d, c) = (2, 1).
115
+ # Then d + c = 4 with (d, c) = (3, 1), (2, 2). Then d + c = 5 with
116
+ # (d, c) = (4, 1), (3, 2), (2, 3), and so forth. For a given (d, c)
117
+ # we go though all sets of coeffs where max = c, before moving on.
118
+ gen = get_coeff_generator(current_degree)
119
+ coeffs = next(gen)
120
+ m = max(abs(c) for c in coeffs)
121
+ if current_degree + m > deg_coeff_sum:
122
+ if current_degree == 2:
123
+ deg_coeff_sum += 1
124
+ current_degree = deg_coeff_sum - 1
125
+ else:
126
+ current_degree -= 1
127
+ gen = get_coeff_generator(current_degree)
128
+ coeffs = next(gen)
129
+ a = [ZZ(1)] + [ZZ(c) for c in coeffs]
130
+
131
+ else:
132
+ # We use a progressive coeff bound, up to the max specified, since it
133
+ # is preferable to succeed with smaller coeffs.
134
+ # Give each coeff bound five tries, before incrementing.
135
+ C = min(i//5 + 1, max_coeff)
136
+ d = random.randint(2, n - 1)
137
+ a = dup_random(d, -C, C, ZZ)
138
+
139
+ A = Poly(a, T.gen)
140
+ U = Poly(T.resultant(X - A), X)
141
+ if U.rep not in history and dup_sqf_p(U.rep.to_list(), ZZ):
142
+ return A, U
143
+ raise MaxTriesException
144
+
145
+
146
+ def has_square_disc(T):
147
+ """Convenience to check if a Poly or dup has square discriminant. """
148
+ d = T.discriminant() if isinstance(T, Poly) else dup_discriminant(T, ZZ)
149
+ return is_square(d)
150
+
151
+
152
+ def _galois_group_degree_3(T, max_tries=30, randomize=False):
153
+ r"""
154
+ Compute the Galois group of a polynomial of degree 3.
155
+
156
+ Explanation
157
+ ===========
158
+
159
+ Uses Prop 6.3.5 of [1].
160
+
161
+ """
162
+ from sympy.combinatorics.galois import S3TransitiveSubgroups
163
+ return ((S3TransitiveSubgroups.A3, True) if has_square_disc(T)
164
+ else (S3TransitiveSubgroups.S3, False))
165
+
166
+
167
+ def _galois_group_degree_4_root_approx(T, max_tries=30, randomize=False):
168
+ r"""
169
+ Compute the Galois group of a polynomial of degree 4.
170
+
171
+ Explanation
172
+ ===========
173
+
174
+ Follows Alg 6.3.7 of [1], using a pure root approximation approach.
175
+
176
+ """
177
+ from sympy.combinatorics.permutations import Permutation
178
+ from sympy.combinatorics.galois import S4TransitiveSubgroups
179
+
180
+ X = symbols('X0 X1 X2 X3')
181
+ # We start by considering the resolvent for the form
182
+ # F = X0*X2 + X1*X3
183
+ # and the group G = S4. In this case, the stabilizer H is D4 = < (0123), (02) >,
184
+ # and a set of representatives of G/H is {I, (01), (03)}
185
+ F1 = X[0]*X[2] + X[1]*X[3]
186
+ s1 = [
187
+ Permutation(3),
188
+ Permutation(3)(0, 1),
189
+ Permutation(3)(0, 3)
190
+ ]
191
+ R1 = Resolvent(F1, X, s1)
192
+
193
+ # In the second half of the algorithm (if we reach it), we use another
194
+ # form and set of coset representatives. However, we may need to permute
195
+ # them first, so cannot form their resolvent now.
196
+ F2_pre = X[0]*X[1]**2 + X[1]*X[2]**2 + X[2]*X[3]**2 + X[3]*X[0]**2
197
+ s2_pre = [
198
+ Permutation(3),
199
+ Permutation(3)(0, 2)
200
+ ]
201
+
202
+ history = set()
203
+ for i in range(max_tries):
204
+ if i > 0:
205
+ # If we're retrying, need a new polynomial T.
206
+ _, T = tschirnhausen_transformation(T, max_tries=max_tries,
207
+ history=history,
208
+ fixed_order=not randomize)
209
+
210
+ R_dup, _, i0 = R1.eval_for_poly(T, find_integer_root=True)
211
+ # If R is not squarefree, must retry.
212
+ if not dup_sqf_p(R_dup, ZZ):
213
+ continue
214
+
215
+ # By Prop 6.3.1 of [1], Gal(T) is contained in A4 iff disc(T) is square.
216
+ sq_disc = has_square_disc(T)
217
+
218
+ if i0 is None:
219
+ # By Thm 6.3.3 of [1], Gal(T) is not conjugate to any subgroup of the
220
+ # stabilizer H = D4 that we chose. This means Gal(T) is either A4 or S4.
221
+ return ((S4TransitiveSubgroups.A4, True) if sq_disc
222
+ else (S4TransitiveSubgroups.S4, False))
223
+
224
+ # Gal(T) is conjugate to a subgroup of H = D4, so it is either V, C4
225
+ # or D4 itself.
226
+
227
+ if sq_disc:
228
+ # Neither C4 nor D4 is contained in A4, so Gal(T) must be V.
229
+ return (S4TransitiveSubgroups.V, True)
230
+
231
+ # Gal(T) can only be D4 or C4.
232
+ # We will now use our second resolvent, with G being that conjugate of D4 that
233
+ # Gal(T) is contained in. To determine the right conjugate, we will need
234
+ # the permutation corresponding to the integer root we found.
235
+ sigma = s1[i0]
236
+ # Applying sigma means permuting the args of F, and
237
+ # conjugating the set of coset representatives.
238
+ F2 = F2_pre.subs(zip(X, sigma(X)), simultaneous=True)
239
+ s2 = [sigma*tau*sigma for tau in s2_pre]
240
+ R2 = Resolvent(F2, X, s2)
241
+ R_dup, _, _ = R2.eval_for_poly(T)
242
+ d = dup_discriminant(R_dup, ZZ)
243
+ # If d is zero (R has a repeated root), must retry.
244
+ if d == 0:
245
+ continue
246
+ if is_square(d):
247
+ return (S4TransitiveSubgroups.C4, False)
248
+ else:
249
+ return (S4TransitiveSubgroups.D4, False)
250
+
251
+ raise MaxTriesException
252
+
253
+
254
+ def _galois_group_degree_4_lookup(T, max_tries=30, randomize=False):
255
+ r"""
256
+ Compute the Galois group of a polynomial of degree 4.
257
+
258
+ Explanation
259
+ ===========
260
+
261
+ Based on Alg 6.3.6 of [1], but uses resolvent coeff lookup.
262
+
263
+ """
264
+ from sympy.combinatorics.galois import S4TransitiveSubgroups
265
+
266
+ history = set()
267
+ for i in range(max_tries):
268
+ R_dup = get_resolvent_by_lookup(T, 0)
269
+ if dup_sqf_p(R_dup, ZZ):
270
+ break
271
+ _, T = tschirnhausen_transformation(T, max_tries=max_tries,
272
+ history=history,
273
+ fixed_order=not randomize)
274
+ else:
275
+ raise MaxTriesException
276
+
277
+ # Compute list L of degrees of irreducible factors of R, in increasing order:
278
+ fl = dup_factor_list(R_dup, ZZ)
279
+ L = sorted(sum([
280
+ [len(r) - 1] * e for r, e in fl[1]
281
+ ], []))
282
+
283
+ if L == [6]:
284
+ return ((S4TransitiveSubgroups.A4, True) if has_square_disc(T)
285
+ else (S4TransitiveSubgroups.S4, False))
286
+
287
+ if L == [1, 1, 4]:
288
+ return (S4TransitiveSubgroups.C4, False)
289
+
290
+ if L == [2, 2, 2]:
291
+ return (S4TransitiveSubgroups.V, True)
292
+
293
+ assert L == [2, 4]
294
+ return (S4TransitiveSubgroups.D4, False)
295
+
296
+
297
+ def _galois_group_degree_5_hybrid(T, max_tries=30, randomize=False):
298
+ r"""
299
+ Compute the Galois group of a polynomial of degree 5.
300
+
301
+ Explanation
302
+ ===========
303
+
304
+ Based on Alg 6.3.9 of [1], but uses a hybrid approach, combining resolvent
305
+ coeff lookup, with root approximation.
306
+
307
+ """
308
+ from sympy.combinatorics.galois import S5TransitiveSubgroups
309
+ from sympy.combinatorics.permutations import Permutation
310
+
311
+ X5 = symbols("X0,X1,X2,X3,X4")
312
+ res = define_resolvents()
313
+ F51, _, s51 = res[(5, 1)]
314
+ F51 = F51.as_expr(*X5)
315
+ R51 = Resolvent(F51, X5, s51)
316
+
317
+ history = set()
318
+ reached_second_stage = False
319
+ for i in range(max_tries):
320
+ if i > 0:
321
+ _, T = tschirnhausen_transformation(T, max_tries=max_tries,
322
+ history=history,
323
+ fixed_order=not randomize)
324
+ R51_dup = get_resolvent_by_lookup(T, 1)
325
+ if not dup_sqf_p(R51_dup, ZZ):
326
+ continue
327
+
328
+ # First stage
329
+ # If we have not yet reached the second stage, then the group still
330
+ # might be S5, A5, or M20, so must test for that.
331
+ if not reached_second_stage:
332
+ sq_disc = has_square_disc(T)
333
+
334
+ if dup_irreducible_p(R51_dup, ZZ):
335
+ return ((S5TransitiveSubgroups.A5, True) if sq_disc
336
+ else (S5TransitiveSubgroups.S5, False))
337
+
338
+ if not sq_disc:
339
+ return (S5TransitiveSubgroups.M20, False)
340
+
341
+ # Second stage
342
+ reached_second_stage = True
343
+ # R51 must have an integer root for T.
344
+ # To choose our second resolvent, we need to know which conjugate of
345
+ # F51 is a root.
346
+ rounded_roots = R51.round_roots_to_integers_for_poly(T)
347
+ # These are integers, and candidates to be roots of R51.
348
+ # We find the first one that actually is a root.
349
+ for permutation_index, candidate_root in rounded_roots.items():
350
+ if not dup_eval(R51_dup, candidate_root, ZZ):
351
+ break
352
+
353
+ X = X5
354
+ F2_pre = X[0]*X[1]**2 + X[1]*X[2]**2 + X[2]*X[3]**2 + X[3]*X[4]**2 + X[4]*X[0]**2
355
+ s2_pre = [
356
+ Permutation(4),
357
+ Permutation(4)(0, 1)(2, 4)
358
+ ]
359
+
360
+ i0 = permutation_index
361
+ sigma = s51[i0]
362
+ F2 = F2_pre.subs(zip(X, sigma(X)), simultaneous=True)
363
+ s2 = [sigma*tau*sigma for tau in s2_pre]
364
+ R2 = Resolvent(F2, X, s2)
365
+ R_dup, _, _ = R2.eval_for_poly(T)
366
+ d = dup_discriminant(R_dup, ZZ)
367
+
368
+ if d == 0:
369
+ continue
370
+ if is_square(d):
371
+ return (S5TransitiveSubgroups.C5, True)
372
+ else:
373
+ return (S5TransitiveSubgroups.D5, True)
374
+
375
+ raise MaxTriesException
376
+
377
+
378
+ def _galois_group_degree_5_lookup_ext_factor(T, max_tries=30, randomize=False):
379
+ r"""
380
+ Compute the Galois group of a polynomial of degree 5.
381
+
382
+ Explanation
383
+ ===========
384
+
385
+ Based on Alg 6.3.9 of [1], but uses resolvent coeff lookup, plus
386
+ factorization over an algebraic extension.
387
+
388
+ """
389
+ from sympy.combinatorics.galois import S5TransitiveSubgroups
390
+
391
+ _T = T
392
+
393
+ history = set()
394
+ for i in range(max_tries):
395
+ R_dup = get_resolvent_by_lookup(T, 1)
396
+ if dup_sqf_p(R_dup, ZZ):
397
+ break
398
+ _, T = tschirnhausen_transformation(T, max_tries=max_tries,
399
+ history=history,
400
+ fixed_order=not randomize)
401
+ else:
402
+ raise MaxTriesException
403
+
404
+ sq_disc = has_square_disc(T)
405
+
406
+ if dup_irreducible_p(R_dup, ZZ):
407
+ return ((S5TransitiveSubgroups.A5, True) if sq_disc
408
+ else (S5TransitiveSubgroups.S5, False))
409
+
410
+ if not sq_disc:
411
+ return (S5TransitiveSubgroups.M20, False)
412
+
413
+ # If we get this far, Gal(T) can only be D5 or C5.
414
+ # But for Gal(T) to have order 5, T must already split completely in
415
+ # the extension field obtained by adjoining a single one of its roots.
416
+ fl = Poly(_T, domain=ZZ.alg_field_from_poly(_T)).factor_list()[1]
417
+ if len(fl) == 5:
418
+ return (S5TransitiveSubgroups.C5, True)
419
+ else:
420
+ return (S5TransitiveSubgroups.D5, True)
421
+
422
+
423
+ def _galois_group_degree_6_lookup(T, max_tries=30, randomize=False):
424
+ r"""
425
+ Compute the Galois group of a polynomial of degree 6.
426
+
427
+ Explanation
428
+ ===========
429
+
430
+ Based on Alg 6.3.10 of [1], but uses resolvent coeff lookup.
431
+
432
+ """
433
+ from sympy.combinatorics.galois import S6TransitiveSubgroups
434
+
435
+ # First resolvent:
436
+
437
+ history = set()
438
+ for i in range(max_tries):
439
+ R_dup = get_resolvent_by_lookup(T, 1)
440
+ if dup_sqf_p(R_dup, ZZ):
441
+ break
442
+ _, T = tschirnhausen_transformation(T, max_tries=max_tries,
443
+ history=history,
444
+ fixed_order=not randomize)
445
+ else:
446
+ raise MaxTriesException
447
+
448
+ fl = dup_factor_list(R_dup, ZZ)
449
+
450
+ # Group the factors by degree.
451
+ factors_by_deg = defaultdict(list)
452
+ for r, _ in fl[1]:
453
+ factors_by_deg[len(r) - 1].append(r)
454
+
455
+ L = sorted(sum([
456
+ [d] * len(ff) for d, ff in factors_by_deg.items()
457
+ ], []))
458
+
459
+ T_has_sq_disc = has_square_disc(T)
460
+
461
+ if L == [1, 2, 3]:
462
+ f1 = factors_by_deg[3][0]
463
+ return ((S6TransitiveSubgroups.C6, False) if has_square_disc(f1)
464
+ else (S6TransitiveSubgroups.D6, False))
465
+
466
+ elif L == [3, 3]:
467
+ f1, f2 = factors_by_deg[3]
468
+ any_square = has_square_disc(f1) or has_square_disc(f2)
469
+ return ((S6TransitiveSubgroups.G18, False) if any_square
470
+ else (S6TransitiveSubgroups.G36m, False))
471
+
472
+ elif L == [2, 4]:
473
+ if T_has_sq_disc:
474
+ return (S6TransitiveSubgroups.S4p, True)
475
+ else:
476
+ f1 = factors_by_deg[4][0]
477
+ return ((S6TransitiveSubgroups.A4xC2, False) if has_square_disc(f1)
478
+ else (S6TransitiveSubgroups.S4xC2, False))
479
+
480
+ elif L == [1, 1, 4]:
481
+ return ((S6TransitiveSubgroups.A4, True) if T_has_sq_disc
482
+ else (S6TransitiveSubgroups.S4m, False))
483
+
484
+ elif L == [1, 5]:
485
+ return ((S6TransitiveSubgroups.PSL2F5, True) if T_has_sq_disc
486
+ else (S6TransitiveSubgroups.PGL2F5, False))
487
+
488
+ elif L == [1, 1, 1, 3]:
489
+ return (S6TransitiveSubgroups.S3, False)
490
+
491
+ assert L == [6]
492
+
493
+ # Second resolvent:
494
+
495
+ history = set()
496
+ for i in range(max_tries):
497
+ R_dup = get_resolvent_by_lookup(T, 2)
498
+ if dup_sqf_p(R_dup, ZZ):
499
+ break
500
+ _, T = tschirnhausen_transformation(T, max_tries=max_tries,
501
+ history=history,
502
+ fixed_order=not randomize)
503
+ else:
504
+ raise MaxTriesException
505
+
506
+ T_has_sq_disc = has_square_disc(T)
507
+
508
+ if dup_irreducible_p(R_dup, ZZ):
509
+ return ((S6TransitiveSubgroups.A6, True) if T_has_sq_disc
510
+ else (S6TransitiveSubgroups.S6, False))
511
+ else:
512
+ return ((S6TransitiveSubgroups.G36p, True) if T_has_sq_disc
513
+ else (S6TransitiveSubgroups.G72, False))
514
+
515
+
516
+ @public
517
+ def galois_group(f, *gens, by_name=False, max_tries=30, randomize=False, **args):
518
+ r"""
519
+ Compute the Galois group for polynomials *f* up to degree 6.
520
+
521
+ Examples
522
+ ========
523
+
524
+ >>> from sympy import galois_group
525
+ >>> from sympy.abc import x
526
+ >>> f = x**4 + 1
527
+ >>> G, alt = galois_group(f)
528
+ >>> print(G)
529
+ PermutationGroup([
530
+ (0 1)(2 3),
531
+ (0 2)(1 3)])
532
+
533
+ The group is returned along with a boolean, indicating whether it is
534
+ contained in the alternating group $A_n$, where $n$ is the degree of *T*.
535
+ Along with other group properties, this can help determine which group it
536
+ is:
537
+
538
+ >>> alt
539
+ True
540
+ >>> G.order()
541
+ 4
542
+
543
+ Alternatively, the group can be returned by name:
544
+
545
+ >>> G_name, _ = galois_group(f, by_name=True)
546
+ >>> print(G_name)
547
+ S4TransitiveSubgroups.V
548
+
549
+ The group itself can then be obtained by calling the name's
550
+ ``get_perm_group()`` method:
551
+
552
+ >>> G_name.get_perm_group()
553
+ PermutationGroup([
554
+ (0 1)(2 3),
555
+ (0 2)(1 3)])
556
+
557
+ Group names are values of the enum classes
558
+ :py:class:`sympy.combinatorics.galois.S1TransitiveSubgroups`,
559
+ :py:class:`sympy.combinatorics.galois.S2TransitiveSubgroups`,
560
+ etc.
561
+
562
+ Parameters
563
+ ==========
564
+
565
+ f : Expr
566
+ Irreducible polynomial over :ref:`ZZ` or :ref:`QQ`, whose Galois group
567
+ is to be determined.
568
+ gens : optional list of symbols
569
+ For converting *f* to Poly, and will be passed on to the
570
+ :py:func:`~.poly_from_expr` function.
571
+ by_name : bool, default False
572
+ If ``True``, the Galois group will be returned by name.
573
+ Otherwise it will be returned as a :py:class:`~.PermutationGroup`.
574
+ max_tries : int, default 30
575
+ Make at most this many attempts in those steps that involve
576
+ generating Tschirnhausen transformations.
577
+ randomize : bool, default False
578
+ If ``True``, then use random coefficients when generating Tschirnhausen
579
+ transformations. Otherwise try transformations in a fixed order. Both
580
+ approaches start with small coefficients and degrees and work upward.
581
+ args : optional
582
+ For converting *f* to Poly, and will be passed on to the
583
+ :py:func:`~.poly_from_expr` function.
584
+
585
+ Returns
586
+ =======
587
+
588
+ Pair ``(G, alt)``
589
+ The first element ``G`` indicates the Galois group. It is an instance
590
+ of one of the :py:class:`sympy.combinatorics.galois.S1TransitiveSubgroups`
591
+ :py:class:`sympy.combinatorics.galois.S2TransitiveSubgroups`, etc. enum
592
+ classes if *by_name* was ``True``, and a :py:class:`~.PermutationGroup`
593
+ if ``False``.
594
+
595
+ The second element is a boolean, saying whether the group is contained
596
+ in the alternating group $A_n$ ($n$ the degree of *T*).
597
+
598
+ Raises
599
+ ======
600
+
601
+ ValueError
602
+ if *f* is of an unsupported degree.
603
+
604
+ MaxTriesException
605
+ if could not complete before exceeding *max_tries* in those steps
606
+ that involve generating Tschirnhausen transformations.
607
+
608
+ See Also
609
+ ========
610
+
611
+ .Poly.galois_group
612
+
613
+ """
614
+ gens = gens or []
615
+ args = args or {}
616
+
617
+ try:
618
+ F, opt = poly_from_expr(f, *gens, **args)
619
+ except PolificationFailed as exc:
620
+ raise ComputationFailed('galois_group', 1, exc)
621
+
622
+ return F.galois_group(by_name=by_name, max_tries=max_tries,
623
+ randomize=randomize)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/minpoly.py ADDED
@@ -0,0 +1,882 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Minimal polynomials for algebraic numbers."""
2
+
3
+ from functools import reduce
4
+
5
+ from sympy.core.add import Add
6
+ from sympy.core.exprtools import Factors
7
+ from sympy.core.function import expand_mul, expand_multinomial, _mexpand
8
+ from sympy.core.mul import Mul
9
+ from sympy.core.numbers import (I, Rational, pi, _illegal)
10
+ from sympy.core.singleton import S
11
+ from sympy.core.symbol import Dummy
12
+ from sympy.core.sympify import sympify
13
+ from sympy.core.traversal import preorder_traversal
14
+ from sympy.functions.elementary.exponential import exp
15
+ from sympy.functions.elementary.miscellaneous import sqrt, cbrt
16
+ from sympy.functions.elementary.trigonometric import cos, sin, tan
17
+ from sympy.ntheory.factor_ import divisors
18
+ from sympy.utilities.iterables import subsets
19
+
20
+ from sympy.polys.domains import ZZ, QQ, FractionField
21
+ from sympy.polys.orthopolys import dup_chebyshevt
22
+ from sympy.polys.polyerrors import (
23
+ NotAlgebraic,
24
+ GeneratorsError,
25
+ )
26
+ from sympy.polys.polytools import (
27
+ Poly, PurePoly, invert, factor_list, groebner, resultant,
28
+ degree, poly_from_expr, parallel_poly_from_expr, lcm
29
+ )
30
+ from sympy.polys.polyutils import dict_from_expr, expr_from_dict
31
+ from sympy.polys.ring_series import rs_compose_add
32
+ from sympy.polys.rings import ring
33
+ from sympy.polys.rootoftools import CRootOf
34
+ from sympy.polys.specialpolys import cyclotomic_poly
35
+ from sympy.utilities import (
36
+ numbered_symbols, public, sift
37
+ )
38
+
39
+
40
+ def _choose_factor(factors, x, v, dom=QQ, prec=200, bound=5):
41
+ """
42
+ Return a factor having root ``v``
43
+ It is assumed that one of the factors has root ``v``.
44
+ """
45
+
46
+ if isinstance(factors[0], tuple):
47
+ factors = [f[0] for f in factors]
48
+ if len(factors) == 1:
49
+ return factors[0]
50
+
51
+ prec1 = 10
52
+ points = {}
53
+ symbols = dom.symbols if hasattr(dom, 'symbols') else []
54
+ while prec1 <= prec:
55
+ # when dealing with non-Rational numbers we usually evaluate
56
+ # with `subs` argument but we only need a ballpark evaluation
57
+ fe = [f.as_expr().xreplace({x:v}) for f in factors]
58
+ if v.is_number:
59
+ fe = [f.n(prec) for f in fe]
60
+
61
+ # assign integers [0, n) to symbols (if any)
62
+ for n in subsets(range(bound), k=len(symbols), repetition=True):
63
+ for s, i in zip(symbols, n):
64
+ points[s] = i
65
+
66
+ # evaluate the expression at these points
67
+ candidates = [(abs(f.subs(points).n(prec1)), i)
68
+ for i,f in enumerate(fe)]
69
+
70
+ # if we get invalid numbers (e.g. from division by zero)
71
+ # we try again
72
+ if any(i in _illegal for i, _ in candidates):
73
+ continue
74
+
75
+ # find the smallest two -- if they differ significantly
76
+ # then we assume we have found the factor that becomes
77
+ # 0 when v is substituted into it
78
+ can = sorted(candidates)
79
+ (a, ix), (b, _) = can[:2]
80
+ if b > a * 10**6: # XXX what to use?
81
+ return factors[ix]
82
+
83
+ prec1 *= 2
84
+
85
+ raise NotImplementedError("multiple candidates for the minimal polynomial of %s" % v)
86
+
87
+
88
+ def _is_sum_surds(p):
89
+ return all(f.is_Rational or f.is_Pow and
90
+ f.base.is_Rational and (2*f.exp).is_Integer and f.is_extended_real
91
+ for t in Add.make_args(p) for f in Mul.make_args(t))
92
+
93
+
94
+ def _separate_sq(p):
95
+ """
96
+ helper function for ``_minimal_polynomial_sq``
97
+
98
+ It selects a rational ``g`` such that the polynomial ``p``
99
+ consists of a sum of terms whose surds squared have gcd equal to ``g``
100
+ and a sum of terms with surds squared prime with ``g``;
101
+ then it takes the field norm to eliminate ``sqrt(g)``
102
+
103
+ See simplify.simplify.split_surds and polytools.sqf_norm.
104
+
105
+ Examples
106
+ ========
107
+
108
+ >>> from sympy import sqrt
109
+ >>> from sympy.abc import x
110
+ >>> from sympy.polys.numberfields.minpoly import _separate_sq
111
+ >>> p= -x + sqrt(2) + sqrt(3) + sqrt(7)
112
+ >>> p = _separate_sq(p); p
113
+ -x**2 + 2*sqrt(3)*x + 2*sqrt(7)*x - 2*sqrt(21) - 8
114
+ >>> p = _separate_sq(p); p
115
+ -x**4 + 4*sqrt(7)*x**3 - 32*x**2 + 8*sqrt(7)*x + 20
116
+ >>> p = _separate_sq(p); p
117
+ -x**8 + 48*x**6 - 536*x**4 + 1728*x**2 - 400
118
+
119
+ """
120
+ def is_sqrt(expr):
121
+ return expr.is_Pow and expr.exp is S.Half
122
+ # p = c1*sqrt(q1) + ... + cn*sqrt(qn) -> a = [(c1, q1), .., (cn, qn)]
123
+ a = []
124
+ for y in p.args:
125
+ if not y.is_Mul:
126
+ if is_sqrt(y):
127
+ a.append((S.One, y**2))
128
+ elif y.is_Atom:
129
+ a.append((y, S.One))
130
+ elif y.is_Pow and y.exp.is_integer:
131
+ a.append((y, S.One))
132
+ else:
133
+ raise NotImplementedError
134
+ else:
135
+ T, F = sift(y.args, is_sqrt, binary=True)
136
+ a.append((Mul(*F), Mul(*T)**2))
137
+ a.sort(key=lambda z: z[1])
138
+ if a[-1][1] is S.One:
139
+ # there are no surds
140
+ return p
141
+ surds = [z for y, z in a]
142
+ for i in range(len(surds)):
143
+ if surds[i] != 1:
144
+ break
145
+ from sympy.simplify.radsimp import _split_gcd
146
+ g, b1, b2 = _split_gcd(*surds[i:])
147
+ a1 = []
148
+ a2 = []
149
+ for y, z in a:
150
+ if z in b1:
151
+ a1.append(y*z**S.Half)
152
+ else:
153
+ a2.append(y*z**S.Half)
154
+ p1 = Add(*a1)
155
+ p2 = Add(*a2)
156
+ p = _mexpand(p1**2) - _mexpand(p2**2)
157
+ return p
158
+
159
+ def _minimal_polynomial_sq(p, n, x):
160
+ """
161
+ Returns the minimal polynomial for the ``nth-root`` of a sum of surds
162
+ or ``None`` if it fails.
163
+
164
+ Parameters
165
+ ==========
166
+
167
+ p : sum of surds
168
+ n : positive integer
169
+ x : variable of the returned polynomial
170
+
171
+ Examples
172
+ ========
173
+
174
+ >>> from sympy.polys.numberfields.minpoly import _minimal_polynomial_sq
175
+ >>> from sympy import sqrt
176
+ >>> from sympy.abc import x
177
+ >>> q = 1 + sqrt(2) + sqrt(3)
178
+ >>> _minimal_polynomial_sq(q, 3, x)
179
+ x**12 - 4*x**9 - 4*x**6 + 16*x**3 - 8
180
+
181
+ """
182
+ p = sympify(p)
183
+ n = sympify(n)
184
+ if not n.is_Integer or not n > 0 or not _is_sum_surds(p):
185
+ return None
186
+ pn = p**Rational(1, n)
187
+ # eliminate the square roots
188
+ p -= x
189
+ while 1:
190
+ p1 = _separate_sq(p)
191
+ if p1 is p:
192
+ p = p1.subs({x:x**n})
193
+ break
194
+ else:
195
+ p = p1
196
+
197
+ # _separate_sq eliminates field extensions in a minimal way, so that
198
+ # if n = 1 then `p = constant*(minimal_polynomial(p))`
199
+ # if n > 1 it contains the minimal polynomial as a factor.
200
+ if n == 1:
201
+ p1 = Poly(p)
202
+ if p.coeff(x**p1.degree(x)) < 0:
203
+ p = -p
204
+ p = p.primitive()[1]
205
+ return p
206
+ # by construction `p` has root `pn`
207
+ # the minimal polynomial is the factor vanishing in x = pn
208
+ factors = factor_list(p)[1]
209
+
210
+ result = _choose_factor(factors, x, pn)
211
+ return result
212
+
213
+ def _minpoly_op_algebraic_element(op, ex1, ex2, x, dom, mp1=None, mp2=None):
214
+ """
215
+ return the minimal polynomial for ``op(ex1, ex2)``
216
+
217
+ Parameters
218
+ ==========
219
+
220
+ op : operation ``Add`` or ``Mul``
221
+ ex1, ex2 : expressions for the algebraic elements
222
+ x : indeterminate of the polynomials
223
+ dom: ground domain
224
+ mp1, mp2 : minimal polynomials for ``ex1`` and ``ex2`` or None
225
+
226
+ Examples
227
+ ========
228
+
229
+ >>> from sympy import sqrt, Add, Mul, QQ
230
+ >>> from sympy.polys.numberfields.minpoly import _minpoly_op_algebraic_element
231
+ >>> from sympy.abc import x, y
232
+ >>> p1 = sqrt(sqrt(2) + 1)
233
+ >>> p2 = sqrt(sqrt(2) - 1)
234
+ >>> _minpoly_op_algebraic_element(Mul, p1, p2, x, QQ)
235
+ x - 1
236
+ >>> q1 = sqrt(y)
237
+ >>> q2 = 1 / y
238
+ >>> _minpoly_op_algebraic_element(Add, q1, q2, x, QQ.frac_field(y))
239
+ x**2*y**2 - 2*x*y - y**3 + 1
240
+
241
+ References
242
+ ==========
243
+
244
+ .. [1] https://en.wikipedia.org/wiki/Resultant
245
+ .. [2] I.M. Isaacs, Proc. Amer. Math. Soc. 25 (1970), 638
246
+ "Degrees of sums in a separable field extension".
247
+
248
+ """
249
+ y = Dummy(str(x))
250
+ if mp1 is None:
251
+ mp1 = _minpoly_compose(ex1, x, dom)
252
+ if mp2 is None:
253
+ mp2 = _minpoly_compose(ex2, y, dom)
254
+ else:
255
+ mp2 = mp2.subs({x: y})
256
+
257
+ if op is Add:
258
+ # mp1a = mp1.subs({x: x - y})
259
+ if dom == QQ:
260
+ R, X = ring('X', QQ)
261
+ p1 = R(dict_from_expr(mp1)[0])
262
+ p2 = R(dict_from_expr(mp2)[0])
263
+ else:
264
+ (p1, p2), _ = parallel_poly_from_expr((mp1, x - y), x, y)
265
+ r = p1.compose(p2)
266
+ mp1a = r.as_expr()
267
+
268
+ elif op is Mul:
269
+ mp1a = _muly(mp1, x, y)
270
+ else:
271
+ raise NotImplementedError('option not available')
272
+
273
+ if op is Mul or dom != QQ:
274
+ r = resultant(mp1a, mp2, gens=[y, x])
275
+ else:
276
+ r = rs_compose_add(p1, p2)
277
+ r = expr_from_dict(r.as_expr_dict(), x)
278
+
279
+ deg1 = degree(mp1, x)
280
+ deg2 = degree(mp2, y)
281
+ if op is Mul and deg1 == 1 or deg2 == 1:
282
+ # if deg1 = 1, then mp1 = x - a; mp1a = x - y - a;
283
+ # r = mp2(x - a), so that `r` is irreducible
284
+ return r
285
+
286
+ r = Poly(r, x, domain=dom)
287
+ _, factors = r.factor_list()
288
+ res = _choose_factor(factors, x, op(ex1, ex2), dom)
289
+ return res.as_expr()
290
+
291
+
292
+ def _invertx(p, x):
293
+ """
294
+ Returns ``expand_mul(x**degree(p, x)*p.subs(x, 1/x))``
295
+ """
296
+ p1 = poly_from_expr(p, x)[0]
297
+
298
+ n = degree(p1)
299
+ a = [c * x**(n - i) for (i,), c in p1.terms()]
300
+ return Add(*a)
301
+
302
+
303
+ def _muly(p, x, y):
304
+ """
305
+ Returns ``_mexpand(y**deg*p.subs({x:x / y}))``
306
+ """
307
+ p1 = poly_from_expr(p, x)[0]
308
+
309
+ n = degree(p1)
310
+ a = [c * x**i * y**(n - i) for (i,), c in p1.terms()]
311
+ return Add(*a)
312
+
313
+
314
+ def _minpoly_pow(ex, pw, x, dom, mp=None):
315
+ """
316
+ Returns ``minpoly(ex**pw, x)``
317
+
318
+ Parameters
319
+ ==========
320
+
321
+ ex : algebraic element
322
+ pw : rational number
323
+ x : indeterminate of the polynomial
324
+ dom: ground domain
325
+ mp : minimal polynomial of ``p``
326
+
327
+ Examples
328
+ ========
329
+
330
+ >>> from sympy import sqrt, QQ, Rational
331
+ >>> from sympy.polys.numberfields.minpoly import _minpoly_pow, minpoly
332
+ >>> from sympy.abc import x, y
333
+ >>> p = sqrt(1 + sqrt(2))
334
+ >>> _minpoly_pow(p, 2, x, QQ)
335
+ x**2 - 2*x - 1
336
+ >>> minpoly(p**2, x)
337
+ x**2 - 2*x - 1
338
+ >>> _minpoly_pow(y, Rational(1, 3), x, QQ.frac_field(y))
339
+ x**3 - y
340
+ >>> minpoly(y**Rational(1, 3), x)
341
+ x**3 - y
342
+
343
+ """
344
+ pw = sympify(pw)
345
+ if not mp:
346
+ mp = _minpoly_compose(ex, x, dom)
347
+ if not pw.is_rational:
348
+ raise NotAlgebraic("%s does not seem to be an algebraic element" % ex)
349
+ if pw < 0:
350
+ if mp == x:
351
+ raise ZeroDivisionError('%s is zero' % ex)
352
+ mp = _invertx(mp, x)
353
+ if pw == -1:
354
+ return mp
355
+ pw = -pw
356
+ ex = 1/ex
357
+
358
+ y = Dummy(str(x))
359
+ mp = mp.subs({x: y})
360
+ n, d = pw.as_numer_denom()
361
+ res = Poly(resultant(mp, x**d - y**n, gens=[y]), x, domain=dom)
362
+ _, factors = res.factor_list()
363
+ res = _choose_factor(factors, x, ex**pw, dom)
364
+ return res.as_expr()
365
+
366
+
367
+ def _minpoly_add(x, dom, *a):
368
+ """
369
+ returns ``minpoly(Add(*a), dom, x)``
370
+ """
371
+ mp = _minpoly_op_algebraic_element(Add, a[0], a[1], x, dom)
372
+ p = a[0] + a[1]
373
+ for px in a[2:]:
374
+ mp = _minpoly_op_algebraic_element(Add, p, px, x, dom, mp1=mp)
375
+ p = p + px
376
+ return mp
377
+
378
+
379
+ def _minpoly_mul(x, dom, *a):
380
+ """
381
+ returns ``minpoly(Mul(*a), dom, x)``
382
+ """
383
+ mp = _minpoly_op_algebraic_element(Mul, a[0], a[1], x, dom)
384
+ p = a[0] * a[1]
385
+ for px in a[2:]:
386
+ mp = _minpoly_op_algebraic_element(Mul, p, px, x, dom, mp1=mp)
387
+ p = p * px
388
+ return mp
389
+
390
+
391
+ def _minpoly_sin(ex, x):
392
+ """
393
+ Returns the minimal polynomial of ``sin(ex)``
394
+ see https://mathworld.wolfram.com/TrigonometryAngles.html
395
+ """
396
+ c, a = ex.args[0].as_coeff_Mul()
397
+ if a is pi:
398
+ if c.is_rational:
399
+ n = c.q
400
+ q = sympify(n)
401
+ if q.is_prime:
402
+ # for a = pi*p/q with q odd prime, using chebyshevt
403
+ # write sin(q*a) = mp(sin(a))*sin(a);
404
+ # the roots of mp(x) are sin(pi*p/q) for p = 1,..., q - 1
405
+ a = dup_chebyshevt(n, ZZ)
406
+ return Add(*[x**(n - i - 1)*a[i] for i in range(n)])
407
+ if c.p == 1:
408
+ if q == 9:
409
+ return 64*x**6 - 96*x**4 + 36*x**2 - 3
410
+
411
+ if n % 2 == 1:
412
+ # for a = pi*p/q with q odd, use
413
+ # sin(q*a) = 0 to see that the minimal polynomial must be
414
+ # a factor of dup_chebyshevt(n, ZZ)
415
+ a = dup_chebyshevt(n, ZZ)
416
+ a = [x**(n - i)*a[i] for i in range(n + 1)]
417
+ r = Add(*a)
418
+ _, factors = factor_list(r)
419
+ res = _choose_factor(factors, x, ex)
420
+ return res
421
+
422
+ expr = ((1 - cos(2*c*pi))/2)**S.Half
423
+ res = _minpoly_compose(expr, x, QQ)
424
+ return res
425
+
426
+ raise NotAlgebraic("%s does not seem to be an algebraic element" % ex)
427
+
428
+
429
+ def _minpoly_cos(ex, x):
430
+ """
431
+ Returns the minimal polynomial of ``cos(ex)``
432
+ see https://mathworld.wolfram.com/TrigonometryAngles.html
433
+ """
434
+ c, a = ex.args[0].as_coeff_Mul()
435
+ if a is pi:
436
+ if c.is_rational:
437
+ if c.p == 1:
438
+ if c.q == 7:
439
+ return 8*x**3 - 4*x**2 - 4*x + 1
440
+ if c.q == 9:
441
+ return 8*x**3 - 6*x - 1
442
+ elif c.p == 2:
443
+ q = sympify(c.q)
444
+ if q.is_prime:
445
+ s = _minpoly_sin(ex, x)
446
+ return _mexpand(s.subs({x:sqrt((1 - x)/2)}))
447
+
448
+ # for a = pi*p/q, cos(q*a) =T_q(cos(a)) = (-1)**p
449
+ n = int(c.q)
450
+ a = dup_chebyshevt(n, ZZ)
451
+ a = [x**(n - i)*a[i] for i in range(n + 1)]
452
+ r = Add(*a) - (-1)**c.p
453
+ _, factors = factor_list(r)
454
+ res = _choose_factor(factors, x, ex)
455
+ return res
456
+
457
+ raise NotAlgebraic("%s does not seem to be an algebraic element" % ex)
458
+
459
+
460
+ def _minpoly_tan(ex, x):
461
+ """
462
+ Returns the minimal polynomial of ``tan(ex)``
463
+ see https://github.com/sympy/sympy/issues/21430
464
+ """
465
+ c, a = ex.args[0].as_coeff_Mul()
466
+ if a is pi:
467
+ if c.is_rational:
468
+ c = c * 2
469
+ n = int(c.q)
470
+ a = n if c.p % 2 == 0 else 1
471
+ terms = []
472
+ for k in range((c.p+1)%2, n+1, 2):
473
+ terms.append(a*x**k)
474
+ a = -(a*(n-k-1)*(n-k)) // ((k+1)*(k+2))
475
+
476
+ r = Add(*terms)
477
+ _, factors = factor_list(r)
478
+ res = _choose_factor(factors, x, ex)
479
+ return res
480
+
481
+ raise NotAlgebraic("%s does not seem to be an algebraic element" % ex)
482
+
483
+
484
+ def _minpoly_exp(ex, x):
485
+ """
486
+ Returns the minimal polynomial of ``exp(ex)``
487
+ """
488
+ c, a = ex.args[0].as_coeff_Mul()
489
+ if a == I*pi:
490
+ if c.is_rational:
491
+ q = sympify(c.q)
492
+ if c.p == 1 or c.p == -1:
493
+ if q == 3:
494
+ return x**2 - x + 1
495
+ if q == 4:
496
+ return x**4 + 1
497
+ if q == 6:
498
+ return x**4 - x**2 + 1
499
+ if q == 8:
500
+ return x**8 + 1
501
+ if q == 9:
502
+ return x**6 - x**3 + 1
503
+ if q == 10:
504
+ return x**8 - x**6 + x**4 - x**2 + 1
505
+ if q.is_prime:
506
+ s = 0
507
+ for i in range(q):
508
+ s += (-x)**i
509
+ return s
510
+
511
+ # x**(2*q) = product(factors)
512
+ factors = [cyclotomic_poly(i, x) for i in divisors(2*q)]
513
+ mp = _choose_factor(factors, x, ex)
514
+ return mp
515
+ else:
516
+ raise NotAlgebraic("%s does not seem to be an algebraic element" % ex)
517
+ raise NotAlgebraic("%s does not seem to be an algebraic element" % ex)
518
+
519
+
520
+ def _minpoly_rootof(ex, x):
521
+ """
522
+ Returns the minimal polynomial of a ``CRootOf`` object.
523
+ """
524
+ p = ex.expr
525
+ p = p.subs({ex.poly.gens[0]:x})
526
+ _, factors = factor_list(p, x)
527
+ result = _choose_factor(factors, x, ex)
528
+ return result
529
+
530
+
531
+ def _minpoly_compose(ex, x, dom):
532
+ """
533
+ Computes the minimal polynomial of an algebraic element
534
+ using operations on minimal polynomials
535
+
536
+ Examples
537
+ ========
538
+
539
+ >>> from sympy import minimal_polynomial, sqrt, Rational
540
+ >>> from sympy.abc import x, y
541
+ >>> minimal_polynomial(sqrt(2) + 3*Rational(1, 3), x, compose=True)
542
+ x**2 - 2*x - 1
543
+ >>> minimal_polynomial(sqrt(y) + 1/y, x, compose=True)
544
+ x**2*y**2 - 2*x*y - y**3 + 1
545
+
546
+ """
547
+ if ex.is_Rational:
548
+ return ex.q*x - ex.p
549
+ if ex is I:
550
+ _, factors = factor_list(x**2 + 1, x, domain=dom)
551
+ return x**2 + 1 if len(factors) == 1 else x - I
552
+
553
+ if ex is S.GoldenRatio:
554
+ _, factors = factor_list(x**2 - x - 1, x, domain=dom)
555
+ if len(factors) == 1:
556
+ return x**2 - x - 1
557
+ else:
558
+ return _choose_factor(factors, x, (1 + sqrt(5))/2, dom=dom)
559
+
560
+ if ex is S.TribonacciConstant:
561
+ _, factors = factor_list(x**3 - x**2 - x - 1, x, domain=dom)
562
+ if len(factors) == 1:
563
+ return x**3 - x**2 - x - 1
564
+ else:
565
+ fac = (1 + cbrt(19 - 3*sqrt(33)) + cbrt(19 + 3*sqrt(33))) / 3
566
+ return _choose_factor(factors, x, fac, dom=dom)
567
+
568
+ if hasattr(dom, 'symbols') and ex in dom.symbols:
569
+ return x - ex
570
+
571
+ if dom.is_QQ and _is_sum_surds(ex):
572
+ # eliminate the square roots
573
+ v = ex
574
+ ex -= x
575
+ while 1:
576
+ ex1 = _separate_sq(ex)
577
+ if ex1 is ex:
578
+ return _choose_factor(factor_list(ex)[1], x, v)
579
+ else:
580
+ ex = ex1
581
+
582
+ if ex.is_Add:
583
+ res = _minpoly_add(x, dom, *ex.args)
584
+ elif ex.is_Mul:
585
+ f = Factors(ex).factors
586
+ r = sift(f.items(), lambda itx: itx[0].is_Rational and itx[1].is_Rational)
587
+ if r[True] and dom == QQ:
588
+ ex1 = Mul(*[bx**ex for bx, ex in r[False] + r[None]])
589
+ r1 = dict(r[True])
590
+ dens = [y.q for y in r1.values()]
591
+ lcmdens = reduce(lcm, dens, 1)
592
+ neg1 = S.NegativeOne
593
+ expn1 = r1.pop(neg1, S.Zero)
594
+ nums = [base**(y.p*lcmdens // y.q) for base, y in r1.items()]
595
+ ex2 = Mul(*nums)
596
+ mp1 = minimal_polynomial(ex1, x)
597
+ # use the fact that in SymPy canonicalization products of integers
598
+ # raised to rational powers are organized in relatively prime
599
+ # bases, and that in ``base**(n/d)`` a perfect power is
600
+ # simplified with the root
601
+ # Powers of -1 have to be treated separately to preserve sign.
602
+ mp2 = ex2.q*x**lcmdens - ex2.p*neg1**(expn1*lcmdens)
603
+ ex2 = neg1**expn1 * ex2**Rational(1, lcmdens)
604
+ res = _minpoly_op_algebraic_element(Mul, ex1, ex2, x, dom, mp1=mp1, mp2=mp2)
605
+ else:
606
+ res = _minpoly_mul(x, dom, *ex.args)
607
+ elif ex.is_Pow:
608
+ res = _minpoly_pow(ex.base, ex.exp, x, dom)
609
+ elif ex.__class__ is sin:
610
+ res = _minpoly_sin(ex, x)
611
+ elif ex.__class__ is cos:
612
+ res = _minpoly_cos(ex, x)
613
+ elif ex.__class__ is tan:
614
+ res = _minpoly_tan(ex, x)
615
+ elif ex.__class__ is exp:
616
+ res = _minpoly_exp(ex, x)
617
+ elif ex.__class__ is CRootOf:
618
+ res = _minpoly_rootof(ex, x)
619
+ else:
620
+ raise NotAlgebraic("%s does not seem to be an algebraic element" % ex)
621
+ return res
622
+
623
+
624
+ @public
625
+ def minimal_polynomial(ex, x=None, compose=True, polys=False, domain=None):
626
+ """
627
+ Computes the minimal polynomial of an algebraic element.
628
+
629
+ Parameters
630
+ ==========
631
+
632
+ ex : Expr
633
+ Element or expression whose minimal polynomial is to be calculated.
634
+
635
+ x : Symbol, optional
636
+ Independent variable of the minimal polynomial
637
+
638
+ compose : boolean, optional (default=True)
639
+ Method to use for computing minimal polynomial. If ``compose=True``
640
+ (default) then ``_minpoly_compose`` is used, if ``compose=False`` then
641
+ groebner bases are used.
642
+
643
+ polys : boolean, optional (default=False)
644
+ If ``True`` returns a ``Poly`` object else an ``Expr`` object.
645
+
646
+ domain : Domain, optional
647
+ Ground domain
648
+
649
+ Notes
650
+ =====
651
+
652
+ By default ``compose=True``, the minimal polynomial of the subexpressions of ``ex``
653
+ are computed, then the arithmetic operations on them are performed using the resultant
654
+ and factorization.
655
+ If ``compose=False``, a bottom-up algorithm is used with ``groebner``.
656
+ The default algorithm stalls less frequently.
657
+
658
+ If no ground domain is given, it will be generated automatically from the expression.
659
+
660
+ Examples
661
+ ========
662
+
663
+ >>> from sympy import minimal_polynomial, sqrt, solve, QQ
664
+ >>> from sympy.abc import x, y
665
+
666
+ >>> minimal_polynomial(sqrt(2), x)
667
+ x**2 - 2
668
+ >>> minimal_polynomial(sqrt(2), x, domain=QQ.algebraic_field(sqrt(2)))
669
+ x - sqrt(2)
670
+ >>> minimal_polynomial(sqrt(2) + sqrt(3), x)
671
+ x**4 - 10*x**2 + 1
672
+ >>> minimal_polynomial(solve(x**3 + x + 3)[0], x)
673
+ x**3 + x + 3
674
+ >>> minimal_polynomial(sqrt(y), x)
675
+ x**2 - y
676
+
677
+ """
678
+
679
+ ex = sympify(ex)
680
+ if ex.is_number:
681
+ # not sure if it's always needed but try it for numbers (issue 8354)
682
+ ex = _mexpand(ex, recursive=True)
683
+ for expr in preorder_traversal(ex):
684
+ if expr.is_AlgebraicNumber:
685
+ compose = False
686
+ break
687
+
688
+ if x is not None:
689
+ x, cls = sympify(x), Poly
690
+ else:
691
+ x, cls = Dummy('x'), PurePoly
692
+
693
+ if not domain:
694
+ if ex.free_symbols:
695
+ domain = FractionField(QQ, list(ex.free_symbols))
696
+ else:
697
+ domain = QQ
698
+ if hasattr(domain, 'symbols') and x in domain.symbols:
699
+ raise GeneratorsError("the variable %s is an element of the ground "
700
+ "domain %s" % (x, domain))
701
+
702
+ if compose:
703
+ result = _minpoly_compose(ex, x, domain)
704
+ result = result.primitive()[1]
705
+ c = result.coeff(x**degree(result, x))
706
+ if c.is_negative:
707
+ result = expand_mul(-result)
708
+ return cls(result, x, field=True) if polys else result.collect(x)
709
+
710
+ if not domain.is_QQ:
711
+ raise NotImplementedError("groebner method only works for QQ")
712
+
713
+ result = _minpoly_groebner(ex, x, cls)
714
+ return cls(result, x, field=True) if polys else result.collect(x)
715
+
716
+
717
+ def _minpoly_groebner(ex, x, cls):
718
+ """
719
+ Computes the minimal polynomial of an algebraic number
720
+ using Groebner bases
721
+
722
+ Examples
723
+ ========
724
+
725
+ >>> from sympy import minimal_polynomial, sqrt, Rational
726
+ >>> from sympy.abc import x
727
+ >>> minimal_polynomial(sqrt(2) + 3*Rational(1, 3), x, compose=False)
728
+ x**2 - 2*x - 1
729
+
730
+ """
731
+
732
+ generator = numbered_symbols('a', cls=Dummy)
733
+ mapping, symbols = {}, {}
734
+
735
+ def update_mapping(ex, exp, base=None):
736
+ a = next(generator)
737
+ symbols[ex] = a
738
+
739
+ if base is not None:
740
+ mapping[ex] = a**exp + base
741
+ else:
742
+ mapping[ex] = exp.as_expr(a)
743
+
744
+ return a
745
+
746
+ def bottom_up_scan(ex):
747
+ """
748
+ Transform a given algebraic expression *ex* into a multivariate
749
+ polynomial, by introducing fresh variables with defining equations.
750
+
751
+ Explanation
752
+ ===========
753
+
754
+ The critical elements of the algebraic expression *ex* are root
755
+ extractions, instances of :py:class:`~.AlgebraicNumber`, and negative
756
+ powers.
757
+
758
+ When we encounter a root extraction or an :py:class:`~.AlgebraicNumber`
759
+ we replace this expression with a fresh variable ``a_i``, and record
760
+ the defining polynomial for ``a_i``. For example, if ``a_0**(1/3)``
761
+ occurs, we will replace it with ``a_1``, and record the new defining
762
+ polynomial ``a_1**3 - a_0``.
763
+
764
+ When we encounter a negative power we transform it into a positive
765
+ power by algebraically inverting the base. This means computing the
766
+ minimal polynomial in ``x`` for the base, inverting ``x`` modulo this
767
+ poly (which generates a new polynomial) and then substituting the
768
+ original base expression for ``x`` in this last polynomial.
769
+
770
+ We return the transformed expression, and we record the defining
771
+ equations for new symbols using the ``update_mapping()`` function.
772
+
773
+ """
774
+ if ex.is_Atom:
775
+ if ex is S.ImaginaryUnit:
776
+ if ex not in mapping:
777
+ return update_mapping(ex, 2, 1)
778
+ else:
779
+ return symbols[ex]
780
+ elif ex.is_Rational:
781
+ return ex
782
+ elif ex.is_Add:
783
+ return Add(*[ bottom_up_scan(g) for g in ex.args ])
784
+ elif ex.is_Mul:
785
+ return Mul(*[ bottom_up_scan(g) for g in ex.args ])
786
+ elif ex.is_Pow:
787
+ if ex.exp.is_Rational:
788
+ if ex.exp < 0:
789
+ minpoly_base = _minpoly_groebner(ex.base, x, cls)
790
+ inverse = invert(x, minpoly_base).as_expr()
791
+ base_inv = inverse.subs(x, ex.base).expand()
792
+
793
+ if ex.exp == -1:
794
+ return bottom_up_scan(base_inv)
795
+ else:
796
+ ex = base_inv**(-ex.exp)
797
+ if not ex.exp.is_Integer:
798
+ base, exp = (
799
+ ex.base**ex.exp.p).expand(), Rational(1, ex.exp.q)
800
+ else:
801
+ base, exp = ex.base, ex.exp
802
+ base = bottom_up_scan(base)
803
+ expr = base**exp
804
+
805
+ if expr not in mapping:
806
+ if exp.is_Integer:
807
+ return expr.expand()
808
+ else:
809
+ return update_mapping(expr, 1 / exp, -base)
810
+ else:
811
+ return symbols[expr]
812
+ elif ex.is_AlgebraicNumber:
813
+ if ex not in mapping:
814
+ return update_mapping(ex, ex.minpoly_of_element())
815
+ else:
816
+ return symbols[ex]
817
+
818
+ raise NotAlgebraic("%s does not seem to be an algebraic number" % ex)
819
+
820
+ def simpler_inverse(ex):
821
+ """
822
+ Returns True if it is more likely that the minimal polynomial
823
+ algorithm works better with the inverse
824
+ """
825
+ if ex.is_Pow:
826
+ if (1/ex.exp).is_integer and ex.exp < 0:
827
+ if ex.base.is_Add:
828
+ return True
829
+ if ex.is_Mul:
830
+ hit = True
831
+ for p in ex.args:
832
+ if p.is_Add:
833
+ return False
834
+ if p.is_Pow:
835
+ if p.base.is_Add and p.exp > 0:
836
+ return False
837
+
838
+ if hit:
839
+ return True
840
+ return False
841
+
842
+ inverted = False
843
+ ex = expand_multinomial(ex)
844
+ if ex.is_AlgebraicNumber:
845
+ return ex.minpoly_of_element().as_expr(x)
846
+ elif ex.is_Rational:
847
+ result = ex.q*x - ex.p
848
+ else:
849
+ inverted = simpler_inverse(ex)
850
+ if inverted:
851
+ ex = ex**-1
852
+ res = None
853
+ if ex.is_Pow and (1/ex.exp).is_Integer:
854
+ n = 1/ex.exp
855
+ res = _minimal_polynomial_sq(ex.base, n, x)
856
+
857
+ elif _is_sum_surds(ex):
858
+ res = _minimal_polynomial_sq(ex, S.One, x)
859
+
860
+ if res is not None:
861
+ result = res
862
+
863
+ if res is None:
864
+ bus = bottom_up_scan(ex)
865
+ F = [x - bus] + list(mapping.values())
866
+ G = groebner(F, list(symbols.values()) + [x], order='lex')
867
+
868
+ _, factors = factor_list(G[-1])
869
+ # by construction G[-1] has root `ex`
870
+ result = _choose_factor(factors, x, ex)
871
+ if inverted:
872
+ result = _invertx(result, x)
873
+ if result.coeff(x**degree(result, x)) < 0:
874
+ result = expand_mul(-result)
875
+
876
+ return result
877
+
878
+
879
+ @public
880
+ def minpoly(ex, x=None, compose=True, polys=False, domain=None):
881
+ """This is a synonym for :py:func:`~.minimal_polynomial`."""
882
+ return minimal_polynomial(ex, x=x, compose=compose, polys=polys, domain=domain)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/modules.py ADDED
@@ -0,0 +1,2114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ r"""Modules in number fields.
2
+
3
+ The classes defined here allow us to work with finitely generated, free
4
+ modules, whose generators are algebraic numbers.
5
+
6
+ There is an abstract base class called :py:class:`~.Module`, which has two
7
+ concrete subclasses, :py:class:`~.PowerBasis` and :py:class:`~.Submodule`.
8
+
9
+ Every module is defined by its basis, or set of generators:
10
+
11
+ * For a :py:class:`~.PowerBasis`, the generators are the first $n$ powers
12
+ (starting with the zeroth) of an algebraic integer $\theta$ of degree $n$.
13
+ The :py:class:`~.PowerBasis` is constructed by passing either the minimal
14
+ polynomial of $\theta$, or an :py:class:`~.AlgebraicField` having $\theta$
15
+ as its primitive element.
16
+
17
+ * For a :py:class:`~.Submodule`, the generators are a set of
18
+ $\mathbb{Q}$-linear combinations of the generators of another module. That
19
+ other module is then the "parent" of the :py:class:`~.Submodule`. The
20
+ coefficients of the $\mathbb{Q}$-linear combinations may be given by an
21
+ integer matrix, and a positive integer denominator. Each column of the matrix
22
+ defines a generator.
23
+
24
+ >>> from sympy.polys import Poly, cyclotomic_poly, ZZ
25
+ >>> from sympy.abc import x
26
+ >>> from sympy.polys.matrices import DomainMatrix, DM
27
+ >>> from sympy.polys.numberfields.modules import PowerBasis
28
+ >>> T = Poly(cyclotomic_poly(5, x))
29
+ >>> A = PowerBasis(T)
30
+ >>> print(A)
31
+ PowerBasis(x**4 + x**3 + x**2 + x + 1)
32
+ >>> B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ), denom=3)
33
+ >>> print(B)
34
+ Submodule[[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [0, 0, 0, 2]]/3
35
+ >>> print(B.parent)
36
+ PowerBasis(x**4 + x**3 + x**2 + x + 1)
37
+
38
+ Thus, every module is either a :py:class:`~.PowerBasis`,
39
+ or a :py:class:`~.Submodule`, some ancestor of which is a
40
+ :py:class:`~.PowerBasis`. (If ``S`` is a :py:class:`~.Submodule`, then its
41
+ ancestors are ``S.parent``, ``S.parent.parent``, and so on).
42
+
43
+ The :py:class:`~.ModuleElement` class represents a linear combination of the
44
+ generators of any module. Critically, the coefficients of this linear
45
+ combination are not restricted to be integers, but may be any rational
46
+ numbers. This is necessary so that any and all algebraic integers be
47
+ representable, starting from the power basis in a primitive element $\theta$
48
+ for the number field in question. For example, in a quadratic field
49
+ $\mathbb{Q}(\sqrt{d})$ where $d \equiv 1 \mod{4}$, a denominator of $2$ is
50
+ needed.
51
+
52
+ A :py:class:`~.ModuleElement` can be constructed from an integer column vector
53
+ and a denominator:
54
+
55
+ >>> U = Poly(x**2 - 5)
56
+ >>> M = PowerBasis(U)
57
+ >>> e = M(DM([[1], [1]], ZZ), denom=2)
58
+ >>> print(e)
59
+ [1, 1]/2
60
+ >>> print(e.module)
61
+ PowerBasis(x**2 - 5)
62
+
63
+ The :py:class:`~.PowerBasisElement` class is a subclass of
64
+ :py:class:`~.ModuleElement` that represents elements of a
65
+ :py:class:`~.PowerBasis`, and adds functionality pertinent to elements
66
+ represented directly over powers of the primitive element $\theta$.
67
+
68
+
69
+ Arithmetic with module elements
70
+ ===============================
71
+
72
+ While a :py:class:`~.ModuleElement` represents a linear combination over the
73
+ generators of a particular module, recall that every module is either a
74
+ :py:class:`~.PowerBasis` or a descendant (along a chain of
75
+ :py:class:`~.Submodule` objects) thereof, so that in fact every
76
+ :py:class:`~.ModuleElement` represents an algebraic number in some field
77
+ $\mathbb{Q}(\theta)$, where $\theta$ is the defining element of some
78
+ :py:class:`~.PowerBasis`. It thus makes sense to talk about the number field
79
+ to which a given :py:class:`~.ModuleElement` belongs.
80
+
81
+ This means that any two :py:class:`~.ModuleElement` instances can be added,
82
+ subtracted, multiplied, or divided, provided they belong to the same number
83
+ field. Similarly, since $\mathbb{Q}$ is a subfield of every number field,
84
+ any :py:class:`~.ModuleElement` may be added, multiplied, etc. by any
85
+ rational number.
86
+
87
+ >>> from sympy import QQ
88
+ >>> from sympy.polys.numberfields.modules import to_col
89
+ >>> T = Poly(cyclotomic_poly(5))
90
+ >>> A = PowerBasis(T)
91
+ >>> C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
92
+ >>> e = A(to_col([0, 2, 0, 0]), denom=3)
93
+ >>> f = A(to_col([0, 0, 0, 7]), denom=5)
94
+ >>> g = C(to_col([1, 1, 1, 1]))
95
+ >>> e + f
96
+ [0, 10, 0, 21]/15
97
+ >>> e - f
98
+ [0, 10, 0, -21]/15
99
+ >>> e - g
100
+ [-9, -7, -9, -9]/3
101
+ >>> e + QQ(7, 10)
102
+ [21, 20, 0, 0]/30
103
+ >>> e * f
104
+ [-14, -14, -14, -14]/15
105
+ >>> e ** 2
106
+ [0, 0, 4, 0]/9
107
+ >>> f // g
108
+ [7, 7, 7, 7]/15
109
+ >>> f * QQ(2, 3)
110
+ [0, 0, 0, 14]/15
111
+
112
+ However, care must be taken with arithmetic operations on
113
+ :py:class:`~.ModuleElement`, because the module $C$ to which the result will
114
+ belong will be the nearest common ancestor (NCA) of the modules $A$, $B$ to
115
+ which the two operands belong, and $C$ may be different from either or both
116
+ of $A$ and $B$.
117
+
118
+ >>> A = PowerBasis(T)
119
+ >>> B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
120
+ >>> C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
121
+ >>> print((B(0) * C(0)).module == A)
122
+ True
123
+
124
+ Before the arithmetic operation is performed, copies of the two operands are
125
+ automatically converted into elements of the NCA (the operands themselves are
126
+ not modified). This upward conversion along an ancestor chain is easy: it just
127
+ requires the successive multiplication by the defining matrix of each
128
+ :py:class:`~.Submodule`.
129
+
130
+ Conversely, downward conversion, i.e. representing a given
131
+ :py:class:`~.ModuleElement` in a submodule, is also supported -- namely by
132
+ the :py:meth:`~sympy.polys.numberfields.modules.Submodule.represent` method
133
+ -- but is not guaranteed to succeed in general, since the given element may
134
+ not belong to the submodule. The main circumstance in which this issue tends
135
+ to arise is with multiplication, since modules, while closed under addition,
136
+ need not be closed under multiplication.
137
+
138
+
139
+ Multiplication
140
+ --------------
141
+
142
+ Generally speaking, a module need not be closed under multiplication, i.e. need
143
+ not form a ring. However, many of the modules we work with in the context of
144
+ number fields are in fact rings, and our classes do support multiplication.
145
+
146
+ Specifically, any :py:class:`~.Module` can attempt to compute its own
147
+ multiplication table, but this does not happen unless an attempt is made to
148
+ multiply two :py:class:`~.ModuleElement` instances belonging to it.
149
+
150
+ >>> A = PowerBasis(T)
151
+ >>> print(A._mult_tab is None)
152
+ True
153
+ >>> a = A(0)*A(1)
154
+ >>> print(A._mult_tab is None)
155
+ False
156
+
157
+ Every :py:class:`~.PowerBasis` is, by its nature, closed under multiplication,
158
+ so instances of :py:class:`~.PowerBasis` can always successfully compute their
159
+ multiplication table.
160
+
161
+ When a :py:class:`~.Submodule` attempts to compute its multiplication table,
162
+ it converts each of its own generators into elements of its parent module,
163
+ multiplies them there, in every possible pairing, and then tries to
164
+ represent the results in itself, i.e. as $\mathbb{Z}$-linear combinations
165
+ over its own generators. This will succeed if and only if the submodule is
166
+ in fact closed under multiplication.
167
+
168
+
169
+ Module Homomorphisms
170
+ ====================
171
+
172
+ Many important number theoretic algorithms require the calculation of the
173
+ kernel of one or more module homomorphisms. Accordingly we have several
174
+ lightweight classes, :py:class:`~.ModuleHomomorphism`,
175
+ :py:class:`~.ModuleEndomorphism`, :py:class:`~.InnerEndomorphism`, and
176
+ :py:class:`~.EndomorphismRing`, which provide the minimal necessary machinery
177
+ to support this.
178
+
179
+ """
180
+
181
+ from sympy.core.intfunc import igcd, ilcm
182
+ from sympy.core.symbol import Dummy
183
+ from sympy.polys.polyclasses import ANP
184
+ from sympy.polys.polytools import Poly
185
+ from sympy.polys.densetools import dup_clear_denoms
186
+ from sympy.polys.domains.algebraicfield import AlgebraicField
187
+ from sympy.polys.domains.finitefield import FF
188
+ from sympy.polys.domains.rationalfield import QQ
189
+ from sympy.polys.domains.integerring import ZZ
190
+ from sympy.polys.matrices.domainmatrix import DomainMatrix
191
+ from sympy.polys.matrices.exceptions import DMBadInputError
192
+ from sympy.polys.matrices.normalforms import hermite_normal_form
193
+ from sympy.polys.polyerrors import CoercionFailed, UnificationFailed
194
+ from sympy.polys.polyutils import IntegerPowerable
195
+ from .exceptions import ClosureFailure, MissingUnityError, StructureError
196
+ from .utilities import AlgIntPowers, is_rat, get_num_denom
197
+
198
+
199
+ def to_col(coeffs):
200
+ r"""Transform a list of integer coefficients into a column vector."""
201
+ return DomainMatrix([[ZZ(c) for c in coeffs]], (1, len(coeffs)), ZZ).transpose()
202
+
203
+
204
+ class Module:
205
+ """
206
+ Generic finitely-generated module.
207
+
208
+ This is an abstract base class, and should not be instantiated directly.
209
+ The two concrete subclasses are :py:class:`~.PowerBasis` and
210
+ :py:class:`~.Submodule`.
211
+
212
+ Every :py:class:`~.Submodule` is derived from another module, referenced
213
+ by its ``parent`` attribute. If ``S`` is a submodule, then we refer to
214
+ ``S.parent``, ``S.parent.parent``, and so on, as the "ancestors" of
215
+ ``S``. Thus, every :py:class:`~.Module` is either a
216
+ :py:class:`~.PowerBasis` or a :py:class:`~.Submodule`, some ancestor of
217
+ which is a :py:class:`~.PowerBasis`.
218
+ """
219
+
220
+ @property
221
+ def n(self):
222
+ """The number of generators of this module."""
223
+ raise NotImplementedError
224
+
225
+ def mult_tab(self):
226
+ """
227
+ Get the multiplication table for this module (if closed under mult).
228
+
229
+ Explanation
230
+ ===========
231
+
232
+ Computes a dictionary ``M`` of dictionaries of lists, representing the
233
+ upper triangular half of the multiplication table.
234
+
235
+ In other words, if ``0 <= i <= j < self.n``, then ``M[i][j]`` is the
236
+ list ``c`` of coefficients such that
237
+ ``g[i] * g[j] == sum(c[k]*g[k], k in range(self.n))``,
238
+ where ``g`` is the list of generators of this module.
239
+
240
+ If ``j < i`` then ``M[i][j]`` is undefined.
241
+
242
+ Examples
243
+ ========
244
+
245
+ >>> from sympy.polys import Poly, cyclotomic_poly
246
+ >>> from sympy.polys.numberfields.modules import PowerBasis
247
+ >>> T = Poly(cyclotomic_poly(5))
248
+ >>> A = PowerBasis(T)
249
+ >>> print(A.mult_tab()) # doctest: +SKIP
250
+ {0: {0: [1, 0, 0, 0], 1: [0, 1, 0, 0], 2: [0, 0, 1, 0], 3: [0, 0, 0, 1]},
251
+ 1: {1: [0, 0, 1, 0], 2: [0, 0, 0, 1], 3: [-1, -1, -1, -1]},
252
+ 2: {2: [-1, -1, -1, -1], 3: [1, 0, 0, 0]},
253
+ 3: {3: [0, 1, 0, 0]}}
254
+
255
+ Returns
256
+ =======
257
+
258
+ dict of dict of lists
259
+
260
+ Raises
261
+ ======
262
+
263
+ ClosureFailure
264
+ If the module is not closed under multiplication.
265
+
266
+ """
267
+ raise NotImplementedError
268
+
269
+ @property
270
+ def parent(self):
271
+ """
272
+ The parent module, if any, for this module.
273
+
274
+ Explanation
275
+ ===========
276
+
277
+ For a :py:class:`~.Submodule` this is its ``parent`` attribute; for a
278
+ :py:class:`~.PowerBasis` this is ``None``.
279
+
280
+ Returns
281
+ =======
282
+
283
+ :py:class:`~.Module`, ``None``
284
+
285
+ See Also
286
+ ========
287
+
288
+ Module
289
+
290
+ """
291
+ return None
292
+
293
+ def represent(self, elt):
294
+ r"""
295
+ Represent a module element as an integer-linear combination over the
296
+ generators of this module.
297
+
298
+ Explanation
299
+ ===========
300
+
301
+ In our system, to "represent" always means to write a
302
+ :py:class:`~.ModuleElement` as a :ref:`ZZ`-linear combination over the
303
+ generators of the present :py:class:`~.Module`. Furthermore, the
304
+ incoming :py:class:`~.ModuleElement` must belong to an ancestor of
305
+ the present :py:class:`~.Module` (or to the present
306
+ :py:class:`~.Module` itself).
307
+
308
+ The most common application is to represent a
309
+ :py:class:`~.ModuleElement` in a :py:class:`~.Submodule`. For example,
310
+ this is involved in computing multiplication tables.
311
+
312
+ On the other hand, representing in a :py:class:`~.PowerBasis` is an
313
+ odd case, and one which tends not to arise in practice, except for
314
+ example when using a :py:class:`~.ModuleEndomorphism` on a
315
+ :py:class:`~.PowerBasis`.
316
+
317
+ In such a case, (1) the incoming :py:class:`~.ModuleElement` must
318
+ belong to the :py:class:`~.PowerBasis` itself (since the latter has no
319
+ proper ancestors) and (2) it is "representable" iff it belongs to
320
+ $\mathbb{Z}[\theta]$ (although generally a
321
+ :py:class:`~.PowerBasisElement` may represent any element of
322
+ $\mathbb{Q}(\theta)$, i.e. any algebraic number).
323
+
324
+ Examples
325
+ ========
326
+
327
+ >>> from sympy import Poly, cyclotomic_poly
328
+ >>> from sympy.polys.numberfields.modules import PowerBasis, to_col
329
+ >>> from sympy.abc import zeta
330
+ >>> T = Poly(cyclotomic_poly(5))
331
+ >>> A = PowerBasis(T)
332
+ >>> a = A(to_col([2, 4, 6, 8]))
333
+
334
+ The :py:class:`~.ModuleElement` ``a`` has all even coefficients.
335
+ If we represent ``a`` in the submodule ``B = 2*A``, the coefficients in
336
+ the column vector will be halved:
337
+
338
+ >>> B = A.submodule_from_gens([2*A(i) for i in range(4)])
339
+ >>> b = B.represent(a)
340
+ >>> print(b.transpose()) # doctest: +SKIP
341
+ DomainMatrix([[1, 2, 3, 4]], (1, 4), ZZ)
342
+
343
+ However, the element of ``B`` so defined still represents the same
344
+ algebraic number:
345
+
346
+ >>> print(a.poly(zeta).as_expr())
347
+ 8*zeta**3 + 6*zeta**2 + 4*zeta + 2
348
+ >>> print(B(b).over_power_basis().poly(zeta).as_expr())
349
+ 8*zeta**3 + 6*zeta**2 + 4*zeta + 2
350
+
351
+ Parameters
352
+ ==========
353
+
354
+ elt : :py:class:`~.ModuleElement`
355
+ The module element to be represented. Must belong to some ancestor
356
+ module of this module (including this module itself).
357
+
358
+ Returns
359
+ =======
360
+
361
+ :py:class:`~.DomainMatrix` over :ref:`ZZ`
362
+ This will be a column vector, representing the coefficients of a
363
+ linear combination of this module's generators, which equals the
364
+ given element.
365
+
366
+ Raises
367
+ ======
368
+
369
+ ClosureFailure
370
+ If the given element cannot be represented as a :ref:`ZZ`-linear
371
+ combination over this module.
372
+
373
+ See Also
374
+ ========
375
+
376
+ .Submodule.represent
377
+ .PowerBasis.represent
378
+
379
+ """
380
+ raise NotImplementedError
381
+
382
+ def ancestors(self, include_self=False):
383
+ """
384
+ Return the list of ancestor modules of this module, from the
385
+ foundational :py:class:`~.PowerBasis` downward, optionally including
386
+ ``self``.
387
+
388
+ See Also
389
+ ========
390
+
391
+ Module
392
+
393
+ """
394
+ c = self.parent
395
+ a = [] if c is None else c.ancestors(include_self=True)
396
+ if include_self:
397
+ a.append(self)
398
+ return a
399
+
400
+ def power_basis_ancestor(self):
401
+ """
402
+ Return the :py:class:`~.PowerBasis` that is an ancestor of this module.
403
+
404
+ See Also
405
+ ========
406
+
407
+ Module
408
+
409
+ """
410
+ if isinstance(self, PowerBasis):
411
+ return self
412
+ c = self.parent
413
+ if c is not None:
414
+ return c.power_basis_ancestor()
415
+ return None
416
+
417
+ def nearest_common_ancestor(self, other):
418
+ """
419
+ Locate the nearest common ancestor of this module and another.
420
+
421
+ Returns
422
+ =======
423
+
424
+ :py:class:`~.Module`, ``None``
425
+
426
+ See Also
427
+ ========
428
+
429
+ Module
430
+
431
+ """
432
+ sA = self.ancestors(include_self=True)
433
+ oA = other.ancestors(include_self=True)
434
+ nca = None
435
+ for sa, oa in zip(sA, oA):
436
+ if sa == oa:
437
+ nca = sa
438
+ else:
439
+ break
440
+ return nca
441
+
442
+ @property
443
+ def number_field(self):
444
+ r"""
445
+ Return the associated :py:class:`~.AlgebraicField`, if any.
446
+
447
+ Explanation
448
+ ===========
449
+
450
+ A :py:class:`~.PowerBasis` can be constructed on a :py:class:`~.Poly`
451
+ $f$ or on an :py:class:`~.AlgebraicField` $K$. In the latter case, the
452
+ :py:class:`~.PowerBasis` and all its descendant modules will return $K$
453
+ as their ``.number_field`` property, while in the former case they will
454
+ all return ``None``.
455
+
456
+ Returns
457
+ =======
458
+
459
+ :py:class:`~.AlgebraicField`, ``None``
460
+
461
+ """
462
+ return self.power_basis_ancestor().number_field
463
+
464
+ def is_compat_col(self, col):
465
+ """Say whether *col* is a suitable column vector for this module."""
466
+ return isinstance(col, DomainMatrix) and col.shape == (self.n, 1) and col.domain.is_ZZ
467
+
468
+ def __call__(self, spec, denom=1):
469
+ r"""
470
+ Generate a :py:class:`~.ModuleElement` belonging to this module.
471
+
472
+ Examples
473
+ ========
474
+
475
+ >>> from sympy.polys import Poly, cyclotomic_poly
476
+ >>> from sympy.polys.numberfields.modules import PowerBasis, to_col
477
+ >>> T = Poly(cyclotomic_poly(5))
478
+ >>> A = PowerBasis(T)
479
+ >>> e = A(to_col([1, 2, 3, 4]), denom=3)
480
+ >>> print(e) # doctest: +SKIP
481
+ [1, 2, 3, 4]/3
482
+ >>> f = A(2)
483
+ >>> print(f) # doctest: +SKIP
484
+ [0, 0, 1, 0]
485
+
486
+ Parameters
487
+ ==========
488
+
489
+ spec : :py:class:`~.DomainMatrix`, int
490
+ Specifies the numerators of the coefficients of the
491
+ :py:class:`~.ModuleElement`. Can be either a column vector over
492
+ :ref:`ZZ`, whose length must equal the number $n$ of generators of
493
+ this module, or else an integer ``j``, $0 \leq j < n$, which is a
494
+ shorthand for column $j$ of $I_n$, the $n \times n$ identity
495
+ matrix.
496
+ denom : int, optional (default=1)
497
+ Denominator for the coefficients of the
498
+ :py:class:`~.ModuleElement`.
499
+
500
+ Returns
501
+ =======
502
+
503
+ :py:class:`~.ModuleElement`
504
+ The coefficients are the entries of the *spec* vector, divided by
505
+ *denom*.
506
+
507
+ """
508
+ if isinstance(spec, int) and 0 <= spec < self.n:
509
+ spec = DomainMatrix.eye(self.n, ZZ)[:, spec].to_dense()
510
+ if not self.is_compat_col(spec):
511
+ raise ValueError('Compatible column vector required.')
512
+ return make_mod_elt(self, spec, denom=denom)
513
+
514
+ def starts_with_unity(self):
515
+ """Say whether the module's first generator equals unity."""
516
+ raise NotImplementedError
517
+
518
+ def basis_elements(self):
519
+ """
520
+ Get list of :py:class:`~.ModuleElement` being the generators of this
521
+ module.
522
+ """
523
+ return [self(j) for j in range(self.n)]
524
+
525
+ def zero(self):
526
+ """Return a :py:class:`~.ModuleElement` representing zero."""
527
+ return self(0) * 0
528
+
529
+ def one(self):
530
+ """
531
+ Return a :py:class:`~.ModuleElement` representing unity,
532
+ and belonging to the first ancestor of this module (including
533
+ itself) that starts with unity.
534
+ """
535
+ return self.element_from_rational(1)
536
+
537
+ def element_from_rational(self, a):
538
+ """
539
+ Return a :py:class:`~.ModuleElement` representing a rational number.
540
+
541
+ Explanation
542
+ ===========
543
+
544
+ The returned :py:class:`~.ModuleElement` will belong to the first
545
+ module on this module's ancestor chain (including this module
546
+ itself) that starts with unity.
547
+
548
+ Examples
549
+ ========
550
+
551
+ >>> from sympy.polys import Poly, cyclotomic_poly, QQ
552
+ >>> from sympy.polys.numberfields.modules import PowerBasis
553
+ >>> T = Poly(cyclotomic_poly(5))
554
+ >>> A = PowerBasis(T)
555
+ >>> a = A.element_from_rational(QQ(2, 3))
556
+ >>> print(a) # doctest: +SKIP
557
+ [2, 0, 0, 0]/3
558
+
559
+ Parameters
560
+ ==========
561
+
562
+ a : int, :ref:`ZZ`, :ref:`QQ`
563
+
564
+ Returns
565
+ =======
566
+
567
+ :py:class:`~.ModuleElement`
568
+
569
+ """
570
+ raise NotImplementedError
571
+
572
+ def submodule_from_gens(self, gens, hnf=True, hnf_modulus=None):
573
+ """
574
+ Form the submodule generated by a list of :py:class:`~.ModuleElement`
575
+ belonging to this module.
576
+
577
+ Examples
578
+ ========
579
+
580
+ >>> from sympy.polys import Poly, cyclotomic_poly
581
+ >>> from sympy.polys.numberfields.modules import PowerBasis
582
+ >>> T = Poly(cyclotomic_poly(5))
583
+ >>> A = PowerBasis(T)
584
+ >>> gens = [A(0), 2*A(1), 3*A(2), 4*A(3)//5]
585
+ >>> B = A.submodule_from_gens(gens)
586
+ >>> print(B) # doctest: +SKIP
587
+ Submodule[[5, 0, 0, 0], [0, 10, 0, 0], [0, 0, 15, 0], [0, 0, 0, 4]]/5
588
+
589
+ Parameters
590
+ ==========
591
+
592
+ gens : list of :py:class:`~.ModuleElement` belonging to this module.
593
+ hnf : boolean, optional (default=True)
594
+ If True, we will reduce the matrix into Hermite Normal Form before
595
+ forming the :py:class:`~.Submodule`.
596
+ hnf_modulus : int, None, optional (default=None)
597
+ Modulus for use in the HNF reduction algorithm. See
598
+ :py:func:`~sympy.polys.matrices.normalforms.hermite_normal_form`.
599
+
600
+ Returns
601
+ =======
602
+
603
+ :py:class:`~.Submodule`
604
+
605
+ See Also
606
+ ========
607
+
608
+ submodule_from_matrix
609
+
610
+ """
611
+ if not all(g.module == self for g in gens):
612
+ raise ValueError('Generators must belong to this module.')
613
+ n = len(gens)
614
+ if n == 0:
615
+ raise ValueError('Need at least one generator.')
616
+ m = gens[0].n
617
+ d = gens[0].denom if n == 1 else ilcm(*[g.denom for g in gens])
618
+ B = DomainMatrix.zeros((m, 0), ZZ).hstack(*[(d // g.denom) * g.col for g in gens])
619
+ if hnf:
620
+ B = hermite_normal_form(B, D=hnf_modulus)
621
+ return self.submodule_from_matrix(B, denom=d)
622
+
623
+ def submodule_from_matrix(self, B, denom=1):
624
+ """
625
+ Form the submodule generated by the elements of this module indicated
626
+ by the columns of a matrix, with an optional denominator.
627
+
628
+ Examples
629
+ ========
630
+
631
+ >>> from sympy.polys import Poly, cyclotomic_poly, ZZ
632
+ >>> from sympy.polys.matrices import DM
633
+ >>> from sympy.polys.numberfields.modules import PowerBasis
634
+ >>> T = Poly(cyclotomic_poly(5))
635
+ >>> A = PowerBasis(T)
636
+ >>> B = A.submodule_from_matrix(DM([
637
+ ... [0, 10, 0, 0],
638
+ ... [0, 0, 7, 0],
639
+ ... ], ZZ).transpose(), denom=15)
640
+ >>> print(B) # doctest: +SKIP
641
+ Submodule[[0, 10, 0, 0], [0, 0, 7, 0]]/15
642
+
643
+ Parameters
644
+ ==========
645
+
646
+ B : :py:class:`~.DomainMatrix` over :ref:`ZZ`
647
+ Each column gives the numerators of the coefficients of one
648
+ generator of the submodule. Thus, the number of rows of *B* must
649
+ equal the number of generators of the present module.
650
+ denom : int, optional (default=1)
651
+ Common denominator for all generators of the submodule.
652
+
653
+ Returns
654
+ =======
655
+
656
+ :py:class:`~.Submodule`
657
+
658
+ Raises
659
+ ======
660
+
661
+ ValueError
662
+ If the given matrix *B* is not over :ref:`ZZ` or its number of rows
663
+ does not equal the number of generators of the present module.
664
+
665
+ See Also
666
+ ========
667
+
668
+ submodule_from_gens
669
+
670
+ """
671
+ m, n = B.shape
672
+ if not B.domain.is_ZZ:
673
+ raise ValueError('Matrix must be over ZZ.')
674
+ if not m == self.n:
675
+ raise ValueError('Matrix row count must match base module.')
676
+ return Submodule(self, B, denom=denom)
677
+
678
+ def whole_submodule(self):
679
+ """
680
+ Return a submodule equal to this entire module.
681
+
682
+ Explanation
683
+ ===========
684
+
685
+ This is useful when you have a :py:class:`~.PowerBasis` and want to
686
+ turn it into a :py:class:`~.Submodule` (in order to use methods
687
+ belonging to the latter).
688
+
689
+ """
690
+ B = DomainMatrix.eye(self.n, ZZ)
691
+ return self.submodule_from_matrix(B)
692
+
693
+ def endomorphism_ring(self):
694
+ """Form the :py:class:`~.EndomorphismRing` for this module."""
695
+ return EndomorphismRing(self)
696
+
697
+
698
+ class PowerBasis(Module):
699
+ """The module generated by the powers of an algebraic integer."""
700
+
701
+ def __init__(self, T):
702
+ """
703
+ Parameters
704
+ ==========
705
+
706
+ T : :py:class:`~.Poly`, :py:class:`~.AlgebraicField`
707
+ Either (1) the monic, irreducible, univariate polynomial over
708
+ :ref:`ZZ`, a root of which is the generator of the power basis,
709
+ or (2) an :py:class:`~.AlgebraicField` whose primitive element
710
+ is the generator of the power basis.
711
+
712
+ """
713
+ K = None
714
+ if isinstance(T, AlgebraicField):
715
+ K, T = T, T.ext.minpoly_of_element()
716
+ # Sometimes incoming Polys are formally over QQ, although all their
717
+ # coeffs are integral. We want them to be formally over ZZ.
718
+ T = T.set_domain(ZZ)
719
+ self.K = K
720
+ self.T = T
721
+ self._n = T.degree()
722
+ self._mult_tab = None
723
+
724
+ @property
725
+ def number_field(self):
726
+ return self.K
727
+
728
+ def __repr__(self):
729
+ return f'PowerBasis({self.T.as_expr()})'
730
+
731
+ def __eq__(self, other):
732
+ if isinstance(other, PowerBasis):
733
+ return self.T == other.T
734
+ return NotImplemented
735
+
736
+ @property
737
+ def n(self):
738
+ return self._n
739
+
740
+ def mult_tab(self):
741
+ if self._mult_tab is None:
742
+ self.compute_mult_tab()
743
+ return self._mult_tab
744
+
745
+ def compute_mult_tab(self):
746
+ theta_pow = AlgIntPowers(self.T)
747
+ M = {}
748
+ n = self.n
749
+ for u in range(n):
750
+ M[u] = {}
751
+ for v in range(u, n):
752
+ M[u][v] = theta_pow[u + v]
753
+ self._mult_tab = M
754
+
755
+ def represent(self, elt):
756
+ r"""
757
+ Represent a module element as an integer-linear combination over the
758
+ generators of this module.
759
+
760
+ See Also
761
+ ========
762
+
763
+ .Module.represent
764
+ .Submodule.represent
765
+
766
+ """
767
+ if elt.module == self and elt.denom == 1:
768
+ return elt.column()
769
+ else:
770
+ raise ClosureFailure('Element not representable in ZZ[theta].')
771
+
772
+ def starts_with_unity(self):
773
+ return True
774
+
775
+ def element_from_rational(self, a):
776
+ return self(0) * a
777
+
778
+ def element_from_poly(self, f):
779
+ """
780
+ Produce an element of this module, representing *f* after reduction mod
781
+ our defining minimal polynomial.
782
+
783
+ Parameters
784
+ ==========
785
+
786
+ f : :py:class:`~.Poly` over :ref:`ZZ` in same var as our defining poly.
787
+
788
+ Returns
789
+ =======
790
+
791
+ :py:class:`~.PowerBasisElement`
792
+
793
+ """
794
+ n, k = self.n, f.degree()
795
+ if k >= n:
796
+ f = f % self.T
797
+ if f == 0:
798
+ return self.zero()
799
+ d, c = dup_clear_denoms(f.rep.to_list(), QQ, convert=True)
800
+ c = list(reversed(c))
801
+ ell = len(c)
802
+ z = [ZZ(0)] * (n - ell)
803
+ col = to_col(c + z)
804
+ return self(col, denom=d)
805
+
806
+ def _element_from_rep_and_mod(self, rep, mod):
807
+ """
808
+ Produce a PowerBasisElement representing a given algebraic number.
809
+
810
+ Parameters
811
+ ==========
812
+
813
+ rep : list of coeffs
814
+ Represents the number as polynomial in the primitive element of the
815
+ field.
816
+
817
+ mod : list of coeffs
818
+ Represents the minimal polynomial of the primitive element of the
819
+ field.
820
+
821
+ Returns
822
+ =======
823
+
824
+ :py:class:`~.PowerBasisElement`
825
+
826
+ """
827
+ if mod != self.T.rep.to_list():
828
+ raise UnificationFailed('Element does not appear to be in the same field.')
829
+ return self.element_from_poly(Poly(rep, self.T.gen))
830
+
831
+ def element_from_ANP(self, a):
832
+ """Convert an ANP into a PowerBasisElement. """
833
+ return self._element_from_rep_and_mod(a.to_list(), a.mod_to_list())
834
+
835
+ def element_from_alg_num(self, a):
836
+ """Convert an AlgebraicNumber into a PowerBasisElement. """
837
+ return self._element_from_rep_and_mod(a.rep.to_list(), a.minpoly.rep.to_list())
838
+
839
+
840
+ class Submodule(Module, IntegerPowerable):
841
+ """A submodule of another module."""
842
+
843
+ def __init__(self, parent, matrix, denom=1, mult_tab=None):
844
+ """
845
+ Parameters
846
+ ==========
847
+
848
+ parent : :py:class:`~.Module`
849
+ The module from which this one is derived.
850
+ matrix : :py:class:`~.DomainMatrix` over :ref:`ZZ`
851
+ The matrix whose columns define this submodule's generators as
852
+ linear combinations over the parent's generators.
853
+ denom : int, optional (default=1)
854
+ Denominator for the coefficients given by the matrix.
855
+ mult_tab : dict, ``None``, optional
856
+ If already known, the multiplication table for this module may be
857
+ supplied.
858
+
859
+ """
860
+ self._parent = parent
861
+ self._matrix = matrix
862
+ self._denom = denom
863
+ self._mult_tab = mult_tab
864
+ self._n = matrix.shape[1]
865
+ self._QQ_matrix = None
866
+ self._starts_with_unity = None
867
+ self._is_sq_maxrank_HNF = None
868
+
869
+ def __repr__(self):
870
+ r = 'Submodule' + repr(self.matrix.transpose().to_Matrix().tolist())
871
+ if self.denom > 1:
872
+ r += f'/{self.denom}'
873
+ return r
874
+
875
+ def reduced(self):
876
+ """
877
+ Produce a reduced version of this submodule.
878
+
879
+ Explanation
880
+ ===========
881
+
882
+ In the reduced version, it is guaranteed that 1 is the only positive
883
+ integer dividing both the submodule's denominator, and every entry in
884
+ the submodule's matrix.
885
+
886
+ Returns
887
+ =======
888
+
889
+ :py:class:`~.Submodule`
890
+
891
+ """
892
+ if self.denom == 1:
893
+ return self
894
+ g = igcd(self.denom, *self.coeffs)
895
+ if g == 1:
896
+ return self
897
+ return type(self)(self.parent, (self.matrix / g).convert_to(ZZ), denom=self.denom // g, mult_tab=self._mult_tab)
898
+
899
+ def discard_before(self, r):
900
+ """
901
+ Produce a new module by discarding all generators before a given
902
+ index *r*.
903
+ """
904
+ W = self.matrix[:, r:]
905
+ s = self.n - r
906
+ M = None
907
+ mt = self._mult_tab
908
+ if mt is not None:
909
+ M = {}
910
+ for u in range(s):
911
+ M[u] = {}
912
+ for v in range(u, s):
913
+ M[u][v] = mt[r + u][r + v][r:]
914
+ return Submodule(self.parent, W, denom=self.denom, mult_tab=M)
915
+
916
+ @property
917
+ def n(self):
918
+ return self._n
919
+
920
+ def mult_tab(self):
921
+ if self._mult_tab is None:
922
+ self.compute_mult_tab()
923
+ return self._mult_tab
924
+
925
+ def compute_mult_tab(self):
926
+ gens = self.basis_element_pullbacks()
927
+ M = {}
928
+ n = self.n
929
+ for u in range(n):
930
+ M[u] = {}
931
+ for v in range(u, n):
932
+ M[u][v] = self.represent(gens[u] * gens[v]).flat()
933
+ self._mult_tab = M
934
+
935
+ @property
936
+ def parent(self):
937
+ return self._parent
938
+
939
+ @property
940
+ def matrix(self):
941
+ return self._matrix
942
+
943
+ @property
944
+ def coeffs(self):
945
+ return self.matrix.flat()
946
+
947
+ @property
948
+ def denom(self):
949
+ return self._denom
950
+
951
+ @property
952
+ def QQ_matrix(self):
953
+ """
954
+ :py:class:`~.DomainMatrix` over :ref:`QQ`, equal to
955
+ ``self.matrix / self.denom``, and guaranteed to be dense.
956
+
957
+ Explanation
958
+ ===========
959
+
960
+ Depending on how it is formed, a :py:class:`~.DomainMatrix` may have
961
+ an internal representation that is sparse or dense. We guarantee a
962
+ dense representation here, so that tests for equivalence of submodules
963
+ always come out as expected.
964
+
965
+ Examples
966
+ ========
967
+
968
+ >>> from sympy.polys import Poly, cyclotomic_poly, ZZ
969
+ >>> from sympy.abc import x
970
+ >>> from sympy.polys.matrices import DomainMatrix
971
+ >>> from sympy.polys.numberfields.modules import PowerBasis
972
+ >>> T = Poly(cyclotomic_poly(5, x))
973
+ >>> A = PowerBasis(T)
974
+ >>> B = A.submodule_from_matrix(3*DomainMatrix.eye(4, ZZ), denom=6)
975
+ >>> C = A.submodule_from_matrix(DomainMatrix.eye(4, ZZ), denom=2)
976
+ >>> print(B.QQ_matrix == C.QQ_matrix)
977
+ True
978
+
979
+ Returns
980
+ =======
981
+
982
+ :py:class:`~.DomainMatrix` over :ref:`QQ`
983
+
984
+ """
985
+ if self._QQ_matrix is None:
986
+ self._QQ_matrix = (self.matrix / self.denom).to_dense()
987
+ return self._QQ_matrix
988
+
989
+ def starts_with_unity(self):
990
+ if self._starts_with_unity is None:
991
+ self._starts_with_unity = self(0).equiv(1)
992
+ return self._starts_with_unity
993
+
994
+ def is_sq_maxrank_HNF(self):
995
+ if self._is_sq_maxrank_HNF is None:
996
+ self._is_sq_maxrank_HNF = is_sq_maxrank_HNF(self._matrix)
997
+ return self._is_sq_maxrank_HNF
998
+
999
+ def is_power_basis_submodule(self):
1000
+ return isinstance(self.parent, PowerBasis)
1001
+
1002
+ def element_from_rational(self, a):
1003
+ if self.starts_with_unity():
1004
+ return self(0) * a
1005
+ else:
1006
+ return self.parent.element_from_rational(a)
1007
+
1008
+ def basis_element_pullbacks(self):
1009
+ """
1010
+ Return list of this submodule's basis elements as elements of the
1011
+ submodule's parent module.
1012
+ """
1013
+ return [e.to_parent() for e in self.basis_elements()]
1014
+
1015
+ def represent(self, elt):
1016
+ """
1017
+ Represent a module element as an integer-linear combination over the
1018
+ generators of this module.
1019
+
1020
+ See Also
1021
+ ========
1022
+
1023
+ .Module.represent
1024
+ .PowerBasis.represent
1025
+
1026
+ """
1027
+ if elt.module == self:
1028
+ return elt.column()
1029
+ elif elt.module == self.parent:
1030
+ try:
1031
+ # The given element should be a ZZ-linear combination over our
1032
+ # basis vectors; however, due to the presence of denominators,
1033
+ # we need to solve over QQ.
1034
+ A = self.QQ_matrix
1035
+ b = elt.QQ_col
1036
+ x = A._solve(b)[0].transpose()
1037
+ x = x.convert_to(ZZ)
1038
+ except DMBadInputError:
1039
+ raise ClosureFailure('Element outside QQ-span of this basis.')
1040
+ except CoercionFailed:
1041
+ raise ClosureFailure('Element in QQ-span but not ZZ-span of this basis.')
1042
+ return x
1043
+ elif isinstance(self.parent, Submodule):
1044
+ coeffs_in_parent = self.parent.represent(elt)
1045
+ parent_element = self.parent(coeffs_in_parent)
1046
+ return self.represent(parent_element)
1047
+ else:
1048
+ raise ClosureFailure('Element outside ancestor chain of this module.')
1049
+
1050
+ def is_compat_submodule(self, other):
1051
+ return isinstance(other, Submodule) and other.parent == self.parent
1052
+
1053
+ def __eq__(self, other):
1054
+ if self.is_compat_submodule(other):
1055
+ return other.QQ_matrix == self.QQ_matrix
1056
+ return NotImplemented
1057
+
1058
+ def add(self, other, hnf=True, hnf_modulus=None):
1059
+ """
1060
+ Add this :py:class:`~.Submodule` to another.
1061
+
1062
+ Explanation
1063
+ ===========
1064
+
1065
+ This represents the module generated by the union of the two modules'
1066
+ sets of generators.
1067
+
1068
+ Parameters
1069
+ ==========
1070
+
1071
+ other : :py:class:`~.Submodule`
1072
+ hnf : boolean, optional (default=True)
1073
+ If ``True``, reduce the matrix of the combined module to its
1074
+ Hermite Normal Form.
1075
+ hnf_modulus : :ref:`ZZ`, None, optional
1076
+ If a positive integer is provided, use this as modulus in the
1077
+ HNF reduction. See
1078
+ :py:func:`~sympy.polys.matrices.normalforms.hermite_normal_form`.
1079
+
1080
+ Returns
1081
+ =======
1082
+
1083
+ :py:class:`~.Submodule`
1084
+
1085
+ """
1086
+ d, e = self.denom, other.denom
1087
+ m = ilcm(d, e)
1088
+ a, b = m // d, m // e
1089
+ B = (a * self.matrix).hstack(b * other.matrix)
1090
+ if hnf:
1091
+ B = hermite_normal_form(B, D=hnf_modulus)
1092
+ return self.parent.submodule_from_matrix(B, denom=m)
1093
+
1094
+ def __add__(self, other):
1095
+ if self.is_compat_submodule(other):
1096
+ return self.add(other)
1097
+ return NotImplemented
1098
+
1099
+ __radd__ = __add__
1100
+
1101
+ def mul(self, other, hnf=True, hnf_modulus=None):
1102
+ """
1103
+ Multiply this :py:class:`~.Submodule` by a rational number, a
1104
+ :py:class:`~.ModuleElement`, or another :py:class:`~.Submodule`.
1105
+
1106
+ Explanation
1107
+ ===========
1108
+
1109
+ To multiply by a rational number or :py:class:`~.ModuleElement` means
1110
+ to form the submodule whose generators are the products of this
1111
+ quantity with all the generators of the present submodule.
1112
+
1113
+ To multiply by another :py:class:`~.Submodule` means to form the
1114
+ submodule whose generators are all the products of one generator from
1115
+ the one submodule, and one generator from the other.
1116
+
1117
+ Parameters
1118
+ ==========
1119
+
1120
+ other : int, :ref:`ZZ`, :ref:`QQ`, :py:class:`~.ModuleElement`, :py:class:`~.Submodule`
1121
+ hnf : boolean, optional (default=True)
1122
+ If ``True``, reduce the matrix of the product module to its
1123
+ Hermite Normal Form.
1124
+ hnf_modulus : :ref:`ZZ`, None, optional
1125
+ If a positive integer is provided, use this as modulus in the
1126
+ HNF reduction. See
1127
+ :py:func:`~sympy.polys.matrices.normalforms.hermite_normal_form`.
1128
+
1129
+ Returns
1130
+ =======
1131
+
1132
+ :py:class:`~.Submodule`
1133
+
1134
+ """
1135
+ if is_rat(other):
1136
+ a, b = get_num_denom(other)
1137
+ if a == b == 1:
1138
+ return self
1139
+ else:
1140
+ return Submodule(self.parent,
1141
+ self.matrix * a, denom=self.denom * b,
1142
+ mult_tab=None).reduced()
1143
+ elif isinstance(other, ModuleElement) and other.module == self.parent:
1144
+ # The submodule is multiplied by an element of the parent module.
1145
+ # We presume this means we want a new submodule of the parent module.
1146
+ gens = [other * e for e in self.basis_element_pullbacks()]
1147
+ return self.parent.submodule_from_gens(gens, hnf=hnf, hnf_modulus=hnf_modulus)
1148
+ elif self.is_compat_submodule(other):
1149
+ # This case usually means you're multiplying ideals, and want another
1150
+ # ideal, i.e. another submodule of the same parent module.
1151
+ alphas, betas = self.basis_element_pullbacks(), other.basis_element_pullbacks()
1152
+ gens = [a * b for a in alphas for b in betas]
1153
+ return self.parent.submodule_from_gens(gens, hnf=hnf, hnf_modulus=hnf_modulus)
1154
+ return NotImplemented
1155
+
1156
+ def __mul__(self, other):
1157
+ return self.mul(other)
1158
+
1159
+ __rmul__ = __mul__
1160
+
1161
+ def _first_power(self):
1162
+ return self
1163
+
1164
+ def reduce_element(self, elt):
1165
+ r"""
1166
+ If this submodule $B$ has defining matrix $W$ in square, maximal-rank
1167
+ Hermite normal form, then, given an element $x$ of the parent module
1168
+ $A$, we produce an element $y \in A$ such that $x - y \in B$, and the
1169
+ $i$th coordinate of $y$ satisfies $0 \leq y_i < w_{i,i}$. This
1170
+ representative $y$ is unique, in the sense that every element of
1171
+ the coset $x + B$ reduces to it under this procedure.
1172
+
1173
+ Explanation
1174
+ ===========
1175
+
1176
+ In the special case where $A$ is a power basis for a number field $K$,
1177
+ and $B$ is a submodule representing an ideal $I$, this operation
1178
+ represents one of a few important ways of reducing an element of $K$
1179
+ modulo $I$ to obtain a "small" representative. See [Cohen00]_ Section
1180
+ 1.4.3.
1181
+
1182
+ Examples
1183
+ ========
1184
+
1185
+ >>> from sympy import QQ, Poly, symbols
1186
+ >>> t = symbols('t')
1187
+ >>> k = QQ.alg_field_from_poly(Poly(t**3 + t**2 - 2*t + 8))
1188
+ >>> Zk = k.maximal_order()
1189
+ >>> A = Zk.parent
1190
+ >>> B = (A(2) - 3*A(0))*Zk
1191
+ >>> B.reduce_element(A(2))
1192
+ [3, 0, 0]
1193
+
1194
+ Parameters
1195
+ ==========
1196
+
1197
+ elt : :py:class:`~.ModuleElement`
1198
+ An element of this submodule's parent module.
1199
+
1200
+ Returns
1201
+ =======
1202
+
1203
+ elt : :py:class:`~.ModuleElement`
1204
+ An element of this submodule's parent module.
1205
+
1206
+ Raises
1207
+ ======
1208
+
1209
+ NotImplementedError
1210
+ If the given :py:class:`~.ModuleElement` does not belong to this
1211
+ submodule's parent module.
1212
+ StructureError
1213
+ If this submodule's defining matrix is not in square, maximal-rank
1214
+ Hermite normal form.
1215
+
1216
+ References
1217
+ ==========
1218
+
1219
+ .. [Cohen00] Cohen, H. *Advanced Topics in Computational Number
1220
+ Theory.*
1221
+
1222
+ """
1223
+ if not elt.module == self.parent:
1224
+ raise NotImplementedError
1225
+ if not self.is_sq_maxrank_HNF():
1226
+ msg = "Reduction not implemented unless matrix square max-rank HNF"
1227
+ raise StructureError(msg)
1228
+ B = self.basis_element_pullbacks()
1229
+ a = elt
1230
+ for i in range(self.n - 1, -1, -1):
1231
+ b = B[i]
1232
+ q = a.coeffs[i]*b.denom // (b.coeffs[i]*a.denom)
1233
+ a -= q*b
1234
+ return a
1235
+
1236
+
1237
+ def is_sq_maxrank_HNF(dm):
1238
+ r"""
1239
+ Say whether a :py:class:`~.DomainMatrix` is in that special case of Hermite
1240
+ Normal Form, in which the matrix is also square and of maximal rank.
1241
+
1242
+ Explanation
1243
+ ===========
1244
+
1245
+ We commonly work with :py:class:`~.Submodule` instances whose matrix is in
1246
+ this form, and it can be useful to be able to check that this condition is
1247
+ satisfied.
1248
+
1249
+ For example this is the case with the :py:class:`~.Submodule` ``ZK``
1250
+ returned by :py:func:`~sympy.polys.numberfields.basis.round_two`, which
1251
+ represents the maximal order in a number field, and with ideals formed
1252
+ therefrom, such as ``2 * ZK``.
1253
+
1254
+ """
1255
+ if dm.domain.is_ZZ and dm.is_square and dm.is_upper:
1256
+ n = dm.shape[0]
1257
+ for i in range(n):
1258
+ d = dm[i, i].element
1259
+ if d <= 0:
1260
+ return False
1261
+ for j in range(i + 1, n):
1262
+ if not (0 <= dm[i, j].element < d):
1263
+ return False
1264
+ return True
1265
+ return False
1266
+
1267
+
1268
+ def make_mod_elt(module, col, denom=1):
1269
+ r"""
1270
+ Factory function which builds a :py:class:`~.ModuleElement`, but ensures
1271
+ that it is a :py:class:`~.PowerBasisElement` if the module is a
1272
+ :py:class:`~.PowerBasis`.
1273
+ """
1274
+ if isinstance(module, PowerBasis):
1275
+ return PowerBasisElement(module, col, denom=denom)
1276
+ else:
1277
+ return ModuleElement(module, col, denom=denom)
1278
+
1279
+
1280
+ class ModuleElement(IntegerPowerable):
1281
+ r"""
1282
+ Represents an element of a :py:class:`~.Module`.
1283
+
1284
+ NOTE: Should not be constructed directly. Use the
1285
+ :py:meth:`~.Module.__call__` method or the :py:func:`make_mod_elt()`
1286
+ factory function instead.
1287
+ """
1288
+
1289
+ def __init__(self, module, col, denom=1):
1290
+ """
1291
+ Parameters
1292
+ ==========
1293
+
1294
+ module : :py:class:`~.Module`
1295
+ The module to which this element belongs.
1296
+ col : :py:class:`~.DomainMatrix` over :ref:`ZZ`
1297
+ Column vector giving the numerators of the coefficients of this
1298
+ element.
1299
+ denom : int, optional (default=1)
1300
+ Denominator for the coefficients of this element.
1301
+
1302
+ """
1303
+ self.module = module
1304
+ self.col = col
1305
+ self.denom = denom
1306
+ self._QQ_col = None
1307
+
1308
+ def __repr__(self):
1309
+ r = str([int(c) for c in self.col.flat()])
1310
+ if self.denom > 1:
1311
+ r += f'/{self.denom}'
1312
+ return r
1313
+
1314
+ def reduced(self):
1315
+ """
1316
+ Produce a reduced version of this ModuleElement, i.e. one in which the
1317
+ gcd of the denominator together with all numerator coefficients is 1.
1318
+ """
1319
+ if self.denom == 1:
1320
+ return self
1321
+ g = igcd(self.denom, *self.coeffs)
1322
+ if g == 1:
1323
+ return self
1324
+ return type(self)(self.module,
1325
+ (self.col / g).convert_to(ZZ),
1326
+ denom=self.denom // g)
1327
+
1328
+ def reduced_mod_p(self, p):
1329
+ """
1330
+ Produce a version of this :py:class:`~.ModuleElement` in which all
1331
+ numerator coefficients have been reduced mod *p*.
1332
+ """
1333
+ return make_mod_elt(self.module,
1334
+ self.col.convert_to(FF(p)).convert_to(ZZ),
1335
+ denom=self.denom)
1336
+
1337
+ @classmethod
1338
+ def from_int_list(cls, module, coeffs, denom=1):
1339
+ """
1340
+ Make a :py:class:`~.ModuleElement` from a list of ints (instead of a
1341
+ column vector).
1342
+ """
1343
+ col = to_col(coeffs)
1344
+ return cls(module, col, denom=denom)
1345
+
1346
+ @property
1347
+ def n(self):
1348
+ """The length of this element's column."""
1349
+ return self.module.n
1350
+
1351
+ def __len__(self):
1352
+ return self.n
1353
+
1354
+ def column(self, domain=None):
1355
+ """
1356
+ Get a copy of this element's column, optionally converting to a domain.
1357
+ """
1358
+ if domain is None:
1359
+ return self.col.copy()
1360
+ else:
1361
+ return self.col.convert_to(domain)
1362
+
1363
+ @property
1364
+ def coeffs(self):
1365
+ return self.col.flat()
1366
+
1367
+ @property
1368
+ def QQ_col(self):
1369
+ """
1370
+ :py:class:`~.DomainMatrix` over :ref:`QQ`, equal to
1371
+ ``self.col / self.denom``, and guaranteed to be dense.
1372
+
1373
+ See Also
1374
+ ========
1375
+
1376
+ .Submodule.QQ_matrix
1377
+
1378
+ """
1379
+ if self._QQ_col is None:
1380
+ self._QQ_col = (self.col / self.denom).to_dense()
1381
+ return self._QQ_col
1382
+
1383
+ def to_parent(self):
1384
+ """
1385
+ Transform into a :py:class:`~.ModuleElement` belonging to the parent of
1386
+ this element's module.
1387
+ """
1388
+ if not isinstance(self.module, Submodule):
1389
+ raise ValueError('Not an element of a Submodule.')
1390
+ return make_mod_elt(
1391
+ self.module.parent, self.module.matrix * self.col,
1392
+ denom=self.module.denom * self.denom)
1393
+
1394
+ def to_ancestor(self, anc):
1395
+ """
1396
+ Transform into a :py:class:`~.ModuleElement` belonging to a given
1397
+ ancestor of this element's module.
1398
+
1399
+ Parameters
1400
+ ==========
1401
+
1402
+ anc : :py:class:`~.Module`
1403
+
1404
+ """
1405
+ if anc == self.module:
1406
+ return self
1407
+ else:
1408
+ return self.to_parent().to_ancestor(anc)
1409
+
1410
+ def over_power_basis(self):
1411
+ """
1412
+ Transform into a :py:class:`~.PowerBasisElement` over our
1413
+ :py:class:`~.PowerBasis` ancestor.
1414
+ """
1415
+ e = self
1416
+ while not isinstance(e.module, PowerBasis):
1417
+ e = e.to_parent()
1418
+ return e
1419
+
1420
+ def is_compat(self, other):
1421
+ """
1422
+ Test whether other is another :py:class:`~.ModuleElement` with same
1423
+ module.
1424
+ """
1425
+ return isinstance(other, ModuleElement) and other.module == self.module
1426
+
1427
+ def unify(self, other):
1428
+ """
1429
+ Try to make a compatible pair of :py:class:`~.ModuleElement`, one
1430
+ equivalent to this one, and one equivalent to the other.
1431
+
1432
+ Explanation
1433
+ ===========
1434
+
1435
+ We search for the nearest common ancestor module for the pair of
1436
+ elements, and represent each one there.
1437
+
1438
+ Returns
1439
+ =======
1440
+
1441
+ Pair ``(e1, e2)``
1442
+ Each ``ei`` is a :py:class:`~.ModuleElement`, they belong to the
1443
+ same :py:class:`~.Module`, ``e1`` is equivalent to ``self``, and
1444
+ ``e2`` is equivalent to ``other``.
1445
+
1446
+ Raises
1447
+ ======
1448
+
1449
+ UnificationFailed
1450
+ If ``self`` and ``other`` have no common ancestor module.
1451
+
1452
+ """
1453
+ if self.module == other.module:
1454
+ return self, other
1455
+ nca = self.module.nearest_common_ancestor(other.module)
1456
+ if nca is not None:
1457
+ return self.to_ancestor(nca), other.to_ancestor(nca)
1458
+ raise UnificationFailed(f"Cannot unify {self} with {other}")
1459
+
1460
+ def __eq__(self, other):
1461
+ if self.is_compat(other):
1462
+ return self.QQ_col == other.QQ_col
1463
+ return NotImplemented
1464
+
1465
+ def equiv(self, other):
1466
+ """
1467
+ A :py:class:`~.ModuleElement` may test as equivalent to a rational
1468
+ number or another :py:class:`~.ModuleElement`, if they represent the
1469
+ same algebraic number.
1470
+
1471
+ Explanation
1472
+ ===========
1473
+
1474
+ This method is intended to check equivalence only in those cases in
1475
+ which it is easy to test; namely, when *other* is either a
1476
+ :py:class:`~.ModuleElement` that can be unified with this one (i.e. one
1477
+ which shares a common :py:class:`~.PowerBasis` ancestor), or else a
1478
+ rational number (which is easy because every :py:class:`~.PowerBasis`
1479
+ represents every rational number).
1480
+
1481
+ Parameters
1482
+ ==========
1483
+
1484
+ other : int, :ref:`ZZ`, :ref:`QQ`, :py:class:`~.ModuleElement`
1485
+
1486
+ Returns
1487
+ =======
1488
+
1489
+ bool
1490
+
1491
+ Raises
1492
+ ======
1493
+
1494
+ UnificationFailed
1495
+ If ``self`` and ``other`` do not share a common
1496
+ :py:class:`~.PowerBasis` ancestor.
1497
+
1498
+ """
1499
+ if self == other:
1500
+ return True
1501
+ elif isinstance(other, ModuleElement):
1502
+ a, b = self.unify(other)
1503
+ return a == b
1504
+ elif is_rat(other):
1505
+ if isinstance(self, PowerBasisElement):
1506
+ return self == self.module(0) * other
1507
+ else:
1508
+ return self.over_power_basis().equiv(other)
1509
+ return False
1510
+
1511
+ def __add__(self, other):
1512
+ """
1513
+ A :py:class:`~.ModuleElement` can be added to a rational number, or to
1514
+ another :py:class:`~.ModuleElement`.
1515
+
1516
+ Explanation
1517
+ ===========
1518
+
1519
+ When the other summand is a rational number, it will be converted into
1520
+ a :py:class:`~.ModuleElement` (belonging to the first ancestor of this
1521
+ module that starts with unity).
1522
+
1523
+ In all cases, the sum belongs to the nearest common ancestor (NCA) of
1524
+ the modules of the two summands. If the NCA does not exist, we return
1525
+ ``NotImplemented``.
1526
+ """
1527
+ if self.is_compat(other):
1528
+ d, e = self.denom, other.denom
1529
+ m = ilcm(d, e)
1530
+ u, v = m // d, m // e
1531
+ col = to_col([u * a + v * b for a, b in zip(self.coeffs, other.coeffs)])
1532
+ return type(self)(self.module, col, denom=m).reduced()
1533
+ elif isinstance(other, ModuleElement):
1534
+ try:
1535
+ a, b = self.unify(other)
1536
+ except UnificationFailed:
1537
+ return NotImplemented
1538
+ return a + b
1539
+ elif is_rat(other):
1540
+ return self + self.module.element_from_rational(other)
1541
+ return NotImplemented
1542
+
1543
+ __radd__ = __add__
1544
+
1545
+ def __neg__(self):
1546
+ return self * -1
1547
+
1548
+ def __sub__(self, other):
1549
+ return self + (-other)
1550
+
1551
+ def __rsub__(self, other):
1552
+ return -self + other
1553
+
1554
+ def __mul__(self, other):
1555
+ """
1556
+ A :py:class:`~.ModuleElement` can be multiplied by a rational number,
1557
+ or by another :py:class:`~.ModuleElement`.
1558
+
1559
+ Explanation
1560
+ ===========
1561
+
1562
+ When the multiplier is a rational number, the product is computed by
1563
+ operating directly on the coefficients of this
1564
+ :py:class:`~.ModuleElement`.
1565
+
1566
+ When the multiplier is another :py:class:`~.ModuleElement`, the product
1567
+ will belong to the nearest common ancestor (NCA) of the modules of the
1568
+ two operands, and that NCA must have a multiplication table. If the NCA
1569
+ does not exist, we return ``NotImplemented``. If the NCA does not have
1570
+ a mult. table, ``ClosureFailure`` will be raised.
1571
+ """
1572
+ if self.is_compat(other):
1573
+ M = self.module.mult_tab()
1574
+ A, B = self.col.flat(), other.col.flat()
1575
+ n = self.n
1576
+ C = [0] * n
1577
+ for u in range(n):
1578
+ for v in range(u, n):
1579
+ c = A[u] * B[v]
1580
+ if v > u:
1581
+ c += A[v] * B[u]
1582
+ if c != 0:
1583
+ R = M[u][v]
1584
+ for k in range(n):
1585
+ C[k] += c * R[k]
1586
+ d = self.denom * other.denom
1587
+ return self.from_int_list(self.module, C, denom=d)
1588
+ elif isinstance(other, ModuleElement):
1589
+ try:
1590
+ a, b = self.unify(other)
1591
+ except UnificationFailed:
1592
+ return NotImplemented
1593
+ return a * b
1594
+ elif is_rat(other):
1595
+ a, b = get_num_denom(other)
1596
+ if a == b == 1:
1597
+ return self
1598
+ else:
1599
+ return make_mod_elt(self.module,
1600
+ self.col * a, denom=self.denom * b).reduced()
1601
+ return NotImplemented
1602
+
1603
+ __rmul__ = __mul__
1604
+
1605
+ def _zeroth_power(self):
1606
+ return self.module.one()
1607
+
1608
+ def _first_power(self):
1609
+ return self
1610
+
1611
+ def __floordiv__(self, a):
1612
+ if is_rat(a):
1613
+ a = QQ(a)
1614
+ return self * (1/a)
1615
+ elif isinstance(a, ModuleElement):
1616
+ return self * (1//a)
1617
+ return NotImplemented
1618
+
1619
+ def __rfloordiv__(self, a):
1620
+ return a // self.over_power_basis()
1621
+
1622
+ def __mod__(self, m):
1623
+ r"""
1624
+ Reduce this :py:class:`~.ModuleElement` mod a :py:class:`~.Submodule`.
1625
+
1626
+ Parameters
1627
+ ==========
1628
+
1629
+ m : int, :ref:`ZZ`, :ref:`QQ`, :py:class:`~.Submodule`
1630
+ If a :py:class:`~.Submodule`, reduce ``self`` relative to this.
1631
+ If an integer or rational, reduce relative to the
1632
+ :py:class:`~.Submodule` that is our own module times this constant.
1633
+
1634
+ See Also
1635
+ ========
1636
+
1637
+ .Submodule.reduce_element
1638
+
1639
+ """
1640
+ if is_rat(m):
1641
+ m = m * self.module.whole_submodule()
1642
+ if isinstance(m, Submodule) and m.parent == self.module:
1643
+ return m.reduce_element(self)
1644
+ return NotImplemented
1645
+
1646
+
1647
+ class PowerBasisElement(ModuleElement):
1648
+ r"""
1649
+ Subclass for :py:class:`~.ModuleElement` instances whose module is a
1650
+ :py:class:`~.PowerBasis`.
1651
+ """
1652
+
1653
+ @property
1654
+ def T(self):
1655
+ """Access the defining polynomial of the :py:class:`~.PowerBasis`."""
1656
+ return self.module.T
1657
+
1658
+ def numerator(self, x=None):
1659
+ """Obtain the numerator as a polynomial over :ref:`ZZ`."""
1660
+ x = x or self.T.gen
1661
+ return Poly(reversed(self.coeffs), x, domain=ZZ)
1662
+
1663
+ def poly(self, x=None):
1664
+ """Obtain the number as a polynomial over :ref:`QQ`."""
1665
+ return self.numerator(x=x) // self.denom
1666
+
1667
+ @property
1668
+ def is_rational(self):
1669
+ """Say whether this element represents a rational number."""
1670
+ return self.col[1:, :].is_zero_matrix
1671
+
1672
+ @property
1673
+ def generator(self):
1674
+ """
1675
+ Return a :py:class:`~.Symbol` to be used when expressing this element
1676
+ as a polynomial.
1677
+
1678
+ If we have an associated :py:class:`~.AlgebraicField` whose primitive
1679
+ element has an alias symbol, we use that. Otherwise we use the variable
1680
+ of the minimal polynomial defining the power basis to which we belong.
1681
+ """
1682
+ K = self.module.number_field
1683
+ return K.ext.alias if K and K.ext.is_aliased else self.T.gen
1684
+
1685
+ def as_expr(self, x=None):
1686
+ """Create a Basic expression from ``self``. """
1687
+ return self.poly(x or self.generator).as_expr()
1688
+
1689
+ def norm(self, T=None):
1690
+ """Compute the norm of this number."""
1691
+ T = T or self.T
1692
+ x = T.gen
1693
+ A = self.numerator(x=x)
1694
+ return T.resultant(A) // self.denom ** self.n
1695
+
1696
+ def inverse(self):
1697
+ f = self.poly()
1698
+ f_inv = f.invert(self.T)
1699
+ return self.module.element_from_poly(f_inv)
1700
+
1701
+ def __rfloordiv__(self, a):
1702
+ return self.inverse() * a
1703
+
1704
+ def _negative_power(self, e, modulo=None):
1705
+ return self.inverse() ** abs(e)
1706
+
1707
+ def to_ANP(self):
1708
+ """Convert to an equivalent :py:class:`~.ANP`. """
1709
+ return ANP(list(reversed(self.QQ_col.flat())), QQ.map(self.T.rep.to_list()), QQ)
1710
+
1711
+ def to_alg_num(self):
1712
+ """
1713
+ Try to convert to an equivalent :py:class:`~.AlgebraicNumber`.
1714
+
1715
+ Explanation
1716
+ ===========
1717
+
1718
+ In general, the conversion from an :py:class:`~.AlgebraicNumber` to a
1719
+ :py:class:`~.PowerBasisElement` throws away information, because an
1720
+ :py:class:`~.AlgebraicNumber` specifies a complex embedding, while a
1721
+ :py:class:`~.PowerBasisElement` does not. However, in some cases it is
1722
+ possible to convert a :py:class:`~.PowerBasisElement` back into an
1723
+ :py:class:`~.AlgebraicNumber`, namely when the associated
1724
+ :py:class:`~.PowerBasis` has a reference to an
1725
+ :py:class:`~.AlgebraicField`.
1726
+
1727
+ Returns
1728
+ =======
1729
+
1730
+ :py:class:`~.AlgebraicNumber`
1731
+
1732
+ Raises
1733
+ ======
1734
+
1735
+ StructureError
1736
+ If the :py:class:`~.PowerBasis` to which this element belongs does
1737
+ not have an associated :py:class:`~.AlgebraicField`.
1738
+
1739
+ """
1740
+ K = self.module.number_field
1741
+ if K:
1742
+ return K.to_alg_num(self.to_ANP())
1743
+ raise StructureError("No associated AlgebraicField")
1744
+
1745
+
1746
+ class ModuleHomomorphism:
1747
+ r"""A homomorphism from one module to another."""
1748
+
1749
+ def __init__(self, domain, codomain, mapping):
1750
+ r"""
1751
+ Parameters
1752
+ ==========
1753
+
1754
+ domain : :py:class:`~.Module`
1755
+ The domain of the mapping.
1756
+
1757
+ codomain : :py:class:`~.Module`
1758
+ The codomain of the mapping.
1759
+
1760
+ mapping : callable
1761
+ An arbitrary callable is accepted, but should be chosen so as
1762
+ to represent an actual module homomorphism. In particular, should
1763
+ accept elements of *domain* and return elements of *codomain*.
1764
+
1765
+ Examples
1766
+ ========
1767
+
1768
+ >>> from sympy import Poly, cyclotomic_poly
1769
+ >>> from sympy.polys.numberfields.modules import PowerBasis, ModuleHomomorphism
1770
+ >>> T = Poly(cyclotomic_poly(5))
1771
+ >>> A = PowerBasis(T)
1772
+ >>> B = A.submodule_from_gens([2*A(j) for j in range(4)])
1773
+ >>> phi = ModuleHomomorphism(A, B, lambda x: 6*x)
1774
+ >>> print(phi.matrix()) # doctest: +SKIP
1775
+ DomainMatrix([[3, 0, 0, 0], [0, 3, 0, 0], [0, 0, 3, 0], [0, 0, 0, 3]], (4, 4), ZZ)
1776
+
1777
+ """
1778
+ self.domain = domain
1779
+ self.codomain = codomain
1780
+ self.mapping = mapping
1781
+
1782
+ def matrix(self, modulus=None):
1783
+ r"""
1784
+ Compute the matrix of this homomorphism.
1785
+
1786
+ Parameters
1787
+ ==========
1788
+
1789
+ modulus : int, optional
1790
+ A positive prime number $p$ if the matrix should be reduced mod
1791
+ $p$.
1792
+
1793
+ Returns
1794
+ =======
1795
+
1796
+ :py:class:`~.DomainMatrix`
1797
+ The matrix is over :ref:`ZZ`, or else over :ref:`GF(p)` if a
1798
+ modulus was given.
1799
+
1800
+ """
1801
+ basis = self.domain.basis_elements()
1802
+ cols = [self.codomain.represent(self.mapping(elt)) for elt in basis]
1803
+ if not cols:
1804
+ return DomainMatrix.zeros((self.codomain.n, 0), ZZ).to_dense()
1805
+ M = cols[0].hstack(*cols[1:])
1806
+ if modulus:
1807
+ M = M.convert_to(FF(modulus))
1808
+ return M
1809
+
1810
+ def kernel(self, modulus=None):
1811
+ r"""
1812
+ Compute a Submodule representing the kernel of this homomorphism.
1813
+
1814
+ Parameters
1815
+ ==========
1816
+
1817
+ modulus : int, optional
1818
+ A positive prime number $p$ if the kernel should be computed mod
1819
+ $p$.
1820
+
1821
+ Returns
1822
+ =======
1823
+
1824
+ :py:class:`~.Submodule`
1825
+ This submodule's generators span the kernel of this
1826
+ homomorphism over :ref:`ZZ`, or else over :ref:`GF(p)` if a
1827
+ modulus was given.
1828
+
1829
+ """
1830
+ M = self.matrix(modulus=modulus)
1831
+ if modulus is None:
1832
+ M = M.convert_to(QQ)
1833
+ # Note: Even when working over a finite field, what we want here is
1834
+ # the pullback into the integers, so in this case the conversion to ZZ
1835
+ # below is appropriate. When working over ZZ, the kernel should be a
1836
+ # ZZ-submodule, so, while the conversion to QQ above was required in
1837
+ # order for the nullspace calculation to work, conversion back to ZZ
1838
+ # afterward should always work.
1839
+ # TODO:
1840
+ # Watch <https://github.com/sympy/sympy/issues/21834>, which calls
1841
+ # for fraction-free algorithms. If this is implemented, we can skip
1842
+ # the conversion to `QQ` above.
1843
+ K = M.nullspace().convert_to(ZZ).transpose()
1844
+ return self.domain.submodule_from_matrix(K)
1845
+
1846
+
1847
+ class ModuleEndomorphism(ModuleHomomorphism):
1848
+ r"""A homomorphism from one module to itself."""
1849
+
1850
+ def __init__(self, domain, mapping):
1851
+ r"""
1852
+ Parameters
1853
+ ==========
1854
+
1855
+ domain : :py:class:`~.Module`
1856
+ The common domain and codomain of the mapping.
1857
+
1858
+ mapping : callable
1859
+ An arbitrary callable is accepted, but should be chosen so as
1860
+ to represent an actual module endomorphism. In particular, should
1861
+ accept and return elements of *domain*.
1862
+
1863
+ """
1864
+ super().__init__(domain, domain, mapping)
1865
+
1866
+
1867
+ class InnerEndomorphism(ModuleEndomorphism):
1868
+ r"""
1869
+ An inner endomorphism on a module, i.e. the endomorphism corresponding to
1870
+ multiplication by a fixed element.
1871
+ """
1872
+
1873
+ def __init__(self, domain, multiplier):
1874
+ r"""
1875
+ Parameters
1876
+ ==========
1877
+
1878
+ domain : :py:class:`~.Module`
1879
+ The domain and codomain of the endomorphism.
1880
+
1881
+ multiplier : :py:class:`~.ModuleElement`
1882
+ The element $a$ defining the mapping as $x \mapsto a x$.
1883
+
1884
+ """
1885
+ super().__init__(domain, lambda x: multiplier * x)
1886
+ self.multiplier = multiplier
1887
+
1888
+
1889
+ class EndomorphismRing:
1890
+ r"""The ring of endomorphisms on a module."""
1891
+
1892
+ def __init__(self, domain):
1893
+ """
1894
+ Parameters
1895
+ ==========
1896
+
1897
+ domain : :py:class:`~.Module`
1898
+ The domain and codomain of the endomorphisms.
1899
+
1900
+ """
1901
+ self.domain = domain
1902
+
1903
+ def inner_endomorphism(self, multiplier):
1904
+ r"""
1905
+ Form an inner endomorphism belonging to this endomorphism ring.
1906
+
1907
+ Parameters
1908
+ ==========
1909
+
1910
+ multiplier : :py:class:`~.ModuleElement`
1911
+ Element $a$ defining the inner endomorphism $x \mapsto a x$.
1912
+
1913
+ Returns
1914
+ =======
1915
+
1916
+ :py:class:`~.InnerEndomorphism`
1917
+
1918
+ """
1919
+ return InnerEndomorphism(self.domain, multiplier)
1920
+
1921
+ def represent(self, element):
1922
+ r"""
1923
+ Represent an element of this endomorphism ring, as a single column
1924
+ vector.
1925
+
1926
+ Explanation
1927
+ ===========
1928
+
1929
+ Let $M$ be a module, and $E$ its ring of endomorphisms. Let $N$ be
1930
+ another module, and consider a homomorphism $\varphi: N \rightarrow E$.
1931
+ In the event that $\varphi$ is to be represented by a matrix $A$, each
1932
+ column of $A$ must represent an element of $E$. This is possible when
1933
+ the elements of $E$ are themselves representable as matrices, by
1934
+ stacking the columns of such a matrix into a single column.
1935
+
1936
+ This method supports calculating such matrices $A$, by representing
1937
+ an element of this endomorphism ring first as a matrix, and then
1938
+ stacking that matrix's columns into a single column.
1939
+
1940
+ Examples
1941
+ ========
1942
+
1943
+ Note that in these examples we print matrix transposes, to make their
1944
+ columns easier to inspect.
1945
+
1946
+ >>> from sympy import Poly, cyclotomic_poly
1947
+ >>> from sympy.polys.numberfields.modules import PowerBasis
1948
+ >>> from sympy.polys.numberfields.modules import ModuleHomomorphism
1949
+ >>> T = Poly(cyclotomic_poly(5))
1950
+ >>> M = PowerBasis(T)
1951
+ >>> E = M.endomorphism_ring()
1952
+
1953
+ Let $\zeta$ be a primitive 5th root of unity, a generator of our field,
1954
+ and consider the inner endomorphism $\tau$ on the ring of integers,
1955
+ induced by $\zeta$:
1956
+
1957
+ >>> zeta = M(1)
1958
+ >>> tau = E.inner_endomorphism(zeta)
1959
+ >>> tau.matrix().transpose() # doctest: +SKIP
1960
+ DomainMatrix(
1961
+ [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [-1, -1, -1, -1]],
1962
+ (4, 4), ZZ)
1963
+
1964
+ The matrix representation of $\tau$ is as expected. The first column
1965
+ shows that multiplying by $\zeta$ carries $1$ to $\zeta$, the second
1966
+ column that it carries $\zeta$ to $\zeta^2$, and so forth.
1967
+
1968
+ The ``represent`` method of the endomorphism ring ``E`` stacks these
1969
+ into a single column:
1970
+
1971
+ >>> E.represent(tau).transpose() # doctest: +SKIP
1972
+ DomainMatrix(
1973
+ [[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, -1, -1, -1, -1]],
1974
+ (1, 16), ZZ)
1975
+
1976
+ This is useful when we want to consider a homomorphism $\varphi$ having
1977
+ ``E`` as codomain:
1978
+
1979
+ >>> phi = ModuleHomomorphism(M, E, lambda x: E.inner_endomorphism(x))
1980
+
1981
+ and we want to compute the matrix of such a homomorphism:
1982
+
1983
+ >>> phi.matrix().transpose() # doctest: +SKIP
1984
+ DomainMatrix(
1985
+ [[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
1986
+ [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, -1, -1, -1, -1],
1987
+ [0, 0, 1, 0, 0, 0, 0, 1, -1, -1, -1, -1, 1, 0, 0, 0],
1988
+ [0, 0, 0, 1, -1, -1, -1, -1, 1, 0, 0, 0, 0, 1, 0, 0]],
1989
+ (4, 16), ZZ)
1990
+
1991
+ Note that the stacked matrix of $\tau$ occurs as the second column in
1992
+ this example. This is because $\zeta$ is the second basis element of
1993
+ ``M``, and $\varphi(\zeta) = \tau$.
1994
+
1995
+ Parameters
1996
+ ==========
1997
+
1998
+ element : :py:class:`~.ModuleEndomorphism` belonging to this ring.
1999
+
2000
+ Returns
2001
+ =======
2002
+
2003
+ :py:class:`~.DomainMatrix`
2004
+ Column vector equalling the vertical stacking of all the columns
2005
+ of the matrix that represents the given *element* as a mapping.
2006
+
2007
+ """
2008
+ if isinstance(element, ModuleEndomorphism) and element.domain == self.domain:
2009
+ M = element.matrix()
2010
+ # Transform the matrix into a single column, which should reproduce
2011
+ # the original columns, one after another.
2012
+ m, n = M.shape
2013
+ if n == 0:
2014
+ return M
2015
+ return M[:, 0].vstack(*[M[:, j] for j in range(1, n)])
2016
+ raise NotImplementedError
2017
+
2018
+
2019
+ def find_min_poly(alpha, domain, x=None, powers=None):
2020
+ r"""
2021
+ Find a polynomial of least degree (not necessarily irreducible) satisfied
2022
+ by an element of a finitely-generated ring with unity.
2023
+
2024
+ Examples
2025
+ ========
2026
+
2027
+ For the $n$th cyclotomic field, $n$ an odd prime, consider the quadratic
2028
+ equation whose roots are the two periods of length $(n-1)/2$. Article 356
2029
+ of Gauss tells us that we should get $x^2 + x - (n-1)/4$ or
2030
+ $x^2 + x + (n+1)/4$ according to whether $n$ is 1 or 3 mod 4, respectively.
2031
+
2032
+ >>> from sympy import Poly, cyclotomic_poly, primitive_root, QQ
2033
+ >>> from sympy.abc import x
2034
+ >>> from sympy.polys.numberfields.modules import PowerBasis, find_min_poly
2035
+ >>> n = 13
2036
+ >>> g = primitive_root(n)
2037
+ >>> C = PowerBasis(Poly(cyclotomic_poly(n, x)))
2038
+ >>> ee = [g**(2*k+1) % n for k in range((n-1)//2)]
2039
+ >>> eta = sum(C(e) for e in ee)
2040
+ >>> print(find_min_poly(eta, QQ, x=x).as_expr())
2041
+ x**2 + x - 3
2042
+ >>> n = 19
2043
+ >>> g = primitive_root(n)
2044
+ >>> C = PowerBasis(Poly(cyclotomic_poly(n, x)))
2045
+ >>> ee = [g**(2*k+2) % n for k in range((n-1)//2)]
2046
+ >>> eta = sum(C(e) for e in ee)
2047
+ >>> print(find_min_poly(eta, QQ, x=x).as_expr())
2048
+ x**2 + x + 5
2049
+
2050
+ Parameters
2051
+ ==========
2052
+
2053
+ alpha : :py:class:`~.ModuleElement`
2054
+ The element whose min poly is to be found, and whose module has
2055
+ multiplication and starts with unity.
2056
+
2057
+ domain : :py:class:`~.Domain`
2058
+ The desired domain of the polynomial.
2059
+
2060
+ x : :py:class:`~.Symbol`, optional
2061
+ The desired variable for the polynomial.
2062
+
2063
+ powers : list, optional
2064
+ If desired, pass an empty list. The powers of *alpha* (as
2065
+ :py:class:`~.ModuleElement` instances) from the zeroth up to the degree
2066
+ of the min poly will be recorded here, as we compute them.
2067
+
2068
+ Returns
2069
+ =======
2070
+
2071
+ :py:class:`~.Poly`, ``None``
2072
+ The minimal polynomial for alpha, or ``None`` if no polynomial could be
2073
+ found over the desired domain.
2074
+
2075
+ Raises
2076
+ ======
2077
+
2078
+ MissingUnityError
2079
+ If the module to which alpha belongs does not start with unity.
2080
+ ClosureFailure
2081
+ If the module to which alpha belongs is not closed under
2082
+ multiplication.
2083
+
2084
+ """
2085
+ R = alpha.module
2086
+ if not R.starts_with_unity():
2087
+ raise MissingUnityError("alpha must belong to finitely generated ring with unity.")
2088
+ if powers is None:
2089
+ powers = []
2090
+ one = R(0)
2091
+ powers.append(one)
2092
+ powers_matrix = one.column(domain=domain)
2093
+ ak = alpha
2094
+ m = None
2095
+ for k in range(1, R.n + 1):
2096
+ powers.append(ak)
2097
+ ak_col = ak.column(domain=domain)
2098
+ try:
2099
+ X = powers_matrix._solve(ak_col)[0]
2100
+ except DMBadInputError:
2101
+ # This means alpha^k still isn't in the domain-span of the lower powers.
2102
+ powers_matrix = powers_matrix.hstack(ak_col)
2103
+ ak *= alpha
2104
+ else:
2105
+ # alpha^k is in the domain-span of the lower powers, so we have found a
2106
+ # minimal-degree poly for alpha.
2107
+ coeffs = [1] + [-c for c in reversed(X.to_list_flat())]
2108
+ x = x or Dummy('x')
2109
+ if domain.is_FF:
2110
+ m = Poly(coeffs, x, modulus=domain.mod)
2111
+ else:
2112
+ m = Poly(coeffs, x, domain=domain)
2113
+ break
2114
+ return m
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/primes.py ADDED
@@ -0,0 +1,784 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Prime ideals in number fields. """
2
+
3
+ from sympy.polys.polytools import Poly
4
+ from sympy.polys.domains.finitefield import FF
5
+ from sympy.polys.domains.rationalfield import QQ
6
+ from sympy.polys.domains.integerring import ZZ
7
+ from sympy.polys.matrices.domainmatrix import DomainMatrix
8
+ from sympy.polys.polyerrors import CoercionFailed
9
+ from sympy.polys.polyutils import IntegerPowerable
10
+ from sympy.utilities.decorator import public
11
+ from .basis import round_two, nilradical_mod_p
12
+ from .exceptions import StructureError
13
+ from .modules import ModuleEndomorphism, find_min_poly
14
+ from .utilities import coeff_search, supplement_a_subspace
15
+
16
+
17
+ def _check_formal_conditions_for_maximal_order(submodule):
18
+ r"""
19
+ Several functions in this module accept an argument which is to be a
20
+ :py:class:`~.Submodule` representing the maximal order in a number field,
21
+ such as returned by the :py:func:`~sympy.polys.numberfields.basis.round_two`
22
+ algorithm.
23
+
24
+ We do not attempt to check that the given ``Submodule`` actually represents
25
+ a maximal order, but we do check a basic set of formal conditions that the
26
+ ``Submodule`` must satisfy, at a minimum. The purpose is to catch an
27
+ obviously ill-formed argument.
28
+ """
29
+ prefix = 'The submodule representing the maximal order should '
30
+ cond = None
31
+ if not submodule.is_power_basis_submodule():
32
+ cond = 'be a direct submodule of a power basis.'
33
+ elif not submodule.starts_with_unity():
34
+ cond = 'have 1 as its first generator.'
35
+ elif not submodule.is_sq_maxrank_HNF():
36
+ cond = 'have square matrix, of maximal rank, in Hermite Normal Form.'
37
+ if cond is not None:
38
+ raise StructureError(prefix + cond)
39
+
40
+
41
+ class PrimeIdeal(IntegerPowerable):
42
+ r"""
43
+ A prime ideal in a ring of algebraic integers.
44
+ """
45
+
46
+ def __init__(self, ZK, p, alpha, f, e=None):
47
+ """
48
+ Parameters
49
+ ==========
50
+
51
+ ZK : :py:class:`~.Submodule`
52
+ The maximal order where this ideal lives.
53
+ p : int
54
+ The rational prime this ideal divides.
55
+ alpha : :py:class:`~.PowerBasisElement`
56
+ Such that the ideal is equal to ``p*ZK + alpha*ZK``.
57
+ f : int
58
+ The inertia degree.
59
+ e : int, ``None``, optional
60
+ The ramification index, if already known. If ``None``, we will
61
+ compute it here.
62
+
63
+ """
64
+ _check_formal_conditions_for_maximal_order(ZK)
65
+ self.ZK = ZK
66
+ self.p = p
67
+ self.alpha = alpha
68
+ self.f = f
69
+ self._test_factor = None
70
+ self.e = e if e is not None else self.valuation(p * ZK)
71
+
72
+ def __str__(self):
73
+ if self.is_inert:
74
+ return f'({self.p})'
75
+ return f'({self.p}, {self.alpha.as_expr()})'
76
+
77
+ @property
78
+ def is_inert(self):
79
+ """
80
+ Say whether the rational prime we divide is inert, i.e. stays prime in
81
+ our ring of integers.
82
+ """
83
+ return self.f == self.ZK.n
84
+
85
+ def repr(self, field_gen=None, just_gens=False):
86
+ """
87
+ Print a representation of this prime ideal.
88
+
89
+ Examples
90
+ ========
91
+
92
+ >>> from sympy import cyclotomic_poly, QQ
93
+ >>> from sympy.abc import x, zeta
94
+ >>> T = cyclotomic_poly(7, x)
95
+ >>> K = QQ.algebraic_field((T, zeta))
96
+ >>> P = K.primes_above(11)
97
+ >>> print(P[0].repr())
98
+ [ (11, x**3 + 5*x**2 + 4*x - 1) e=1, f=3 ]
99
+ >>> print(P[0].repr(field_gen=zeta))
100
+ [ (11, zeta**3 + 5*zeta**2 + 4*zeta - 1) e=1, f=3 ]
101
+ >>> print(P[0].repr(field_gen=zeta, just_gens=True))
102
+ (11, zeta**3 + 5*zeta**2 + 4*zeta - 1)
103
+
104
+ Parameters
105
+ ==========
106
+
107
+ field_gen : :py:class:`~.Symbol`, ``None``, optional (default=None)
108
+ The symbol to use for the generator of the field. This will appear
109
+ in our representation of ``self.alpha``. If ``None``, we use the
110
+ variable of the defining polynomial of ``self.ZK``.
111
+ just_gens : bool, optional (default=False)
112
+ If ``True``, just print the "(p, alpha)" part, showing "just the
113
+ generators" of the prime ideal. Otherwise, print a string of the
114
+ form "[ (p, alpha) e=..., f=... ]", giving the ramification index
115
+ and inertia degree, along with the generators.
116
+
117
+ """
118
+ field_gen = field_gen or self.ZK.parent.T.gen
119
+ p, alpha, e, f = self.p, self.alpha, self.e, self.f
120
+ alpha_rep = str(alpha.numerator(x=field_gen).as_expr())
121
+ if alpha.denom > 1:
122
+ alpha_rep = f'({alpha_rep})/{alpha.denom}'
123
+ gens = f'({p}, {alpha_rep})'
124
+ if just_gens:
125
+ return gens
126
+ return f'[ {gens} e={e}, f={f} ]'
127
+
128
+ def __repr__(self):
129
+ return self.repr()
130
+
131
+ def as_submodule(self):
132
+ r"""
133
+ Represent this prime ideal as a :py:class:`~.Submodule`.
134
+
135
+ Explanation
136
+ ===========
137
+
138
+ The :py:class:`~.PrimeIdeal` class serves to bundle information about
139
+ a prime ideal, such as its inertia degree, ramification index, and
140
+ two-generator representation, as well as to offer helpful methods like
141
+ :py:meth:`~.PrimeIdeal.valuation` and
142
+ :py:meth:`~.PrimeIdeal.test_factor`.
143
+
144
+ However, in order to be added and multiplied by other ideals or
145
+ rational numbers, it must first be converted into a
146
+ :py:class:`~.Submodule`, which is a class that supports these
147
+ operations.
148
+
149
+ In many cases, the user need not perform this conversion deliberately,
150
+ since it is automatically performed by the arithmetic operator methods
151
+ :py:meth:`~.PrimeIdeal.__add__` and :py:meth:`~.PrimeIdeal.__mul__`.
152
+
153
+ Raising a :py:class:`~.PrimeIdeal` to a non-negative integer power is
154
+ also supported.
155
+
156
+ Examples
157
+ ========
158
+
159
+ >>> from sympy import Poly, cyclotomic_poly, prime_decomp
160
+ >>> T = Poly(cyclotomic_poly(7))
161
+ >>> P0 = prime_decomp(7, T)[0]
162
+ >>> print(P0**6 == 7*P0.ZK)
163
+ True
164
+
165
+ Note that, on both sides of the equation above, we had a
166
+ :py:class:`~.Submodule`. In the next equation we recall that adding
167
+ ideals yields their GCD. This time, we need a deliberate conversion
168
+ to :py:class:`~.Submodule` on the right:
169
+
170
+ >>> print(P0 + 7*P0.ZK == P0.as_submodule())
171
+ True
172
+
173
+ Returns
174
+ =======
175
+
176
+ :py:class:`~.Submodule`
177
+ Will be equal to ``self.p * self.ZK + self.alpha * self.ZK``.
178
+
179
+ See Also
180
+ ========
181
+
182
+ __add__
183
+ __mul__
184
+
185
+ """
186
+ M = self.p * self.ZK + self.alpha * self.ZK
187
+ # Pre-set expensive boolean properties whose value we already know:
188
+ M._starts_with_unity = False
189
+ M._is_sq_maxrank_HNF = True
190
+ return M
191
+
192
+ def __eq__(self, other):
193
+ if isinstance(other, PrimeIdeal):
194
+ return self.as_submodule() == other.as_submodule()
195
+ return NotImplemented
196
+
197
+ def __add__(self, other):
198
+ """
199
+ Convert to a :py:class:`~.Submodule` and add to another
200
+ :py:class:`~.Submodule`.
201
+
202
+ See Also
203
+ ========
204
+
205
+ as_submodule
206
+
207
+ """
208
+ return self.as_submodule() + other
209
+
210
+ __radd__ = __add__
211
+
212
+ def __mul__(self, other):
213
+ """
214
+ Convert to a :py:class:`~.Submodule` and multiply by another
215
+ :py:class:`~.Submodule` or a rational number.
216
+
217
+ See Also
218
+ ========
219
+
220
+ as_submodule
221
+
222
+ """
223
+ return self.as_submodule() * other
224
+
225
+ __rmul__ = __mul__
226
+
227
+ def _zeroth_power(self):
228
+ return self.ZK
229
+
230
+ def _first_power(self):
231
+ return self
232
+
233
+ def test_factor(self):
234
+ r"""
235
+ Compute a test factor for this prime ideal.
236
+
237
+ Explanation
238
+ ===========
239
+
240
+ Write $\mathfrak{p}$ for this prime ideal, $p$ for the rational prime
241
+ it divides. Then, for computing $\mathfrak{p}$-adic valuations it is
242
+ useful to have a number $\beta \in \mathbb{Z}_K$ such that
243
+ $p/\mathfrak{p} = p \mathbb{Z}_K + \beta \mathbb{Z}_K$.
244
+
245
+ Essentially, this is the same as the number $\Psi$ (or the "reagent")
246
+ from Kummer's 1847 paper (*Ueber die Zerlegung...*, Crelle vol. 35) in
247
+ which ideal divisors were invented.
248
+ """
249
+ if self._test_factor is None:
250
+ self._test_factor = _compute_test_factor(self.p, [self.alpha], self.ZK)
251
+ return self._test_factor
252
+
253
+ def valuation(self, I):
254
+ r"""
255
+ Compute the $\mathfrak{p}$-adic valuation of integral ideal I at this
256
+ prime ideal.
257
+
258
+ Parameters
259
+ ==========
260
+
261
+ I : :py:class:`~.Submodule`
262
+
263
+ See Also
264
+ ========
265
+
266
+ prime_valuation
267
+
268
+ """
269
+ return prime_valuation(I, self)
270
+
271
+ def reduce_element(self, elt):
272
+ """
273
+ Reduce a :py:class:`~.PowerBasisElement` to a "small representative"
274
+ modulo this prime ideal.
275
+
276
+ Parameters
277
+ ==========
278
+
279
+ elt : :py:class:`~.PowerBasisElement`
280
+ The element to be reduced.
281
+
282
+ Returns
283
+ =======
284
+
285
+ :py:class:`~.PowerBasisElement`
286
+ The reduced element.
287
+
288
+ See Also
289
+ ========
290
+
291
+ reduce_ANP
292
+ reduce_alg_num
293
+ .Submodule.reduce_element
294
+
295
+ """
296
+ return self.as_submodule().reduce_element(elt)
297
+
298
+ def reduce_ANP(self, a):
299
+ """
300
+ Reduce an :py:class:`~.ANP` to a "small representative" modulo this
301
+ prime ideal.
302
+
303
+ Parameters
304
+ ==========
305
+
306
+ elt : :py:class:`~.ANP`
307
+ The element to be reduced.
308
+
309
+ Returns
310
+ =======
311
+
312
+ :py:class:`~.ANP`
313
+ The reduced element.
314
+
315
+ See Also
316
+ ========
317
+
318
+ reduce_element
319
+ reduce_alg_num
320
+ .Submodule.reduce_element
321
+
322
+ """
323
+ elt = self.ZK.parent.element_from_ANP(a)
324
+ red = self.reduce_element(elt)
325
+ return red.to_ANP()
326
+
327
+ def reduce_alg_num(self, a):
328
+ """
329
+ Reduce an :py:class:`~.AlgebraicNumber` to a "small representative"
330
+ modulo this prime ideal.
331
+
332
+ Parameters
333
+ ==========
334
+
335
+ elt : :py:class:`~.AlgebraicNumber`
336
+ The element to be reduced.
337
+
338
+ Returns
339
+ =======
340
+
341
+ :py:class:`~.AlgebraicNumber`
342
+ The reduced element.
343
+
344
+ See Also
345
+ ========
346
+
347
+ reduce_element
348
+ reduce_ANP
349
+ .Submodule.reduce_element
350
+
351
+ """
352
+ elt = self.ZK.parent.element_from_alg_num(a)
353
+ red = self.reduce_element(elt)
354
+ return a.field_element(list(reversed(red.QQ_col.flat())))
355
+
356
+
357
+ def _compute_test_factor(p, gens, ZK):
358
+ r"""
359
+ Compute the test factor for a :py:class:`~.PrimeIdeal` $\mathfrak{p}$.
360
+
361
+ Parameters
362
+ ==========
363
+
364
+ p : int
365
+ The rational prime $\mathfrak{p}$ divides
366
+
367
+ gens : list of :py:class:`PowerBasisElement`
368
+ A complete set of generators for $\mathfrak{p}$ over *ZK*, EXCEPT that
369
+ an element equivalent to rational *p* can and should be omitted (since
370
+ it has no effect except to waste time).
371
+
372
+ ZK : :py:class:`~.Submodule`
373
+ The maximal order where the prime ideal $\mathfrak{p}$ lives.
374
+
375
+ Returns
376
+ =======
377
+
378
+ :py:class:`~.PowerBasisElement`
379
+
380
+ References
381
+ ==========
382
+
383
+ .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
384
+ (See Proposition 4.8.15.)
385
+
386
+ """
387
+ _check_formal_conditions_for_maximal_order(ZK)
388
+ E = ZK.endomorphism_ring()
389
+ matrices = [E.inner_endomorphism(g).matrix(modulus=p) for g in gens]
390
+ B = DomainMatrix.zeros((0, ZK.n), FF(p)).vstack(*matrices)
391
+ # A nonzero element of the nullspace of B will represent a
392
+ # lin comb over the omegas which (i) is not a multiple of p
393
+ # (since it is nonzero over FF(p)), while (ii) is such that
394
+ # its product with each g in gens _is_ a multiple of p (since
395
+ # B represents multiplication by these generators). Theory
396
+ # predicts that such an element must exist, so nullspace should
397
+ # be non-trivial.
398
+ x = B.nullspace()[0, :].transpose()
399
+ beta = ZK.parent(ZK.matrix * x.convert_to(ZZ), denom=ZK.denom)
400
+ return beta
401
+
402
+
403
+ @public
404
+ def prime_valuation(I, P):
405
+ r"""
406
+ Compute the *P*-adic valuation for an integral ideal *I*.
407
+
408
+ Examples
409
+ ========
410
+
411
+ >>> from sympy import QQ
412
+ >>> from sympy.polys.numberfields import prime_valuation
413
+ >>> K = QQ.cyclotomic_field(5)
414
+ >>> P = K.primes_above(5)
415
+ >>> ZK = K.maximal_order()
416
+ >>> print(prime_valuation(25*ZK, P[0]))
417
+ 8
418
+
419
+ Parameters
420
+ ==========
421
+
422
+ I : :py:class:`~.Submodule`
423
+ An integral ideal whose valuation is desired.
424
+
425
+ P : :py:class:`~.PrimeIdeal`
426
+ The prime at which to compute the valuation.
427
+
428
+ Returns
429
+ =======
430
+
431
+ int
432
+
433
+ See Also
434
+ ========
435
+
436
+ .PrimeIdeal.valuation
437
+
438
+ References
439
+ ==========
440
+
441
+ .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
442
+ (See Algorithm 4.8.17.)
443
+
444
+ """
445
+ p, ZK = P.p, P.ZK
446
+ n, W, d = ZK.n, ZK.matrix, ZK.denom
447
+
448
+ A = W.convert_to(QQ).inv() * I.matrix * d / I.denom
449
+ # Although A must have integer entries, given that I is an integral ideal,
450
+ # as a DomainMatrix it will still be over QQ, so we convert back:
451
+ A = A.convert_to(ZZ)
452
+ D = A.det()
453
+ if D % p != 0:
454
+ return 0
455
+
456
+ beta = P.test_factor()
457
+
458
+ f = d ** n // W.det()
459
+ need_complete_test = (f % p == 0)
460
+ v = 0
461
+ while True:
462
+ # Entering the loop, the cols of A represent lin combs of omegas.
463
+ # Turn them into lin combs of thetas:
464
+ A = W * A
465
+ # And then one column at a time...
466
+ for j in range(n):
467
+ c = ZK.parent(A[:, j], denom=d)
468
+ c *= beta
469
+ # ...turn back into lin combs of omegas, after multiplying by beta:
470
+ c = ZK.represent(c).flat()
471
+ for i in range(n):
472
+ A[i, j] = c[i]
473
+ if A[n - 1, n - 1].element % p != 0:
474
+ break
475
+ A = A / p
476
+ # As noted above, domain converts to QQ even when division goes evenly.
477
+ # So must convert back, even when we don't "need_complete_test".
478
+ if need_complete_test:
479
+ # In this case, having a non-integer entry is actually just our
480
+ # halting condition.
481
+ try:
482
+ A = A.convert_to(ZZ)
483
+ except CoercionFailed:
484
+ break
485
+ else:
486
+ # In this case theory says we should not have any non-integer entries.
487
+ A = A.convert_to(ZZ)
488
+ v += 1
489
+ return v
490
+
491
+
492
+ def _two_elt_rep(gens, ZK, p, f=None, Np=None):
493
+ r"""
494
+ Given a set of *ZK*-generators of a prime ideal, compute a set of just two
495
+ *ZK*-generators for the same ideal, one of which is *p* itself.
496
+
497
+ Parameters
498
+ ==========
499
+
500
+ gens : list of :py:class:`PowerBasisElement`
501
+ Generators for the prime ideal over *ZK*, the ring of integers of the
502
+ field $K$.
503
+
504
+ ZK : :py:class:`~.Submodule`
505
+ The maximal order in $K$.
506
+
507
+ p : int
508
+ The rational prime divided by the prime ideal.
509
+
510
+ f : int, optional
511
+ The inertia degree of the prime ideal, if known.
512
+
513
+ Np : int, optional
514
+ The norm $p^f$ of the prime ideal, if known.
515
+ NOTE: There is no reason to supply both *f* and *Np*. Either one will
516
+ save us from having to compute the norm *Np* ourselves. If both are known,
517
+ *Np* is preferred since it saves one exponentiation.
518
+
519
+ Returns
520
+ =======
521
+
522
+ :py:class:`~.PowerBasisElement` representing a single algebraic integer
523
+ alpha such that the prime ideal is equal to ``p*ZK + alpha*ZK``.
524
+
525
+ References
526
+ ==========
527
+
528
+ .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
529
+ (See Algorithm 4.7.10.)
530
+
531
+ """
532
+ _check_formal_conditions_for_maximal_order(ZK)
533
+ pb = ZK.parent
534
+ T = pb.T
535
+ # Detect the special cases in which either (a) all generators are multiples
536
+ # of p, or (b) there are no generators (so `all` is vacuously true):
537
+ if all((g % p).equiv(0) for g in gens):
538
+ return pb.zero()
539
+
540
+ if Np is None:
541
+ if f is not None:
542
+ Np = p**f
543
+ else:
544
+ Np = abs(pb.submodule_from_gens(gens).matrix.det())
545
+
546
+ omega = ZK.basis_element_pullbacks()
547
+ beta = [p*om for om in omega[1:]] # note: we omit omega[0] == 1
548
+ beta += gens
549
+ search = coeff_search(len(beta), 1)
550
+ for c in search:
551
+ alpha = sum(ci*betai for ci, betai in zip(c, beta))
552
+ # Note: It may be tempting to reduce alpha mod p here, to try to work
553
+ # with smaller numbers, but must not do that, as it can result in an
554
+ # infinite loop! E.g. try factoring 2 in Q(sqrt(-7)).
555
+ n = alpha.norm(T) // Np
556
+ if n % p != 0:
557
+ # Now can reduce alpha mod p.
558
+ return alpha % p
559
+
560
+
561
+ def _prime_decomp_easy_case(p, ZK):
562
+ r"""
563
+ Compute the decomposition of rational prime *p* in the ring of integers
564
+ *ZK* (given as a :py:class:`~.Submodule`), in the "easy case", i.e. the
565
+ case where *p* does not divide the index of $\theta$ in *ZK*, where
566
+ $\theta$ is the generator of the ``PowerBasis`` of which *ZK* is a
567
+ ``Submodule``.
568
+ """
569
+ T = ZK.parent.T
570
+ T_bar = Poly(T, modulus=p)
571
+ lc, fl = T_bar.factor_list()
572
+ if len(fl) == 1 and fl[0][1] == 1:
573
+ return [PrimeIdeal(ZK, p, ZK.parent.zero(), ZK.n, 1)]
574
+ return [PrimeIdeal(ZK, p,
575
+ ZK.parent.element_from_poly(Poly(t, domain=ZZ)),
576
+ t.degree(), e)
577
+ for t, e in fl]
578
+
579
+
580
+ def _prime_decomp_compute_kernel(I, p, ZK):
581
+ r"""
582
+ Parameters
583
+ ==========
584
+
585
+ I : :py:class:`~.Module`
586
+ An ideal of ``ZK/pZK``.
587
+ p : int
588
+ The rational prime being factored.
589
+ ZK : :py:class:`~.Submodule`
590
+ The maximal order.
591
+
592
+ Returns
593
+ =======
594
+
595
+ Pair ``(N, G)``, where:
596
+
597
+ ``N`` is a :py:class:`~.Module` representing the kernel of the map
598
+ ``a |--> a**p - a`` on ``(O/pO)/I``, guaranteed to be a module with
599
+ unity.
600
+
601
+ ``G`` is a :py:class:`~.Module` representing a basis for the separable
602
+ algebra ``A = O/I`` (see Cohen).
603
+
604
+ """
605
+ W = I.matrix
606
+ n, r = W.shape
607
+ # Want to take the Fp-basis given by the columns of I, adjoin (1, 0, ..., 0)
608
+ # (which we know is not already in there since I is a basis for a prime ideal)
609
+ # and then supplement this with additional columns to make an invertible n x n
610
+ # matrix. This will then represent a full basis for ZK, whose first r columns
611
+ # are pullbacks of the basis for I.
612
+ if r == 0:
613
+ B = W.eye(n, ZZ)
614
+ else:
615
+ B = W.hstack(W.eye(n, ZZ)[:, 0])
616
+ if B.shape[1] < n:
617
+ B = supplement_a_subspace(B.convert_to(FF(p))).convert_to(ZZ)
618
+
619
+ G = ZK.submodule_from_matrix(B)
620
+ # Must compute G's multiplication table _before_ discarding the first r
621
+ # columns. (See Step 9 in Alg 6.2.9 in Cohen, where the betas are actually
622
+ # needed in order to represent each product of gammas. However, once we've
623
+ # found the representations, then we can ignore the betas.)
624
+ G.compute_mult_tab()
625
+ G = G.discard_before(r)
626
+
627
+ phi = ModuleEndomorphism(G, lambda x: x**p - x)
628
+ N = phi.kernel(modulus=p)
629
+ assert N.starts_with_unity()
630
+ return N, G
631
+
632
+
633
+ def _prime_decomp_maximal_ideal(I, p, ZK):
634
+ r"""
635
+ We have reached the case where we have a maximal (hence prime) ideal *I*,
636
+ which we know because the quotient ``O/I`` is a field.
637
+
638
+ Parameters
639
+ ==========
640
+
641
+ I : :py:class:`~.Module`
642
+ An ideal of ``O/pO``.
643
+ p : int
644
+ The rational prime being factored.
645
+ ZK : :py:class:`~.Submodule`
646
+ The maximal order.
647
+
648
+ Returns
649
+ =======
650
+
651
+ :py:class:`~.PrimeIdeal` instance representing this prime
652
+
653
+ """
654
+ m, n = I.matrix.shape
655
+ f = m - n
656
+ G = ZK.matrix * I.matrix
657
+ gens = [ZK.parent(G[:, j], denom=ZK.denom) for j in range(G.shape[1])]
658
+ alpha = _two_elt_rep(gens, ZK, p, f=f)
659
+ return PrimeIdeal(ZK, p, alpha, f)
660
+
661
+
662
+ def _prime_decomp_split_ideal(I, p, N, G, ZK):
663
+ r"""
664
+ Perform the step in the prime decomposition algorithm where we have determined
665
+ the quotient ``ZK/I`` is _not_ a field, and we want to perform a non-trivial
666
+ factorization of *I* by locating an idempotent element of ``ZK/I``.
667
+ """
668
+ assert I.parent == ZK and G.parent is ZK and N.parent is G
669
+ # Since ZK/I is not a field, the kernel computed in the previous step contains
670
+ # more than just the prime field Fp, and our basis N for the nullspace therefore
671
+ # contains at least a second column (which represents an element outside Fp).
672
+ # Let alpha be such an element:
673
+ alpha = N(1).to_parent()
674
+ assert alpha.module is G
675
+
676
+ alpha_powers = []
677
+ m = find_min_poly(alpha, FF(p), powers=alpha_powers)
678
+ # TODO (future work):
679
+ # We don't actually need full factorization, so might use a faster method
680
+ # to just break off a single non-constant factor m1?
681
+ lc, fl = m.factor_list()
682
+ m1 = fl[0][0]
683
+ m2 = m.quo(m1)
684
+ U, V, g = m1.gcdex(m2)
685
+ # Sanity check: theory says m is squarefree, so m1, m2 should be coprime:
686
+ assert g == 1
687
+ E = list(reversed(Poly(U * m1, domain=ZZ).rep.to_list()))
688
+ eps1 = sum(E[i]*alpha_powers[i] for i in range(len(E)))
689
+ eps2 = 1 - eps1
690
+ idemps = [eps1, eps2]
691
+ factors = []
692
+ for eps in idemps:
693
+ e = eps.to_parent()
694
+ assert e.module is ZK
695
+ D = I.matrix.convert_to(FF(p)).hstack(*[
696
+ (e * om).column(domain=FF(p)) for om in ZK.basis_elements()
697
+ ])
698
+ W = D.columnspace().convert_to(ZZ)
699
+ H = ZK.submodule_from_matrix(W)
700
+ factors.append(H)
701
+ return factors
702
+
703
+
704
+ @public
705
+ def prime_decomp(p, T=None, ZK=None, dK=None, radical=None):
706
+ r"""
707
+ Compute the decomposition of rational prime *p* in a number field.
708
+
709
+ Explanation
710
+ ===========
711
+
712
+ Ordinarily this should be accessed through the
713
+ :py:meth:`~.AlgebraicField.primes_above` method of an
714
+ :py:class:`~.AlgebraicField`.
715
+
716
+ Examples
717
+ ========
718
+
719
+ >>> from sympy import Poly, QQ
720
+ >>> from sympy.abc import x, theta
721
+ >>> T = Poly(x ** 3 + x ** 2 - 2 * x + 8)
722
+ >>> K = QQ.algebraic_field((T, theta))
723
+ >>> print(K.primes_above(2))
724
+ [[ (2, x**2 + 1) e=1, f=1 ], [ (2, (x**2 + 3*x + 2)/2) e=1, f=1 ],
725
+ [ (2, (3*x**2 + 3*x)/2) e=1, f=1 ]]
726
+
727
+ Parameters
728
+ ==========
729
+
730
+ p : int
731
+ The rational prime whose decomposition is desired.
732
+
733
+ T : :py:class:`~.Poly`, optional
734
+ Monic irreducible polynomial defining the number field $K$ in which to
735
+ factor. NOTE: at least one of *T* or *ZK* must be provided.
736
+
737
+ ZK : :py:class:`~.Submodule`, optional
738
+ The maximal order for $K$, if already known.
739
+ NOTE: at least one of *T* or *ZK* must be provided.
740
+
741
+ dK : int, optional
742
+ The discriminant of the field $K$, if already known.
743
+
744
+ radical : :py:class:`~.Submodule`, optional
745
+ The nilradical mod *p* in the integers of $K$, if already known.
746
+
747
+ Returns
748
+ =======
749
+
750
+ List of :py:class:`~.PrimeIdeal` instances.
751
+
752
+ References
753
+ ==========
754
+
755
+ .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
756
+ (See Algorithm 6.2.9.)
757
+
758
+ """
759
+ if T is None and ZK is None:
760
+ raise ValueError('At least one of T or ZK must be provided.')
761
+ if ZK is not None:
762
+ _check_formal_conditions_for_maximal_order(ZK)
763
+ if T is None:
764
+ T = ZK.parent.T
765
+ radicals = {}
766
+ if dK is None or ZK is None:
767
+ ZK, dK = round_two(T, radicals=radicals)
768
+ dT = T.discriminant()
769
+ f_squared = dT // dK
770
+ if f_squared % p != 0:
771
+ return _prime_decomp_easy_case(p, ZK)
772
+ radical = radical or radicals.get(p) or nilradical_mod_p(ZK, p)
773
+ stack = [radical]
774
+ primes = []
775
+ while stack:
776
+ I = stack.pop()
777
+ N, G = _prime_decomp_compute_kernel(I, p, ZK)
778
+ if N.n == 1:
779
+ P = _prime_decomp_maximal_ideal(I, p, ZK)
780
+ primes.append(P)
781
+ else:
782
+ I1, I2 = _prime_decomp_split_ideal(I, p, N, G, ZK)
783
+ stack.extend([I1, I2])
784
+ return primes
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/resolvent_lookup.py ADDED
@@ -0,0 +1,456 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Lookup table for Galois resolvents for polys of degree 4 through 6. """
2
+ # This table was generated by a call to
3
+ # `sympy.polys.numberfields.galois_resolvents.generate_lambda_lookup()`.
4
+ # The entire job took 543.23s.
5
+ # Of this, Case (6, 1) took 539.03s.
6
+ # The final polynomial of Case (6, 1) alone took 455.09s.
7
+ resolvent_coeff_lambdas = {
8
+ (4, 0): [
9
+ lambda s1, s2, s3, s4: (-2*s1*s2 + 6*s3),
10
+ lambda s1, s2, s3, s4: (2*s1**3*s3 + s1**2*s2**2 + s1**2*s4 - 17*s1*s2*s3 + 2*s2**3 - 8*s2*s4 + 24*s3**2),
11
+ lambda s1, s2, s3, s4: (-2*s1**5*s4 - 2*s1**4*s2*s3 + 10*s1**3*s2*s4 + 8*s1**3*s3**2 + 10*s1**2*s2**2*s3 -
12
+ 12*s1**2*s3*s4 - 2*s1*s2**4 - 54*s1*s2*s3**2 + 32*s1*s4**2 + 8*s2**3*s3 - 32*s2*s3*s4
13
+ + 56*s3**3),
14
+ lambda s1, s2, s3, s4: (2*s1**6*s2*s4 + s1**6*s3**2 - 5*s1**5*s3*s4 - 11*s1**4*s2**2*s4 - 13*s1**4*s2*s3**2
15
+ + 7*s1**4*s4**2 + 3*s1**3*s2**3*s3 + 30*s1**3*s2*s3*s4 + 22*s1**3*s3**3 + 10*s1**2*s2**3*s4
16
+ + 33*s1**2*s2**2*s3**2 - 72*s1**2*s2*s4**2 - 36*s1**2*s3**2*s4 - 13*s1*s2**4*s3 +
17
+ 48*s1*s2**2*s3*s4 - 116*s1*s2*s3**3 + 144*s1*s3*s4**2 + s2**6 - 12*s2**4*s4 + 22*s2**3*s3**2
18
+ + 48*s2**2*s4**2 - 120*s2*s3**2*s4 + 96*s3**4 - 64*s4**3),
19
+ lambda s1, s2, s3, s4: (-2*s1**8*s3*s4 - s1**7*s4**2 + 22*s1**6*s2*s3*s4 + 2*s1**6*s3**3 - 2*s1**5*s2**3*s4
20
+ - s1**5*s2**2*s3**2 - 29*s1**5*s3**2*s4 - 60*s1**4*s2**2*s3*s4 - 19*s1**4*s2*s3**3
21
+ + 38*s1**4*s3*s4**2 + 9*s1**3*s2**4*s4 + 10*s1**3*s2**3*s3**2 + 24*s1**3*s2**2*s4**2
22
+ + 134*s1**3*s2*s3**2*s4 + 28*s1**3*s3**4 + 16*s1**3*s4**3 - s1**2*s2**5*s3 - 4*s1**2*s2**3*s3*s4
23
+ + 34*s1**2*s2**2*s3**3 - 288*s1**2*s2*s3*s4**2 - 104*s1**2*s3**3*s4 - 19*s1*s2**4*s3**2
24
+ + 120*s1*s2**2*s3**2*s4 - 128*s1*s2*s3**4 + 336*s1*s3**2*s4**2 + 2*s2**6*s3 - 24*s2**4*s3*s4
25
+ + 28*s2**3*s3**3 + 96*s2**2*s3*s4**2 - 176*s2*s3**3*s4 + 96*s3**5 - 128*s3*s4**3),
26
+ lambda s1, s2, s3, s4: (s1**10*s4**2 - 11*s1**8*s2*s4**2 - 2*s1**8*s3**2*s4 + s1**7*s2**2*s3*s4 + 15*s1**7*s3*s4**2
27
+ + 45*s1**6*s2**2*s4**2 + 17*s1**6*s2*s3**2*s4 + s1**6*s3**4 - 5*s1**6*s4**3 - 12*s1**5*s2**3*s3*s4
28
+ - 133*s1**5*s2*s3*s4**2 - 22*s1**5*s3**3*s4 + s1**4*s2**5*s4 - 76*s1**4*s2**3*s4**2
29
+ - 6*s1**4*s2**2*s3**2*s4 - 12*s1**4*s2*s3**4 + 32*s1**4*s2*s4**3 + 128*s1**4*s3**2*s4**2
30
+ + 29*s1**3*s2**4*s3*s4 + 2*s1**3*s2**3*s3**3 + 344*s1**3*s2**2*s3*s4**2 + 48*s1**3*s2*s3**3*s4
31
+ + 16*s1**3*s3**5 - 48*s1**3*s3*s4**3 - 4*s1**2*s2**6*s4 + 32*s1**2*s2**4*s4**2 - 134*s1**2*s2**3*s3**2*s4
32
+ + 36*s1**2*s2**2*s3**4 - 64*s1**2*s2**2*s4**3 - 648*s1**2*s2*s3**2*s4**2 - 48*s1**2*s3**4*s4
33
+ + 16*s1*s2**5*s3*s4 - 12*s1*s2**4*s3**3 - 128*s1*s2**3*s3*s4**2 + 296*s1*s2**2*s3**3*s4
34
+ - 96*s1*s2*s3**5 + 256*s1*s2*s3*s4**3 + 416*s1*s3**3*s4**2 + s2**6*s3**2 - 28*s2**4*s3**2*s4
35
+ + 16*s2**3*s3**4 + 176*s2**2*s3**2*s4**2 - 224*s2*s3**4*s4 + 64*s3**6 - 320*s3**2*s4**3)
36
+ ],
37
+ (4, 1): [
38
+ lambda s1, s2, s3, s4: (-s2),
39
+ lambda s1, s2, s3, s4: (s1*s3 - 4*s4),
40
+ lambda s1, s2, s3, s4: (-s1**2*s4 + 4*s2*s4 - s3**2)
41
+ ],
42
+ (5, 1): [
43
+ lambda s1, s2, s3, s4, s5: (-2*s1*s3 + 8*s4),
44
+ lambda s1, s2, s3, s4, s5: (-8*s1**3*s5 + 2*s1**2*s2*s4 + s1**2*s3**2 + 30*s1*s2*s5 - 14*s1*s3*s4 - 6*s2**2*s4
45
+ + 2*s2*s3**2 - 50*s3*s5 + 40*s4**2),
46
+ lambda s1, s2, s3, s4, s5: (16*s1**4*s3*s5 - 2*s1**4*s4**2 - 2*s1**3*s2**2*s5 - 2*s1**3*s2*s3*s4 - 44*s1**3*s4*s5
47
+ - 66*s1**2*s2*s3*s5 + 21*s1**2*s2*s4**2 + 6*s1**2*s3**2*s4 - 50*s1**2*s5**2 + 9*s1*s2**3*s5
48
+ + 5*s1*s2**2*s3*s4 - 2*s1*s2*s3**3 + 190*s1*s2*s4*s5 + 120*s1*s3**2*s5 - 80*s1*s3*s4**2
49
+ - 15*s2**2*s3*s5 - 40*s2**2*s4**2 + 21*s2*s3**2*s4 + 125*s2*s5**2 - 2*s3**4 - 400*s3*s4*s5
50
+ + 160*s4**3),
51
+ lambda s1, s2, s3, s4, s5: (16*s1**6*s5**2 - 8*s1**5*s2*s4*s5 - 8*s1**5*s3**2*s5 + 2*s1**5*s3*s4**2 + 2*s1**4*s2**2*s3*s5
52
+ + s1**4*s2**2*s4**2 - 120*s1**4*s2*s5**2 + 68*s1**4*s3*s4*s5 - 8*s1**4*s4**3 + 46*s1**3*s2**2*s4*s5
53
+ + 28*s1**3*s2*s3**2*s5 - 19*s1**3*s2*s3*s4**2 + 250*s1**3*s3*s5**2 - 144*s1**3*s4**2*s5
54
+ - 9*s1**2*s2**3*s3*s5 - 6*s1**2*s2**3*s4**2 + 3*s1**2*s2**2*s3**2*s4 + 225*s1**2*s2**2*s5**2
55
+ - 354*s1**2*s2*s3*s4*s5 + 76*s1**2*s2*s4**3 - 70*s1**2*s3**3*s5 + 41*s1**2*s3**2*s4**2
56
+ - 200*s1**2*s4*s5**2 - 54*s1*s2**3*s4*s5 + 45*s1*s2**2*s3**2*s5 + 30*s1*s2**2*s3*s4**2
57
+ - 19*s1*s2*s3**3*s4 - 875*s1*s2*s3*s5**2 + 640*s1*s2*s4**2*s5 + 2*s1*s3**5 + 630*s1*s3**2*s4*s5
58
+ - 264*s1*s3*s4**3 + 9*s2**4*s4**2 - 6*s2**3*s3**2*s4 + s2**2*s3**4 + 90*s2**2*s3*s4*s5
59
+ - 136*s2**2*s4**3 - 50*s2*s3**3*s5 + 76*s2*s3**2*s4**2 + 500*s2*s4*s5**2 - 8*s3**4*s4
60
+ + 625*s3**2*s5**2 - 1400*s3*s4**2*s5 + 400*s4**4),
61
+ lambda s1, s2, s3, s4, s5: (-32*s1**7*s3*s5**2 + 8*s1**7*s4**2*s5 + 8*s1**6*s2**2*s5**2 + 8*s1**6*s2*s3*s4*s5
62
+ - 2*s1**6*s2*s4**3 + 48*s1**6*s4*s5**2 - 2*s1**5*s2**3*s4*s5 + 264*s1**5*s2*s3*s5**2
63
+ - 94*s1**5*s2*s4**2*s5 - 24*s1**5*s3**2*s4*s5 + 6*s1**5*s3*s4**3 - 56*s1**5*s5**3
64
+ - 66*s1**4*s2**3*s5**2 - 50*s1**4*s2**2*s3*s4*s5 + 19*s1**4*s2**2*s4**3 + 8*s1**4*s2*s3**3*s5
65
+ - 2*s1**4*s2*s3**2*s4**2 - 318*s1**4*s2*s4*s5**2 - 352*s1**4*s3**2*s5**2 + 166*s1**4*s3*s4**2*s5
66
+ + 3*s1**4*s4**4 + 15*s1**3*s2**4*s4*s5 - 2*s1**3*s2**3*s3**2*s5 - s1**3*s2**3*s3*s4**2
67
+ - 574*s1**3*s2**2*s3*s5**2 + 347*s1**3*s2**2*s4**2*s5 + 194*s1**3*s2*s3**2*s4*s5 -
68
+ 89*s1**3*s2*s3*s4**3 + 350*s1**3*s2*s5**3 - 8*s1**3*s3**4*s5 + 4*s1**3*s3**3*s4**2
69
+ + 1090*s1**3*s3*s4*s5**2 - 364*s1**3*s4**3*s5 + 162*s1**2*s2**4*s5**2 + 33*s1**2*s2**3*s3*s4*s5
70
+ - 51*s1**2*s2**3*s4**3 - 32*s1**2*s2**2*s3**3*s5 + 28*s1**2*s2**2*s3**2*s4**2 + 305*s1**2*s2**2*s4*s5**2
71
+ - 2*s1**2*s2*s3**4*s4 + 1340*s1**2*s2*s3**2*s5**2 - 901*s1**2*s2*s3*s4**2*s5 + 76*s1**2*s2*s4**4
72
+ - 234*s1**2*s3**3*s4*s5 + 102*s1**2*s3**2*s4**3 - 750*s1**2*s3*s5**3 - 550*s1**2*s4**2*s5**2
73
+ - 27*s1*s2**5*s4*s5 + 9*s1*s2**4*s3**2*s5 + 3*s1*s2**4*s3*s4**2 - s1*s2**3*s3**3*s4
74
+ + 180*s1*s2**3*s3*s5**2 - 366*s1*s2**3*s4**2*s5 - 231*s1*s2**2*s3**2*s4*s5 + 212*s1*s2**2*s3*s4**3
75
+ - 375*s1*s2**2*s5**3 + 112*s1*s2*s3**4*s5 - 89*s1*s2*s3**3*s4**2 - 3075*s1*s2*s3*s4*s5**2
76
+ + 1640*s1*s2*s4**3*s5 + 6*s1*s3**5*s4 - 850*s1*s3**3*s5**2 + 1220*s1*s3**2*s4**2*s5
77
+ - 384*s1*s3*s4**4 + 2500*s1*s4*s5**3 - 108*s2**5*s5**2 + 117*s2**4*s3*s4*s5 + 32*s2**4*s4**3
78
+ - 31*s2**3*s3**3*s5 - 51*s2**3*s3**2*s4**2 + 525*s2**3*s4*s5**2 + 19*s2**2*s3**4*s4
79
+ - 325*s2**2*s3**2*s5**2 + 260*s2**2*s3*s4**2*s5 - 256*s2**2*s4**4 - 2*s2*s3**6 + 105*s2*s3**3*s4*s5
80
+ + 76*s2*s3**2*s4**3 + 625*s2*s3*s5**3 - 500*s2*s4**2*s5**2 - 58*s3**5*s5 + 3*s3**4*s4**2
81
+ + 2750*s3**2*s4*s5**2 - 2400*s3*s4**3*s5 + 512*s4**5 - 3125*s5**4),
82
+ lambda s1, s2, s3, s4, s5: (16*s1**8*s3**2*s5**2 - 8*s1**8*s3*s4**2*s5 + s1**8*s4**4 - 8*s1**7*s2**2*s3*s5**2
83
+ + 2*s1**7*s2**2*s4**2*s5 - 48*s1**7*s3*s4*s5**2 + 12*s1**7*s4**3*s5 + s1**6*s2**4*s5**2
84
+ + 12*s1**6*s2**2*s4*s5**2 - 144*s1**6*s2*s3**2*s5**2 + 88*s1**6*s2*s3*s4**2*s5 - 13*s1**6*s2*s4**4
85
+ + 56*s1**6*s3*s5**3 + 86*s1**6*s4**2*s5**2 + 72*s1**5*s2**3*s3*s5**2 - 22*s1**5*s2**3*s4**2*s5
86
+ - 4*s1**5*s2**2*s3**2*s4*s5 + s1**5*s2**2*s3*s4**3 - 14*s1**5*s2**2*s5**3 + 304*s1**5*s2*s3*s4*s5**2
87
+ - 148*s1**5*s2*s4**3*s5 + 152*s1**5*s3**3*s5**2 - 54*s1**5*s3**2*s4**2*s5 + 5*s1**5*s3*s4**4
88
+ - 468*s1**5*s4*s5**3 - 9*s1**4*s2**5*s5**2 + s1**4*s2**4*s3*s4*s5 - 76*s1**4*s2**3*s4*s5**2
89
+ + 370*s1**4*s2**2*s3**2*s5**2 - 287*s1**4*s2**2*s3*s4**2*s5 + 65*s1**4*s2**2*s4**4
90
+ - 28*s1**4*s2*s3**3*s4*s5 + 5*s1**4*s2*s3**2*s4**3 - 200*s1**4*s2*s3*s5**3 - 294*s1**4*s2*s4**2*s5**2
91
+ + 8*s1**4*s3**5*s5 - 2*s1**4*s3**4*s4**2 - 676*s1**4*s3**2*s4*s5**2 + 180*s1**4*s3*s4**3*s5
92
+ + 17*s1**4*s4**5 + 625*s1**4*s5**4 - 210*s1**3*s2**4*s3*s5**2 + 76*s1**3*s2**4*s4**2*s5
93
+ + 43*s1**3*s2**3*s3**2*s4*s5 - 15*s1**3*s2**3*s3*s4**3 + 50*s1**3*s2**3*s5**3 - 6*s1**3*s2**2*s3**4*s5
94
+ + 2*s1**3*s2**2*s3**3*s4**2 - 397*s1**3*s2**2*s3*s4*s5**2 + 514*s1**3*s2**2*s4**3*s5
95
+ - 700*s1**3*s2*s3**3*s5**2 + 447*s1**3*s2*s3**2*s4**2*s5 - 118*s1**3*s2*s3*s4**4 +
96
+ 2300*s1**3*s2*s4*s5**3 - 12*s1**3*s3**4*s4*s5 + 6*s1**3*s3**3*s4**3 + 250*s1**3*s3**2*s5**3
97
+ + 1470*s1**3*s3*s4**2*s5**2 - 276*s1**3*s4**4*s5 + 27*s1**2*s2**6*s5**2 - 9*s1**2*s2**5*s3*s4*s5
98
+ + s1**2*s2**5*s4**3 + s1**2*s2**4*s3**3*s5 + 141*s1**2*s2**4*s4*s5**2 - 185*s1**2*s2**3*s3**2*s5**2
99
+ + 168*s1**2*s2**3*s3*s4**2*s5 - 128*s1**2*s2**3*s4**4 + 93*s1**2*s2**2*s3**3*s4*s5
100
+ + 19*s1**2*s2**2*s3**2*s4**3 - 125*s1**2*s2**2*s3*s5**3 - 610*s1**2*s2**2*s4**2*s5**2
101
+ - 36*s1**2*s2*s3**5*s5 + 5*s1**2*s2*s3**4*s4**2 + 1995*s1**2*s2*s3**2*s4*s5**2 - 1174*s1**2*s2*s3*s4**3*s5
102
+ - 16*s1**2*s2*s4**5 - 3125*s1**2*s2*s5**4 + 375*s1**2*s3**4*s5**2 - 172*s1**2*s3**3*s4**2*s5
103
+ + 82*s1**2*s3**2*s4**4 - 3500*s1**2*s3*s4*s5**3 - 1450*s1**2*s4**3*s5**2 + 198*s1*s2**5*s3*s5**2
104
+ - 78*s1*s2**5*s4**2*s5 - 95*s1*s2**4*s3**2*s4*s5 + 44*s1*s2**4*s3*s4**3 + 25*s1*s2**3*s3**4*s5
105
+ - 15*s1*s2**3*s3**3*s4**2 + 15*s1*s2**3*s3*s4*s5**2 - 384*s1*s2**3*s4**3*s5 + s1*s2**2*s3**5*s4
106
+ + 525*s1*s2**2*s3**3*s5**2 - 528*s1*s2**2*s3**2*s4**2*s5 + 384*s1*s2**2*s3*s4**4 -
107
+ 1750*s1*s2**2*s4*s5**3 - 29*s1*s2*s3**4*s4*s5 - 118*s1*s2*s3**3*s4**3 + 625*s1*s2*s3**2*s5**3
108
+ - 850*s1*s2*s3*s4**2*s5**2 + 1760*s1*s2*s4**4*s5 + 38*s1*s3**6*s5 + 5*s1*s3**5*s4**2
109
+ - 2050*s1*s3**3*s4*s5**2 + 780*s1*s3**2*s4**3*s5 - 192*s1*s3*s4**5 + 3125*s1*s3*s5**4
110
+ + 7500*s1*s4**2*s5**3 - 27*s2**7*s5**2 + 18*s2**6*s3*s4*s5 - 4*s2**6*s4**3 - 4*s2**5*s3**3*s5
111
+ + s2**5*s3**2*s4**2 - 99*s2**5*s4*s5**2 - 150*s2**4*s3**2*s5**2 + 196*s2**4*s3*s4**2*s5
112
+ + 48*s2**4*s4**4 + 12*s2**3*s3**3*s4*s5 - 128*s2**3*s3**2*s4**3 + 1200*s2**3*s4**2*s5**2
113
+ - 12*s2**2*s3**5*s5 + 65*s2**2*s3**4*s4**2 - 725*s2**2*s3**2*s4*s5**2 - 160*s2**2*s3*s4**3*s5
114
+ - 192*s2**2*s4**5 + 3125*s2**2*s5**4 - 13*s2*s3**6*s4 - 125*s2*s3**4*s5**2 + 590*s2*s3**3*s4**2*s5
115
+ - 16*s2*s3**2*s4**4 - 1250*s2*s3*s4*s5**3 - 2000*s2*s4**3*s5**2 + s3**8 - 124*s3**5*s4*s5
116
+ + 17*s3**4*s4**3 + 3250*s3**2*s4**2*s5**2 - 1600*s3*s4**4*s5 + 256*s4**6 - 9375*s4*s5**4)
117
+ ],
118
+ (6, 1): [
119
+ lambda s1, s2, s3, s4, s5, s6: (8*s1*s5 - 2*s2*s4 - 18*s6),
120
+ lambda s1, s2, s3, s4, s5, s6: (-50*s1**2*s4*s6 + 40*s1**2*s5**2 + 30*s1*s2*s3*s6 - 14*s1*s2*s4*s5 - 6*s1*s3**2*s5
121
+ + 2*s1*s3*s4**2 - 30*s1*s5*s6 - 8*s2**3*s6 + 2*s2**2*s3*s5 + s2**2*s4**2 + 114*s2*s4*s6
122
+ - 50*s2*s5**2 - 54*s3**2*s6 + 30*s3*s4*s5 - 8*s4**3 - 135*s6**2),
123
+ lambda s1, s2, s3, s4, s5, s6: (125*s1**3*s3*s6**2 - 400*s1**3*s4*s5*s6 + 160*s1**3*s5**3 - 50*s1**2*s2**2*s6**2 +
124
+ 190*s1**2*s2*s3*s5*s6 + 120*s1**2*s2*s4**2*s6 - 80*s1**2*s2*s4*s5**2 - 15*s1**2*s3**2*s4*s6
125
+ - 40*s1**2*s3**2*s5**2 + 21*s1**2*s3*s4**2*s5 - 2*s1**2*s4**4 + 900*s1**2*s4*s6**2
126
+ - 80*s1**2*s5**2*s6 - 44*s1*s2**3*s5*s6 - 66*s1*s2**2*s3*s4*s6 + 21*s1*s2**2*s3*s5**2
127
+ + 6*s1*s2**2*s4**2*s5 + 9*s1*s2*s3**3*s6 + 5*s1*s2*s3**2*s4*s5 - 2*s1*s2*s3*s4**3
128
+ - 990*s1*s2*s3*s6**2 + 920*s1*s2*s4*s5*s6 - 400*s1*s2*s5**3 - 135*s1*s3**2*s5*s6 -
129
+ 126*s1*s3*s4**2*s6 + 190*s1*s3*s4*s5**2 - 44*s1*s4**3*s5 - 2070*s1*s5*s6**2 + 16*s2**4*s4*s6
130
+ - 2*s2**4*s5**2 - 2*s2**3*s3**2*s6 - 2*s2**3*s3*s4*s5 + 304*s2**3*s6**2 - 126*s2**2*s3*s5*s6
131
+ - 232*s2**2*s4**2*s6 + 120*s2**2*s4*s5**2 + 198*s2*s3**2*s4*s6 - 15*s2*s3**2*s5**2
132
+ - 66*s2*s3*s4**2*s5 + 16*s2*s4**4 - 1440*s2*s4*s6**2 + 900*s2*s5**2*s6 - 27*s3**4*s6
133
+ + 9*s3**3*s4*s5 - 2*s3**2*s4**3 + 1350*s3**2*s6**2 - 990*s3*s4*s5*s6 + 125*s3*s5**3
134
+ + 304*s4**3*s6 - 50*s4**2*s5**2 + 3240*s6**3),
135
+ lambda s1, s2, s3, s4, s5, s6: (500*s1**4*s3*s5*s6**2 + 625*s1**4*s4**2*s6**2 - 1400*s1**4*s4*s5**2*s6 + 400*s1**4*s5**4
136
+ - 200*s1**3*s2**2*s5*s6**2 - 875*s1**3*s2*s3*s4*s6**2 + 640*s1**3*s2*s3*s5**2*s6 +
137
+ 630*s1**3*s2*s4**2*s5*s6 - 264*s1**3*s2*s4*s5**3 + 90*s1**3*s3**2*s4*s5*s6 - 136*s1**3*s3**2*s5**3
138
+ - 50*s1**3*s3*s4**3*s6 + 76*s1**3*s3*s4**2*s5**2 - 1125*s1**3*s3*s6**3 - 8*s1**3*s4**4*s5
139
+ + 2550*s1**3*s4*s5*s6**2 - 200*s1**3*s5**3*s6 + 250*s1**2*s2**3*s4*s6**2 - 144*s1**2*s2**3*s5**2*s6
140
+ + 225*s1**2*s2**2*s3**2*s6**2 - 354*s1**2*s2**2*s3*s4*s5*s6 + 76*s1**2*s2**2*s3*s5**3
141
+ - 70*s1**2*s2**2*s4**3*s6 + 41*s1**2*s2**2*s4**2*s5**2 + 450*s1**2*s2**2*s6**3 - 54*s1**2*s2*s3**3*s5*s6
142
+ + 45*s1**2*s2*s3**2*s4**2*s6 + 30*s1**2*s2*s3**2*s4*s5**2 - 19*s1**2*s2*s3*s4**3*s5
143
+ - 2880*s1**2*s2*s3*s5*s6**2 + 2*s1**2*s2*s4**5 - 3480*s1**2*s2*s4**2*s6**2 + 4692*s1**2*s2*s4*s5**2*s6
144
+ - 1400*s1**2*s2*s5**4 + 9*s1**2*s3**4*s5**2 - 6*s1**2*s3**3*s4**2*s5 + s1**2*s3**2*s4**4
145
+ + 1485*s1**2*s3**2*s4*s6**2 - 522*s1**2*s3**2*s5**2*s6 - 1257*s1**2*s3*s4**2*s5*s6
146
+ + 640*s1**2*s3*s4*s5**3 + 218*s1**2*s4**4*s6 - 144*s1**2*s4**3*s5**2 + 1350*s1**2*s4*s6**3
147
+ - 5175*s1**2*s5**2*s6**2 - 120*s1*s2**4*s3*s6**2 + 68*s1*s2**4*s4*s5*s6 - 8*s1*s2**4*s5**3
148
+ + 46*s1*s2**3*s3**2*s5*s6 + 28*s1*s2**3*s3*s4**2*s6 - 19*s1*s2**3*s3*s4*s5**2 + 868*s1*s2**3*s5*s6**2
149
+ - 9*s1*s2**2*s3**3*s4*s6 - 6*s1*s2**2*s3**3*s5**2 + 3*s1*s2**2*s3**2*s4**2*s5 + 2484*s1*s2**2*s3*s4*s6**2
150
+ - 1257*s1*s2**2*s3*s5**2*s6 - 1356*s1*s2**2*s4**2*s5*s6 + 630*s1*s2**2*s4*s5**3 -
151
+ 891*s1*s2*s3**3*s6**2 + 882*s1*s2*s3**2*s4*s5*s6 + 90*s1*s2*s3**2*s5**3 + 84*s1*s2*s3*s4**3*s6
152
+ - 354*s1*s2*s3*s4**2*s5**2 + 3240*s1*s2*s3*s6**3 + 68*s1*s2*s4**4*s5 - 4392*s1*s2*s4*s5*s6**2
153
+ + 2550*s1*s2*s5**3*s6 + 54*s1*s3**4*s5*s6 - 54*s1*s3**3*s4**2*s6 - 54*s1*s3**3*s4*s5**2
154
+ + 46*s1*s3**2*s4**3*s5 + 2727*s1*s3**2*s5*s6**2 - 8*s1*s3*s4**5 + 756*s1*s3*s4**2*s6**2
155
+ - 2880*s1*s3*s4*s5**2*s6 + 500*s1*s3*s5**4 + 868*s1*s4**3*s5*s6 - 200*s1*s4**2*s5**3
156
+ + 8100*s1*s5*s6**3 + 16*s2**6*s6**2 - 8*s2**5*s3*s5*s6 - 8*s2**5*s4**2*s6 + 2*s2**5*s4*s5**2
157
+ + 2*s2**4*s3**2*s4*s6 + s2**4*s3**2*s5**2 - 688*s2**4*s4*s6**2 + 218*s2**4*s5**2*s6
158
+ + 234*s2**3*s3**2*s6**2 + 84*s2**3*s3*s4*s5*s6 - 50*s2**3*s3*s5**3 + 168*s2**3*s4**3*s6
159
+ - 70*s2**3*s4**2*s5**2 - 1224*s2**3*s6**3 - 54*s2**2*s3**3*s5*s6 - 144*s2**2*s3**2*s4**2*s6
160
+ + 45*s2**2*s3**2*s4*s5**2 + 28*s2**2*s3*s4**3*s5 + 756*s2**2*s3*s5*s6**2 - 8*s2**2*s4**5
161
+ + 4320*s2**2*s4**2*s6**2 - 3480*s2**2*s4*s5**2*s6 + 625*s2**2*s5**4 + 27*s2*s3**4*s4*s6
162
+ - 9*s2*s3**3*s4**2*s5 + 2*s2*s3**2*s4**4 - 4752*s2*s3**2*s4*s6**2 + 1485*s2*s3**2*s5**2*s6
163
+ + 2484*s2*s3*s4**2*s5*s6 - 875*s2*s3*s4*s5**3 - 688*s2*s4**4*s6 + 250*s2*s4**3*s5**2
164
+ - 4536*s2*s4*s6**3 + 1350*s2*s5**2*s6**2 + 972*s3**4*s6**2 - 891*s3**3*s4*s5*s6 +
165
+ 234*s3**2*s4**3*s6 + 225*s3**2*s4**2*s5**2 - 1944*s3**2*s6**3 - 120*s3*s4**4*s5 +
166
+ 3240*s3*s4*s5*s6**2 - 1125*s3*s5**3*s6 + 16*s4**6 - 1224*s4**3*s6**2 + 450*s4**2*s5**2*s6),
167
+ lambda s1, s2, s3, s4, s5, s6: (-3125*s1**6*s6**4 + 2500*s1**5*s2*s5*s6**3 + 625*s1**5*s3*s4*s6**3 - 500*s1**5*s3*s5**2*s6**2
168
+ + 2750*s1**5*s4**2*s5*s6**2 - 2400*s1**5*s4*s5**3*s6 + 512*s1**5*s5**5 - 750*s1**4*s2**2*s4*s6**3
169
+ - 550*s1**4*s2**2*s5**2*s6**2 - 375*s1**4*s2*s3**2*s6**3 - 3075*s1**4*s2*s3*s4*s5*s6**2
170
+ + 1640*s1**4*s2*s3*s5**3*s6 - 850*s1**4*s2*s4**3*s6**2 + 1220*s1**4*s2*s4**2*s5**2*s6
171
+ - 384*s1**4*s2*s4*s5**4 + 22500*s1**4*s2*s6**4 + 525*s1**4*s3**3*s5*s6**2 - 325*s1**4*s3**2*s4**2*s6**2
172
+ + 260*s1**4*s3**2*s4*s5**2*s6 - 256*s1**4*s3**2*s5**4 + 105*s1**4*s3*s4**3*s5*s6 +
173
+ 76*s1**4*s3*s4**2*s5**3 + 375*s1**4*s3*s5*s6**3 - 58*s1**4*s4**5*s6 + 3*s1**4*s4**4*s5**2
174
+ - 12750*s1**4*s4**2*s6**3 + 3700*s1**4*s4*s5**2*s6**2 + 640*s1**4*s5**4*s6 + 350*s1**3*s2**3*s3*s6**3
175
+ + 1090*s1**3*s2**3*s4*s5*s6**2 - 364*s1**3*s2**3*s5**3*s6 + 305*s1**3*s2**2*s3**2*s5*s6**2
176
+ + 1340*s1**3*s2**2*s3*s4**2*s6**2 - 901*s1**3*s2**2*s3*s4*s5**2*s6 + 76*s1**3*s2**2*s3*s5**4
177
+ - 234*s1**3*s2**2*s4**3*s5*s6 + 102*s1**3*s2**2*s4**2*s5**3 - 16650*s1**3*s2**2*s5*s6**3
178
+ + 180*s1**3*s2*s3**3*s4*s6**2 - 366*s1**3*s2*s3**3*s5**2*s6 - 231*s1**3*s2*s3**2*s4**2*s5*s6
179
+ + 212*s1**3*s2*s3**2*s4*s5**3 + 112*s1**3*s2*s3*s4**4*s6 - 89*s1**3*s2*s3*s4**3*s5**2
180
+ + 10950*s1**3*s2*s3*s4*s6**3 + 1555*s1**3*s2*s3*s5**2*s6**2 + 6*s1**3*s2*s4**5*s5
181
+ - 9540*s1**3*s2*s4**2*s5*s6**2 + 9016*s1**3*s2*s4*s5**3*s6 - 2400*s1**3*s2*s5**5 -
182
+ 108*s1**3*s3**5*s6**2 + 117*s1**3*s3**4*s4*s5*s6 + 32*s1**3*s3**4*s5**3 - 31*s1**3*s3**3*s4**3*s6
183
+ - 51*s1**3*s3**3*s4**2*s5**2 - 2025*s1**3*s3**3*s6**3 + 19*s1**3*s3**2*s4**4*s5 +
184
+ 2955*s1**3*s3**2*s4*s5*s6**2 - 1436*s1**3*s3**2*s5**3*s6 - 2*s1**3*s3*s4**6 + 2770*s1**3*s3*s4**3*s6**2
185
+ - 5123*s1**3*s3*s4**2*s5**2*s6 + 1640*s1**3*s3*s4*s5**4 - 40500*s1**3*s3*s6**4 + 914*s1**3*s4**4*s5*s6
186
+ - 364*s1**3*s4**3*s5**3 + 53550*s1**3*s4*s5*s6**3 - 17930*s1**3*s5**3*s6**2 - 56*s1**2*s2**5*s6**3
187
+ - 318*s1**2*s2**4*s3*s5*s6**2 - 352*s1**2*s2**4*s4**2*s6**2 + 166*s1**2*s2**4*s4*s5**2*s6
188
+ + 3*s1**2*s2**4*s5**4 - 574*s1**2*s2**3*s3**2*s4*s6**2 + 347*s1**2*s2**3*s3**2*s5**2*s6
189
+ + 194*s1**2*s2**3*s3*s4**2*s5*s6 - 89*s1**2*s2**3*s3*s4*s5**3 - 8*s1**2*s2**3*s4**4*s6
190
+ + 4*s1**2*s2**3*s4**3*s5**2 + 560*s1**2*s2**3*s4*s6**3 + 3662*s1**2*s2**3*s5**2*s6**2
191
+ + 162*s1**2*s2**2*s3**4*s6**2 + 33*s1**2*s2**2*s3**3*s4*s5*s6 - 51*s1**2*s2**2*s3**3*s5**3
192
+ - 32*s1**2*s2**2*s3**2*s4**3*s6 + 28*s1**2*s2**2*s3**2*s4**2*s5**2 + 270*s1**2*s2**2*s3**2*s6**3
193
+ - 2*s1**2*s2**2*s3*s4**4*s5 + 4872*s1**2*s2**2*s3*s4*s5*s6**2 - 5123*s1**2*s2**2*s3*s5**3*s6
194
+ + 2144*s1**2*s2**2*s4**3*s6**2 - 2812*s1**2*s2**2*s4**2*s5**2*s6 + 1220*s1**2*s2**2*s4*s5**4
195
+ - 37800*s1**2*s2**2*s6**4 - 27*s1**2*s2*s3**5*s5*s6 + 9*s1**2*s2*s3**4*s4**2*s6 +
196
+ 3*s1**2*s2*s3**4*s4*s5**2 - s1**2*s2*s3**3*s4**3*s5 - 3078*s1**2*s2*s3**3*s5*s6**2
197
+ - 4014*s1**2*s2*s3**2*s4**2*s6**2 + 5412*s1**2*s2*s3**2*s4*s5**2*s6 + 260*s1**2*s2*s3**2*s5**4
198
+ - 310*s1**2*s2*s3*s4**3*s5*s6 - 901*s1**2*s2*s3*s4**2*s5**3 - 3780*s1**2*s2*s3*s5*s6**3
199
+ + 166*s1**2*s2*s4**4*s5**2 + 40320*s1**2*s2*s4**2*s6**3 - 25344*s1**2*s2*s4*s5**2*s6**2
200
+ + 3700*s1**2*s2*s5**4*s6 + 918*s1**2*s3**4*s4*s6**2 + 27*s1**2*s3**4*s5**2*s6 - 342*s1**2*s3**3*s4**2*s5*s6
201
+ - 366*s1**2*s3**3*s4*s5**3 + 32*s1**2*s3**2*s4**4*s6 + 347*s1**2*s3**2*s4**3*s5**2
202
+ - 4590*s1**2*s3**2*s4*s6**3 + 594*s1**2*s3**2*s5**2*s6**2 - 94*s1**2*s3*s4**5*s5 +
203
+ 3618*s1**2*s3*s4**2*s5*s6**2 + 1555*s1**2*s3*s4*s5**3*s6 - 500*s1**2*s3*s5**5 + 8*s1**2*s4**7
204
+ - 7192*s1**2*s4**4*s6**2 + 3662*s1**2*s4**3*s5**2*s6 - 550*s1**2*s4**2*s5**4 - 48600*s1**2*s4*s6**4
205
+ + 1080*s1**2*s5**2*s6**3 + 48*s1*s2**6*s5*s6**2 + 264*s1*s2**5*s3*s4*s6**2 - 94*s1*s2**5*s3*s5**2*s6
206
+ - 24*s1*s2**5*s4**2*s5*s6 + 6*s1*s2**5*s4*s5**3 - 66*s1*s2**4*s3**3*s6**2 - 50*s1*s2**4*s3**2*s4*s5*s6
207
+ + 19*s1*s2**4*s3**2*s5**3 + 8*s1*s2**4*s3*s4**3*s6 - 2*s1*s2**4*s3*s4**2*s5**2 - 552*s1*s2**4*s3*s6**3
208
+ - 2560*s1*s2**4*s4*s5*s6**2 + 914*s1*s2**4*s5**3*s6 + 15*s1*s2**3*s3**4*s5*s6 - 2*s1*s2**3*s3**3*s4**2*s6
209
+ - s1*s2**3*s3**3*s4*s5**2 + 1602*s1*s2**3*s3**2*s5*s6**2 - 608*s1*s2**3*s3*s4**2*s6**2
210
+ - 310*s1*s2**3*s3*s4*s5**2*s6 + 105*s1*s2**3*s3*s5**4 + 600*s1*s2**3*s4**3*s5*s6 -
211
+ 234*s1*s2**3*s4**2*s5**3 + 31368*s1*s2**3*s5*s6**3 + 756*s1*s2**2*s3**3*s4*s6**2 -
212
+ 342*s1*s2**2*s3**3*s5**2*s6 + 216*s1*s2**2*s3**2*s4**2*s5*s6 - 231*s1*s2**2*s3**2*s4*s5**3
213
+ - 192*s1*s2**2*s3*s4**4*s6 + 194*s1*s2**2*s3*s4**3*s5**2 - 39096*s1*s2**2*s3*s4*s6**3
214
+ + 3618*s1*s2**2*s3*s5**2*s6**2 - 24*s1*s2**2*s4**5*s5 + 9408*s1*s2**2*s4**2*s5*s6**2
215
+ - 9540*s1*s2**2*s4*s5**3*s6 + 2750*s1*s2**2*s5**5 - 162*s1*s2*s3**5*s6**2 - 378*s1*s2*s3**4*s4*s5*s6
216
+ + 117*s1*s2*s3**4*s5**3 + 150*s1*s2*s3**3*s4**3*s6 + 33*s1*s2*s3**3*s4**2*s5**2 +
217
+ 10044*s1*s2*s3**3*s6**3 - 50*s1*s2*s3**2*s4**4*s5 - 8640*s1*s2*s3**2*s4*s5*s6**2 +
218
+ 2955*s1*s2*s3**2*s5**3*s6 + 8*s1*s2*s3*s4**6 + 6144*s1*s2*s3*s4**3*s6**2 + 4872*s1*s2*s3*s4**2*s5**2*s6
219
+ - 3075*s1*s2*s3*s4*s5**4 + 174960*s1*s2*s3*s6**4 - 2560*s1*s2*s4**4*s5*s6 + 1090*s1*s2*s4**3*s5**3
220
+ - 148824*s1*s2*s4*s5*s6**3 + 53550*s1*s2*s5**3*s6**2 + 81*s1*s3**6*s5*s6 - 27*s1*s3**5*s4**2*s6
221
+ - 27*s1*s3**5*s4*s5**2 + 15*s1*s3**4*s4**3*s5 + 2430*s1*s3**4*s5*s6**2 - 2*s1*s3**3*s4**5
222
+ - 2052*s1*s3**3*s4**2*s6**2 - 3078*s1*s3**3*s4*s5**2*s6 + 525*s1*s3**3*s5**4 + 1602*s1*s3**2*s4**3*s5*s6
223
+ + 305*s1*s3**2*s4**2*s5**3 + 18144*s1*s3**2*s5*s6**3 - 104*s1*s3*s4**5*s6 - 318*s1*s3*s4**4*s5**2
224
+ - 33696*s1*s3*s4**2*s6**3 - 3780*s1*s3*s4*s5**2*s6**2 + 375*s1*s3*s5**4*s6 + 48*s1*s4**6*s5
225
+ + 31368*s1*s4**3*s5*s6**2 - 16650*s1*s4**2*s5**3*s6 + 2500*s1*s4*s5**5 + 77760*s1*s5*s6**4
226
+ - 32*s2**7*s4*s6**2 + 8*s2**7*s5**2*s6 + 8*s2**6*s3**2*s6**2 + 8*s2**6*s3*s4*s5*s6
227
+ - 2*s2**6*s3*s5**3 + 96*s2**6*s6**3 - 2*s2**5*s3**3*s5*s6 - 104*s2**5*s3*s5*s6**2
228
+ + 416*s2**5*s4**2*s6**2 - 58*s2**5*s5**4 - 312*s2**4*s3**2*s4*s6**2 + 32*s2**4*s3**2*s5**2*s6
229
+ - 192*s2**4*s3*s4**2*s5*s6 + 112*s2**4*s3*s4*s5**3 - 8*s2**4*s4**3*s5**2 + 4224*s2**4*s4*s6**3
230
+ - 7192*s2**4*s5**2*s6**2 + 54*s2**3*s3**4*s6**2 + 150*s2**3*s3**3*s4*s5*s6 - 31*s2**3*s3**3*s5**3
231
+ - 32*s2**3*s3**2*s4**2*s5**2 - 864*s2**3*s3**2*s6**3 + 8*s2**3*s3*s4**4*s5 + 6144*s2**3*s3*s4*s5*s6**2
232
+ + 2770*s2**3*s3*s5**3*s6 - 4032*s2**3*s4**3*s6**2 + 2144*s2**3*s4**2*s5**2*s6 - 850*s2**3*s4*s5**4
233
+ - 16416*s2**3*s6**4 - 27*s2**2*s3**5*s5*s6 + 9*s2**2*s3**4*s4*s5**2 - 2*s2**2*s3**3*s4**3*s5
234
+ - 2052*s2**2*s3**3*s5*s6**2 + 2376*s2**2*s3**2*s4**2*s6**2 - 4014*s2**2*s3**2*s4*s5**2*s6
235
+ - 325*s2**2*s3**2*s5**4 - 608*s2**2*s3*s4**3*s5*s6 + 1340*s2**2*s3*s4**2*s5**3 - 33696*s2**2*s3*s5*s6**3
236
+ + 416*s2**2*s4**5*s6 - 352*s2**2*s4**4*s5**2 - 6048*s2**2*s4**2*s6**3 + 40320*s2**2*s4*s5**2*s6**2
237
+ - 12750*s2**2*s5**4*s6 - 324*s2*s3**4*s4*s6**2 + 918*s2*s3**4*s5**2*s6 + 756*s2*s3**3*s4**2*s5*s6
238
+ + 180*s2*s3**3*s4*s5**3 - 312*s2*s3**2*s4**4*s6 - 574*s2*s3**2*s4**3*s5**2 + 43416*s2*s3**2*s4*s6**3
239
+ - 4590*s2*s3**2*s5**2*s6**2 + 264*s2*s3*s4**5*s5 - 39096*s2*s3*s4**2*s5*s6**2 + 10950*s2*s3*s4*s5**3*s6
240
+ + 625*s2*s3*s5**5 - 32*s2*s4**7 + 4224*s2*s4**4*s6**2 + 560*s2*s4**3*s5**2*s6 - 750*s2*s4**2*s5**4
241
+ + 85536*s2*s4*s6**4 - 48600*s2*s5**2*s6**3 - 162*s3**5*s4*s5*s6 - 108*s3**5*s5**3
242
+ + 54*s3**4*s4**3*s6 + 162*s3**4*s4**2*s5**2 - 11664*s3**4*s6**3 - 66*s3**3*s4**4*s5
243
+ + 10044*s3**3*s4*s5*s6**2 - 2025*s3**3*s5**3*s6 + 8*s3**2*s4**6 - 864*s3**2*s4**3*s6**2
244
+ + 270*s3**2*s4**2*s5**2*s6 - 375*s3**2*s4*s5**4 - 163296*s3**2*s6**4 - 552*s3*s4**4*s5*s6
245
+ + 350*s3*s4**3*s5**3 + 174960*s3*s4*s5*s6**3 - 40500*s3*s5**3*s6**2 + 96*s4**6*s6
246
+ - 56*s4**5*s5**2 - 16416*s4**3*s6**3 - 37800*s4**2*s5**2*s6**2 + 22500*s4*s5**4*s6
247
+ - 3125*s5**6 - 93312*s6**5),
248
+ lambda s1, s2, s3, s4, s5, s6: (-9375*s1**7*s5*s6**4 + 3125*s1**6*s2*s4*s6**4 + 7500*s1**6*s2*s5**2*s6**3 + 3125*s1**6*s3**2*s6**4
249
+ - 1250*s1**6*s3*s4*s5*s6**3 - 2000*s1**6*s3*s5**3*s6**2 + 3250*s1**6*s4**2*s5**2*s6**2
250
+ - 1600*s1**6*s4*s5**4*s6 + 256*s1**6*s5**6 + 40625*s1**6*s6**5 - 3125*s1**5*s2**2*s3*s6**4
251
+ - 3500*s1**5*s2**2*s4*s5*s6**3 - 1450*s1**5*s2**2*s5**3*s6**2 - 1750*s1**5*s2*s3**2*s5*s6**3
252
+ + 625*s1**5*s2*s3*s4**2*s6**3 - 850*s1**5*s2*s3*s4*s5**2*s6**2 + 1760*s1**5*s2*s3*s5**4*s6
253
+ - 2050*s1**5*s2*s4**3*s5*s6**2 + 780*s1**5*s2*s4**2*s5**3*s6 - 192*s1**5*s2*s4*s5**5
254
+ + 35000*s1**5*s2*s5*s6**4 + 1200*s1**5*s3**3*s5**2*s6**2 - 725*s1**5*s3**2*s4**2*s5*s6**2
255
+ - 160*s1**5*s3**2*s4*s5**3*s6 - 192*s1**5*s3**2*s5**5 - 125*s1**5*s3*s4**4*s6**2 +
256
+ 590*s1**5*s3*s4**3*s5**2*s6 - 16*s1**5*s3*s4**2*s5**4 - 20625*s1**5*s3*s4*s6**4 +
257
+ 17250*s1**5*s3*s5**2*s6**3 - 124*s1**5*s4**5*s5*s6 + 17*s1**5*s4**4*s5**3 - 20250*s1**5*s4**2*s5*s6**3
258
+ + 1900*s1**5*s4*s5**3*s6**2 + 1344*s1**5*s5**5*s6 + 625*s1**4*s2**4*s6**4 + 2300*s1**4*s2**3*s3*s5*s6**3
259
+ + 250*s1**4*s2**3*s4**2*s6**3 + 1470*s1**4*s2**3*s4*s5**2*s6**2 - 276*s1**4*s2**3*s5**4*s6
260
+ - 125*s1**4*s2**2*s3**2*s4*s6**3 - 610*s1**4*s2**2*s3**2*s5**2*s6**2 + 1995*s1**4*s2**2*s3*s4**2*s5*s6**2
261
+ - 1174*s1**4*s2**2*s3*s4*s5**3*s6 - 16*s1**4*s2**2*s3*s5**5 + 375*s1**4*s2**2*s4**4*s6**2
262
+ - 172*s1**4*s2**2*s4**3*s5**2*s6 + 82*s1**4*s2**2*s4**2*s5**4 - 7750*s1**4*s2**2*s4*s6**4
263
+ - 46650*s1**4*s2**2*s5**2*s6**3 + 15*s1**4*s2*s3**3*s4*s5*s6**2 - 384*s1**4*s2*s3**3*s5**3*s6
264
+ + 525*s1**4*s2*s3**2*s4**3*s6**2 - 528*s1**4*s2*s3**2*s4**2*s5**2*s6 + 384*s1**4*s2*s3**2*s4*s5**4
265
+ - 10125*s1**4*s2*s3**2*s6**4 - 29*s1**4*s2*s3*s4**4*s5*s6 - 118*s1**4*s2*s3*s4**3*s5**3
266
+ + 36700*s1**4*s2*s3*s4*s5*s6**3 + 2410*s1**4*s2*s3*s5**3*s6**2 + 38*s1**4*s2*s4**6*s6
267
+ + 5*s1**4*s2*s4**5*s5**2 + 5550*s1**4*s2*s4**3*s6**3 - 10040*s1**4*s2*s4**2*s5**2*s6**2
268
+ + 5800*s1**4*s2*s4*s5**4*s6 - 1600*s1**4*s2*s5**6 - 292500*s1**4*s2*s6**5 - 99*s1**4*s3**5*s5*s6**2
269
+ - 150*s1**4*s3**4*s4**2*s6**2 + 196*s1**4*s3**4*s4*s5**2*s6 + 48*s1**4*s3**4*s5**4
270
+ + 12*s1**4*s3**3*s4**3*s5*s6 - 128*s1**4*s3**3*s4**2*s5**3 - 6525*s1**4*s3**3*s5*s6**3
271
+ - 12*s1**4*s3**2*s4**5*s6 + 65*s1**4*s3**2*s4**4*s5**2 + 225*s1**4*s3**2*s4**2*s6**3
272
+ + 80*s1**4*s3**2*s4*s5**2*s6**2 - 13*s1**4*s3*s4**6*s5 + 5145*s1**4*s3*s4**3*s5*s6**2
273
+ - 6746*s1**4*s3*s4**2*s5**3*s6 + 1760*s1**4*s3*s4*s5**5 - 103500*s1**4*s3*s5*s6**4
274
+ + s1**4*s4**8 + 954*s1**4*s4**5*s6**2 + 449*s1**4*s4**4*s5**2*s6 - 276*s1**4*s4**3*s5**4
275
+ + 70125*s1**4*s4**2*s6**4 + 58900*s1**4*s4*s5**2*s6**3 - 23310*s1**4*s5**4*s6**2 -
276
+ 468*s1**3*s2**5*s5*s6**3 - 200*s1**3*s2**4*s3*s4*s6**3 - 294*s1**3*s2**4*s3*s5**2*s6**2
277
+ - 676*s1**3*s2**4*s4**2*s5*s6**2 + 180*s1**3*s2**4*s4*s5**3*s6 + 17*s1**3*s2**4*s5**5
278
+ + 50*s1**3*s2**3*s3**3*s6**3 - 397*s1**3*s2**3*s3**2*s4*s5*s6**2 + 514*s1**3*s2**3*s3**2*s5**3*s6
279
+ - 700*s1**3*s2**3*s3*s4**3*s6**2 + 447*s1**3*s2**3*s3*s4**2*s5**2*s6 - 118*s1**3*s2**3*s3*s4*s5**4
280
+ + 11700*s1**3*s2**3*s3*s6**4 - 12*s1**3*s2**3*s4**4*s5*s6 + 6*s1**3*s2**3*s4**3*s5**3
281
+ + 10360*s1**3*s2**3*s4*s5*s6**3 + 11404*s1**3*s2**3*s5**3*s6**2 + 141*s1**3*s2**2*s3**4*s5*s6**2
282
+ - 185*s1**3*s2**2*s3**3*s4**2*s6**2 + 168*s1**3*s2**2*s3**3*s4*s5**2*s6 - 128*s1**3*s2**2*s3**3*s5**4
283
+ + 93*s1**3*s2**2*s3**2*s4**3*s5*s6 + 19*s1**3*s2**2*s3**2*s4**2*s5**3 + 5895*s1**3*s2**2*s3**2*s5*s6**3
284
+ - 36*s1**3*s2**2*s3*s4**5*s6 + 5*s1**3*s2**2*s3*s4**4*s5**2 - 12020*s1**3*s2**2*s3*s4**2*s6**3
285
+ - 5698*s1**3*s2**2*s3*s4*s5**2*s6**2 - 6746*s1**3*s2**2*s3*s5**4*s6 + 5064*s1**3*s2**2*s4**3*s5*s6**2
286
+ - 762*s1**3*s2**2*s4**2*s5**3*s6 + 780*s1**3*s2**2*s4*s5**5 + 93900*s1**3*s2**2*s5*s6**4
287
+ + 198*s1**3*s2*s3**5*s4*s6**2 - 78*s1**3*s2*s3**5*s5**2*s6 - 95*s1**3*s2*s3**4*s4**2*s5*s6
288
+ + 44*s1**3*s2*s3**4*s4*s5**3 + 25*s1**3*s2*s3**3*s4**4*s6 - 15*s1**3*s2*s3**3*s4**3*s5**2
289
+ + 1935*s1**3*s2*s3**3*s4*s6**3 - 2808*s1**3*s2*s3**3*s5**2*s6**2 + s1**3*s2*s3**2*s4**5*s5
290
+ - 4844*s1**3*s2*s3**2*s4**2*s5*s6**2 + 8996*s1**3*s2*s3**2*s4*s5**3*s6 - 160*s1**3*s2*s3**2*s5**5
291
+ - 3616*s1**3*s2*s3*s4**4*s6**2 + 500*s1**3*s2*s3*s4**3*s5**2*s6 - 1174*s1**3*s2*s3*s4**2*s5**4
292
+ + 72900*s1**3*s2*s3*s4*s6**4 - 55665*s1**3*s2*s3*s5**2*s6**3 + 128*s1**3*s2*s4**5*s5*s6
293
+ + 180*s1**3*s2*s4**4*s5**3 + 16240*s1**3*s2*s4**2*s5*s6**3 - 9330*s1**3*s2*s4*s5**3*s6**2
294
+ + 1900*s1**3*s2*s5**5*s6 - 27*s1**3*s3**7*s6**2 + 18*s1**3*s3**6*s4*s5*s6 - 4*s1**3*s3**6*s5**3
295
+ - 4*s1**3*s3**5*s4**3*s6 + s1**3*s3**5*s4**2*s5**2 + 54*s1**3*s3**5*s6**3 + 1143*s1**3*s3**4*s4*s5*s6**2
296
+ - 820*s1**3*s3**4*s5**3*s6 + 923*s1**3*s3**3*s4**3*s6**2 + 57*s1**3*s3**3*s4**2*s5**2*s6
297
+ - 384*s1**3*s3**3*s4*s5**4 + 29700*s1**3*s3**3*s6**4 - 547*s1**3*s3**2*s4**4*s5*s6
298
+ + 514*s1**3*s3**2*s4**3*s5**3 - 10305*s1**3*s3**2*s4*s5*s6**3 - 7405*s1**3*s3**2*s5**3*s6**2
299
+ + 108*s1**3*s3*s4**6*s6 - 148*s1**3*s3*s4**5*s5**2 - 11360*s1**3*s3*s4**3*s6**3 +
300
+ 22209*s1**3*s3*s4**2*s5**2*s6**2 + 2410*s1**3*s3*s4*s5**4*s6 - 2000*s1**3*s3*s5**6
301
+ + 432000*s1**3*s3*s6**5 + 12*s1**3*s4**7*s5 - 22624*s1**3*s4**4*s5*s6**2 + 11404*s1**3*s4**3*s5**3*s6
302
+ - 1450*s1**3*s4**2*s5**5 - 242100*s1**3*s4*s5*s6**4 + 58430*s1**3*s5**3*s6**3 + 56*s1**2*s2**6*s4*s6**3
303
+ + 86*s1**2*s2**6*s5**2*s6**2 - 14*s1**2*s2**5*s3**2*s6**3 + 304*s1**2*s2**5*s3*s4*s5*s6**2
304
+ - 148*s1**2*s2**5*s3*s5**3*s6 + 152*s1**2*s2**5*s4**3*s6**2 - 54*s1**2*s2**5*s4**2*s5**2*s6
305
+ + 5*s1**2*s2**5*s4*s5**4 - 2472*s1**2*s2**5*s6**4 - 76*s1**2*s2**4*s3**3*s5*s6**2
306
+ + 370*s1**2*s2**4*s3**2*s4**2*s6**2 - 287*s1**2*s2**4*s3**2*s4*s5**2*s6 + 65*s1**2*s2**4*s3**2*s5**4
307
+ - 28*s1**2*s2**4*s3*s4**3*s5*s6 + 5*s1**2*s2**4*s3*s4**2*s5**3 - 8092*s1**2*s2**4*s3*s5*s6**3
308
+ + 8*s1**2*s2**4*s4**5*s6 - 2*s1**2*s2**4*s4**4*s5**2 + 1096*s1**2*s2**4*s4**2*s6**3
309
+ - 5144*s1**2*s2**4*s4*s5**2*s6**2 + 449*s1**2*s2**4*s5**4*s6 - 210*s1**2*s2**3*s3**4*s4*s6**2
310
+ + 76*s1**2*s2**3*s3**4*s5**2*s6 + 43*s1**2*s2**3*s3**3*s4**2*s5*s6 - 15*s1**2*s2**3*s3**3*s4*s5**3
311
+ - 6*s1**2*s2**3*s3**2*s4**4*s6 + 2*s1**2*s2**3*s3**2*s4**3*s5**2 + 1962*s1**2*s2**3*s3**2*s4*s6**3
312
+ + 3181*s1**2*s2**3*s3**2*s5**2*s6**2 + 1684*s1**2*s2**3*s3*s4**2*s5*s6**2 + 500*s1**2*s2**3*s3*s4*s5**3*s6
313
+ + 590*s1**2*s2**3*s3*s5**5 - 168*s1**2*s2**3*s4**4*s6**2 - 494*s1**2*s2**3*s4**3*s5**2*s6
314
+ - 172*s1**2*s2**3*s4**2*s5**4 - 22080*s1**2*s2**3*s4*s6**4 + 58894*s1**2*s2**3*s5**2*s6**3
315
+ + 27*s1**2*s2**2*s3**6*s6**2 - 9*s1**2*s2**2*s3**5*s4*s5*s6 + s1**2*s2**2*s3**5*s5**3
316
+ + s1**2*s2**2*s3**4*s4**3*s6 - 486*s1**2*s2**2*s3**4*s6**3 + 1071*s1**2*s2**2*s3**3*s4*s5*s6**2
317
+ + 57*s1**2*s2**2*s3**3*s5**3*s6 + 2262*s1**2*s2**2*s3**2*s4**3*s6**2 - 2742*s1**2*s2**2*s3**2*s4**2*s5**2*s6
318
+ - 528*s1**2*s2**2*s3**2*s4*s5**4 - 29160*s1**2*s2**2*s3**2*s6**4 + 772*s1**2*s2**2*s3*s4**4*s5*s6
319
+ + 447*s1**2*s2**2*s3*s4**3*s5**3 - 96732*s1**2*s2**2*s3*s4*s5*s6**3 + 22209*s1**2*s2**2*s3*s5**3*s6**2
320
+ - 160*s1**2*s2**2*s4**6*s6 - 54*s1**2*s2**2*s4**5*s5**2 - 7992*s1**2*s2**2*s4**3*s6**3
321
+ + 8634*s1**2*s2**2*s4**2*s5**2*s6**2 - 10040*s1**2*s2**2*s4*s5**4*s6 + 3250*s1**2*s2**2*s5**6
322
+ + 529200*s1**2*s2**2*s6**5 - 351*s1**2*s2*s3**5*s5*s6**2 - 1215*s1**2*s2*s3**4*s4**2*s6**2
323
+ - 360*s1**2*s2*s3**4*s4*s5**2*s6 + 196*s1**2*s2*s3**4*s5**4 + 741*s1**2*s2*s3**3*s4**3*s5*s6
324
+ + 168*s1**2*s2*s3**3*s4**2*s5**3 + 11718*s1**2*s2*s3**3*s5*s6**3 - 106*s1**2*s2*s3**2*s4**5*s6
325
+ - 287*s1**2*s2*s3**2*s4**4*s5**2 + 22572*s1**2*s2*s3**2*s4**2*s6**3 - 8892*s1**2*s2*s3**2*s4*s5**2*s6**2
326
+ + 80*s1**2*s2*s3**2*s5**4*s6 + 88*s1**2*s2*s3*s4**6*s5 + 22144*s1**2*s2*s3*s4**3*s5*s6**2
327
+ - 5698*s1**2*s2*s3*s4**2*s5**3*s6 - 850*s1**2*s2*s3*s4*s5**5 + 169560*s1**2*s2*s3*s5*s6**4
328
+ - 8*s1**2*s2*s4**8 + 3032*s1**2*s2*s4**5*s6**2 - 5144*s1**2*s2*s4**4*s5**2*s6 + 1470*s1**2*s2*s4**3*s5**4
329
+ - 249480*s1**2*s2*s4**2*s6**4 - 105390*s1**2*s2*s4*s5**2*s6**3 + 58900*s1**2*s2*s5**4*s6**2
330
+ + 162*s1**2*s3**6*s4*s6**2 + 216*s1**2*s3**6*s5**2*s6 - 216*s1**2*s3**5*s4**2*s5*s6
331
+ - 78*s1**2*s3**5*s4*s5**3 + 36*s1**2*s3**4*s4**4*s6 + 76*s1**2*s3**4*s4**3*s5**2 -
332
+ 3564*s1**2*s3**4*s4*s6**3 + 8802*s1**2*s3**4*s5**2*s6**2 - 22*s1**2*s3**3*s4**5*s5
333
+ - 11475*s1**2*s3**3*s4**2*s5*s6**2 - 2808*s1**2*s3**3*s4*s5**3*s6 + 1200*s1**2*s3**3*s5**5
334
+ + 2*s1**2*s3**2*s4**7 + 222*s1**2*s3**2*s4**4*s6**2 + 3181*s1**2*s3**2*s4**3*s5**2*s6
335
+ - 610*s1**2*s3**2*s4**2*s5**4 - 165240*s1**2*s3**2*s4*s6**4 + 118260*s1**2*s3**2*s5**2*s6**3
336
+ + 572*s1**2*s3*s4**5*s5*s6 - 294*s1**2*s3*s4**4*s5**3 - 32616*s1**2*s3*s4**2*s5*s6**3
337
+ - 55665*s1**2*s3*s4*s5**3*s6**2 + 17250*s1**2*s3*s5**5*s6 - 232*s1**2*s4**7*s6 + 86*s1**2*s4**6*s5**2
338
+ + 48408*s1**2*s4**4*s6**3 + 58894*s1**2*s4**3*s5**2*s6**2 - 46650*s1**2*s4**2*s5**4*s6
339
+ + 7500*s1**2*s4*s5**6 - 129600*s1**2*s4*s6**5 + 41040*s1**2*s5**2*s6**4 - 48*s1*s2**7*s4*s5*s6**2
340
+ + 12*s1*s2**7*s5**3*s6 + 12*s1*s2**6*s3**2*s5*s6**2 - 144*s1*s2**6*s3*s4**2*s6**2
341
+ + 88*s1*s2**6*s3*s4*s5**2*s6 - 13*s1*s2**6*s3*s5**4 + 1680*s1*s2**6*s5*s6**3 + 72*s1*s2**5*s3**3*s4*s6**2
342
+ - 22*s1*s2**5*s3**3*s5**2*s6 - 4*s1*s2**5*s3**2*s4**2*s5*s6 + s1*s2**5*s3**2*s4*s5**3
343
+ - 144*s1*s2**5*s3*s4*s6**3 + 572*s1*s2**5*s3*s5**2*s6**2 + 736*s1*s2**5*s4**2*s5*s6**2
344
+ + 128*s1*s2**5*s4*s5**3*s6 - 124*s1*s2**5*s5**5 - 9*s1*s2**4*s3**5*s6**2 + s1*s2**4*s3**4*s4*s5*s6
345
+ + 36*s1*s2**4*s3**3*s6**3 - 2028*s1*s2**4*s3**2*s4*s5*s6**2 - 547*s1*s2**4*s3**2*s5**3*s6
346
+ - 480*s1*s2**4*s3*s4**3*s6**2 + 772*s1*s2**4*s3*s4**2*s5**2*s6 - 29*s1*s2**4*s3*s4*s5**4
347
+ + 6336*s1*s2**4*s3*s6**4 - 12*s1*s2**4*s4**3*s5**3 + 4368*s1*s2**4*s4*s5*s6**3 - 22624*s1*s2**4*s5**3*s6**2
348
+ + 441*s1*s2**3*s3**4*s5*s6**2 + 336*s1*s2**3*s3**3*s4**2*s6**2 + 741*s1*s2**3*s3**3*s4*s5**2*s6
349
+ + 12*s1*s2**3*s3**3*s5**4 - 868*s1*s2**3*s3**2*s4**3*s5*s6 + 93*s1*s2**3*s3**2*s4**2*s5**3
350
+ + 11016*s1*s2**3*s3**2*s5*s6**3 + 176*s1*s2**3*s3*s4**5*s6 - 28*s1*s2**3*s3*s4**4*s5**2
351
+ + 14784*s1*s2**3*s3*s4**2*s6**3 + 22144*s1*s2**3*s3*s4*s5**2*s6**2 + 5145*s1*s2**3*s3*s5**4*s6
352
+ - 11344*s1*s2**3*s4**3*s5*s6**2 + 5064*s1*s2**3*s4**2*s5**3*s6 - 2050*s1*s2**3*s4*s5**5
353
+ - 346896*s1*s2**3*s5*s6**4 - 54*s1*s2**2*s3**5*s4*s6**2 - 216*s1*s2**2*s3**5*s5**2*s6
354
+ + 324*s1*s2**2*s3**4*s4**2*s5*s6 - 95*s1*s2**2*s3**4*s4*s5**3 - 80*s1*s2**2*s3**3*s4**4*s6
355
+ + 43*s1*s2**2*s3**3*s4**3*s5**2 - 12204*s1*s2**2*s3**3*s4*s6**3 - 11475*s1*s2**2*s3**3*s5**2*s6**2
356
+ - 4*s1*s2**2*s3**2*s4**5*s5 - 3888*s1*s2**2*s3**2*s4**2*s5*s6**2 - 4844*s1*s2**2*s3**2*s4*s5**3*s6
357
+ - 725*s1*s2**2*s3**2*s5**5 - 1312*s1*s2**2*s3*s4**4*s6**2 + 1684*s1*s2**2*s3*s4**3*s5**2*s6
358
+ + 1995*s1*s2**2*s3*s4**2*s5**4 + 139104*s1*s2**2*s3*s4*s6**4 - 32616*s1*s2**2*s3*s5**2*s6**3
359
+ + 736*s1*s2**2*s4**5*s5*s6 - 676*s1*s2**2*s4**4*s5**3 + 131040*s1*s2**2*s4**2*s5*s6**3
360
+ + 16240*s1*s2**2*s4*s5**3*s6**2 - 20250*s1*s2**2*s5**5*s6 - 27*s1*s2*s3**6*s4*s5*s6
361
+ + 18*s1*s2*s3**6*s5**3 + 9*s1*s2*s3**5*s4**3*s6 - 9*s1*s2*s3**5*s4**2*s5**2 + 1944*s1*s2*s3**5*s6**3
362
+ + s1*s2*s3**4*s4**4*s5 + 6156*s1*s2*s3**4*s4*s5*s6**2 + 1143*s1*s2*s3**4*s5**3*s6
363
+ + 324*s1*s2*s3**3*s4**3*s6**2 + 1071*s1*s2*s3**3*s4**2*s5**2*s6 + 15*s1*s2*s3**3*s4*s5**4
364
+ - 7776*s1*s2*s3**3*s6**4 - 2028*s1*s2*s3**2*s4**4*s5*s6 - 397*s1*s2*s3**2*s4**3*s5**3
365
+ + 112860*s1*s2*s3**2*s4*s5*s6**3 - 10305*s1*s2*s3**2*s5**3*s6**2 + 336*s1*s2*s3*s4**6*s6
366
+ + 304*s1*s2*s3*s4**5*s5**2 - 68976*s1*s2*s3*s4**3*s6**3 - 96732*s1*s2*s3*s4**2*s5**2*s6**2
367
+ + 36700*s1*s2*s3*s4*s5**4*s6 - 1250*s1*s2*s3*s5**6 - 1477440*s1*s2*s3*s6**5 - 48*s1*s2*s4**7*s5
368
+ + 4368*s1*s2*s4**4*s5*s6**2 + 10360*s1*s2*s4**3*s5**3*s6 - 3500*s1*s2*s4**2*s5**5
369
+ + 935280*s1*s2*s4*s5*s6**4 - 242100*s1*s2*s5**3*s6**3 - 972*s1*s3**6*s5*s6**2 - 351*s1*s3**5*s4*s5**2*s6
370
+ - 99*s1*s3**5*s5**4 + 441*s1*s3**4*s4**3*s5*s6 + 141*s1*s3**4*s4**2*s5**3 - 36936*s1*s3**4*s5*s6**3
371
+ - 84*s1*s3**3*s4**5*s6 - 76*s1*s3**3*s4**4*s5**2 + 17496*s1*s3**3*s4**2*s6**3 + 11718*s1*s3**3*s4*s5**2*s6**2
372
+ - 6525*s1*s3**3*s5**4*s6 + 12*s1*s3**2*s4**6*s5 + 11016*s1*s3**2*s4**3*s5*s6**2 +
373
+ 5895*s1*s3**2*s4**2*s5**3*s6 - 1750*s1*s3**2*s4*s5**5 - 252720*s1*s3**2*s5*s6**4 -
374
+ 2544*s1*s3*s4**5*s6**2 - 8092*s1*s3*s4**4*s5**2*s6 + 2300*s1*s3*s4**3*s5**4 + 536544*s1*s3*s4**2*s6**4
375
+ + 169560*s1*s3*s4*s5**2*s6**3 - 103500*s1*s3*s5**4*s6**2 + 1680*s1*s4**6*s5*s6 - 468*s1*s4**5*s5**3
376
+ - 346896*s1*s4**3*s5*s6**3 + 93900*s1*s4**2*s5**3*s6**2 + 35000*s1*s4*s5**5*s6 - 9375*s1*s5**7
377
+ + 108864*s1*s5*s6**5 + 16*s2**8*s4**2*s6**2 - 8*s2**8*s4*s5**2*s6 + s2**8*s5**4 -
378
+ 8*s2**7*s3**2*s4*s6**2 + 2*s2**7*s3**2*s5**2*s6 - 96*s2**7*s4*s6**3 - 232*s2**7*s5**2*s6**2
379
+ + s2**6*s3**4*s6**2 + 24*s2**6*s3**2*s6**3 + 336*s2**6*s3*s4*s5*s6**2 + 108*s2**6*s3*s5**3*s6
380
+ - 32*s2**6*s4**3*s6**2 - 160*s2**6*s4**2*s5**2*s6 + 38*s2**6*s4*s5**4 + 144*s2**6*s6**4
381
+ - 84*s2**5*s3**3*s5*s6**2 + 8*s2**5*s3**2*s4**2*s6**2 - 106*s2**5*s3**2*s4*s5**2*s6
382
+ - 12*s2**5*s3**2*s5**4 + 176*s2**5*s3*s4**3*s5*s6 - 36*s2**5*s3*s4**2*s5**3 - 2544*s2**5*s3*s5*s6**3
383
+ - 32*s2**5*s4**5*s6 + 8*s2**5*s4**4*s5**2 - 3072*s2**5*s4**2*s6**3 + 3032*s2**5*s4*s5**2*s6**2
384
+ + 954*s2**5*s5**4*s6 + 36*s2**4*s3**4*s5**2*s6 - 80*s2**4*s3**3*s4**2*s5*s6 + 25*s2**4*s3**3*s4*s5**3
385
+ + 16*s2**4*s3**2*s4**4*s6 - 6*s2**4*s3**2*s4**3*s5**2 + 2520*s2**4*s3**2*s4*s6**3
386
+ + 222*s2**4*s3**2*s5**2*s6**2 - 1312*s2**4*s3*s4**2*s5*s6**2 - 3616*s2**4*s3*s4*s5**3*s6
387
+ - 125*s2**4*s3*s5**5 + 1296*s2**4*s4**4*s6**2 - 168*s2**4*s4**3*s5**2*s6 + 375*s2**4*s4**2*s5**4
388
+ + 19296*s2**4*s4*s6**4 + 48408*s2**4*s5**2*s6**3 + 9*s2**3*s3**5*s4*s5*s6 - 4*s2**3*s3**5*s5**3
389
+ - 2*s2**3*s3**4*s4**3*s6 + s2**3*s3**4*s4**2*s5**2 - 432*s2**3*s3**4*s6**3 + 324*s2**3*s3**3*s4*s5*s6**2
390
+ + 923*s2**3*s3**3*s5**3*s6 - 752*s2**3*s3**2*s4**3*s6**2 + 2262*s2**3*s3**2*s4**2*s5**2*s6
391
+ + 525*s2**3*s3**2*s4*s5**4 - 9936*s2**3*s3**2*s6**4 - 480*s2**3*s3*s4**4*s5*s6 - 700*s2**3*s3*s4**3*s5**3
392
+ - 68976*s2**3*s3*s4*s5*s6**3 - 11360*s2**3*s3*s5**3*s6**2 - 32*s2**3*s4**6*s6 + 152*s2**3*s4**5*s5**2
393
+ + 6912*s2**3*s4**3*s6**3 - 7992*s2**3*s4**2*s5**2*s6**2 + 5550*s2**3*s4*s5**4*s6 -
394
+ 29376*s2**3*s6**5 + 108*s2**2*s3**4*s4**2*s6**2 - 1215*s2**2*s3**4*s4*s5**2*s6 - 150*s2**2*s3**4*s5**4
395
+ + 336*s2**2*s3**3*s4**3*s5*s6 - 185*s2**2*s3**3*s4**2*s5**3 + 17496*s2**2*s3**3*s5*s6**3
396
+ + 8*s2**2*s3**2*s4**5*s6 + 370*s2**2*s3**2*s4**4*s5**2 - 864*s2**2*s3**2*s4**2*s6**3
397
+ + 22572*s2**2*s3**2*s4*s5**2*s6**2 + 225*s2**2*s3**2*s5**4*s6 - 144*s2**2*s3*s4**6*s5
398
+ + 14784*s2**2*s3*s4**3*s5*s6**2 - 12020*s2**2*s3*s4**2*s5**3*s6 + 625*s2**2*s3*s4*s5**5
399
+ + 536544*s2**2*s3*s5*s6**4 + 16*s2**2*s4**8 - 3072*s2**2*s4**5*s6**2 + 1096*s2**2*s4**4*s5**2*s6
400
+ + 250*s2**2*s4**3*s5**4 - 93744*s2**2*s4**2*s6**4 - 249480*s2**2*s4*s5**2*s6**3 +
401
+ 70125*s2**2*s5**4*s6**2 + 162*s2*s3**6*s5**2*s6 - 54*s2*s3**5*s4**2*s5*s6 + 198*s2*s3**5*s4*s5**3
402
+ - 210*s2*s3**4*s4**3*s5**2 - 3564*s2*s3**4*s5**2*s6**2 + 72*s2*s3**3*s4**5*s5 - 12204*s2*s3**3*s4**2*s5*s6**2
403
+ + 1935*s2*s3**3*s4*s5**3*s6 - 8*s2*s3**2*s4**7 + 2520*s2*s3**2*s4**4*s6**2 + 1962*s2*s3**2*s4**3*s5**2*s6
404
+ - 125*s2*s3**2*s4**2*s5**4 - 178848*s2*s3**2*s4*s6**4 - 165240*s2*s3**2*s5**2*s6**3
405
+ - 144*s2*s3*s4**5*s5*s6 - 200*s2*s3*s4**4*s5**3 + 139104*s2*s3*s4**2*s5*s6**3 + 72900*s2*s3*s4*s5**3*s6**2
406
+ - 20625*s2*s3*s5**5*s6 - 96*s2*s4**7*s6 + 56*s2*s4**6*s5**2 + 19296*s2*s4**4*s6**3
407
+ - 22080*s2*s4**3*s5**2*s6**2 - 7750*s2*s4**2*s5**4*s6 + 3125*s2*s4*s5**6 + 248832*s2*s4*s6**5
408
+ - 129600*s2*s5**2*s6**4 - 27*s3**7*s5**3 + 27*s3**6*s4**2*s5**2 - 9*s3**5*s4**4*s5
409
+ + 1944*s3**5*s4*s5*s6**2 + 54*s3**5*s5**3*s6 + s3**4*s4**6 - 432*s3**4*s4**3*s6**2
410
+ - 486*s3**4*s4**2*s5**2*s6 + 46656*s3**4*s6**4 + 36*s3**3*s4**4*s5*s6 + 50*s3**3*s4**3*s5**3
411
+ - 7776*s3**3*s4*s5*s6**3 + 29700*s3**3*s5**3*s6**2 + 24*s3**2*s4**6*s6 - 14*s3**2*s4**5*s5**2
412
+ - 9936*s3**2*s4**3*s6**3 - 29160*s3**2*s4**2*s5**2*s6**2 - 10125*s3**2*s4*s5**4*s6
413
+ + 3125*s3**2*s5**6 + 1026432*s3**2*s6**5 + 6336*s3*s4**4*s5*s6**2 + 11700*s3*s4**3*s5**3*s6
414
+ - 3125*s3*s4**2*s5**5 - 1477440*s3*s4*s5*s6**4 + 432000*s3*s5**3*s6**3 + 144*s4**6*s6**2
415
+ - 2472*s4**5*s5**2*s6 + 625*s4**4*s5**4 - 29376*s4**3*s6**4 + 529200*s4**2*s5**2*s6**3
416
+ - 292500*s4*s5**4*s6**2 + 40625*s5**6*s6 - 186624*s6**6)
417
+ ],
418
+ (6, 2): [
419
+ lambda s1, s2, s3, s4, s5, s6: (-s3),
420
+ lambda s1, s2, s3, s4, s5, s6: (-s1*s5 + s2*s4 - 9*s6),
421
+ lambda s1, s2, s3, s4, s5, s6: (s1*s2*s6 + 2*s1*s3*s5 - s1*s4**2 - s2**2*s5 + 6*s3*s6 + s4*s5),
422
+ lambda s1, s2, s3, s4, s5, s6: (s1**2*s4*s6 - s1**2*s5**2 - 3*s1*s2*s3*s6 + s1*s2*s4*s5 + 9*s1*s5*s6 + s2**3*s6 -
423
+ 9*s2*s4*s6 + s2*s5**2 + 3*s3**2*s6 - 3*s3*s4*s5 + s4**3 + 27*s6**2),
424
+ lambda s1, s2, s3, s4, s5, s6: (-2*s1**3*s6**2 + 2*s1**2*s2*s5*s6 + 2*s1**2*s3*s4*s6 - s1**2*s3*s5**2 - s1*s2**2*s4*s6
425
+ - 3*s1*s2*s6**2 - 16*s1*s3*s5*s6 + 4*s1*s4**2*s6 + 2*s1*s4*s5**2 + 4*s2**2*s5*s6 +
426
+ s2*s3*s4*s6 + 2*s2*s3*s5**2 - s2*s4**2*s5 - 9*s3*s6**2 - 3*s4*s5*s6 - 2*s5**3),
427
+ lambda s1, s2, s3, s4, s5, s6: (s1**3*s3*s6**2 - 3*s1**3*s4*s5*s6 + s1**3*s5**3 - s1**2*s2**2*s6**2 + s1**2*s2*s3*s5*s6
428
+ - 2*s1**2*s4*s6**2 + 6*s1**2*s5**2*s6 + 16*s1*s2*s3*s6**2 - 3*s1*s2*s5**3 - s1*s3**2*s5*s6
429
+ - 2*s1*s3*s4**2*s6 + s1*s3*s4*s5**2 - 30*s1*s5*s6**2 - 4*s2**3*s6**2 - 2*s2**2*s3*s5*s6
430
+ + s2**2*s4**2*s6 + 18*s2*s4*s6**2 - 2*s2*s5**2*s6 - 15*s3**2*s6**2 + 16*s3*s4*s5*s6
431
+ + s3*s5**3 - 4*s4**3*s6 - s4**2*s5**2 - 27*s6**3),
432
+ lambda s1, s2, s3, s4, s5, s6: (s1**4*s5*s6**2 + 2*s1**3*s2*s4*s6**2 - s1**3*s2*s5**2*s6 - s1**3*s3**2*s6**2 + 9*s1**3*s6**3
433
+ - 14*s1**2*s2*s5*s6**2 - 11*s1**2*s3*s4*s6**2 + 6*s1**2*s3*s5**2*s6 + 3*s1**2*s4**2*s5*s6
434
+ - s1**2*s4*s5**3 + 3*s1*s2**2*s5**2*s6 + 3*s1*s2*s3**2*s6**2 - s1*s2*s3*s4*s5*s6 +
435
+ 39*s1*s3*s5*s6**2 - 14*s1*s4*s5**2*s6 + s1*s5**4 - 11*s2*s3*s5**2*s6 + 2*s2*s4*s5**3
436
+ - 3*s3**3*s6**2 + 3*s3**2*s4*s5*s6 - s3**2*s5**3 + 9*s5**3*s6),
437
+ lambda s1, s2, s3, s4, s5, s6: (-s1**4*s2*s6**3 + s1**4*s3*s5*s6**2 - 4*s1**3*s3*s6**3 + 10*s1**3*s4*s5*s6**2 - 4*s1**3*s5**3*s6
438
+ + 8*s1**2*s2**2*s6**3 - 8*s1**2*s2*s3*s5*s6**2 - 2*s1**2*s2*s4**2*s6**2 + s1**2*s2*s4*s5**2*s6
439
+ + s1**2*s3**2*s4*s6**2 - 6*s1**2*s4*s6**3 - 7*s1**2*s5**2*s6**2 - 24*s1*s2*s3*s6**3
440
+ - 4*s1*s2*s4*s5*s6**2 + 10*s1*s2*s5**3*s6 + 8*s1*s3**2*s5*s6**2 + 8*s1*s3*s4**2*s6**2
441
+ - 8*s1*s3*s4*s5**2*s6 + s1*s3*s5**4 + 36*s1*s5*s6**3 + 8*s2**2*s3*s5*s6**2 - 2*s2**2*s4*s5**2*s6
442
+ - 2*s2*s3**2*s4*s6**2 + s2*s3**2*s5**2*s6 - 6*s2*s5**2*s6**2 + 18*s3**2*s6**3 - 24*s3*s4*s5*s6**2
443
+ - 4*s3*s5**3*s6 + 8*s4**2*s5**2*s6 - s4*s5**4),
444
+ lambda s1, s2, s3, s4, s5, s6: (-s1**5*s4*s6**3 - 2*s1**4*s5*s6**3 + 3*s1**3*s2*s5**2*s6**2 + 3*s1**3*s3**2*s6**3
445
+ - s1**3*s3*s4*s5*s6**2 - 8*s1**3*s6**4 + 16*s1**2*s2*s5*s6**3 + 8*s1**2*s3*s4*s6**3
446
+ - 6*s1**2*s3*s5**2*s6**2 - 8*s1**2*s4**2*s5*s6**2 + 3*s1**2*s4*s5**3*s6 - 8*s1*s2**2*s5**2*s6**2
447
+ - 8*s1*s2*s3**2*s6**3 + 8*s1*s2*s3*s4*s5*s6**2 - s1*s2*s3*s5**3*s6 - s1*s3**3*s5*s6**2
448
+ - 24*s1*s3*s5*s6**3 + 16*s1*s4*s5**2*s6**2 - 2*s1*s5**4*s6 + 8*s2*s3*s5**2*s6**2 -
449
+ s2*s5**5 + 8*s3**3*s6**3 - 8*s3**2*s4*s5*s6**2 + 3*s3**2*s5**3*s6 - 8*s5**3*s6**2),
450
+ lambda s1, s2, s3, s4, s5, s6: (s1**6*s6**4 - 4*s1**4*s2*s6**4 - 2*s1**4*s3*s5*s6**3 + s1**4*s4**2*s6**3 + 8*s1**3*s3*s6**4
451
+ - 4*s1**3*s4*s5*s6**3 + 2*s1**3*s5**3*s6**2 + 8*s1**2*s2*s3*s5*s6**3 - 2*s1**2*s2*s4*s5**2*s6**2
452
+ - 2*s1**2*s3**2*s4*s6**3 + s1**2*s3**2*s5**2*s6**2 - 4*s1*s2*s5**3*s6**2 - 12*s1*s3**2*s5*s6**3
453
+ + 8*s1*s3*s4*s5**2*s6**2 - 2*s1*s3*s5**4*s6 + s2**2*s5**4*s6 - 2*s2*s3**2*s5**2*s6**2
454
+ + s3**4*s6**3 + 8*s3*s5**3*s6**2 - 4*s4*s5**4*s6 + s5**6)
455
+ ],
456
+ }
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/subfield.py ADDED
@@ -0,0 +1,516 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ r"""
2
+ Functions in ``polys.numberfields.subfield`` solve the "Subfield Problem" and
3
+ allied problems, for algebraic number fields.
4
+
5
+ Following Cohen (see [Cohen93]_ Section 4.5), we can define the main problem as
6
+ follows:
7
+
8
+ * **Subfield Problem:**
9
+
10
+ Given two number fields $\mathbb{Q}(\alpha)$, $\mathbb{Q}(\beta)$
11
+ via the minimal polynomials for their generators $\alpha$ and $\beta$, decide
12
+ whether one field is isomorphic to a subfield of the other.
13
+
14
+ From a solution to this problem flow solutions to the following problems as
15
+ well:
16
+
17
+ * **Primitive Element Problem:**
18
+
19
+ Given several algebraic numbers
20
+ $\alpha_1, \ldots, \alpha_m$, compute a single algebraic number $\theta$
21
+ such that $\mathbb{Q}(\alpha_1, \ldots, \alpha_m) = \mathbb{Q}(\theta)$.
22
+
23
+ * **Field Isomorphism Problem:**
24
+
25
+ Decide whether two number fields
26
+ $\mathbb{Q}(\alpha)$, $\mathbb{Q}(\beta)$ are isomorphic.
27
+
28
+ * **Field Membership Problem:**
29
+
30
+ Given two algebraic numbers $\alpha$,
31
+ $\beta$, decide whether $\alpha \in \mathbb{Q}(\beta)$, and if so write
32
+ $\alpha = f(\beta)$ for some $f(x) \in \mathbb{Q}[x]$.
33
+ """
34
+
35
+ from sympy.core.add import Add
36
+ from sympy.core.numbers import AlgebraicNumber
37
+ from sympy.core.singleton import S
38
+ from sympy.core.symbol import Dummy
39
+ from sympy.core.sympify import sympify, _sympify
40
+ from sympy.ntheory import sieve
41
+ from sympy.polys.densetools import dup_eval
42
+ from sympy.polys.domains import QQ
43
+ from sympy.polys.numberfields.minpoly import _choose_factor, minimal_polynomial
44
+ from sympy.polys.polyerrors import IsomorphismFailed
45
+ from sympy.polys.polytools import Poly, PurePoly, factor_list
46
+ from sympy.utilities import public
47
+
48
+ from mpmath import MPContext
49
+
50
+
51
+ def is_isomorphism_possible(a, b):
52
+ """Necessary but not sufficient test for isomorphism. """
53
+ n = a.minpoly.degree()
54
+ m = b.minpoly.degree()
55
+
56
+ if m % n != 0:
57
+ return False
58
+
59
+ if n == m:
60
+ return True
61
+
62
+ da = a.minpoly.discriminant()
63
+ db = b.minpoly.discriminant()
64
+
65
+ i, k, half = 1, m//n, db//2
66
+
67
+ while True:
68
+ p = sieve[i]
69
+ P = p**k
70
+
71
+ if P > half:
72
+ break
73
+
74
+ if ((da % p) % 2) and not (db % P):
75
+ return False
76
+
77
+ i += 1
78
+
79
+ return True
80
+
81
+
82
+ def field_isomorphism_pslq(a, b):
83
+ """Construct field isomorphism using PSLQ algorithm. """
84
+ if not a.root.is_real or not b.root.is_real:
85
+ raise NotImplementedError("PSLQ doesn't support complex coefficients")
86
+
87
+ f = a.minpoly
88
+ g = b.minpoly.replace(f.gen)
89
+
90
+ n, m, prev = 100, b.minpoly.degree(), None
91
+ ctx = MPContext()
92
+
93
+ for i in range(1, 5):
94
+ A = a.root.evalf(n)
95
+ B = b.root.evalf(n)
96
+
97
+ basis = [1, B] + [ B**i for i in range(2, m) ] + [-A]
98
+
99
+ ctx.dps = n
100
+ coeffs = ctx.pslq(basis, maxcoeff=10**10, maxsteps=1000)
101
+
102
+ if coeffs is None:
103
+ # PSLQ can't find an integer linear combination. Give up.
104
+ break
105
+
106
+ if coeffs != prev:
107
+ prev = coeffs
108
+ else:
109
+ # Increasing precision didn't produce anything new. Give up.
110
+ break
111
+
112
+ # We have
113
+ # c0 + c1*B + c2*B^2 + ... + cm-1*B^(m-1) - cm*A ~ 0.
114
+ # So bring cm*A to the other side, and divide through by cm,
115
+ # for an approximate representation of A as a polynomial in B.
116
+ # (We know cm != 0 since `b.minpoly` is irreducible.)
117
+ coeffs = [S(c)/coeffs[-1] for c in coeffs[:-1]]
118
+
119
+ # Throw away leading zeros.
120
+ while not coeffs[-1]:
121
+ coeffs.pop()
122
+
123
+ coeffs = list(reversed(coeffs))
124
+ h = Poly(coeffs, f.gen, domain='QQ')
125
+
126
+ # We only have A ~ h(B). We must check whether the relation is exact.
127
+ if f.compose(h).rem(g).is_zero:
128
+ # Now we know that h(b) is in fact equal to _some conjugate of_ a.
129
+ # But from the very precise approximation A ~ h(B) we can assume
130
+ # the conjugate is a itself.
131
+ return coeffs
132
+ else:
133
+ n *= 2
134
+
135
+ return None
136
+
137
+
138
+ def field_isomorphism_factor(a, b):
139
+ """Construct field isomorphism via factorization. """
140
+ _, factors = factor_list(a.minpoly, extension=b)
141
+ for f, _ in factors:
142
+ if f.degree() == 1:
143
+ # Any linear factor f(x) represents some conjugate of a in QQ(b).
144
+ # We want to know whether this linear factor represents a itself.
145
+ # Let f = x - c
146
+ c = -f.rep.TC()
147
+ # Write c as polynomial in b
148
+ coeffs = c.to_sympy_list()
149
+ d, terms = len(coeffs) - 1, []
150
+ for i, coeff in enumerate(coeffs):
151
+ terms.append(coeff*b.root**(d - i))
152
+ r = Add(*terms)
153
+ # Check whether we got the number a
154
+ if a.minpoly.same_root(r, a):
155
+ return coeffs
156
+
157
+ # If none of the linear factors represented a in QQ(b), then in fact a is
158
+ # not an element of QQ(b).
159
+ return None
160
+
161
+
162
+ @public
163
+ def field_isomorphism(a, b, *, fast=True):
164
+ r"""
165
+ Find an embedding of one number field into another.
166
+
167
+ Explanation
168
+ ===========
169
+
170
+ This function looks for an isomorphism from $\mathbb{Q}(a)$ onto some
171
+ subfield of $\mathbb{Q}(b)$. Thus, it solves the Subfield Problem.
172
+
173
+ Examples
174
+ ========
175
+
176
+ >>> from sympy import sqrt, field_isomorphism, I
177
+ >>> print(field_isomorphism(3, sqrt(2))) # doctest: +SKIP
178
+ [3]
179
+ >>> print(field_isomorphism( I*sqrt(3), I*sqrt(3)/2)) # doctest: +SKIP
180
+ [2, 0]
181
+
182
+ Parameters
183
+ ==========
184
+
185
+ a : :py:class:`~.Expr`
186
+ Any expression representing an algebraic number.
187
+ b : :py:class:`~.Expr`
188
+ Any expression representing an algebraic number.
189
+ fast : boolean, optional (default=True)
190
+ If ``True``, we first attempt a potentially faster way of computing the
191
+ isomorphism, falling back on a slower method if this fails. If
192
+ ``False``, we go directly to the slower method, which is guaranteed to
193
+ return a result.
194
+
195
+ Returns
196
+ =======
197
+
198
+ List of rational numbers, or None
199
+ If $\mathbb{Q}(a)$ is not isomorphic to some subfield of
200
+ $\mathbb{Q}(b)$, then return ``None``. Otherwise, return a list of
201
+ rational numbers representing an element of $\mathbb{Q}(b)$ to which
202
+ $a$ may be mapped, in order to define a monomorphism, i.e. an
203
+ isomorphism from $\mathbb{Q}(a)$ to some subfield of $\mathbb{Q}(b)$.
204
+ The elements of the list are the coefficients of falling powers of $b$.
205
+
206
+ """
207
+ a, b = sympify(a), sympify(b)
208
+
209
+ if not a.is_AlgebraicNumber:
210
+ a = AlgebraicNumber(a)
211
+
212
+ if not b.is_AlgebraicNumber:
213
+ b = AlgebraicNumber(b)
214
+
215
+ a = a.to_primitive_element()
216
+ b = b.to_primitive_element()
217
+
218
+ if a == b:
219
+ return a.coeffs()
220
+
221
+ n = a.minpoly.degree()
222
+ m = b.minpoly.degree()
223
+
224
+ if n == 1:
225
+ return [a.root]
226
+
227
+ if m % n != 0:
228
+ return None
229
+
230
+ if fast:
231
+ try:
232
+ result = field_isomorphism_pslq(a, b)
233
+
234
+ if result is not None:
235
+ return result
236
+ except NotImplementedError:
237
+ pass
238
+
239
+ return field_isomorphism_factor(a, b)
240
+
241
+
242
+ def _switch_domain(g, K):
243
+ # An algebraic relation f(a, b) = 0 over Q can also be written
244
+ # g(b) = 0 where g is in Q(a)[x] and h(a) = 0 where h is in Q(b)[x].
245
+ # This function transforms g into h where Q(b) = K.
246
+ frep = g.rep.inject()
247
+ hrep = frep.eject(K, front=True)
248
+
249
+ return g.new(hrep, g.gens[0])
250
+
251
+
252
+ def _linsolve(p):
253
+ # Compute root of linear polynomial.
254
+ c, d = p.rep.to_list()
255
+ return -d/c
256
+
257
+
258
+ @public
259
+ def primitive_element(extension, x=None, *, ex=False, polys=False):
260
+ r"""
261
+ Find a single generator for a number field given by several generators.
262
+
263
+ Explanation
264
+ ===========
265
+
266
+ The basic problem is this: Given several algebraic numbers
267
+ $\alpha_1, \alpha_2, \ldots, \alpha_n$, find a single algebraic number
268
+ $\theta$ such that
269
+ $\mathbb{Q}(\alpha_1, \alpha_2, \ldots, \alpha_n) = \mathbb{Q}(\theta)$.
270
+
271
+ This function actually guarantees that $\theta$ will be a linear
272
+ combination of the $\alpha_i$, with non-negative integer coefficients.
273
+
274
+ Furthermore, if desired, this function will tell you how to express each
275
+ $\alpha_i$ as a $\mathbb{Q}$-linear combination of the powers of $\theta$.
276
+
277
+ Examples
278
+ ========
279
+
280
+ >>> from sympy import primitive_element, sqrt, S, minpoly, simplify
281
+ >>> from sympy.abc import x
282
+ >>> f, lincomb, reps = primitive_element([sqrt(2), sqrt(3)], x, ex=True)
283
+
284
+ Then ``lincomb`` tells us the primitive element as a linear combination of
285
+ the given generators ``sqrt(2)`` and ``sqrt(3)``.
286
+
287
+ >>> print(lincomb)
288
+ [1, 1]
289
+
290
+ This means the primtiive element is $\sqrt{2} + \sqrt{3}$.
291
+ Meanwhile ``f`` is the minimal polynomial for this primitive element.
292
+
293
+ >>> print(f)
294
+ x**4 - 10*x**2 + 1
295
+ >>> print(minpoly(sqrt(2) + sqrt(3), x))
296
+ x**4 - 10*x**2 + 1
297
+
298
+ Finally, ``reps`` (which was returned only because we set keyword arg
299
+ ``ex=True``) tells us how to recover each of the generators $\sqrt{2}$ and
300
+ $\sqrt{3}$ as $\mathbb{Q}$-linear combinations of the powers of the
301
+ primitive element $\sqrt{2} + \sqrt{3}$.
302
+
303
+ >>> print([S(r) for r in reps[0]])
304
+ [1/2, 0, -9/2, 0]
305
+ >>> theta = sqrt(2) + sqrt(3)
306
+ >>> print(simplify(theta**3/2 - 9*theta/2))
307
+ sqrt(2)
308
+ >>> print([S(r) for r in reps[1]])
309
+ [-1/2, 0, 11/2, 0]
310
+ >>> print(simplify(-theta**3/2 + 11*theta/2))
311
+ sqrt(3)
312
+
313
+ Parameters
314
+ ==========
315
+
316
+ extension : list of :py:class:`~.Expr`
317
+ Each expression must represent an algebraic number $\alpha_i$.
318
+ x : :py:class:`~.Symbol`, optional (default=None)
319
+ The desired symbol to appear in the computed minimal polynomial for the
320
+ primitive element $\theta$. If ``None``, we use a dummy symbol.
321
+ ex : boolean, optional (default=False)
322
+ If and only if ``True``, compute the representation of each $\alpha_i$
323
+ as a $\mathbb{Q}$-linear combination over the powers of $\theta$.
324
+ polys : boolean, optional (default=False)
325
+ If ``True``, return the minimal polynomial as a :py:class:`~.Poly`.
326
+ Otherwise return it as an :py:class:`~.Expr`.
327
+
328
+ Returns
329
+ =======
330
+
331
+ Pair (f, coeffs) or triple (f, coeffs, reps), where:
332
+ ``f`` is the minimal polynomial for the primitive element.
333
+ ``coeffs`` gives the primitive element as a linear combination of the
334
+ given generators.
335
+ ``reps`` is present if and only if argument ``ex=True`` was passed,
336
+ and is a list of lists of rational numbers. Each list gives the
337
+ coefficients of falling powers of the primitive element, to recover
338
+ one of the original, given generators.
339
+
340
+ """
341
+ if not extension:
342
+ raise ValueError("Cannot compute primitive element for empty extension")
343
+ extension = [_sympify(ext) for ext in extension]
344
+
345
+ if x is not None:
346
+ x, cls = sympify(x), Poly
347
+ else:
348
+ x, cls = Dummy('x'), PurePoly
349
+
350
+ def _canonicalize(f):
351
+ _, f = f.primitive()
352
+ if f.LC() < 0:
353
+ f = -f
354
+ return f
355
+
356
+ if not ex:
357
+ gen, coeffs = extension[0], [1]
358
+ g = minimal_polynomial(gen, x, polys=True)
359
+ for ext in extension[1:]:
360
+ if ext.is_Rational:
361
+ coeffs.append(0)
362
+ continue
363
+ _, factors = factor_list(g, extension=ext)
364
+ g = _choose_factor(factors, x, gen)
365
+ [s], _, g = g.sqf_norm()
366
+ gen += s*ext
367
+ coeffs.append(s)
368
+
369
+ g = _canonicalize(g)
370
+ if not polys:
371
+ return g.as_expr(), coeffs
372
+ else:
373
+ return cls(g), coeffs
374
+
375
+ gen, coeffs = extension[0], [1]
376
+ f = minimal_polynomial(gen, x, polys=True)
377
+ K = QQ.algebraic_field((f, gen)) # incrementally constructed field
378
+ reps = [K.unit] # representations of extension elements in K
379
+ for ext in extension[1:]:
380
+ if ext.is_Rational:
381
+ coeffs.append(0) # rational ext is not included in the expression of a primitive element
382
+ reps.append(K.convert(ext)) # but it is included in reps
383
+ continue
384
+ p = minimal_polynomial(ext, x, polys=True)
385
+ L = QQ.algebraic_field((p, ext))
386
+ _, factors = factor_list(f, domain=L)
387
+ f = _choose_factor(factors, x, gen)
388
+ [s], g, f = f.sqf_norm()
389
+ gen += s*ext
390
+ coeffs.append(s)
391
+ K = QQ.algebraic_field((f, gen))
392
+ h = _switch_domain(g, K)
393
+ erep = _linsolve(h.gcd(p)) # ext as element of K
394
+ ogen = K.unit - s*erep # old gen as element of K
395
+ reps = [dup_eval(_.to_list(), ogen, K) for _ in reps] + [erep]
396
+
397
+ if K.ext.root.is_Rational: # all extensions are rational
398
+ H = [K.convert(_).rep for _ in extension]
399
+ coeffs = [0]*len(extension)
400
+ f = cls(x, domain=QQ)
401
+ else:
402
+ H = [_.to_list() for _ in reps]
403
+
404
+ f = _canonicalize(f)
405
+ if not polys:
406
+ return f.as_expr(), coeffs, H
407
+ else:
408
+ return f, coeffs, H
409
+
410
+
411
+ @public
412
+ def to_number_field(extension, theta=None, *, gen=None, alias=None):
413
+ r"""
414
+ Express one algebraic number in the field generated by another.
415
+
416
+ Explanation
417
+ ===========
418
+
419
+ Given two algebraic numbers $\eta, \theta$, this function either expresses
420
+ $\eta$ as an element of $\mathbb{Q}(\theta)$, or else raises an exception
421
+ if $\eta \not\in \mathbb{Q}(\theta)$.
422
+
423
+ This function is essentially just a convenience, utilizing
424
+ :py:func:`~.field_isomorphism` (our solution of the Subfield Problem) to
425
+ solve this, the Field Membership Problem.
426
+
427
+ As an additional convenience, this function allows you to pass a list of
428
+ algebraic numbers $\alpha_1, \alpha_2, \ldots, \alpha_n$ instead of $\eta$.
429
+ It then computes $\eta$ for you, as a solution of the Primitive Element
430
+ Problem, using :py:func:`~.primitive_element` on the list of $\alpha_i$.
431
+
432
+ Examples
433
+ ========
434
+
435
+ >>> from sympy import sqrt, to_number_field
436
+ >>> eta = sqrt(2)
437
+ >>> theta = sqrt(2) + sqrt(3)
438
+ >>> a = to_number_field(eta, theta)
439
+ >>> print(type(a))
440
+ <class 'sympy.core.numbers.AlgebraicNumber'>
441
+ >>> a.root
442
+ sqrt(2) + sqrt(3)
443
+ >>> print(a)
444
+ sqrt(2)
445
+ >>> a.coeffs()
446
+ [1/2, 0, -9/2, 0]
447
+
448
+ We get an :py:class:`~.AlgebraicNumber`, whose ``.root`` is $\theta$, whose
449
+ value is $\eta$, and whose ``.coeffs()`` show how to write $\eta$ as a
450
+ $\mathbb{Q}$-linear combination in falling powers of $\theta$.
451
+
452
+ Parameters
453
+ ==========
454
+
455
+ extension : :py:class:`~.Expr` or list of :py:class:`~.Expr`
456
+ Either the algebraic number that is to be expressed in the other field,
457
+ or else a list of algebraic numbers, a primitive element for which is
458
+ to be expressed in the other field.
459
+ theta : :py:class:`~.Expr`, None, optional (default=None)
460
+ If an :py:class:`~.Expr` representing an algebraic number, behavior is
461
+ as described under **Explanation**. If ``None``, then this function
462
+ reduces to a shorthand for calling :py:func:`~.primitive_element` on
463
+ ``extension`` and turning the computed primitive element into an
464
+ :py:class:`~.AlgebraicNumber`.
465
+ gen : :py:class:`~.Symbol`, None, optional (default=None)
466
+ If provided, this will be used as the generator symbol for the minimal
467
+ polynomial in the returned :py:class:`~.AlgebraicNumber`.
468
+ alias : str, :py:class:`~.Symbol`, None, optional (default=None)
469
+ If provided, this will be used as the alias symbol for the returned
470
+ :py:class:`~.AlgebraicNumber`.
471
+
472
+ Returns
473
+ =======
474
+
475
+ AlgebraicNumber
476
+ Belonging to $\mathbb{Q}(\theta)$ and equaling $\eta$.
477
+
478
+ Raises
479
+ ======
480
+
481
+ IsomorphismFailed
482
+ If $\eta \not\in \mathbb{Q}(\theta)$.
483
+
484
+ See Also
485
+ ========
486
+
487
+ field_isomorphism
488
+ primitive_element
489
+
490
+ """
491
+ if hasattr(extension, '__iter__'):
492
+ extension = list(extension)
493
+ else:
494
+ extension = [extension]
495
+
496
+ if len(extension) == 1 and isinstance(extension[0], tuple):
497
+ return AlgebraicNumber(extension[0], alias=alias)
498
+
499
+ minpoly, coeffs = primitive_element(extension, gen, polys=True)
500
+ root = sum(coeff*ext for coeff, ext in zip(coeffs, extension))
501
+
502
+ if theta is None:
503
+ return AlgebraicNumber((minpoly, root), alias=alias)
504
+ else:
505
+ theta = sympify(theta)
506
+
507
+ if not theta.is_AlgebraicNumber:
508
+ theta = AlgebraicNumber(theta, gen=gen, alias=alias)
509
+
510
+ coeffs = field_isomorphism(root, theta)
511
+
512
+ if coeffs is not None:
513
+ return AlgebraicNumber(theta, coeffs, alias=alias)
514
+ else:
515
+ raise IsomorphismFailed(
516
+ "%s is not in a subfield of %s" % (root, theta.root))
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/__init__.py ADDED
File without changes
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_basis.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.abc import x
2
+ from sympy.core import S
3
+ from sympy.core.numbers import AlgebraicNumber
4
+ from sympy.functions.elementary.miscellaneous import sqrt
5
+ from sympy.polys import Poly, cyclotomic_poly
6
+ from sympy.polys.domains import QQ
7
+ from sympy.polys.matrices import DomainMatrix, DM
8
+ from sympy.polys.numberfields.basis import round_two
9
+ from sympy.testing.pytest import raises
10
+
11
+
12
+ def test_round_two():
13
+ # Poly must be irreducible, and over ZZ or QQ:
14
+ raises(ValueError, lambda: round_two(Poly(x ** 2 - 1)))
15
+ raises(ValueError, lambda: round_two(Poly(x ** 2 + sqrt(2))))
16
+
17
+ # Test on many fields:
18
+ cases = (
19
+ # A couple of cyclotomic fields:
20
+ (cyclotomic_poly(5), DomainMatrix.eye(4, QQ), 125),
21
+ (cyclotomic_poly(7), DomainMatrix.eye(6, QQ), -16807),
22
+ # A couple of quadratic fields (one 1 mod 4, one 3 mod 4):
23
+ (x ** 2 - 5, DM([[1, (1, 2)], [0, (1, 2)]], QQ), 5),
24
+ (x ** 2 - 7, DM([[1, 0], [0, 1]], QQ), 28),
25
+ # Dedekind's example of a field with 2 as essential disc divisor:
26
+ (x ** 3 + x ** 2 - 2 * x + 8, DM([[1, 0, 0], [0, 1, 0], [0, (1, 2), (1, 2)]], QQ).transpose(), -503),
27
+ # A bunch of cubics with various forms for F -- all of these require
28
+ # second or third enlargements. (Five of them require a third, while the rest require just a second.)
29
+ # F = 2^2
30
+ (x**3 + 3 * x**2 - 4 * x + 4, DM([((1, 2), (1, 4), (1, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), -83),
31
+ # F = 2^2 * 3
32
+ (x**3 + 3 * x**2 + 3 * x - 3, DM([((1, 2), 0, (1, 2)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -108),
33
+ # F = 2^3
34
+ (x**3 + 5 * x**2 - x + 3, DM([((1, 4), 0, (3, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), -31),
35
+ # F = 2^2 * 5
36
+ (x**3 + 5 * x**2 - 5 * x - 5, DM([((1, 2), 0, (1, 2)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), 1300),
37
+ # F = 3^2
38
+ (x**3 + 3 * x**2 + 5, DM([((1, 3), (1, 3), (1, 3)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -135),
39
+ # F = 3^3
40
+ (x**3 + 6 * x**2 + 3 * x - 1, DM([((1, 3), (1, 3), (1, 3)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), 81),
41
+ # F = 2^2 * 3^2
42
+ (x**3 + 6 * x**2 + 4, DM([((1, 3), (2, 3), (1, 3)), (0, 1, 0), (0, 0, (1, 2))], QQ).transpose(), -108),
43
+ # F = 2^3 * 7
44
+ (x**3 + 7 * x**2 + 7 * x - 7, DM([((1, 4), 0, (3, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), 49),
45
+ # F = 2^2 * 13
46
+ (x**3 + 7 * x**2 - x + 5, DM([((1, 2), 0, (1, 2)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -2028),
47
+ # F = 2^4
48
+ (x**3 + 7 * x**2 - 5 * x + 5, DM([((1, 4), 0, (3, 4)), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), -140),
49
+ # F = 5^2
50
+ (x**3 + 4 * x**2 - 3 * x + 7, DM([((1, 5), (4, 5), (4, 5)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -175),
51
+ # F = 7^2
52
+ (x**3 + 8 * x**2 + 5 * x - 1, DM([((1, 7), (6, 7), (2, 7)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), 49),
53
+ # F = 2 * 5 * 7
54
+ (x**3 + 8 * x**2 - 2 * x + 6, DM([(1, 0, 0), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -14700),
55
+ # F = 2^2 * 3 * 5
56
+ (x**3 + 6 * x**2 - 3 * x + 8, DM([(1, 0, 0), (0, (1, 4), (1, 4)), (0, 0, 1)], QQ).transpose(), -675),
57
+ # F = 2 * 3^2 * 7
58
+ (x**3 + 9 * x**2 + 6 * x - 8, DM([(1, 0, 0), (0, (1, 2), (1, 2)), (0, 0, 1)], QQ).transpose(), 3969),
59
+ # F = 2^2 * 3^2 * 7
60
+ (x**3 + 15 * x**2 - 9 * x + 13, DM([((1, 6), (1, 3), (1, 6)), (0, 1, 0), (0, 0, 1)], QQ).transpose(), -5292),
61
+ # Polynomial need not be monic
62
+ (5*x**3 + 5*x**2 - 10 * x + 40, DM([[1, 0, 0], [0, 1, 0], [0, (1, 2), (1, 2)]], QQ).transpose(), -503),
63
+ # Polynomial can have non-integer rational coeffs
64
+ (QQ(5, 3)*x**3 + QQ(5, 3)*x**2 - QQ(10, 3)*x + QQ(40, 3), DM([[1, 0, 0], [0, 1, 0], [0, (1, 2), (1, 2)]], QQ).transpose(), -503),
65
+ )
66
+ for f, B_exp, d_exp in cases:
67
+ K = QQ.alg_field_from_poly(f)
68
+ B = K.maximal_order().QQ_matrix
69
+ d = K.discriminant()
70
+ assert d == d_exp
71
+ # The computed basis need not equal the expected one, but their quotient
72
+ # must be unimodular:
73
+ assert (B.inv()*B_exp).det()**2 == 1
74
+
75
+
76
+ def test_AlgebraicField_integral_basis():
77
+ alpha = AlgebraicNumber(sqrt(5), alias='alpha')
78
+ k = QQ.algebraic_field(alpha)
79
+ B0 = k.integral_basis()
80
+ B1 = k.integral_basis(fmt='sympy')
81
+ B2 = k.integral_basis(fmt='alg')
82
+ assert B0 == [k([1]), k([S.Half, S.Half])]
83
+ assert B1 == [1, S.Half + alpha/2]
84
+ assert B2 == [k.ext.field_element([1]),
85
+ k.ext.field_element([S.Half, S.Half])]
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_galoisgroups.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for computing Galois groups. """
2
+
3
+ from sympy.abc import x
4
+ from sympy.combinatorics.galois import (
5
+ S1TransitiveSubgroups, S2TransitiveSubgroups, S3TransitiveSubgroups,
6
+ S4TransitiveSubgroups, S5TransitiveSubgroups, S6TransitiveSubgroups,
7
+ )
8
+ from sympy.polys.domains.rationalfield import QQ
9
+ from sympy.polys.numberfields.galoisgroups import (
10
+ tschirnhausen_transformation,
11
+ galois_group,
12
+ _galois_group_degree_4_root_approx,
13
+ _galois_group_degree_5_hybrid,
14
+ )
15
+ from sympy.polys.numberfields.subfield import field_isomorphism
16
+ from sympy.polys.polytools import Poly
17
+ from sympy.testing.pytest import raises
18
+
19
+
20
+ def test_tschirnhausen_transformation():
21
+ for T in [
22
+ Poly(x**2 - 2),
23
+ Poly(x**2 + x + 1),
24
+ Poly(x**4 + 1),
25
+ Poly(x**4 - x**3 + x**2 - x + 1),
26
+ ]:
27
+ _, U = tschirnhausen_transformation(T)
28
+ assert U.degree() == T.degree()
29
+ assert U.is_monic
30
+ assert U.is_irreducible
31
+ K = QQ.alg_field_from_poly(T)
32
+ L = QQ.alg_field_from_poly(U)
33
+ assert field_isomorphism(K.ext, L.ext) is not None
34
+
35
+
36
+ # Test polys are from:
37
+ # Cohen, H. *A Course in Computational Algebraic Number Theory*.
38
+ test_polys_by_deg = {
39
+ # Degree 1
40
+ 1: [
41
+ (x, S1TransitiveSubgroups.S1, True)
42
+ ],
43
+ # Degree 2
44
+ 2: [
45
+ (x**2 + x + 1, S2TransitiveSubgroups.S2, False)
46
+ ],
47
+ # Degree 3
48
+ 3: [
49
+ (x**3 + x**2 - 2*x - 1, S3TransitiveSubgroups.A3, True),
50
+ (x**3 + 2, S3TransitiveSubgroups.S3, False),
51
+ ],
52
+ # Degree 4
53
+ 4: [
54
+ (x**4 + x**3 + x**2 + x + 1, S4TransitiveSubgroups.C4, False),
55
+ (x**4 + 1, S4TransitiveSubgroups.V, True),
56
+ (x**4 - 2, S4TransitiveSubgroups.D4, False),
57
+ (x**4 + 8*x + 12, S4TransitiveSubgroups.A4, True),
58
+ (x**4 + x + 1, S4TransitiveSubgroups.S4, False),
59
+ ],
60
+ # Degree 5
61
+ 5: [
62
+ (x**5 + x**4 - 4*x**3 - 3*x**2 + 3*x + 1, S5TransitiveSubgroups.C5, True),
63
+ (x**5 - 5*x + 12, S5TransitiveSubgroups.D5, True),
64
+ (x**5 + 2, S5TransitiveSubgroups.M20, False),
65
+ (x**5 + 20*x + 16, S5TransitiveSubgroups.A5, True),
66
+ (x**5 - x + 1, S5TransitiveSubgroups.S5, False),
67
+ ],
68
+ # Degree 6
69
+ 6: [
70
+ (x**6 + x**5 + x**4 + x**3 + x**2 + x + 1, S6TransitiveSubgroups.C6, False),
71
+ (x**6 + 108, S6TransitiveSubgroups.S3, False),
72
+ (x**6 + 2, S6TransitiveSubgroups.D6, False),
73
+ (x**6 - 3*x**2 - 1, S6TransitiveSubgroups.A4, True),
74
+ (x**6 + 3*x**3 + 3, S6TransitiveSubgroups.G18, False),
75
+ (x**6 - 3*x**2 + 1, S6TransitiveSubgroups.A4xC2, False),
76
+ (x**6 - 4*x**2 - 1, S6TransitiveSubgroups.S4p, True),
77
+ (x**6 - 3*x**5 + 6*x**4 - 7*x**3 + 2*x**2 + x - 4, S6TransitiveSubgroups.S4m, False),
78
+ (x**6 + 2*x**3 - 2, S6TransitiveSubgroups.G36m, False),
79
+ (x**6 + 2*x**2 + 2, S6TransitiveSubgroups.S4xC2, False),
80
+ (x**6 + 10*x**5 + 55*x**4 + 140*x**3 + 175*x**2 + 170*x + 25, S6TransitiveSubgroups.PSL2F5, True),
81
+ (x**6 + 10*x**5 + 55*x**4 + 140*x**3 + 175*x**2 - 3019*x + 25, S6TransitiveSubgroups.PGL2F5, False),
82
+ (x**6 + 6*x**4 + 2*x**3 + 9*x**2 + 6*x - 4, S6TransitiveSubgroups.G36p, True),
83
+ (x**6 + 2*x**4 + 2*x**3 + x**2 + 2*x + 2, S6TransitiveSubgroups.G72, False),
84
+ (x**6 + 24*x - 20, S6TransitiveSubgroups.A6, True),
85
+ (x**6 + x + 1, S6TransitiveSubgroups.S6, False),
86
+ ],
87
+ }
88
+
89
+
90
+ def test_galois_group():
91
+ """
92
+ Try all the test polys.
93
+ """
94
+ for deg in range(1, 7):
95
+ polys = test_polys_by_deg[deg]
96
+ for T, G, alt in polys:
97
+ assert galois_group(T, by_name=True) == (G, alt)
98
+
99
+
100
+ def test_galois_group_degree_out_of_bounds():
101
+ raises(ValueError, lambda: galois_group(Poly(0, x)))
102
+ raises(ValueError, lambda: galois_group(Poly(1, x)))
103
+ raises(ValueError, lambda: galois_group(Poly(x ** 7 + 1)))
104
+
105
+
106
+ def test_galois_group_not_by_name():
107
+ """
108
+ Check at least one polynomial of each supported degree, to see that
109
+ conversion from name to group works.
110
+ """
111
+ for deg in range(1, 7):
112
+ T, G_name, _ = test_polys_by_deg[deg][0]
113
+ G, _ = galois_group(T)
114
+ assert G == G_name.get_perm_group()
115
+
116
+
117
+ def test_galois_group_not_monic_over_ZZ():
118
+ """
119
+ Check that we can work with polys that are not monic over ZZ.
120
+ """
121
+ for deg in range(1, 7):
122
+ T, G, alt = test_polys_by_deg[deg][0]
123
+ assert galois_group(T/2, by_name=True) == (G, alt)
124
+
125
+
126
+ def test__galois_group_degree_4_root_approx():
127
+ for T, G, alt in test_polys_by_deg[4]:
128
+ assert _galois_group_degree_4_root_approx(Poly(T)) == (G, alt)
129
+
130
+
131
+ def test__galois_group_degree_5_hybrid():
132
+ for T, G, alt in test_polys_by_deg[5]:
133
+ assert _galois_group_degree_5_hybrid(Poly(T)) == (G, alt)
134
+
135
+
136
+ def test_AlgebraicField_galois_group():
137
+ k = QQ.alg_field_from_poly(Poly(x**4 + 1))
138
+ G, _ = k.galois_group(by_name=True)
139
+ assert G == S4TransitiveSubgroups.V
140
+
141
+ k = QQ.alg_field_from_poly(Poly(x**4 - 2))
142
+ G, _ = k.galois_group(by_name=True)
143
+ assert G == S4TransitiveSubgroups.D4
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_minpoly.py ADDED
@@ -0,0 +1,490 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for minimal polynomials. """
2
+
3
+ from sympy.core.function import expand
4
+ from sympy.core import (GoldenRatio, TribonacciConstant)
5
+ from sympy.core.numbers import (AlgebraicNumber, I, Rational, oo, pi)
6
+ from sympy.core.power import Pow
7
+ from sympy.core.singleton import S
8
+ from sympy.functions.elementary.exponential import exp
9
+ from sympy.functions.elementary.miscellaneous import (cbrt, sqrt)
10
+ from sympy.functions.elementary.trigonometric import (cos, sin, tan)
11
+ from sympy.ntheory.generate import nextprime
12
+ from sympy.polys.polytools import Poly
13
+ from sympy.polys.rootoftools import CRootOf
14
+ from sympy.solvers.solveset import nonlinsolve
15
+ from sympy.geometry import Circle, intersection
16
+ from sympy.testing.pytest import raises, slow
17
+ from sympy.sets.sets import FiniteSet
18
+ from sympy.geometry.point import Point2D
19
+ from sympy.polys.numberfields.minpoly import (
20
+ minimal_polynomial,
21
+ _choose_factor,
22
+ _minpoly_op_algebraic_element,
23
+ _separate_sq,
24
+ _minpoly_groebner,
25
+ )
26
+ from sympy.polys.partfrac import apart
27
+ from sympy.polys.polyerrors import (
28
+ NotAlgebraic,
29
+ GeneratorsError,
30
+ )
31
+
32
+ from sympy.polys.domains import QQ
33
+ from sympy.polys.rootoftools import rootof
34
+ from sympy.polys.polytools import degree
35
+
36
+ from sympy.abc import x, y, z
37
+
38
+ Q = Rational
39
+
40
+
41
+ def test_minimal_polynomial():
42
+ assert minimal_polynomial(-7, x) == x + 7
43
+ assert minimal_polynomial(-1, x) == x + 1
44
+ assert minimal_polynomial( 0, x) == x
45
+ assert minimal_polynomial( 1, x) == x - 1
46
+ assert minimal_polynomial( 7, x) == x - 7
47
+
48
+ assert minimal_polynomial(sqrt(2), x) == x**2 - 2
49
+ assert minimal_polynomial(sqrt(5), x) == x**2 - 5
50
+ assert minimal_polynomial(sqrt(6), x) == x**2 - 6
51
+
52
+ assert minimal_polynomial(2*sqrt(2), x) == x**2 - 8
53
+ assert minimal_polynomial(3*sqrt(5), x) == x**2 - 45
54
+ assert minimal_polynomial(4*sqrt(6), x) == x**2 - 96
55
+
56
+ assert minimal_polynomial(2*sqrt(2) + 3, x) == x**2 - 6*x + 1
57
+ assert minimal_polynomial(3*sqrt(5) + 6, x) == x**2 - 12*x - 9
58
+ assert minimal_polynomial(4*sqrt(6) + 7, x) == x**2 - 14*x - 47
59
+
60
+ assert minimal_polynomial(2*sqrt(2) - 3, x) == x**2 + 6*x + 1
61
+ assert minimal_polynomial(3*sqrt(5) - 6, x) == x**2 + 12*x - 9
62
+ assert minimal_polynomial(4*sqrt(6) - 7, x) == x**2 + 14*x - 47
63
+
64
+ assert minimal_polynomial(sqrt(1 + sqrt(6)), x) == x**4 - 2*x**2 - 5
65
+ assert minimal_polynomial(sqrt(I + sqrt(6)), x) == x**8 - 10*x**4 + 49
66
+
67
+ assert minimal_polynomial(2*I + sqrt(2 + I), x) == x**4 + 4*x**2 + 8*x + 37
68
+
69
+ assert minimal_polynomial(sqrt(2) + sqrt(3), x) == x**4 - 10*x**2 + 1
70
+ assert minimal_polynomial(
71
+ sqrt(2) + sqrt(3) + sqrt(6), x) == x**4 - 22*x**2 - 48*x - 23
72
+
73
+ a = 1 - 9*sqrt(2) + 7*sqrt(3)
74
+
75
+ assert minimal_polynomial(
76
+ 1/a, x) == 392*x**4 - 1232*x**3 + 612*x**2 + 4*x - 1
77
+ assert minimal_polynomial(
78
+ 1/sqrt(a), x) == 392*x**8 - 1232*x**6 + 612*x**4 + 4*x**2 - 1
79
+
80
+ raises(NotAlgebraic, lambda: minimal_polynomial(oo, x))
81
+ raises(NotAlgebraic, lambda: minimal_polynomial(2**y, x))
82
+ raises(NotAlgebraic, lambda: minimal_polynomial(sin(1), x))
83
+
84
+ assert minimal_polynomial(sqrt(2)).dummy_eq(x**2 - 2)
85
+ assert minimal_polynomial(sqrt(2), x) == x**2 - 2
86
+
87
+ assert minimal_polynomial(sqrt(2), polys=True) == Poly(x**2 - 2)
88
+ assert minimal_polynomial(sqrt(2), x, polys=True) == Poly(x**2 - 2, domain='QQ')
89
+ assert minimal_polynomial(sqrt(2), x, polys=True, compose=False) == Poly(x**2 - 2, domain='QQ')
90
+
91
+ a = AlgebraicNumber(sqrt(2))
92
+ b = AlgebraicNumber(sqrt(3))
93
+
94
+ assert minimal_polynomial(a, x) == x**2 - 2
95
+ assert minimal_polynomial(b, x) == x**2 - 3
96
+
97
+ assert minimal_polynomial(a, x, polys=True) == Poly(x**2 - 2, domain='QQ')
98
+ assert minimal_polynomial(b, x, polys=True) == Poly(x**2 - 3, domain='QQ')
99
+
100
+ assert minimal_polynomial(sqrt(a/2 + 17), x) == 2*x**4 - 68*x**2 + 577
101
+ assert minimal_polynomial(sqrt(b/2 + 17), x) == 4*x**4 - 136*x**2 + 1153
102
+
103
+ a, b = sqrt(2)/3 + 7, AlgebraicNumber(sqrt(2)/3 + 7)
104
+
105
+ f = 81*x**8 - 2268*x**6 - 4536*x**5 + 22644*x**4 + 63216*x**3 - \
106
+ 31608*x**2 - 189648*x + 141358
107
+
108
+ assert minimal_polynomial(sqrt(a) + sqrt(sqrt(a)), x) == f
109
+ assert minimal_polynomial(sqrt(b) + sqrt(sqrt(b)), x) == f
110
+
111
+ assert minimal_polynomial(
112
+ a**Q(3, 2), x) == 729*x**4 - 506898*x**2 + 84604519
113
+
114
+ # issue 5994
115
+ eq = S('''
116
+ -1/(800*sqrt(-1/240 + 1/(18000*(-1/17280000 +
117
+ sqrt(15)*I/28800000)**(1/3)) + 2*(-1/17280000 +
118
+ sqrt(15)*I/28800000)**(1/3)))''')
119
+ assert minimal_polynomial(eq, x) == 8000*x**2 - 1
120
+
121
+ ex = (sqrt(5)*sqrt(I)/(5*sqrt(1 + 125*I))
122
+ + 25*sqrt(5)/(I**Q(5,2)*(1 + 125*I)**Q(3,2))
123
+ + 3125*sqrt(5)/(I**Q(11,2)*(1 + 125*I)**Q(3,2))
124
+ + 5*I*sqrt(1 - I/125))
125
+ mp = minimal_polynomial(ex, x)
126
+ assert mp == 25*x**4 + 5000*x**2 + 250016
127
+
128
+ ex = 1 + sqrt(2) + sqrt(3)
129
+ mp = minimal_polynomial(ex, x)
130
+ assert mp == x**4 - 4*x**3 - 4*x**2 + 16*x - 8
131
+
132
+ ex = 1/(1 + sqrt(2) + sqrt(3))
133
+ mp = minimal_polynomial(ex, x)
134
+ assert mp == 8*x**4 - 16*x**3 + 4*x**2 + 4*x - 1
135
+
136
+ p = (expand((1 + sqrt(2) - 2*sqrt(3) + sqrt(7))**3))**Rational(1, 3)
137
+ mp = minimal_polynomial(p, x)
138
+ assert mp == x**8 - 8*x**7 - 56*x**6 + 448*x**5 + 480*x**4 - 5056*x**3 + 1984*x**2 + 7424*x - 3008
139
+ p = expand((1 + sqrt(2) - 2*sqrt(3) + sqrt(7))**3)
140
+ mp = minimal_polynomial(p, x)
141
+ assert mp == x**8 - 512*x**7 - 118208*x**6 + 31131136*x**5 + 647362560*x**4 - 56026611712*x**3 + 116994310144*x**2 + 404854931456*x - 27216576512
142
+
143
+ assert minimal_polynomial(S("-sqrt(5)/2 - 1/2 + (-sqrt(5)/2 - 1/2)**2"), x) == x - 1
144
+ a = 1 + sqrt(2)
145
+ assert minimal_polynomial((a*sqrt(2) + a)**3, x) == x**2 - 198*x + 1
146
+
147
+ p = 1/(1 + sqrt(2) + sqrt(3))
148
+ assert minimal_polynomial(p, x, compose=False) == 8*x**4 - 16*x**3 + 4*x**2 + 4*x - 1
149
+
150
+ p = 2/(1 + sqrt(2) + sqrt(3))
151
+ assert minimal_polynomial(p, x, compose=False) == x**4 - 4*x**3 + 2*x**2 + 4*x - 2
152
+
153
+ assert minimal_polynomial(1 + sqrt(2)*I, x, compose=False) == x**2 - 2*x + 3
154
+ assert minimal_polynomial(1/(1 + sqrt(2)) + 1, x, compose=False) == x**2 - 2
155
+ assert minimal_polynomial(sqrt(2)*I + I*(1 + sqrt(2)), x,
156
+ compose=False) == x**4 + 18*x**2 + 49
157
+
158
+ # minimal polynomial of I
159
+ assert minimal_polynomial(I, x, domain=QQ.algebraic_field(I)) == x - I
160
+ K = QQ.algebraic_field(I*(sqrt(2) + 1))
161
+ assert minimal_polynomial(I, x, domain=K) == x - I
162
+ assert minimal_polynomial(I, x, domain=QQ) == x**2 + 1
163
+ assert minimal_polynomial(I, x, domain='QQ(y)') == x**2 + 1
164
+
165
+ #issue 11553
166
+ assert minimal_polynomial(GoldenRatio, x) == x**2 - x - 1
167
+ assert minimal_polynomial(TribonacciConstant + 3, x) == x**3 - 10*x**2 + 32*x - 34
168
+ assert minimal_polynomial(GoldenRatio, x, domain=QQ.algebraic_field(sqrt(5))) == \
169
+ 2*x - sqrt(5) - 1
170
+ assert minimal_polynomial(TribonacciConstant, x, domain=QQ.algebraic_field(cbrt(19 - 3*sqrt(33)))) == \
171
+ 48*x - 19*(19 - 3*sqrt(33))**Rational(2, 3) - 3*sqrt(33)*(19 - 3*sqrt(33))**Rational(2, 3) \
172
+ - 16*(19 - 3*sqrt(33))**Rational(1, 3) - 16
173
+
174
+ # AlgebraicNumber with an alias.
175
+ # Wester H24
176
+ phi = AlgebraicNumber(S.GoldenRatio.expand(func=True), alias='phi')
177
+ assert minimal_polynomial(phi, x) == x**2 - x - 1
178
+
179
+
180
+ def test_issue_26903():
181
+ p1 = nextprime(10**16) # greater than 10**15
182
+ p2 = nextprime(p1)
183
+ assert sqrt(p1**2*p2).is_Pow # square not extracted
184
+ zero = sqrt(p1**2*p2) - p1*sqrt(p2)
185
+ assert minimal_polynomial(zero, x) == x
186
+ assert minimal_polynomial(sqrt(2) - zero, x) == x**2 - 2
187
+
188
+
189
+ def test_issue_8353():
190
+ assert minimal_polynomial(exp(3*I*pi, evaluate=False), x) == x + 1
191
+ assert minimal_polynomial(Pow(8, S(1)/3, evaluate=False), x
192
+ ) == x - 2
193
+
194
+
195
+ def test_minimal_polynomial_issue_19732():
196
+ # https://github.com/sympy/sympy/issues/19732
197
+ expr = (-280898097948878450887044002323982963174671632174995451265117559518123750720061943079105185551006003416773064305074191140286225850817291393988597615/(-488144716373031204149459129212782509078221364279079444636386844223983756114492222145074506571622290776245390771587888364089507840000000*sqrt(238368341569)*sqrt(S(11918417078450)/63568729
198
+ - 24411360*sqrt(238368341569)/63568729) +
199
+ 238326799225996604451373809274348704114327860564921529846705817404208077866956345381951726531296652901169111729944612727047670549086208000000*sqrt(S(11918417078450)/63568729
200
+ - 24411360*sqrt(238368341569)/63568729)) -
201
+ 180561807339168676696180573852937120123827201075968945871075967679148461189459480842956689723484024031016208588658753107/(-59358007109636562851035004992802812513575019937126272896569856090962677491318275291141463850327474176000000*sqrt(238368341569)*sqrt(S(11918417078450)/63568729
202
+ - 24411360*sqrt(238368341569)/63568729) +
203
+ 28980348180319251787320809875930301310576055074938369007463004788921613896002936637780993064387310446267596800000*sqrt(S(11918417078450)/63568729
204
+ - 24411360*sqrt(238368341569)/63568729)))
205
+ poly = (2151288870990266634727173620565483054187142169311153766675688628985237817262915166497766867289157986631135400926544697981091151416655364879773546003475813114962656742744975460025956167152918469472166170500512008351638710934022160294849059721218824490226159355197136265032810944357335461128949781377875451881300105989490353140886315677977149440000000000000000000000*x**4
206
+ - 5773274155644072033773937864114266313663195672820501581692669271302387257492905909558846459600429795784309388968498783843631580008547382703258503404023153694528041873101120067477617592651525155101107144042679962433039557235772239171616433004024998230222455940044709064078962397144550855715640331680262171410099614469231080995436488414164502751395405398078353242072696360734131090111239998110773292915337556205692674790561090109440000000000000*x**2
207
+ + 211295968822207088328287206509522887719741955693091053353263782924470627623790749534705683380138972642560898936171035770539616881000369889020398551821767092685775598633794696371561234818461806577723412581353857653829324364446419444210520602157621008010129702779407422072249192199762604318993590841636967747488049176548615614290254356975376588506729604345612047361483789518445332415765213187893207704958013682516462853001964919444736320672860140355089)
208
+ assert minimal_polynomial(expr, x) == poly
209
+
210
+
211
+ def test_minimal_polynomial_hi_prec():
212
+ p = 1/sqrt(1 - 9*sqrt(2) + 7*sqrt(3) + Rational(1, 10)**30)
213
+ mp = minimal_polynomial(p, x)
214
+ # checked with Wolfram Alpha
215
+ assert mp.coeff(x**6) == -1232000000000000000000000000001223999999999999999999999999999987999999999999999999999999999996000000000000000000000000000000
216
+
217
+
218
+ def test_minimal_polynomial_sq():
219
+ from sympy.core.add import Add
220
+ from sympy.core.function import expand_multinomial
221
+ p = expand_multinomial((1 + 5*sqrt(2) + 2*sqrt(3))**3)
222
+ mp = minimal_polynomial(p**Rational(1, 3), x)
223
+ assert mp == x**4 - 4*x**3 - 118*x**2 + 244*x + 1321
224
+ p = expand_multinomial((1 + sqrt(2) - 2*sqrt(3) + sqrt(7))**3)
225
+ mp = minimal_polynomial(p**Rational(1, 3), x)
226
+ assert mp == x**8 - 8*x**7 - 56*x**6 + 448*x**5 + 480*x**4 - 5056*x**3 + 1984*x**2 + 7424*x - 3008
227
+ p = Add(*[sqrt(i) for i in range(1, 12)])
228
+ mp = minimal_polynomial(p, x)
229
+ assert mp.subs({x: 0}) == -71965773323122507776
230
+
231
+
232
+ def test_minpoly_compose():
233
+ # issue 6868
234
+ eq = S('''
235
+ -1/(800*sqrt(-1/240 + 1/(18000*(-1/17280000 +
236
+ sqrt(15)*I/28800000)**(1/3)) + 2*(-1/17280000 +
237
+ sqrt(15)*I/28800000)**(1/3)))''')
238
+ mp = minimal_polynomial(eq + 3, x)
239
+ assert mp == 8000*x**2 - 48000*x + 71999
240
+
241
+ # issue 5888
242
+ assert minimal_polynomial(exp(I*pi/8), x) == x**8 + 1
243
+
244
+ mp = minimal_polynomial(sin(pi/7) + sqrt(2), x)
245
+ assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \
246
+ 770912*x**4 - 268432*x**2 + 28561
247
+ mp = minimal_polynomial(cos(pi/7) + sqrt(2), x)
248
+ assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \
249
+ 232*x - 239
250
+ mp = minimal_polynomial(exp(I*pi/7) + sqrt(2), x)
251
+ assert mp == x**12 - 2*x**11 - 9*x**10 + 16*x**9 + 43*x**8 - 70*x**7 - 97*x**6 + 126*x**5 + 211*x**4 - 212*x**3 - 37*x**2 + 142*x + 127
252
+
253
+ mp = minimal_polynomial(sin(pi/7) + sqrt(2), x)
254
+ assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \
255
+ 770912*x**4 - 268432*x**2 + 28561
256
+ mp = minimal_polynomial(cos(pi/7) + sqrt(2), x)
257
+ assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \
258
+ 232*x - 239
259
+ mp = minimal_polynomial(exp(I*pi/7) + sqrt(2), x)
260
+ assert mp == x**12 - 2*x**11 - 9*x**10 + 16*x**9 + 43*x**8 - 70*x**7 - 97*x**6 + 126*x**5 + 211*x**4 - 212*x**3 - 37*x**2 + 142*x + 127
261
+
262
+ mp = minimal_polynomial(exp(I*pi*Rational(2, 7)), x)
263
+ assert mp == x**6 + x**5 + x**4 + x**3 + x**2 + x + 1
264
+ mp = minimal_polynomial(exp(I*pi*Rational(2, 15)), x)
265
+ assert mp == x**8 - x**7 + x**5 - x**4 + x**3 - x + 1
266
+ mp = minimal_polynomial(cos(pi*Rational(2, 7)), x)
267
+ assert mp == 8*x**3 + 4*x**2 - 4*x - 1
268
+ mp = minimal_polynomial(sin(pi*Rational(2, 7)), x)
269
+ ex = (5*cos(pi*Rational(2, 7)) - 7)/(9*cos(pi/7) - 5*cos(pi*Rational(3, 7)))
270
+ mp = minimal_polynomial(ex, x)
271
+ assert mp == x**3 + 2*x**2 - x - 1
272
+ assert minimal_polynomial(-1/(2*cos(pi/7)), x) == x**3 + 2*x**2 - x - 1
273
+ assert minimal_polynomial(sin(pi*Rational(2, 15)), x) == \
274
+ 256*x**8 - 448*x**6 + 224*x**4 - 32*x**2 + 1
275
+ assert minimal_polynomial(sin(pi*Rational(5, 14)), x) == 8*x**3 - 4*x**2 - 4*x + 1
276
+ assert minimal_polynomial(cos(pi/15), x) == 16*x**4 + 8*x**3 - 16*x**2 - 8*x + 1
277
+
278
+ ex = rootof(x**3 +x*4 + 1, 0)
279
+ mp = minimal_polynomial(ex, x)
280
+ assert mp == x**3 + 4*x + 1
281
+ mp = minimal_polynomial(ex + 1, x)
282
+ assert mp == x**3 - 3*x**2 + 7*x - 4
283
+ assert minimal_polynomial(exp(I*pi/3), x) == x**2 - x + 1
284
+ assert minimal_polynomial(exp(I*pi/4), x) == x**4 + 1
285
+ assert minimal_polynomial(exp(I*pi/6), x) == x**4 - x**2 + 1
286
+ assert minimal_polynomial(exp(I*pi/9), x) == x**6 - x**3 + 1
287
+ assert minimal_polynomial(exp(I*pi/10), x) == x**8 - x**6 + x**4 - x**2 + 1
288
+ assert minimal_polynomial(sin(pi/9), x) == 64*x**6 - 96*x**4 + 36*x**2 - 3
289
+ assert minimal_polynomial(sin(pi/11), x) == 1024*x**10 - 2816*x**8 + \
290
+ 2816*x**6 - 1232*x**4 + 220*x**2 - 11
291
+ assert minimal_polynomial(sin(pi/21), x) == 4096*x**12 - 11264*x**10 + \
292
+ 11264*x**8 - 4992*x**6 + 960*x**4 - 64*x**2 + 1
293
+ assert minimal_polynomial(cos(pi/9), x) == 8*x**3 - 6*x - 1
294
+
295
+ ex = 2**Rational(1, 3)*exp(2*I*pi/3)
296
+ assert minimal_polynomial(ex, x) == x**3 - 2
297
+
298
+ raises(NotAlgebraic, lambda: minimal_polynomial(cos(pi*sqrt(2)), x))
299
+ raises(NotAlgebraic, lambda: minimal_polynomial(sin(pi*sqrt(2)), x))
300
+ raises(NotAlgebraic, lambda: minimal_polynomial(exp(1.618*I*pi), x))
301
+ raises(NotAlgebraic, lambda: minimal_polynomial(exp(I*pi*sqrt(2)), x))
302
+
303
+ # issue 5934
304
+ ex = 1/(-36000 - 7200*sqrt(5) + (12*sqrt(10)*sqrt(sqrt(5) + 5) +
305
+ 24*sqrt(10)*sqrt(-sqrt(5) + 5))**2) + 1
306
+ raises(ZeroDivisionError, lambda: minimal_polynomial(ex, x))
307
+
308
+ ex = sqrt(1 + 2**Rational(1,3)) + sqrt(1 + 2**Rational(1,4)) + sqrt(2)
309
+ mp = minimal_polynomial(ex, x)
310
+ assert degree(mp) == 48 and mp.subs({x:0}) == -16630256576
311
+
312
+ ex = tan(pi/5, evaluate=False)
313
+ mp = minimal_polynomial(ex, x)
314
+ assert mp == x**4 - 10*x**2 + 5
315
+ assert mp.subs(x, tan(pi/5)).is_zero
316
+
317
+ ex = tan(pi/6, evaluate=False)
318
+ mp = minimal_polynomial(ex, x)
319
+ assert mp == 3*x**2 - 1
320
+ assert mp.subs(x, tan(pi/6)).is_zero
321
+
322
+ ex = tan(pi/10, evaluate=False)
323
+ mp = minimal_polynomial(ex, x)
324
+ assert mp == 5*x**4 - 10*x**2 + 1
325
+ assert mp.subs(x, tan(pi/10)).is_zero
326
+
327
+ raises(NotAlgebraic, lambda: minimal_polynomial(tan(pi*sqrt(2)), x))
328
+
329
+
330
+ def test_minpoly_issue_7113():
331
+ # see discussion in https://github.com/sympy/sympy/pull/2234
332
+ from sympy.simplify.simplify import nsimplify
333
+ r = nsimplify(pi, tolerance=0.000000001)
334
+ mp = minimal_polynomial(r, x)
335
+ assert mp == 1768292677839237920489538677417507171630859375*x**109 - \
336
+ 2734577732179183863586489182929671773182898498218854181690460140337930774573792597743853652058046464
337
+
338
+
339
+ def test_minpoly_issue_23677():
340
+ r1 = CRootOf(4000000*x**3 - 239960000*x**2 + 4782399900*x - 31663998001, 0)
341
+ r2 = CRootOf(4000000*x**3 - 239960000*x**2 + 4782399900*x - 31663998001, 1)
342
+ num = (7680000000000000000*r1**4*r2**4 - 614323200000000000000*r1**4*r2**3
343
+ + 18458112576000000000000*r1**4*r2**2 - 246896663036160000000000*r1**4*r2
344
+ + 1240473830323209600000000*r1**4 - 614323200000000000000*r1**3*r2**4
345
+ - 1476464424954240000000000*r1**3*r2**2 - 99225501687553535904000000*r1**3
346
+ + 18458112576000000000000*r1**2*r2**4 - 1476464424954240000000000*r1**2*r2**3
347
+ - 593391458458356671712000000*r1**2*r2 + 2981354896834339226880720000*r1**2
348
+ - 246896663036160000000000*r1*r2**4 - 593391458458356671712000000*r1*r2**2
349
+ - 39878756418031796275267195200*r1 + 1240473830323209600000000*r2**4
350
+ - 99225501687553535904000000*r2**3 + 2981354896834339226880720000*r2**2 -
351
+ 39878756418031796275267195200*r2 + 200361370275616536577343808012)
352
+ mp = (x**3 + 59426520028417434406408556687919*x**2 +
353
+ 1161475464966574421163316896737773190861975156439163671112508400*x +
354
+ 7467465541178623874454517208254940823818304424383315270991298807299003671748074773558707779600)
355
+ assert minimal_polynomial(num, x) == mp
356
+
357
+
358
+ def test_minpoly_issue_7574():
359
+ ex = -(-1)**Rational(1, 3) + (-1)**Rational(2,3)
360
+ assert minimal_polynomial(ex, x) == x + 1
361
+
362
+
363
+ def test_choose_factor():
364
+ # Test that this does not enter an infinite loop:
365
+ bad_factors = [Poly(x-2, x), Poly(x+2, x)]
366
+ raises(NotImplementedError, lambda: _choose_factor(bad_factors, x, sqrt(3)))
367
+
368
+
369
+ def test_minpoly_fraction_field():
370
+ assert minimal_polynomial(1/x, y) == -x*y + 1
371
+ assert minimal_polynomial(1 / (x + 1), y) == (x + 1)*y - 1
372
+
373
+ assert minimal_polynomial(sqrt(x), y) == y**2 - x
374
+ assert minimal_polynomial(sqrt(x + 1), y) == y**2 - x - 1
375
+ assert minimal_polynomial(sqrt(x) / x, y) == x*y**2 - 1
376
+ assert minimal_polynomial(sqrt(2) * sqrt(x), y) == y**2 - 2 * x
377
+ assert minimal_polynomial(sqrt(2) + sqrt(x), y) == \
378
+ y**4 + (-2*x - 4)*y**2 + x**2 - 4*x + 4
379
+
380
+ assert minimal_polynomial(x**Rational(1,3), y) == y**3 - x
381
+ assert minimal_polynomial(x**Rational(1,3) + sqrt(x), y) == \
382
+ y**6 - 3*x*y**4 - 2*x*y**3 + 3*x**2*y**2 - 6*x**2*y - x**3 + x**2
383
+
384
+ assert minimal_polynomial(sqrt(x) / z, y) == z**2*y**2 - x
385
+ assert minimal_polynomial(sqrt(x) / (z + 1), y) == (z**2 + 2*z + 1)*y**2 - x
386
+
387
+ assert minimal_polynomial(1/x, y, polys=True) == Poly(-x*y + 1, y, domain='ZZ(x)')
388
+ assert minimal_polynomial(1 / (x + 1), y, polys=True) == \
389
+ Poly((x + 1)*y - 1, y, domain='ZZ(x)')
390
+ assert minimal_polynomial(sqrt(x), y, polys=True) == Poly(y**2 - x, y, domain='ZZ(x)')
391
+ assert minimal_polynomial(sqrt(x) / z, y, polys=True) == \
392
+ Poly(z**2*y**2 - x, y, domain='ZZ(x, z)')
393
+
394
+ # this is (sqrt(1 + x**3)/x).integrate(x).diff(x) - sqrt(1 + x**3)/x
395
+ a = sqrt(x)/sqrt(1 + x**(-3)) - sqrt(x**3 + 1)/x + 1/(x**Rational(5, 2)* \
396
+ (1 + x**(-3))**Rational(3, 2)) + 1/(x**Rational(11, 2)*(1 + x**(-3))**Rational(3, 2))
397
+
398
+ assert minimal_polynomial(a, y) == y
399
+
400
+ raises(NotAlgebraic, lambda: minimal_polynomial(exp(x), y))
401
+ raises(GeneratorsError, lambda: minimal_polynomial(sqrt(x), x))
402
+ raises(GeneratorsError, lambda: minimal_polynomial(sqrt(x) - y, x))
403
+ raises(NotImplementedError, lambda: minimal_polynomial(sqrt(x), y, compose=False))
404
+
405
+ @slow
406
+ def test_minpoly_fraction_field_slow():
407
+ assert minimal_polynomial(minimal_polynomial(sqrt(x**Rational(1,5) - 1),
408
+ y).subs(y, sqrt(x**Rational(1,5) - 1)), z) == z
409
+
410
+ def test_minpoly_domain():
411
+ assert minimal_polynomial(sqrt(2), x, domain=QQ.algebraic_field(sqrt(2))) == \
412
+ x - sqrt(2)
413
+ assert minimal_polynomial(sqrt(8), x, domain=QQ.algebraic_field(sqrt(2))) == \
414
+ x - 2*sqrt(2)
415
+ assert minimal_polynomial(sqrt(Rational(3,2)), x,
416
+ domain=QQ.algebraic_field(sqrt(2))) == 2*x**2 - 3
417
+
418
+ raises(NotAlgebraic, lambda: minimal_polynomial(y, x, domain=QQ))
419
+
420
+
421
+ def test_issue_14831():
422
+ a = -2*sqrt(2)*sqrt(12*sqrt(2) + 17)
423
+ assert minimal_polynomial(a, x) == x**2 + 16*x - 8
424
+ e = (-3*sqrt(12*sqrt(2) + 17) + 12*sqrt(2) +
425
+ 17 - 2*sqrt(2)*sqrt(12*sqrt(2) + 17))
426
+ assert minimal_polynomial(e, x) == x
427
+
428
+
429
+ def test_issue_18248():
430
+ assert nonlinsolve([x*y**3-sqrt(2)/3, x*y**6-4/(9*(sqrt(3)))],x,y) == \
431
+ FiniteSet((sqrt(3)/2, sqrt(6)/3), (sqrt(3)/2, -sqrt(6)/6 - sqrt(2)*I/2),
432
+ (sqrt(3)/2, -sqrt(6)/6 + sqrt(2)*I/2))
433
+
434
+
435
+ def test_issue_13230():
436
+ c1 = Circle(Point2D(3, sqrt(5)), 5)
437
+ c2 = Circle(Point2D(4, sqrt(7)), 6)
438
+ assert intersection(c1, c2) == [Point2D(-1 + (-sqrt(7) + sqrt(5))*(-2*sqrt(7)/29
439
+ + 9*sqrt(5)/29 + sqrt(196*sqrt(35) + 1941)/29), -2*sqrt(7)/29 + 9*sqrt(5)/29
440
+ + sqrt(196*sqrt(35) + 1941)/29), Point2D(-1 + (-sqrt(7) + sqrt(5))*(-sqrt(196*sqrt(35)
441
+ + 1941)/29 - 2*sqrt(7)/29 + 9*sqrt(5)/29), -sqrt(196*sqrt(35) + 1941)/29 - 2*sqrt(7)/29 + 9*sqrt(5)/29)]
442
+
443
+ def test_issue_19760():
444
+ e = 1/(sqrt(1 + sqrt(2)) - sqrt(2)*sqrt(1 + sqrt(2))) + 1
445
+ mp_expected = x**4 - 4*x**3 + 4*x**2 - 2
446
+
447
+ for comp in (True, False):
448
+ mp = Poly(minimal_polynomial(e, compose=comp))
449
+ assert mp(x) == mp_expected, "minimal_polynomial(e, compose=%s) = %s; %s expected" % (comp, mp(x), mp_expected)
450
+
451
+
452
+ def test_issue_20163():
453
+ assert apart(1/(x**6+1), extension=[sqrt(3), I]) == \
454
+ (sqrt(3) + I)/(2*x + sqrt(3) + I)/6 + \
455
+ (sqrt(3) - I)/(2*x + sqrt(3) - I)/6 - \
456
+ (sqrt(3) - I)/(2*x - sqrt(3) + I)/6 - \
457
+ (sqrt(3) + I)/(2*x - sqrt(3) - I)/6 + \
458
+ I/(x + I)/6 - I/(x - I)/6
459
+
460
+
461
+ def test_issue_22559():
462
+ alpha = AlgebraicNumber(sqrt(2))
463
+ assert minimal_polynomial(alpha**3, x) == x**2 - 8
464
+
465
+
466
+ def test_issue_22561():
467
+ a = AlgebraicNumber(sqrt(2) + sqrt(3), [S(1) / 2, 0, S(-9) / 2, 0], gen=x)
468
+ assert a.as_expr() == sqrt(2)
469
+ assert minimal_polynomial(a, x) == x**2 - 2
470
+ assert minimal_polynomial(a**3, x) == x**2 - 8
471
+
472
+
473
+ def test_separate_sq_not_impl():
474
+ raises(NotImplementedError, lambda: _separate_sq(x**(S(1)/3) + x))
475
+
476
+
477
+ def test_minpoly_op_algebraic_element_not_impl():
478
+ raises(NotImplementedError,
479
+ lambda: _minpoly_op_algebraic_element(Pow, sqrt(2), sqrt(3), x, QQ))
480
+
481
+
482
+ def test_minpoly_groebner():
483
+ assert _minpoly_groebner(S(2)/3, x, Poly) == 3*x - 2
484
+ assert _minpoly_groebner(
485
+ (sqrt(2) + 3)*(sqrt(2) + 1), x, Poly) == x**2 - 10*x - 7
486
+ assert _minpoly_groebner((sqrt(2) + 3)**(S(1)/3)*(sqrt(2) + 1)**(S(1)/3),
487
+ x, Poly) == x**6 - 10*x**3 - 7
488
+ assert _minpoly_groebner((sqrt(2) + 3)**(-S(1)/3)*(sqrt(2) + 1)**(S(1)/3),
489
+ x, Poly) == 7*x**6 - 2*x**3 - 1
490
+ raises(NotAlgebraic, lambda: _minpoly_groebner(pi**2, x, Poly))
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_modules.py ADDED
@@ -0,0 +1,752 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.abc import x, zeta
2
+ from sympy.polys import Poly, cyclotomic_poly
3
+ from sympy.polys.domains import FF, QQ, ZZ
4
+ from sympy.polys.matrices import DomainMatrix, DM
5
+ from sympy.polys.numberfields.exceptions import (
6
+ ClosureFailure, MissingUnityError, StructureError
7
+ )
8
+ from sympy.polys.numberfields.modules import (
9
+ Module, ModuleElement, ModuleEndomorphism, PowerBasis, PowerBasisElement,
10
+ find_min_poly, is_sq_maxrank_HNF, make_mod_elt, to_col,
11
+ )
12
+ from sympy.polys.numberfields.utilities import is_int
13
+ from sympy.polys.polyerrors import UnificationFailed
14
+ from sympy.testing.pytest import raises
15
+
16
+
17
+ def test_to_col():
18
+ c = [1, 2, 3, 4]
19
+ m = to_col(c)
20
+ assert m.domain.is_ZZ
21
+ assert m.shape == (4, 1)
22
+ assert m.flat() == c
23
+
24
+
25
+ def test_Module_NotImplemented():
26
+ M = Module()
27
+ raises(NotImplementedError, lambda: M.n)
28
+ raises(NotImplementedError, lambda: M.mult_tab())
29
+ raises(NotImplementedError, lambda: M.represent(None))
30
+ raises(NotImplementedError, lambda: M.starts_with_unity())
31
+ raises(NotImplementedError, lambda: M.element_from_rational(QQ(2, 3)))
32
+
33
+
34
+ def test_Module_ancestors():
35
+ T = Poly(cyclotomic_poly(5, x))
36
+ A = PowerBasis(T)
37
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
38
+ C = B.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
39
+ D = B.submodule_from_matrix(5 * DomainMatrix.eye(4, ZZ))
40
+ assert C.ancestors(include_self=True) == [A, B, C]
41
+ assert D.ancestors(include_self=True) == [A, B, D]
42
+ assert C.power_basis_ancestor() == A
43
+ assert C.nearest_common_ancestor(D) == B
44
+ M = Module()
45
+ assert M.power_basis_ancestor() is None
46
+
47
+
48
+ def test_Module_compat_col():
49
+ T = Poly(cyclotomic_poly(5, x))
50
+ A = PowerBasis(T)
51
+ col = to_col([1, 2, 3, 4])
52
+ row = col.transpose()
53
+ assert A.is_compat_col(col) is True
54
+ assert A.is_compat_col(row) is False
55
+ assert A.is_compat_col(1) is False
56
+ assert A.is_compat_col(DomainMatrix.eye(3, ZZ)[:, 0]) is False
57
+ assert A.is_compat_col(DomainMatrix.eye(4, QQ)[:, 0]) is False
58
+ assert A.is_compat_col(DomainMatrix.eye(4, ZZ)[:, 0]) is True
59
+
60
+
61
+ def test_Module_call():
62
+ T = Poly(cyclotomic_poly(5, x))
63
+ B = PowerBasis(T)
64
+ assert B(0).col.flat() == [1, 0, 0, 0]
65
+ assert B(1).col.flat() == [0, 1, 0, 0]
66
+ col = DomainMatrix.eye(4, ZZ)[:, 2]
67
+ assert B(col).col == col
68
+ raises(ValueError, lambda: B(-1))
69
+
70
+
71
+ def test_Module_starts_with_unity():
72
+ T = Poly(cyclotomic_poly(5, x))
73
+ A = PowerBasis(T)
74
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
75
+ assert A.starts_with_unity() is True
76
+ assert B.starts_with_unity() is False
77
+
78
+
79
+ def test_Module_basis_elements():
80
+ T = Poly(cyclotomic_poly(5, x))
81
+ A = PowerBasis(T)
82
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
83
+ basis = B.basis_elements()
84
+ bp = B.basis_element_pullbacks()
85
+ for i, (e, p) in enumerate(zip(basis, bp)):
86
+ c = [0] * 4
87
+ assert e.module == B
88
+ assert p.module == A
89
+ c[i] = 1
90
+ assert e == B(to_col(c))
91
+ c[i] = 2
92
+ assert p == A(to_col(c))
93
+
94
+
95
+ def test_Module_zero():
96
+ T = Poly(cyclotomic_poly(5, x))
97
+ A = PowerBasis(T)
98
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
99
+ assert A.zero().col.flat() == [0, 0, 0, 0]
100
+ assert A.zero().module == A
101
+ assert B.zero().col.flat() == [0, 0, 0, 0]
102
+ assert B.zero().module == B
103
+
104
+
105
+ def test_Module_one():
106
+ T = Poly(cyclotomic_poly(5, x))
107
+ A = PowerBasis(T)
108
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
109
+ assert A.one().col.flat() == [1, 0, 0, 0]
110
+ assert A.one().module == A
111
+ assert B.one().col.flat() == [1, 0, 0, 0]
112
+ assert B.one().module == A
113
+
114
+
115
+ def test_Module_element_from_rational():
116
+ T = Poly(cyclotomic_poly(5, x))
117
+ A = PowerBasis(T)
118
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
119
+ rA = A.element_from_rational(QQ(22, 7))
120
+ rB = B.element_from_rational(QQ(22, 7))
121
+ assert rA.coeffs == [22, 0, 0, 0]
122
+ assert rA.denom == 7
123
+ assert rA.module == A
124
+ assert rB.coeffs == [22, 0, 0, 0]
125
+ assert rB.denom == 7
126
+ assert rB.module == A
127
+
128
+
129
+ def test_Module_submodule_from_gens():
130
+ T = Poly(cyclotomic_poly(5, x))
131
+ A = PowerBasis(T)
132
+ gens = [2*A(0), 2*A(1), 6*A(0), 6*A(1)]
133
+ B = A.submodule_from_gens(gens)
134
+ # Because the 3rd and 4th generators do not add anything new, we expect
135
+ # the cols of the matrix of B to just reproduce the first two gens:
136
+ M = gens[0].column().hstack(gens[1].column())
137
+ assert B.matrix == M
138
+ # At least one generator must be provided:
139
+ raises(ValueError, lambda: A.submodule_from_gens([]))
140
+ # All generators must belong to A:
141
+ raises(ValueError, lambda: A.submodule_from_gens([3*A(0), B(0)]))
142
+
143
+
144
+ def test_Module_submodule_from_matrix():
145
+ T = Poly(cyclotomic_poly(5, x))
146
+ A = PowerBasis(T)
147
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
148
+ e = B(to_col([1, 2, 3, 4]))
149
+ f = e.to_parent()
150
+ assert f.col.flat() == [2, 4, 6, 8]
151
+ # Matrix must be over ZZ:
152
+ raises(ValueError, lambda: A.submodule_from_matrix(DomainMatrix.eye(4, QQ)))
153
+ # Number of rows of matrix must equal number of generators of module A:
154
+ raises(ValueError, lambda: A.submodule_from_matrix(2 * DomainMatrix.eye(5, ZZ)))
155
+
156
+
157
+ def test_Module_whole_submodule():
158
+ T = Poly(cyclotomic_poly(5, x))
159
+ A = PowerBasis(T)
160
+ B = A.whole_submodule()
161
+ e = B(to_col([1, 2, 3, 4]))
162
+ f = e.to_parent()
163
+ assert f.col.flat() == [1, 2, 3, 4]
164
+ e0, e1, e2, e3 = B(0), B(1), B(2), B(3)
165
+ assert e2 * e3 == e0
166
+ assert e3 ** 2 == e1
167
+
168
+
169
+ def test_PowerBasis_repr():
170
+ T = Poly(cyclotomic_poly(5, x))
171
+ A = PowerBasis(T)
172
+ assert repr(A) == 'PowerBasis(x**4 + x**3 + x**2 + x + 1)'
173
+
174
+
175
+ def test_PowerBasis_eq():
176
+ T = Poly(cyclotomic_poly(5, x))
177
+ A = PowerBasis(T)
178
+ B = PowerBasis(T)
179
+ assert A == B
180
+
181
+
182
+ def test_PowerBasis_mult_tab():
183
+ T = Poly(cyclotomic_poly(5, x))
184
+ A = PowerBasis(T)
185
+ M = A.mult_tab()
186
+ exp = {0: {0: [1, 0, 0, 0], 1: [0, 1, 0, 0], 2: [0, 0, 1, 0], 3: [0, 0, 0, 1]},
187
+ 1: {1: [0, 0, 1, 0], 2: [0, 0, 0, 1], 3: [-1, -1, -1, -1]},
188
+ 2: {2: [-1, -1, -1, -1], 3: [1, 0, 0, 0]},
189
+ 3: {3: [0, 1, 0, 0]}}
190
+ # We get the table we expect:
191
+ assert M == exp
192
+ # And all entries are of expected type:
193
+ assert all(is_int(c) for u in M for v in M[u] for c in M[u][v])
194
+
195
+
196
+ def test_PowerBasis_represent():
197
+ T = Poly(cyclotomic_poly(5, x))
198
+ A = PowerBasis(T)
199
+ col = to_col([1, 2, 3, 4])
200
+ a = A(col)
201
+ assert A.represent(a) == col
202
+ b = A(col, denom=2)
203
+ raises(ClosureFailure, lambda: A.represent(b))
204
+
205
+
206
+ def test_PowerBasis_element_from_poly():
207
+ T = Poly(cyclotomic_poly(5, x))
208
+ A = PowerBasis(T)
209
+ f = Poly(1 + 2*x)
210
+ g = Poly(x**4)
211
+ h = Poly(0, x)
212
+ assert A.element_from_poly(f).coeffs == [1, 2, 0, 0]
213
+ assert A.element_from_poly(g).coeffs == [-1, -1, -1, -1]
214
+ assert A.element_from_poly(h).coeffs == [0, 0, 0, 0]
215
+
216
+
217
+ def test_PowerBasis_element__conversions():
218
+ k = QQ.cyclotomic_field(5)
219
+ L = QQ.cyclotomic_field(7)
220
+ B = PowerBasis(k)
221
+
222
+ # ANP --> PowerBasisElement
223
+ a = k([QQ(1, 2), QQ(1, 3), 5, 7])
224
+ e = B.element_from_ANP(a)
225
+ assert e.coeffs == [42, 30, 2, 3]
226
+ assert e.denom == 6
227
+
228
+ # PowerBasisElement --> ANP
229
+ assert e.to_ANP() == a
230
+
231
+ # Cannot convert ANP from different field
232
+ d = L([QQ(1, 2), QQ(1, 3), 5, 7])
233
+ raises(UnificationFailed, lambda: B.element_from_ANP(d))
234
+
235
+ # AlgebraicNumber --> PowerBasisElement
236
+ alpha = k.to_alg_num(a)
237
+ eps = B.element_from_alg_num(alpha)
238
+ assert eps.coeffs == [42, 30, 2, 3]
239
+ assert eps.denom == 6
240
+
241
+ # PowerBasisElement --> AlgebraicNumber
242
+ assert eps.to_alg_num() == alpha
243
+
244
+ # Cannot convert AlgebraicNumber from different field
245
+ delta = L.to_alg_num(d)
246
+ raises(UnificationFailed, lambda: B.element_from_alg_num(delta))
247
+
248
+ # When we don't know the field:
249
+ C = PowerBasis(k.ext.minpoly)
250
+ # Can convert from AlgebraicNumber:
251
+ eps = C.element_from_alg_num(alpha)
252
+ assert eps.coeffs == [42, 30, 2, 3]
253
+ assert eps.denom == 6
254
+ # But can't convert back:
255
+ raises(StructureError, lambda: eps.to_alg_num())
256
+
257
+
258
+ def test_Submodule_repr():
259
+ T = Poly(cyclotomic_poly(5, x))
260
+ A = PowerBasis(T)
261
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ), denom=3)
262
+ assert repr(B) == 'Submodule[[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [0, 0, 0, 2]]/3'
263
+
264
+
265
+ def test_Submodule_reduced():
266
+ T = Poly(cyclotomic_poly(5, x))
267
+ A = PowerBasis(T)
268
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
269
+ C = A.submodule_from_matrix(6 * DomainMatrix.eye(4, ZZ), denom=3)
270
+ D = C.reduced()
271
+ assert D.denom == 1 and D == C == B
272
+
273
+
274
+ def test_Submodule_discard_before():
275
+ T = Poly(cyclotomic_poly(5, x))
276
+ A = PowerBasis(T)
277
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
278
+ B.compute_mult_tab()
279
+ C = B.discard_before(2)
280
+ assert C.parent == B.parent
281
+ assert B.is_sq_maxrank_HNF() and not C.is_sq_maxrank_HNF()
282
+ assert C.matrix == B.matrix[:, 2:]
283
+ assert C.mult_tab() == {0: {0: [-2, -2], 1: [0, 0]}, 1: {1: [0, 0]}}
284
+
285
+
286
+ def test_Submodule_QQ_matrix():
287
+ T = Poly(cyclotomic_poly(5, x))
288
+ A = PowerBasis(T)
289
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
290
+ C = A.submodule_from_matrix(6 * DomainMatrix.eye(4, ZZ), denom=3)
291
+ assert C.QQ_matrix == B.QQ_matrix
292
+
293
+
294
+ def test_Submodule_represent():
295
+ T = Poly(cyclotomic_poly(5, x))
296
+ A = PowerBasis(T)
297
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
298
+ C = B.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
299
+ a0 = A(to_col([6, 12, 18, 24]))
300
+ a1 = A(to_col([2, 4, 6, 8]))
301
+ a2 = A(to_col([1, 3, 5, 7]))
302
+
303
+ b1 = B.represent(a1)
304
+ assert b1.flat() == [1, 2, 3, 4]
305
+
306
+ c0 = C.represent(a0)
307
+ assert c0.flat() == [1, 2, 3, 4]
308
+
309
+ Y = A.submodule_from_matrix(DomainMatrix([
310
+ [1, 0, 0, 0],
311
+ [0, 1, 0, 0],
312
+ [0, 0, 1, 0],
313
+ ], (3, 4), ZZ).transpose())
314
+
315
+ U = Poly(cyclotomic_poly(7, x))
316
+ Z = PowerBasis(U)
317
+ z0 = Z(to_col([1, 2, 3, 4, 5, 6]))
318
+
319
+ raises(ClosureFailure, lambda: Y.represent(A(3)))
320
+ raises(ClosureFailure, lambda: B.represent(a2))
321
+ raises(ClosureFailure, lambda: B.represent(z0))
322
+
323
+
324
+ def test_Submodule_is_compat_submodule():
325
+ T = Poly(cyclotomic_poly(5, x))
326
+ A = PowerBasis(T)
327
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
328
+ C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
329
+ D = C.submodule_from_matrix(5 * DomainMatrix.eye(4, ZZ))
330
+ assert B.is_compat_submodule(C) is True
331
+ assert B.is_compat_submodule(A) is False
332
+ assert B.is_compat_submodule(D) is False
333
+
334
+
335
+ def test_Submodule_eq():
336
+ T = Poly(cyclotomic_poly(5, x))
337
+ A = PowerBasis(T)
338
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
339
+ C = A.submodule_from_matrix(6 * DomainMatrix.eye(4, ZZ), denom=3)
340
+ assert C == B
341
+
342
+
343
+ def test_Submodule_add():
344
+ T = Poly(cyclotomic_poly(5, x))
345
+ A = PowerBasis(T)
346
+ B = A.submodule_from_matrix(DomainMatrix([
347
+ [4, 0, 0, 0],
348
+ [0, 4, 0, 0],
349
+ ], (2, 4), ZZ).transpose(), denom=6)
350
+ C = A.submodule_from_matrix(DomainMatrix([
351
+ [0, 10, 0, 0],
352
+ [0, 0, 7, 0],
353
+ ], (2, 4), ZZ).transpose(), denom=15)
354
+ D = A.submodule_from_matrix(DomainMatrix([
355
+ [20, 0, 0, 0],
356
+ [ 0, 20, 0, 0],
357
+ [ 0, 0, 14, 0],
358
+ ], (3, 4), ZZ).transpose(), denom=30)
359
+ assert B + C == D
360
+
361
+ U = Poly(cyclotomic_poly(7, x))
362
+ Z = PowerBasis(U)
363
+ Y = Z.submodule_from_gens([Z(0), Z(1)])
364
+ raises(TypeError, lambda: B + Y)
365
+
366
+
367
+ def test_Submodule_mul():
368
+ T = Poly(cyclotomic_poly(5, x))
369
+ A = PowerBasis(T)
370
+ C = A.submodule_from_matrix(DomainMatrix([
371
+ [0, 10, 0, 0],
372
+ [0, 0, 7, 0],
373
+ ], (2, 4), ZZ).transpose(), denom=15)
374
+ C1 = A.submodule_from_matrix(DomainMatrix([
375
+ [0, 20, 0, 0],
376
+ [0, 0, 14, 0],
377
+ ], (2, 4), ZZ).transpose(), denom=3)
378
+ C2 = A.submodule_from_matrix(DomainMatrix([
379
+ [0, 0, 10, 0],
380
+ [0, 0, 0, 7],
381
+ ], (2, 4), ZZ).transpose(), denom=15)
382
+ C3_unred = A.submodule_from_matrix(DomainMatrix([
383
+ [0, 0, 100, 0],
384
+ [0, 0, 0, 70],
385
+ [0, 0, 0, 70],
386
+ [-49, -49, -49, -49]
387
+ ], (4, 4), ZZ).transpose(), denom=225)
388
+ C3 = A.submodule_from_matrix(DomainMatrix([
389
+ [4900, 4900, 0, 0],
390
+ [4410, 4410, 10, 0],
391
+ [2107, 2107, 7, 7]
392
+ ], (3, 4), ZZ).transpose(), denom=225)
393
+ assert C * 1 == C
394
+ assert C ** 1 == C
395
+ assert C * 10 == C1
396
+ assert C * A(1) == C2
397
+ assert C.mul(C, hnf=False) == C3_unred
398
+ assert C * C == C3
399
+ assert C ** 2 == C3
400
+
401
+
402
+ def test_Submodule_reduce_element():
403
+ T = Poly(cyclotomic_poly(5, x))
404
+ A = PowerBasis(T)
405
+ B = A.whole_submodule()
406
+ b = B(to_col([90, 84, 80, 75]), denom=120)
407
+
408
+ C = B.submodule_from_matrix(DomainMatrix.eye(4, ZZ), denom=2)
409
+ b_bar_expected = B(to_col([30, 24, 20, 15]), denom=120)
410
+ b_bar = C.reduce_element(b)
411
+ assert b_bar == b_bar_expected
412
+
413
+ C = B.submodule_from_matrix(DomainMatrix.eye(4, ZZ), denom=4)
414
+ b_bar_expected = B(to_col([0, 24, 20, 15]), denom=120)
415
+ b_bar = C.reduce_element(b)
416
+ assert b_bar == b_bar_expected
417
+
418
+ C = B.submodule_from_matrix(DomainMatrix.eye(4, ZZ), denom=8)
419
+ b_bar_expected = B(to_col([0, 9, 5, 0]), denom=120)
420
+ b_bar = C.reduce_element(b)
421
+ assert b_bar == b_bar_expected
422
+
423
+ a = A(to_col([1, 2, 3, 4]))
424
+ raises(NotImplementedError, lambda: C.reduce_element(a))
425
+
426
+ C = B.submodule_from_matrix(DomainMatrix([
427
+ [5, 4, 3, 2],
428
+ [0, 8, 7, 6],
429
+ [0, 0,11,12],
430
+ [0, 0, 0, 1]
431
+ ], (4, 4), ZZ).transpose())
432
+ raises(StructureError, lambda: C.reduce_element(b))
433
+
434
+
435
+ def test_is_HNF():
436
+ M = DM([
437
+ [3, 2, 1],
438
+ [0, 2, 1],
439
+ [0, 0, 1]
440
+ ], ZZ)
441
+ M1 = DM([
442
+ [3, 2, 1],
443
+ [0, -2, 1],
444
+ [0, 0, 1]
445
+ ], ZZ)
446
+ M2 = DM([
447
+ [3, 2, 3],
448
+ [0, 2, 1],
449
+ [0, 0, 1]
450
+ ], ZZ)
451
+ assert is_sq_maxrank_HNF(M) is True
452
+ assert is_sq_maxrank_HNF(M1) is False
453
+ assert is_sq_maxrank_HNF(M2) is False
454
+
455
+
456
+ def test_make_mod_elt():
457
+ T = Poly(cyclotomic_poly(5, x))
458
+ A = PowerBasis(T)
459
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
460
+ col = to_col([1, 2, 3, 4])
461
+ eA = make_mod_elt(A, col)
462
+ eB = make_mod_elt(B, col)
463
+ assert isinstance(eA, PowerBasisElement)
464
+ assert not isinstance(eB, PowerBasisElement)
465
+
466
+
467
+ def test_ModuleElement_repr():
468
+ T = Poly(cyclotomic_poly(5, x))
469
+ A = PowerBasis(T)
470
+ e = A(to_col([1, 2, 3, 4]), denom=2)
471
+ assert repr(e) == '[1, 2, 3, 4]/2'
472
+
473
+
474
+ def test_ModuleElement_reduced():
475
+ T = Poly(cyclotomic_poly(5, x))
476
+ A = PowerBasis(T)
477
+ e = A(to_col([2, 4, 6, 8]), denom=2)
478
+ f = e.reduced()
479
+ assert f.denom == 1 and f == e
480
+
481
+
482
+ def test_ModuleElement_reduced_mod_p():
483
+ T = Poly(cyclotomic_poly(5, x))
484
+ A = PowerBasis(T)
485
+ e = A(to_col([20, 40, 60, 80]))
486
+ f = e.reduced_mod_p(7)
487
+ assert f.coeffs == [-1, -2, -3, 3]
488
+
489
+
490
+ def test_ModuleElement_from_int_list():
491
+ T = Poly(cyclotomic_poly(5, x))
492
+ A = PowerBasis(T)
493
+ c = [1, 2, 3, 4]
494
+ assert ModuleElement.from_int_list(A, c).coeffs == c
495
+
496
+
497
+ def test_ModuleElement_len():
498
+ T = Poly(cyclotomic_poly(5, x))
499
+ A = PowerBasis(T)
500
+ e = A(0)
501
+ assert len(e) == 4
502
+
503
+
504
+ def test_ModuleElement_column():
505
+ T = Poly(cyclotomic_poly(5, x))
506
+ A = PowerBasis(T)
507
+ e = A(0)
508
+ col1 = e.column()
509
+ assert col1 == e.col and col1 is not e.col
510
+ col2 = e.column(domain=FF(5))
511
+ assert col2.domain.is_FF
512
+
513
+
514
+ def test_ModuleElement_QQ_col():
515
+ T = Poly(cyclotomic_poly(5, x))
516
+ A = PowerBasis(T)
517
+ e = A(to_col([1, 2, 3, 4]), denom=1)
518
+ f = A(to_col([3, 6, 9, 12]), denom=3)
519
+ assert e.QQ_col == f.QQ_col
520
+
521
+
522
+ def test_ModuleElement_to_ancestors():
523
+ T = Poly(cyclotomic_poly(5, x))
524
+ A = PowerBasis(T)
525
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
526
+ C = B.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
527
+ D = C.submodule_from_matrix(5 * DomainMatrix.eye(4, ZZ))
528
+ eD = D(0)
529
+ eC = eD.to_parent()
530
+ eB = eD.to_ancestor(B)
531
+ eA = eD.over_power_basis()
532
+ assert eC.module is C and eC.coeffs == [5, 0, 0, 0]
533
+ assert eB.module is B and eB.coeffs == [15, 0, 0, 0]
534
+ assert eA.module is A and eA.coeffs == [30, 0, 0, 0]
535
+
536
+ a = A(0)
537
+ raises(ValueError, lambda: a.to_parent())
538
+
539
+
540
+ def test_ModuleElement_compatibility():
541
+ T = Poly(cyclotomic_poly(5, x))
542
+ A = PowerBasis(T)
543
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
544
+ C = B.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
545
+ D = B.submodule_from_matrix(5 * DomainMatrix.eye(4, ZZ))
546
+ assert C(0).is_compat(C(1)) is True
547
+ assert C(0).is_compat(D(0)) is False
548
+ u, v = C(0).unify(D(0))
549
+ assert u.module is B and v.module is B
550
+ assert C(C.represent(u)) == C(0) and D(D.represent(v)) == D(0)
551
+
552
+ u, v = C(0).unify(C(1))
553
+ assert u == C(0) and v == C(1)
554
+
555
+ U = Poly(cyclotomic_poly(7, x))
556
+ Z = PowerBasis(U)
557
+ raises(UnificationFailed, lambda: C(0).unify(Z(1)))
558
+
559
+
560
+ def test_ModuleElement_eq():
561
+ T = Poly(cyclotomic_poly(5, x))
562
+ A = PowerBasis(T)
563
+ e = A(to_col([1, 2, 3, 4]), denom=1)
564
+ f = A(to_col([3, 6, 9, 12]), denom=3)
565
+ assert e == f
566
+
567
+ U = Poly(cyclotomic_poly(7, x))
568
+ Z = PowerBasis(U)
569
+ assert e != Z(0)
570
+ assert e != 3.14
571
+
572
+
573
+ def test_ModuleElement_equiv():
574
+ T = Poly(cyclotomic_poly(5, x))
575
+ A = PowerBasis(T)
576
+ e = A(to_col([1, 2, 3, 4]), denom=1)
577
+ f = A(to_col([3, 6, 9, 12]), denom=3)
578
+ assert e.equiv(f)
579
+
580
+ C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
581
+ g = C(to_col([1, 2, 3, 4]), denom=1)
582
+ h = A(to_col([3, 6, 9, 12]), denom=1)
583
+ assert g.equiv(h)
584
+ assert C(to_col([5, 0, 0, 0]), denom=7).equiv(QQ(15, 7))
585
+
586
+ U = Poly(cyclotomic_poly(7, x))
587
+ Z = PowerBasis(U)
588
+ raises(UnificationFailed, lambda: e.equiv(Z(0)))
589
+
590
+ assert e.equiv(3.14) is False
591
+
592
+
593
+ def test_ModuleElement_add():
594
+ T = Poly(cyclotomic_poly(5, x))
595
+ A = PowerBasis(T)
596
+ C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
597
+ e = A(to_col([1, 2, 3, 4]), denom=6)
598
+ f = A(to_col([5, 6, 7, 8]), denom=10)
599
+ g = C(to_col([1, 1, 1, 1]), denom=2)
600
+ assert e + f == A(to_col([10, 14, 18, 22]), denom=15)
601
+ assert e - f == A(to_col([-5, -4, -3, -2]), denom=15)
602
+ assert e + g == A(to_col([10, 11, 12, 13]), denom=6)
603
+ assert e + QQ(7, 10) == A(to_col([26, 10, 15, 20]), denom=30)
604
+ assert g + QQ(7, 10) == A(to_col([22, 15, 15, 15]), denom=10)
605
+
606
+ U = Poly(cyclotomic_poly(7, x))
607
+ Z = PowerBasis(U)
608
+ raises(TypeError, lambda: e + Z(0))
609
+ raises(TypeError, lambda: e + 3.14)
610
+
611
+
612
+ def test_ModuleElement_mul():
613
+ T = Poly(cyclotomic_poly(5, x))
614
+ A = PowerBasis(T)
615
+ C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
616
+ e = A(to_col([0, 2, 0, 0]), denom=3)
617
+ f = A(to_col([0, 0, 0, 7]), denom=5)
618
+ g = C(to_col([0, 0, 0, 1]), denom=2)
619
+ h = A(to_col([0, 0, 3, 1]), denom=7)
620
+ assert e * f == A(to_col([-14, -14, -14, -14]), denom=15)
621
+ assert e * g == A(to_col([-1, -1, -1, -1]))
622
+ assert e * h == A(to_col([-2, -2, -2, 4]), denom=21)
623
+ assert e * QQ(6, 5) == A(to_col([0, 4, 0, 0]), denom=5)
624
+ assert (g * QQ(10, 21)).equiv(A(to_col([0, 0, 0, 5]), denom=7))
625
+ assert e // QQ(6, 5) == A(to_col([0, 5, 0, 0]), denom=9)
626
+
627
+ U = Poly(cyclotomic_poly(7, x))
628
+ Z = PowerBasis(U)
629
+ raises(TypeError, lambda: e * Z(0))
630
+ raises(TypeError, lambda: e * 3.14)
631
+ raises(TypeError, lambda: e // 3.14)
632
+ raises(ZeroDivisionError, lambda: e // 0)
633
+
634
+
635
+ def test_ModuleElement_div():
636
+ T = Poly(cyclotomic_poly(5, x))
637
+ A = PowerBasis(T)
638
+ C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
639
+ e = A(to_col([0, 2, 0, 0]), denom=3)
640
+ f = A(to_col([0, 0, 0, 7]), denom=5)
641
+ g = C(to_col([1, 1, 1, 1]))
642
+ assert e // f == 10*A(3)//21
643
+ assert e // g == -2*A(2)//9
644
+ assert 3 // g == -A(1)
645
+
646
+
647
+ def test_ModuleElement_pow():
648
+ T = Poly(cyclotomic_poly(5, x))
649
+ A = PowerBasis(T)
650
+ C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
651
+ e = A(to_col([0, 2, 0, 0]), denom=3)
652
+ g = C(to_col([0, 0, 0, 1]), denom=2)
653
+ assert e ** 3 == A(to_col([0, 0, 0, 8]), denom=27)
654
+ assert g ** 2 == C(to_col([0, 3, 0, 0]), denom=4)
655
+ assert e ** 0 == A(to_col([1, 0, 0, 0]))
656
+ assert g ** 0 == A(to_col([1, 0, 0, 0]))
657
+ assert e ** 1 == e
658
+ assert g ** 1 == g
659
+
660
+
661
+ def test_ModuleElement_mod():
662
+ T = Poly(cyclotomic_poly(5, x))
663
+ A = PowerBasis(T)
664
+ e = A(to_col([1, 15, 8, 0]), denom=2)
665
+ assert e % 7 == A(to_col([1, 1, 8, 0]), denom=2)
666
+ assert e % QQ(1, 2) == A.zero()
667
+ assert e % QQ(1, 3) == A(to_col([1, 1, 0, 0]), denom=6)
668
+
669
+ B = A.submodule_from_gens([A(0), 5*A(1), 3*A(2), A(3)])
670
+ assert e % B == A(to_col([1, 5, 2, 0]), denom=2)
671
+
672
+ C = B.whole_submodule()
673
+ raises(TypeError, lambda: e % C)
674
+
675
+
676
+ def test_PowerBasisElement_polys():
677
+ T = Poly(cyclotomic_poly(5, x))
678
+ A = PowerBasis(T)
679
+ e = A(to_col([1, 15, 8, 0]), denom=2)
680
+ assert e.numerator(x=zeta) == Poly(8 * zeta ** 2 + 15 * zeta + 1, domain=ZZ)
681
+ assert e.poly(x=zeta) == Poly(4 * zeta ** 2 + QQ(15, 2) * zeta + QQ(1, 2), domain=QQ)
682
+
683
+
684
+ def test_PowerBasisElement_norm():
685
+ T = Poly(cyclotomic_poly(5, x))
686
+ A = PowerBasis(T)
687
+ lam = A(to_col([1, -1, 0, 0]))
688
+ assert lam.norm() == 5
689
+
690
+
691
+ def test_PowerBasisElement_inverse():
692
+ T = Poly(cyclotomic_poly(5, x))
693
+ A = PowerBasis(T)
694
+ e = A(to_col([1, 1, 1, 1]))
695
+ assert 2 // e == -2*A(1)
696
+ assert e ** -3 == -A(3)
697
+
698
+
699
+ def test_ModuleHomomorphism_matrix():
700
+ T = Poly(cyclotomic_poly(5, x))
701
+ A = PowerBasis(T)
702
+ phi = ModuleEndomorphism(A, lambda a: a ** 2)
703
+ M = phi.matrix()
704
+ assert M == DomainMatrix([
705
+ [1, 0, -1, 0],
706
+ [0, 0, -1, 1],
707
+ [0, 1, -1, 0],
708
+ [0, 0, -1, 0]
709
+ ], (4, 4), ZZ)
710
+
711
+
712
+ def test_ModuleHomomorphism_kernel():
713
+ T = Poly(cyclotomic_poly(5, x))
714
+ A = PowerBasis(T)
715
+ phi = ModuleEndomorphism(A, lambda a: a ** 5)
716
+ N = phi.kernel()
717
+ assert N.n == 3
718
+
719
+
720
+ def test_EndomorphismRing_represent():
721
+ T = Poly(cyclotomic_poly(5, x))
722
+ A = PowerBasis(T)
723
+ R = A.endomorphism_ring()
724
+ phi = R.inner_endomorphism(A(1))
725
+ col = R.represent(phi)
726
+ assert col.transpose() == DomainMatrix([
727
+ [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, -1, -1, -1, -1]
728
+ ], (1, 16), ZZ)
729
+
730
+ B = A.submodule_from_matrix(DomainMatrix.zeros((4, 0), ZZ))
731
+ S = B.endomorphism_ring()
732
+ psi = S.inner_endomorphism(A(1))
733
+ col = S.represent(psi)
734
+ assert col == DomainMatrix([], (0, 0), ZZ)
735
+
736
+ raises(NotImplementedError, lambda: R.represent(3.14))
737
+
738
+
739
+ def test_find_min_poly():
740
+ T = Poly(cyclotomic_poly(5, x))
741
+ A = PowerBasis(T)
742
+ powers = []
743
+ m = find_min_poly(A(1), QQ, x=x, powers=powers)
744
+ assert m == Poly(T, domain=QQ)
745
+ assert len(powers) == 5
746
+
747
+ # powers list need not be passed
748
+ m = find_min_poly(A(1), QQ, x=x)
749
+ assert m == Poly(T, domain=QQ)
750
+
751
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
752
+ raises(MissingUnityError, lambda: find_min_poly(B(1), QQ))
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_numbers.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests on algebraic numbers. """
2
+
3
+ from sympy.core.containers import Tuple
4
+ from sympy.core.numbers import (AlgebraicNumber, I, Rational)
5
+ from sympy.core.singleton import S
6
+ from sympy.core.symbol import Symbol
7
+ from sympy.functions.elementary.miscellaneous import sqrt
8
+ from sympy.polys.polytools import Poly
9
+ from sympy.polys.numberfields.subfield import to_number_field
10
+ from sympy.polys.polyclasses import DMP
11
+ from sympy.polys.domains import QQ
12
+ from sympy.polys.rootoftools import CRootOf
13
+ from sympy.abc import x, y
14
+
15
+
16
+ def test_AlgebraicNumber():
17
+ minpoly, root = x**2 - 2, sqrt(2)
18
+
19
+ a = AlgebraicNumber(root, gen=x)
20
+
21
+ assert a.rep == DMP([QQ(1), QQ(0)], QQ)
22
+ assert a.root == root
23
+ assert a.alias is None
24
+ assert a.minpoly == minpoly
25
+ assert a.is_number
26
+
27
+ assert a.is_aliased is False
28
+
29
+ assert a.coeffs() == [S.One, S.Zero]
30
+ assert a.native_coeffs() == [QQ(1), QQ(0)]
31
+
32
+ a = AlgebraicNumber(root, gen=x, alias='y')
33
+
34
+ assert a.rep == DMP([QQ(1), QQ(0)], QQ)
35
+ assert a.root == root
36
+ assert a.alias == Symbol('y')
37
+ assert a.minpoly == minpoly
38
+ assert a.is_number
39
+
40
+ assert a.is_aliased is True
41
+
42
+ a = AlgebraicNumber(root, gen=x, alias=Symbol('y'))
43
+
44
+ assert a.rep == DMP([QQ(1), QQ(0)], QQ)
45
+ assert a.root == root
46
+ assert a.alias == Symbol('y')
47
+ assert a.minpoly == minpoly
48
+ assert a.is_number
49
+
50
+ assert a.is_aliased is True
51
+
52
+ assert AlgebraicNumber(sqrt(2), []).rep == DMP([], QQ)
53
+ assert AlgebraicNumber(sqrt(2), ()).rep == DMP([], QQ)
54
+ assert AlgebraicNumber(sqrt(2), (0, 0)).rep == DMP([], QQ)
55
+
56
+ assert AlgebraicNumber(sqrt(2), [8]).rep == DMP([QQ(8)], QQ)
57
+ assert AlgebraicNumber(sqrt(2), [Rational(8, 3)]).rep == DMP([QQ(8, 3)], QQ)
58
+
59
+ assert AlgebraicNumber(sqrt(2), [7, 3]).rep == DMP([QQ(7), QQ(3)], QQ)
60
+ assert AlgebraicNumber(
61
+ sqrt(2), [Rational(7, 9), Rational(3, 2)]).rep == DMP([QQ(7, 9), QQ(3, 2)], QQ)
62
+
63
+ assert AlgebraicNumber(sqrt(2), [1, 2, 3]).rep == DMP([QQ(2), QQ(5)], QQ)
64
+
65
+ a = AlgebraicNumber(AlgebraicNumber(root, gen=x), [1, 2])
66
+
67
+ assert a.rep == DMP([QQ(1), QQ(2)], QQ)
68
+ assert a.root == root
69
+ assert a.alias is None
70
+ assert a.minpoly == minpoly
71
+ assert a.is_number
72
+
73
+ assert a.is_aliased is False
74
+
75
+ assert a.coeffs() == [S.One, S(2)]
76
+ assert a.native_coeffs() == [QQ(1), QQ(2)]
77
+
78
+ a = AlgebraicNumber((minpoly, root), [1, 2])
79
+
80
+ assert a.rep == DMP([QQ(1), QQ(2)], QQ)
81
+ assert a.root == root
82
+ assert a.alias is None
83
+ assert a.minpoly == minpoly
84
+ assert a.is_number
85
+
86
+ assert a.is_aliased is False
87
+
88
+ a = AlgebraicNumber((Poly(minpoly), root), [1, 2])
89
+
90
+ assert a.rep == DMP([QQ(1), QQ(2)], QQ)
91
+ assert a.root == root
92
+ assert a.alias is None
93
+ assert a.minpoly == minpoly
94
+ assert a.is_number
95
+
96
+ assert a.is_aliased is False
97
+
98
+ assert AlgebraicNumber( sqrt(3)).rep == DMP([ QQ(1), QQ(0)], QQ)
99
+ assert AlgebraicNumber(-sqrt(3)).rep == DMP([ QQ(1), QQ(0)], QQ)
100
+
101
+ a = AlgebraicNumber(sqrt(2))
102
+ b = AlgebraicNumber(sqrt(2))
103
+
104
+ assert a == b
105
+
106
+ c = AlgebraicNumber(sqrt(2), gen=x)
107
+
108
+ assert a == b
109
+ assert a == c
110
+
111
+ a = AlgebraicNumber(sqrt(2), [1, 2])
112
+ b = AlgebraicNumber(sqrt(2), [1, 3])
113
+
114
+ assert a != b and a != sqrt(2) + 3
115
+
116
+ assert (a == x) is False and (a != x) is True
117
+
118
+ a = AlgebraicNumber(sqrt(2), [1, 0])
119
+ b = AlgebraicNumber(sqrt(2), [1, 0], alias=y)
120
+
121
+ assert a.as_poly(x) == Poly(x, domain='QQ')
122
+ assert b.as_poly() == Poly(y, domain='QQ')
123
+
124
+ assert a.as_expr() == sqrt(2)
125
+ assert a.as_expr(x) == x
126
+ assert b.as_expr() == sqrt(2)
127
+ assert b.as_expr(x) == x
128
+
129
+ a = AlgebraicNumber(sqrt(2), [2, 3])
130
+ b = AlgebraicNumber(sqrt(2), [2, 3], alias=y)
131
+
132
+ p = a.as_poly()
133
+
134
+ assert p == Poly(2*p.gen + 3)
135
+
136
+ assert a.as_poly(x) == Poly(2*x + 3, domain='QQ')
137
+ assert b.as_poly() == Poly(2*y + 3, domain='QQ')
138
+
139
+ assert a.as_expr() == 2*sqrt(2) + 3
140
+ assert a.as_expr(x) == 2*x + 3
141
+ assert b.as_expr() == 2*sqrt(2) + 3
142
+ assert b.as_expr(x) == 2*x + 3
143
+
144
+ a = AlgebraicNumber(sqrt(2))
145
+ b = to_number_field(sqrt(2))
146
+ assert a.args == b.args == (sqrt(2), Tuple(1, 0))
147
+ b = AlgebraicNumber(sqrt(2), alias='alpha')
148
+ assert b.args == (sqrt(2), Tuple(1, 0), Symbol('alpha'))
149
+
150
+ a = AlgebraicNumber(sqrt(2), [1, 2, 3])
151
+ assert a.args == (sqrt(2), Tuple(1, 2, 3))
152
+
153
+ a = AlgebraicNumber(sqrt(2), [1, 2], "alpha")
154
+ b = AlgebraicNumber(a)
155
+ c = AlgebraicNumber(a, alias="gamma")
156
+ assert a == b
157
+ assert c.alias.name == "gamma"
158
+
159
+ a = AlgebraicNumber(sqrt(2) + sqrt(3), [S(1)/2, 0, S(-9)/2, 0])
160
+ b = AlgebraicNumber(a, [1, 0, 0])
161
+ assert b.root == a.root
162
+ assert a.to_root() == sqrt(2)
163
+ assert b.to_root() == 2
164
+
165
+ a = AlgebraicNumber(2)
166
+ assert a.is_primitive_element is True
167
+
168
+
169
+ def test_to_algebraic_integer():
170
+ a = AlgebraicNumber(sqrt(3), gen=x).to_algebraic_integer()
171
+
172
+ assert a.minpoly == x**2 - 3
173
+ assert a.root == sqrt(3)
174
+ assert a.rep == DMP([QQ(1), QQ(0)], QQ)
175
+
176
+ a = AlgebraicNumber(2*sqrt(3), gen=x).to_algebraic_integer()
177
+ assert a.minpoly == x**2 - 12
178
+ assert a.root == 2*sqrt(3)
179
+ assert a.rep == DMP([QQ(1), QQ(0)], QQ)
180
+
181
+ a = AlgebraicNumber(sqrt(3)/2, gen=x).to_algebraic_integer()
182
+
183
+ assert a.minpoly == x**2 - 12
184
+ assert a.root == 2*sqrt(3)
185
+ assert a.rep == DMP([QQ(1), QQ(0)], QQ)
186
+
187
+ a = AlgebraicNumber(sqrt(3)/2, [Rational(7, 19), 3], gen=x).to_algebraic_integer()
188
+
189
+ assert a.minpoly == x**2 - 12
190
+ assert a.root == 2*sqrt(3)
191
+ assert a.rep == DMP([QQ(7, 19), QQ(3)], QQ)
192
+
193
+
194
+ def test_AlgebraicNumber_to_root():
195
+ assert AlgebraicNumber(sqrt(2)).to_root() == sqrt(2)
196
+
197
+ zeta5_squared = AlgebraicNumber(CRootOf(x**5 - 1, 4), coeffs=[1, 0, 0])
198
+ assert zeta5_squared.to_root() == CRootOf(x**4 + x**3 + x**2 + x + 1, 1)
199
+
200
+ zeta3_squared = AlgebraicNumber(CRootOf(x**3 - 1, 2), coeffs=[1, 0, 0])
201
+ assert zeta3_squared.to_root() == -S(1)/2 - sqrt(3)*I/2
202
+ assert zeta3_squared.to_root(radicals=False) == CRootOf(x**2 + x + 1, 0)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_primes.py ADDED
@@ -0,0 +1,296 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from math import prod
2
+
3
+ from sympy import QQ, ZZ
4
+ from sympy.abc import x, theta
5
+ from sympy.ntheory import factorint
6
+ from sympy.ntheory.residue_ntheory import n_order
7
+ from sympy.polys import Poly, cyclotomic_poly
8
+ from sympy.polys.matrices import DomainMatrix
9
+ from sympy.polys.numberfields.basis import round_two
10
+ from sympy.polys.numberfields.exceptions import StructureError
11
+ from sympy.polys.numberfields.modules import PowerBasis, to_col
12
+ from sympy.polys.numberfields.primes import (
13
+ prime_decomp, _two_elt_rep,
14
+ _check_formal_conditions_for_maximal_order,
15
+ )
16
+ from sympy.testing.pytest import raises
17
+
18
+
19
+ def test_check_formal_conditions_for_maximal_order():
20
+ T = Poly(cyclotomic_poly(5, x))
21
+ A = PowerBasis(T)
22
+ B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
23
+ C = B.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ))
24
+ D = A.submodule_from_matrix(DomainMatrix.eye(4, ZZ)[:, :-1])
25
+ # Is a direct submodule of a power basis, but lacks 1 as first generator:
26
+ raises(StructureError, lambda: _check_formal_conditions_for_maximal_order(B))
27
+ # Is not a direct submodule of a power basis:
28
+ raises(StructureError, lambda: _check_formal_conditions_for_maximal_order(C))
29
+ # Is direct submod of pow basis, and starts with 1, but not sq/max rank/HNF:
30
+ raises(StructureError, lambda: _check_formal_conditions_for_maximal_order(D))
31
+
32
+
33
+ def test_two_elt_rep():
34
+ ell = 7
35
+ T = Poly(cyclotomic_poly(ell))
36
+ ZK, dK = round_two(T)
37
+ for p in [29, 13, 11, 5]:
38
+ P = prime_decomp(p, T)
39
+ for Pi in P:
40
+ # We have Pi in two-element representation, and, because we are
41
+ # looking at a cyclotomic field, this was computed by the "easy"
42
+ # method that just factors T mod p. We will now convert this to
43
+ # a set of Z-generators, then convert that back into a two-element
44
+ # rep. The latter need not be identical to the two-elt rep we
45
+ # already have, but it must have the same HNF.
46
+ H = p*ZK + Pi.alpha*ZK
47
+ gens = H.basis_element_pullbacks()
48
+ # Note: we could supply f = Pi.f, but prefer to test behavior without it.
49
+ b = _two_elt_rep(gens, ZK, p)
50
+ if b != Pi.alpha:
51
+ H2 = p*ZK + b*ZK
52
+ assert H2 == H
53
+
54
+
55
+ def test_valuation_at_prime_ideal():
56
+ p = 7
57
+ T = Poly(cyclotomic_poly(p))
58
+ ZK, dK = round_two(T)
59
+ P = prime_decomp(p, T, dK=dK, ZK=ZK)
60
+ assert len(P) == 1
61
+ P0 = P[0]
62
+ v = P0.valuation(p*ZK)
63
+ assert v == P0.e
64
+ # Test easy 0 case:
65
+ assert P0.valuation(5*ZK) == 0
66
+
67
+
68
+ def test_decomp_1():
69
+ # All prime decompositions in cyclotomic fields are in the "easy case,"
70
+ # since the index is unity.
71
+ # Here we check the ramified prime.
72
+ T = Poly(cyclotomic_poly(7))
73
+ raises(ValueError, lambda: prime_decomp(7))
74
+ P = prime_decomp(7, T)
75
+ assert len(P) == 1
76
+ P0 = P[0]
77
+ assert P0.e == 6
78
+ assert P0.f == 1
79
+ # Test powers:
80
+ assert P0**0 == P0.ZK
81
+ assert P0**1 == P0
82
+ assert P0**6 == 7 * P0.ZK
83
+
84
+
85
+ def test_decomp_2():
86
+ # More easy cyclotomic cases, but here we check unramified primes.
87
+ ell = 7
88
+ T = Poly(cyclotomic_poly(ell))
89
+ for p in [29, 13, 11, 5]:
90
+ f_exp = n_order(p, ell)
91
+ g_exp = (ell - 1) // f_exp
92
+ P = prime_decomp(p, T)
93
+ assert len(P) == g_exp
94
+ for Pi in P:
95
+ assert Pi.e == 1
96
+ assert Pi.f == f_exp
97
+
98
+
99
+ def test_decomp_3():
100
+ T = Poly(x ** 2 - 35)
101
+ rad = {}
102
+ ZK, dK = round_two(T, radicals=rad)
103
+ # 35 is 3 mod 4, so field disc is 4*5*7, and theory says each of the
104
+ # rational primes 2, 5, 7 should be the square of a prime ideal.
105
+ for p in [2, 5, 7]:
106
+ P = prime_decomp(p, T, dK=dK, ZK=ZK, radical=rad.get(p))
107
+ assert len(P) == 1
108
+ assert P[0].e == 2
109
+ assert P[0]**2 == p*ZK
110
+
111
+
112
+ def test_decomp_4():
113
+ T = Poly(x ** 2 - 21)
114
+ rad = {}
115
+ ZK, dK = round_two(T, radicals=rad)
116
+ # 21 is 1 mod 4, so field disc is 3*7, and theory says the
117
+ # rational primes 3, 7 should be the square of a prime ideal.
118
+ for p in [3, 7]:
119
+ P = prime_decomp(p, T, dK=dK, ZK=ZK, radical=rad.get(p))
120
+ assert len(P) == 1
121
+ assert P[0].e == 2
122
+ assert P[0]**2 == p*ZK
123
+
124
+
125
+ def test_decomp_5():
126
+ # Here is our first test of the "hard case" of prime decomposition.
127
+ # We work in a quadratic extension Q(sqrt(d)) where d is 1 mod 4, and
128
+ # we consider the factorization of the rational prime 2, which divides
129
+ # the index.
130
+ # Theory says the form of p's factorization depends on the residue of
131
+ # d mod 8, so we consider both cases, d = 1 mod 8 and d = 5 mod 8.
132
+ for d in [-7, -3]:
133
+ T = Poly(x ** 2 - d)
134
+ rad = {}
135
+ ZK, dK = round_two(T, radicals=rad)
136
+ p = 2
137
+ P = prime_decomp(p, T, dK=dK, ZK=ZK, radical=rad.get(p))
138
+ if d % 8 == 1:
139
+ assert len(P) == 2
140
+ assert all(P[i].e == 1 and P[i].f == 1 for i in range(2))
141
+ assert prod(Pi**Pi.e for Pi in P) == p * ZK
142
+ else:
143
+ assert d % 8 == 5
144
+ assert len(P) == 1
145
+ assert P[0].e == 1
146
+ assert P[0].f == 2
147
+ assert P[0].as_submodule() == p * ZK
148
+
149
+
150
+ def test_decomp_6():
151
+ # Another case where 2 divides the index. This is Dedekind's example of
152
+ # an essential discriminant divisor. (See Cohen, Exercise 6.10.)
153
+ T = Poly(x ** 3 + x ** 2 - 2 * x + 8)
154
+ rad = {}
155
+ ZK, dK = round_two(T, radicals=rad)
156
+ p = 2
157
+ P = prime_decomp(p, T, dK=dK, ZK=ZK, radical=rad.get(p))
158
+ assert len(P) == 3
159
+ assert all(Pi.e == Pi.f == 1 for Pi in P)
160
+ assert prod(Pi**Pi.e for Pi in P) == p*ZK
161
+
162
+
163
+ def test_decomp_7():
164
+ # Try working through an AlgebraicField
165
+ T = Poly(x ** 3 + x ** 2 - 2 * x + 8)
166
+ K = QQ.alg_field_from_poly(T)
167
+ p = 2
168
+ P = K.primes_above(p)
169
+ ZK = K.maximal_order()
170
+ assert len(P) == 3
171
+ assert all(Pi.e == Pi.f == 1 for Pi in P)
172
+ assert prod(Pi**Pi.e for Pi in P) == p*ZK
173
+
174
+
175
+ def test_decomp_8():
176
+ # This time we consider various cubics, and try factoring all primes
177
+ # dividing the index.
178
+ cases = (
179
+ x ** 3 + 3 * x ** 2 - 4 * x + 4,
180
+ x ** 3 + 3 * x ** 2 + 3 * x - 3,
181
+ x ** 3 + 5 * x ** 2 - x + 3,
182
+ x ** 3 + 5 * x ** 2 - 5 * x - 5,
183
+ x ** 3 + 3 * x ** 2 + 5,
184
+ x ** 3 + 6 * x ** 2 + 3 * x - 1,
185
+ x ** 3 + 6 * x ** 2 + 4,
186
+ x ** 3 + 7 * x ** 2 + 7 * x - 7,
187
+ x ** 3 + 7 * x ** 2 - x + 5,
188
+ x ** 3 + 7 * x ** 2 - 5 * x + 5,
189
+ x ** 3 + 4 * x ** 2 - 3 * x + 7,
190
+ x ** 3 + 8 * x ** 2 + 5 * x - 1,
191
+ x ** 3 + 8 * x ** 2 - 2 * x + 6,
192
+ x ** 3 + 6 * x ** 2 - 3 * x + 8,
193
+ x ** 3 + 9 * x ** 2 + 6 * x - 8,
194
+ x ** 3 + 15 * x ** 2 - 9 * x + 13,
195
+ )
196
+ def display(T, p, radical, P, I, J):
197
+ """Useful for inspection, when running test manually."""
198
+ print('=' * 20)
199
+ print(T, p, radical)
200
+ for Pi in P:
201
+ print(f' ({Pi!r})')
202
+ print("I: ", I)
203
+ print("J: ", J)
204
+ print(f'Equal: {I == J}')
205
+ inspect = False
206
+ for g in cases:
207
+ T = Poly(g)
208
+ rad = {}
209
+ ZK, dK = round_two(T, radicals=rad)
210
+ dT = T.discriminant()
211
+ f_squared = dT // dK
212
+ F = factorint(f_squared)
213
+ for p in F:
214
+ radical = rad.get(p)
215
+ P = prime_decomp(p, T, dK=dK, ZK=ZK, radical=radical)
216
+ I = prod(Pi**Pi.e for Pi in P)
217
+ J = p * ZK
218
+ if inspect:
219
+ display(T, p, radical, P, I, J)
220
+ assert I == J
221
+
222
+
223
+ def test_PrimeIdeal_eq():
224
+ # `==` should fail on objects of different types, so even a completely
225
+ # inert PrimeIdeal should test unequal to the rational prime it divides.
226
+ T = Poly(cyclotomic_poly(7))
227
+ P0 = prime_decomp(5, T)[0]
228
+ assert P0.f == 6
229
+ assert P0.as_submodule() == 5 * P0.ZK
230
+ assert P0 != 5
231
+
232
+
233
+ def test_PrimeIdeal_add():
234
+ T = Poly(cyclotomic_poly(7))
235
+ P0 = prime_decomp(7, T)[0]
236
+ # Adding ideals computes their GCD, so adding the ramified prime dividing
237
+ # 7 to 7 itself should reproduce this prime (as a submodule).
238
+ assert P0 + 7 * P0.ZK == P0.as_submodule()
239
+
240
+
241
+ def test_str():
242
+ # Without alias:
243
+ k = QQ.alg_field_from_poly(Poly(x**2 + 7))
244
+ frp = k.primes_above(2)[0]
245
+ assert str(frp) == '(2, 3*_x/2 + 1/2)'
246
+
247
+ frp = k.primes_above(3)[0]
248
+ assert str(frp) == '(3)'
249
+
250
+ # With alias:
251
+ k = QQ.alg_field_from_poly(Poly(x ** 2 + 7), alias='alpha')
252
+ frp = k.primes_above(2)[0]
253
+ assert str(frp) == '(2, 3*alpha/2 + 1/2)'
254
+
255
+ frp = k.primes_above(3)[0]
256
+ assert str(frp) == '(3)'
257
+
258
+
259
+ def test_repr():
260
+ T = Poly(x**2 + 7)
261
+ ZK, dK = round_two(T)
262
+ P = prime_decomp(2, T, dK=dK, ZK=ZK)
263
+ assert repr(P[0]) == '[ (2, (3*x + 1)/2) e=1, f=1 ]'
264
+ assert P[0].repr(field_gen=theta) == '[ (2, (3*theta + 1)/2) e=1, f=1 ]'
265
+ assert P[0].repr(field_gen=theta, just_gens=True) == '(2, (3*theta + 1)/2)'
266
+
267
+
268
+ def test_PrimeIdeal_reduce():
269
+ k = QQ.alg_field_from_poly(Poly(x ** 3 + x ** 2 - 2 * x + 8))
270
+ Zk = k.maximal_order()
271
+ P = k.primes_above(2)
272
+ frp = P[2]
273
+
274
+ # reduce_element
275
+ a = Zk.parent(to_col([23, 20, 11]), denom=6)
276
+ a_bar_expected = Zk.parent(to_col([11, 5, 2]), denom=6)
277
+ a_bar = frp.reduce_element(a)
278
+ assert a_bar == a_bar_expected
279
+
280
+ # reduce_ANP
281
+ a = k([QQ(11, 6), QQ(20, 6), QQ(23, 6)])
282
+ a_bar_expected = k([QQ(2, 6), QQ(5, 6), QQ(11, 6)])
283
+ a_bar = frp.reduce_ANP(a)
284
+ assert a_bar == a_bar_expected
285
+
286
+ # reduce_alg_num
287
+ a = k.to_alg_num(a)
288
+ a_bar_expected = k.to_alg_num(a_bar_expected)
289
+ a_bar = frp.reduce_alg_num(a)
290
+ assert a_bar == a_bar_expected
291
+
292
+
293
+ def test_issue_23402():
294
+ k = QQ.alg_field_from_poly(Poly(x ** 3 + x ** 2 - 2 * x + 8))
295
+ P = k.primes_above(3)
296
+ assert P[0].alpha.equiv(0)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_subfield.py ADDED
@@ -0,0 +1,317 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for the subfield problem and allied problems. """
2
+
3
+ from sympy.core.numbers import (AlgebraicNumber, I, pi, Rational)
4
+ from sympy.core.singleton import S
5
+ from sympy.functions.elementary.exponential import exp
6
+ from sympy.functions.elementary.miscellaneous import sqrt
7
+ from sympy.external.gmpy import MPQ
8
+ from sympy.polys.numberfields.subfield import (
9
+ is_isomorphism_possible,
10
+ field_isomorphism_pslq,
11
+ field_isomorphism,
12
+ primitive_element,
13
+ to_number_field,
14
+ )
15
+ from sympy.polys.domains import QQ
16
+ from sympy.polys.polyerrors import IsomorphismFailed
17
+ from sympy.polys.polytools import Poly
18
+ from sympy.polys.rootoftools import CRootOf
19
+ from sympy.testing.pytest import raises
20
+
21
+ from sympy.abc import x
22
+
23
+ Q = Rational
24
+
25
+
26
+ def test_field_isomorphism_pslq():
27
+ a = AlgebraicNumber(I)
28
+ b = AlgebraicNumber(I*sqrt(3))
29
+
30
+ raises(NotImplementedError, lambda: field_isomorphism_pslq(a, b))
31
+
32
+ a = AlgebraicNumber(sqrt(2))
33
+ b = AlgebraicNumber(sqrt(3))
34
+ c = AlgebraicNumber(sqrt(7))
35
+ d = AlgebraicNumber(sqrt(2) + sqrt(3))
36
+ e = AlgebraicNumber(sqrt(2) + sqrt(3) + sqrt(7))
37
+
38
+ assert field_isomorphism_pslq(a, a) == [1, 0]
39
+ assert field_isomorphism_pslq(a, b) is None
40
+ assert field_isomorphism_pslq(a, c) is None
41
+ assert field_isomorphism_pslq(a, d) == [Q(1, 2), 0, -Q(9, 2), 0]
42
+ assert field_isomorphism_pslq(
43
+ a, e) == [Q(1, 80), 0, -Q(1, 2), 0, Q(59, 20), 0]
44
+
45
+ assert field_isomorphism_pslq(b, a) is None
46
+ assert field_isomorphism_pslq(b, b) == [1, 0]
47
+ assert field_isomorphism_pslq(b, c) is None
48
+ assert field_isomorphism_pslq(b, d) == [-Q(1, 2), 0, Q(11, 2), 0]
49
+ assert field_isomorphism_pslq(b, e) == [-Q(
50
+ 3, 640), 0, Q(67, 320), 0, -Q(297, 160), 0, Q(313, 80), 0]
51
+
52
+ assert field_isomorphism_pslq(c, a) is None
53
+ assert field_isomorphism_pslq(c, b) is None
54
+ assert field_isomorphism_pslq(c, c) == [1, 0]
55
+ assert field_isomorphism_pslq(c, d) is None
56
+ assert field_isomorphism_pslq(c, e) == [Q(
57
+ 3, 640), 0, -Q(71, 320), 0, Q(377, 160), 0, -Q(469, 80), 0]
58
+
59
+ assert field_isomorphism_pslq(d, a) is None
60
+ assert field_isomorphism_pslq(d, b) is None
61
+ assert field_isomorphism_pslq(d, c) is None
62
+ assert field_isomorphism_pslq(d, d) == [1, 0]
63
+ assert field_isomorphism_pslq(d, e) == [-Q(
64
+ 3, 640), 0, Q(71, 320), 0, -Q(377, 160), 0, Q(549, 80), 0]
65
+
66
+ assert field_isomorphism_pslq(e, a) is None
67
+ assert field_isomorphism_pslq(e, b) is None
68
+ assert field_isomorphism_pslq(e, c) is None
69
+ assert field_isomorphism_pslq(e, d) is None
70
+ assert field_isomorphism_pslq(e, e) == [1, 0]
71
+
72
+ f = AlgebraicNumber(3*sqrt(2) + 8*sqrt(7) - 5)
73
+
74
+ assert field_isomorphism_pslq(
75
+ f, e) == [Q(3, 80), 0, -Q(139, 80), 0, Q(347, 20), 0, -Q(761, 20), -5]
76
+
77
+
78
+ def test_field_isomorphism():
79
+ assert field_isomorphism(3, sqrt(2)) == [3]
80
+
81
+ assert field_isomorphism( I*sqrt(3), I*sqrt(3)/2) == [ 2, 0]
82
+ assert field_isomorphism(-I*sqrt(3), I*sqrt(3)/2) == [-2, 0]
83
+
84
+ assert field_isomorphism( I*sqrt(3), -I*sqrt(3)/2) == [-2, 0]
85
+ assert field_isomorphism(-I*sqrt(3), -I*sqrt(3)/2) == [ 2, 0]
86
+
87
+ assert field_isomorphism( 2*I*sqrt(3)/7, 5*I*sqrt(3)/3) == [ Rational(6, 35), 0]
88
+ assert field_isomorphism(-2*I*sqrt(3)/7, 5*I*sqrt(3)/3) == [Rational(-6, 35), 0]
89
+
90
+ assert field_isomorphism( 2*I*sqrt(3)/7, -5*I*sqrt(3)/3) == [Rational(-6, 35), 0]
91
+ assert field_isomorphism(-2*I*sqrt(3)/7, -5*I*sqrt(3)/3) == [ Rational(6, 35), 0]
92
+
93
+ assert field_isomorphism(
94
+ 2*I*sqrt(3)/7 + 27, 5*I*sqrt(3)/3) == [ Rational(6, 35), 27]
95
+ assert field_isomorphism(
96
+ -2*I*sqrt(3)/7 + 27, 5*I*sqrt(3)/3) == [Rational(-6, 35), 27]
97
+
98
+ assert field_isomorphism(
99
+ 2*I*sqrt(3)/7 + 27, -5*I*sqrt(3)/3) == [Rational(-6, 35), 27]
100
+ assert field_isomorphism(
101
+ -2*I*sqrt(3)/7 + 27, -5*I*sqrt(3)/3) == [ Rational(6, 35), 27]
102
+
103
+ p = AlgebraicNumber( sqrt(2) + sqrt(3))
104
+ q = AlgebraicNumber(-sqrt(2) + sqrt(3))
105
+ r = AlgebraicNumber( sqrt(2) - sqrt(3))
106
+ s = AlgebraicNumber(-sqrt(2) - sqrt(3))
107
+
108
+ pos_coeffs = [ S.Half, S.Zero, Rational(-9, 2), S.Zero]
109
+ neg_coeffs = [Rational(-1, 2), S.Zero, Rational(9, 2), S.Zero]
110
+
111
+ a = AlgebraicNumber(sqrt(2))
112
+
113
+ assert is_isomorphism_possible(a, p) is True
114
+ assert is_isomorphism_possible(a, q) is True
115
+ assert is_isomorphism_possible(a, r) is True
116
+ assert is_isomorphism_possible(a, s) is True
117
+
118
+ assert field_isomorphism(a, p, fast=True) == pos_coeffs
119
+ assert field_isomorphism(a, q, fast=True) == neg_coeffs
120
+ assert field_isomorphism(a, r, fast=True) == pos_coeffs
121
+ assert field_isomorphism(a, s, fast=True) == neg_coeffs
122
+
123
+ assert field_isomorphism(a, p, fast=False) == pos_coeffs
124
+ assert field_isomorphism(a, q, fast=False) == neg_coeffs
125
+ assert field_isomorphism(a, r, fast=False) == pos_coeffs
126
+ assert field_isomorphism(a, s, fast=False) == neg_coeffs
127
+
128
+ a = AlgebraicNumber(-sqrt(2))
129
+
130
+ assert is_isomorphism_possible(a, p) is True
131
+ assert is_isomorphism_possible(a, q) is True
132
+ assert is_isomorphism_possible(a, r) is True
133
+ assert is_isomorphism_possible(a, s) is True
134
+
135
+ assert field_isomorphism(a, p, fast=True) == neg_coeffs
136
+ assert field_isomorphism(a, q, fast=True) == pos_coeffs
137
+ assert field_isomorphism(a, r, fast=True) == neg_coeffs
138
+ assert field_isomorphism(a, s, fast=True) == pos_coeffs
139
+
140
+ assert field_isomorphism(a, p, fast=False) == neg_coeffs
141
+ assert field_isomorphism(a, q, fast=False) == pos_coeffs
142
+ assert field_isomorphism(a, r, fast=False) == neg_coeffs
143
+ assert field_isomorphism(a, s, fast=False) == pos_coeffs
144
+
145
+ pos_coeffs = [ S.Half, S.Zero, Rational(-11, 2), S.Zero]
146
+ neg_coeffs = [Rational(-1, 2), S.Zero, Rational(11, 2), S.Zero]
147
+
148
+ a = AlgebraicNumber(sqrt(3))
149
+
150
+ assert is_isomorphism_possible(a, p) is True
151
+ assert is_isomorphism_possible(a, q) is True
152
+ assert is_isomorphism_possible(a, r) is True
153
+ assert is_isomorphism_possible(a, s) is True
154
+
155
+ assert field_isomorphism(a, p, fast=True) == neg_coeffs
156
+ assert field_isomorphism(a, q, fast=True) == neg_coeffs
157
+ assert field_isomorphism(a, r, fast=True) == pos_coeffs
158
+ assert field_isomorphism(a, s, fast=True) == pos_coeffs
159
+
160
+ assert field_isomorphism(a, p, fast=False) == neg_coeffs
161
+ assert field_isomorphism(a, q, fast=False) == neg_coeffs
162
+ assert field_isomorphism(a, r, fast=False) == pos_coeffs
163
+ assert field_isomorphism(a, s, fast=False) == pos_coeffs
164
+
165
+ a = AlgebraicNumber(-sqrt(3))
166
+
167
+ assert is_isomorphism_possible(a, p) is True
168
+ assert is_isomorphism_possible(a, q) is True
169
+ assert is_isomorphism_possible(a, r) is True
170
+ assert is_isomorphism_possible(a, s) is True
171
+
172
+ assert field_isomorphism(a, p, fast=True) == pos_coeffs
173
+ assert field_isomorphism(a, q, fast=True) == pos_coeffs
174
+ assert field_isomorphism(a, r, fast=True) == neg_coeffs
175
+ assert field_isomorphism(a, s, fast=True) == neg_coeffs
176
+
177
+ assert field_isomorphism(a, p, fast=False) == pos_coeffs
178
+ assert field_isomorphism(a, q, fast=False) == pos_coeffs
179
+ assert field_isomorphism(a, r, fast=False) == neg_coeffs
180
+ assert field_isomorphism(a, s, fast=False) == neg_coeffs
181
+
182
+ pos_coeffs = [ Rational(3, 2), S.Zero, Rational(-33, 2), -S(8)]
183
+ neg_coeffs = [Rational(-3, 2), S.Zero, Rational(33, 2), -S(8)]
184
+
185
+ a = AlgebraicNumber(3*sqrt(3) - 8)
186
+
187
+ assert is_isomorphism_possible(a, p) is True
188
+ assert is_isomorphism_possible(a, q) is True
189
+ assert is_isomorphism_possible(a, r) is True
190
+ assert is_isomorphism_possible(a, s) is True
191
+
192
+ assert field_isomorphism(a, p, fast=True) == neg_coeffs
193
+ assert field_isomorphism(a, q, fast=True) == neg_coeffs
194
+ assert field_isomorphism(a, r, fast=True) == pos_coeffs
195
+ assert field_isomorphism(a, s, fast=True) == pos_coeffs
196
+
197
+ assert field_isomorphism(a, p, fast=False) == neg_coeffs
198
+ assert field_isomorphism(a, q, fast=False) == neg_coeffs
199
+ assert field_isomorphism(a, r, fast=False) == pos_coeffs
200
+ assert field_isomorphism(a, s, fast=False) == pos_coeffs
201
+
202
+ a = AlgebraicNumber(3*sqrt(2) + 2*sqrt(3) + 1)
203
+
204
+ pos_1_coeffs = [ S.Half, S.Zero, Rational(-5, 2), S.One]
205
+ neg_5_coeffs = [Rational(-5, 2), S.Zero, Rational(49, 2), S.One]
206
+ pos_5_coeffs = [ Rational(5, 2), S.Zero, Rational(-49, 2), S.One]
207
+ neg_1_coeffs = [Rational(-1, 2), S.Zero, Rational(5, 2), S.One]
208
+
209
+ assert is_isomorphism_possible(a, p) is True
210
+ assert is_isomorphism_possible(a, q) is True
211
+ assert is_isomorphism_possible(a, r) is True
212
+ assert is_isomorphism_possible(a, s) is True
213
+
214
+ assert field_isomorphism(a, p, fast=True) == pos_1_coeffs
215
+ assert field_isomorphism(a, q, fast=True) == neg_5_coeffs
216
+ assert field_isomorphism(a, r, fast=True) == pos_5_coeffs
217
+ assert field_isomorphism(a, s, fast=True) == neg_1_coeffs
218
+
219
+ assert field_isomorphism(a, p, fast=False) == pos_1_coeffs
220
+ assert field_isomorphism(a, q, fast=False) == neg_5_coeffs
221
+ assert field_isomorphism(a, r, fast=False) == pos_5_coeffs
222
+ assert field_isomorphism(a, s, fast=False) == neg_1_coeffs
223
+
224
+ a = AlgebraicNumber(sqrt(2))
225
+ b = AlgebraicNumber(sqrt(3))
226
+ c = AlgebraicNumber(sqrt(7))
227
+
228
+ assert is_isomorphism_possible(a, b) is True
229
+ assert is_isomorphism_possible(b, a) is True
230
+
231
+ assert is_isomorphism_possible(c, p) is False
232
+
233
+ assert field_isomorphism(sqrt(2), sqrt(3), fast=True) is None
234
+ assert field_isomorphism(sqrt(3), sqrt(2), fast=True) is None
235
+
236
+ assert field_isomorphism(sqrt(2), sqrt(3), fast=False) is None
237
+ assert field_isomorphism(sqrt(3), sqrt(2), fast=False) is None
238
+
239
+ a = AlgebraicNumber(sqrt(2))
240
+ b = AlgebraicNumber(2 ** (S(1) / 3))
241
+
242
+ assert is_isomorphism_possible(a, b) is False
243
+ assert field_isomorphism(a, b) is None
244
+
245
+
246
+ def test_primitive_element():
247
+ assert primitive_element([sqrt(2)], x) == (x**2 - 2, [1])
248
+ assert primitive_element(
249
+ [sqrt(2), sqrt(3)], x) == (x**4 - 10*x**2 + 1, [1, 1])
250
+
251
+ assert primitive_element([sqrt(2)], x, polys=True) == (Poly(x**2 - 2, domain='QQ'), [1])
252
+ assert primitive_element([sqrt(
253
+ 2), sqrt(3)], x, polys=True) == (Poly(x**4 - 10*x**2 + 1, domain='QQ'), [1, 1])
254
+
255
+ assert primitive_element(
256
+ [sqrt(2)], x, ex=True) == (x**2 - 2, [1], [[1, 0]])
257
+ assert primitive_element([sqrt(2), sqrt(3)], x, ex=True) == \
258
+ (x**4 - 10*x**2 + 1, [1, 1], [[Q(1, 2), 0, -Q(9, 2), 0], [-
259
+ Q(1, 2), 0, Q(11, 2), 0]])
260
+
261
+ assert primitive_element(
262
+ [sqrt(2)], x, ex=True, polys=True) == (Poly(x**2 - 2, domain='QQ'), [1], [[1, 0]])
263
+ assert primitive_element([sqrt(2), sqrt(3)], x, ex=True, polys=True) == \
264
+ (Poly(x**4 - 10*x**2 + 1, domain='QQ'), [1, 1], [[Q(1, 2), 0, -Q(9, 2),
265
+ 0], [-Q(1, 2), 0, Q(11, 2), 0]])
266
+
267
+ assert primitive_element([sqrt(2)], polys=True) == (Poly(x**2 - 2), [1])
268
+
269
+ raises(ValueError, lambda: primitive_element([], x, ex=False))
270
+ raises(ValueError, lambda: primitive_element([], x, ex=True))
271
+
272
+ # Issue 14117
273
+ a, b = I*sqrt(2*sqrt(2) + 3), I*sqrt(-2*sqrt(2) + 3)
274
+ assert primitive_element([a, b, I], x) == (x**4 + 6*x**2 + 1, [1, 0, 0])
275
+
276
+ assert primitive_element([sqrt(2), 0], x) == (x**2 - 2, [1, 0])
277
+ assert primitive_element([0, sqrt(2)], x) == (x**2 - 2, [1, 1])
278
+ assert primitive_element([sqrt(2), 0], x, ex=True) == (x**2 - 2, [1, 0], [[MPQ(1,1), MPQ(0,1)], []])
279
+ assert primitive_element([0, sqrt(2)], x, ex=True) == (x**2 - 2, [1, 1], [[], [MPQ(1,1), MPQ(0,1)]])
280
+
281
+
282
+ def test_to_number_field():
283
+ assert to_number_field(sqrt(2)) == AlgebraicNumber(sqrt(2))
284
+ assert to_number_field(
285
+ [sqrt(2), sqrt(3)]) == AlgebraicNumber(sqrt(2) + sqrt(3))
286
+
287
+ a = AlgebraicNumber(sqrt(2) + sqrt(3), [S.Half, S.Zero, Rational(-9, 2), S.Zero])
288
+
289
+ assert to_number_field(sqrt(2), sqrt(2) + sqrt(3)) == a
290
+ assert to_number_field(sqrt(2), AlgebraicNumber(sqrt(2) + sqrt(3))) == a
291
+
292
+ raises(IsomorphismFailed, lambda: to_number_field(sqrt(2), sqrt(3)))
293
+
294
+
295
+ def test_issue_22561():
296
+ a = to_number_field(sqrt(2), sqrt(2) + sqrt(3))
297
+ b = to_number_field(sqrt(2), sqrt(2) + sqrt(5))
298
+ assert field_isomorphism(a, b) == [1, 0]
299
+
300
+
301
+ def test_issue_22736():
302
+ a = CRootOf(x**4 + x**3 + x**2 + x + 1, -1)
303
+ a._reset()
304
+ b = exp(2*I*pi/5)
305
+ assert field_isomorphism(a, b) == [1, 0]
306
+
307
+
308
+ def test_issue_27798():
309
+ # https://github.com/sympy/sympy/issues/27798
310
+ a, b = CRootOf(49*x**3 - 49*x**2 + 14*x - 1, 2), CRootOf(49*x**3 - 49*x**2 + 14*x - 1, 0)
311
+ assert primitive_element([a, b], polys=True)[0].primitive()[0] == 1
312
+ assert primitive_element([a, b], polys=True, ex=True)[0].primitive()[0] == 1
313
+
314
+ f1, f2 = QQ.algebraic_field(a), QQ.algebraic_field(b)
315
+ f3 = f1.unify(f2)
316
+ assert f3.mod.primitive()[0] == 1
317
+ assert Poly(x, x, domain=f1) + Poly(x, x, domain=f2) == Poly(2*x, x, domain=f3)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/tests/test_utilities.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.abc import x
2
+ from sympy.core.numbers import (I, Rational)
3
+ from sympy.core.singleton import S
4
+ from sympy.functions.elementary.miscellaneous import sqrt
5
+ from sympy.polys import Poly, cyclotomic_poly
6
+ from sympy.polys.domains import FF, QQ
7
+ from sympy.polys.matrices import DomainMatrix, DM
8
+ from sympy.polys.matrices.exceptions import DMRankError
9
+ from sympy.polys.numberfields.utilities import (
10
+ AlgIntPowers, coeff_search, extract_fundamental_discriminant,
11
+ isolate, supplement_a_subspace,
12
+ )
13
+ from sympy.printing.lambdarepr import IntervalPrinter
14
+ from sympy.testing.pytest import raises
15
+
16
+
17
+ def test_AlgIntPowers_01():
18
+ T = Poly(cyclotomic_poly(5))
19
+ zeta_pow = AlgIntPowers(T)
20
+ raises(ValueError, lambda: zeta_pow[-1])
21
+ for e in range(10):
22
+ a = e % 5
23
+ if a < 4:
24
+ c = zeta_pow[e]
25
+ assert c[a] == 1 and all(c[i] == 0 for i in range(4) if i != a)
26
+ else:
27
+ assert zeta_pow[e] == [-1] * 4
28
+
29
+
30
+ def test_AlgIntPowers_02():
31
+ T = Poly(x**3 + 2*x**2 + 3*x + 4)
32
+ m = 7
33
+ theta_pow = AlgIntPowers(T, m)
34
+ for e in range(10):
35
+ computed = theta_pow[e]
36
+ coeffs = (Poly(x)**e % T + Poly(x**3)).rep.to_list()[1:]
37
+ expected = [c % m for c in reversed(coeffs)]
38
+ assert computed == expected
39
+
40
+
41
+ def test_coeff_search():
42
+ C = []
43
+ search = coeff_search(2, 1)
44
+ for i, c in enumerate(search):
45
+ C.append(c)
46
+ if i == 12:
47
+ break
48
+ assert C == [[1, 1], [1, 0], [1, -1], [0, 1], [2, 2], [2, 1], [2, 0], [2, -1], [2, -2], [1, 2], [1, -2], [0, 2], [3, 3]]
49
+
50
+
51
+ def test_extract_fundamental_discriminant():
52
+ # To extract, integer must be 0 or 1 mod 4.
53
+ raises(ValueError, lambda: extract_fundamental_discriminant(2))
54
+ raises(ValueError, lambda: extract_fundamental_discriminant(3))
55
+ # Try many cases, of different forms:
56
+ cases = (
57
+ (0, {}, {0: 1}),
58
+ (1, {}, {}),
59
+ (8, {2: 3}, {}),
60
+ (-8, {2: 3, -1: 1}, {}),
61
+ (12, {2: 2, 3: 1}, {}),
62
+ (36, {}, {2: 1, 3: 1}),
63
+ (45, {5: 1}, {3: 1}),
64
+ (48, {2: 2, 3: 1}, {2: 1}),
65
+ (1125, {5: 1}, {3: 1, 5: 1}),
66
+ )
67
+ for a, D_expected, F_expected in cases:
68
+ D, F = extract_fundamental_discriminant(a)
69
+ assert D == D_expected
70
+ assert F == F_expected
71
+
72
+
73
+ def test_supplement_a_subspace_1():
74
+ M = DM([[1, 7, 0], [2, 3, 4]], QQ).transpose()
75
+
76
+ # First supplement over QQ:
77
+ B = supplement_a_subspace(M)
78
+ assert B[:, :2] == M
79
+ assert B[:, 2] == DomainMatrix.eye(3, QQ).to_dense()[:, 0]
80
+
81
+ # Now supplement over FF(7):
82
+ M = M.convert_to(FF(7))
83
+ B = supplement_a_subspace(M)
84
+ assert B[:, :2] == M
85
+ # When we work mod 7, first col of M goes to [1, 0, 0],
86
+ # so the supplementary vector cannot equal this, as it did
87
+ # when we worked over QQ. Instead, we get the second std basis vector:
88
+ assert B[:, 2] == DomainMatrix.eye(3, FF(7)).to_dense()[:, 1]
89
+
90
+
91
+ def test_supplement_a_subspace_2():
92
+ M = DM([[1, 0, 0], [2, 0, 0]], QQ).transpose()
93
+ with raises(DMRankError):
94
+ supplement_a_subspace(M)
95
+
96
+
97
+ def test_IntervalPrinter():
98
+ ip = IntervalPrinter()
99
+ assert ip.doprint(x**Rational(1, 3)) == "x**(mpi('1/3'))"
100
+ assert ip.doprint(sqrt(x)) == "x**(mpi('1/2'))"
101
+
102
+
103
+ def test_isolate():
104
+ assert isolate(1) == (1, 1)
105
+ assert isolate(S.Half) == (S.Half, S.Half)
106
+
107
+ assert isolate(sqrt(2)) == (1, 2)
108
+ assert isolate(-sqrt(2)) == (-2, -1)
109
+
110
+ assert isolate(sqrt(2), eps=Rational(1, 100)) == (Rational(24, 17), Rational(17, 12))
111
+ assert isolate(-sqrt(2), eps=Rational(1, 100)) == (Rational(-17, 12), Rational(-24, 17))
112
+
113
+ raises(NotImplementedError, lambda: isolate(I))
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/numberfields/utilities.py ADDED
@@ -0,0 +1,474 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Utilities for algebraic number theory. """
2
+
3
+ from sympy.core.sympify import sympify
4
+ from sympy.ntheory.factor_ import factorint
5
+ from sympy.polys.domains.rationalfield import QQ
6
+ from sympy.polys.domains.integerring import ZZ
7
+ from sympy.polys.matrices.exceptions import DMRankError
8
+ from sympy.polys.numberfields.minpoly import minpoly
9
+ from sympy.printing.lambdarepr import IntervalPrinter
10
+ from sympy.utilities.decorator import public
11
+ from sympy.utilities.lambdify import lambdify
12
+
13
+ from mpmath import mp
14
+
15
+
16
+ def is_rat(c):
17
+ r"""
18
+ Test whether an argument is of an acceptable type to be used as a rational
19
+ number.
20
+
21
+ Explanation
22
+ ===========
23
+
24
+ Returns ``True`` on any argument of type ``int``, :ref:`ZZ`, or :ref:`QQ`.
25
+
26
+ See Also
27
+ ========
28
+
29
+ is_int
30
+
31
+ """
32
+ # ``c in QQ`` is too accepting (e.g. ``3.14 in QQ`` is ``True``),
33
+ # ``QQ.of_type(c)`` is too demanding (e.g. ``QQ.of_type(3)`` is ``False``).
34
+ #
35
+ # Meanwhile, if gmpy2 is installed then ``ZZ.of_type()`` accepts only
36
+ # ``mpz``, not ``int``, so we need another clause to ensure ``int`` is
37
+ # accepted.
38
+ return isinstance(c, int) or ZZ.of_type(c) or QQ.of_type(c)
39
+
40
+
41
+ def is_int(c):
42
+ r"""
43
+ Test whether an argument is of an acceptable type to be used as an integer.
44
+
45
+ Explanation
46
+ ===========
47
+
48
+ Returns ``True`` on any argument of type ``int`` or :ref:`ZZ`.
49
+
50
+ See Also
51
+ ========
52
+
53
+ is_rat
54
+
55
+ """
56
+ # If gmpy2 is installed then ``ZZ.of_type()`` accepts only
57
+ # ``mpz``, not ``int``, so we need another clause to ensure ``int`` is
58
+ # accepted.
59
+ return isinstance(c, int) or ZZ.of_type(c)
60
+
61
+
62
+ def get_num_denom(c):
63
+ r"""
64
+ Given any argument on which :py:func:`~.is_rat` is ``True``, return the
65
+ numerator and denominator of this number.
66
+
67
+ See Also
68
+ ========
69
+
70
+ is_rat
71
+
72
+ """
73
+ r = QQ(c)
74
+ return r.numerator, r.denominator
75
+
76
+
77
+ @public
78
+ def extract_fundamental_discriminant(a):
79
+ r"""
80
+ Extract a fundamental discriminant from an integer *a*.
81
+
82
+ Explanation
83
+ ===========
84
+
85
+ Given any rational integer *a* that is 0 or 1 mod 4, write $a = d f^2$,
86
+ where $d$ is either 1 or a fundamental discriminant, and return a pair
87
+ of dictionaries ``(D, F)`` giving the prime factorizations of $d$ and $f$
88
+ respectively, in the same format returned by :py:func:`~.factorint`.
89
+
90
+ A fundamental discriminant $d$ is different from unity, and is either
91
+ 1 mod 4 and squarefree, or is 0 mod 4 and such that $d/4$ is squarefree
92
+ and 2 or 3 mod 4. This is the same as being the discriminant of some
93
+ quadratic field.
94
+
95
+ Examples
96
+ ========
97
+
98
+ >>> from sympy.polys.numberfields.utilities import extract_fundamental_discriminant
99
+ >>> print(extract_fundamental_discriminant(-432))
100
+ ({3: 1, -1: 1}, {2: 2, 3: 1})
101
+
102
+ For comparison:
103
+
104
+ >>> from sympy import factorint
105
+ >>> print(factorint(-432))
106
+ {2: 4, 3: 3, -1: 1}
107
+
108
+ Parameters
109
+ ==========
110
+
111
+ a: int, must be 0 or 1 mod 4
112
+
113
+ Returns
114
+ =======
115
+
116
+ Pair ``(D, F)`` of dictionaries.
117
+
118
+ Raises
119
+ ======
120
+
121
+ ValueError
122
+ If *a* is not 0 or 1 mod 4.
123
+
124
+ References
125
+ ==========
126
+
127
+ .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
128
+ (See Prop. 5.1.3)
129
+
130
+ """
131
+ if a % 4 not in [0, 1]:
132
+ raise ValueError('To extract fundamental discriminant, number must be 0 or 1 mod 4.')
133
+ if a == 0:
134
+ return {}, {0: 1}
135
+ if a == 1:
136
+ return {}, {}
137
+ a_factors = factorint(a)
138
+ D = {}
139
+ F = {}
140
+ # First pass: just make d squarefree, and a/d a perfect square.
141
+ # We'll count primes (and units! i.e. -1) that are 3 mod 4 and present in d.
142
+ num_3_mod_4 = 0
143
+ for p, e in a_factors.items():
144
+ if e % 2 == 1:
145
+ D[p] = 1
146
+ if p % 4 == 3:
147
+ num_3_mod_4 += 1
148
+ if e >= 3:
149
+ F[p] = (e - 1) // 2
150
+ else:
151
+ F[p] = e // 2
152
+ # Second pass: if d is cong. to 2 or 3 mod 4, then we must steal away
153
+ # another factor of 4 from f**2 and give it to d.
154
+ even = 2 in D
155
+ if even or num_3_mod_4 % 2 == 1:
156
+ e2 = F[2]
157
+ assert e2 > 0
158
+ if e2 == 1:
159
+ del F[2]
160
+ else:
161
+ F[2] = e2 - 1
162
+ D[2] = 3 if even else 2
163
+ return D, F
164
+
165
+
166
+ @public
167
+ class AlgIntPowers:
168
+ r"""
169
+ Compute the powers of an algebraic integer.
170
+
171
+ Explanation
172
+ ===========
173
+
174
+ Given an algebraic integer $\theta$ by its monic irreducible polynomial
175
+ ``T`` over :ref:`ZZ`, this class computes representations of arbitrarily
176
+ high powers of $\theta$, as :ref:`ZZ`-linear combinations over
177
+ $\{1, \theta, \ldots, \theta^{n-1}\}$, where $n = \deg(T)$.
178
+
179
+ The representations are computed using the linear recurrence relations for
180
+ powers of $\theta$, derived from the polynomial ``T``. See [1], Sec. 4.2.2.
181
+
182
+ Optionally, the representations may be reduced with respect to a modulus.
183
+
184
+ Examples
185
+ ========
186
+
187
+ >>> from sympy import Poly, cyclotomic_poly
188
+ >>> from sympy.polys.numberfields.utilities import AlgIntPowers
189
+ >>> T = Poly(cyclotomic_poly(5))
190
+ >>> zeta_pow = AlgIntPowers(T)
191
+ >>> print(zeta_pow[0])
192
+ [1, 0, 0, 0]
193
+ >>> print(zeta_pow[1])
194
+ [0, 1, 0, 0]
195
+ >>> print(zeta_pow[4]) # doctest: +SKIP
196
+ [-1, -1, -1, -1]
197
+ >>> print(zeta_pow[24]) # doctest: +SKIP
198
+ [-1, -1, -1, -1]
199
+
200
+ References
201
+ ==========
202
+
203
+ .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.*
204
+
205
+ """
206
+
207
+ def __init__(self, T, modulus=None):
208
+ """
209
+ Parameters
210
+ ==========
211
+
212
+ T : :py:class:`~.Poly`
213
+ The monic irreducible polynomial over :ref:`ZZ` defining the
214
+ algebraic integer.
215
+
216
+ modulus : int, None, optional
217
+ If not ``None``, all representations will be reduced w.r.t. this.
218
+
219
+ """
220
+ self.T = T
221
+ self.modulus = modulus
222
+ self.n = T.degree()
223
+ self.powers_n_and_up = [[-c % self for c in reversed(T.rep.to_list())][:-1]]
224
+ self.max_so_far = self.n
225
+
226
+ def red(self, exp):
227
+ return exp if self.modulus is None else exp % self.modulus
228
+
229
+ def __rmod__(self, other):
230
+ return self.red(other)
231
+
232
+ def compute_up_through(self, e):
233
+ m = self.max_so_far
234
+ if e <= m: return
235
+ n = self.n
236
+ r = self.powers_n_and_up
237
+ c = r[0]
238
+ for k in range(m+1, e+1):
239
+ b = r[k-1-n][n-1]
240
+ r.append(
241
+ [c[0]*b % self] + [
242
+ (r[k-1-n][i-1] + c[i]*b) % self for i in range(1, n)
243
+ ]
244
+ )
245
+ self.max_so_far = e
246
+
247
+ def get(self, e):
248
+ n = self.n
249
+ if e < 0:
250
+ raise ValueError('Exponent must be non-negative.')
251
+ elif e < n:
252
+ return [1 if i == e else 0 for i in range(n)]
253
+ else:
254
+ self.compute_up_through(e)
255
+ return self.powers_n_and_up[e - n]
256
+
257
+ def __getitem__(self, item):
258
+ return self.get(item)
259
+
260
+
261
+ @public
262
+ def coeff_search(m, R):
263
+ r"""
264
+ Generate coefficients for searching through polynomials.
265
+
266
+ Explanation
267
+ ===========
268
+
269
+ Lead coeff is always non-negative. Explore all combinations with coeffs
270
+ bounded in absolute value before increasing the bound. Skip the all-zero
271
+ list, and skip any repeats. See examples.
272
+
273
+ Examples
274
+ ========
275
+
276
+ >>> from sympy.polys.numberfields.utilities import coeff_search
277
+ >>> cs = coeff_search(2, 1)
278
+ >>> C = [next(cs) for i in range(13)]
279
+ >>> print(C)
280
+ [[1, 1], [1, 0], [1, -1], [0, 1], [2, 2], [2, 1], [2, 0], [2, -1], [2, -2],
281
+ [1, 2], [1, -2], [0, 2], [3, 3]]
282
+
283
+ Parameters
284
+ ==========
285
+
286
+ m : int
287
+ Length of coeff list.
288
+ R : int
289
+ Initial max abs val for coeffs (will increase as search proceeds).
290
+
291
+ Returns
292
+ =======
293
+
294
+ generator
295
+ Infinite generator of lists of coefficients.
296
+
297
+ """
298
+ R0 = R
299
+ c = [R] * m
300
+ while True:
301
+ if R == R0 or R in c or -R in c:
302
+ yield c[:]
303
+ j = m - 1
304
+ while c[j] == -R:
305
+ j -= 1
306
+ c[j] -= 1
307
+ for i in range(j + 1, m):
308
+ c[i] = R
309
+ for j in range(m):
310
+ if c[j] != 0:
311
+ break
312
+ else:
313
+ R += 1
314
+ c = [R] * m
315
+
316
+
317
+ def supplement_a_subspace(M):
318
+ r"""
319
+ Extend a basis for a subspace to a basis for the whole space.
320
+
321
+ Explanation
322
+ ===========
323
+
324
+ Given an $n \times r$ matrix *M* of rank $r$ (so $r \leq n$), this function
325
+ computes an invertible $n \times n$ matrix $B$ such that the first $r$
326
+ columns of $B$ equal *M*.
327
+
328
+ This operation can be interpreted as a way of extending a basis for a
329
+ subspace, to give a basis for the whole space.
330
+
331
+ To be precise, suppose you have an $n$-dimensional vector space $V$, with
332
+ basis $\{v_1, v_2, \ldots, v_n\}$, and an $r$-dimensional subspace $W$ of
333
+ $V$, spanned by a basis $\{w_1, w_2, \ldots, w_r\}$, where the $w_j$ are
334
+ given as linear combinations of the $v_i$. If the columns of *M* represent
335
+ the $w_j$ as such linear combinations, then the columns of the matrix $B$
336
+ computed by this function give a new basis $\{u_1, u_2, \ldots, u_n\}$ for
337
+ $V$, again relative to the $\{v_i\}$ basis, and such that $u_j = w_j$
338
+ for $1 \leq j \leq r$.
339
+
340
+ Examples
341
+ ========
342
+
343
+ Note: The function works in terms of columns, so in these examples we
344
+ print matrix transposes in order to make the columns easier to inspect.
345
+
346
+ >>> from sympy.polys.matrices import DM
347
+ >>> from sympy import QQ, FF
348
+ >>> from sympy.polys.numberfields.utilities import supplement_a_subspace
349
+ >>> M = DM([[1, 7, 0], [2, 3, 4]], QQ).transpose()
350
+ >>> print(supplement_a_subspace(M).to_Matrix().transpose())
351
+ Matrix([[1, 7, 0], [2, 3, 4], [1, 0, 0]])
352
+
353
+ >>> M2 = M.convert_to(FF(7))
354
+ >>> print(M2.to_Matrix().transpose())
355
+ Matrix([[1, 0, 0], [2, 3, -3]])
356
+ >>> print(supplement_a_subspace(M2).to_Matrix().transpose())
357
+ Matrix([[1, 0, 0], [2, 3, -3], [0, 1, 0]])
358
+
359
+ Parameters
360
+ ==========
361
+
362
+ M : :py:class:`~.DomainMatrix`
363
+ The columns give the basis for the subspace.
364
+
365
+ Returns
366
+ =======
367
+
368
+ :py:class:`~.DomainMatrix`
369
+ This matrix is invertible and its first $r$ columns equal *M*.
370
+
371
+ Raises
372
+ ======
373
+
374
+ DMRankError
375
+ If *M* was not of maximal rank.
376
+
377
+ References
378
+ ==========
379
+
380
+ .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory*
381
+ (See Sec. 2.3.2.)
382
+
383
+ """
384
+ n, r = M.shape
385
+ # Let In be the n x n identity matrix.
386
+ # Form the augmented matrix [M | In] and compute RREF.
387
+ Maug = M.hstack(M.eye(n, M.domain))
388
+ R, pivots = Maug.rref()
389
+ if pivots[:r] != tuple(range(r)):
390
+ raise DMRankError('M was not of maximal rank')
391
+ # Let J be the n x r matrix equal to the first r columns of In.
392
+ # Since M is of rank r, RREF reduces [M | In] to [J | A], where A is the product of
393
+ # elementary matrices Ei corresp. to the row ops performed by RREF. Since the Ei are
394
+ # invertible, so is A. Let B = A^(-1).
395
+ A = R[:, r:]
396
+ B = A.inv()
397
+ # Then B is the desired matrix. It is invertible, since B^(-1) == A.
398
+ # And A * [M | In] == [J | A]
399
+ # => A * M == J
400
+ # => M == B * J == the first r columns of B.
401
+ return B
402
+
403
+
404
+ @public
405
+ def isolate(alg, eps=None, fast=False):
406
+ """
407
+ Find a rational isolating interval for a real algebraic number.
408
+
409
+ Examples
410
+ ========
411
+
412
+ >>> from sympy import isolate, sqrt, Rational
413
+ >>> print(isolate(sqrt(2))) # doctest: +SKIP
414
+ (1, 2)
415
+ >>> print(isolate(sqrt(2), eps=Rational(1, 100)))
416
+ (24/17, 17/12)
417
+
418
+ Parameters
419
+ ==========
420
+
421
+ alg : str, int, :py:class:`~.Expr`
422
+ The algebraic number to be isolated. Must be a real number, to use this
423
+ particular function. However, see also :py:meth:`.Poly.intervals`,
424
+ which isolates complex roots when you pass ``all=True``.
425
+ eps : positive element of :ref:`QQ`, None, optional (default=None)
426
+ Precision to be passed to :py:meth:`.Poly.refine_root`
427
+ fast : boolean, optional (default=False)
428
+ Say whether fast refinement procedure should be used.
429
+ (Will be passed to :py:meth:`.Poly.refine_root`.)
430
+
431
+ Returns
432
+ =======
433
+
434
+ Pair of rational numbers defining an isolating interval for the given
435
+ algebraic number.
436
+
437
+ See Also
438
+ ========
439
+
440
+ .Poly.intervals
441
+
442
+ """
443
+ alg = sympify(alg)
444
+
445
+ if alg.is_Rational:
446
+ return (alg, alg)
447
+ elif not alg.is_real:
448
+ raise NotImplementedError(
449
+ "complex algebraic numbers are not supported")
450
+
451
+ func = lambdify((), alg, modules="mpmath", printer=IntervalPrinter())
452
+
453
+ poly = minpoly(alg, polys=True)
454
+ intervals = poly.intervals(sqf=True)
455
+
456
+ dps, done = mp.dps, False
457
+
458
+ try:
459
+ while not done:
460
+ alg = func()
461
+
462
+ for a, b in intervals:
463
+ if a <= alg.a and alg.b <= b:
464
+ done = True
465
+ break
466
+ else:
467
+ mp.dps *= 2
468
+ finally:
469
+ mp.dps = dps
470
+
471
+ if eps is not None:
472
+ a, b = poly.refine_root(a, b, eps=eps, fast=fast)
473
+
474
+ return (a, b)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/__init__.py ADDED
File without changes
miniconda3/envs/ladir/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)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_constructor.py ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for tools for constructing domains for expressions. """
2
+
3
+ from sympy.testing.pytest import tooslow
4
+
5
+ from sympy.polys.constructor import construct_domain
6
+ from sympy.polys.domains import ZZ, QQ, ZZ_I, QQ_I, RR, CC, EX
7
+ from sympy.polys.domains.realfield import RealField
8
+ from sympy.polys.domains.complexfield import ComplexField
9
+
10
+ from sympy.core import (Catalan, GoldenRatio)
11
+ from sympy.core.numbers import (E, Float, I, Rational, pi)
12
+ from sympy.core.singleton import S
13
+ from sympy.functions.elementary.exponential import exp
14
+ from sympy.functions.elementary.miscellaneous import sqrt
15
+ from sympy.functions.elementary.trigonometric import sin
16
+ from sympy import rootof
17
+
18
+ from sympy.abc import x, y
19
+
20
+
21
+ def test_construct_domain():
22
+
23
+ assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
24
+ assert construct_domain([1, 2, 3], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])
25
+
26
+ assert construct_domain([S.One, S(2), S(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
27
+ assert construct_domain([S.One, S(2), S(3)], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])
28
+
29
+ assert construct_domain([S.Half, S(2)]) == (QQ, [QQ(1, 2), QQ(2)])
30
+ result = construct_domain([3.14, 1, S.Half])
31
+ assert isinstance(result[0], RealField)
32
+ assert result[1] == [RR(3.14), RR(1.0), RR(0.5)]
33
+
34
+ result = construct_domain([3.14, I, S.Half])
35
+ assert isinstance(result[0], ComplexField)
36
+ assert result[1] == [CC(3.14), CC(1.0j), CC(0.5)]
37
+
38
+ assert construct_domain([1.0+I]) == (CC, [CC(1.0, 1.0)])
39
+ assert construct_domain([2.0+3.0*I]) == (CC, [CC(2.0, 3.0)])
40
+
41
+ assert construct_domain([1, I]) == (ZZ_I, [ZZ_I(1, 0), ZZ_I(0, 1)])
42
+ assert construct_domain([1, I/2]) == (QQ_I, [QQ_I(1, 0), QQ_I(0, S.Half)])
43
+
44
+ assert construct_domain([3.14, sqrt(2)], extension=None) == (EX, [EX(3.14), EX(sqrt(2))])
45
+ assert construct_domain([3.14, sqrt(2)], extension=True) == (EX, [EX(3.14), EX(sqrt(2))])
46
+
47
+ assert construct_domain([1, sqrt(2)], extension=None) == (EX, [EX(1), EX(sqrt(2))])
48
+
49
+ assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))])
50
+ assert construct_domain([x, sqrt(x), sqrt(y)]) == (EX, [EX(x), EX(sqrt(x)), EX(sqrt(y))])
51
+
52
+ alg = QQ.algebraic_field(sqrt(2))
53
+
54
+ assert construct_domain([7, S.Half, sqrt(2)], extension=True) == \
55
+ (alg, [alg.convert(7), alg.convert(S.Half), alg.convert(sqrt(2))])
56
+
57
+ alg = QQ.algebraic_field(sqrt(2) + sqrt(3))
58
+
59
+ assert construct_domain([7, sqrt(2), sqrt(3)], extension=True) == \
60
+ (alg, [alg.convert(7), alg.convert(sqrt(2)), alg.convert(sqrt(3))])
61
+
62
+ dom = ZZ[x]
63
+
64
+ assert construct_domain([2*x, 3]) == \
65
+ (dom, [dom.convert(2*x), dom.convert(3)])
66
+
67
+ dom = ZZ[x, y]
68
+
69
+ assert construct_domain([2*x, 3*y]) == \
70
+ (dom, [dom.convert(2*x), dom.convert(3*y)])
71
+
72
+ dom = QQ[x]
73
+
74
+ assert construct_domain([x/2, 3]) == \
75
+ (dom, [dom.convert(x/2), dom.convert(3)])
76
+
77
+ dom = QQ[x, y]
78
+
79
+ assert construct_domain([x/2, 3*y]) == \
80
+ (dom, [dom.convert(x/2), dom.convert(3*y)])
81
+
82
+ dom = ZZ_I[x]
83
+
84
+ assert construct_domain([2*x, I]) == \
85
+ (dom, [dom.convert(2*x), dom.convert(I)])
86
+
87
+ dom = ZZ_I[x, y]
88
+
89
+ assert construct_domain([2*x, I*y]) == \
90
+ (dom, [dom.convert(2*x), dom.convert(I*y)])
91
+
92
+ dom = QQ_I[x]
93
+
94
+ assert construct_domain([x/2, I]) == \
95
+ (dom, [dom.convert(x/2), dom.convert(I)])
96
+
97
+ dom = QQ_I[x, y]
98
+
99
+ assert construct_domain([x/2, I*y]) == \
100
+ (dom, [dom.convert(x/2), dom.convert(I*y)])
101
+
102
+ dom = RR[x]
103
+
104
+ assert construct_domain([x/2, 3.5]) == \
105
+ (dom, [dom.convert(x/2), dom.convert(3.5)])
106
+
107
+ dom = RR[x, y]
108
+
109
+ assert construct_domain([x/2, 3.5*y]) == \
110
+ (dom, [dom.convert(x/2), dom.convert(3.5*y)])
111
+
112
+ dom = CC[x]
113
+
114
+ assert construct_domain([I*x/2, 3.5]) == \
115
+ (dom, [dom.convert(I*x/2), dom.convert(3.5)])
116
+
117
+ dom = CC[x, y]
118
+
119
+ assert construct_domain([I*x/2, 3.5*y]) == \
120
+ (dom, [dom.convert(I*x/2), dom.convert(3.5*y)])
121
+
122
+ dom = CC[x]
123
+
124
+ assert construct_domain([x/2, I*3.5]) == \
125
+ (dom, [dom.convert(x/2), dom.convert(I*3.5)])
126
+
127
+ dom = CC[x, y]
128
+
129
+ assert construct_domain([x/2, I*3.5*y]) == \
130
+ (dom, [dom.convert(x/2), dom.convert(I*3.5*y)])
131
+
132
+ dom = ZZ.frac_field(x)
133
+
134
+ assert construct_domain([2/x, 3]) == \
135
+ (dom, [dom.convert(2/x), dom.convert(3)])
136
+
137
+ dom = ZZ.frac_field(x, y)
138
+
139
+ assert construct_domain([2/x, 3*y]) == \
140
+ (dom, [dom.convert(2/x), dom.convert(3*y)])
141
+
142
+ dom = RR.frac_field(x)
143
+
144
+ assert construct_domain([2/x, 3.5]) == \
145
+ (dom, [dom.convert(2/x), dom.convert(3.5)])
146
+
147
+ dom = RR.frac_field(x, y)
148
+
149
+ assert construct_domain([2/x, 3.5*y]) == \
150
+ (dom, [dom.convert(2/x), dom.convert(3.5*y)])
151
+
152
+ dom = RealField(prec=336)[x]
153
+
154
+ assert construct_domain([pi.evalf(100)*x]) == \
155
+ (dom, [dom.convert(pi.evalf(100)*x)])
156
+
157
+ assert construct_domain(2) == (ZZ, ZZ(2))
158
+ assert construct_domain(S(2)/3) == (QQ, QQ(2, 3))
159
+ assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3))
160
+
161
+ assert construct_domain({}) == (ZZ, {})
162
+
163
+
164
+ def test_complex_exponential():
165
+ w = exp(-I*2*pi/3, evaluate=False)
166
+ alg = QQ.algebraic_field(w)
167
+ assert construct_domain([w**2, w, 1], extension=True) == (
168
+ alg,
169
+ [alg.convert(w**2),
170
+ alg.convert(w),
171
+ alg.convert(1)]
172
+ )
173
+
174
+
175
+ def test_rootof():
176
+ r1 = rootof(x**3 + x + 1, 0)
177
+ r2 = rootof(x**3 + x + 1, 1)
178
+ K1 = QQ.algebraic_field(r1)
179
+ K2 = QQ.algebraic_field(r2)
180
+ assert construct_domain([r1]) == (EX, [EX(r1)])
181
+ assert construct_domain([r2]) == (EX, [EX(r2)])
182
+ assert construct_domain([r1, r2]) == (EX, [EX(r1), EX(r2)])
183
+
184
+ assert construct_domain([r1], extension=True) == (
185
+ K1, [K1.from_sympy(r1)])
186
+ assert construct_domain([r2], extension=True) == (
187
+ K2, [K2.from_sympy(r2)])
188
+
189
+
190
+ @tooslow
191
+ def test_rootof_primitive_element():
192
+ r1 = rootof(x**3 + x + 1, 0)
193
+ r2 = rootof(x**3 + x + 1, 1)
194
+ K12 = QQ.algebraic_field(r1 + r2)
195
+ assert construct_domain([r1, r2], extension=True) == (
196
+ K12, [K12.from_sympy(r1), K12.from_sympy(r2)])
197
+
198
+
199
+ def test_composite_option():
200
+ assert construct_domain({(1,): sin(y)}, composite=False) == \
201
+ (EX, {(1,): EX(sin(y))})
202
+
203
+ assert construct_domain({(1,): y}, composite=False) == \
204
+ (EX, {(1,): EX(y)})
205
+
206
+ assert construct_domain({(1, 1): 1}, composite=False) == \
207
+ (ZZ, {(1, 1): 1})
208
+
209
+ assert construct_domain({(1, 0): y}, composite=False) == \
210
+ (EX, {(1, 0): EX(y)})
211
+
212
+
213
+ def test_precision():
214
+ f1 = Float("1.01")
215
+ f2 = Float("1.0000000000000000000001")
216
+ for u in [1, 1e-2, 1e-6, 1e-13, 1e-14, 1e-16, 1e-20, 1e-100, 1e-300,
217
+ f1, f2]:
218
+ result = construct_domain([u])
219
+ v = float(result[1][0])
220
+ assert abs(u - v) / u < 1e-14 # Test relative accuracy
221
+
222
+ result = construct_domain([f1])
223
+ y = result[1][0]
224
+ assert y-1 > 1e-50
225
+
226
+ result = construct_domain([f2])
227
+ y = result[1][0]
228
+ assert y-1 > 1e-50
229
+
230
+
231
+ def test_issue_11538():
232
+ for n in [E, pi, Catalan]:
233
+ assert construct_domain(n)[0] == ZZ[n]
234
+ assert construct_domain(x + n)[0] == ZZ[x, n]
235
+ assert construct_domain(GoldenRatio)[0] == EX
236
+ assert construct_domain(x + GoldenRatio)[0] == EX
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_densearith.py ADDED
@@ -0,0 +1,1007 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for dense recursive polynomials' arithmetics. """
2
+
3
+ from sympy.external.gmpy import GROUND_TYPES
4
+
5
+ from sympy.polys.densebasic import (
6
+ dup_normal, dmp_normal,
7
+ )
8
+
9
+ from sympy.polys.densearith import (
10
+ dup_add_term, dmp_add_term,
11
+ dup_sub_term, dmp_sub_term,
12
+ dup_mul_term, dmp_mul_term,
13
+ dup_add_ground, dmp_add_ground,
14
+ dup_sub_ground, dmp_sub_ground,
15
+ dup_mul_ground, dmp_mul_ground,
16
+ dup_quo_ground, dmp_quo_ground,
17
+ dup_exquo_ground, dmp_exquo_ground,
18
+ dup_lshift, dup_rshift,
19
+ dup_abs, dmp_abs,
20
+ dup_neg, dmp_neg,
21
+ dup_add, dmp_add,
22
+ dup_sub, dmp_sub,
23
+ dup_mul, dmp_mul,
24
+ dup_sqr, dmp_sqr,
25
+ dup_pow, dmp_pow,
26
+ dup_add_mul, dmp_add_mul,
27
+ dup_sub_mul, dmp_sub_mul,
28
+ dup_pdiv, dup_prem, dup_pquo, dup_pexquo,
29
+ dmp_pdiv, dmp_prem, dmp_pquo, dmp_pexquo,
30
+ dup_rr_div, dmp_rr_div,
31
+ dup_ff_div, dmp_ff_div,
32
+ dup_div, dup_rem, dup_quo, dup_exquo,
33
+ dmp_div, dmp_rem, dmp_quo, dmp_exquo,
34
+ dup_max_norm, dmp_max_norm,
35
+ dup_l1_norm, dmp_l1_norm,
36
+ dup_l2_norm_squared, dmp_l2_norm_squared,
37
+ dup_expand, dmp_expand,
38
+ )
39
+
40
+ from sympy.polys.polyerrors import (
41
+ ExactQuotientFailed,
42
+ )
43
+
44
+ from sympy.polys.specialpolys import f_polys, Symbol, Poly
45
+ from sympy.polys.domains import FF, ZZ, QQ, CC
46
+
47
+ from sympy.testing.pytest import raises
48
+
49
+ x = Symbol('x')
50
+
51
+ f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
52
+ F_0 = dmp_mul_ground(dmp_normal(f_0, 2, QQ), QQ(1, 7), 2, QQ)
53
+
54
+ def test_dup_add_term():
55
+ f = dup_normal([], ZZ)
56
+
57
+ assert dup_add_term(f, ZZ(0), 0, ZZ) == dup_normal([], ZZ)
58
+
59
+ assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1], ZZ)
60
+ assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 0], ZZ)
61
+ assert dup_add_term(f, ZZ(1), 2, ZZ) == dup_normal([1, 0, 0], ZZ)
62
+
63
+ f = dup_normal([1, 1, 1], ZZ)
64
+
65
+ assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1, 1, 2], ZZ)
66
+ assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 2, 1], ZZ)
67
+ assert dup_add_term(f, ZZ(1), 2, ZZ) == dup_normal([2, 1, 1], ZZ)
68
+
69
+ assert dup_add_term(f, ZZ(1), 3, ZZ) == dup_normal([1, 1, 1, 1], ZZ)
70
+ assert dup_add_term(f, ZZ(1), 4, ZZ) == dup_normal([1, 0, 1, 1, 1], ZZ)
71
+ assert dup_add_term(f, ZZ(1), 5, ZZ) == dup_normal([1, 0, 0, 1, 1, 1], ZZ)
72
+ assert dup_add_term(
73
+ f, ZZ(1), 6, ZZ) == dup_normal([1, 0, 0, 0, 1, 1, 1], ZZ)
74
+
75
+ assert dup_add_term(f, ZZ(-1), 2, ZZ) == dup_normal([1, 1], ZZ)
76
+
77
+
78
+ def test_dmp_add_term():
79
+ assert dmp_add_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, 0, ZZ) == \
80
+ dup_add_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, ZZ)
81
+ assert dmp_add_term(f_0, [[]], 3, 2, ZZ) == f_0
82
+ assert dmp_add_term(F_0, [[]], 3, 2, QQ) == F_0
83
+
84
+
85
+ def test_dup_sub_term():
86
+ f = dup_normal([], ZZ)
87
+
88
+ assert dup_sub_term(f, ZZ(0), 0, ZZ) == dup_normal([], ZZ)
89
+
90
+ assert dup_sub_term(f, ZZ(1), 0, ZZ) == dup_normal([-1], ZZ)
91
+ assert dup_sub_term(f, ZZ(1), 1, ZZ) == dup_normal([-1, 0], ZZ)
92
+ assert dup_sub_term(f, ZZ(1), 2, ZZ) == dup_normal([-1, 0, 0], ZZ)
93
+
94
+ f = dup_normal([1, 1, 1], ZZ)
95
+
96
+ assert dup_sub_term(f, ZZ(2), 0, ZZ) == dup_normal([ 1, 1, -1], ZZ)
97
+ assert dup_sub_term(f, ZZ(2), 1, ZZ) == dup_normal([ 1, -1, 1], ZZ)
98
+ assert dup_sub_term(f, ZZ(2), 2, ZZ) == dup_normal([-1, 1, 1], ZZ)
99
+
100
+ assert dup_sub_term(f, ZZ(1), 3, ZZ) == dup_normal([-1, 1, 1, 1], ZZ)
101
+ assert dup_sub_term(f, ZZ(1), 4, ZZ) == dup_normal([-1, 0, 1, 1, 1], ZZ)
102
+ assert dup_sub_term(f, ZZ(1), 5, ZZ) == dup_normal([-1, 0, 0, 1, 1, 1], ZZ)
103
+ assert dup_sub_term(
104
+ f, ZZ(1), 6, ZZ) == dup_normal([-1, 0, 0, 0, 1, 1, 1], ZZ)
105
+
106
+ assert dup_sub_term(f, ZZ(1), 2, ZZ) == dup_normal([1, 1], ZZ)
107
+
108
+
109
+ def test_dmp_sub_term():
110
+ assert dmp_sub_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, 0, ZZ) == \
111
+ dup_sub_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, ZZ)
112
+ assert dmp_sub_term(f_0, [[]], 3, 2, ZZ) == f_0
113
+ assert dmp_sub_term(F_0, [[]], 3, 2, QQ) == F_0
114
+
115
+
116
+ def test_dup_mul_term():
117
+ f = dup_normal([], ZZ)
118
+
119
+ assert dup_mul_term(f, ZZ(2), 3, ZZ) == dup_normal([], ZZ)
120
+
121
+ f = dup_normal([1, 1], ZZ)
122
+
123
+ assert dup_mul_term(f, ZZ(0), 3, ZZ) == dup_normal([], ZZ)
124
+
125
+ f = dup_normal([1, 2, 3], ZZ)
126
+
127
+ assert dup_mul_term(f, ZZ(2), 0, ZZ) == dup_normal([2, 4, 6], ZZ)
128
+ assert dup_mul_term(f, ZZ(2), 1, ZZ) == dup_normal([2, 4, 6, 0], ZZ)
129
+ assert dup_mul_term(f, ZZ(2), 2, ZZ) == dup_normal([2, 4, 6, 0, 0], ZZ)
130
+ assert dup_mul_term(f, ZZ(2), 3, ZZ) == dup_normal([2, 4, 6, 0, 0, 0], ZZ)
131
+
132
+
133
+ def test_dmp_mul_term():
134
+ assert dmp_mul_term([ZZ(1), ZZ(2), ZZ(3)], ZZ(2), 1, 0, ZZ) == \
135
+ dup_mul_term([ZZ(1), ZZ(2), ZZ(3)], ZZ(2), 1, ZZ)
136
+
137
+ assert dmp_mul_term([[]], [ZZ(2)], 3, 1, ZZ) == [[]]
138
+ assert dmp_mul_term([[ZZ(1)]], [], 3, 1, ZZ) == [[]]
139
+
140
+ assert dmp_mul_term([[ZZ(1), ZZ(2)], [ZZ(3)]], [ZZ(2)], 2, 1, ZZ) == \
141
+ [[ZZ(2), ZZ(4)], [ZZ(6)], [], []]
142
+
143
+ assert dmp_mul_term([[]], [QQ(2, 3)], 3, 1, QQ) == [[]]
144
+ assert dmp_mul_term([[QQ(1, 2)]], [], 3, 1, QQ) == [[]]
145
+
146
+ assert dmp_mul_term([[QQ(1, 5), QQ(2, 5)], [QQ(3, 5)]], [QQ(2, 3)], 2, 1, QQ) == \
147
+ [[QQ(2, 15), QQ(4, 15)], [QQ(6, 15)], [], []]
148
+
149
+
150
+ def test_dup_add_ground():
151
+ f = ZZ.map([1, 2, 3, 4])
152
+ g = ZZ.map([1, 2, 3, 8])
153
+
154
+ assert dup_add_ground(f, ZZ(4), ZZ) == g
155
+
156
+
157
+ def test_dmp_add_ground():
158
+ f = ZZ.map([[1], [2], [3], [4]])
159
+ g = ZZ.map([[1], [2], [3], [8]])
160
+
161
+ assert dmp_add_ground(f, ZZ(4), 1, ZZ) == g
162
+
163
+
164
+ def test_dup_sub_ground():
165
+ f = ZZ.map([1, 2, 3, 4])
166
+ g = ZZ.map([1, 2, 3, 0])
167
+
168
+ assert dup_sub_ground(f, ZZ(4), ZZ) == g
169
+
170
+
171
+ def test_dmp_sub_ground():
172
+ f = ZZ.map([[1], [2], [3], [4]])
173
+ g = ZZ.map([[1], [2], [3], []])
174
+
175
+ assert dmp_sub_ground(f, ZZ(4), 1, ZZ) == g
176
+
177
+
178
+ def test_dup_mul_ground():
179
+ f = dup_normal([], ZZ)
180
+
181
+ assert dup_mul_ground(f, ZZ(2), ZZ) == dup_normal([], ZZ)
182
+
183
+ f = dup_normal([1, 2, 3], ZZ)
184
+
185
+ assert dup_mul_ground(f, ZZ(0), ZZ) == dup_normal([], ZZ)
186
+ assert dup_mul_ground(f, ZZ(2), ZZ) == dup_normal([2, 4, 6], ZZ)
187
+
188
+
189
+ def test_dmp_mul_ground():
190
+ assert dmp_mul_ground(f_0, ZZ(2), 2, ZZ) == [
191
+ [[ZZ(2), ZZ(4), ZZ(6)], [ZZ(4)]],
192
+ [[ZZ(6)]],
193
+ [[ZZ(8), ZZ(10), ZZ(12)], [ZZ(2), ZZ(4), ZZ(2)], [ZZ(2)]]
194
+ ]
195
+
196
+ assert dmp_mul_ground(F_0, QQ(1, 2), 2, QQ) == [
197
+ [[QQ(1, 14), QQ(2, 14), QQ(3, 14)], [QQ(2, 14)]],
198
+ [[QQ(3, 14)]],
199
+ [[QQ(4, 14), QQ(5, 14), QQ(6, 14)], [QQ(1, 14), QQ(2, 14),
200
+ QQ(1, 14)], [QQ(1, 14)]]
201
+ ]
202
+
203
+
204
+ def test_dup_quo_ground():
205
+ raises(ZeroDivisionError, lambda: dup_quo_ground(dup_normal([1, 2,
206
+ 3], ZZ), ZZ(0), ZZ))
207
+
208
+ f = dup_normal([], ZZ)
209
+
210
+ assert dup_quo_ground(f, ZZ(3), ZZ) == dup_normal([], ZZ)
211
+
212
+ f = dup_normal([6, 2, 8], ZZ)
213
+
214
+ assert dup_quo_ground(f, ZZ(1), ZZ) == f
215
+ assert dup_quo_ground(f, ZZ(2), ZZ) == dup_normal([3, 1, 4], ZZ)
216
+
217
+ assert dup_quo_ground(f, ZZ(3), ZZ) == dup_normal([2, 0, 2], ZZ)
218
+
219
+ f = dup_normal([6, 2, 8], QQ)
220
+
221
+ assert dup_quo_ground(f, QQ(1), QQ) == f
222
+ assert dup_quo_ground(f, QQ(2), QQ) == [QQ(3), QQ(1), QQ(4)]
223
+ assert dup_quo_ground(f, QQ(7), QQ) == [QQ(6, 7), QQ(2, 7), QQ(8, 7)]
224
+
225
+
226
+ def test_dup_exquo_ground():
227
+ raises(ZeroDivisionError, lambda: dup_exquo_ground(dup_normal([1,
228
+ 2, 3], ZZ), ZZ(0), ZZ))
229
+ raises(ExactQuotientFailed, lambda: dup_exquo_ground(dup_normal([1,
230
+ 2, 3], ZZ), ZZ(3), ZZ))
231
+
232
+ f = dup_normal([], ZZ)
233
+
234
+ assert dup_exquo_ground(f, ZZ(3), ZZ) == dup_normal([], ZZ)
235
+
236
+ f = dup_normal([6, 2, 8], ZZ)
237
+
238
+ assert dup_exquo_ground(f, ZZ(1), ZZ) == f
239
+ assert dup_exquo_ground(f, ZZ(2), ZZ) == dup_normal([3, 1, 4], ZZ)
240
+
241
+ f = dup_normal([6, 2, 8], QQ)
242
+
243
+ assert dup_exquo_ground(f, QQ(1), QQ) == f
244
+ assert dup_exquo_ground(f, QQ(2), QQ) == [QQ(3), QQ(1), QQ(4)]
245
+ assert dup_exquo_ground(f, QQ(7), QQ) == [QQ(6, 7), QQ(2, 7), QQ(8, 7)]
246
+
247
+
248
+ def test_dmp_quo_ground():
249
+ f = dmp_normal([[6], [2], [8]], 1, ZZ)
250
+
251
+ assert dmp_quo_ground(f, ZZ(1), 1, ZZ) == f
252
+ assert dmp_quo_ground(
253
+ f, ZZ(2), 1, ZZ) == dmp_normal([[3], [1], [4]], 1, ZZ)
254
+
255
+ assert dmp_normal(dmp_quo_ground(
256
+ f, ZZ(3), 1, ZZ), 1, ZZ) == dmp_normal([[2], [], [2]], 1, ZZ)
257
+
258
+
259
+ def test_dmp_exquo_ground():
260
+ f = dmp_normal([[6], [2], [8]], 1, ZZ)
261
+
262
+ assert dmp_exquo_ground(f, ZZ(1), 1, ZZ) == f
263
+ assert dmp_exquo_ground(
264
+ f, ZZ(2), 1, ZZ) == dmp_normal([[3], [1], [4]], 1, ZZ)
265
+
266
+
267
+ def test_dup_lshift():
268
+ assert dup_lshift([], 3, ZZ) == []
269
+ assert dup_lshift([1], 3, ZZ) == [1, 0, 0, 0]
270
+
271
+
272
+ def test_dup_rshift():
273
+ assert dup_rshift([], 3, ZZ) == []
274
+ assert dup_rshift([1, 0, 0, 0], 3, ZZ) == [1]
275
+
276
+
277
+ def test_dup_abs():
278
+ assert dup_abs([], ZZ) == []
279
+ assert dup_abs([ZZ( 1)], ZZ) == [ZZ(1)]
280
+ assert dup_abs([ZZ(-7)], ZZ) == [ZZ(7)]
281
+ assert dup_abs([ZZ(-1), ZZ(2), ZZ(3)], ZZ) == [ZZ(1), ZZ(2), ZZ(3)]
282
+
283
+ assert dup_abs([], QQ) == []
284
+ assert dup_abs([QQ( 1, 2)], QQ) == [QQ(1, 2)]
285
+ assert dup_abs([QQ(-7, 3)], QQ) == [QQ(7, 3)]
286
+ assert dup_abs(
287
+ [QQ(-1, 7), QQ(2, 7), QQ(3, 7)], QQ) == [QQ(1, 7), QQ(2, 7), QQ(3, 7)]
288
+
289
+
290
+ def test_dmp_abs():
291
+ assert dmp_abs([ZZ(-1)], 0, ZZ) == [ZZ(1)]
292
+ assert dmp_abs([QQ(-1, 2)], 0, QQ) == [QQ(1, 2)]
293
+
294
+ assert dmp_abs([[[]]], 2, ZZ) == [[[]]]
295
+ assert dmp_abs([[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
296
+ assert dmp_abs([[[ZZ(-7)]]], 2, ZZ) == [[[ZZ(7)]]]
297
+
298
+ assert dmp_abs([[[]]], 2, QQ) == [[[]]]
299
+ assert dmp_abs([[[QQ(1, 2)]]], 2, QQ) == [[[QQ(1, 2)]]]
300
+ assert dmp_abs([[[QQ(-7, 9)]]], 2, QQ) == [[[QQ(7, 9)]]]
301
+
302
+
303
+ def test_dup_neg():
304
+ assert dup_neg([], ZZ) == []
305
+ assert dup_neg([ZZ(1)], ZZ) == [ZZ(-1)]
306
+ assert dup_neg([ZZ(-7)], ZZ) == [ZZ(7)]
307
+ assert dup_neg([ZZ(-1), ZZ(2), ZZ(3)], ZZ) == [ZZ(1), ZZ(-2), ZZ(-3)]
308
+
309
+ assert dup_neg([], QQ) == []
310
+ assert dup_neg([QQ(1, 2)], QQ) == [QQ(-1, 2)]
311
+ assert dup_neg([QQ(-7, 9)], QQ) == [QQ(7, 9)]
312
+ assert dup_neg([QQ(
313
+ -1, 7), QQ(2, 7), QQ(3, 7)], QQ) == [QQ(1, 7), QQ(-2, 7), QQ(-3, 7)]
314
+
315
+
316
+ def test_dmp_neg():
317
+ assert dmp_neg([ZZ(-1)], 0, ZZ) == [ZZ(1)]
318
+ assert dmp_neg([QQ(-1, 2)], 0, QQ) == [QQ(1, 2)]
319
+
320
+ assert dmp_neg([[[]]], 2, ZZ) == [[[]]]
321
+ assert dmp_neg([[[ZZ(1)]]], 2, ZZ) == [[[ZZ(-1)]]]
322
+ assert dmp_neg([[[ZZ(-7)]]], 2, ZZ) == [[[ZZ(7)]]]
323
+
324
+ assert dmp_neg([[[]]], 2, QQ) == [[[]]]
325
+ assert dmp_neg([[[QQ(1, 9)]]], 2, QQ) == [[[QQ(-1, 9)]]]
326
+ assert dmp_neg([[[QQ(-7, 9)]]], 2, QQ) == [[[QQ(7, 9)]]]
327
+
328
+
329
+ def test_dup_add():
330
+ assert dup_add([], [], ZZ) == []
331
+ assert dup_add([ZZ(1)], [], ZZ) == [ZZ(1)]
332
+ assert dup_add([], [ZZ(1)], ZZ) == [ZZ(1)]
333
+ assert dup_add([ZZ(1)], [ZZ(1)], ZZ) == [ZZ(2)]
334
+ assert dup_add([ZZ(1)], [ZZ(2)], ZZ) == [ZZ(3)]
335
+
336
+ assert dup_add([ZZ(1), ZZ(2)], [ZZ(1)], ZZ) == [ZZ(1), ZZ(3)]
337
+ assert dup_add([ZZ(1)], [ZZ(1), ZZ(2)], ZZ) == [ZZ(1), ZZ(3)]
338
+
339
+ assert dup_add([ZZ(1), ZZ(
340
+ 2), ZZ(3)], [ZZ(8), ZZ(9), ZZ(10)], ZZ) == [ZZ(9), ZZ(11), ZZ(13)]
341
+
342
+ assert dup_add([], [], QQ) == []
343
+ assert dup_add([QQ(1, 2)], [], QQ) == [QQ(1, 2)]
344
+ assert dup_add([], [QQ(1, 2)], QQ) == [QQ(1, 2)]
345
+ assert dup_add([QQ(1, 4)], [QQ(1, 4)], QQ) == [QQ(1, 2)]
346
+ assert dup_add([QQ(1, 4)], [QQ(1, 2)], QQ) == [QQ(3, 4)]
347
+
348
+ assert dup_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ) == [QQ(1, 2), QQ(5, 3)]
349
+ assert dup_add([QQ(1)], [QQ(1, 2), QQ(2, 3)], QQ) == [QQ(1, 2), QQ(5, 3)]
350
+
351
+ assert dup_add([QQ(1, 7), QQ(2, 7), QQ(3, 7)], [QQ(
352
+ 8, 7), QQ(9, 7), QQ(10, 7)], QQ) == [QQ(9, 7), QQ(11, 7), QQ(13, 7)]
353
+
354
+
355
+ def test_dmp_add():
356
+ assert dmp_add([ZZ(1), ZZ(2)], [ZZ(1)], 0, ZZ) == \
357
+ dup_add([ZZ(1), ZZ(2)], [ZZ(1)], ZZ)
358
+ assert dmp_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], 0, QQ) == \
359
+ dup_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ)
360
+
361
+ assert dmp_add([[[]]], [[[]]], 2, ZZ) == [[[]]]
362
+ assert dmp_add([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[ZZ(1)]]]
363
+ assert dmp_add([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
364
+ assert dmp_add([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(3)]]]
365
+ assert dmp_add([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(3)]]]
366
+
367
+ assert dmp_add([[[]]], [[[]]], 2, QQ) == [[[]]]
368
+ assert dmp_add([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[QQ(1, 2)]]]
369
+ assert dmp_add([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[QQ(1, 2)]]]
370
+ assert dmp_add([[[QQ(2, 7)]]], [[[QQ(1, 7)]]], 2, QQ) == [[[QQ(3, 7)]]]
371
+ assert dmp_add([[[QQ(1, 7)]]], [[[QQ(2, 7)]]], 2, QQ) == [[[QQ(3, 7)]]]
372
+
373
+
374
+ def test_dup_sub():
375
+ assert dup_sub([], [], ZZ) == []
376
+ assert dup_sub([ZZ(1)], [], ZZ) == [ZZ(1)]
377
+ assert dup_sub([], [ZZ(1)], ZZ) == [ZZ(-1)]
378
+ assert dup_sub([ZZ(1)], [ZZ(1)], ZZ) == []
379
+ assert dup_sub([ZZ(1)], [ZZ(2)], ZZ) == [ZZ(-1)]
380
+
381
+ assert dup_sub([ZZ(1), ZZ(2)], [ZZ(1)], ZZ) == [ZZ(1), ZZ(1)]
382
+ assert dup_sub([ZZ(1)], [ZZ(1), ZZ(2)], ZZ) == [ZZ(-1), ZZ(-1)]
383
+
384
+ assert dup_sub([ZZ(3), ZZ(
385
+ 2), ZZ(1)], [ZZ(8), ZZ(9), ZZ(10)], ZZ) == [ZZ(-5), ZZ(-7), ZZ(-9)]
386
+
387
+ assert dup_sub([], [], QQ) == []
388
+ assert dup_sub([QQ(1, 2)], [], QQ) == [QQ(1, 2)]
389
+ assert dup_sub([], [QQ(1, 2)], QQ) == [QQ(-1, 2)]
390
+ assert dup_sub([QQ(1, 3)], [QQ(1, 3)], QQ) == []
391
+ assert dup_sub([QQ(1, 3)], [QQ(2, 3)], QQ) == [QQ(-1, 3)]
392
+
393
+ assert dup_sub([QQ(1, 7), QQ(2, 7)], [QQ(1)], QQ) == [QQ(1, 7), QQ(-5, 7)]
394
+ assert dup_sub([QQ(1)], [QQ(1, 7), QQ(2, 7)], QQ) == [QQ(-1, 7), QQ(5, 7)]
395
+
396
+ assert dup_sub([QQ(3, 7), QQ(2, 7), QQ(1, 7)], [QQ(
397
+ 8, 7), QQ(9, 7), QQ(10, 7)], QQ) == [QQ(-5, 7), QQ(-7, 7), QQ(-9, 7)]
398
+
399
+
400
+ def test_dmp_sub():
401
+ assert dmp_sub([ZZ(1), ZZ(2)], [ZZ(1)], 0, ZZ) == \
402
+ dup_sub([ZZ(1), ZZ(2)], [ZZ(1)], ZZ)
403
+ assert dmp_sub([QQ(1, 2), QQ(2, 3)], [QQ(1)], 0, QQ) == \
404
+ dup_sub([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ)
405
+
406
+ assert dmp_sub([[[]]], [[[]]], 2, ZZ) == [[[]]]
407
+ assert dmp_sub([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[ZZ(1)]]]
408
+ assert dmp_sub([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(-1)]]]
409
+ assert dmp_sub([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
410
+ assert dmp_sub([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(-1)]]]
411
+
412
+ assert dmp_sub([[[]]], [[[]]], 2, QQ) == [[[]]]
413
+ assert dmp_sub([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[QQ(1, 2)]]]
414
+ assert dmp_sub([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[QQ(-1, 2)]]]
415
+ assert dmp_sub([[[QQ(2, 7)]]], [[[QQ(1, 7)]]], 2, QQ) == [[[QQ(1, 7)]]]
416
+ assert dmp_sub([[[QQ(1, 7)]]], [[[QQ(2, 7)]]], 2, QQ) == [[[QQ(-1, 7)]]]
417
+
418
+
419
+ def test_dup_add_mul():
420
+ assert dup_add_mul([ZZ(1), ZZ(2), ZZ(3)], [ZZ(3), ZZ(2), ZZ(1)],
421
+ [ZZ(1), ZZ(2)], ZZ) == [ZZ(3), ZZ(9), ZZ(7), ZZ(5)]
422
+ assert dmp_add_mul([[ZZ(1), ZZ(2)], [ZZ(3)]], [[ZZ(3)], [ZZ(2), ZZ(1)]],
423
+ [[ZZ(1)], [ZZ(2)]], 1, ZZ) == [[ZZ(3)], [ZZ(3), ZZ(9)], [ZZ(4), ZZ(5)]]
424
+
425
+
426
+ def test_dup_sub_mul():
427
+ assert dup_sub_mul([ZZ(1), ZZ(2), ZZ(3)], [ZZ(3), ZZ(2), ZZ(1)],
428
+ [ZZ(1), ZZ(2)], ZZ) == [ZZ(-3), ZZ(-7), ZZ(-3), ZZ(1)]
429
+ assert dmp_sub_mul([[ZZ(1), ZZ(2)], [ZZ(3)]], [[ZZ(3)], [ZZ(2), ZZ(1)]],
430
+ [[ZZ(1)], [ZZ(2)]], 1, ZZ) == [[ZZ(-3)], [ZZ(-1), ZZ(-5)], [ZZ(-4), ZZ(1)]]
431
+
432
+
433
+ def test_dup_mul():
434
+ assert dup_mul([], [], ZZ) == []
435
+ assert dup_mul([], [ZZ(1)], ZZ) == []
436
+ assert dup_mul([ZZ(1)], [], ZZ) == []
437
+ assert dup_mul([ZZ(1)], [ZZ(1)], ZZ) == [ZZ(1)]
438
+ assert dup_mul([ZZ(5)], [ZZ(7)], ZZ) == [ZZ(35)]
439
+
440
+ assert dup_mul([], [], QQ) == []
441
+ assert dup_mul([], [QQ(1, 2)], QQ) == []
442
+ assert dup_mul([QQ(1, 2)], [], QQ) == []
443
+ assert dup_mul([QQ(1, 2)], [QQ(4, 7)], QQ) == [QQ(2, 7)]
444
+ assert dup_mul([QQ(5, 7)], [QQ(3, 7)], QQ) == [QQ(15, 49)]
445
+
446
+ f = dup_normal([3, 0, 0, 6, 1, 2], ZZ)
447
+ g = dup_normal([4, 0, 1, 0], ZZ)
448
+ h = dup_normal([12, 0, 3, 24, 4, 14, 1, 2, 0], ZZ)
449
+
450
+ assert dup_mul(f, g, ZZ) == h
451
+ assert dup_mul(g, f, ZZ) == h
452
+
453
+ f = dup_normal([2, 0, 0, 1, 7], ZZ)
454
+ h = dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
455
+
456
+ assert dup_mul(f, f, ZZ) == h
457
+
458
+ K = FF(6)
459
+
460
+ assert dup_mul([K(2), K(1)], [K(3), K(4)], K) == [K(5), K(4)]
461
+
462
+ p1 = dup_normal([79, -1, 78, -94, -10, 11, 32, -19, 78, 2, -89, 30, 73, 42,
463
+ 85, 77, 83, -30, -34, -2, 95, -81, 37, -49, -46, -58, -16, 37, 35, -11,
464
+ -57, -15, -31, 67, -20, 27, 76, 2, 70, 67, -65, 65, -26, -93, -44, -12,
465
+ -92, 57, -90, -57, -11, -67, -98, -69, 97, -41, 89, 33, 89, -50, 81,
466
+ -31, 60, -27, 43, 29, -77, 44, 21, -91, 32, -57, 33, 3, 53, -51, -38,
467
+ -99, -84, 23, -50, 66, -100, 1, -75, -25, 27, -60, 98, -51, -87, 6, 8,
468
+ 78, -28, -95, -88, 12, -35, 26, -9, 16, -92, 55, -7, -86, 68, -39, -46,
469
+ 84, 94, 45, 60, 92, 68, -75, -74, -19, 8, 75, 78, 91, 57, 34, 14, -3,
470
+ -49, 65, 78, -18, 6, -29, -80, -98, 17, 13, 58, 21, 20, 9, 37, 7, -30,
471
+ -53, -20, 34, 67, -42, 89, -22, 73, 43, -6, 5, 51, -8, -15, -52, -22,
472
+ -58, -72, -3, 43, -92, 82, 83, -2, -13, -23, -60, 16, -94, -8, -28,
473
+ -95, -72, 63, -90, 76, 6, -43, -100, -59, 76, 3, 3, 46, -85, 75, 62,
474
+ -71, -76, 88, 97, -72, -1, 30, -64, 72, -48, 14, -78, 58, 63, -91, 24,
475
+ -87, -27, -80, -100, -44, 98, 70, 100, -29, -38, 11, 77, 100, 52, 86,
476
+ 65, -5, -42, -81, -38, -42, 43, -2, -70, -63, -52], ZZ)
477
+ p2 = dup_normal([65, -19, -47, 1, 90, 81, -15, -34, 25, -75, 9, -83, 50, -5,
478
+ -44, 31, 1, 70, -7, 78, 74, 80, 85, 65, 21, 41, 66, 19, -40, 63, -21,
479
+ -27, 32, 69, 83, 34, -35, 14, 81, 57, -75, 32, -67, -89, -100, -61, 46,
480
+ 84, -78, -29, -50, -94, -24, -32, -68, -16, 100, -7, -72, -89, 35, 82,
481
+ 58, 81, -92, 62, 5, -47, -39, -58, -72, -13, 84, 44, 55, -25, 48, -54,
482
+ -31, -56, -11, -50, -84, 10, 67, 17, 13, -14, 61, 76, -64, -44, -40,
483
+ -96, 11, -11, -94, 2, 6, 27, -6, 68, -54, 66, -74, -14, -1, -24, -73,
484
+ 96, 89, -11, -89, 56, -53, 72, -43, 96, 25, 63, -31, 29, 68, 83, 91,
485
+ -93, -19, -38, -40, 40, -12, -19, -79, 44, 100, -66, -29, -77, 62, 39,
486
+ -8, 11, -97, 14, 87, 64, 21, -18, 13, 15, -59, -75, -99, -88, 57, 54,
487
+ 56, -67, 6, -63, -59, -14, 28, 87, -20, -39, 84, -91, -2, 49, -75, 11,
488
+ -24, -95, 36, 66, 5, 25, -72, -40, 86, 90, 37, -33, 57, -35, 29, -18,
489
+ 4, -79, 64, -17, -27, 21, 29, -5, -44, -87, -24, 52, 78, 11, -23, -53,
490
+ 36, 42, 21, -68, 94, -91, -51, -21, 51, -76, 72, 31, 24, -48, -80, -9,
491
+ 37, -47, -6, -8, -63, -91, 79, -79, -100, 38, -20, 38, 100, 83, -90,
492
+ 87, 63, -36, 82, -19, 18, -98, -38, 26, 98, -70, 79, 92, 12, 12, 70,
493
+ 74, 36, 48, -13, 31, 31, -47, -71, -12, -64, 36, -42, 32, -86, 60, 83,
494
+ 70, 55, 0, 1, 29, -35, 8, -82, 8, -73, -46, -50, 43, 48, -5, -86, -72,
495
+ 44, -90, 19, 19, 5, -20, 97, -13, -66, -5, 5, -69, 64, -30, 41, 51, 36,
496
+ 13, -99, -61, 94, -12, 74, 98, 68, 24, 46, -97, -87, -6, -27, 82, 62,
497
+ -11, -77, 86, 66, -47, -49, -50, 13, 18, 89, -89, 46, -80, 13, 98, -35,
498
+ -36, -25, 12, 20, 26, -52, 79, 27, 79, 100, 8, 62, -58, -28, 37], ZZ)
499
+ res = dup_normal([5135, -1566, 1376, -7466, 4579, 11710, 8001, -7183,
500
+ -3737, -7439, 345, -10084, 24522, -1201, 1070, -10245, 9582, 9264,
501
+ 1903, 23312, 18953, 10037, -15268, -5450, 6442, -6243, -3777, 5110,
502
+ 10936, -16649, -6022, 16255, 31300, 24818, 31922, 32760, 7854, 27080,
503
+ 15766, 29596, 7139, 31945, -19810, 465, -38026, -3971, 9641, 465,
504
+ -19375, 5524, -30112, -11960, -12813, 13535, 30670, 5925, -43725,
505
+ -14089, 11503, -22782, 6371, 43881, 37465, -33529, -33590, -39798,
506
+ -37854, -18466, -7908, -35825, -26020, -36923, -11332, -5699, 25166,
507
+ -3147, 19885, 12962, -20659, -1642, 27723, -56331, -24580, -11010,
508
+ -20206, 20087, -23772, -16038, 38580, 20901, -50731, 32037, -4299,
509
+ 26508, 18038, -28357, 31846, -7405, -20172, -15894, 2096, 25110,
510
+ -45786, 45918, -55333, -31928, -49428, -29824, -58796, -24609, -15408,
511
+ 69, -35415, -18439, 10123, -20360, -65949, 33356, -20333, 26476,
512
+ -32073, 33621, 930, 28803, -42791, 44716, 38164, 12302, -1739, 11421,
513
+ 73385, -7613, 14297, 38155, -414, 77587, 24338, -21415, 29367, 42639,
514
+ 13901, -288, 51027, -11827, 91260, 43407, 88521, -15186, 70572, -12049,
515
+ 5090, -12208, -56374, 15520, -623, -7742, 50825, 11199, -14894, 40892,
516
+ 59591, -31356, -28696, -57842, -87751, -33744, -28436, -28945, -40287,
517
+ 37957, -35638, 33401, -61534, 14870, 40292, 70366, -10803, 102290,
518
+ -71719, -85251, 7902, -22409, 75009, 99927, 35298, -1175, -762, -34744,
519
+ -10587, -47574, -62629, -19581, -43659, -54369, -32250, -39545, 15225,
520
+ -24454, 11241, -67308, -30148, 39929, 37639, 14383, -73475, -77636,
521
+ -81048, -35992, 41601, -90143, 76937, -8112, 56588, 9124, -40094,
522
+ -32340, 13253, 10898, -51639, 36390, 12086, -1885, 100714, -28561,
523
+ -23784, -18735, 18916, 16286, 10742, -87360, -13697, 10689, -19477,
524
+ -29770, 5060, 20189, -8297, 112407, 47071, 47743, 45519, -4109, 17468,
525
+ -68831, 78325, -6481, -21641, -19459, 30919, 96115, 8607, 53341, 32105,
526
+ -16211, 23538, 57259, -76272, -40583, 62093, 38511, -34255, -40665,
527
+ -40604, -37606, -15274, 33156, -13885, 103636, 118678, -14101, -92682,
528
+ -100791, 2634, 63791, 98266, 19286, -34590, -21067, -71130, 25380,
529
+ -40839, -27614, -26060, 52358, -15537, 27138, -6749, 36269, -33306,
530
+ 13207, -91084, -5540, -57116, 69548, 44169, -57742, -41234, -103327,
531
+ -62904, -8566, 41149, -12866, 71188, 23980, 1838, 58230, 73950, 5594,
532
+ 43113, -8159, -15925, 6911, 85598, -75016, -16214, -62726, -39016,
533
+ 8618, -63882, -4299, 23182, 49959, 49342, -3238, -24913, -37138, 78361,
534
+ 32451, 6337, -11438, -36241, -37737, 8169, -3077, -24829, 57953, 53016,
535
+ -31511, -91168, 12599, -41849, 41576, 55275, -62539, 47814, -62319,
536
+ 12300, -32076, -55137, -84881, -27546, 4312, -3433, -54382, 113288,
537
+ -30157, 74469, 18219, 79880, -2124, 98911, 17655, -33499, -32861,
538
+ 47242, -37393, 99765, 14831, -44483, 10800, -31617, -52710, 37406,
539
+ 22105, 29704, -20050, 13778, 43683, 36628, 8494, 60964, -22644, 31550,
540
+ -17693, 33805, -124879, -12302, 19343, 20400, -30937, -21574, -34037,
541
+ -33380, 56539, -24993, -75513, -1527, 53563, 65407, -101, 53577, 37991,
542
+ 18717, -23795, -8090, -47987, -94717, 41967, 5170, -14815, -94311,
543
+ 17896, -17734, -57718, -774, -38410, 24830, 29682, 76480, 58802,
544
+ -46416, -20348, -61353, -68225, -68306, 23822, -31598, 42972, 36327,
545
+ 28968, -65638, -21638, 24354, -8356, 26777, 52982, -11783, -44051,
546
+ -26467, -44721, -28435, -53265, -25574, -2669, 44155, 22946, -18454,
547
+ -30718, -11252, 58420, 8711, 67447, 4425, 41749, 67543, 43162, 11793,
548
+ -41907, 20477, -13080, 6559, -6104, -13244, 42853, 42935, 29793, 36730,
549
+ -28087, 28657, 17946, 7503, 7204, 21491, -27450, -24241, -98156,
550
+ -18082, -42613, -24928, 10775, -14842, -44127, 55910, 14777, 31151, -2194,
551
+ 39206, -2100, -4211, 11827, -8918, -19471, 72567, 36447, -65590, -34861,
552
+ -17147, -45303, 9025, -7333, -35473, 11101, 11638, 3441, 6626, -41800,
553
+ 9416, 13679, 33508, 40502, -60542, 16358, 8392, -43242, -35864, -34127,
554
+ -48721, 35878, 30598, 28630, 20279, -19983, -14638, -24455, -1851, -11344,
555
+ 45150, 42051, 26034, -28889, -32382, -3527, -14532, 22564, -22346, 477,
556
+ 11706, 28338, -25972, -9185, -22867, -12522, 32120, -4424, 11339, -33913,
557
+ -7184, 5101, -23552, -17115, -31401, -6104, 21906, 25708, 8406, 6317,
558
+ -7525, 5014, 20750, 20179, 22724, 11692, 13297, 2493, -253, -16841, -17339,
559
+ -6753, -4808, 2976, -10881, -10228, -13816, -12686, 1385, 2316, 2190, -875,
560
+ -1924], ZZ)
561
+
562
+ assert dup_mul(p1, p2, ZZ) == res
563
+
564
+ p1 = dup_normal([83, -61, -86, -24, 12, 43, -88, -9, 42, 55, -66, 74, 95,
565
+ -25, -12, 68, -99, 4, 45, 6, -15, -19, 78, 65, -55, 47, -13, 17, 86,
566
+ 81, -58, -27, 50, -40, -24, 39, -41, -92, 75, 90, -1, 40, -15, -27,
567
+ -35, 68, 70, -64, -40, 78, -88, -58, -39, 69, 46, 12, 28, -94, -37,
568
+ -50, -80, -96, -61, 25, 1, 71, 4, 12, 48, 4, 34, -47, -75, 5, 48, 82,
569
+ 88, 23, 98, 35, 17, -10, 48, -61, -95, 47, 65, -19, -66, -57, -6, -51,
570
+ -42, -89, 66, -13, 18, 37, 90, -23, 72, 96, -53, 0, 40, -73, -52, -68,
571
+ 32, -25, -53, 79, -52, 18, 44, 73, -81, 31, -90, 70, 3, 36, 48, 76,
572
+ -24, -44, 23, 98, -4, 73, 69, 88, -70, 14, -68, 94, -78, -15, -64, -97,
573
+ -70, -35, 65, 88, 49, -53, -7, 12, -45, -7, 59, -94, 99, -2, 67, -60,
574
+ -71, 29, -62, -77, 1, 51, 17, 80, -20, -47, -19, 24, -9, 39, -23, 21,
575
+ -84, 10, 84, 56, -17, -21, -66, 85, 70, 46, -51, -22, -95, 78, -60,
576
+ -96, -97, -45, 72, 35, 30, -61, -92, -93, -60, -61, 4, -4, -81, -73,
577
+ 46, 53, -11, 26, 94, 45, 14, -78, 55, 84, -68, 98, 60, 23, 100, -63,
578
+ 68, 96, -16, 3, 56, 21, -58, 62, -67, 66, 85, 41, -79, -22, 97, -67,
579
+ 82, 82, -96, -20, -7, 48, -67, 48, -9, -39, 78], ZZ)
580
+ p2 = dup_normal([52, 88, 76, 66, 9, -64, 46, -20, -28, 69, 60, 96, -36,
581
+ -92, -30, -11, -35, 35, 55, 63, -92, -7, 25, -58, 74, 55, -6, 4, 47,
582
+ -92, -65, 67, -45, 74, -76, 59, -6, 69, 39, 24, -71, -7, 39, -45, 60,
583
+ -68, 98, 97, -79, 17, 4, 94, -64, 68, -100, -96, -2, 3, 22, 96, 54,
584
+ -77, -86, 67, 6, 57, 37, 40, 89, -78, 64, -94, -45, -92, 57, 87, -26,
585
+ 36, 19, 97, 25, 77, -87, 24, 43, -5, 35, 57, 83, 71, 35, 63, 61, 96,
586
+ -22, 8, -1, 96, 43, 45, 94, -93, 36, 71, -41, -99, 85, -48, 59, 52,
587
+ -17, 5, 87, -16, -68, -54, 76, -18, 100, 91, -42, -70, -66, -88, -12,
588
+ 1, 95, -82, 52, 43, -29, 3, 12, 72, -99, -43, -32, -93, -51, 16, -20,
589
+ -12, -11, 5, 33, -38, 93, -5, -74, 25, 74, -58, 93, 59, -63, -86, 63,
590
+ -20, -4, -74, -73, -95, 29, -28, 93, -91, -2, -38, -62, 77, -58, -85,
591
+ -28, 95, 38, 19, -69, 86, 94, 25, -2, -4, 47, 34, -59, 35, -48, 29,
592
+ -63, -53, 34, 29, 66, 73, 6, 92, -84, 89, 15, 81, 93, 97, 51, -72, -78,
593
+ 25, 60, 90, -45, 39, 67, -84, -62, 57, 26, -32, -56, -14, -83, 76, 5,
594
+ -2, 99, -100, 28, 46, 94, -7, 53, -25, 16, -23, -36, 89, -78, -63, 31,
595
+ 1, 84, -99, -52, 76, 48, 90, -76, 44, -19, 54, -36, -9, -73, -100, -69,
596
+ 31, 42, 25, -39, 76, -26, -8, -14, 51, 3, 37, 45, 2, -54, 13, -34, -92,
597
+ 17, -25, -65, 53, -63, 30, 4, -70, -67, 90, 52, 51, 18, -3, 31, -45,
598
+ -9, 59, 63, -87, 22, -32, 29, -38, 21, 36, -82, 27, -11], ZZ)
599
+ res = dup_normal([4316, 4132, -3532, -7974, -11303, -10069, 5484, -3330,
600
+ -5874, 7734, 4673, 11327, -9884, -8031, 17343, 21035, -10570, -9285,
601
+ 15893, 3780, -14083, 8819, 17592, 10159, 7174, -11587, 8598, -16479,
602
+ 3602, 25596, 9781, 12163, 150, 18749, -21782, -12307, 27578, -2757,
603
+ -12573, 12565, 6345, -18956, 19503, -15617, 1443, -16778, 36851, 23588,
604
+ -28474, 5749, 40695, -7521, -53669, -2497, -18530, 6770, 57038, 3926,
605
+ -6927, -15399, 1848, -64649, -27728, 3644, 49608, 15187, -8902, -9480,
606
+ -7398, -40425, 4824, 23767, -7594, -6905, 33089, 18786, 12192, 24670,
607
+ 31114, 35334, -4501, -14676, 7107, -59018, -21352, 20777, 19661, 20653,
608
+ 33754, -885, -43758, 6269, 51897, -28719, -97488, -9527, 13746, 11644,
609
+ 17644, -21720, 23782, -10481, 47867, 20752, 33810, -1875, 39918, -7710,
610
+ -40840, 19808, -47075, 23066, 46616, 25201, 9287, 35436, -1602, 9645,
611
+ -11978, 13273, 15544, 33465, 20063, 44539, 11687, 27314, -6538, -37467,
612
+ 14031, 32970, -27086, 41323, 29551, 65910, -39027, -37800, -22232,
613
+ 8212, 46316, -28981, -55282, 50417, -44929, -44062, 73879, 37573,
614
+ -2596, -10877, -21893, -133218, -33707, -25753, -9531, 17530, 61126,
615
+ 2748, -56235, 43874, -10872, -90459, -30387, 115267, -7264, -44452,
616
+ 122626, 14839, -599, 10337, 57166, -67467, -54957, 63669, 1202, 18488,
617
+ 52594, 7205, -97822, 612, 78069, -5403, -63562, 47236, 36873, -154827,
618
+ -26188, 82427, -39521, 5628, 7416, 5276, -53095, 47050, 26121, -42207,
619
+ 79021, -13035, 2499, -66943, 29040, -72355, -23480, 23416, -12885,
620
+ -44225, -42688, -4224, 19858, 55299, 15735, 11465, 101876, -39169,
621
+ 51786, 14723, 43280, -68697, 16410, 92295, 56767, 7183, 111850, 4550,
622
+ 115451, -38443, -19642, -35058, 10230, 93829, 8925, 63047, 3146, 29250,
623
+ 8530, 5255, -98117, -115517, -76817, -8724, 41044, 1312, -35974, 79333,
624
+ -28567, 7547, -10580, -24559, -16238, 10794, -3867, 24848, 57770,
625
+ -51536, -35040, 71033, 29853, 62029, -7125, -125585, -32169, -47907,
626
+ 156811, -65176, -58006, -15757, -57861, 11963, 30225, -41901, -41681,
627
+ 31310, 27982, 18613, 61760, 60746, -59096, 33499, 30097, -17997, 24032,
628
+ 56442, -83042, 23747, -20931, -21978, -158752, -9883, -73598, -7987,
629
+ -7333, -125403, -116329, 30585, 53281, 51018, -29193, 88575, 8264,
630
+ -40147, -16289, 113088, 12810, -6508, 101552, -13037, 34440, -41840,
631
+ 101643, 24263, 80532, 61748, 65574, 6423, -20672, 6591, -10834, -71716,
632
+ 86919, -92626, 39161, 28490, 81319, 46676, 106720, 43530, 26998, 57456,
633
+ -8862, 60989, 13982, 3119, -2224, 14743, 55415, -49093, -29303, 28999,
634
+ 1789, 55953, -84043, -7780, -65013, 57129, -47251, 61484, 61994,
635
+ -78361, -82778, 22487, -26894, 9756, -74637, -15519, -4360, 30115,
636
+ 42433, 35475, 15286, 69768, 21509, -20214, 78675, -21163, 13596, 11443,
637
+ -10698, -53621, -53867, -24155, 64500, -42784, -33077, -16500, 873,
638
+ -52788, 14546, -38011, 36974, -39849, -34029, -94311, 83068, -50437,
639
+ -26169, -46746, 59185, 42259, -101379, -12943, 30089, -59086, 36271,
640
+ 22723, -30253, -52472, -70826, -23289, 3331, -31687, 14183, -857,
641
+ -28627, 35246, -51284, 5636, -6933, 66539, 36654, 50927, 24783, 3457,
642
+ 33276, 45281, 45650, -4938, -9968, -22590, 47995, 69229, 5214, -58365,
643
+ -17907, -14651, 18668, 18009, 12649, -11851, -13387, 20339, 52472,
644
+ -1087, -21458, -68647, 52295, 15849, 40608, 15323, 25164, -29368,
645
+ 10352, -7055, 7159, 21695, -5373, -54849, 101103, -24963, -10511,
646
+ 33227, 7659, 41042, -69588, 26718, -20515, 6441, 38135, -63, 24088,
647
+ -35364, -12785, -18709, 47843, 48533, -48575, 17251, -19394, 32878,
648
+ -9010, -9050, 504, -12407, 28076, -3429, 25324, -4210, -26119, 752,
649
+ -29203, 28251, -11324, -32140, -3366, -25135, 18702, -31588, -7047,
650
+ -24267, 49987, -14975, -33169, 37744, -7720, -9035, 16964, -2807, -421,
651
+ 14114, -17097, -13662, 40628, -12139, -9427, 5369, 17551, -13232, -16211,
652
+ 9804, -7422, 2677, 28635, -8280, -4906, 2908, -22558, 5604, 12459, 8756,
653
+ -3980, -4745, -18525, 7913, 5970, -16457, 20230, -6247, -13812, 2505,
654
+ 11899, 1409, -15094, 22540, -18863, 137, 11123, -4516, 2290, -8594, 12150,
655
+ -10380, 3005, 5235, -7350, 2535, -858], ZZ)
656
+
657
+ assert dup_mul(p1, p2, ZZ) == res
658
+
659
+
660
+ def test_dmp_mul():
661
+ assert dmp_mul([ZZ(5)], [ZZ(7)], 0, ZZ) == \
662
+ dup_mul([ZZ(5)], [ZZ(7)], ZZ)
663
+ assert dmp_mul([QQ(5, 7)], [QQ(3, 7)], 0, QQ) == \
664
+ dup_mul([QQ(5, 7)], [QQ(3, 7)], QQ)
665
+
666
+ assert dmp_mul([[[]]], [[[]]], 2, ZZ) == [[[]]]
667
+ assert dmp_mul([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[]]]
668
+ assert dmp_mul([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[]]]
669
+ assert dmp_mul([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(2)]]]
670
+ assert dmp_mul([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(2)]]]
671
+
672
+ assert dmp_mul([[[]]], [[[]]], 2, QQ) == [[[]]]
673
+ assert dmp_mul([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[]]]
674
+ assert dmp_mul([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[]]]
675
+ assert dmp_mul([[[QQ(2, 7)]]], [[[QQ(1, 3)]]], 2, QQ) == [[[QQ(2, 21)]]]
676
+ assert dmp_mul([[[QQ(1, 7)]]], [[[QQ(2, 3)]]], 2, QQ) == [[[QQ(2, 21)]]]
677
+
678
+ K = FF(6)
679
+
680
+ assert dmp_mul(
681
+ [[K(2)], [K(1)]], [[K(3)], [K(4)]], 1, K) == [[K(5)], [K(4)]]
682
+
683
+
684
+ def test_dup_sqr():
685
+ assert dup_sqr([], ZZ) == []
686
+ assert dup_sqr([ZZ(2)], ZZ) == [ZZ(4)]
687
+ assert dup_sqr([ZZ(1), ZZ(2)], ZZ) == [ZZ(1), ZZ(4), ZZ(4)]
688
+
689
+ assert dup_sqr([], QQ) == []
690
+ assert dup_sqr([QQ(2, 3)], QQ) == [QQ(4, 9)]
691
+ assert dup_sqr([QQ(1, 3), QQ(2, 3)], QQ) == [QQ(1, 9), QQ(4, 9), QQ(4, 9)]
692
+
693
+ f = dup_normal([2, 0, 0, 1, 7], ZZ)
694
+
695
+ assert dup_sqr(f, ZZ) == dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
696
+
697
+ K = FF(9)
698
+
699
+ assert dup_sqr([K(3), K(4)], K) == [K(6), K(7)]
700
+
701
+
702
+ def test_dmp_sqr():
703
+ assert dmp_sqr([ZZ(1), ZZ(2)], 0, ZZ) == \
704
+ dup_sqr([ZZ(1), ZZ(2)], ZZ)
705
+
706
+ assert dmp_sqr([[[]]], 2, ZZ) == [[[]]]
707
+ assert dmp_sqr([[[ZZ(2)]]], 2, ZZ) == [[[ZZ(4)]]]
708
+
709
+ assert dmp_sqr([[[]]], 2, QQ) == [[[]]]
710
+ assert dmp_sqr([[[QQ(2, 3)]]], 2, QQ) == [[[QQ(4, 9)]]]
711
+
712
+ K = FF(9)
713
+
714
+ assert dmp_sqr([[K(3)], [K(4)]], 1, K) == [[K(6)], [K(7)]]
715
+
716
+
717
+ def test_dup_pow():
718
+ assert dup_pow([], 0, ZZ) == [ZZ(1)]
719
+ assert dup_pow([], 0, QQ) == [QQ(1)]
720
+
721
+ assert dup_pow([], 1, ZZ) == []
722
+ assert dup_pow([], 7, ZZ) == []
723
+
724
+ assert dup_pow([ZZ(1)], 0, ZZ) == [ZZ(1)]
725
+ assert dup_pow([ZZ(1)], 1, ZZ) == [ZZ(1)]
726
+ assert dup_pow([ZZ(1)], 7, ZZ) == [ZZ(1)]
727
+
728
+ assert dup_pow([ZZ(3)], 0, ZZ) == [ZZ(1)]
729
+ assert dup_pow([ZZ(3)], 1, ZZ) == [ZZ(3)]
730
+ assert dup_pow([ZZ(3)], 7, ZZ) == [ZZ(2187)]
731
+
732
+ assert dup_pow([QQ(1, 1)], 0, QQ) == [QQ(1, 1)]
733
+ assert dup_pow([QQ(1, 1)], 1, QQ) == [QQ(1, 1)]
734
+ assert dup_pow([QQ(1, 1)], 7, QQ) == [QQ(1, 1)]
735
+
736
+ assert dup_pow([QQ(3, 7)], 0, QQ) == [QQ(1, 1)]
737
+ assert dup_pow([QQ(3, 7)], 1, QQ) == [QQ(3, 7)]
738
+ assert dup_pow([QQ(3, 7)], 7, QQ) == [QQ(2187, 823543)]
739
+
740
+ f = dup_normal([2, 0, 0, 1, 7], ZZ)
741
+
742
+ assert dup_pow(f, 0, ZZ) == dup_normal([1], ZZ)
743
+ assert dup_pow(f, 1, ZZ) == dup_normal([2, 0, 0, 1, 7], ZZ)
744
+ assert dup_pow(f, 2, ZZ) == dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
745
+ assert dup_pow(f, 3, ZZ) == dup_normal(
746
+ [8, 0, 0, 12, 84, 0, 6, 84, 294, 1, 21, 147, 343], ZZ)
747
+
748
+
749
+ def test_dmp_pow():
750
+ assert dmp_pow([[]], 0, 1, ZZ) == [[ZZ(1)]]
751
+ assert dmp_pow([[]], 0, 1, QQ) == [[QQ(1)]]
752
+
753
+ assert dmp_pow([[]], 1, 1, ZZ) == [[]]
754
+ assert dmp_pow([[]], 7, 1, ZZ) == [[]]
755
+
756
+ assert dmp_pow([[ZZ(1)]], 0, 1, ZZ) == [[ZZ(1)]]
757
+ assert dmp_pow([[ZZ(1)]], 1, 1, ZZ) == [[ZZ(1)]]
758
+ assert dmp_pow([[ZZ(1)]], 7, 1, ZZ) == [[ZZ(1)]]
759
+
760
+ assert dmp_pow([[QQ(3, 7)]], 0, 1, QQ) == [[QQ(1, 1)]]
761
+ assert dmp_pow([[QQ(3, 7)]], 1, 1, QQ) == [[QQ(3, 7)]]
762
+ assert dmp_pow([[QQ(3, 7)]], 7, 1, QQ) == [[QQ(2187, 823543)]]
763
+
764
+ f = dup_normal([2, 0, 0, 1, 7], ZZ)
765
+
766
+ assert dmp_pow(f, 2, 0, ZZ) == dup_pow(f, 2, ZZ)
767
+
768
+
769
+ def test_dup_pdiv():
770
+ f = dup_normal([3, 1, 1, 5], ZZ)
771
+ g = dup_normal([5, -3, 1], ZZ)
772
+
773
+ q = dup_normal([15, 14], ZZ)
774
+ r = dup_normal([52, 111], ZZ)
775
+
776
+ assert dup_pdiv(f, g, ZZ) == (q, r)
777
+ assert dup_pquo(f, g, ZZ) == q
778
+ assert dup_prem(f, g, ZZ) == r
779
+
780
+ raises(ExactQuotientFailed, lambda: dup_pexquo(f, g, ZZ))
781
+
782
+ f = dup_normal([3, 1, 1, 5], QQ)
783
+ g = dup_normal([5, -3, 1], QQ)
784
+
785
+ q = dup_normal([15, 14], QQ)
786
+ r = dup_normal([52, 111], QQ)
787
+
788
+ assert dup_pdiv(f, g, QQ) == (q, r)
789
+ assert dup_pquo(f, g, QQ) == q
790
+ assert dup_prem(f, g, QQ) == r
791
+
792
+ raises(ExactQuotientFailed, lambda: dup_pexquo(f, g, QQ))
793
+
794
+
795
+ def test_dmp_pdiv():
796
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
797
+ g = dmp_normal([[1], [-1, 0]], 1, ZZ)
798
+
799
+ q = dmp_normal([[1], [1, 0]], 1, ZZ)
800
+ r = dmp_normal([[2, 0, 0]], 1, ZZ)
801
+
802
+ assert dmp_pdiv(f, g, 1, ZZ) == (q, r)
803
+ assert dmp_pquo(f, g, 1, ZZ) == q
804
+ assert dmp_prem(f, g, 1, ZZ) == r
805
+
806
+ raises(ExactQuotientFailed, lambda: dmp_pexquo(f, g, 1, ZZ))
807
+
808
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
809
+ g = dmp_normal([[2], [-2, 0]], 1, ZZ)
810
+
811
+ q = dmp_normal([[2], [2, 0]], 1, ZZ)
812
+ r = dmp_normal([[8, 0, 0]], 1, ZZ)
813
+
814
+ assert dmp_pdiv(f, g, 1, ZZ) == (q, r)
815
+ assert dmp_pquo(f, g, 1, ZZ) == q
816
+ assert dmp_prem(f, g, 1, ZZ) == r
817
+
818
+ raises(ExactQuotientFailed, lambda: dmp_pexquo(f, g, 1, ZZ))
819
+
820
+
821
+ def test_dup_rr_div():
822
+ raises(ZeroDivisionError, lambda: dup_rr_div([1, 2, 3], [], ZZ))
823
+
824
+ f = dup_normal([3, 1, 1, 5], ZZ)
825
+ g = dup_normal([5, -3, 1], ZZ)
826
+
827
+ q, r = [], f
828
+
829
+ assert dup_rr_div(f, g, ZZ) == (q, r)
830
+
831
+
832
+ def test_dmp_rr_div():
833
+ raises(ZeroDivisionError, lambda: dmp_rr_div([[1, 2], [3]], [[]], 1, ZZ))
834
+
835
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
836
+ g = dmp_normal([[1], [-1, 0]], 1, ZZ)
837
+
838
+ q = dmp_normal([[1], [1, 0]], 1, ZZ)
839
+ r = dmp_normal([[2, 0, 0]], 1, ZZ)
840
+
841
+ assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
842
+
843
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
844
+ g = dmp_normal([[-1], [1, 0]], 1, ZZ)
845
+
846
+ q = dmp_normal([[-1], [-1, 0]], 1, ZZ)
847
+ r = dmp_normal([[2, 0, 0]], 1, ZZ)
848
+
849
+ assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
850
+
851
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
852
+ g = dmp_normal([[2], [-2, 0]], 1, ZZ)
853
+
854
+ q, r = [[]], f
855
+
856
+ assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
857
+
858
+
859
+ def test_dup_ff_div():
860
+ raises(ZeroDivisionError, lambda: dup_ff_div([1, 2, 3], [], QQ))
861
+
862
+ f = dup_normal([3, 1, 1, 5], QQ)
863
+ g = dup_normal([5, -3, 1], QQ)
864
+
865
+ q = [QQ(3, 5), QQ(14, 25)]
866
+ r = [QQ(52, 25), QQ(111, 25)]
867
+
868
+ assert dup_ff_div(f, g, QQ) == (q, r)
869
+
870
+ def test_dup_ff_div_gmpy2():
871
+ if GROUND_TYPES != 'gmpy2':
872
+ return
873
+
874
+ from gmpy2 import mpq
875
+ from sympy.polys.domains import GMPYRationalField
876
+ K = GMPYRationalField()
877
+
878
+ f = [mpq(1,3), mpq(3,2)]
879
+ g = [mpq(2,1)]
880
+ assert dmp_ff_div(f, g, 0, K) == ([mpq(1,6), mpq(3,4)], [])
881
+
882
+ f = [mpq(1,2), mpq(1,3), mpq(1,4), mpq(1,5)]
883
+ g = [mpq(-1,1), mpq(1,1), mpq(-1,1)]
884
+ assert dmp_ff_div(f, g, 0, K) == ([mpq(-1,2), mpq(-5,6)], [mpq(7,12), mpq(-19,30)])
885
+
886
+ def test_dmp_ff_div():
887
+ raises(ZeroDivisionError, lambda: dmp_ff_div([[1, 2], [3]], [[]], 1, QQ))
888
+
889
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
890
+ g = dmp_normal([[1], [-1, 0]], 1, QQ)
891
+
892
+ q = [[QQ(1, 1)], [QQ(1, 1), QQ(0, 1)]]
893
+ r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
894
+
895
+ assert dmp_ff_div(f, g, 1, QQ) == (q, r)
896
+
897
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
898
+ g = dmp_normal([[-1], [1, 0]], 1, QQ)
899
+
900
+ q = [[QQ(-1, 1)], [QQ(-1, 1), QQ(0, 1)]]
901
+ r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
902
+
903
+ assert dmp_ff_div(f, g, 1, QQ) == (q, r)
904
+
905
+ f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
906
+ g = dmp_normal([[2], [-2, 0]], 1, QQ)
907
+
908
+ q = [[QQ(1, 2)], [QQ(1, 2), QQ(0, 1)]]
909
+ r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
910
+
911
+ assert dmp_ff_div(f, g, 1, QQ) == (q, r)
912
+
913
+
914
+ def test_dup_div():
915
+ f, g, q, r = [5, 4, 3, 2, 1], [1, 2, 3], [5, -6, 0], [20, 1]
916
+
917
+ assert dup_div(f, g, ZZ) == (q, r)
918
+ assert dup_quo(f, g, ZZ) == q
919
+ assert dup_rem(f, g, ZZ) == r
920
+
921
+ raises(ExactQuotientFailed, lambda: dup_exquo(f, g, ZZ))
922
+
923
+ f, g, q, r = [5, 4, 3, 2, 1, 0], [1, 2, 0, 0, 9], [5, -6], [15, 2, -44, 54]
924
+
925
+ assert dup_div(f, g, ZZ) == (q, r)
926
+ assert dup_quo(f, g, ZZ) == q
927
+ assert dup_rem(f, g, ZZ) == r
928
+
929
+ raises(ExactQuotientFailed, lambda: dup_exquo(f, g, ZZ))
930
+
931
+
932
+ def test_dmp_div():
933
+ f, g, q, r = [5, 4, 3, 2, 1], [1, 2, 3], [5, -6, 0], [20, 1]
934
+
935
+ assert dmp_div(f, g, 0, ZZ) == (q, r)
936
+ assert dmp_quo(f, g, 0, ZZ) == q
937
+ assert dmp_rem(f, g, 0, ZZ) == r
938
+
939
+ raises(ExactQuotientFailed, lambda: dmp_exquo(f, g, 0, ZZ))
940
+
941
+ f, g, q, r = [[[1]]], [[[2]], [1]], [[[]]], [[[1]]]
942
+
943
+ assert dmp_div(f, g, 2, ZZ) == (q, r)
944
+ assert dmp_quo(f, g, 2, ZZ) == q
945
+ assert dmp_rem(f, g, 2, ZZ) == r
946
+
947
+ raises(ExactQuotientFailed, lambda: dmp_exquo(f, g, 2, ZZ))
948
+
949
+
950
+ def test_dup_max_norm():
951
+ assert dup_max_norm([], ZZ) == 0
952
+ assert dup_max_norm([1], ZZ) == 1
953
+
954
+ assert dup_max_norm([1, 4, 2, 3], ZZ) == 4
955
+
956
+
957
+ def test_dmp_max_norm():
958
+ assert dmp_max_norm([[[]]], 2, ZZ) == 0
959
+ assert dmp_max_norm([[[1]]], 2, ZZ) == 1
960
+
961
+ assert dmp_max_norm(f_0, 2, ZZ) == 6
962
+
963
+
964
+ def test_dup_l1_norm():
965
+ assert dup_l1_norm([], ZZ) == 0
966
+ assert dup_l1_norm([1], ZZ) == 1
967
+ assert dup_l1_norm([1, 4, 2, 3], ZZ) == 10
968
+
969
+
970
+ def test_dmp_l1_norm():
971
+ assert dmp_l1_norm([[[]]], 2, ZZ) == 0
972
+ assert dmp_l1_norm([[[1]]], 2, ZZ) == 1
973
+
974
+ assert dmp_l1_norm(f_0, 2, ZZ) == 31
975
+
976
+
977
+ def test_dup_l2_norm_squared():
978
+ assert dup_l2_norm_squared([], ZZ) == 0
979
+ assert dup_l2_norm_squared([1], ZZ) == 1
980
+ assert dup_l2_norm_squared([1, 4, 2, 3], ZZ) == 30
981
+
982
+
983
+ def test_dmp_l2_norm_squared():
984
+ assert dmp_l2_norm_squared([[[]]], 2, ZZ) == 0
985
+ assert dmp_l2_norm_squared([[[1]]], 2, ZZ) == 1
986
+ assert dmp_l2_norm_squared(f_0, 2, ZZ) == 111
987
+
988
+
989
+ def test_dup_expand():
990
+ assert dup_expand((), ZZ) == [1]
991
+ assert dup_expand(([1, 2, 3], [1, 2], [7, 5, 4, 3]), ZZ) == \
992
+ dup_mul([1, 2, 3], dup_mul([1, 2], [7, 5, 4, 3], ZZ), ZZ)
993
+
994
+
995
+ def test_dmp_expand():
996
+ assert dmp_expand((), 1, ZZ) == [[1]]
997
+ assert dmp_expand(([[1], [2], [3]], [[1], [2]], [[7], [5], [4], [3]]), 1, ZZ) == \
998
+ dmp_mul([[1], [2], [3]], dmp_mul([[1], [2]], [[7], [5], [
999
+ 4], [3]], 1, ZZ), 1, ZZ)
1000
+
1001
+ def test_dup_mul_poly():
1002
+ p = Poly(18786186952704.0*x**165 + 9.31746684052255e+31*x**82, x, domain='RR')
1003
+ px = Poly(18786186952704.0*x**166 + 9.31746684052255e+31*x**83, x, domain='RR')
1004
+
1005
+ assert p * x == px
1006
+ assert p.set_domain(QQ) * x == px.set_domain(QQ)
1007
+ assert p.set_domain(CC) * x == px.set_domain(CC)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_densebasic.py ADDED
@@ -0,0 +1,730 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for dense recursive polynomials' basic tools. """
2
+
3
+ from sympy.polys.densebasic import (
4
+ ninf,
5
+ dup_LC, dmp_LC,
6
+ dup_TC, dmp_TC,
7
+ dmp_ground_LC, dmp_ground_TC,
8
+ dmp_true_LT,
9
+ dup_degree, dmp_degree,
10
+ dmp_degree_in, dmp_degree_list,
11
+ dup_strip, dmp_strip,
12
+ dmp_validate,
13
+ dup_reverse,
14
+ dup_copy, dmp_copy,
15
+ dup_normal, dmp_normal,
16
+ dup_convert, dmp_convert,
17
+ dup_from_sympy, dmp_from_sympy,
18
+ dup_nth, dmp_nth, dmp_ground_nth,
19
+ dmp_zero_p, dmp_zero,
20
+ dmp_one_p, dmp_one,
21
+ dmp_ground_p, dmp_ground,
22
+ dmp_negative_p, dmp_positive_p,
23
+ dmp_zeros, dmp_grounds,
24
+ dup_from_dict, dup_from_raw_dict,
25
+ dup_to_dict, dup_to_raw_dict,
26
+ dmp_from_dict, dmp_to_dict,
27
+ dmp_swap, dmp_permute,
28
+ dmp_nest, dmp_raise,
29
+ dup_deflate, dmp_deflate,
30
+ dup_multi_deflate, dmp_multi_deflate,
31
+ dup_inflate, dmp_inflate,
32
+ dmp_exclude, dmp_include,
33
+ dmp_inject, dmp_eject,
34
+ dup_terms_gcd, dmp_terms_gcd,
35
+ dmp_list_terms, dmp_apply_pairs,
36
+ dup_slice,
37
+ dup_random,
38
+ )
39
+
40
+ from sympy.polys.specialpolys import f_polys
41
+ from sympy.polys.domains import ZZ, QQ
42
+ from sympy.polys.rings import ring
43
+
44
+ from sympy.core.singleton import S
45
+ from sympy.testing.pytest import raises
46
+
47
+ from sympy.core.numbers import oo
48
+
49
+ f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
50
+
51
+ def test_dup_LC():
52
+ assert dup_LC([], ZZ) == 0
53
+ assert dup_LC([2, 3, 4, 5], ZZ) == 2
54
+
55
+
56
+ def test_dup_TC():
57
+ assert dup_TC([], ZZ) == 0
58
+ assert dup_TC([2, 3, 4, 5], ZZ) == 5
59
+
60
+
61
+ def test_dmp_LC():
62
+ assert dmp_LC([[]], ZZ) == []
63
+ assert dmp_LC([[2, 3, 4], [5]], ZZ) == [2, 3, 4]
64
+ assert dmp_LC([[[]]], ZZ) == [[]]
65
+ assert dmp_LC([[[2], [3, 4]], [[5]]], ZZ) == [[2], [3, 4]]
66
+
67
+
68
+ def test_dmp_TC():
69
+ assert dmp_TC([[]], ZZ) == []
70
+ assert dmp_TC([[2, 3, 4], [5]], ZZ) == [5]
71
+ assert dmp_TC([[[]]], ZZ) == [[]]
72
+ assert dmp_TC([[[2], [3, 4]], [[5]]], ZZ) == [[5]]
73
+
74
+
75
+ def test_dmp_ground_LC():
76
+ assert dmp_ground_LC([[]], 1, ZZ) == 0
77
+ assert dmp_ground_LC([[2, 3, 4], [5]], 1, ZZ) == 2
78
+ assert dmp_ground_LC([[[]]], 2, ZZ) == 0
79
+ assert dmp_ground_LC([[[2], [3, 4]], [[5]]], 2, ZZ) == 2
80
+
81
+
82
+ def test_dmp_ground_TC():
83
+ assert dmp_ground_TC([[]], 1, ZZ) == 0
84
+ assert dmp_ground_TC([[2, 3, 4], [5]], 1, ZZ) == 5
85
+ assert dmp_ground_TC([[[]]], 2, ZZ) == 0
86
+ assert dmp_ground_TC([[[2], [3, 4]], [[5]]], 2, ZZ) == 5
87
+
88
+
89
+ def test_dmp_true_LT():
90
+ assert dmp_true_LT([[]], 1, ZZ) == ((0, 0), 0)
91
+ assert dmp_true_LT([[7]], 1, ZZ) == ((0, 0), 7)
92
+
93
+ assert dmp_true_LT([[1, 0]], 1, ZZ) == ((0, 1), 1)
94
+ assert dmp_true_LT([[1], []], 1, ZZ) == ((1, 0), 1)
95
+ assert dmp_true_LT([[1, 0], []], 1, ZZ) == ((1, 1), 1)
96
+
97
+
98
+ def test_dup_degree():
99
+ assert ninf == float('-inf')
100
+ assert dup_degree([]) is ninf
101
+ assert dup_degree([1]) == 0
102
+ assert dup_degree([1, 0]) == 1
103
+ assert dup_degree([1, 0, 0, 0, 1]) == 4
104
+
105
+
106
+ def test_dmp_degree():
107
+ assert dmp_degree([[]], 1) is ninf
108
+ assert dmp_degree([[[]]], 2) is ninf
109
+
110
+ assert dmp_degree([[1]], 1) == 0
111
+ assert dmp_degree([[2], [1]], 1) == 1
112
+
113
+
114
+ def test_dmp_degree_in():
115
+ assert dmp_degree_in([[[]]], 0, 2) is ninf
116
+ assert dmp_degree_in([[[]]], 1, 2) is ninf
117
+ assert dmp_degree_in([[[]]], 2, 2) is ninf
118
+
119
+ assert dmp_degree_in([[[1]]], 0, 2) == 0
120
+ assert dmp_degree_in([[[1]]], 1, 2) == 0
121
+ assert dmp_degree_in([[[1]]], 2, 2) == 0
122
+
123
+ assert dmp_degree_in(f_4, 0, 2) == 9
124
+ assert dmp_degree_in(f_4, 1, 2) == 12
125
+ assert dmp_degree_in(f_4, 2, 2) == 8
126
+
127
+ assert dmp_degree_in(f_6, 0, 2) == 4
128
+ assert dmp_degree_in(f_6, 1, 2) == 4
129
+ assert dmp_degree_in(f_6, 2, 2) == 6
130
+ assert dmp_degree_in(f_6, 3, 3) == 3
131
+
132
+ raises(IndexError, lambda: dmp_degree_in([[1]], -5, 1))
133
+
134
+
135
+ def test_dmp_degree_list():
136
+ assert dmp_degree_list([[[[ ]]]], 3) == (-oo, -oo, -oo, -oo)
137
+ assert dmp_degree_list([[[[1]]]], 3) == ( 0, 0, 0, 0)
138
+
139
+ assert dmp_degree_list(f_0, 2) == (2, 2, 2)
140
+ assert dmp_degree_list(f_1, 2) == (3, 3, 3)
141
+ assert dmp_degree_list(f_2, 2) == (5, 3, 3)
142
+ assert dmp_degree_list(f_3, 2) == (5, 4, 7)
143
+ assert dmp_degree_list(f_4, 2) == (9, 12, 8)
144
+ assert dmp_degree_list(f_5, 2) == (3, 3, 3)
145
+ assert dmp_degree_list(f_6, 3) == (4, 4, 6, 3)
146
+
147
+
148
+ def test_dup_strip():
149
+ assert dup_strip([]) == []
150
+ assert dup_strip([0]) == []
151
+ assert dup_strip([0, 0, 0]) == []
152
+
153
+ assert dup_strip([1]) == [1]
154
+ assert dup_strip([0, 1]) == [1]
155
+ assert dup_strip([0, 0, 0, 1]) == [1]
156
+
157
+ assert dup_strip([1, 2, 0]) == [1, 2, 0]
158
+ assert dup_strip([0, 1, 2, 0]) == [1, 2, 0]
159
+ assert dup_strip([0, 0, 0, 1, 2, 0]) == [1, 2, 0]
160
+
161
+
162
+ def test_dmp_strip():
163
+ assert dmp_strip([0, 1, 0], 0) == [1, 0]
164
+
165
+ assert dmp_strip([[]], 1) == [[]]
166
+ assert dmp_strip([[], []], 1) == [[]]
167
+ assert dmp_strip([[], [], []], 1) == [[]]
168
+
169
+ assert dmp_strip([[[]]], 2) == [[[]]]
170
+ assert dmp_strip([[[]], [[]]], 2) == [[[]]]
171
+ assert dmp_strip([[[]], [[]], [[]]], 2) == [[[]]]
172
+
173
+ assert dmp_strip([[[1]]], 2) == [[[1]]]
174
+ assert dmp_strip([[[]], [[1]]], 2) == [[[1]]]
175
+ assert dmp_strip([[[]], [[1]], [[]]], 2) == [[[1]], [[]]]
176
+
177
+
178
+ def test_dmp_validate():
179
+ assert dmp_validate([]) == ([], 0)
180
+ assert dmp_validate([0, 0, 0, 1, 0]) == ([1, 0], 0)
181
+
182
+ assert dmp_validate([[[]]]) == ([[[]]], 2)
183
+ assert dmp_validate([[0], [], [0], [1], [0]]) == ([[1], []], 1)
184
+
185
+ raises(ValueError, lambda: dmp_validate([[0], 0, [0], [1], [0]]))
186
+
187
+
188
+ def test_dup_reverse():
189
+ assert dup_reverse([1, 2, 0, 3]) == [3, 0, 2, 1]
190
+ assert dup_reverse([1, 2, 3, 0]) == [3, 2, 1]
191
+
192
+
193
+ def test_dup_copy():
194
+ f = [ZZ(1), ZZ(0), ZZ(2)]
195
+ g = dup_copy(f)
196
+
197
+ g[0], g[2] = ZZ(7), ZZ(0)
198
+
199
+ assert f != g
200
+
201
+
202
+ def test_dmp_copy():
203
+ f = [[ZZ(1)], [ZZ(2), ZZ(0)]]
204
+ g = dmp_copy(f, 1)
205
+
206
+ g[0][0], g[1][1] = ZZ(7), ZZ(1)
207
+
208
+ assert f != g
209
+
210
+
211
+ def test_dup_normal():
212
+ assert dup_normal([0, 0, 2, 1, 0, 11, 0], ZZ) == \
213
+ [ZZ(2), ZZ(1), ZZ(0), ZZ(11), ZZ(0)]
214
+
215
+
216
+ def test_dmp_normal():
217
+ assert dmp_normal([[0], [], [0, 2, 1], [0], [11], []], 1, ZZ) == \
218
+ [[ZZ(2), ZZ(1)], [], [ZZ(11)], []]
219
+
220
+
221
+ def test_dup_convert():
222
+ K0, K1 = ZZ['x'], ZZ
223
+
224
+ f = [K0(1), K0(2), K0(0), K0(3)]
225
+
226
+ assert dup_convert(f, K0, K1) == \
227
+ [ZZ(1), ZZ(2), ZZ(0), ZZ(3)]
228
+
229
+
230
+ def test_dmp_convert():
231
+ K0, K1 = ZZ['x'], ZZ
232
+
233
+ f = [[K0(1)], [K0(2)], [], [K0(3)]]
234
+
235
+ assert dmp_convert(f, 1, K0, K1) == \
236
+ [[ZZ(1)], [ZZ(2)], [], [ZZ(3)]]
237
+
238
+
239
+ def test_dup_from_sympy():
240
+ assert dup_from_sympy([S.One, S(2)], ZZ) == \
241
+ [ZZ(1), ZZ(2)]
242
+ assert dup_from_sympy([S.Half, S(3)], QQ) == \
243
+ [QQ(1, 2), QQ(3, 1)]
244
+
245
+
246
+ def test_dmp_from_sympy():
247
+ assert dmp_from_sympy([[S.One, S(2)], [S.Zero]], 1, ZZ) == \
248
+ [[ZZ(1), ZZ(2)], []]
249
+ assert dmp_from_sympy([[S.Half, S(2)]], 1, QQ) == \
250
+ [[QQ(1, 2), QQ(2, 1)]]
251
+
252
+
253
+ def test_dup_nth():
254
+ assert dup_nth([1, 2, 3], 0, ZZ) == 3
255
+ assert dup_nth([1, 2, 3], 1, ZZ) == 2
256
+ assert dup_nth([1, 2, 3], 2, ZZ) == 1
257
+
258
+ assert dup_nth([1, 2, 3], 9, ZZ) == 0
259
+
260
+ raises(IndexError, lambda: dup_nth([3, 4, 5], -1, ZZ))
261
+
262
+
263
+ def test_dmp_nth():
264
+ assert dmp_nth([[1], [2], [3]], 0, 1, ZZ) == [3]
265
+ assert dmp_nth([[1], [2], [3]], 1, 1, ZZ) == [2]
266
+ assert dmp_nth([[1], [2], [3]], 2, 1, ZZ) == [1]
267
+
268
+ assert dmp_nth([[1], [2], [3]], 9, 1, ZZ) == []
269
+
270
+ raises(IndexError, lambda: dmp_nth([[3], [4], [5]], -1, 1, ZZ))
271
+
272
+
273
+ def test_dmp_ground_nth():
274
+ assert dmp_ground_nth([[]], (0, 0), 1, ZZ) == 0
275
+ assert dmp_ground_nth([[1], [2], [3]], (0, 0), 1, ZZ) == 3
276
+ assert dmp_ground_nth([[1], [2], [3]], (1, 0), 1, ZZ) == 2
277
+ assert dmp_ground_nth([[1], [2], [3]], (2, 0), 1, ZZ) == 1
278
+
279
+ assert dmp_ground_nth([[1], [2], [3]], (2, 1), 1, ZZ) == 0
280
+ assert dmp_ground_nth([[1], [2], [3]], (3, 0), 1, ZZ) == 0
281
+
282
+ raises(IndexError, lambda: dmp_ground_nth([[3], [4], [5]], (2, -1), 1, ZZ))
283
+
284
+
285
+ def test_dmp_zero_p():
286
+ assert dmp_zero_p([], 0) is True
287
+ assert dmp_zero_p([[]], 1) is True
288
+
289
+ assert dmp_zero_p([[[]]], 2) is True
290
+ assert dmp_zero_p([[[1]]], 2) is False
291
+
292
+
293
+ def test_dmp_zero():
294
+ assert dmp_zero(0) == []
295
+ assert dmp_zero(2) == [[[]]]
296
+
297
+
298
+ def test_dmp_one_p():
299
+ assert dmp_one_p([1], 0, ZZ) is True
300
+ assert dmp_one_p([[1]], 1, ZZ) is True
301
+ assert dmp_one_p([[[1]]], 2, ZZ) is True
302
+ assert dmp_one_p([[[12]]], 2, ZZ) is False
303
+
304
+
305
+ def test_dmp_one():
306
+ assert dmp_one(0, ZZ) == [ZZ(1)]
307
+ assert dmp_one(2, ZZ) == [[[ZZ(1)]]]
308
+
309
+
310
+ def test_dmp_ground_p():
311
+ assert dmp_ground_p([], 0, 0) is True
312
+ assert dmp_ground_p([[]], 0, 1) is True
313
+ assert dmp_ground_p([[]], 1, 1) is False
314
+
315
+ assert dmp_ground_p([[ZZ(1)]], 1, 1) is True
316
+ assert dmp_ground_p([[[ZZ(2)]]], 2, 2) is True
317
+
318
+ assert dmp_ground_p([[[ZZ(2)]]], 3, 2) is False
319
+ assert dmp_ground_p([[[ZZ(3)], []]], 3, 2) is False
320
+
321
+ assert dmp_ground_p([], None, 0) is True
322
+ assert dmp_ground_p([[]], None, 1) is True
323
+
324
+ assert dmp_ground_p([ZZ(1)], None, 0) is True
325
+ assert dmp_ground_p([[[ZZ(1)]]], None, 2) is True
326
+
327
+ assert dmp_ground_p([[[ZZ(3)], []]], None, 2) is False
328
+
329
+
330
+ def test_dmp_ground():
331
+ assert dmp_ground(ZZ(0), 2) == [[[]]]
332
+
333
+ assert dmp_ground(ZZ(7), -1) == ZZ(7)
334
+ assert dmp_ground(ZZ(7), 0) == [ZZ(7)]
335
+ assert dmp_ground(ZZ(7), 2) == [[[ZZ(7)]]]
336
+
337
+
338
+ def test_dmp_zeros():
339
+ assert dmp_zeros(4, 0, ZZ) == [[], [], [], []]
340
+
341
+ assert dmp_zeros(0, 2, ZZ) == []
342
+ assert dmp_zeros(1, 2, ZZ) == [[[[]]]]
343
+ assert dmp_zeros(2, 2, ZZ) == [[[[]]], [[[]]]]
344
+ assert dmp_zeros(3, 2, ZZ) == [[[[]]], [[[]]], [[[]]]]
345
+
346
+ assert dmp_zeros(3, -1, ZZ) == [0, 0, 0]
347
+
348
+
349
+ def test_dmp_grounds():
350
+ assert dmp_grounds(ZZ(7), 0, 2) == []
351
+
352
+ assert dmp_grounds(ZZ(7), 1, 2) == [[[[7]]]]
353
+ assert dmp_grounds(ZZ(7), 2, 2) == [[[[7]]], [[[7]]]]
354
+ assert dmp_grounds(ZZ(7), 3, 2) == [[[[7]]], [[[7]]], [[[7]]]]
355
+
356
+ assert dmp_grounds(ZZ(7), 3, -1) == [7, 7, 7]
357
+
358
+
359
+ def test_dmp_negative_p():
360
+ assert dmp_negative_p([[[]]], 2, ZZ) is False
361
+ assert dmp_negative_p([[[1], [2]]], 2, ZZ) is False
362
+ assert dmp_negative_p([[[-1], [2]]], 2, ZZ) is True
363
+
364
+
365
+ def test_dmp_positive_p():
366
+ assert dmp_positive_p([[[]]], 2, ZZ) is False
367
+ assert dmp_positive_p([[[1], [2]]], 2, ZZ) is True
368
+ assert dmp_positive_p([[[-1], [2]]], 2, ZZ) is False
369
+
370
+
371
+ def test_dup_from_to_dict():
372
+ assert dup_from_raw_dict({}, ZZ) == []
373
+ assert dup_from_dict({}, ZZ) == []
374
+
375
+ assert dup_to_raw_dict([]) == {}
376
+ assert dup_to_dict([]) == {}
377
+
378
+ assert dup_to_raw_dict([], ZZ, zero=True) == {0: ZZ(0)}
379
+ assert dup_to_dict([], ZZ, zero=True) == {(0,): ZZ(0)}
380
+
381
+ f = [3, 0, 0, 2, 0, 0, 0, 0, 8]
382
+ g = {8: 3, 5: 2, 0: 8}
383
+ h = {(8,): 3, (5,): 2, (0,): 8}
384
+
385
+ assert dup_from_raw_dict(g, ZZ) == f
386
+ assert dup_from_dict(h, ZZ) == f
387
+
388
+ assert dup_to_raw_dict(f) == g
389
+ assert dup_to_dict(f) == h
390
+
391
+ R, x,y = ring("x,y", ZZ)
392
+ K = R.to_domain()
393
+
394
+ f = [R(3), R(0), R(2), R(0), R(0), R(8)]
395
+ g = {5: R(3), 3: R(2), 0: R(8)}
396
+ h = {(5,): R(3), (3,): R(2), (0,): R(8)}
397
+
398
+ assert dup_from_raw_dict(g, K) == f
399
+ assert dup_from_dict(h, K) == f
400
+
401
+ assert dup_to_raw_dict(f) == g
402
+ assert dup_to_dict(f) == h
403
+
404
+
405
+ def test_dmp_from_to_dict():
406
+ assert dmp_from_dict({}, 1, ZZ) == [[]]
407
+ assert dmp_to_dict([[]], 1) == {}
408
+
409
+ assert dmp_to_dict([], 0, ZZ, zero=True) == {(0,): ZZ(0)}
410
+ assert dmp_to_dict([[]], 1, ZZ, zero=True) == {(0, 0): ZZ(0)}
411
+
412
+ f = [[3], [], [], [2], [], [], [], [], [8]]
413
+ g = {(8, 0): 3, (5, 0): 2, (0, 0): 8}
414
+
415
+ assert dmp_from_dict(g, 1, ZZ) == f
416
+ assert dmp_to_dict(f, 1) == g
417
+
418
+
419
+ def test_dmp_swap():
420
+ f = dmp_normal([[1, 0, 0], [], [1, 0], [], [1]], 1, ZZ)
421
+ g = dmp_normal([[1, 0, 0, 0, 0], [1, 0, 0], [1]], 1, ZZ)
422
+
423
+ assert dmp_swap(f, 1, 1, 1, ZZ) == f
424
+
425
+ assert dmp_swap(f, 0, 1, 1, ZZ) == g
426
+ assert dmp_swap(g, 0, 1, 1, ZZ) == f
427
+
428
+ raises(IndexError, lambda: dmp_swap(f, -1, -7, 1, ZZ))
429
+
430
+
431
+ def test_dmp_permute():
432
+ f = dmp_normal([[1, 0, 0], [], [1, 0], [], [1]], 1, ZZ)
433
+ g = dmp_normal([[1, 0, 0, 0, 0], [1, 0, 0], [1]], 1, ZZ)
434
+
435
+ assert dmp_permute(f, [0, 1], 1, ZZ) == f
436
+ assert dmp_permute(g, [0, 1], 1, ZZ) == g
437
+
438
+ assert dmp_permute(f, [1, 0], 1, ZZ) == g
439
+ assert dmp_permute(g, [1, 0], 1, ZZ) == f
440
+
441
+
442
+ def test_dmp_nest():
443
+ assert dmp_nest(ZZ(1), 2, ZZ) == [[[1]]]
444
+
445
+ assert dmp_nest([[1]], 0, ZZ) == [[1]]
446
+ assert dmp_nest([[1]], 1, ZZ) == [[[1]]]
447
+ assert dmp_nest([[1]], 2, ZZ) == [[[[1]]]]
448
+
449
+
450
+ def test_dmp_raise():
451
+ assert dmp_raise([], 2, 0, ZZ) == [[[]]]
452
+ assert dmp_raise([[1]], 0, 1, ZZ) == [[1]]
453
+
454
+ assert dmp_raise([[1, 2, 3], [], [2, 3]], 2, 1, ZZ) == \
455
+ [[[[1]], [[2]], [[3]]], [[[]]], [[[2]], [[3]]]]
456
+
457
+
458
+ def test_dup_deflate():
459
+ assert dup_deflate([], ZZ) == (1, [])
460
+ assert dup_deflate([2], ZZ) == (1, [2])
461
+ assert dup_deflate([1, 2, 3], ZZ) == (1, [1, 2, 3])
462
+ assert dup_deflate([1, 0, 2, 0, 3], ZZ) == (2, [1, 2, 3])
463
+
464
+ assert dup_deflate(dup_from_raw_dict({7: 1, 1: 1}, ZZ), ZZ) == \
465
+ (1, [1, 0, 0, 0, 0, 0, 1, 0])
466
+ assert dup_deflate(dup_from_raw_dict({7: 1, 0: 1}, ZZ), ZZ) == \
467
+ (7, [1, 1])
468
+ assert dup_deflate(dup_from_raw_dict({7: 1, 3: 1}, ZZ), ZZ) == \
469
+ (1, [1, 0, 0, 0, 1, 0, 0, 0])
470
+
471
+ assert dup_deflate(dup_from_raw_dict({7: 1, 4: 1}, ZZ), ZZ) == \
472
+ (1, [1, 0, 0, 1, 0, 0, 0, 0])
473
+ assert dup_deflate(dup_from_raw_dict({8: 1, 4: 1}, ZZ), ZZ) == \
474
+ (4, [1, 1, 0])
475
+
476
+ assert dup_deflate(dup_from_raw_dict({8: 1}, ZZ), ZZ) == \
477
+ (8, [1, 0])
478
+ assert dup_deflate(dup_from_raw_dict({7: 1}, ZZ), ZZ) == \
479
+ (7, [1, 0])
480
+ assert dup_deflate(dup_from_raw_dict({1: 1}, ZZ), ZZ) == \
481
+ (1, [1, 0])
482
+
483
+
484
+ def test_dmp_deflate():
485
+ assert dmp_deflate([[]], 1, ZZ) == ((1, 1), [[]])
486
+ assert dmp_deflate([[2]], 1, ZZ) == ((1, 1), [[2]])
487
+
488
+ f = [[1, 0, 0], [], [1, 0], [], [1]]
489
+
490
+ assert dmp_deflate(f, 1, ZZ) == ((2, 1), [[1, 0, 0], [1, 0], [1]])
491
+
492
+
493
+ def test_dup_multi_deflate():
494
+ assert dup_multi_deflate(([2],), ZZ) == (1, ([2],))
495
+ assert dup_multi_deflate(([], []), ZZ) == (1, ([], []))
496
+
497
+ assert dup_multi_deflate(([1, 2, 3],), ZZ) == (1, ([1, 2, 3],))
498
+ assert dup_multi_deflate(([1, 0, 2, 0, 3],), ZZ) == (2, ([1, 2, 3],))
499
+
500
+ assert dup_multi_deflate(([1, 0, 2, 0, 3], [2, 0, 0]), ZZ) == \
501
+ (2, ([1, 2, 3], [2, 0]))
502
+ assert dup_multi_deflate(([1, 0, 2, 0, 3], [2, 1, 0]), ZZ) == \
503
+ (1, ([1, 0, 2, 0, 3], [2, 1, 0]))
504
+
505
+
506
+ def test_dmp_multi_deflate():
507
+ assert dmp_multi_deflate(([[]],), 1, ZZ) == \
508
+ ((1, 1), ([[]],))
509
+ assert dmp_multi_deflate(([[]], [[]]), 1, ZZ) == \
510
+ ((1, 1), ([[]], [[]]))
511
+
512
+ assert dmp_multi_deflate(([[1]], [[]]), 1, ZZ) == \
513
+ ((1, 1), ([[1]], [[]]))
514
+ assert dmp_multi_deflate(([[1]], [[2]]), 1, ZZ) == \
515
+ ((1, 1), ([[1]], [[2]]))
516
+ assert dmp_multi_deflate(([[1]], [[2, 0]]), 1, ZZ) == \
517
+ ((1, 1), ([[1]], [[2, 0]]))
518
+
519
+ assert dmp_multi_deflate(([[2, 0]], [[2, 0]]), 1, ZZ) == \
520
+ ((1, 1), ([[2, 0]], [[2, 0]]))
521
+
522
+ assert dmp_multi_deflate(
523
+ ([[2]], [[2, 0, 0]]), 1, ZZ) == ((1, 2), ([[2]], [[2, 0]]))
524
+ assert dmp_multi_deflate(
525
+ ([[2, 0, 0]], [[2, 0, 0]]), 1, ZZ) == ((1, 2), ([[2, 0]], [[2, 0]]))
526
+
527
+ assert dmp_multi_deflate(([2, 0, 0], [1, 0, 4, 0, 1]), 0, ZZ) == \
528
+ ((2,), ([2, 0], [1, 4, 1]))
529
+
530
+ f = [[1, 0, 0], [], [1, 0], [], [1]]
531
+ g = [[1, 0, 1, 0], [], [1]]
532
+
533
+ assert dmp_multi_deflate((f,), 1, ZZ) == \
534
+ ((2, 1), ([[1, 0, 0], [1, 0], [1]],))
535
+
536
+ assert dmp_multi_deflate((f, g), 1, ZZ) == \
537
+ ((2, 1), ([[1, 0, 0], [1, 0], [1]],
538
+ [[1, 0, 1, 0], [1]]))
539
+
540
+
541
+ def test_dup_inflate():
542
+ assert dup_inflate([], 17, ZZ) == []
543
+
544
+ assert dup_inflate([1, 2, 3], 1, ZZ) == [1, 2, 3]
545
+ assert dup_inflate([1, 2, 3], 2, ZZ) == [1, 0, 2, 0, 3]
546
+ assert dup_inflate([1, 2, 3], 3, ZZ) == [1, 0, 0, 2, 0, 0, 3]
547
+ assert dup_inflate([1, 2, 3], 4, ZZ) == [1, 0, 0, 0, 2, 0, 0, 0, 3]
548
+
549
+ raises(IndexError, lambda: dup_inflate([1, 2, 3], 0, ZZ))
550
+
551
+
552
+ def test_dmp_inflate():
553
+ assert dmp_inflate([1], (3,), 0, ZZ) == [1]
554
+
555
+ assert dmp_inflate([[]], (3, 7), 1, ZZ) == [[]]
556
+ assert dmp_inflate([[2]], (1, 2), 1, ZZ) == [[2]]
557
+
558
+ assert dmp_inflate([[2, 0]], (1, 1), 1, ZZ) == [[2, 0]]
559
+ assert dmp_inflate([[2, 0]], (1, 2), 1, ZZ) == [[2, 0, 0]]
560
+ assert dmp_inflate([[2, 0]], (1, 3), 1, ZZ) == [[2, 0, 0, 0]]
561
+
562
+ assert dmp_inflate([[1, 0, 0], [1], [1, 0]], (2, 1), 1, ZZ) == \
563
+ [[1, 0, 0], [], [1], [], [1, 0]]
564
+
565
+ raises(IndexError, lambda: dmp_inflate([[]], (-3, 7), 1, ZZ))
566
+
567
+
568
+ def test_dmp_exclude():
569
+ assert dmp_exclude([[[]]], 2, ZZ) == ([], [[[]]], 2)
570
+ assert dmp_exclude([[[7]]], 2, ZZ) == ([], [[[7]]], 2)
571
+
572
+ assert dmp_exclude([1, 2, 3], 0, ZZ) == ([], [1, 2, 3], 0)
573
+ assert dmp_exclude([[1], [2, 3]], 1, ZZ) == ([], [[1], [2, 3]], 1)
574
+
575
+ assert dmp_exclude([[1, 2, 3]], 1, ZZ) == ([0], [1, 2, 3], 0)
576
+ assert dmp_exclude([[1], [2], [3]], 1, ZZ) == ([1], [1, 2, 3], 0)
577
+
578
+ assert dmp_exclude([[[1, 2, 3]]], 2, ZZ) == ([0, 1], [1, 2, 3], 0)
579
+ assert dmp_exclude([[[1]], [[2]], [[3]]], 2, ZZ) == ([1, 2], [1, 2, 3], 0)
580
+
581
+
582
+ def test_dmp_include():
583
+ assert dmp_include([1, 2, 3], [], 0, ZZ) == [1, 2, 3]
584
+
585
+ assert dmp_include([1, 2, 3], [0], 0, ZZ) == [[1, 2, 3]]
586
+ assert dmp_include([1, 2, 3], [1], 0, ZZ) == [[1], [2], [3]]
587
+
588
+ assert dmp_include([1, 2, 3], [0, 1], 0, ZZ) == [[[1, 2, 3]]]
589
+ assert dmp_include([1, 2, 3], [1, 2], 0, ZZ) == [[[1]], [[2]], [[3]]]
590
+
591
+
592
+ def test_dmp_inject():
593
+ R, x,y = ring("x,y", ZZ)
594
+ K = R.to_domain()
595
+
596
+ assert dmp_inject([], 0, K) == ([[[]]], 2)
597
+ assert dmp_inject([[]], 1, K) == ([[[[]]]], 3)
598
+
599
+ assert dmp_inject([R(1)], 0, K) == ([[[1]]], 2)
600
+ assert dmp_inject([[R(1)]], 1, K) == ([[[[1]]]], 3)
601
+
602
+ assert dmp_inject([R(1), 2*x + 3*y + 4], 0, K) == ([[[1]], [[2], [3, 4]]], 2)
603
+
604
+ f = [3*x**2 + 7*x*y + 5*y**2, 2*x, R(0), x*y**2 + 11]
605
+ g = [[[3], [7, 0], [5, 0, 0]], [[2], []], [[]], [[1, 0, 0], [11]]]
606
+
607
+ assert dmp_inject(f, 0, K) == (g, 2)
608
+
609
+
610
+ def test_dmp_eject():
611
+ R, x,y = ring("x,y", ZZ)
612
+ K = R.to_domain()
613
+
614
+ assert dmp_eject([[[]]], 2, K) == []
615
+ assert dmp_eject([[[[]]]], 3, K) == [[]]
616
+
617
+ assert dmp_eject([[[1]]], 2, K) == [R(1)]
618
+ assert dmp_eject([[[[1]]]], 3, K) == [[R(1)]]
619
+
620
+ assert dmp_eject([[[1]], [[2], [3, 4]]], 2, K) == [R(1), 2*x + 3*y + 4]
621
+
622
+ f = [3*x**2 + 7*x*y + 5*y**2, 2*x, R(0), x*y**2 + 11]
623
+ g = [[[3], [7, 0], [5, 0, 0]], [[2], []], [[]], [[1, 0, 0], [11]]]
624
+
625
+ assert dmp_eject(g, 2, K) == f
626
+
627
+
628
+ def test_dup_terms_gcd():
629
+ assert dup_terms_gcd([], ZZ) == (0, [])
630
+ assert dup_terms_gcd([1, 0, 1], ZZ) == (0, [1, 0, 1])
631
+ assert dup_terms_gcd([1, 0, 1, 0], ZZ) == (1, [1, 0, 1])
632
+
633
+
634
+ def test_dmp_terms_gcd():
635
+ assert dmp_terms_gcd([[]], 1, ZZ) == ((0, 0), [[]])
636
+
637
+ assert dmp_terms_gcd([1, 0, 1, 0], 0, ZZ) == ((1,), [1, 0, 1])
638
+ assert dmp_terms_gcd([[1], [], [1], []], 1, ZZ) == ((1, 0), [[1], [], [1]])
639
+
640
+ assert dmp_terms_gcd(
641
+ [[1, 0], [], [1]], 1, ZZ) == ((0, 0), [[1, 0], [], [1]])
642
+ assert dmp_terms_gcd(
643
+ [[1, 0], [1, 0, 0], [], []], 1, ZZ) == ((2, 1), [[1], [1, 0]])
644
+
645
+
646
+ def test_dmp_list_terms():
647
+ assert dmp_list_terms([[[]]], 2, ZZ) == [((0, 0, 0), 0)]
648
+ assert dmp_list_terms([[[1]]], 2, ZZ) == [((0, 0, 0), 1)]
649
+
650
+ assert dmp_list_terms([1, 2, 4, 3, 5], 0, ZZ) == \
651
+ [((4,), 1), ((3,), 2), ((2,), 4), ((1,), 3), ((0,), 5)]
652
+
653
+ assert dmp_list_terms([[1], [2, 4], [3, 5, 0]], 1, ZZ) == \
654
+ [((2, 0), 1), ((1, 1), 2), ((1, 0), 4), ((0, 2), 3), ((0, 1), 5)]
655
+
656
+ f = [[2, 0, 0, 0], [1, 0, 0], []]
657
+
658
+ assert dmp_list_terms(f, 1, ZZ, order='lex') == [((2, 3), 2), ((1, 2), 1)]
659
+ assert dmp_list_terms(
660
+ f, 1, ZZ, order='grlex') == [((2, 3), 2), ((1, 2), 1)]
661
+
662
+ f = [[2, 0, 0, 0], [1, 0, 0, 0, 0, 0], []]
663
+
664
+ assert dmp_list_terms(f, 1, ZZ, order='lex') == [((2, 3), 2), ((1, 5), 1)]
665
+ assert dmp_list_terms(
666
+ f, 1, ZZ, order='grlex') == [((1, 5), 1), ((2, 3), 2)]
667
+
668
+
669
+ def test_dmp_apply_pairs():
670
+ h = lambda a, b: a*b
671
+
672
+ assert dmp_apply_pairs([1, 2, 3], [4, 5, 6], h, [], 0, ZZ) == [4, 10, 18]
673
+
674
+ assert dmp_apply_pairs([2, 3], [4, 5, 6], h, [], 0, ZZ) == [10, 18]
675
+ assert dmp_apply_pairs([1, 2, 3], [5, 6], h, [], 0, ZZ) == [10, 18]
676
+
677
+ assert dmp_apply_pairs(
678
+ [[1, 2], [3]], [[4, 5], [6]], h, [], 1, ZZ) == [[4, 10], [18]]
679
+
680
+ assert dmp_apply_pairs(
681
+ [[1, 2], [3]], [[4], [5, 6]], h, [], 1, ZZ) == [[8], [18]]
682
+ assert dmp_apply_pairs(
683
+ [[1], [2, 3]], [[4, 5], [6]], h, [], 1, ZZ) == [[5], [18]]
684
+
685
+
686
+ def test_dup_slice():
687
+ f = [1, 2, 3, 4]
688
+
689
+ assert dup_slice(f, 0, 0, ZZ) == []
690
+ assert dup_slice(f, 0, 1, ZZ) == [4]
691
+ assert dup_slice(f, 0, 2, ZZ) == [3, 4]
692
+ assert dup_slice(f, 0, 3, ZZ) == [2, 3, 4]
693
+ assert dup_slice(f, 0, 4, ZZ) == [1, 2, 3, 4]
694
+
695
+ assert dup_slice(f, 0, 4, ZZ) == f
696
+ assert dup_slice(f, 0, 9, ZZ) == f
697
+
698
+ assert dup_slice(f, 1, 0, ZZ) == []
699
+ assert dup_slice(f, 1, 1, ZZ) == []
700
+ assert dup_slice(f, 1, 2, ZZ) == [3, 0]
701
+ assert dup_slice(f, 1, 3, ZZ) == [2, 3, 0]
702
+ assert dup_slice(f, 1, 4, ZZ) == [1, 2, 3, 0]
703
+
704
+ assert dup_slice([1, 2], 0, 3, ZZ) == [1, 2]
705
+
706
+ g = [1, 0, 0, 2]
707
+
708
+ assert dup_slice(g, 0, 3, ZZ) == [2]
709
+
710
+
711
+ def test_dup_random():
712
+ f = dup_random(0, -10, 10, ZZ)
713
+
714
+ assert dup_degree(f) == 0
715
+ assert all(-10 <= c <= 10 for c in f)
716
+
717
+ f = dup_random(1, -20, 20, ZZ)
718
+
719
+ assert dup_degree(f) == 1
720
+ assert all(-20 <= c <= 20 for c in f)
721
+
722
+ f = dup_random(2, -30, 30, ZZ)
723
+
724
+ assert dup_degree(f) == 2
725
+ assert all(-30 <= c <= 30 for c in f)
726
+
727
+ f = dup_random(3, -40, 40, ZZ)
728
+
729
+ assert dup_degree(f) == 3
730
+ assert all(-40 <= c <= 40 for c in f)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_densetools.py ADDED
@@ -0,0 +1,714 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, dmp_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
+ from sympy.polys.polyclasses import ANP
32
+
33
+ from sympy.polys.polyerrors import (
34
+ MultivariatePolynomialError,
35
+ ExactQuotientFailed,
36
+ NotReversible,
37
+ DomainError,
38
+ )
39
+
40
+ from sympy.polys.specialpolys import f_polys
41
+
42
+ from sympy.polys.domains import FF, ZZ, QQ, ZZ_I, QQ_I, EX, RR
43
+ from sympy.polys.rings import ring
44
+
45
+ from sympy.core.numbers import I
46
+ from sympy.core.singleton import S
47
+ from sympy.functions.elementary.trigonometric import sin
48
+
49
+ from sympy.abc import x
50
+ from sympy.testing.pytest import raises
51
+
52
+ f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
53
+
54
+ def test_dup_integrate():
55
+ assert dup_integrate([], 1, QQ) == []
56
+ assert dup_integrate([], 2, QQ) == []
57
+
58
+ assert dup_integrate([QQ(1)], 1, QQ) == [QQ(1), QQ(0)]
59
+ assert dup_integrate([QQ(1)], 2, QQ) == [QQ(1, 2), QQ(0), QQ(0)]
60
+
61
+ assert dup_integrate([QQ(1), QQ(2), QQ(3)], 0, QQ) == \
62
+ [QQ(1), QQ(2), QQ(3)]
63
+ assert dup_integrate([QQ(1), QQ(2), QQ(3)], 1, QQ) == \
64
+ [QQ(1, 3), QQ(1), QQ(3), QQ(0)]
65
+ assert dup_integrate([QQ(1), QQ(2), QQ(3)], 2, QQ) == \
66
+ [QQ(1, 12), QQ(1, 3), QQ(3, 2), QQ(0), QQ(0)]
67
+ assert dup_integrate([QQ(1), QQ(2), QQ(3)], 3, QQ) == \
68
+ [QQ(1, 60), QQ(1, 12), QQ(1, 2), QQ(0), QQ(0), QQ(0)]
69
+
70
+ assert dup_integrate(dup_from_raw_dict({29: QQ(17)}, QQ), 3, QQ) == \
71
+ dup_from_raw_dict({32: QQ(17, 29760)}, QQ)
72
+
73
+ assert dup_integrate(dup_from_raw_dict({29: QQ(17), 5: QQ(1, 2)}, QQ), 3, QQ) == \
74
+ dup_from_raw_dict({32: QQ(17, 29760), 8: QQ(1, 672)}, QQ)
75
+
76
+
77
+ def test_dmp_integrate():
78
+ assert dmp_integrate([QQ(1)], 2, 0, QQ) == [QQ(1, 2), QQ(0), QQ(0)]
79
+
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
+ raises(IndexError, lambda: dmp_integrate_in(f, 1, -1, 3, QQ))
113
+ raises(IndexError, lambda: dmp_integrate_in(f, 1, 4, 3, QQ))
114
+
115
+
116
+ def test_dup_diff():
117
+ assert dup_diff([], 1, ZZ) == []
118
+ assert dup_diff([7], 1, ZZ) == []
119
+ assert dup_diff([2, 7], 1, ZZ) == [2]
120
+ assert dup_diff([1, 2, 1], 1, ZZ) == [2, 2]
121
+ assert dup_diff([1, 2, 3, 4], 1, ZZ) == [3, 4, 3]
122
+ assert dup_diff([1, -1, 0, 0, 2], 1, ZZ) == [4, -3, 0, 0]
123
+
124
+ f = dup_normal([17, 34, 56, -345, 23, 76, 0, 0, 12, 3, 7], ZZ)
125
+
126
+ assert dup_diff(f, 0, ZZ) == f
127
+ assert dup_diff(f, 1, ZZ) == [170, 306, 448, -2415, 138, 380, 0, 0, 24, 3]
128
+ assert dup_diff(f, 2, ZZ) == dup_diff(dup_diff(f, 1, ZZ), 1, ZZ)
129
+ assert dup_diff(
130
+ f, 3, ZZ) == dup_diff(dup_diff(dup_diff(f, 1, ZZ), 1, ZZ), 1, ZZ)
131
+
132
+ K = FF(3)
133
+ f = dup_normal([17, 34, 56, -345, 23, 76, 0, 0, 12, 3, 7], K)
134
+
135
+ assert dup_diff(f, 1, K) == dup_normal([2, 0, 1, 0, 0, 2, 0, 0, 0, 0], K)
136
+ assert dup_diff(f, 2, K) == dup_normal([1, 0, 0, 2, 0, 0, 0], K)
137
+ assert dup_diff(f, 3, K) == dup_normal([], K)
138
+
139
+ assert dup_diff(f, 0, K) == f
140
+ assert dup_diff(f, 2, K) == dup_diff(dup_diff(f, 1, K), 1, K)
141
+ assert dup_diff(
142
+ f, 3, K) == dup_diff(dup_diff(dup_diff(f, 1, K), 1, K), 1, K)
143
+
144
+
145
+ def test_dmp_diff():
146
+ assert dmp_diff([], 1, 0, ZZ) == []
147
+ assert dmp_diff([[]], 1, 1, ZZ) == [[]]
148
+ assert dmp_diff([[[]]], 1, 2, ZZ) == [[[]]]
149
+
150
+ assert dmp_diff([[[1], [2]]], 1, 2, ZZ) == [[[]]]
151
+
152
+ assert dmp_diff([[[1]], [[]]], 1, 2, ZZ) == [[[1]]]
153
+ assert dmp_diff([[[3]], [[1]], [[]]], 1, 2, ZZ) == [[[6]], [[1]]]
154
+
155
+ assert dmp_diff([1, -1, 0, 0, 2], 1, 0, ZZ) == \
156
+ dup_diff([1, -1, 0, 0, 2], 1, ZZ)
157
+
158
+ assert dmp_diff(f_6, 0, 3, ZZ) == f_6
159
+ assert dmp_diff(f_6, 1, 3, ZZ) == [[[[8460]], [[]]],
160
+ [[[135, 0, 0], [], [], [-135, 0, 0]]],
161
+ [[[]]],
162
+ [[[-423]], [[-47]], [[]], [[141], [], [94, 0], []], [[]]]]
163
+ assert dmp_diff(
164
+ f_6, 2, 3, ZZ) == dmp_diff(dmp_diff(f_6, 1, 3, ZZ), 1, 3, ZZ)
165
+ assert dmp_diff(f_6, 3, 3, ZZ) == dmp_diff(
166
+ dmp_diff(dmp_diff(f_6, 1, 3, ZZ), 1, 3, ZZ), 1, 3, ZZ)
167
+
168
+ K = FF(23)
169
+ F_6 = dmp_normal(f_6, 3, K)
170
+
171
+ assert dmp_diff(F_6, 0, 3, K) == F_6
172
+ assert dmp_diff(F_6, 1, 3, K) == dmp_diff(F_6, 1, 3, K)
173
+ assert dmp_diff(F_6, 2, 3, K) == dmp_diff(dmp_diff(F_6, 1, 3, K), 1, 3, K)
174
+ assert dmp_diff(F_6, 3, 3, K) == dmp_diff(
175
+ dmp_diff(dmp_diff(F_6, 1, 3, K), 1, 3, K), 1, 3, K)
176
+
177
+
178
+ def test_dmp_diff_in():
179
+ assert dmp_diff_in(f_6, 2, 1, 3, ZZ) == \
180
+ dmp_swap(dmp_diff(dmp_swap(f_6, 0, 1, 3, ZZ), 2, 3, ZZ), 0, 1, 3, ZZ)
181
+ assert dmp_diff_in(f_6, 3, 1, 3, ZZ) == \
182
+ dmp_swap(dmp_diff(dmp_swap(f_6, 0, 1, 3, ZZ), 3, 3, ZZ), 0, 1, 3, ZZ)
183
+ assert dmp_diff_in(f_6, 2, 2, 3, ZZ) == \
184
+ dmp_swap(dmp_diff(dmp_swap(f_6, 0, 2, 3, ZZ), 2, 3, ZZ), 0, 2, 3, ZZ)
185
+ assert dmp_diff_in(f_6, 3, 2, 3, ZZ) == \
186
+ dmp_swap(dmp_diff(dmp_swap(f_6, 0, 2, 3, ZZ), 3, 3, ZZ), 0, 2, 3, ZZ)
187
+
188
+ raises(IndexError, lambda: dmp_diff_in(f_6, 1, -1, 3, ZZ))
189
+ raises(IndexError, lambda: dmp_diff_in(f_6, 1, 4, 3, ZZ))
190
+
191
+ def test_dup_eval():
192
+ assert dup_eval([], 7, ZZ) == 0
193
+ assert dup_eval([1, 2], 0, ZZ) == 2
194
+ assert dup_eval([1, 2, 3], 7, ZZ) == 66
195
+
196
+
197
+ def test_dmp_eval():
198
+ assert dmp_eval([], 3, 0, ZZ) == 0
199
+
200
+ assert dmp_eval([[]], 3, 1, ZZ) == []
201
+ assert dmp_eval([[[]]], 3, 2, ZZ) == [[]]
202
+
203
+ assert dmp_eval([[1, 2]], 0, 1, ZZ) == [1, 2]
204
+
205
+ assert dmp_eval([[[1]]], 3, 2, ZZ) == [[1]]
206
+ assert dmp_eval([[[1, 2]]], 3, 2, ZZ) == [[1, 2]]
207
+
208
+ assert dmp_eval([[3, 2], [1, 2]], 3, 1, ZZ) == [10, 8]
209
+ assert dmp_eval([[[3, 2]], [[1, 2]]], 3, 2, ZZ) == [[10, 8]]
210
+
211
+
212
+ def test_dmp_eval_in():
213
+ assert dmp_eval_in(
214
+ f_6, -2, 1, 3, ZZ) == dmp_eval(dmp_swap(f_6, 0, 1, 3, ZZ), -2, 3, ZZ)
215
+ assert dmp_eval_in(
216
+ f_6, 7, 1, 3, ZZ) == dmp_eval(dmp_swap(f_6, 0, 1, 3, ZZ), 7, 3, ZZ)
217
+ assert dmp_eval_in(f_6, -2, 2, 3, ZZ) == dmp_swap(
218
+ dmp_eval(dmp_swap(f_6, 0, 2, 3, ZZ), -2, 3, ZZ), 0, 1, 2, ZZ)
219
+ assert dmp_eval_in(f_6, 7, 2, 3, ZZ) == dmp_swap(
220
+ dmp_eval(dmp_swap(f_6, 0, 2, 3, ZZ), 7, 3, ZZ), 0, 1, 2, ZZ)
221
+
222
+ f = [[[int(45)]], [[]], [[]], [[int(-9)], [-1], [], [int(3), int(0), int(10), int(0)]]]
223
+
224
+ assert dmp_eval_in(f, -2, 2, 2, ZZ) == \
225
+ [[45], [], [], [-9, -1, 0, -44]]
226
+
227
+ raises(IndexError, lambda: dmp_eval_in(f_6, ZZ(1), -1, 3, ZZ))
228
+ raises(IndexError, lambda: dmp_eval_in(f_6, ZZ(1), 4, 3, ZZ))
229
+
230
+
231
+ def test_dmp_eval_tail():
232
+ assert dmp_eval_tail([[]], [1], 1, ZZ) == []
233
+ assert dmp_eval_tail([[[]]], [1], 2, ZZ) == [[]]
234
+ assert dmp_eval_tail([[[]]], [1, 2], 2, ZZ) == []
235
+
236
+ assert dmp_eval_tail(f_0, [], 2, ZZ) == f_0
237
+
238
+ assert dmp_eval_tail(f_0, [1, -17, 8], 2, ZZ) == 84496
239
+ assert dmp_eval_tail(f_0, [-17, 8], 2, ZZ) == [-1409, 3, 85902]
240
+ assert dmp_eval_tail(f_0, [8], 2, ZZ) == [[83, 2], [3], [302, 81, 1]]
241
+
242
+ assert dmp_eval_tail(f_1, [-17, 8], 2, ZZ) == [-136, 15699, 9166, -27144]
243
+
244
+ assert dmp_eval_tail(
245
+ f_2, [-12, 3], 2, ZZ) == [-1377, 0, -702, -1224, 0, -624]
246
+ assert dmp_eval_tail(
247
+ f_3, [-12, 3], 2, ZZ) == [144, 82, -5181, -28872, -14868, -540]
248
+
249
+ assert dmp_eval_tail(
250
+ f_4, [25, -1], 2, ZZ) == [152587890625, 9765625, -59605407714843750,
251
+ -3839159765625, -1562475, 9536712644531250, 610349546750, -4, 24414375000, 1562520]
252
+ assert dmp_eval_tail(f_5, [25, -1], 2, ZZ) == [-1, -78, -2028, -17576]
253
+
254
+ assert dmp_eval_tail(f_6, [0, 2, 4], 3, ZZ) == [5040, 0, 0, 4480]
255
+
256
+
257
+ def test_dmp_diff_eval_in():
258
+ assert dmp_diff_eval_in(f_6, 2, 7, 1, 3, ZZ) == \
259
+ dmp_eval(dmp_diff(dmp_swap(f_6, 0, 1, 3, ZZ), 2, 3, ZZ), 7, 3, ZZ)
260
+
261
+ assert dmp_diff_eval_in(f_6, 2, 7, 0, 3, ZZ) == \
262
+ dmp_eval(dmp_diff(f_6, 2, 3, ZZ), 7, 3, ZZ)
263
+
264
+ raises(IndexError, lambda: dmp_diff_eval_in(f_6, 1, ZZ(1), 4, 3, ZZ))
265
+
266
+
267
+ def test_dup_revert():
268
+ f = [-QQ(1, 720), QQ(0), QQ(1, 24), QQ(0), -QQ(1, 2), QQ(0), QQ(1)]
269
+ g = [QQ(61, 720), QQ(0), QQ(5, 24), QQ(0), QQ(1, 2), QQ(0), QQ(1)]
270
+
271
+ assert dup_revert(f, 8, QQ) == g
272
+
273
+ raises(NotReversible, lambda: dup_revert([QQ(1), QQ(0)], 3, QQ))
274
+
275
+
276
+ def test_dmp_revert():
277
+ f = [-QQ(1, 720), QQ(0), QQ(1, 24), QQ(0), -QQ(1, 2), QQ(0), QQ(1)]
278
+ g = [QQ(61, 720), QQ(0), QQ(5, 24), QQ(0), QQ(1, 2), QQ(0), QQ(1)]
279
+
280
+ assert dmp_revert(f, 8, 0, QQ) == g
281
+
282
+ raises(MultivariatePolynomialError, lambda: dmp_revert([[1]], 2, 1, QQ))
283
+
284
+
285
+ def test_dup_trunc():
286
+ assert dup_trunc([1, 2, 3, 4, 5, 6], ZZ(3), ZZ) == [1, -1, 0, 1, -1, 0]
287
+ assert dup_trunc([6, 5, 4, 3, 2, 1], ZZ(3), ZZ) == [-1, 1, 0, -1, 1]
288
+
289
+ R = ZZ_I
290
+ assert dup_trunc([R(3), R(4), R(5)], R(3), R) == [R(1), R(-1)]
291
+
292
+ K = FF(5)
293
+ assert dup_trunc([K(3), K(4), K(5)], K(3), K) == [K(1), K(0)]
294
+
295
+
296
+ def test_dmp_trunc():
297
+ assert dmp_trunc([[]], [1, 2], 2, ZZ) == [[]]
298
+ assert dmp_trunc([[1, 2], [1, 4, 1], [1]], [1, 2], 1, ZZ) == [[-3], [1]]
299
+
300
+
301
+ def test_dmp_ground_trunc():
302
+ assert dmp_ground_trunc(f_0, ZZ(3), 2, ZZ) == \
303
+ dmp_normal(
304
+ [[[1, -1, 0], [-1]], [[]], [[1, -1, 0], [1, -1, 1], [1]]], 2, ZZ)
305
+
306
+
307
+ def test_dup_monic():
308
+ assert dup_monic([3, 6, 9], ZZ) == [1, 2, 3]
309
+
310
+ raises(ExactQuotientFailed, lambda: dup_monic([3, 4, 5], ZZ))
311
+
312
+ assert dup_monic([], QQ) == []
313
+ assert dup_monic([QQ(1)], QQ) == [QQ(1)]
314
+ assert dup_monic([QQ(7), QQ(1), QQ(21)], QQ) == [QQ(1), QQ(1, 7), QQ(3)]
315
+
316
+
317
+ def test_dmp_ground_monic():
318
+ assert dmp_ground_monic([3, 6, 9], 0, ZZ) == [1, 2, 3]
319
+
320
+ assert dmp_ground_monic([[3], [6], [9]], 1, ZZ) == [[1], [2], [3]]
321
+
322
+ raises(
323
+ ExactQuotientFailed, lambda: dmp_ground_monic([[3], [4], [5]], 1, ZZ))
324
+
325
+ assert dmp_ground_monic([[]], 1, QQ) == [[]]
326
+ assert dmp_ground_monic([[QQ(1)]], 1, QQ) == [[QQ(1)]]
327
+ assert dmp_ground_monic(
328
+ [[QQ(7)], [QQ(1)], [QQ(21)]], 1, QQ) == [[QQ(1)], [QQ(1, 7)], [QQ(3)]]
329
+
330
+
331
+ def test_dup_content():
332
+ assert dup_content([], ZZ) == ZZ(0)
333
+ assert dup_content([1], ZZ) == ZZ(1)
334
+ assert dup_content([-1], ZZ) == ZZ(1)
335
+ assert dup_content([1, 1], ZZ) == ZZ(1)
336
+ assert dup_content([2, 2], ZZ) == ZZ(2)
337
+ assert dup_content([1, 2, 1], ZZ) == ZZ(1)
338
+ assert dup_content([2, 4, 2], ZZ) == ZZ(2)
339
+
340
+ assert dup_content([QQ(2, 3), QQ(4, 9)], QQ) == QQ(2, 9)
341
+ assert dup_content([QQ(2, 3), QQ(4, 5)], QQ) == QQ(2, 15)
342
+
343
+
344
+ def test_dmp_ground_content():
345
+ assert dmp_ground_content([[]], 1, ZZ) == ZZ(0)
346
+ assert dmp_ground_content([[]], 1, QQ) == QQ(0)
347
+ assert dmp_ground_content([[1]], 1, ZZ) == ZZ(1)
348
+ assert dmp_ground_content([[-1]], 1, ZZ) == ZZ(1)
349
+ assert dmp_ground_content([[1], [1]], 1, ZZ) == ZZ(1)
350
+ assert dmp_ground_content([[2], [2]], 1, ZZ) == ZZ(2)
351
+ assert dmp_ground_content([[1], [2], [1]], 1, ZZ) == ZZ(1)
352
+ assert dmp_ground_content([[2], [4], [2]], 1, ZZ) == ZZ(2)
353
+
354
+ assert dmp_ground_content([[QQ(2, 3)], [QQ(4, 9)]], 1, QQ) == QQ(2, 9)
355
+ assert dmp_ground_content([[QQ(2, 3)], [QQ(4, 5)]], 1, QQ) == QQ(2, 15)
356
+
357
+ assert dmp_ground_content(f_0, 2, ZZ) == ZZ(1)
358
+ assert dmp_ground_content(
359
+ dmp_mul_ground(f_0, ZZ(2), 2, ZZ), 2, ZZ) == ZZ(2)
360
+
361
+ assert dmp_ground_content(f_1, 2, ZZ) == ZZ(1)
362
+ assert dmp_ground_content(
363
+ dmp_mul_ground(f_1, ZZ(3), 2, ZZ), 2, ZZ) == ZZ(3)
364
+
365
+ assert dmp_ground_content(f_2, 2, ZZ) == ZZ(1)
366
+ assert dmp_ground_content(
367
+ dmp_mul_ground(f_2, ZZ(4), 2, ZZ), 2, ZZ) == ZZ(4)
368
+
369
+ assert dmp_ground_content(f_3, 2, ZZ) == ZZ(1)
370
+ assert dmp_ground_content(
371
+ dmp_mul_ground(f_3, ZZ(5), 2, ZZ), 2, ZZ) == ZZ(5)
372
+
373
+ assert dmp_ground_content(f_4, 2, ZZ) == ZZ(1)
374
+ assert dmp_ground_content(
375
+ dmp_mul_ground(f_4, ZZ(6), 2, ZZ), 2, ZZ) == ZZ(6)
376
+
377
+ assert dmp_ground_content(f_5, 2, ZZ) == ZZ(1)
378
+ assert dmp_ground_content(
379
+ dmp_mul_ground(f_5, ZZ(7), 2, ZZ), 2, ZZ) == ZZ(7)
380
+
381
+ assert dmp_ground_content(f_6, 3, ZZ) == ZZ(1)
382
+ assert dmp_ground_content(
383
+ dmp_mul_ground(f_6, ZZ(8), 3, ZZ), 3, ZZ) == ZZ(8)
384
+
385
+
386
+ def test_dup_primitive():
387
+ assert dup_primitive([], ZZ) == (ZZ(0), [])
388
+ assert dup_primitive([ZZ(1)], ZZ) == (ZZ(1), [ZZ(1)])
389
+ assert dup_primitive([ZZ(1), ZZ(1)], ZZ) == (ZZ(1), [ZZ(1), ZZ(1)])
390
+ assert dup_primitive([ZZ(2), ZZ(2)], ZZ) == (ZZ(2), [ZZ(1), ZZ(1)])
391
+ assert dup_primitive(
392
+ [ZZ(1), ZZ(2), ZZ(1)], ZZ) == (ZZ(1), [ZZ(1), ZZ(2), ZZ(1)])
393
+ assert dup_primitive(
394
+ [ZZ(2), ZZ(4), ZZ(2)], ZZ) == (ZZ(2), [ZZ(1), ZZ(2), ZZ(1)])
395
+
396
+ assert dup_primitive([], QQ) == (QQ(0), [])
397
+ assert dup_primitive([QQ(1)], QQ) == (QQ(1), [QQ(1)])
398
+ assert dup_primitive([QQ(1), QQ(1)], QQ) == (QQ(1), [QQ(1), QQ(1)])
399
+ assert dup_primitive([QQ(2), QQ(2)], QQ) == (QQ(2), [QQ(1), QQ(1)])
400
+ assert dup_primitive(
401
+ [QQ(1), QQ(2), QQ(1)], QQ) == (QQ(1), [QQ(1), QQ(2), QQ(1)])
402
+ assert dup_primitive(
403
+ [QQ(2), QQ(4), QQ(2)], QQ) == (QQ(2), [QQ(1), QQ(2), QQ(1)])
404
+
405
+ assert dup_primitive(
406
+ [QQ(2, 3), QQ(4, 9)], QQ) == (QQ(2, 9), [QQ(3), QQ(2)])
407
+ assert dup_primitive(
408
+ [QQ(2, 3), QQ(4, 5)], QQ) == (QQ(2, 15), [QQ(5), QQ(6)])
409
+
410
+
411
+ def test_dmp_ground_primitive():
412
+ assert dmp_ground_primitive([ZZ(1)], 0, ZZ) == (ZZ(1), [ZZ(1)])
413
+
414
+ assert dmp_ground_primitive([[]], 1, ZZ) == (ZZ(0), [[]])
415
+
416
+ assert dmp_ground_primitive(f_0, 2, ZZ) == (ZZ(1), f_0)
417
+ assert dmp_ground_primitive(
418
+ dmp_mul_ground(f_0, ZZ(2), 2, ZZ), 2, ZZ) == (ZZ(2), f_0)
419
+
420
+ assert dmp_ground_primitive(f_1, 2, ZZ) == (ZZ(1), f_1)
421
+ assert dmp_ground_primitive(
422
+ dmp_mul_ground(f_1, ZZ(3), 2, ZZ), 2, ZZ) == (ZZ(3), f_1)
423
+
424
+ assert dmp_ground_primitive(f_2, 2, ZZ) == (ZZ(1), f_2)
425
+ assert dmp_ground_primitive(
426
+ dmp_mul_ground(f_2, ZZ(4), 2, ZZ), 2, ZZ) == (ZZ(4), f_2)
427
+
428
+ assert dmp_ground_primitive(f_3, 2, ZZ) == (ZZ(1), f_3)
429
+ assert dmp_ground_primitive(
430
+ dmp_mul_ground(f_3, ZZ(5), 2, ZZ), 2, ZZ) == (ZZ(5), f_3)
431
+
432
+ assert dmp_ground_primitive(f_4, 2, ZZ) == (ZZ(1), f_4)
433
+ assert dmp_ground_primitive(
434
+ dmp_mul_ground(f_4, ZZ(6), 2, ZZ), 2, ZZ) == (ZZ(6), f_4)
435
+
436
+ assert dmp_ground_primitive(f_5, 2, ZZ) == (ZZ(1), f_5)
437
+ assert dmp_ground_primitive(
438
+ dmp_mul_ground(f_5, ZZ(7), 2, ZZ), 2, ZZ) == (ZZ(7), f_5)
439
+
440
+ assert dmp_ground_primitive(f_6, 3, ZZ) == (ZZ(1), f_6)
441
+ assert dmp_ground_primitive(
442
+ dmp_mul_ground(f_6, ZZ(8), 3, ZZ), 3, ZZ) == (ZZ(8), f_6)
443
+
444
+ assert dmp_ground_primitive([[ZZ(2)]], 1, ZZ) == (ZZ(2), [[ZZ(1)]])
445
+ assert dmp_ground_primitive([[QQ(2)]], 1, QQ) == (QQ(2), [[QQ(1)]])
446
+
447
+ assert dmp_ground_primitive(
448
+ [[QQ(2, 3)], [QQ(4, 9)]], 1, QQ) == (QQ(2, 9), [[QQ(3)], [QQ(2)]])
449
+ assert dmp_ground_primitive(
450
+ [[QQ(2, 3)], [QQ(4, 5)]], 1, QQ) == (QQ(2, 15), [[QQ(5)], [QQ(6)]])
451
+
452
+
453
+ def test_dup_extract():
454
+ f = dup_normal([2930944, 0, 2198208, 0, 549552, 0, 45796], ZZ)
455
+ g = dup_normal([17585664, 0, 8792832, 0, 1099104, 0], ZZ)
456
+
457
+ F = dup_normal([64, 0, 48, 0, 12, 0, 1], ZZ)
458
+ G = dup_normal([384, 0, 192, 0, 24, 0], ZZ)
459
+
460
+ assert dup_extract(f, g, ZZ) == (45796, F, G)
461
+
462
+
463
+ def test_dmp_ground_extract():
464
+ f = dmp_normal(
465
+ [[2930944], [], [2198208], [], [549552], [], [45796]], 1, ZZ)
466
+ g = dmp_normal([[17585664], [], [8792832], [], [1099104], []], 1, ZZ)
467
+
468
+ F = dmp_normal([[64], [], [48], [], [12], [], [1]], 1, ZZ)
469
+ G = dmp_normal([[384], [], [192], [], [24], []], 1, ZZ)
470
+
471
+ assert dmp_ground_extract(f, g, 1, ZZ) == (45796, F, G)
472
+
473
+
474
+ def test_dup_real_imag():
475
+ assert dup_real_imag([], ZZ) == ([[]], [[]])
476
+ assert dup_real_imag([1], ZZ) == ([[1]], [[]])
477
+
478
+ assert dup_real_imag([1, 1], ZZ) == ([[1], [1]], [[1, 0]])
479
+ assert dup_real_imag([1, 2], ZZ) == ([[1], [2]], [[1, 0]])
480
+
481
+ assert dup_real_imag(
482
+ [1, 2, 3], ZZ) == ([[1], [2], [-1, 0, 3]], [[2, 0], [2, 0]])
483
+
484
+ assert dup_real_imag([ZZ(1), ZZ(0), ZZ(1), ZZ(3)], ZZ) == (
485
+ [[ZZ(1)], [], [ZZ(-3), ZZ(0), ZZ(1)], [ZZ(3)]],
486
+ [[ZZ(3), ZZ(0)], [], [ZZ(-1), ZZ(0), ZZ(1), ZZ(0)]]
487
+ )
488
+
489
+ raises(DomainError, lambda: dup_real_imag([EX(1), EX(2)], EX))
490
+
491
+
492
+
493
+ def test_dup_mirror():
494
+ assert dup_mirror([], ZZ) == []
495
+ assert dup_mirror([1], ZZ) == [1]
496
+
497
+ assert dup_mirror([1, 2, 3, 4, 5], ZZ) == [1, -2, 3, -4, 5]
498
+ assert dup_mirror([1, 2, 3, 4, 5, 6], ZZ) == [-1, 2, -3, 4, -5, 6]
499
+
500
+
501
+ def test_dup_scale():
502
+ assert dup_scale([], -1, ZZ) == []
503
+ assert dup_scale([1], -1, ZZ) == [1]
504
+
505
+ assert dup_scale([1, 2, 3, 4, 5], -1, ZZ) == [1, -2, 3, -4, 5]
506
+ assert dup_scale([1, 2, 3, 4, 5], -7, ZZ) == [2401, -686, 147, -28, 5]
507
+
508
+
509
+ def test_dup_shift():
510
+ assert dup_shift([], 1, ZZ) == []
511
+ assert dup_shift([1], 1, ZZ) == [1]
512
+
513
+ assert dup_shift([1, 2, 3, 4, 5], 1, ZZ) == [1, 6, 15, 20, 15]
514
+ assert dup_shift([1, 2, 3, 4, 5], 7, ZZ) == [1, 30, 339, 1712, 3267]
515
+
516
+
517
+ def test_dmp_shift():
518
+ assert dmp_shift([ZZ(1), ZZ(2)], [ZZ(1)], 0, ZZ) == [ZZ(1), ZZ(3)]
519
+
520
+ assert dmp_shift([[]], [ZZ(1), ZZ(2)], 1, ZZ) == [[]]
521
+
522
+ xy = [[ZZ(1), ZZ(0)], []] # x*y
523
+ x1y2 = [[ZZ(1), ZZ(2)], [ZZ(1), ZZ(2)]] # (x+1)*(y+2)
524
+ assert dmp_shift(xy, [ZZ(1), ZZ(2)], 1, ZZ) == x1y2
525
+
526
+
527
+ def test_dup_transform():
528
+ assert dup_transform([], [], [1, 1], ZZ) == []
529
+ assert dup_transform([], [1], [1, 1], ZZ) == []
530
+ assert dup_transform([], [1, 2], [1, 1], ZZ) == []
531
+
532
+ assert dup_transform([6, -5, 4, -3, 17], [1, -3, 4], [2, -3], ZZ) == \
533
+ [6, -82, 541, -2205, 6277, -12723, 17191, -13603, 4773]
534
+
535
+
536
+ def test_dup_compose():
537
+ assert dup_compose([], [], ZZ) == []
538
+ assert dup_compose([], [1], ZZ) == []
539
+ assert dup_compose([], [1, 2], ZZ) == []
540
+
541
+ assert dup_compose([1], [], ZZ) == [1]
542
+
543
+ assert dup_compose([1, 2, 0], [], ZZ) == []
544
+ assert dup_compose([1, 2, 1], [], ZZ) == [1]
545
+
546
+ assert dup_compose([1, 2, 1], [1], ZZ) == [4]
547
+ assert dup_compose([1, 2, 1], [7], ZZ) == [64]
548
+
549
+ assert dup_compose([1, 2, 1], [1, -1], ZZ) == [1, 0, 0]
550
+ assert dup_compose([1, 2, 1], [1, 1], ZZ) == [1, 4, 4]
551
+ assert dup_compose([1, 2, 1], [1, 2, 1], ZZ) == [1, 4, 8, 8, 4]
552
+
553
+
554
+ def test_dmp_compose():
555
+ assert dmp_compose([1, 2, 1], [1, 2, 1], 0, ZZ) == [1, 4, 8, 8, 4]
556
+
557
+ assert dmp_compose([[[]]], [[[]]], 2, ZZ) == [[[]]]
558
+ assert dmp_compose([[[]]], [[[1]]], 2, ZZ) == [[[]]]
559
+ assert dmp_compose([[[]]], [[[1]], [[2]]], 2, ZZ) == [[[]]]
560
+
561
+ assert dmp_compose([[[1]]], [], 2, ZZ) == [[[1]]]
562
+
563
+ assert dmp_compose([[1], [2], [ ]], [[]], 1, ZZ) == [[]]
564
+ assert dmp_compose([[1], [2], [1]], [[]], 1, ZZ) == [[1]]
565
+
566
+ assert dmp_compose([[1], [2], [1]], [[1]], 1, ZZ) == [[4]]
567
+ assert dmp_compose([[1], [2], [1]], [[7]], 1, ZZ) == [[64]]
568
+
569
+ assert dmp_compose([[1], [2], [1]], [[1], [-1]], 1, ZZ) == [[1], [ ], [ ]]
570
+ assert dmp_compose([[1], [2], [1]], [[1], [ 1]], 1, ZZ) == [[1], [4], [4]]
571
+
572
+ assert dmp_compose(
573
+ [[1], [2], [1]], [[1], [2], [1]], 1, ZZ) == [[1], [4], [8], [8], [4]]
574
+
575
+
576
+ def test_dup_decompose():
577
+ assert dup_decompose([1], ZZ) == [[1]]
578
+
579
+ assert dup_decompose([1, 0], ZZ) == [[1, 0]]
580
+ assert dup_decompose([1, 0, 0, 0], ZZ) == [[1, 0, 0, 0]]
581
+
582
+ assert dup_decompose([1, 0, 0, 0, 0], ZZ) == [[1, 0, 0], [1, 0, 0]]
583
+ assert dup_decompose(
584
+ [1, 0, 0, 0, 0, 0, 0], ZZ) == [[1, 0, 0, 0], [1, 0, 0]]
585
+
586
+ assert dup_decompose([7, 0, 0, 0, 1], ZZ) == [[7, 0, 1], [1, 0, 0]]
587
+ assert dup_decompose([4, 0, 3, 0, 2], ZZ) == [[4, 3, 2], [1, 0, 0]]
588
+
589
+ f = [1, 0, 20, 0, 150, 0, 500, 0, 625, -2, 0, -10, 9]
590
+
591
+ assert dup_decompose(f, ZZ) == [[1, 0, 0, -2, 9], [1, 0, 5, 0]]
592
+
593
+ f = [2, 0, 40, 0, 300, 0, 1000, 0, 1250, -4, 0, -20, 18]
594
+
595
+ assert dup_decompose(f, ZZ) == [[2, 0, 0, -4, 18], [1, 0, 5, 0]]
596
+
597
+ f = [1, 0, 20, -8, 150, -120, 524, -600, 865, -1034, 600, -170, 29]
598
+
599
+ assert dup_decompose(f, ZZ) == [[1, -8, 24, -34, 29], [1, 0, 5, 0]]
600
+
601
+ R, t = ring("t", ZZ)
602
+ f = [6*t**2 - 42,
603
+ 48*t**2 + 96,
604
+ 144*t**2 + 648*t + 288,
605
+ 624*t**2 + 864*t + 384,
606
+ 108*t**3 + 312*t**2 + 432*t + 192]
607
+
608
+ assert dup_decompose(f, R.to_domain()) == [f]
609
+
610
+
611
+ def test_dmp_lift():
612
+ q = [QQ(1, 1), QQ(0, 1), QQ(1, 1)]
613
+
614
+ f_a = [ANP([QQ(1, 1)], q, QQ), ANP([], q, QQ), ANP([], q, QQ),
615
+ ANP([QQ(1, 1), QQ(0, 1)], q, QQ), ANP([QQ(17, 1), QQ(0, 1)], q, QQ)]
616
+
617
+ f_lift = QQ.map([1, 0, 0, 0, 0, 0, 1, 34, 289])
618
+
619
+ assert dmp_lift(f_a, 0, QQ.algebraic_field(I)) == f_lift
620
+
621
+ f_g = [QQ_I(1), QQ_I(0), QQ_I(0), QQ_I(0, 1), QQ_I(0, 17)]
622
+
623
+ assert dmp_lift(f_g, 0, QQ_I) == f_lift
624
+
625
+ raises(DomainError, lambda: dmp_lift([EX(1), EX(2)], 0, EX))
626
+
627
+
628
+ def test_dup_sign_variations():
629
+ assert dup_sign_variations([], ZZ) == 0
630
+ assert dup_sign_variations([1, 0], ZZ) == 0
631
+ assert dup_sign_variations([1, 0, 2], ZZ) == 0
632
+ assert dup_sign_variations([1, 0, 3, 0], ZZ) == 0
633
+ assert dup_sign_variations([1, 0, 4, 0, 5], ZZ) == 0
634
+
635
+ assert dup_sign_variations([-1, 0, 2], ZZ) == 1
636
+ assert dup_sign_variations([-1, 0, 3, 0], ZZ) == 1
637
+ assert dup_sign_variations([-1, 0, 4, 0, 5], ZZ) == 1
638
+
639
+ assert dup_sign_variations([-1, -4, -5], ZZ) == 0
640
+ assert dup_sign_variations([ 1, -4, -5], ZZ) == 1
641
+ assert dup_sign_variations([ 1, 4, -5], ZZ) == 1
642
+ assert dup_sign_variations([ 1, -4, 5], ZZ) == 2
643
+ assert dup_sign_variations([-1, 4, -5], ZZ) == 2
644
+ assert dup_sign_variations([-1, 4, 5], ZZ) == 1
645
+ assert dup_sign_variations([-1, -4, 5], ZZ) == 1
646
+ assert dup_sign_variations([ 1, 4, 5], ZZ) == 0
647
+
648
+ assert dup_sign_variations([-1, 0, -4, 0, -5], ZZ) == 0
649
+ assert dup_sign_variations([ 1, 0, -4, 0, -5], ZZ) == 1
650
+ assert dup_sign_variations([ 1, 0, 4, 0, -5], ZZ) == 1
651
+ assert dup_sign_variations([ 1, 0, -4, 0, 5], ZZ) == 2
652
+ assert dup_sign_variations([-1, 0, 4, 0, -5], ZZ) == 2
653
+ assert dup_sign_variations([-1, 0, 4, 0, 5], ZZ) == 1
654
+ assert dup_sign_variations([-1, 0, -4, 0, 5], ZZ) == 1
655
+ assert dup_sign_variations([ 1, 0, 4, 0, 5], ZZ) == 0
656
+
657
+
658
+ def test_dup_clear_denoms():
659
+ assert dup_clear_denoms([], QQ, ZZ) == (ZZ(1), [])
660
+
661
+ assert dup_clear_denoms([QQ(1)], QQ, ZZ) == (ZZ(1), [QQ(1)])
662
+ assert dup_clear_denoms([QQ(7)], QQ, ZZ) == (ZZ(1), [QQ(7)])
663
+
664
+ assert dup_clear_denoms([QQ(7, 3)], QQ) == (ZZ(3), [QQ(7)])
665
+ assert dup_clear_denoms([QQ(7, 3)], QQ, ZZ) == (ZZ(3), [QQ(7)])
666
+
667
+ assert dup_clear_denoms(
668
+ [QQ(3), QQ(1), QQ(0)], QQ, ZZ) == (ZZ(1), [QQ(3), QQ(1), QQ(0)])
669
+ assert dup_clear_denoms(
670
+ [QQ(1), QQ(1, 2), QQ(0)], QQ, ZZ) == (ZZ(2), [QQ(2), QQ(1), QQ(0)])
671
+
672
+ assert dup_clear_denoms([QQ(3), QQ(
673
+ 1), QQ(0)], QQ, ZZ, convert=True) == (ZZ(1), [ZZ(3), ZZ(1), ZZ(0)])
674
+ assert dup_clear_denoms([QQ(1), QQ(
675
+ 1, 2), QQ(0)], QQ, ZZ, convert=True) == (ZZ(2), [ZZ(2), ZZ(1), ZZ(0)])
676
+
677
+ assert dup_clear_denoms(
678
+ [EX(S(3)/2), EX(S(9)/4)], EX) == (EX(4), [EX(6), EX(9)])
679
+
680
+ assert dup_clear_denoms([EX(7)], EX) == (EX(1), [EX(7)])
681
+ assert dup_clear_denoms([EX(sin(x)/x), EX(0)], EX) == (EX(x), [EX(sin(x)), EX(0)])
682
+
683
+ F = RR.frac_field(x)
684
+ result = dup_clear_denoms([F(8.48717/(8.0089*x + 2.83)), F(0.0)], F)
685
+ assert str(result) == "(x + 0.353356890459364, [1.05971731448763, 0.0])"
686
+
687
+ def test_dmp_clear_denoms():
688
+ assert dmp_clear_denoms([[]], 1, QQ, ZZ) == (ZZ(1), [[]])
689
+
690
+ assert dmp_clear_denoms([[QQ(1)]], 1, QQ, ZZ) == (ZZ(1), [[QQ(1)]])
691
+ assert dmp_clear_denoms([[QQ(7)]], 1, QQ, ZZ) == (ZZ(1), [[QQ(7)]])
692
+
693
+ assert dmp_clear_denoms([[QQ(7, 3)]], 1, QQ) == (ZZ(3), [[QQ(7)]])
694
+ assert dmp_clear_denoms([[QQ(7, 3)]], 1, QQ, ZZ) == (ZZ(3), [[QQ(7)]])
695
+
696
+ assert dmp_clear_denoms(
697
+ [[QQ(3)], [QQ(1)], []], 1, QQ, ZZ) == (ZZ(1), [[QQ(3)], [QQ(1)], []])
698
+ assert dmp_clear_denoms([[QQ(
699
+ 1)], [QQ(1, 2)], []], 1, QQ, ZZ) == (ZZ(2), [[QQ(2)], [QQ(1)], []])
700
+
701
+ assert dmp_clear_denoms([QQ(3), QQ(
702
+ 1), QQ(0)], 0, QQ, ZZ, convert=True) == (ZZ(1), [ZZ(3), ZZ(1), ZZ(0)])
703
+ assert dmp_clear_denoms([QQ(1), QQ(1, 2), QQ(
704
+ 0)], 0, QQ, ZZ, convert=True) == (ZZ(2), [ZZ(2), ZZ(1), ZZ(0)])
705
+
706
+ assert dmp_clear_denoms([[QQ(3)], [QQ(
707
+ 1)], []], 1, QQ, ZZ, convert=True) == (ZZ(1), [[QQ(3)], [QQ(1)], []])
708
+ assert dmp_clear_denoms([[QQ(1)], [QQ(1, 2)], []], 1, QQ, ZZ,
709
+ convert=True) == (ZZ(2), [[QQ(2)], [QQ(1)], []])
710
+
711
+ assert dmp_clear_denoms(
712
+ [[EX(S(3)/2)], [EX(S(9)/4)]], 1, EX) == (EX(4), [[EX(6)], [EX(9)]])
713
+ assert dmp_clear_denoms([[EX(7)]], 1, EX) == (EX(1), [[EX(7)]])
714
+ assert dmp_clear_denoms([[EX(sin(x)/x), EX(0)]], 1, EX) == (EX(x), [[EX(sin(x)), EX(0)]])
miniconda3/envs/ladir/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 happens 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
miniconda3/envs/ladir/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
miniconda3/envs/ladir/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)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_factortools.py ADDED
@@ -0,0 +1,784 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # The order of the factors will be different when the ground types are
183
+ # flint. At the higher level dup_factor_list will sort the factors.
184
+ c, factors = R.dup_zz_factor(x**3 - 6*x**2 + 11*x - 6)
185
+ assert c == 1
186
+ assert set(factors) == {(x - 3, 1), (x - 2, 1), (x - 1, 1)}
187
+
188
+ assert R.dup_zz_factor_sqf(x**3 - 6*x**2 + 11*x - 6) == \
189
+ (1, [x - 3,
190
+ x - 2,
191
+ x - 1])
192
+
193
+ assert R.dup_zz_factor(3*x**3 + 10*x**2 + 13*x + 10) == \
194
+ (1, [(x + 2, 1),
195
+ (3*x**2 + 4*x + 5, 1)])
196
+
197
+ assert R.dup_zz_factor_sqf(3*x**3 + 10*x**2 + 13*x + 10) == \
198
+ (1, [x + 2,
199
+ 3*x**2 + 4*x + 5])
200
+
201
+ c, factors = R.dup_zz_factor(-x**6 + x**2)
202
+ assert c == -1
203
+ assert set(factors) == {(x, 2), (x - 1, 1), (x + 1, 1), (x**2 + 1, 1)}
204
+
205
+ f = 1080*x**8 + 5184*x**7 + 2099*x**6 + 744*x**5 + 2736*x**4 - 648*x**3 + 129*x**2 - 324
206
+
207
+ assert R.dup_zz_factor(f) == \
208
+ (1, [(5*x**4 + 24*x**3 + 9*x**2 + 12, 1),
209
+ (216*x**4 + 31*x**2 - 27, 1)])
210
+
211
+ f = -29802322387695312500000000000000000000*x**25 \
212
+ + 2980232238769531250000000000000000*x**20 \
213
+ + 1743435859680175781250000000000*x**15 \
214
+ + 114142894744873046875000000*x**10 \
215
+ - 210106372833251953125*x**5 \
216
+ + 95367431640625
217
+
218
+ c, factors = R.dup_zz_factor(f)
219
+ assert c == -95367431640625
220
+ assert set(factors) == {
221
+ (5*x - 1, 1),
222
+ (100*x**2 + 10*x - 1, 2),
223
+ (625*x**4 + 125*x**3 + 25*x**2 + 5*x + 1, 1),
224
+ (10000*x**4 - 3000*x**3 + 400*x**2 - 20*x + 1, 2),
225
+ (10000*x**4 + 2000*x**3 + 400*x**2 + 30*x + 1, 2),
226
+ }
227
+
228
+ f = x**10 - 1
229
+
230
+ config.setup('USE_CYCLOTOMIC_FACTOR', True)
231
+ c0, F_0 = R.dup_zz_factor(f)
232
+
233
+ config.setup('USE_CYCLOTOMIC_FACTOR', False)
234
+ c1, F_1 = R.dup_zz_factor(f)
235
+
236
+ assert c0 == c1 == 1
237
+ assert set(F_0) == set(F_1) == {
238
+ (x - 1, 1),
239
+ (x + 1, 1),
240
+ (x**4 - x**3 + x**2 - x + 1, 1),
241
+ (x**4 + x**3 + x**2 + x + 1, 1),
242
+ }
243
+
244
+ config.setup('USE_CYCLOTOMIC_FACTOR')
245
+
246
+ f = x**10 + 1
247
+
248
+ config.setup('USE_CYCLOTOMIC_FACTOR', True)
249
+ F_0 = R.dup_zz_factor(f)
250
+
251
+ config.setup('USE_CYCLOTOMIC_FACTOR', False)
252
+ F_1 = R.dup_zz_factor(f)
253
+
254
+ assert F_0 == F_1 == \
255
+ (1, [(x**2 + 1, 1),
256
+ (x**8 - x**6 + x**4 - x**2 + 1, 1)])
257
+
258
+ config.setup('USE_CYCLOTOMIC_FACTOR')
259
+
260
+ def test_dmp_zz_wang():
261
+ R, x,y,z = ring("x,y,z", ZZ)
262
+ UV, _x = ring("x", ZZ)
263
+
264
+ p = ZZ(nextprime(R.dmp_zz_mignotte_bound(w_1)))
265
+ assert p == 6291469
266
+
267
+ t_1, k_1, e_1 = y, 1, ZZ(-14)
268
+ t_2, k_2, e_2 = z, 2, ZZ(3)
269
+ t_3, k_3, e_3 = y + z, 2, ZZ(-11)
270
+ t_4, k_4, e_4 = y - z, 1, ZZ(-17)
271
+
272
+ T = [t_1, t_2, t_3, t_4]
273
+ K = [k_1, k_2, k_3, k_4]
274
+ E = [e_1, e_2, e_3, e_4]
275
+
276
+ T = zip([ t.drop(x) for t in T ], K)
277
+
278
+ A = [ZZ(-14), ZZ(3)]
279
+
280
+ S = R.dmp_eval_tail(w_1, A)
281
+ cs, s = UV.dup_primitive(S)
282
+
283
+ assert cs == 1 and s == S == \
284
+ 1036728*_x**6 + 915552*_x**5 + 55748*_x**4 + 105621*_x**3 - 17304*_x**2 - 26841*_x - 644
285
+
286
+ assert R.dmp_zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17]
287
+ assert UV.dup_sqf_p(s) and UV.dup_degree(s) == R.dmp_degree(w_1)
288
+
289
+ _, H = UV.dup_zz_factor_sqf(s)
290
+
291
+ h_1 = 44*_x**2 + 42*_x + 1
292
+ h_2 = 126*_x**2 - 9*_x + 28
293
+ h_3 = 187*_x**2 - 23
294
+
295
+ assert H == [h_1, h_2, h_3]
296
+
297
+ LC = [ lc.drop(x) for lc in [-4*y - 4*z, -y*z**2, y**2 - z**2] ]
298
+
299
+ assert R.dmp_zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC)
300
+
301
+ factors = R.dmp_zz_wang_hensel_lifting(w_1, H, LC, A, p)
302
+ assert R.dmp_expand(factors) == w_1
303
+
304
+
305
+ @XFAIL
306
+ def test_dmp_zz_wang_fail():
307
+ R, x,y,z = ring("x,y,z", ZZ)
308
+ UV, _x = ring("x", ZZ)
309
+
310
+ p = ZZ(nextprime(R.dmp_zz_mignotte_bound(w_1)))
311
+ assert p == 6291469
312
+
313
+ H_1 = [44*x**2 + 42*x + 1, 126*x**2 - 9*x + 28, 187*x**2 - 23]
314
+ 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]
315
+ 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]
316
+
317
+ c_1 = -70686*x**5 - 5863*x**4 - 17826*x**3 + 2009*x**2 + 5031*x + 74
318
+ 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
319
+ 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
320
+
321
+ assert R.dmp_zz_diophantine(H_1, c_1, [], 5, p) == [-3*x, -2, 1]
322
+ assert R.dmp_zz_diophantine(H_2, c_2, [ZZ(-14)], 5, p) == [-x*y, -3*x, -6]
323
+ assert R.dmp_zz_diophantine(H_3, c_3, [ZZ(-14)], 5, p) == [0, 0, -1]
324
+
325
+
326
+ def test_issue_6355():
327
+ # This tests a bug in the Wang algorithm that occurred only with a very
328
+ # specific set of random numbers.
329
+ random_sequence = [-1, -1, 0, 0, 0, 0, -1, -1, 0, -1, 3, -1, 3, 3, 3, 3, -1, 3]
330
+
331
+ R, x, y, z = ring("x,y,z", ZZ)
332
+ f = 2*x**2 + y*z - y - z**2 + z
333
+
334
+ assert R.dmp_zz_wang(f, seed=random_sequence) == [f]
335
+
336
+
337
+ def test_dmp_zz_factor():
338
+ R, x = ring("x", ZZ)
339
+ assert R.dmp_zz_factor(0) == (0, [])
340
+ assert R.dmp_zz_factor(7) == (7, [])
341
+ assert R.dmp_zz_factor(-7) == (-7, [])
342
+
343
+ assert R.dmp_zz_factor(x**2 - 9) == (1, [(x - 3, 1), (x + 3, 1)])
344
+
345
+ R, x, y = ring("x,y", ZZ)
346
+ assert R.dmp_zz_factor(0) == (0, [])
347
+ assert R.dmp_zz_factor(7) == (7, [])
348
+ assert R.dmp_zz_factor(-7) == (-7, [])
349
+
350
+ assert R.dmp_zz_factor(x) == (1, [(x, 1)])
351
+ assert R.dmp_zz_factor(4*x) == (4, [(x, 1)])
352
+ assert R.dmp_zz_factor(4*x + 2) == (2, [(2*x + 1, 1)])
353
+ assert R.dmp_zz_factor(x*y + 1) == (1, [(x*y + 1, 1)])
354
+ assert R.dmp_zz_factor(y**2 + 1) == (1, [(y**2 + 1, 1)])
355
+ assert R.dmp_zz_factor(y**2 - 1) == (1, [(y - 1, 1), (y + 1, 1)])
356
+
357
+ 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)])
358
+ assert R.dmp_zz_factor(x**2*y**2 - 9) == (1, [(x*y - 3, 1), (x*y + 3, 1)])
359
+
360
+ R, x, y, z = ring("x,y,z", ZZ)
361
+ assert R.dmp_zz_factor(x**2*y**2*z**2 - 9) == \
362
+ (1, [(x*y*z - 3, 1),
363
+ (x*y*z + 3, 1)])
364
+
365
+ R, x, y, z, u = ring("x,y,z,u", ZZ)
366
+ assert R.dmp_zz_factor(x**2*y**2*z**2*u**2 - 9) == \
367
+ (1, [(x*y*z*u - 3, 1),
368
+ (x*y*z*u + 3, 1)])
369
+
370
+ R, x, y, z = ring("x,y,z", ZZ)
371
+ assert R.dmp_zz_factor(f_1) == \
372
+ (1, [(x + y*z + 20, 1),
373
+ (x*y + z + 10, 1),
374
+ (x*z + y + 30, 1)])
375
+
376
+ assert R.dmp_zz_factor(f_2) == \
377
+ (1, [(x**2*y**2 + x**2*z**2 + y + 90, 1),
378
+ (x**3*y + x**3*z + z - 11, 1)])
379
+
380
+ assert R.dmp_zz_factor(f_3) == \
381
+ (1, [(x**2*y**2 + x*z**4 + x + z, 1),
382
+ (x**3 + x*y*z + y**2 + y*z**3, 1)])
383
+
384
+ assert R.dmp_zz_factor(f_4) == \
385
+ (-1, [(x*y**3 + z**2, 1),
386
+ (x**2*z + y**4*z**2 + 5, 1),
387
+ (x**3*y - z**2 - 3, 1),
388
+ (x**3*y**4 + z**2, 1)])
389
+
390
+ assert R.dmp_zz_factor(f_5) == \
391
+ (-1, [(x + y - z, 3)])
392
+
393
+ R, x, y, z, t = ring("x,y,z,t", ZZ)
394
+ assert R.dmp_zz_factor(f_6) == \
395
+ (1, [(47*x*y + z**3*t**2 - t**2, 1),
396
+ (45*x**3 - 9*y**3 - y**2 + 3*z**3 + 2*z*t, 1)])
397
+
398
+ R, x, y, z = ring("x,y,z", ZZ)
399
+ assert R.dmp_zz_factor(w_1) == \
400
+ (1, [(x**2*y**2 - x**2*z**2 + y - z**2, 1),
401
+ (x**2*y*z**2 + 3*x*z + 2*y, 1),
402
+ (4*x**2*y + 4*x**2*z + x*y*z - 1, 1)])
403
+
404
+ R, x, y = ring("x,y", ZZ)
405
+ 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
406
+
407
+ assert R.dmp_zz_factor(f) == \
408
+ (-12, [(y, 1),
409
+ (x**2 - y, 6),
410
+ (x**4 + 6*x**2*y + y**2, 1)])
411
+
412
+
413
+ def test_dup_qq_i_factor():
414
+ R, x = ring("x", QQ_I)
415
+ i = QQ_I(0, 1)
416
+
417
+ assert R.dup_qq_i_factor(x**2 - 2) == (QQ_I(1, 0), [(x**2 - 2, 1)])
418
+
419
+ assert R.dup_qq_i_factor(x**2 - 1) == (QQ_I(1, 0), [(x - 1, 1), (x + 1, 1)])
420
+
421
+ assert R.dup_qq_i_factor(x**2 + 1) == (QQ_I(1, 0), [(x - i, 1), (x + i, 1)])
422
+
423
+ assert R.dup_qq_i_factor(x**2/4 + 1) == \
424
+ (QQ_I(QQ(1, 4), 0), [(x - 2*i, 1), (x + 2*i, 1)])
425
+
426
+ assert R.dup_qq_i_factor(x**2 + 4) == \
427
+ (QQ_I(1, 0), [(x - 2*i, 1), (x + 2*i, 1)])
428
+
429
+ assert R.dup_qq_i_factor(x**2 + 2*x + 1) == \
430
+ (QQ_I(1, 0), [(x + 1, 2)])
431
+
432
+ assert R.dup_qq_i_factor(x**2 + 2*i*x - 1) == \
433
+ (QQ_I(1, 0), [(x + i, 2)])
434
+
435
+ f = 8192*x**2 + x*(22656 + 175232*i) - 921416 + 242313*i
436
+
437
+ assert R.dup_qq_i_factor(f) == \
438
+ (QQ_I(8192, 0), [(x + QQ_I(QQ(177, 128), QQ(1369, 128)), 2)])
439
+
440
+
441
+ def test_dmp_qq_i_factor():
442
+ R, x, y = ring("x, y", QQ_I)
443
+ i = QQ_I(0, 1)
444
+
445
+ assert R.dmp_qq_i_factor(x**2 + 2*y**2) == \
446
+ (QQ_I(1, 0), [(x**2 + 2*y**2, 1)])
447
+
448
+ assert R.dmp_qq_i_factor(x**2 + y**2) == \
449
+ (QQ_I(1, 0), [(x - i*y, 1), (x + i*y, 1)])
450
+
451
+ assert R.dmp_qq_i_factor(x**2 + y**2/4) == \
452
+ (QQ_I(1, 0), [(x - i*y/2, 1), (x + i*y/2, 1)])
453
+
454
+ assert R.dmp_qq_i_factor(4*x**2 + y**2) == \
455
+ (QQ_I(4, 0), [(x - i*y/2, 1), (x + i*y/2, 1)])
456
+
457
+
458
+ def test_dup_zz_i_factor():
459
+ R, x = ring("x", ZZ_I)
460
+ i = ZZ_I(0, 1)
461
+
462
+ assert R.dup_zz_i_factor(x**2 - 2) == (ZZ_I(1, 0), [(x**2 - 2, 1)])
463
+
464
+ assert R.dup_zz_i_factor(x**2 - 1) == (ZZ_I(1, 0), [(x - 1, 1), (x + 1, 1)])
465
+
466
+ assert R.dup_zz_i_factor(x**2 + 1) == (ZZ_I(1, 0), [(x - i, 1), (x + i, 1)])
467
+
468
+ assert R.dup_zz_i_factor(x**2 + 4) == \
469
+ (ZZ_I(1, 0), [(x - 2*i, 1), (x + 2*i, 1)])
470
+
471
+ assert R.dup_zz_i_factor(x**2 + 2*x + 1) == \
472
+ (ZZ_I(1, 0), [(x + 1, 2)])
473
+
474
+ assert R.dup_zz_i_factor(x**2 + 2*i*x - 1) == \
475
+ (ZZ_I(1, 0), [(x + i, 2)])
476
+
477
+ f = 8192*x**2 + x*(22656 + 175232*i) - 921416 + 242313*i
478
+
479
+ assert R.dup_zz_i_factor(f) == \
480
+ (ZZ_I(0, 1), [((64 - 64*i)*x + (773 + 596*i), 2)])
481
+
482
+
483
+ def test_dmp_zz_i_factor():
484
+ R, x, y = ring("x, y", ZZ_I)
485
+ i = ZZ_I(0, 1)
486
+
487
+ assert R.dmp_zz_i_factor(x**2 + 2*y**2) == \
488
+ (ZZ_I(1, 0), [(x**2 + 2*y**2, 1)])
489
+
490
+ assert R.dmp_zz_i_factor(x**2 + y**2) == \
491
+ (ZZ_I(1, 0), [(x - i*y, 1), (x + i*y, 1)])
492
+
493
+ assert R.dmp_zz_i_factor(4*x**2 + y**2) == \
494
+ (ZZ_I(1, 0), [(2*x - i*y, 1), (2*x + i*y, 1)])
495
+
496
+
497
+ def test_dup_ext_factor():
498
+ R, x = ring("x", QQ.algebraic_field(I))
499
+ def anp(element):
500
+ return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)
501
+
502
+ assert R.dup_ext_factor(0) == (anp([]), [])
503
+
504
+ f = anp([QQ(1)])*x + anp([QQ(1)])
505
+
506
+ assert R.dup_ext_factor(f) == (anp([QQ(1)]), [(f, 1)])
507
+
508
+ g = anp([QQ(2)])*x + anp([QQ(2)])
509
+
510
+ assert R.dup_ext_factor(g) == (anp([QQ(2)]), [(f, 1)])
511
+
512
+ f = anp([QQ(7)])*x**4 + anp([QQ(1, 1)])
513
+ g = anp([QQ(1)])*x**4 + anp([QQ(1, 7)])
514
+
515
+ assert R.dup_ext_factor(f) == (anp([QQ(7)]), [(g, 1)])
516
+
517
+ f = anp([QQ(1)])*x**4 + anp([QQ(1)])
518
+
519
+ assert R.dup_ext_factor(f) == \
520
+ (anp([QQ(1, 1)]), [(anp([QQ(1)])*x**2 + anp([QQ(-1), QQ(0)]), 1),
521
+ (anp([QQ(1)])*x**2 + anp([QQ( 1), QQ(0)]), 1)])
522
+
523
+ f = anp([QQ(4, 1)])*x**2 + anp([QQ(9, 1)])
524
+
525
+ assert R.dup_ext_factor(f) == \
526
+ (anp([QQ(4, 1)]), [(anp([QQ(1, 1)])*x + anp([-QQ(3, 2), QQ(0, 1)]), 1),
527
+ (anp([QQ(1, 1)])*x + anp([ QQ(3, 2), QQ(0, 1)]), 1)])
528
+
529
+ 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)])
530
+
531
+ assert R.dup_ext_factor(f) == \
532
+ (anp([QQ(4, 1)]), [(anp([QQ(1, 1)])*x + anp([-QQ(4, 1), QQ(1, 1)]), 1),
533
+ (anp([QQ(1, 1)])*x + anp([-QQ(3, 2), QQ(0, 1)]), 1),
534
+ (anp([QQ(1, 1)])*x + anp([ QQ(3, 2), QQ(0, 1)]), 1),
535
+ (anp([QQ(1, 1)])*x + anp([ QQ(4, 1), QQ(1, 1)]), 1)])
536
+
537
+ R, x = ring("x", QQ.algebraic_field(sqrt(2)))
538
+ def anp(element):
539
+ return ANP(element, [QQ(1), QQ(0), QQ(-2)], QQ)
540
+
541
+ f = anp([QQ(1)])*x**4 + anp([QQ(1, 1)])
542
+
543
+ assert R.dup_ext_factor(f) == \
544
+ (anp([QQ(1)]), [(anp([QQ(1)])*x**2 + anp([QQ(-1), QQ(0)])*x + anp([QQ(1)]), 1),
545
+ (anp([QQ(1)])*x**2 + anp([QQ( 1), QQ(0)])*x + anp([QQ(1)]), 1)])
546
+
547
+ f = anp([QQ(1, 1)])*x**2 + anp([QQ(2), QQ(0)])*x + anp([QQ(2, 1)])
548
+
549
+ assert R.dup_ext_factor(f) == \
550
+ (anp([QQ(1, 1)]), [(anp([1])*x + anp([1, 0]), 2)])
551
+
552
+ assert R.dup_ext_factor(f**3) == \
553
+ (anp([QQ(1, 1)]), [(anp([1])*x + anp([1, 0]), 6)])
554
+
555
+ f *= anp([QQ(2, 1)])
556
+
557
+ assert R.dup_ext_factor(f) == \
558
+ (anp([QQ(2, 1)]), [(anp([1])*x + anp([1, 0]), 2)])
559
+
560
+ assert R.dup_ext_factor(f**3) == \
561
+ (anp([QQ(8, 1)]), [(anp([1])*x + anp([1, 0]), 6)])
562
+
563
+
564
+ def test_dmp_ext_factor():
565
+ K = QQ.algebraic_field(sqrt(2))
566
+ R, x,y = ring("x,y", K)
567
+ sqrt2 = K.unit
568
+
569
+ def anp(x):
570
+ return ANP(x, [QQ(1), QQ(0), QQ(-2)], QQ)
571
+
572
+ assert R.dmp_ext_factor(0) == (anp([]), [])
573
+
574
+ f = anp([QQ(1)])*x + anp([QQ(1)])
575
+
576
+ assert R.dmp_ext_factor(f) == (anp([QQ(1)]), [(f, 1)])
577
+
578
+ g = anp([QQ(2)])*x + anp([QQ(2)])
579
+
580
+ assert R.dmp_ext_factor(g) == (anp([QQ(2)]), [(f, 1)])
581
+
582
+ f = anp([QQ(1)])*x**2 + anp([QQ(-2)])*y**2
583
+
584
+ assert R.dmp_ext_factor(f) == \
585
+ (anp([QQ(1)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1),
586
+ (anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)])
587
+
588
+ f = anp([QQ(2)])*x**2 + anp([QQ(-4)])*y**2
589
+
590
+ assert R.dmp_ext_factor(f) == \
591
+ (anp([QQ(2)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1),
592
+ (anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)])
593
+
594
+ f1 = y + 1
595
+ f2 = y + sqrt2
596
+ f3 = x**2 + x + 2 + 3*sqrt2
597
+ f = f1**2 * f2**2 * f3**2
598
+ assert R.dmp_ext_factor(f) == (K.one, [(f1, 2), (f2, 2), (f3, 2)])
599
+
600
+
601
+ def test_dup_factor_list():
602
+ R, x = ring("x", ZZ)
603
+ assert R.dup_factor_list(0) == (0, [])
604
+ assert R.dup_factor_list(7) == (7, [])
605
+
606
+ R, x = ring("x", QQ)
607
+ assert R.dup_factor_list(0) == (0, [])
608
+ assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
609
+
610
+ R, x = ring("x", ZZ['t'])
611
+ assert R.dup_factor_list(0) == (0, [])
612
+ assert R.dup_factor_list(7) == (7, [])
613
+
614
+ R, x = ring("x", QQ['t'])
615
+ assert R.dup_factor_list(0) == (0, [])
616
+ assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
617
+
618
+ R, x = ring("x", ZZ)
619
+ assert R.dup_factor_list_include(0) == [(0, 1)]
620
+ assert R.dup_factor_list_include(7) == [(7, 1)]
621
+
622
+ assert R.dup_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
623
+ assert R.dup_factor_list_include(x**2 + 2*x + 1) == [(x + 1, 2)]
624
+ # issue 8037
625
+ assert R.dup_factor_list(6*x**2 - 5*x - 6) == (1, [(2*x - 3, 1), (3*x + 2, 1)])
626
+
627
+ R, x = ring("x", QQ)
628
+ assert R.dup_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1, 2), [(x + 1, 2)])
629
+
630
+ R, x = ring("x", FF(2))
631
+ assert R.dup_factor_list(x**2 + 1) == (1, [(x + 1, 2)])
632
+
633
+ R, x = ring("x", RR)
634
+ assert R.dup_factor_list(1.0*x**2 + 2.0*x + 1.0) == (1.0, [(1.0*x + 1.0, 2)])
635
+ assert R.dup_factor_list(2.0*x**2 + 4.0*x + 2.0) == (2.0, [(1.0*x + 1.0, 2)])
636
+
637
+ f = 6.7225336055071*x**2 - 10.6463972754741*x - 0.33469524022264
638
+ coeff, factors = R.dup_factor_list(f)
639
+ assert coeff == RR(10.6463972754741)
640
+ assert len(factors) == 1
641
+ assert factors[0][0].max_norm() == RR(1.0)
642
+ assert factors[0][1] == 1
643
+
644
+ Rt, t = ring("t", ZZ)
645
+ R, x = ring("x", Rt)
646
+
647
+ f = 4*t*x**2 + 4*t**2*x
648
+
649
+ assert R.dup_factor_list(f) == \
650
+ (4*t, [(x, 1),
651
+ (x + t, 1)])
652
+
653
+ Rt, t = ring("t", QQ)
654
+ R, x = ring("x", Rt)
655
+
656
+ f = QQ(1, 2)*t*x**2 + QQ(1, 2)*t**2*x
657
+
658
+ assert R.dup_factor_list(f) == \
659
+ (QQ(1, 2)*t, [(x, 1),
660
+ (x + t, 1)])
661
+
662
+ R, x = ring("x", QQ.algebraic_field(I))
663
+ def anp(element):
664
+ return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)
665
+
666
+ f = anp([QQ(1, 1)])*x**4 + anp([QQ(2, 1)])*x**2
667
+
668
+ assert R.dup_factor_list(f) == \
669
+ (anp([QQ(1, 1)]), [(anp([QQ(1, 1)])*x, 2),
670
+ (anp([QQ(1, 1)])*x**2 + anp([])*x + anp([QQ(2, 1)]), 1)])
671
+
672
+ R, x = ring("x", EX)
673
+ raises(DomainError, lambda: R.dup_factor_list(EX(sin(1))))
674
+
675
+
676
+ def test_dmp_factor_list():
677
+ R, x, y = ring("x,y", ZZ)
678
+ assert R.dmp_factor_list(0) == (ZZ(0), [])
679
+ assert R.dmp_factor_list(7) == (7, [])
680
+
681
+ R, x, y = ring("x,y", QQ)
682
+ assert R.dmp_factor_list(0) == (QQ(0), [])
683
+ assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
684
+
685
+ Rt, t = ring("t", ZZ)
686
+ R, x, y = ring("x,y", Rt)
687
+ assert R.dmp_factor_list(0) == (0, [])
688
+ assert R.dmp_factor_list(7) == (ZZ(7), [])
689
+
690
+ Rt, t = ring("t", QQ)
691
+ R, x, y = ring("x,y", Rt)
692
+ assert R.dmp_factor_list(0) == (0, [])
693
+ assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
694
+
695
+ R, x, y = ring("x,y", ZZ)
696
+ assert R.dmp_factor_list_include(0) == [(0, 1)]
697
+ assert R.dmp_factor_list_include(7) == [(7, 1)]
698
+
699
+ R, X = xring("x:200", ZZ)
700
+
701
+ f, g = X[0]**2 + 2*X[0] + 1, X[0] + 1
702
+ assert R.dmp_factor_list(f) == (1, [(g, 2)])
703
+
704
+ f, g = X[-1]**2 + 2*X[-1] + 1, X[-1] + 1
705
+ assert R.dmp_factor_list(f) == (1, [(g, 2)])
706
+
707
+ R, x = ring("x", ZZ)
708
+ assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
709
+ R, x = ring("x", QQ)
710
+ assert R.dmp_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1,2), [(x + 1, 2)])
711
+
712
+ R, x, y = ring("x,y", ZZ)
713
+ assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
714
+ R, x, y = ring("x,y", QQ)
715
+ assert R.dmp_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1,2), [(x + 1, 2)])
716
+
717
+ R, x, y = ring("x,y", ZZ)
718
+ f = 4*x**2*y + 4*x*y**2
719
+
720
+ assert R.dmp_factor_list(f) == \
721
+ (4, [(y, 1),
722
+ (x, 1),
723
+ (x + y, 1)])
724
+
725
+ assert R.dmp_factor_list_include(f) == \
726
+ [(4*y, 1),
727
+ (x, 1),
728
+ (x + y, 1)]
729
+
730
+ R, x, y = ring("x,y", QQ)
731
+ f = QQ(1,2)*x**2*y + QQ(1,2)*x*y**2
732
+
733
+ assert R.dmp_factor_list(f) == \
734
+ (QQ(1,2), [(y, 1),
735
+ (x, 1),
736
+ (x + y, 1)])
737
+
738
+ R, x, y = ring("x,y", RR)
739
+ f = 2.0*x**2 - 8.0*y**2
740
+
741
+ assert R.dmp_factor_list(f) == \
742
+ (RR(8.0), [(0.5*x - y, 1),
743
+ (0.5*x + y, 1)])
744
+
745
+ f = 6.7225336055071*x**2*y**2 - 10.6463972754741*x*y - 0.33469524022264
746
+ coeff, factors = R.dmp_factor_list(f)
747
+ assert coeff == RR(10.6463972754741)
748
+ assert len(factors) == 1
749
+ assert factors[0][0].max_norm() == RR(1.0)
750
+ assert factors[0][1] == 1
751
+
752
+ Rt, t = ring("t", ZZ)
753
+ R, x, y = ring("x,y", Rt)
754
+ f = 4*t*x**2 + 4*t**2*x
755
+
756
+ assert R.dmp_factor_list(f) == \
757
+ (4*t, [(x, 1),
758
+ (x + t, 1)])
759
+
760
+ Rt, t = ring("t", QQ)
761
+ R, x, y = ring("x,y", Rt)
762
+ f = QQ(1, 2)*t*x**2 + QQ(1, 2)*t**2*x
763
+
764
+ assert R.dmp_factor_list(f) == \
765
+ (QQ(1, 2)*t, [(x, 1),
766
+ (x + t, 1)])
767
+
768
+ R, x, y = ring("x,y", FF(2))
769
+ raises(NotImplementedError, lambda: R.dmp_factor_list(x**2 + y**2))
770
+
771
+ R, x, y = ring("x,y", EX)
772
+ raises(DomainError, lambda: R.dmp_factor_list(EX(sin(1))))
773
+
774
+
775
+ def test_dup_irreducible_p():
776
+ R, x = ring("x", ZZ)
777
+ assert R.dup_irreducible_p(x**2 + x + 1) is True
778
+ assert R.dup_irreducible_p(x**2 + 2*x + 1) is False
779
+
780
+
781
+ def test_dmp_irreducible_p():
782
+ R, x, y = ring("x,y", ZZ)
783
+ assert R.dmp_irreducible_p(x**2 + x + 1) is True
784
+ assert R.dmp_irreducible_p(x**2 + 2*x + 1) is False
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_fields.py ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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] != field("x,y,z", ZZ)[0]
33
+ assert field("x,y,z", ZZ)[0] != field("x,y,z", QQ)[0]
34
+ assert field("x,y,z", QQ)[0] != field("x,y", QQ)[0]
35
+ assert field("x,y", QQ)[0] != field("x,y,z", QQ)[0]
36
+
37
+ def test_sfield():
38
+ x = symbols("x")
39
+
40
+ F = FracField((E, exp(exp(x)), exp(x)), ZZ, lex)
41
+ e, exex, ex = F.gens
42
+ assert sfield(exp(x)*exp(exp(x) + 1 + log(exp(x) + 3)/2)**2/(exp(x) + 3)) \
43
+ == (F, e**2*exex**2*ex)
44
+
45
+ F = FracField((x, exp(1/x), log(x), x**QQ(1, 3)), ZZ, lex)
46
+ _, ex, lg, x3 = F.gens
47
+ assert sfield(((x-3)*log(x)+4*x**2)*exp(1/x+log(x)/3)/x**2) == \
48
+ (F, (4*F.x**2*ex + F.x*ex*lg - 3*ex*lg)/x3**5)
49
+
50
+ F = FracField((x, log(x), sqrt(x + log(x))), ZZ, lex)
51
+ _, lg, srt = F.gens
52
+ assert sfield((x + 1) / (x * (x + log(x))**QQ(3, 2)) - 1/(x * log(x)**2)) \
53
+ == (F, (F.x*lg**2 - F.x*srt + lg**2 - lg*srt)/
54
+ (F.x**2*lg**2*srt + F.x*lg**3*srt))
55
+
56
+ def test_FracElement___hash__():
57
+ F, x, y, z = field("x,y,z", QQ)
58
+ assert hash(x*y/z)
59
+
60
+ def test_FracElement_copy():
61
+ F, x, y, z = field("x,y,z", ZZ)
62
+
63
+ f = x*y/3*z
64
+ g = f.copy()
65
+
66
+ assert f == g
67
+ g.numer[(1, 1, 1)] = 7
68
+ assert f != g
69
+
70
+ def test_FracElement_as_expr():
71
+ F, x, y, z = field("x,y,z", ZZ)
72
+ f = (3*x**2*y - x*y*z)/(7*z**3 + 1)
73
+
74
+ X, Y, Z = F.symbols
75
+ g = (3*X**2*Y - X*Y*Z)/(7*Z**3 + 1)
76
+
77
+ assert f != g
78
+ assert f.as_expr() == g
79
+
80
+ X, Y, Z = symbols("x,y,z")
81
+ g = (3*X**2*Y - X*Y*Z)/(7*Z**3 + 1)
82
+
83
+ assert f != g
84
+ assert f.as_expr(X, Y, Z) == g
85
+
86
+ raises(ValueError, lambda: f.as_expr(X))
87
+
88
+ def test_FracElement_from_expr():
89
+ x, y, z = symbols("x,y,z")
90
+ F, X, Y, Z = field((x, y, z), ZZ)
91
+
92
+ f = F.from_expr(1)
93
+ assert f == 1 and F.is_element(f)
94
+
95
+ f = F.from_expr(Rational(3, 7))
96
+ assert f == F(3)/7 and F.is_element(f)
97
+
98
+ f = F.from_expr(x)
99
+ assert f == X and F.is_element(f)
100
+
101
+ f = F.from_expr(Rational(3,7)*x)
102
+ assert f == X*Rational(3, 7) and F.is_element(f)
103
+
104
+ f = F.from_expr(1/x)
105
+ assert f == 1/X and F.is_element(f)
106
+
107
+ f = F.from_expr(x*y*z)
108
+ assert f == X*Y*Z and F.is_element(f)
109
+
110
+ f = F.from_expr(x*y/z)
111
+ assert f == X*Y/Z and F.is_element(f)
112
+
113
+ f = F.from_expr(x*y*z + x*y + x)
114
+ assert f == X*Y*Z + X*Y + X and F.is_element(f)
115
+
116
+ f = F.from_expr((x*y*z + x*y + x)/(x*y + 7))
117
+ assert f == (X*Y*Z + X*Y + X)/(X*Y + 7) and F.is_element(f)
118
+
119
+ f = F.from_expr(x**3*y*z + x**2*y**7 + 1)
120
+ assert f == X**3*Y*Z + X**2*Y**7 + 1 and F.is_element(f)
121
+
122
+ raises(ValueError, lambda: F.from_expr(2**x))
123
+ raises(ValueError, lambda: F.from_expr(7*x + sqrt(2)))
124
+
125
+ assert isinstance(ZZ[2**x].get_field().convert(2**(-x)),
126
+ FracElement)
127
+ assert isinstance(ZZ[x**2].get_field().convert(x**(-6)),
128
+ FracElement)
129
+ assert isinstance(ZZ[exp(Rational(1, 3))].get_field().convert(E),
130
+ FracElement)
131
+
132
+
133
+ def test_FracField_nested():
134
+ a, b, x = symbols('a b x')
135
+ F1 = ZZ.frac_field(a, b)
136
+ F2 = F1.frac_field(x)
137
+ frac = F2(a + b)
138
+ assert frac.numer == F1.poly_ring(x)(a + b)
139
+ assert frac.numer.coeffs() == [F1(a + b)]
140
+ assert frac.denom == F1.poly_ring(x)(1)
141
+
142
+ F3 = ZZ.poly_ring(a, b)
143
+ F4 = F3.frac_field(x)
144
+ frac = F4(a + b)
145
+ assert frac.numer == F3.poly_ring(x)(a + b)
146
+ assert frac.numer.coeffs() == [F3(a + b)]
147
+ assert frac.denom == F3.poly_ring(x)(1)
148
+
149
+ frac = F2(F3(a + b))
150
+ assert frac.numer == F1.poly_ring(x)(a + b)
151
+ assert frac.numer.coeffs() == [F1(a + b)]
152
+ assert frac.denom == F1.poly_ring(x)(1)
153
+
154
+ frac = F4(F1(a + b))
155
+ assert frac.numer == F3.poly_ring(x)(a + b)
156
+ assert frac.numer.coeffs() == [F3(a + b)]
157
+ assert frac.denom == F3.poly_ring(x)(1)
158
+
159
+
160
+ def test_FracElement__lt_le_gt_ge__():
161
+ F, x, y = field("x,y", ZZ)
162
+
163
+ assert F(1) < 1/x < 1/x**2 < 1/x**3
164
+ assert F(1) <= 1/x <= 1/x**2 <= 1/x**3
165
+
166
+ assert -7/x < 1/x < 3/x < y/x < 1/x**2
167
+ assert -7/x <= 1/x <= 3/x <= y/x <= 1/x**2
168
+
169
+ assert 1/x**3 > 1/x**2 > 1/x > F(1)
170
+ assert 1/x**3 >= 1/x**2 >= 1/x >= F(1)
171
+
172
+ assert 1/x**2 > y/x > 3/x > 1/x > -7/x
173
+ assert 1/x**2 >= y/x >= 3/x >= 1/x >= -7/x
174
+
175
+ def test_FracElement___neg__():
176
+ F, x,y = field("x,y", QQ)
177
+
178
+ f = (7*x - 9)/y
179
+ g = (-7*x + 9)/y
180
+
181
+ assert -f == g
182
+ assert -g == f
183
+
184
+ def test_FracElement___add__():
185
+ F, x,y = field("x,y", QQ)
186
+
187
+ f, g = 1/x, 1/y
188
+ assert f + g == g + f == (x + y)/(x*y)
189
+
190
+ assert x + F.ring.gens[0] == F.ring.gens[0] + x == 2*x
191
+
192
+ F, x,y = field("x,y", ZZ)
193
+ assert x + 3 == 3 + x
194
+ assert x + QQ(3,7) == QQ(3,7) + x == (7*x + 3)/7
195
+
196
+ Fuv, u,v = field("u,v", ZZ)
197
+ Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
198
+
199
+ f = (u*v + x)/(y + u*v)
200
+ assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u*v}
201
+ assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u*v}
202
+
203
+ Ruv, u,v = ring("u,v", ZZ)
204
+ Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
205
+
206
+ f = (u*v + x)/(y + u*v)
207
+ assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u*v}
208
+ assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u*v}
209
+
210
+ def test_FracElement___sub__():
211
+ F, x,y = field("x,y", QQ)
212
+
213
+ f, g = 1/x, 1/y
214
+ assert f - g == (-x + y)/(x*y)
215
+
216
+ assert x - F.ring.gens[0] == F.ring.gens[0] - x == 0
217
+
218
+ F, x,y = field("x,y", ZZ)
219
+ assert x - 3 == -(3 - x)
220
+ assert x - QQ(3,7) == -(QQ(3,7) - x) == (7*x - 3)/7
221
+
222
+ Fuv, u,v = field("u,v", ZZ)
223
+ Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
224
+
225
+ f = (u*v - x)/(y - u*v)
226
+ assert dict(f.numer) == {(1, 0, 0, 0):-1, (0, 0, 0, 0): u*v}
227
+ assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0):-u*v}
228
+
229
+ Ruv, u,v = ring("u,v", ZZ)
230
+ Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
231
+
232
+ f = (u*v - x)/(y - u*v)
233
+ assert dict(f.numer) == {(1, 0, 0, 0):-1, (0, 0, 0, 0): u*v}
234
+ assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0):-u*v}
235
+
236
+ def test_FracElement___mul__():
237
+ F, x,y = field("x,y", QQ)
238
+
239
+ f, g = 1/x, 1/y
240
+ assert f*g == g*f == 1/(x*y)
241
+
242
+ assert x*F.ring.gens[0] == F.ring.gens[0]*x == x**2
243
+
244
+ F, x,y = field("x,y", ZZ)
245
+ assert x*3 == 3*x
246
+ assert x*QQ(3,7) == QQ(3,7)*x == x*Rational(3, 7)
247
+
248
+ Fuv, u,v = field("u,v", ZZ)
249
+ Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
250
+
251
+ f = ((u + 1)*x*y + 1)/((v - 1)*z - t*u*v - 1)
252
+ assert dict(f.numer) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
253
+ assert dict(f.denom) == {(0, 0, 1, 0): v - 1, (0, 0, 0, 1): -u*v, (0, 0, 0, 0): -1}
254
+
255
+ Ruv, u,v = ring("u,v", ZZ)
256
+ Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
257
+
258
+ f = ((u + 1)*x*y + 1)/((v - 1)*z - t*u*v - 1)
259
+ assert dict(f.numer) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
260
+ assert dict(f.denom) == {(0, 0, 1, 0): v - 1, (0, 0, 0, 1): -u*v, (0, 0, 0, 0): -1}
261
+
262
+ def test_FracElement___truediv__():
263
+ F, x,y = field("x,y", QQ)
264
+
265
+ f, g = 1/x, 1/y
266
+ assert f/g == y/x
267
+
268
+ assert x/F.ring.gens[0] == F.ring.gens[0]/x == 1
269
+
270
+ F, x,y = field("x,y", ZZ)
271
+ assert x*3 == 3*x
272
+ assert x/QQ(3,7) == (QQ(3,7)/x)**-1 == x*Rational(7, 3)
273
+
274
+ raises(ZeroDivisionError, lambda: x/0)
275
+ raises(ZeroDivisionError, lambda: 1/(x - x))
276
+ raises(ZeroDivisionError, lambda: x/(x - x))
277
+
278
+ Fuv, u,v = field("u,v", ZZ)
279
+ Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
280
+
281
+ f = (u*v)/(x*y)
282
+ assert dict(f.numer) == {(0, 0, 0, 0): u*v}
283
+ assert dict(f.denom) == {(1, 1, 0, 0): 1}
284
+
285
+ g = (x*y)/(u*v)
286
+ assert dict(g.numer) == {(1, 1, 0, 0): 1}
287
+ assert dict(g.denom) == {(0, 0, 0, 0): u*v}
288
+
289
+ Ruv, u,v = ring("u,v", ZZ)
290
+ Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
291
+
292
+ f = (u*v)/(x*y)
293
+ assert dict(f.numer) == {(0, 0, 0, 0): u*v}
294
+ assert dict(f.denom) == {(1, 1, 0, 0): 1}
295
+
296
+ g = (x*y)/(u*v)
297
+ assert dict(g.numer) == {(1, 1, 0, 0): 1}
298
+ assert dict(g.denom) == {(0, 0, 0, 0): u*v}
299
+
300
+ def test_FracElement___pow__():
301
+ F, x,y = field("x,y", QQ)
302
+
303
+ f, g = 1/x, 1/y
304
+
305
+ assert f**3 == 1/x**3
306
+ assert g**3 == 1/y**3
307
+
308
+ assert (f*g)**3 == 1/(x**3*y**3)
309
+ assert (f*g)**-3 == (x*y)**3
310
+
311
+ raises(ZeroDivisionError, lambda: (x - x)**-3)
312
+
313
+ def test_FracElement_diff():
314
+ F, x,y,z = field("x,y,z", ZZ)
315
+
316
+ assert ((x**2 + y)/(z + 1)).diff(x) == 2*x/(z + 1)
317
+
318
+ @XFAIL
319
+ def test_FracElement___call__():
320
+ F, x,y,z = field("x,y,z", ZZ)
321
+ f = (x**2 + 3*y)/z
322
+
323
+ r = f(1, 1, 1)
324
+ assert r == 4 and not isinstance(r, FracElement)
325
+ raises(ZeroDivisionError, lambda: f(1, 1, 0))
326
+
327
+ def test_FracElement_evaluate():
328
+ F, x,y,z = field("x,y,z", ZZ)
329
+ Fyz = field("y,z", ZZ)[0]
330
+ f = (x**2 + 3*y)/z
331
+
332
+ assert f.evaluate(x, 0) == 3*Fyz.y/Fyz.z
333
+ raises(ZeroDivisionError, lambda: f.evaluate(z, 0))
334
+
335
+ def test_FracElement_subs():
336
+ F, x,y,z = field("x,y,z", ZZ)
337
+ f = (x**2 + 3*y)/z
338
+
339
+ assert f.subs(x, 0) == 3*y/z
340
+ raises(ZeroDivisionError, lambda: f.subs(z, 0))
341
+
342
+ def test_FracElement_compose():
343
+ pass
344
+
345
+ def test_FracField_index():
346
+ a = symbols("a")
347
+ F, x, y, z = field('x y z', QQ)
348
+ assert F.index(x) == 0
349
+ assert F.index(y) == 1
350
+
351
+ raises(ValueError, lambda: F.index(1))
352
+ raises(ValueError, lambda: F.index(a))
353
+ pass
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_galoistools.py ADDED
@@ -0,0 +1,875 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.polys.galoistools import (
2
+ gf_crt, gf_crt1, gf_crt2, gf_int,
3
+ gf_degree, gf_strip, gf_trunc, gf_normal,
4
+ gf_from_dict, gf_to_dict,
5
+ gf_from_int_poly, gf_to_int_poly,
6
+ gf_neg, gf_add_ground, gf_sub_ground, gf_mul_ground,
7
+ gf_add, gf_sub, gf_add_mul, gf_sub_mul, gf_mul, gf_sqr,
8
+ gf_div, gf_rem, gf_quo, gf_exquo,
9
+ gf_lshift, gf_rshift, gf_expand,
10
+ gf_pow, gf_pow_mod,
11
+ gf_gcdex, gf_gcd, gf_lcm, gf_cofactors,
12
+ gf_LC, gf_TC, gf_monic,
13
+ gf_eval, gf_multi_eval,
14
+ gf_compose, gf_compose_mod,
15
+ gf_trace_map,
16
+ gf_diff,
17
+ gf_irreducible, gf_irreducible_p,
18
+ gf_irred_p_ben_or, gf_irred_p_rabin,
19
+ gf_sqf_list, gf_sqf_part, gf_sqf_p,
20
+ gf_Qmatrix, gf_Qbasis,
21
+ gf_ddf_zassenhaus, gf_ddf_shoup,
22
+ gf_edf_zassenhaus, gf_edf_shoup,
23
+ gf_berlekamp,
24
+ gf_factor_sqf, gf_factor,
25
+ gf_value, linear_congruence, _csolve_prime_las_vegas,
26
+ csolve_prime, gf_csolve, gf_frobenius_map, gf_frobenius_monomial_base
27
+ )
28
+
29
+ from sympy.polys.polyerrors import (
30
+ ExactQuotientFailed,
31
+ )
32
+
33
+ from sympy.polys import polyconfig as config
34
+
35
+ from sympy.polys.domains import ZZ
36
+ from sympy.core.numbers import pi
37
+ from sympy.ntheory.generate import nextprime
38
+ from sympy.testing.pytest import raises
39
+
40
+
41
+ def test_gf_crt():
42
+ U = [49, 76, 65]
43
+ M = [99, 97, 95]
44
+
45
+ p = 912285
46
+ u = 639985
47
+
48
+ assert gf_crt(U, M, ZZ) == u
49
+
50
+ E = [9215, 9405, 9603]
51
+ S = [62, 24, 12]
52
+
53
+ assert gf_crt1(M, ZZ) == (p, E, S)
54
+ assert gf_crt2(U, M, p, E, S, ZZ) == u
55
+
56
+
57
+ def test_gf_int():
58
+ assert gf_int(0, 5) == 0
59
+ assert gf_int(1, 5) == 1
60
+ assert gf_int(2, 5) == 2
61
+ assert gf_int(3, 5) == -2
62
+ assert gf_int(4, 5) == -1
63
+ assert gf_int(5, 5) == 0
64
+
65
+
66
+ def test_gf_degree():
67
+ assert gf_degree([]) == -1
68
+ assert gf_degree([1]) == 0
69
+ assert gf_degree([1, 0]) == 1
70
+ assert gf_degree([1, 0, 0, 0, 1]) == 4
71
+
72
+
73
+ def test_gf_strip():
74
+ assert gf_strip([]) == []
75
+ assert gf_strip([0]) == []
76
+ assert gf_strip([0, 0, 0]) == []
77
+
78
+ assert gf_strip([1]) == [1]
79
+ assert gf_strip([0, 1]) == [1]
80
+ assert gf_strip([0, 0, 0, 1]) == [1]
81
+
82
+ assert gf_strip([1, 2, 0]) == [1, 2, 0]
83
+ assert gf_strip([0, 1, 2, 0]) == [1, 2, 0]
84
+ assert gf_strip([0, 0, 0, 1, 2, 0]) == [1, 2, 0]
85
+
86
+
87
+ def test_gf_trunc():
88
+ assert gf_trunc([], 11) == []
89
+ assert gf_trunc([1], 11) == [1]
90
+ assert gf_trunc([22], 11) == []
91
+ assert gf_trunc([12], 11) == [1]
92
+
93
+ assert gf_trunc([11, 22, 17, 1, 0], 11) == [6, 1, 0]
94
+ assert gf_trunc([12, 23, 17, 1, 0], 11) == [1, 1, 6, 1, 0]
95
+
96
+
97
+ def test_gf_normal():
98
+ assert gf_normal([11, 22, 17, 1, 0], 11, ZZ) == [6, 1, 0]
99
+
100
+
101
+ def test_gf_from_to_dict():
102
+ f = {11: 12, 6: 2, 0: 25}
103
+ F = {11: 1, 6: 2, 0: 3}
104
+ g = [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 3]
105
+
106
+ assert gf_from_dict(f, 11, ZZ) == g
107
+ assert gf_to_dict(g, 11) == F
108
+
109
+ f = {11: -5, 4: 0, 3: 1, 0: 12}
110
+ F = {11: -5, 3: 1, 0: 1}
111
+ g = [6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
112
+
113
+ assert gf_from_dict(f, 11, ZZ) == g
114
+ assert gf_to_dict(g, 11) == F
115
+
116
+ assert gf_to_dict([10], 11, symmetric=True) == {0: -1}
117
+ assert gf_to_dict([10], 11, symmetric=False) == {0: 10}
118
+
119
+
120
+ def test_gf_from_to_int_poly():
121
+ assert gf_from_int_poly([1, 0, 7, 2, 20], 5) == [1, 0, 2, 2, 0]
122
+ assert gf_to_int_poly([1, 0, 4, 2, 3], 5) == [1, 0, -1, 2, -2]
123
+
124
+ assert gf_to_int_poly([10], 11, symmetric=True) == [-1]
125
+ assert gf_to_int_poly([10], 11, symmetric=False) == [10]
126
+
127
+
128
+ def test_gf_LC():
129
+ assert gf_LC([], ZZ) == 0
130
+ assert gf_LC([1], ZZ) == 1
131
+ assert gf_LC([1, 2], ZZ) == 1
132
+
133
+
134
+ def test_gf_TC():
135
+ assert gf_TC([], ZZ) == 0
136
+ assert gf_TC([1], ZZ) == 1
137
+ assert gf_TC([1, 2], ZZ) == 2
138
+
139
+
140
+ def test_gf_monic():
141
+ assert gf_monic(ZZ.map([]), 11, ZZ) == (0, [])
142
+
143
+ assert gf_monic(ZZ.map([1]), 11, ZZ) == (1, [1])
144
+ assert gf_monic(ZZ.map([2]), 11, ZZ) == (2, [1])
145
+
146
+ assert gf_monic(ZZ.map([1, 2, 3, 4]), 11, ZZ) == (1, [1, 2, 3, 4])
147
+ assert gf_monic(ZZ.map([2, 3, 4, 5]), 11, ZZ) == (2, [1, 7, 2, 8])
148
+
149
+
150
+ def test_gf_arith():
151
+ assert gf_neg([], 11, ZZ) == []
152
+ assert gf_neg([1], 11, ZZ) == [10]
153
+ assert gf_neg([1, 2, 3], 11, ZZ) == [10, 9, 8]
154
+
155
+ assert gf_add_ground([], 0, 11, ZZ) == []
156
+ assert gf_sub_ground([], 0, 11, ZZ) == []
157
+
158
+ assert gf_add_ground([], 3, 11, ZZ) == [3]
159
+ assert gf_sub_ground([], 3, 11, ZZ) == [8]
160
+
161
+ assert gf_add_ground([1], 3, 11, ZZ) == [4]
162
+ assert gf_sub_ground([1], 3, 11, ZZ) == [9]
163
+
164
+ assert gf_add_ground([8], 3, 11, ZZ) == []
165
+ assert gf_sub_ground([3], 3, 11, ZZ) == []
166
+
167
+ assert gf_add_ground([1, 2, 3], 3, 11, ZZ) == [1, 2, 6]
168
+ assert gf_sub_ground([1, 2, 3], 3, 11, ZZ) == [1, 2, 0]
169
+
170
+ assert gf_mul_ground([], 0, 11, ZZ) == []
171
+ assert gf_mul_ground([], 1, 11, ZZ) == []
172
+
173
+ assert gf_mul_ground([1], 0, 11, ZZ) == []
174
+ assert gf_mul_ground([1], 1, 11, ZZ) == [1]
175
+
176
+ assert gf_mul_ground([1, 2, 3], 0, 11, ZZ) == []
177
+ assert gf_mul_ground([1, 2, 3], 1, 11, ZZ) == [1, 2, 3]
178
+ assert gf_mul_ground([1, 2, 3], 7, 11, ZZ) == [7, 3, 10]
179
+
180
+ assert gf_add([], [], 11, ZZ) == []
181
+ assert gf_add([1], [], 11, ZZ) == [1]
182
+ assert gf_add([], [1], 11, ZZ) == [1]
183
+ assert gf_add([1], [1], 11, ZZ) == [2]
184
+ assert gf_add([1], [2], 11, ZZ) == [3]
185
+
186
+ assert gf_add([1, 2], [1], 11, ZZ) == [1, 3]
187
+ assert gf_add([1], [1, 2], 11, ZZ) == [1, 3]
188
+
189
+ assert gf_add([1, 2, 3], [8, 9, 10], 11, ZZ) == [9, 0, 2]
190
+
191
+ assert gf_sub([], [], 11, ZZ) == []
192
+ assert gf_sub([1], [], 11, ZZ) == [1]
193
+ assert gf_sub([], [1], 11, ZZ) == [10]
194
+ assert gf_sub([1], [1], 11, ZZ) == []
195
+ assert gf_sub([1], [2], 11, ZZ) == [10]
196
+
197
+ assert gf_sub([1, 2], [1], 11, ZZ) == [1, 1]
198
+ assert gf_sub([1], [1, 2], 11, ZZ) == [10, 10]
199
+
200
+ assert gf_sub([3, 2, 1], [8, 9, 10], 11, ZZ) == [6, 4, 2]
201
+
202
+ assert gf_add_mul(
203
+ [1, 5, 6], [7, 3], [8, 0, 6, 1], 11, ZZ) == [1, 2, 10, 8, 9]
204
+ assert gf_sub_mul(
205
+ [1, 5, 6], [7, 3], [8, 0, 6, 1], 11, ZZ) == [10, 9, 3, 2, 3]
206
+
207
+ assert gf_mul([], [], 11, ZZ) == []
208
+ assert gf_mul([], [1], 11, ZZ) == []
209
+ assert gf_mul([1], [], 11, ZZ) == []
210
+ assert gf_mul([1], [1], 11, ZZ) == [1]
211
+ assert gf_mul([5], [7], 11, ZZ) == [2]
212
+
213
+ assert gf_mul([3, 0, 0, 6, 1, 2], [4, 0, 1, 0], 11, ZZ) == [1, 0,
214
+ 3, 2, 4, 3, 1, 2, 0]
215
+ assert gf_mul([4, 0, 1, 0], [3, 0, 0, 6, 1, 2], 11, ZZ) == [1, 0,
216
+ 3, 2, 4, 3, 1, 2, 0]
217
+
218
+ assert gf_mul([2, 0, 0, 1, 7], [2, 0, 0, 1, 7], 11, ZZ) == [4, 0,
219
+ 0, 4, 6, 0, 1, 3, 5]
220
+
221
+ assert gf_sqr([], 11, ZZ) == []
222
+ assert gf_sqr([2], 11, ZZ) == [4]
223
+ assert gf_sqr([1, 2], 11, ZZ) == [1, 4, 4]
224
+
225
+ assert gf_sqr([2, 0, 0, 1, 7], 11, ZZ) == [4, 0, 0, 4, 6, 0, 1, 3, 5]
226
+
227
+
228
+ def test_gf_division():
229
+ raises(ZeroDivisionError, lambda: gf_div([1, 2, 3], [], 11, ZZ))
230
+ raises(ZeroDivisionError, lambda: gf_rem([1, 2, 3], [], 11, ZZ))
231
+ raises(ZeroDivisionError, lambda: gf_quo([1, 2, 3], [], 11, ZZ))
232
+ raises(ZeroDivisionError, lambda: gf_quo([1, 2, 3], [], 11, ZZ))
233
+
234
+ assert gf_div([1], [1, 2, 3], 7, ZZ) == ([], [1])
235
+ assert gf_rem([1], [1, 2, 3], 7, ZZ) == [1]
236
+ assert gf_quo([1], [1, 2, 3], 7, ZZ) == []
237
+
238
+ f = ZZ.map([5, 4, 3, 2, 1, 0])
239
+ g = ZZ.map([1, 2, 3])
240
+ q = [5, 1, 0, 6]
241
+ r = [3, 3]
242
+
243
+ assert gf_div(f, g, 7, ZZ) == (q, r)
244
+ assert gf_rem(f, g, 7, ZZ) == r
245
+ assert gf_quo(f, g, 7, ZZ) == q
246
+
247
+ raises(ExactQuotientFailed, lambda: gf_exquo(f, g, 7, ZZ))
248
+
249
+ f = ZZ.map([5, 4, 3, 2, 1, 0])
250
+ g = ZZ.map([1, 2, 3, 0])
251
+ q = [5, 1, 0]
252
+ r = [6, 1, 0]
253
+
254
+ assert gf_div(f, g, 7, ZZ) == (q, r)
255
+ assert gf_rem(f, g, 7, ZZ) == r
256
+ assert gf_quo(f, g, 7, ZZ) == q
257
+
258
+ raises(ExactQuotientFailed, lambda: gf_exquo(f, g, 7, ZZ))
259
+
260
+ assert gf_quo(ZZ.map([1, 2, 1]), ZZ.map([1, 1]), 11, ZZ) == [1, 1]
261
+
262
+
263
+ def test_gf_shift():
264
+ f = [1, 2, 3, 4, 5]
265
+
266
+ assert gf_lshift([], 5, ZZ) == []
267
+ assert gf_rshift([], 5, ZZ) == ([], [])
268
+
269
+ assert gf_lshift(f, 1, ZZ) == [1, 2, 3, 4, 5, 0]
270
+ assert gf_lshift(f, 2, ZZ) == [1, 2, 3, 4, 5, 0, 0]
271
+
272
+ assert gf_rshift(f, 0, ZZ) == (f, [])
273
+ assert gf_rshift(f, 1, ZZ) == ([1, 2, 3, 4], [5])
274
+ assert gf_rshift(f, 3, ZZ) == ([1, 2], [3, 4, 5])
275
+ assert gf_rshift(f, 5, ZZ) == ([], f)
276
+
277
+
278
+ def test_gf_expand():
279
+ F = [([1, 1], 2), ([1, 2], 3)]
280
+
281
+ assert gf_expand(F, 11, ZZ) == [1, 8, 3, 5, 6, 8]
282
+ assert gf_expand((4, F), 11, ZZ) == [4, 10, 1, 9, 2, 10]
283
+
284
+
285
+ def test_gf_powering():
286
+ assert gf_pow([1, 0, 0, 1, 8], 0, 11, ZZ) == [1]
287
+ assert gf_pow([1, 0, 0, 1, 8], 1, 11, ZZ) == [1, 0, 0, 1, 8]
288
+ assert gf_pow([1, 0, 0, 1, 8], 2, 11, ZZ) == [1, 0, 0, 2, 5, 0, 1, 5, 9]
289
+
290
+ assert gf_pow([1, 0, 0, 1, 8], 5, 11, ZZ) == \
291
+ [1, 0, 0, 5, 7, 0, 10, 6, 2, 10, 9, 6, 10, 6, 6, 0, 5, 2, 5, 9, 10]
292
+
293
+ assert gf_pow([1, 0, 0, 1, 8], 8, 11, ZZ) == \
294
+ [1, 0, 0, 8, 9, 0, 6, 8, 10, 1, 2, 5, 10, 7, 7, 9, 1, 2, 0, 0, 6, 2,
295
+ 5, 2, 5, 7, 7, 9, 10, 10, 7, 5, 5]
296
+
297
+ assert gf_pow([1, 0, 0, 1, 8], 45, 11, ZZ) == \
298
+ [ 1, 0, 0, 1, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
299
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 4, 10, 0, 0, 0, 0, 0, 0,
300
+ 10, 0, 0, 10, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
301
+ 6, 0, 0, 6, 4, 0, 0, 0, 0, 0, 0, 8, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0,
302
+ 10, 0, 0, 10, 3, 0, 0, 0, 0, 0, 0, 4, 0, 0, 4, 10, 0, 0, 0, 0, 0, 0,
303
+ 8, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 9, 0, 0, 9, 6, 0, 0, 0, 0, 0, 0,
304
+ 3, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 10, 0, 0, 10, 3, 0, 0, 0, 0, 0, 0,
305
+ 10, 0, 0, 10, 3, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 5, 0, 0, 0, 0, 0, 0,
306
+ 4, 0, 0, 4, 10]
307
+
308
+ assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 0, ZZ.map([2, 0, 7]), 11, ZZ) == [1]
309
+ assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 1, ZZ.map([2, 0, 7]), 11, ZZ) == [1, 1]
310
+ assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 2, ZZ.map([2, 0, 7]), 11, ZZ) == [2, 3]
311
+ assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 5, ZZ.map([2, 0, 7]), 11, ZZ) == [7, 8]
312
+ assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 8, ZZ.map([2, 0, 7]), 11, ZZ) == [1, 5]
313
+ assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 45, ZZ.map([2, 0, 7]), 11, ZZ) == [5, 4]
314
+
315
+
316
+ def test_gf_gcdex():
317
+ assert gf_gcdex(ZZ.map([]), ZZ.map([]), 11, ZZ) == ([1], [], [])
318
+ assert gf_gcdex(ZZ.map([2]), ZZ.map([]), 11, ZZ) == ([6], [], [1])
319
+ assert gf_gcdex(ZZ.map([]), ZZ.map([2]), 11, ZZ) == ([], [6], [1])
320
+ assert gf_gcdex(ZZ.map([2]), ZZ.map([2]), 11, ZZ) == ([], [6], [1])
321
+
322
+ assert gf_gcdex(ZZ.map([]), ZZ.map([3, 0]), 11, ZZ) == ([], [4], [1, 0])
323
+ assert gf_gcdex(ZZ.map([3, 0]), ZZ.map([]), 11, ZZ) == ([4], [], [1, 0])
324
+
325
+ assert gf_gcdex(ZZ.map([3, 0]), ZZ.map([3, 0]), 11, ZZ) == ([], [4], [1, 0])
326
+
327
+ assert gf_gcdex(ZZ.map([1, 8, 7]), ZZ.map([1, 7, 1, 7]), 11, ZZ) == ([5, 6], [6], [1, 7])
328
+
329
+
330
+ def test_gf_gcd():
331
+ assert gf_gcd(ZZ.map([]), ZZ.map([]), 11, ZZ) == []
332
+ assert gf_gcd(ZZ.map([2]), ZZ.map([]), 11, ZZ) == [1]
333
+ assert gf_gcd(ZZ.map([]), ZZ.map([2]), 11, ZZ) == [1]
334
+ assert gf_gcd(ZZ.map([2]), ZZ.map([2]), 11, ZZ) == [1]
335
+
336
+ assert gf_gcd(ZZ.map([]), ZZ.map([1, 0]), 11, ZZ) == [1, 0]
337
+ assert gf_gcd(ZZ.map([1, 0]), ZZ.map([]), 11, ZZ) == [1, 0]
338
+
339
+ assert gf_gcd(ZZ.map([3, 0]), ZZ.map([3, 0]), 11, ZZ) == [1, 0]
340
+ assert gf_gcd(ZZ.map([1, 8, 7]), ZZ.map([1, 7, 1, 7]), 11, ZZ) == [1, 7]
341
+
342
+
343
+ def test_gf_lcm():
344
+ assert gf_lcm(ZZ.map([]), ZZ.map([]), 11, ZZ) == []
345
+ assert gf_lcm(ZZ.map([2]), ZZ.map([]), 11, ZZ) == []
346
+ assert gf_lcm(ZZ.map([]), ZZ.map([2]), 11, ZZ) == []
347
+ assert gf_lcm(ZZ.map([2]), ZZ.map([2]), 11, ZZ) == [1]
348
+
349
+ assert gf_lcm(ZZ.map([]), ZZ.map([1, 0]), 11, ZZ) == []
350
+ assert gf_lcm(ZZ.map([1, 0]), ZZ.map([]), 11, ZZ) == []
351
+
352
+ assert gf_lcm(ZZ.map([3, 0]), ZZ.map([3, 0]), 11, ZZ) == [1, 0]
353
+ assert gf_lcm(ZZ.map([1, 8, 7]), ZZ.map([1, 7, 1, 7]), 11, ZZ) == [1, 8, 8, 8, 7]
354
+
355
+
356
+ def test_gf_cofactors():
357
+ assert gf_cofactors(ZZ.map([]), ZZ.map([]), 11, ZZ) == ([], [], [])
358
+ assert gf_cofactors(ZZ.map([2]), ZZ.map([]), 11, ZZ) == ([1], [2], [])
359
+ assert gf_cofactors(ZZ.map([]), ZZ.map([2]), 11, ZZ) == ([1], [], [2])
360
+ assert gf_cofactors(ZZ.map([2]), ZZ.map([2]), 11, ZZ) == ([1], [2], [2])
361
+
362
+ assert gf_cofactors(ZZ.map([]), ZZ.map([1, 0]), 11, ZZ) == ([1, 0], [], [1])
363
+ assert gf_cofactors(ZZ.map([1, 0]), ZZ.map([]), 11, ZZ) == ([1, 0], [1], [])
364
+
365
+ assert gf_cofactors(ZZ.map([3, 0]), ZZ.map([3, 0]), 11, ZZ) == (
366
+ [1, 0], [3], [3])
367
+ assert gf_cofactors(ZZ.map([1, 8, 7]), ZZ.map([1, 7, 1, 7]), 11, ZZ) == (
368
+ ([1, 7], [1, 1], [1, 0, 1]))
369
+
370
+
371
+ def test_gf_diff():
372
+ assert gf_diff([], 11, ZZ) == []
373
+ assert gf_diff([7], 11, ZZ) == []
374
+
375
+ assert gf_diff([7, 3], 11, ZZ) == [7]
376
+ assert gf_diff([7, 3, 1], 11, ZZ) == [3, 3]
377
+
378
+ assert gf_diff([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 11, ZZ) == []
379
+
380
+
381
+ def test_gf_eval():
382
+ assert gf_eval([], 4, 11, ZZ) == 0
383
+ assert gf_eval([], 27, 11, ZZ) == 0
384
+ assert gf_eval([7], 4, 11, ZZ) == 7
385
+ assert gf_eval([7], 27, 11, ZZ) == 7
386
+
387
+ assert gf_eval([1, 0, 3, 2, 4, 3, 1, 2, 0], 0, 11, ZZ) == 0
388
+ assert gf_eval([1, 0, 3, 2, 4, 3, 1, 2, 0], 4, 11, ZZ) == 9
389
+ assert gf_eval([1, 0, 3, 2, 4, 3, 1, 2, 0], 27, 11, ZZ) == 5
390
+
391
+ assert gf_eval([4, 0, 0, 4, 6, 0, 1, 3, 5], 0, 11, ZZ) == 5
392
+ assert gf_eval([4, 0, 0, 4, 6, 0, 1, 3, 5], 4, 11, ZZ) == 3
393
+ assert gf_eval([4, 0, 0, 4, 6, 0, 1, 3, 5], 27, 11, ZZ) == 9
394
+
395
+ assert gf_multi_eval([3, 2, 1], [0, 1, 2, 3], 11, ZZ) == [1, 6, 6, 1]
396
+
397
+
398
+ def test_gf_compose():
399
+ assert gf_compose([], [1, 0], 11, ZZ) == []
400
+ assert gf_compose_mod([], [1, 0], [1, 0], 11, ZZ) == []
401
+
402
+ assert gf_compose([1], [], 11, ZZ) == [1]
403
+ assert gf_compose([1, 0], [], 11, ZZ) == []
404
+ assert gf_compose([1, 0], [1, 0], 11, ZZ) == [1, 0]
405
+
406
+ f = ZZ.map([1, 1, 4, 9, 1])
407
+ g = ZZ.map([1, 1, 1])
408
+ h = ZZ.map([1, 0, 0, 2])
409
+
410
+ assert gf_compose(g, h, 11, ZZ) == [1, 0, 0, 5, 0, 0, 7]
411
+ assert gf_compose_mod(g, h, f, 11, ZZ) == [3, 9, 6, 10]
412
+
413
+
414
+ def test_gf_trace_map():
415
+ f = ZZ.map([1, 1, 4, 9, 1])
416
+ a = [1, 1, 1]
417
+ c = ZZ.map([1, 0])
418
+ b = gf_pow_mod(c, 11, f, 11, ZZ)
419
+
420
+ assert gf_trace_map(a, b, c, 0, f, 11, ZZ) == \
421
+ ([1, 1, 1], [1, 1, 1])
422
+ assert gf_trace_map(a, b, c, 1, f, 11, ZZ) == \
423
+ ([5, 2, 10, 3], [5, 3, 0, 4])
424
+ assert gf_trace_map(a, b, c, 2, f, 11, ZZ) == \
425
+ ([5, 9, 5, 3], [10, 1, 5, 7])
426
+ assert gf_trace_map(a, b, c, 3, f, 11, ZZ) == \
427
+ ([1, 10, 6, 0], [7])
428
+ assert gf_trace_map(a, b, c, 4, f, 11, ZZ) == \
429
+ ([1, 1, 1], [1, 1, 8])
430
+ assert gf_trace_map(a, b, c, 5, f, 11, ZZ) == \
431
+ ([5, 2, 10, 3], [5, 3, 0, 0])
432
+ assert gf_trace_map(a, b, c, 11, f, 11, ZZ) == \
433
+ ([1, 10, 6, 0], [10])
434
+
435
+
436
+ def test_gf_irreducible():
437
+ assert gf_irreducible_p(gf_irreducible(1, 11, ZZ), 11, ZZ) is True
438
+ assert gf_irreducible_p(gf_irreducible(2, 11, ZZ), 11, ZZ) is True
439
+ assert gf_irreducible_p(gf_irreducible(3, 11, ZZ), 11, ZZ) is True
440
+ assert gf_irreducible_p(gf_irreducible(4, 11, ZZ), 11, ZZ) is True
441
+ assert gf_irreducible_p(gf_irreducible(5, 11, ZZ), 11, ZZ) is True
442
+ assert gf_irreducible_p(gf_irreducible(6, 11, ZZ), 11, ZZ) is True
443
+ assert gf_irreducible_p(gf_irreducible(7, 11, ZZ), 11, ZZ) is True
444
+
445
+
446
+ def test_gf_irreducible_p():
447
+ assert gf_irred_p_ben_or(ZZ.map([7]), 11, ZZ) is True
448
+ assert gf_irred_p_ben_or(ZZ.map([7, 3]), 11, ZZ) is True
449
+ assert gf_irred_p_ben_or(ZZ.map([7, 3, 1]), 11, ZZ) is False
450
+
451
+ assert gf_irred_p_rabin(ZZ.map([7]), 11, ZZ) is True
452
+ assert gf_irred_p_rabin(ZZ.map([7, 3]), 11, ZZ) is True
453
+ assert gf_irred_p_rabin(ZZ.map([7, 3, 1]), 11, ZZ) is False
454
+
455
+ config.setup('GF_IRRED_METHOD', 'ben-or')
456
+
457
+ assert gf_irreducible_p(ZZ.map([7]), 11, ZZ) is True
458
+ assert gf_irreducible_p(ZZ.map([7, 3]), 11, ZZ) is True
459
+ assert gf_irreducible_p(ZZ.map([7, 3, 1]), 11, ZZ) is False
460
+
461
+ config.setup('GF_IRRED_METHOD', 'rabin')
462
+
463
+ assert gf_irreducible_p(ZZ.map([7]), 11, ZZ) is True
464
+ assert gf_irreducible_p(ZZ.map([7, 3]), 11, ZZ) is True
465
+ assert gf_irreducible_p(ZZ.map([7, 3, 1]), 11, ZZ) is False
466
+
467
+ config.setup('GF_IRRED_METHOD', 'other')
468
+ raises(KeyError, lambda: gf_irreducible_p([7], 11, ZZ))
469
+ config.setup('GF_IRRED_METHOD')
470
+
471
+ f = ZZ.map([1, 9, 9, 13, 16, 15, 6, 7, 7, 7, 10])
472
+ g = ZZ.map([1, 7, 16, 7, 15, 13, 13, 11, 16, 10, 9])
473
+
474
+ h = gf_mul(f, g, 17, ZZ)
475
+
476
+ assert gf_irred_p_ben_or(f, 17, ZZ) is True
477
+ assert gf_irred_p_ben_or(g, 17, ZZ) is True
478
+
479
+ assert gf_irred_p_ben_or(h, 17, ZZ) is False
480
+
481
+ assert gf_irred_p_rabin(f, 17, ZZ) is True
482
+ assert gf_irred_p_rabin(g, 17, ZZ) is True
483
+
484
+ assert gf_irred_p_rabin(h, 17, ZZ) is False
485
+
486
+
487
+ def test_gf_squarefree():
488
+ assert gf_sqf_list([], 11, ZZ) == (0, [])
489
+ assert gf_sqf_list([1], 11, ZZ) == (1, [])
490
+ assert gf_sqf_list([1, 1], 11, ZZ) == (1, [([1, 1], 1)])
491
+
492
+ assert gf_sqf_p([], 11, ZZ) is True
493
+ assert gf_sqf_p([1], 11, ZZ) is True
494
+ assert gf_sqf_p([1, 1], 11, ZZ) is True
495
+
496
+ f = gf_from_dict({11: 1, 0: 1}, 11, ZZ)
497
+
498
+ assert gf_sqf_p(f, 11, ZZ) is False
499
+
500
+ assert gf_sqf_list(f, 11, ZZ) == \
501
+ (1, [([1, 1], 11)])
502
+
503
+ f = [1, 5, 8, 4]
504
+
505
+ assert gf_sqf_p(f, 11, ZZ) is False
506
+
507
+ assert gf_sqf_list(f, 11, ZZ) == \
508
+ (1, [([1, 1], 1),
509
+ ([1, 2], 2)])
510
+
511
+ assert gf_sqf_part(f, 11, ZZ) == [1, 3, 2]
512
+
513
+ f = [1, 0, 0, 2, 0, 0, 2, 0, 0, 1, 0]
514
+
515
+ assert gf_sqf_list(f, 3, ZZ) == \
516
+ (1, [([1, 0], 1),
517
+ ([1, 1], 3),
518
+ ([1, 2], 6)])
519
+
520
+ def test_gf_frobenius_map():
521
+ f = ZZ.map([2, 0, 1, 0, 2, 2, 0, 2, 2, 2])
522
+ g = ZZ.map([1,1,0,2,0,1,0,2,0,1])
523
+ p = 3
524
+ b = gf_frobenius_monomial_base(g, p, ZZ)
525
+ h = gf_frobenius_map(f, g, b, p, ZZ)
526
+ h1 = gf_pow_mod(f, p, g, p, ZZ)
527
+ assert h == h1
528
+
529
+
530
+ def test_gf_berlekamp():
531
+ f = gf_from_int_poly([1, -3, 1, -3, -1, -3, 1], 11)
532
+
533
+ Q = [[1, 0, 0, 0, 0, 0],
534
+ [3, 5, 8, 8, 6, 5],
535
+ [3, 6, 6, 1, 10, 0],
536
+ [9, 4, 10, 3, 7, 9],
537
+ [7, 8, 10, 0, 0, 8],
538
+ [8, 10, 7, 8, 10, 8]]
539
+
540
+ V = [[1, 0, 0, 0, 0, 0],
541
+ [0, 1, 1, 1, 1, 0],
542
+ [0, 0, 7, 9, 0, 1]]
543
+
544
+ assert gf_Qmatrix(f, 11, ZZ) == Q
545
+ assert gf_Qbasis(Q, 11, ZZ) == V
546
+
547
+ assert gf_berlekamp(f, 11, ZZ) == \
548
+ [[1, 1], [1, 5, 3], [1, 2, 3, 4]]
549
+
550
+ f = ZZ.map([1, 0, 1, 0, 10, 10, 8, 2, 8])
551
+
552
+ Q = ZZ.map([[1, 0, 0, 0, 0, 0, 0, 0],
553
+ [2, 1, 7, 11, 10, 12, 5, 11],
554
+ [3, 6, 4, 3, 0, 4, 7, 2],
555
+ [4, 3, 6, 5, 1, 6, 2, 3],
556
+ [2, 11, 8, 8, 3, 1, 3, 11],
557
+ [6, 11, 8, 6, 2, 7, 10, 9],
558
+ [5, 11, 7, 10, 0, 11, 7, 12],
559
+ [3, 3, 12, 5, 0, 11, 9, 12]])
560
+
561
+ V = [[1, 0, 0, 0, 0, 0, 0, 0],
562
+ [0, 5, 5, 0, 9, 5, 1, 0],
563
+ [0, 9, 11, 9, 10, 12, 0, 1]]
564
+
565
+ assert gf_Qmatrix(f, 13, ZZ) == Q
566
+ assert gf_Qbasis(Q, 13, ZZ) == V
567
+
568
+ assert gf_berlekamp(f, 13, ZZ) == \
569
+ [[1, 3], [1, 8, 4, 12], [1, 2, 3, 4, 6]]
570
+
571
+
572
+ def test_gf_ddf():
573
+ f = gf_from_dict({15: ZZ(1), 0: ZZ(-1)}, 11, ZZ)
574
+ g = [([1, 0, 0, 0, 0, 10], 1),
575
+ ([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 2)]
576
+
577
+ assert gf_ddf_zassenhaus(f, 11, ZZ) == g
578
+ assert gf_ddf_shoup(f, 11, ZZ) == g
579
+
580
+ f = gf_from_dict({63: ZZ(1), 0: ZZ(1)}, 2, ZZ)
581
+ g = [([1, 1], 1),
582
+ ([1, 1, 1], 2),
583
+ ([1, 1, 1, 1, 1, 1, 1], 3),
584
+ ([1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0,
585
+ 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0,
586
+ 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1], 6)]
587
+
588
+ assert gf_ddf_zassenhaus(f, 2, ZZ) == g
589
+ assert gf_ddf_shoup(f, 2, ZZ) == g
590
+
591
+ f = gf_from_dict({6: ZZ(1), 5: ZZ(-1), 4: ZZ(1), 3: ZZ(1), 1: ZZ(-1)}, 3, ZZ)
592
+ g = [([1, 1, 0], 1),
593
+ ([1, 1, 0, 1, 2], 2)]
594
+
595
+ assert gf_ddf_zassenhaus(f, 3, ZZ) == g
596
+ assert gf_ddf_shoup(f, 3, ZZ) == g
597
+
598
+ f = ZZ.map([1, 2, 5, 26, 677, 436, 791, 325, 456, 24, 577])
599
+ g = [([1, 701], 1),
600
+ ([1, 110, 559, 532, 694, 151, 110, 70, 735, 122], 9)]
601
+
602
+ assert gf_ddf_zassenhaus(f, 809, ZZ) == g
603
+ assert gf_ddf_shoup(f, 809, ZZ) == g
604
+
605
+ p = ZZ(nextprime(int((2**15 * pi).evalf())))
606
+ f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)
607
+ g = [([1, 22730, 68144], 2),
608
+ ([1, 64876, 83977, 10787, 12561, 68608, 52650, 88001, 84356], 4),
609
+ ([1, 15347, 95022, 84569, 94508, 92335], 5)]
610
+
611
+ assert gf_ddf_zassenhaus(f, p, ZZ) == g
612
+ assert gf_ddf_shoup(f, p, ZZ) == g
613
+
614
+
615
+ def test_gf_edf():
616
+ f = ZZ.map([1, 1, 0, 1, 2])
617
+ g = ZZ.map([[1, 0, 1], [1, 1, 2]])
618
+
619
+ assert gf_edf_zassenhaus(f, 2, 3, ZZ) == g
620
+ assert gf_edf_shoup(f, 2, 3, ZZ) == g
621
+
622
+
623
+ def test_issue_23174():
624
+ f = ZZ.map([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
625
+ g = ZZ.map([[1, 0, 0, 1, 1, 1, 0, 0, 1], [1, 1, 1, 0, 1, 0, 1, 1, 1]])
626
+
627
+ assert gf_edf_zassenhaus(f, 8, 2, ZZ) == g
628
+
629
+
630
+ def test_gf_factor():
631
+ assert gf_factor([], 11, ZZ) == (0, [])
632
+ assert gf_factor([1], 11, ZZ) == (1, [])
633
+ assert gf_factor([1, 1], 11, ZZ) == (1, [([1, 1], 1)])
634
+
635
+ assert gf_factor_sqf([], 11, ZZ) == (0, [])
636
+ assert gf_factor_sqf([1], 11, ZZ) == (1, [])
637
+ assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])
638
+
639
+ config.setup('GF_FACTOR_METHOD', 'berlekamp')
640
+
641
+ assert gf_factor_sqf([], 11, ZZ) == (0, [])
642
+ assert gf_factor_sqf([1], 11, ZZ) == (1, [])
643
+ assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])
644
+
645
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
646
+
647
+ assert gf_factor_sqf([], 11, ZZ) == (0, [])
648
+ assert gf_factor_sqf([1], 11, ZZ) == (1, [])
649
+ assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])
650
+
651
+ config.setup('GF_FACTOR_METHOD', 'shoup')
652
+
653
+ assert gf_factor_sqf(ZZ.map([]), 11, ZZ) == (0, [])
654
+ assert gf_factor_sqf(ZZ.map([1]), 11, ZZ) == (1, [])
655
+ assert gf_factor_sqf(ZZ.map([1, 1]), 11, ZZ) == (1, [[1, 1]])
656
+
657
+ f, p = ZZ.map([1, 0, 0, 1, 0]), 2
658
+
659
+ g = (1, [([1, 0], 1),
660
+ ([1, 1], 1),
661
+ ([1, 1, 1], 1)])
662
+
663
+ config.setup('GF_FACTOR_METHOD', 'berlekamp')
664
+ assert gf_factor(f, p, ZZ) == g
665
+
666
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
667
+ assert gf_factor(f, p, ZZ) == g
668
+
669
+ config.setup('GF_FACTOR_METHOD', 'shoup')
670
+ assert gf_factor(f, p, ZZ) == g
671
+
672
+ g = (1, [[1, 0],
673
+ [1, 1],
674
+ [1, 1, 1]])
675
+
676
+ config.setup('GF_FACTOR_METHOD', 'berlekamp')
677
+ assert gf_factor_sqf(f, p, ZZ) == g
678
+
679
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
680
+ assert gf_factor_sqf(f, p, ZZ) == g
681
+
682
+ config.setup('GF_FACTOR_METHOD', 'shoup')
683
+ assert gf_factor_sqf(f, p, ZZ) == g
684
+
685
+ f, p = gf_from_int_poly([1, -3, 1, -3, -1, -3, 1], 11), 11
686
+
687
+ g = (1, [([1, 1], 1),
688
+ ([1, 5, 3], 1),
689
+ ([1, 2, 3, 4], 1)])
690
+
691
+ config.setup('GF_FACTOR_METHOD', 'berlekamp')
692
+ assert gf_factor(f, p, ZZ) == g
693
+
694
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
695
+ assert gf_factor(f, p, ZZ) == g
696
+
697
+ config.setup('GF_FACTOR_METHOD', 'shoup')
698
+ assert gf_factor(f, p, ZZ) == g
699
+
700
+ f, p = [1, 5, 8, 4], 11
701
+
702
+ g = (1, [([1, 1], 1), ([1, 2], 2)])
703
+
704
+ config.setup('GF_FACTOR_METHOD', 'berlekamp')
705
+ assert gf_factor(f, p, ZZ) == g
706
+
707
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
708
+ assert gf_factor(f, p, ZZ) == g
709
+
710
+ config.setup('GF_FACTOR_METHOD', 'shoup')
711
+ assert gf_factor(f, p, ZZ) == g
712
+
713
+ f, p = [1, 1, 10, 1, 0, 10, 10, 10, 0, 0], 11
714
+
715
+ g = (1, [([1, 0], 2), ([1, 9, 5], 1), ([1, 3, 0, 8, 5, 2], 1)])
716
+
717
+ config.setup('GF_FACTOR_METHOD', 'berlekamp')
718
+ assert gf_factor(f, p, ZZ) == g
719
+
720
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
721
+ assert gf_factor(f, p, ZZ) == g
722
+
723
+ config.setup('GF_FACTOR_METHOD', 'shoup')
724
+ assert gf_factor(f, p, ZZ) == g
725
+
726
+ f, p = gf_from_dict({32: 1, 0: 1}, 11, ZZ), 11
727
+
728
+ g = (1, [([1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 10], 1),
729
+ ([1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 10], 1)])
730
+
731
+ config.setup('GF_FACTOR_METHOD', 'berlekamp')
732
+ assert gf_factor(f, p, ZZ) == g
733
+
734
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
735
+ assert gf_factor(f, p, ZZ) == g
736
+
737
+ config.setup('GF_FACTOR_METHOD', 'shoup')
738
+ assert gf_factor(f, p, ZZ) == g
739
+
740
+ f, p = gf_from_dict({32: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11
741
+
742
+ g = (8, [([1, 3], 1),
743
+ ([1, 8], 1),
744
+ ([1, 0, 9], 1),
745
+ ([1, 2, 2], 1),
746
+ ([1, 9, 2], 1),
747
+ ([1, 0, 5, 0, 7], 1),
748
+ ([1, 0, 6, 0, 7], 1),
749
+ ([1, 0, 0, 0, 1, 0, 0, 0, 6], 1),
750
+ ([1, 0, 0, 0, 10, 0, 0, 0, 6], 1)])
751
+
752
+ config.setup('GF_FACTOR_METHOD', 'berlekamp')
753
+ assert gf_factor(f, p, ZZ) == g
754
+
755
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
756
+ assert gf_factor(f, p, ZZ) == g
757
+
758
+ config.setup('GF_FACTOR_METHOD', 'shoup')
759
+ assert gf_factor(f, p, ZZ) == g
760
+
761
+ f, p = gf_from_dict({63: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11
762
+
763
+ g = (8, [([1, 7], 1),
764
+ ([1, 4, 5], 1),
765
+ ([1, 6, 8, 2], 1),
766
+ ([1, 9, 9, 2], 1),
767
+ ([1, 0, 0, 9, 0, 0, 4], 1),
768
+ ([1, 2, 0, 8, 4, 6, 4], 1),
769
+ ([1, 2, 3, 8, 0, 6, 4], 1),
770
+ ([1, 2, 6, 0, 8, 4, 4], 1),
771
+ ([1, 3, 3, 1, 6, 8, 4], 1),
772
+ ([1, 5, 6, 0, 8, 6, 4], 1),
773
+ ([1, 6, 2, 7, 9, 8, 4], 1),
774
+ ([1, 10, 4, 7, 10, 7, 4], 1),
775
+ ([1, 10, 10, 1, 4, 9, 4], 1)])
776
+
777
+ config.setup('GF_FACTOR_METHOD', 'berlekamp')
778
+ assert gf_factor(f, p, ZZ) == g
779
+
780
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
781
+ assert gf_factor(f, p, ZZ) == g
782
+
783
+ config.setup('GF_FACTOR_METHOD', 'shoup')
784
+ assert gf_factor(f, p, ZZ) == g
785
+
786
+ # Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi)
787
+
788
+ p = ZZ(nextprime(int((2**15 * pi).evalf())))
789
+ f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)
790
+
791
+ assert gf_sqf_p(f, p, ZZ) is True
792
+
793
+ g = (1, [([1, 22730, 68144], 1),
794
+ ([1, 81553, 77449, 86810, 4724], 1),
795
+ ([1, 86276, 56779, 14859, 31575], 1),
796
+ ([1, 15347, 95022, 84569, 94508, 92335], 1)])
797
+
798
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
799
+ assert gf_factor(f, p, ZZ) == g
800
+
801
+ config.setup('GF_FACTOR_METHOD', 'shoup')
802
+ assert gf_factor(f, p, ZZ) == g
803
+
804
+ g = (1, [[1, 22730, 68144],
805
+ [1, 81553, 77449, 86810, 4724],
806
+ [1, 86276, 56779, 14859, 31575],
807
+ [1, 15347, 95022, 84569, 94508, 92335]])
808
+
809
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
810
+ assert gf_factor_sqf(f, p, ZZ) == g
811
+
812
+ config.setup('GF_FACTOR_METHOD', 'shoup')
813
+ assert gf_factor_sqf(f, p, ZZ) == g
814
+
815
+ # Shoup polynomials: f = a_0 x**n + a_1 x**(n-1) + ... + a_n
816
+ # (mod p > 2**(n-2) * pi), where a_n = a_{n-1}**2 + 1, a_0 = 1
817
+
818
+ p = ZZ(nextprime(int((2**4 * pi).evalf())))
819
+ f = ZZ.map([1, 2, 5, 26, 41, 39, 38])
820
+
821
+ assert gf_sqf_p(f, p, ZZ) is True
822
+
823
+ g = (1, [([1, 44, 26], 1),
824
+ ([1, 11, 25, 18, 30], 1)])
825
+
826
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
827
+ assert gf_factor(f, p, ZZ) == g
828
+
829
+ config.setup('GF_FACTOR_METHOD', 'shoup')
830
+ assert gf_factor(f, p, ZZ) == g
831
+
832
+ g = (1, [[1, 44, 26],
833
+ [1, 11, 25, 18, 30]])
834
+
835
+ config.setup('GF_FACTOR_METHOD', 'zassenhaus')
836
+ assert gf_factor_sqf(f, p, ZZ) == g
837
+
838
+ config.setup('GF_FACTOR_METHOD', 'shoup')
839
+ assert gf_factor_sqf(f, p, ZZ) == g
840
+
841
+ config.setup('GF_FACTOR_METHOD', 'other')
842
+ raises(KeyError, lambda: gf_factor([1, 1], 11, ZZ))
843
+ config.setup('GF_FACTOR_METHOD')
844
+
845
+
846
+ def test_gf_csolve():
847
+ assert gf_value([1, 7, 2, 4], 11) == 2204
848
+
849
+ assert linear_congruence(4, 3, 5) == [2]
850
+ assert linear_congruence(0, 3, 5) == []
851
+ assert linear_congruence(6, 1, 4) == []
852
+ assert linear_congruence(0, 5, 5) == [0, 1, 2, 3, 4]
853
+ assert linear_congruence(3, 12, 15) == [4, 9, 14]
854
+ assert linear_congruence(6, 0, 18) == [0, 3, 6, 9, 12, 15]
855
+ # _csolve_prime_las_vegas
856
+ assert _csolve_prime_las_vegas([2, 3, 1], 5) == [2, 4]
857
+ assert _csolve_prime_las_vegas([2, 0, 1], 5) == []
858
+ from sympy.ntheory import primerange
859
+ for p in primerange(2, 100):
860
+ # f = x**(p-1) - 1
861
+ f = gf_sub_ground(gf_pow([1, 0], p - 1, p, ZZ), 1, p, ZZ)
862
+ assert _csolve_prime_las_vegas(f, p) == list(range(1, p))
863
+ # with power = 1
864
+ assert csolve_prime([1, 3, 2, 17], 7) == [3]
865
+ assert csolve_prime([1, 3, 1, 5], 5) == [0, 1]
866
+ assert csolve_prime([3, 6, 9, 3], 3) == [0, 1, 2]
867
+ # with power > 1
868
+ assert csolve_prime(
869
+ [1, 1, 223], 3, 4) == [4, 13, 22, 31, 40, 49, 58, 67, 76]
870
+ assert csolve_prime([3, 5, 2, 25], 5, 3) == [16, 50, 99]
871
+ assert csolve_prime([3, 2, 2, 49], 7, 3) == [147, 190, 234]
872
+
873
+ assert gf_csolve([1, 1, 7], 189) == [13, 49, 76, 112, 139, 175]
874
+ assert gf_csolve([1, 3, 4, 1, 30], 60) == [10, 30]
875
+ assert gf_csolve([1, 1, 7], 15) == []
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_groebnertools.py ADDED
@@ -0,0 +1,533 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for Groebner bases. """
2
+
3
+ from sympy.polys.groebnertools import (
4
+ groebner, sig, sig_key,
5
+ lbp, lbp_key, critical_pair,
6
+ cp_key, is_rewritable_or_comparable,
7
+ Sign, Polyn, Num, s_poly, f5_reduce,
8
+ groebner_lcm, groebner_gcd, is_groebner,
9
+ is_reduced
10
+ )
11
+
12
+ from sympy.polys.fglmtools import _representing_matrices
13
+ from sympy.polys.orderings import lex, grlex
14
+
15
+ from sympy.polys.rings import ring, xring
16
+ from sympy.polys.domains import ZZ, QQ
17
+
18
+ from sympy.testing.pytest import slow
19
+ from sympy.polys import polyconfig as config
20
+
21
+ def _do_test_groebner():
22
+ R, x,y = ring("x,y", QQ, lex)
23
+ f = x**2 + 2*x*y**2
24
+ g = x*y + 2*y**3 - 1
25
+
26
+ assert groebner([f, g], R) == [x, y**3 - QQ(1,2)]
27
+
28
+ R, y,x = ring("y,x", QQ, lex)
29
+ f = 2*x**2*y + y**2
30
+ g = 2*x**3 + x*y - 1
31
+
32
+ assert groebner([f, g], R) == [y, x**3 - QQ(1,2)]
33
+
34
+ R, x,y,z = ring("x,y,z", QQ, lex)
35
+ f = x - z**2
36
+ g = y - z**3
37
+
38
+ assert groebner([f, g], R) == [f, g]
39
+
40
+ R, x,y = ring("x,y", QQ, grlex)
41
+ f = x**3 - 2*x*y
42
+ g = x**2*y + x - 2*y**2
43
+
44
+ assert groebner([f, g], R) == [x**2, x*y, -QQ(1,2)*x + y**2]
45
+
46
+ R, x,y,z = ring("x,y,z", QQ, lex)
47
+ f = -x**2 + y
48
+ g = -x**3 + z
49
+
50
+ assert groebner([f, g], R) == [x**2 - y, x*y - z, x*z - y**2, y**3 - z**2]
51
+
52
+ R, x,y,z = ring("x,y,z", QQ, grlex)
53
+ f = -x**2 + y
54
+ g = -x**3 + z
55
+
56
+ assert groebner([f, g], R) == [y**3 - z**2, x**2 - y, x*y - z, x*z - y**2]
57
+
58
+ R, x,y,z = ring("x,y,z", QQ, lex)
59
+ f = -x**2 + z
60
+ g = -x**3 + y
61
+
62
+ assert groebner([f, g], R) == [x**2 - z, x*y - z**2, x*z - y, y**2 - z**3]
63
+
64
+ R, x,y,z = ring("x,y,z", QQ, grlex)
65
+ f = -x**2 + z
66
+ g = -x**3 + y
67
+
68
+ assert groebner([f, g], R) == [-y**2 + z**3, x**2 - z, x*y - z**2, x*z - y]
69
+
70
+ R, x,y,z = ring("x,y,z", QQ, lex)
71
+ f = x - y**2
72
+ g = -y**3 + z
73
+
74
+ assert groebner([f, g], R) == [x - y**2, y**3 - z]
75
+
76
+ R, x,y,z = ring("x,y,z", QQ, grlex)
77
+ f = x - y**2
78
+ g = -y**3 + z
79
+
80
+ assert groebner([f, g], R) == [x**2 - y*z, x*y - z, -x + y**2]
81
+
82
+ R, x,y,z = ring("x,y,z", QQ, lex)
83
+ f = x - z**2
84
+ g = y - z**3
85
+
86
+ assert groebner([f, g], R) == [x - z**2, y - z**3]
87
+
88
+ R, x,y,z = ring("x,y,z", QQ, grlex)
89
+ f = x - z**2
90
+ g = y - z**3
91
+
92
+ assert groebner([f, g], R) == [x**2 - y*z, x*z - y, -x + z**2]
93
+
94
+ R, x,y,z = ring("x,y,z", QQ, lex)
95
+ f = -y**2 + z
96
+ g = x - y**3
97
+
98
+ assert groebner([f, g], R) == [x - y*z, y**2 - z]
99
+
100
+ R, x,y,z = ring("x,y,z", QQ, grlex)
101
+ f = -y**2 + z
102
+ g = x - y**3
103
+
104
+ assert groebner([f, g], R) == [-x**2 + z**3, x*y - z**2, y**2 - z, -x + y*z]
105
+
106
+ R, x,y,z = ring("x,y,z", QQ, lex)
107
+ f = y - z**2
108
+ g = x - z**3
109
+
110
+ assert groebner([f, g], R) == [x - z**3, y - z**2]
111
+
112
+ R, x,y,z = ring("x,y,z", QQ, grlex)
113
+ f = y - z**2
114
+ g = x - z**3
115
+
116
+ assert groebner([f, g], R) == [-x**2 + y**3, x*z - y**2, -x + y*z, -y + z**2]
117
+
118
+ R, x,y,z = ring("x,y,z", QQ, lex)
119
+ f = 4*x**2*y**2 + 4*x*y + 1
120
+ g = x**2 + y**2 - 1
121
+
122
+ assert groebner([f, g], R) == [
123
+ x - 4*y**7 + 8*y**5 - 7*y**3 + 3*y,
124
+ y**8 - 2*y**6 + QQ(3,2)*y**4 - QQ(1,2)*y**2 + QQ(1,16),
125
+ ]
126
+
127
+ def test_groebner_buchberger():
128
+ with config.using(groebner='buchberger'):
129
+ _do_test_groebner()
130
+
131
+ def test_groebner_f5b():
132
+ with config.using(groebner='f5b'):
133
+ _do_test_groebner()
134
+
135
+ def _do_test_benchmark_minpoly():
136
+ R, x,y,z = ring("x,y,z", QQ, lex)
137
+
138
+ F = [x**3 + x + 1, y**2 + y + 1, (x + y) * z - (x**2 + y)]
139
+ G = [x + QQ(155,2067)*z**5 - QQ(355,689)*z**4 + QQ(6062,2067)*z**3 - QQ(3687,689)*z**2 + QQ(6878,2067)*z - QQ(25,53),
140
+ y + QQ(4,53)*z**5 - QQ(91,159)*z**4 + QQ(523,159)*z**3 - QQ(387,53)*z**2 + QQ(1043,159)*z - QQ(308,159),
141
+ z**6 - 7*z**5 + 41*z**4 - 82*z**3 + 89*z**2 - 46*z + 13]
142
+
143
+ assert groebner(F, R) == G
144
+
145
+ def test_benchmark_minpoly_buchberger():
146
+ with config.using(groebner='buchberger'):
147
+ _do_test_benchmark_minpoly()
148
+
149
+ def test_benchmark_minpoly_f5b():
150
+ with config.using(groebner='f5b'):
151
+ _do_test_benchmark_minpoly()
152
+
153
+
154
+ def test_benchmark_coloring():
155
+ V = range(1, 12 + 1)
156
+ E = [(1, 2), (2, 3), (1, 4), (1, 6), (1, 12), (2, 5), (2, 7), (3, 8), (3, 10),
157
+ (4, 11), (4, 9), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10), (10, 11),
158
+ (11, 12), (5, 12), (5, 9), (6, 10), (7, 11), (8, 12), (3, 4)]
159
+
160
+ R, V = xring([ "x%d" % v for v in V ], QQ, lex)
161
+ E = [(V[i - 1], V[j - 1]) for i, j in E]
162
+
163
+ x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12 = V
164
+
165
+ I3 = [x**3 - 1 for x in V]
166
+ Ig = [x**2 + x*y + y**2 for x, y in E]
167
+
168
+ I = I3 + Ig
169
+
170
+ assert groebner(I[:-1], R) == [
171
+ x1 + x11 + x12,
172
+ x2 - x11,
173
+ x3 - x12,
174
+ x4 - x12,
175
+ x5 + x11 + x12,
176
+ x6 - x11,
177
+ x7 - x12,
178
+ x8 + x11 + x12,
179
+ x9 - x11,
180
+ x10 + x11 + x12,
181
+ x11**2 + x11*x12 + x12**2,
182
+ x12**3 - 1,
183
+ ]
184
+
185
+ assert groebner(I, R) == [1]
186
+
187
+
188
+ def _do_test_benchmark_katsura_3():
189
+ R, x0,x1,x2 = ring("x:3", ZZ, lex)
190
+ I = [x0 + 2*x1 + 2*x2 - 1,
191
+ x0**2 + 2*x1**2 + 2*x2**2 - x0,
192
+ 2*x0*x1 + 2*x1*x2 - x1]
193
+
194
+ assert groebner(I, R) == [
195
+ -7 + 7*x0 + 8*x2 + 158*x2**2 - 420*x2**3,
196
+ 7*x1 + 3*x2 - 79*x2**2 + 210*x2**3,
197
+ x2 + x2**2 - 40*x2**3 + 84*x2**4,
198
+ ]
199
+
200
+ R, x0,x1,x2 = ring("x:3", ZZ, grlex)
201
+ I = [ i.set_ring(R) for i in I ]
202
+
203
+ assert groebner(I, R) == [
204
+ 7*x1 + 3*x2 - 79*x2**2 + 210*x2**3,
205
+ -x1 + x2 - 3*x2**2 + 5*x1**2,
206
+ -x1 - 4*x2 + 10*x1*x2 + 12*x2**2,
207
+ -1 + x0 + 2*x1 + 2*x2,
208
+ ]
209
+
210
+ def test_benchmark_katsura3_buchberger():
211
+ with config.using(groebner='buchberger'):
212
+ _do_test_benchmark_katsura_3()
213
+
214
+ def test_benchmark_katsura3_f5b():
215
+ with config.using(groebner='f5b'):
216
+ _do_test_benchmark_katsura_3()
217
+
218
+ def _do_test_benchmark_katsura_4():
219
+ R, x0,x1,x2,x3 = ring("x:4", ZZ, lex)
220
+ I = [x0 + 2*x1 + 2*x2 + 2*x3 - 1,
221
+ x0**2 + 2*x1**2 + 2*x2**2 + 2*x3**2 - x0,
222
+ 2*x0*x1 + 2*x1*x2 + 2*x2*x3 - x1,
223
+ x1**2 + 2*x0*x2 + 2*x1*x3 - x2]
224
+
225
+ assert groebner(I, R) == [
226
+ 5913075*x0 - 159690237696*x3**7 + 31246269696*x3**6 + 27439610544*x3**5 - 6475723368*x3**4 - 838935856*x3**3 + 275119624*x3**2 + 4884038*x3 - 5913075,
227
+ 1971025*x1 - 97197721632*x3**7 + 73975630752*x3**6 - 12121915032*x3**5 - 2760941496*x3**4 + 814792828*x3**3 - 1678512*x3**2 - 9158924*x3,
228
+ 5913075*x2 + 371438283744*x3**7 - 237550027104*x3**6 + 22645939824*x3**5 + 11520686172*x3**4 - 2024910556*x3**3 - 132524276*x3**2 + 30947828*x3,
229
+ 128304*x3**8 - 93312*x3**7 + 15552*x3**6 + 3144*x3**5 -
230
+ 1120*x3**4 + 36*x3**3 + 15*x3**2 - x3,
231
+ ]
232
+
233
+ R, x0,x1,x2,x3 = ring("x:4", ZZ, grlex)
234
+ I = [ i.set_ring(R) for i in I ]
235
+
236
+ assert groebner(I, R) == [
237
+ 393*x1 - 4662*x2**2 + 4462*x2*x3 - 59*x2 + 224532*x3**4 - 91224*x3**3 - 678*x3**2 + 2046*x3,
238
+ -x1 + 196*x2**3 - 21*x2**2 + 60*x2*x3 - 18*x2 - 168*x3**3 + 83*x3**2 - 9*x3,
239
+ -6*x1 + 1134*x2**2*x3 - 189*x2**2 - 466*x2*x3 + 32*x2 - 630*x3**3 + 57*x3**2 + 51*x3,
240
+ 33*x1 + 63*x2**2 + 2268*x2*x3**2 - 188*x2*x3 + 34*x2 + 2520*x3**3 - 849*x3**2 + 3*x3,
241
+ 7*x1**2 - x1 - 7*x2**2 - 24*x2*x3 + 3*x2 - 15*x3**2 + 5*x3,
242
+ 14*x1*x2 - x1 + 14*x2**2 + 18*x2*x3 - 4*x2 + 6*x3**2 - 2*x3,
243
+ 14*x1*x3 - x1 + 7*x2**2 + 32*x2*x3 - 4*x2 + 27*x3**2 - 9*x3,
244
+ x0 + 2*x1 + 2*x2 + 2*x3 - 1,
245
+ ]
246
+
247
+ def test_benchmark_kastura_4_buchberger():
248
+ with config.using(groebner='buchberger'):
249
+ _do_test_benchmark_katsura_4()
250
+
251
+ def test_benchmark_kastura_4_f5b():
252
+ with config.using(groebner='f5b'):
253
+ _do_test_benchmark_katsura_4()
254
+
255
+ def _do_test_benchmark_czichowski():
256
+ R, x,t = ring("x,t", ZZ, lex)
257
+ I = [9*x**8 + 36*x**7 - 32*x**6 - 252*x**5 - 78*x**4 + 468*x**3 + 288*x**2 - 108*x + 9,
258
+ (-72 - 72*t)*x**7 + (-256 - 252*t)*x**6 + (192 + 192*t)*x**5 + (1280 + 1260*t)*x**4 + (312 + 312*t)*x**3 + (-404*t)*x**2 + (-576 - 576*t)*x + 96 + 108*t]
259
+
260
+ assert groebner(I, R) == [
261
+ 3725588592068034903797967297424801242396746870413359539263038139343329273586196480000*x -
262
+ 160420835591776763325581422211936558925462474417709511019228211783493866564923546661604487873*t**7 -
263
+ 1406108495478033395547109582678806497509499966197028487131115097902188374051595011248311352864*t**6 -
264
+ 5241326875850889518164640374668786338033653548841427557880599579174438246266263602956254030352*t**5 -
265
+ 10758917262823299139373269714910672770004760114329943852726887632013485035262879510837043892416*t**4 -
266
+ 13119383576444715672578819534846747735372132018341964647712009275306635391456880068261130581248*t**3 -
267
+ 9491412317016197146080450036267011389660653495578680036574753839055748080962214787557853941760*t**2 -
268
+ 3767520915562795326943800040277726397326609797172964377014046018280260848046603967211258368000*t -
269
+ 632314652371226552085897259159210286886724229880266931574701654721512325555116066073245696000,
270
+ 610733380717522355121*t**8 +
271
+ 6243748742141230639968*t**7 +
272
+ 27761407182086143225024*t**6 +
273
+ 70066148869420956398592*t**5 +
274
+ 109701225644313784229376*t**4 +
275
+ 109009005495588442152960*t**3 +
276
+ 67072101084384786432000*t**2 +
277
+ 23339979742629593088000*t +
278
+ 3513592776846090240000,
279
+ ]
280
+
281
+ R, x,t = ring("x,t", ZZ, grlex)
282
+ I = [ i.set_ring(R) for i in I ]
283
+
284
+ assert groebner(I, R) == [
285
+ 16996618586000601590732959134095643086442*t**3*x -
286
+ 32936701459297092865176560282688198064839*t**3 +
287
+ 78592411049800639484139414821529525782364*t**2*x -
288
+ 120753953358671750165454009478961405619916*t**2 +
289
+ 120988399875140799712152158915653654637280*t*x -
290
+ 144576390266626470824138354942076045758736*t +
291
+ 60017634054270480831259316163620768960*x**2 +
292
+ 61976058033571109604821862786675242894400*x -
293
+ 56266268491293858791834120380427754600960,
294
+ 576689018321912327136790519059646508441672750656050290242749*t**4 +
295
+ 2326673103677477425562248201573604572527893938459296513327336*t**3 +
296
+ 110743790416688497407826310048520299245819959064297990236000*t**2*x +
297
+ 3308669114229100853338245486174247752683277925010505284338016*t**2 +
298
+ 323150205645687941261103426627818874426097912639158572428800*t*x +
299
+ 1914335199925152083917206349978534224695445819017286960055680*t +
300
+ 861662882561803377986838989464278045397192862768588480000*x**2 +
301
+ 235296483281783440197069672204341465480107019878814196672000*x +
302
+ 361850798943225141738895123621685122544503614946436727532800,
303
+ -117584925286448670474763406733005510014188341867*t**3 +
304
+ 68566565876066068463853874568722190223721653044*t**2*x -
305
+ 435970731348366266878180788833437896139920683940*t**2 +
306
+ 196297602447033751918195568051376792491869233408*t*x -
307
+ 525011527660010557871349062870980202067479780112*t +
308
+ 517905853447200553360289634770487684447317120*x**3 +
309
+ 569119014870778921949288951688799397569321920*x**2 +
310
+ 138877356748142786670127389526667463202210102080*x -
311
+ 205109210539096046121625447192779783475018619520,
312
+ -3725142681462373002731339445216700112264527*t**3 +
313
+ 583711207282060457652784180668273817487940*t**2*x -
314
+ 12381382393074485225164741437227437062814908*t**2 +
315
+ 151081054097783125250959636747516827435040*t*x**2 +
316
+ 1814103857455163948531448580501928933873280*t*x -
317
+ 13353115629395094645843682074271212731433648*t +
318
+ 236415091385250007660606958022544983766080*x**2 +
319
+ 1390443278862804663728298060085399578417600*x -
320
+ 4716885828494075789338754454248931750698880,
321
+ ]
322
+
323
+ # NOTE: This is very slow (> 2 minutes on 3.4 GHz) without GMPY
324
+ @slow
325
+ def test_benchmark_czichowski_buchberger():
326
+ with config.using(groebner='buchberger'):
327
+ _do_test_benchmark_czichowski()
328
+
329
+ def test_benchmark_czichowski_f5b():
330
+ with config.using(groebner='f5b'):
331
+ _do_test_benchmark_czichowski()
332
+
333
+ def _do_test_benchmark_cyclic_4():
334
+ R, a,b,c,d = ring("a,b,c,d", ZZ, lex)
335
+
336
+ I = [a + b + c + d,
337
+ a*b + a*d + b*c + b*d,
338
+ a*b*c + a*b*d + a*c*d + b*c*d,
339
+ a*b*c*d - 1]
340
+
341
+ assert groebner(I, R) == [
342
+ 4*a + 3*d**9 - 4*d**5 - 3*d,
343
+ 4*b + 4*c - 3*d**9 + 4*d**5 + 7*d,
344
+ 4*c**2 + 3*d**10 - 4*d**6 - 3*d**2,
345
+ 4*c*d**4 + 4*c - d**9 + 4*d**5 + 5*d, d**12 - d**8 - d**4 + 1
346
+ ]
347
+
348
+ R, a,b,c,d = ring("a,b,c,d", ZZ, grlex)
349
+ I = [ i.set_ring(R) for i in I ]
350
+
351
+ assert groebner(I, R) == [
352
+ 3*b*c - c**2 + d**6 - 3*d**2,
353
+ -b + 3*c**2*d**3 - c - d**5 - 4*d,
354
+ -b + 3*c*d**4 + 2*c + 2*d**5 + 2*d,
355
+ c**4 + 2*c**2*d**2 - d**4 - 2,
356
+ c**3*d + c*d**3 + d**4 + 1,
357
+ b*c**2 - c**3 - c**2*d - 2*c*d**2 - d**3,
358
+ b**2 - c**2, b*d + c**2 + c*d + d**2,
359
+ a + b + c + d
360
+ ]
361
+
362
+ def test_benchmark_cyclic_4_buchberger():
363
+ with config.using(groebner='buchberger'):
364
+ _do_test_benchmark_cyclic_4()
365
+
366
+ def test_benchmark_cyclic_4_f5b():
367
+ with config.using(groebner='f5b'):
368
+ _do_test_benchmark_cyclic_4()
369
+
370
+ def test_sig_key():
371
+ s1 = sig((0,) * 3, 2)
372
+ s2 = sig((1,) * 3, 4)
373
+ s3 = sig((2,) * 3, 2)
374
+
375
+ assert sig_key(s1, lex) > sig_key(s2, lex)
376
+ assert sig_key(s2, lex) < sig_key(s3, lex)
377
+
378
+
379
+ def test_lbp_key():
380
+ R, x,y,z,t = ring("x,y,z,t", ZZ, lex)
381
+
382
+ p1 = lbp(sig((0,) * 4, 3), R.zero, 12)
383
+ p2 = lbp(sig((0,) * 4, 4), R.zero, 13)
384
+ p3 = lbp(sig((0,) * 4, 4), R.zero, 12)
385
+
386
+ assert lbp_key(p1) > lbp_key(p2)
387
+ assert lbp_key(p2) < lbp_key(p3)
388
+
389
+
390
+ def test_critical_pair():
391
+ # from cyclic4 with grlex
392
+ R, x,y,z,t = ring("x,y,z,t", QQ, grlex)
393
+
394
+ p1 = (((0, 0, 0, 0), 4), y*z*t**2 + z**2*t**2 - t**4 - 1, 4)
395
+ q1 = (((0, 0, 0, 0), 2), -y**2 - y*t - z*t - t**2, 2)
396
+
397
+ p2 = (((0, 0, 0, 2), 3), z**3*t**2 + z**2*t**3 - z - t, 5)
398
+ q2 = (((0, 0, 2, 2), 2), y*z + z*t**5 + z*t + t**6, 13)
399
+
400
+ assert critical_pair(p1, q1, R) == (
401
+ ((0, 0, 1, 2), 2), ((0, 0, 1, 2), QQ(-1, 1)), (((0, 0, 0, 0), 2), -y**2 - y*t - z*t - t**2, 2),
402
+ ((0, 1, 0, 0), 4), ((0, 1, 0, 0), QQ(1, 1)), (((0, 0, 0, 0), 4), y*z*t**2 + z**2*t**2 - t**4 - 1, 4)
403
+ )
404
+ assert critical_pair(p2, q2, R) == (
405
+ ((0, 0, 4, 2), 2), ((0, 0, 2, 0), QQ(1, 1)), (((0, 0, 2, 2), 2), y*z + z*t**5 + z*t + t**6, 13),
406
+ ((0, 0, 0, 5), 3), ((0, 0, 0, 3), QQ(1, 1)), (((0, 0, 0, 2), 3), z**3*t**2 + z**2*t**3 - z - t, 5)
407
+ )
408
+
409
+ def test_cp_key():
410
+ # from cyclic4 with grlex
411
+ R, x,y,z,t = ring("x,y,z,t", QQ, grlex)
412
+
413
+ p1 = (((0, 0, 0, 0), 4), y*z*t**2 + z**2*t**2 - t**4 - 1, 4)
414
+ q1 = (((0, 0, 0, 0), 2), -y**2 - y*t - z*t - t**2, 2)
415
+
416
+ p2 = (((0, 0, 0, 2), 3), z**3*t**2 + z**2*t**3 - z - t, 5)
417
+ q2 = (((0, 0, 2, 2), 2), y*z + z*t**5 + z*t + t**6, 13)
418
+
419
+ cp1 = critical_pair(p1, q1, R)
420
+ cp2 = critical_pair(p2, q2, R)
421
+
422
+ assert cp_key(cp1, R) < cp_key(cp2, R)
423
+
424
+ cp1 = critical_pair(p1, p2, R)
425
+ cp2 = critical_pair(q1, q2, R)
426
+
427
+ assert cp_key(cp1, R) < cp_key(cp2, R)
428
+
429
+
430
+ def test_is_rewritable_or_comparable():
431
+ # from katsura4 with grlex
432
+ R, x,y,z,t = ring("x,y,z,t", QQ, grlex)
433
+
434
+ p = lbp(sig((0, 0, 2, 1), 2), R.zero, 2)
435
+ B = [lbp(sig((0, 0, 0, 1), 2), QQ(2,45)*y**2 + QQ(1,5)*y*z + QQ(5,63)*y*t + z**2*t + QQ(4,45)*z**2 + QQ(76,35)*z*t**2 - QQ(32,105)*z*t + QQ(13,7)*t**3 - QQ(13,21)*t**2, 6)]
436
+
437
+ # rewritable:
438
+ assert is_rewritable_or_comparable(Sign(p), Num(p), B) is True
439
+
440
+ p = lbp(sig((0, 1, 1, 0), 2), R.zero, 7)
441
+ B = [lbp(sig((0, 0, 0, 0), 3), QQ(10,3)*y*z + QQ(4,3)*y*t - QQ(1,3)*y + 4*z**2 + QQ(22,3)*z*t - QQ(4,3)*z + 4*t**2 - QQ(4,3)*t, 3)]
442
+
443
+ # comparable:
444
+ assert is_rewritable_or_comparable(Sign(p), Num(p), B) is True
445
+
446
+
447
+ def test_f5_reduce():
448
+ # katsura3 with lex
449
+ R, x,y,z = ring("x,y,z", QQ, lex)
450
+
451
+ F = [(((0, 0, 0), 1), x + 2*y + 2*z - 1, 1),
452
+ (((0, 0, 0), 2), 6*y**2 + 8*y*z - 2*y + 6*z**2 - 2*z, 2),
453
+ (((0, 0, 0), 3), QQ(10,3)*y*z - QQ(1,3)*y + 4*z**2 - QQ(4,3)*z, 3),
454
+ (((0, 0, 1), 2), y + 30*z**3 - QQ(79,7)*z**2 + QQ(3,7)*z, 4),
455
+ (((0, 0, 2), 2), z**4 - QQ(10,21)*z**3 + QQ(1,84)*z**2 + QQ(1,84)*z, 5)]
456
+
457
+ cp = critical_pair(F[0], F[1], R)
458
+ s = s_poly(cp)
459
+
460
+ assert f5_reduce(s, F) == (((0, 2, 0), 1), R.zero, 1)
461
+
462
+ s = lbp(sig(Sign(s)[0], 100), Polyn(s), Num(s))
463
+ assert f5_reduce(s, F) == s
464
+
465
+
466
+ def test_representing_matrices():
467
+ R, x,y = ring("x,y", QQ, grlex)
468
+
469
+ basis = [(0, 0), (0, 1), (1, 0), (1, 1)]
470
+ F = [x**2 - x - 3*y + 1, -2*x + y**2 + y - 1]
471
+
472
+ assert _representing_matrices(basis, F, R) == [
473
+ [[QQ(0, 1), QQ(0, 1),-QQ(1, 1), QQ(3, 1)],
474
+ [QQ(0, 1), QQ(0, 1), QQ(3, 1),-QQ(4, 1)],
475
+ [QQ(1, 1), QQ(0, 1), QQ(1, 1), QQ(6, 1)],
476
+ [QQ(0, 1), QQ(1, 1), QQ(0, 1), QQ(1, 1)]],
477
+ [[QQ(0, 1), QQ(1, 1), QQ(0, 1),-QQ(2, 1)],
478
+ [QQ(1, 1),-QQ(1, 1), QQ(0, 1), QQ(6, 1)],
479
+ [QQ(0, 1), QQ(2, 1), QQ(0, 1), QQ(3, 1)],
480
+ [QQ(0, 1), QQ(0, 1), QQ(1, 1),-QQ(1, 1)]]]
481
+
482
+ def test_groebner_lcm():
483
+ R, x,y,z = ring("x,y,z", ZZ)
484
+
485
+ assert groebner_lcm(x**2 - y**2, x - y) == x**2 - y**2
486
+ assert groebner_lcm(2*x**2 - 2*y**2, 2*x - 2*y) == 2*x**2 - 2*y**2
487
+
488
+ R, x,y,z = ring("x,y,z", QQ)
489
+
490
+ assert groebner_lcm(x**2 - y**2, x - y) == x**2 - y**2
491
+ assert groebner_lcm(2*x**2 - 2*y**2, 2*x - 2*y) == 2*x**2 - 2*y**2
492
+
493
+ R, x,y = ring("x,y", ZZ)
494
+
495
+ assert groebner_lcm(x**2*y, x*y**2) == x**2*y**2
496
+
497
+ f = 2*x*y**5 - 3*x*y**4 - 2*x*y**3 + 3*x*y**2
498
+ g = y**5 - 2*y**3 + y
499
+ 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
500
+
501
+ assert groebner_lcm(f, g) == h
502
+
503
+ f = x**3 - 3*x**2*y - 9*x*y**2 - 5*y**3
504
+ g = x**4 + 6*x**3*y + 12*x**2*y**2 + 10*x*y**3 + 3*y**4
505
+ h = x**5 + x**4*y - 18*x**3*y**2 - 50*x**2*y**3 - 47*x*y**4 - 15*y**5
506
+
507
+ assert groebner_lcm(f, g) == h
508
+
509
+ def test_groebner_gcd():
510
+ R, x,y,z = ring("x,y,z", ZZ)
511
+
512
+ assert groebner_gcd(x**2 - y**2, x - y) == x - y
513
+ assert groebner_gcd(2*x**2 - 2*y**2, 2*x - 2*y) == 2*x - 2*y
514
+
515
+ R, x,y,z = ring("x,y,z", QQ)
516
+
517
+ assert groebner_gcd(x**2 - y**2, x - y) == x - y
518
+ assert groebner_gcd(2*x**2 - 2*y**2, 2*x - 2*y) == x - y
519
+
520
+ def test_is_groebner():
521
+ R, x,y = ring("x,y", QQ, grlex)
522
+ valid_groebner = [x**2, x*y, -QQ(1,2)*x + y**2]
523
+ invalid_groebner = [x**3, x*y, -QQ(1,2)*x + y**2]
524
+ assert is_groebner(valid_groebner, R) is True
525
+ assert is_groebner(invalid_groebner, R) is False
526
+
527
+ def test_is_reduced():
528
+ R, x, y = ring("x,y", QQ, lex)
529
+ f = x**2 + 2*x*y**2
530
+ g = x*y + 2*y**3 - 1
531
+ assert is_reduced([f, g], R) == False
532
+ G = groebner([f, g], R)
533
+ assert is_reduced(G, R) == True
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_heuristicgcd.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.polys.rings import ring
2
+ from sympy.polys.domains import ZZ
3
+ from sympy.polys.heuristicgcd import heugcd
4
+
5
+
6
+ def test_heugcd_univariate_integers():
7
+ R, x = ring("x", ZZ)
8
+
9
+ f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
10
+ g = x**3 + 6*x**2 + 11*x + 6
11
+
12
+ h = x**2 + 3*x + 2
13
+
14
+ cff = x**2 + 5*x + 4
15
+ cfg = x + 3
16
+
17
+ assert heugcd(f, g) == (h, cff, cfg)
18
+
19
+ f = x**4 - 4
20
+ g = x**4 + 4*x**2 + 4
21
+
22
+ h = x**2 + 2
23
+
24
+ cff = x**2 - 2
25
+ cfg = x**2 + 2
26
+
27
+ assert heugcd(f, g) == (h, cff, cfg)
28
+
29
+ f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
30
+ g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
31
+
32
+ h = 1
33
+
34
+ cff = f
35
+ cfg = g
36
+
37
+ assert heugcd(f, g) == (h, cff, cfg)
38
+
39
+ f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \
40
+ + 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \
41
+ + 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \
42
+ + 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \
43
+ - 12278371209708240950316872681744825481125965781519138077173235712*x**21 \
44
+ + 289127344604779611146960547954288113529690984687482920704*x**14 \
45
+ + 19007977035740498977629742919480623972236450681*x**7 \
46
+ + 311973482284542371301330321821976049
47
+
48
+ g = 365431878023781158602430064717380211405897160759702125019136*x**21 \
49
+ + 197599133478719444145775798221171663643171734081650688*x**14 \
50
+ - 9504116979659010018253915765478924103928886144*x**7 \
51
+ - 311973482284542371301330321821976049
52
+
53
+ # TODO: assert heugcd(f, f.diff(x))[0] == g
54
+
55
+ f = 1317378933230047068160*x + 2945748836994210856960
56
+ g = 120352542776360960*x + 269116466014453760
57
+
58
+ h = 120352542776360960*x + 269116466014453760
59
+ cff = 10946
60
+ cfg = 1
61
+
62
+ assert heugcd(f, g) == (h, cff, cfg)
63
+
64
+ def test_heugcd_multivariate_integers():
65
+ R, x, y = ring("x,y", ZZ)
66
+
67
+ f, g = 2*x**2 + 4*x + 2, x + 1
68
+ assert heugcd(f, g) == (x + 1, 2*x + 2, 1)
69
+
70
+ f, g = x + 1, 2*x**2 + 4*x + 2
71
+ assert heugcd(f, g) == (x + 1, 1, 2*x + 2)
72
+
73
+ R, x, y, z, u = ring("x,y,z,u", ZZ)
74
+
75
+ f, g = u**2 + 2*u + 1, 2*u + 2
76
+ assert heugcd(f, g) == (u + 1, u + 1, 2)
77
+
78
+ f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1
79
+ h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1
80
+
81
+ assert heugcd(f, g) == (h, cff, cfg)
82
+ assert heugcd(g, f) == (h, cfg, cff)
83
+
84
+ R, x, y, z = ring("x,y,z", ZZ)
85
+
86
+ f, g, h = R.fateman_poly_F_1()
87
+ H, cff, cfg = heugcd(f, g)
88
+
89
+ assert H == h and H*cff == f and H*cfg == g
90
+
91
+ R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
92
+
93
+ f, g, h = R.fateman_poly_F_1()
94
+ H, cff, cfg = heugcd(f, g)
95
+
96
+ assert H == h and H*cff == f and H*cfg == g
97
+
98
+ R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ)
99
+
100
+ f, g, h = R.fateman_poly_F_1()
101
+ H, cff, cfg = heugcd(f, g)
102
+
103
+ assert H == h and H*cff == f and H*cfg == g
104
+
105
+ R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ)
106
+
107
+ f, g, h = R.fateman_poly_F_1()
108
+ H, cff, cfg = heugcd(f, g)
109
+
110
+ assert H == h and H*cff == f and H*cfg == g
111
+
112
+ R, x, y, z = ring("x,y,z", ZZ)
113
+
114
+ f, g, h = R.fateman_poly_F_2()
115
+ H, cff, cfg = heugcd(f, g)
116
+
117
+ assert H == h and H*cff == f and H*cfg == g
118
+
119
+ f, g, h = R.fateman_poly_F_3()
120
+ H, cff, cfg = heugcd(f, g)
121
+
122
+ assert H == h and H*cff == f and H*cfg == g
123
+
124
+ R, x, y, z, t = ring("x,y,z,t", ZZ)
125
+
126
+ f, g, h = R.fateman_poly_F_3()
127
+ H, cff, cfg = heugcd(f, g)
128
+
129
+ assert H == h and H*cff == f and H*cfg == g
130
+
131
+
132
+ def test_issue_10996():
133
+ R, x, y, z = ring("x,y,z", ZZ)
134
+
135
+ f = 12*x**6*y**7*z**3 - 3*x**4*y**9*z**3 + 12*x**3*y**5*z**4
136
+ g = -48*x**7*y**8*z**3 + 12*x**5*y**10*z**3 - 48*x**5*y**7*z**2 + \
137
+ 36*x**4*y**7*z - 48*x**4*y**6*z**4 + 12*x**3*y**9*z**2 - 48*x**3*y**4 \
138
+ - 9*x**2*y**9*z - 48*x**2*y**5*z**3 + 12*x*y**6 + 36*x*y**5*z**2 - 48*y**2*z
139
+
140
+ H, cff, cfg = heugcd(f, g)
141
+
142
+ assert H == 12*x**3*y**4 - 3*x*y**6 + 12*y**2*z
143
+ assert H*cff == f and H*cfg == g
144
+
145
+
146
+ def test_issue_25793():
147
+ R, x = ring("x", ZZ)
148
+ f = x - 4851 # failure starts for values more than 4850
149
+ g = f*(2*x + 1)
150
+ H, cff, cfg = R.dup_zz_heu_gcd(f, g)
151
+ assert H == f
152
+ # needs a test for dmp, too, that fails in master before this change
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_hypothesis.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from hypothesis import given
2
+ from hypothesis import strategies as st
3
+ from sympy.abc import x
4
+ from sympy.polys.polytools import Poly
5
+
6
+
7
+ def polys(*, nonzero=False, domain="ZZ"):
8
+ # This is a simple strategy, but sufficient the tests below
9
+ elems = {"ZZ": st.integers(), "QQ": st.fractions()}
10
+ coeff_st = st.lists(elems[domain])
11
+ if nonzero:
12
+ coeff_st = coeff_st.filter(any)
13
+ return st.builds(Poly, coeff_st, st.just(x), domain=st.just(domain))
14
+
15
+
16
+ @given(f=polys(), g=polys(), r=polys())
17
+ def test_gcd_hypothesis(f, g, r):
18
+ gcd_1 = f.gcd(g)
19
+ gcd_2 = g.gcd(f)
20
+ assert gcd_1 == gcd_2
21
+
22
+ # multiply by r
23
+ gcd_3 = g.gcd(f + r * g)
24
+ assert gcd_1 == gcd_3
25
+
26
+
27
+ @given(f_z=polys(), g_z=polys(nonzero=True))
28
+ def test_poly_hypothesis_integers(f_z, g_z):
29
+ remainder_z = f_z.rem(g_z)
30
+ assert g_z.degree() >= remainder_z.degree() or remainder_z.degree() == 0
31
+
32
+
33
+ @given(f_q=polys(domain="QQ"), g_q=polys(nonzero=True, domain="QQ"))
34
+ def test_poly_hypothesis_rationals(f_q, g_q):
35
+ remainder_q = f_q.rem(g_q)
36
+ assert g_q.degree() >= remainder_q.degree() or remainder_q.degree() == 0
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_injections.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for functions that inject symbols into the global namespace. """
2
+
3
+ from sympy.polys.rings import vring
4
+ from sympy.polys.fields import vfield
5
+ from sympy.polys.domains import QQ
6
+
7
+ def test_vring():
8
+ ns = {'vring':vring, 'QQ':QQ}
9
+ exec('R = vring("r", QQ)', ns)
10
+ exec('assert r == R.gens[0]', ns)
11
+
12
+ exec('R = vring("rb rbb rcc rzz _rx", QQ)', ns)
13
+ exec('assert rb == R.gens[0]', ns)
14
+ exec('assert rbb == R.gens[1]', ns)
15
+ exec('assert rcc == R.gens[2]', ns)
16
+ exec('assert rzz == R.gens[3]', ns)
17
+ exec('assert _rx == R.gens[4]', ns)
18
+
19
+ exec('R = vring(["rd", "re", "rfg"], QQ)', ns)
20
+ exec('assert rd == R.gens[0]', ns)
21
+ exec('assert re == R.gens[1]', ns)
22
+ exec('assert rfg == R.gens[2]', ns)
23
+
24
+ def test_vfield():
25
+ ns = {'vfield':vfield, 'QQ':QQ}
26
+ exec('F = vfield("f", QQ)', ns)
27
+ exec('assert f == F.gens[0]', ns)
28
+
29
+ exec('F = vfield("fb fbb fcc fzz _fx", QQ)', ns)
30
+ exec('assert fb == F.gens[0]', ns)
31
+ exec('assert fbb == F.gens[1]', ns)
32
+ exec('assert fcc == F.gens[2]', ns)
33
+ exec('assert fzz == F.gens[3]', ns)
34
+ exec('assert _fx == F.gens[4]', ns)
35
+
36
+ exec('F = vfield(["fd", "fe", "ffg"], QQ)', ns)
37
+ exec('assert fd == F.gens[0]', ns)
38
+ exec('assert fe == F.gens[1]', ns)
39
+ exec('assert ffg == F.gens[2]', ns)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_modulargcd.py ADDED
@@ -0,0 +1,325 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.polys.rings import ring
2
+ from sympy.polys.domains import ZZ, QQ, AlgebraicField
3
+ from sympy.polys.modulargcd import (
4
+ modgcd_univariate,
5
+ modgcd_bivariate,
6
+ _chinese_remainder_reconstruction_multivariate,
7
+ modgcd_multivariate,
8
+ _to_ZZ_poly,
9
+ _to_ANP_poly,
10
+ func_field_modgcd,
11
+ _func_field_modgcd_m)
12
+ from sympy.functions.elementary.miscellaneous import sqrt
13
+
14
+
15
+ def test_modgcd_univariate_integers():
16
+ R, x = ring("x", ZZ)
17
+
18
+ f, g = R.zero, R.zero
19
+ assert modgcd_univariate(f, g) == (0, 0, 0)
20
+
21
+ f, g = R.zero, x
22
+ assert modgcd_univariate(f, g) == (x, 0, 1)
23
+ assert modgcd_univariate(g, f) == (x, 1, 0)
24
+
25
+ f, g = R.zero, -x
26
+ assert modgcd_univariate(f, g) == (x, 0, -1)
27
+ assert modgcd_univariate(g, f) == (x, -1, 0)
28
+
29
+ f, g = 2*x, R(2)
30
+ assert modgcd_univariate(f, g) == (2, x, 1)
31
+
32
+ f, g = 2*x + 2, 6*x**2 - 6
33
+ assert modgcd_univariate(f, g) == (2*x + 2, 1, 3*x - 3)
34
+
35
+ f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
36
+ g = x**3 + 6*x**2 + 11*x + 6
37
+
38
+ h = x**2 + 3*x + 2
39
+
40
+ cff = x**2 + 5*x + 4
41
+ cfg = x + 3
42
+
43
+ assert modgcd_univariate(f, g) == (h, cff, cfg)
44
+
45
+ f = x**4 - 4
46
+ g = x**4 + 4*x**2 + 4
47
+
48
+ h = x**2 + 2
49
+
50
+ cff = x**2 - 2
51
+ cfg = x**2 + 2
52
+
53
+ assert modgcd_univariate(f, g) == (h, cff, cfg)
54
+
55
+ f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
56
+ g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
57
+
58
+ h = 1
59
+
60
+ cff = f
61
+ cfg = g
62
+
63
+ assert modgcd_univariate(f, g) == (h, cff, cfg)
64
+
65
+ f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \
66
+ + 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \
67
+ + 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \
68
+ + 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \
69
+ - 12278371209708240950316872681744825481125965781519138077173235712*x**21 \
70
+ + 289127344604779611146960547954288113529690984687482920704*x**14 \
71
+ + 19007977035740498977629742919480623972236450681*x**7 \
72
+ + 311973482284542371301330321821976049
73
+
74
+ g = 365431878023781158602430064717380211405897160759702125019136*x**21 \
75
+ + 197599133478719444145775798221171663643171734081650688*x**14 \
76
+ - 9504116979659010018253915765478924103928886144*x**7 \
77
+ - 311973482284542371301330321821976049
78
+
79
+ assert modgcd_univariate(f, f.diff(x))[0] == g
80
+
81
+ f = 1317378933230047068160*x + 2945748836994210856960
82
+ g = 120352542776360960*x + 269116466014453760
83
+
84
+ h = 120352542776360960*x + 269116466014453760
85
+ cff = 10946
86
+ cfg = 1
87
+
88
+ assert modgcd_univariate(f, g) == (h, cff, cfg)
89
+
90
+
91
+ def test_modgcd_bivariate_integers():
92
+ R, x, y = ring("x,y", ZZ)
93
+
94
+ f, g = R.zero, R.zero
95
+ assert modgcd_bivariate(f, g) == (0, 0, 0)
96
+
97
+ f, g = 2*x, R(2)
98
+ assert modgcd_bivariate(f, g) == (2, x, 1)
99
+
100
+ f, g = x + 2*y, x + y
101
+ assert modgcd_bivariate(f, g) == (1, f, g)
102
+
103
+ f, g = x**2 + 2*x*y + y**2, x**3 + y**3
104
+ assert modgcd_bivariate(f, g) == (x + y, x + y, x**2 - x*y + y**2)
105
+
106
+ f, g = x*y**2 + 2*x*y + x, x*y**3 + x
107
+ assert modgcd_bivariate(f, g) == (x*y + x, y + 1, y**2 - y + 1)
108
+
109
+ f, g = x**2*y**2 + x**2*y + 1, x*y**2 + x*y + 1
110
+ assert modgcd_bivariate(f, g) == (1, f, g)
111
+
112
+ f = 2*x*y**2 + 4*x*y + 2*x + y**2 + 2*y + 1
113
+ g = 2*x*y**3 + 2*x + y**3 + 1
114
+ assert modgcd_bivariate(f, g) == (2*x*y + 2*x + y + 1, y + 1, y**2 - y + 1)
115
+
116
+ f, g = 2*x**2 + 4*x + 2, x + 1
117
+ assert modgcd_bivariate(f, g) == (x + 1, 2*x + 2, 1)
118
+
119
+ f, g = x + 1, 2*x**2 + 4*x + 2
120
+ assert modgcd_bivariate(f, g) == (x + 1, 1, 2*x + 2)
121
+
122
+ f = 2*x**2 + 4*x*y - 2*x - 4*y
123
+ g = x**2 + x - 2
124
+ assert modgcd_bivariate(f, g) == (x - 1, 2*x + 4*y, x + 2)
125
+
126
+ f = 2*x**2 + 2*x*y - 3*x - 3*y
127
+ g = 4*x*y - 2*x + 4*y**2 - 2*y
128
+ assert modgcd_bivariate(f, g) == (x + y, 2*x - 3, 4*y - 2)
129
+
130
+
131
+ def test_chinese_remainder():
132
+ R, x, y = ring("x, y", ZZ)
133
+ p, q = 3, 5
134
+
135
+ hp = x**3*y - x**2 - 1
136
+ hq = -x**3*y - 2*x*y**2 + 2
137
+
138
+ hpq = _chinese_remainder_reconstruction_multivariate(hp, hq, p, q)
139
+
140
+ assert hpq.trunc_ground(p) == hp
141
+ assert hpq.trunc_ground(q) == hq
142
+
143
+ T, z = ring("z", R)
144
+ p, q = 3, 7
145
+
146
+ hp = (x*y + 1)*z**2 + x
147
+ hq = (x**2 - 3*y)*z + 2
148
+
149
+ hpq = _chinese_remainder_reconstruction_multivariate(hp, hq, p, q)
150
+
151
+ assert hpq.trunc_ground(p) == hp
152
+ assert hpq.trunc_ground(q) == hq
153
+
154
+
155
+ def test_modgcd_multivariate_integers():
156
+ R, x, y = ring("x,y", ZZ)
157
+
158
+ f, g = R.zero, R.zero
159
+ assert modgcd_multivariate(f, g) == (0, 0, 0)
160
+
161
+ f, g = 2*x**2 + 4*x + 2, x + 1
162
+ assert modgcd_multivariate(f, g) == (x + 1, 2*x + 2, 1)
163
+
164
+ f, g = x + 1, 2*x**2 + 4*x + 2
165
+ assert modgcd_multivariate(f, g) == (x + 1, 1, 2*x + 2)
166
+
167
+ f = 2*x**2 + 2*x*y - 3*x - 3*y
168
+ g = 4*x*y - 2*x + 4*y**2 - 2*y
169
+ assert modgcd_multivariate(f, g) == (x + y, 2*x - 3, 4*y - 2)
170
+
171
+ f, g = x*y**2 + 2*x*y + x, x*y**3 + x
172
+ assert modgcd_multivariate(f, g) == (x*y + x, y + 1, y**2 - y + 1)
173
+
174
+ f, g = x**2*y**2 + x**2*y + 1, x*y**2 + x*y + 1
175
+ assert modgcd_multivariate(f, g) == (1, f, g)
176
+
177
+ f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
178
+ g = x**3 + 6*x**2 + 11*x + 6
179
+
180
+ h = x**2 + 3*x + 2
181
+
182
+ cff = x**2 + 5*x + 4
183
+ cfg = x + 3
184
+
185
+ assert modgcd_multivariate(f, g) == (h, cff, cfg)
186
+
187
+ R, x, y, z, u = ring("x,y,z,u", ZZ)
188
+
189
+ f, g = x + y + z, -x - y - z - u
190
+ assert modgcd_multivariate(f, g) == (1, f, g)
191
+
192
+ f, g = u**2 + 2*u + 1, 2*u + 2
193
+ assert modgcd_multivariate(f, g) == (u + 1, u + 1, 2)
194
+
195
+ f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1
196
+ h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1
197
+
198
+ assert modgcd_multivariate(f, g) == (h, cff, cfg)
199
+ assert modgcd_multivariate(g, f) == (h, cfg, cff)
200
+
201
+ R, x, y, z = ring("x,y,z", ZZ)
202
+
203
+ f, g = x - y*z, x - y*z
204
+ assert modgcd_multivariate(f, g) == (x - y*z, 1, 1)
205
+
206
+ f, g, h = R.fateman_poly_F_1()
207
+ H, cff, cfg = modgcd_multivariate(f, g)
208
+
209
+ assert H == h and H*cff == f and H*cfg == g
210
+
211
+ R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
212
+
213
+ f, g, h = R.fateman_poly_F_1()
214
+ H, cff, cfg = modgcd_multivariate(f, g)
215
+
216
+ assert H == h and H*cff == f and H*cfg == g
217
+
218
+ R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ)
219
+
220
+ f, g, h = R.fateman_poly_F_1()
221
+ H, cff, cfg = modgcd_multivariate(f, g)
222
+
223
+ assert H == h and H*cff == f and H*cfg == g
224
+
225
+ R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ)
226
+
227
+ f, g, h = R.fateman_poly_F_1()
228
+ H, cff, cfg = modgcd_multivariate(f, g)
229
+
230
+ assert H == h and H*cff == f and H*cfg == g
231
+
232
+ R, x, y, z = ring("x,y,z", ZZ)
233
+
234
+ f, g, h = R.fateman_poly_F_2()
235
+ H, cff, cfg = modgcd_multivariate(f, g)
236
+
237
+ assert H == h and H*cff == f and H*cfg == g
238
+
239
+ f, g, h = R.fateman_poly_F_3()
240
+ H, cff, cfg = modgcd_multivariate(f, g)
241
+
242
+ assert H == h and H*cff == f and H*cfg == g
243
+
244
+ R, x, y, z, t = ring("x,y,z,t", ZZ)
245
+
246
+ f, g, h = R.fateman_poly_F_3()
247
+ H, cff, cfg = modgcd_multivariate(f, g)
248
+
249
+ assert H == h and H*cff == f and H*cfg == g
250
+
251
+
252
+ def test_to_ZZ_ANP_poly():
253
+ A = AlgebraicField(QQ, sqrt(2))
254
+ R, x = ring("x", A)
255
+ f = x*(sqrt(2) + 1)
256
+
257
+ T, x_, z_ = ring("x_, z_", ZZ)
258
+ f_ = x_*z_ + x_
259
+
260
+ assert _to_ZZ_poly(f, T) == f_
261
+ assert _to_ANP_poly(f_, R) == f
262
+
263
+ R, x, t, s = ring("x, t, s", A)
264
+ f = x*t**2 + x*s + sqrt(2)
265
+
266
+ D, t_, s_ = ring("t_, s_", ZZ)
267
+ T, x_, z_ = ring("x_, z_", D)
268
+ f_ = (t_**2 + s_)*x_ + z_
269
+
270
+ assert _to_ZZ_poly(f, T) == f_
271
+ assert _to_ANP_poly(f_, R) == f
272
+
273
+
274
+ def test_modgcd_algebraic_field():
275
+ A = AlgebraicField(QQ, sqrt(2))
276
+ R, x = ring("x", A)
277
+ one = A.one
278
+
279
+ f, g = 2*x, R(2)
280
+ assert func_field_modgcd(f, g) == (one, f, g)
281
+
282
+ f, g = 2*x, R(sqrt(2))
283
+ assert func_field_modgcd(f, g) == (one, f, g)
284
+
285
+ f, g = 2*x + 2, 6*x**2 - 6
286
+ assert func_field_modgcd(f, g) == (x + 1, R(2), 6*x - 6)
287
+
288
+ R, x, y = ring("x, y", A)
289
+
290
+ f, g = x + sqrt(2)*y, x + y
291
+ assert func_field_modgcd(f, g) == (one, f, g)
292
+
293
+ f, g = x*y + sqrt(2)*y**2, R(sqrt(2))*y
294
+ assert func_field_modgcd(f, g) == (y, x + sqrt(2)*y, R(sqrt(2)))
295
+
296
+ f, g = x**2 + 2*sqrt(2)*x*y + 2*y**2, x + sqrt(2)*y
297
+ assert func_field_modgcd(f, g) == (g, g, one)
298
+
299
+ A = AlgebraicField(QQ, sqrt(2), sqrt(3))
300
+ R, x, y, z = ring("x, y, z", A)
301
+
302
+ h = x**2*y**7 + sqrt(6)/21*z
303
+ f, g = h*(27*y**3 + 1), h*(y + x)
304
+ assert func_field_modgcd(f, g) == (h, 27*y**3+1, y+x)
305
+
306
+ h = x**13*y**3 + 1/2*x**10 + 1/sqrt(2)
307
+ f, g = h*(x + 1), h*sqrt(2)/sqrt(3)
308
+ assert func_field_modgcd(f, g) == (h, x + 1, R(sqrt(2)/sqrt(3)))
309
+
310
+ A = AlgebraicField(QQ, sqrt(2)**(-1)*sqrt(3))
311
+ R, x = ring("x", A)
312
+
313
+ f, g = x + 1, x - 1
314
+ assert func_field_modgcd(f, g) == (A.one, f, g)
315
+
316
+
317
+ # when func_field_modgcd supports function fields, this test can be changed
318
+ def test_modgcd_func_field():
319
+ D, t = ring("t", ZZ)
320
+ R, x, z = ring("x, z", D)
321
+
322
+ minpoly = (z**2*t**2 + z**2*t - 1).drop(0)
323
+ f, g = x + 1, x - 1
324
+
325
+ assert _func_field_modgcd_m(f, g, minpoly) == R.one
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_monomials.py ADDED
@@ -0,0 +1,269 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for tools and arithmetics for monomials of distributed polynomials. """
2
+
3
+ from sympy.polys.monomials import (
4
+ itermonomials, monomial_count,
5
+ monomial_mul, monomial_div,
6
+ monomial_gcd, monomial_lcm,
7
+ monomial_max, monomial_min,
8
+ monomial_divides, monomial_pow,
9
+ Monomial,
10
+ )
11
+
12
+ from sympy.polys.polyerrors import ExactQuotientFailed
13
+
14
+ from sympy.abc import a, b, c, x, y, z
15
+ from sympy.core import S, symbols
16
+ from sympy.testing.pytest import raises
17
+
18
+ def test_monomials():
19
+
20
+ # total_degree tests
21
+ assert set(itermonomials([], 0)) == {S.One}
22
+ assert set(itermonomials([], 1)) == {S.One}
23
+ assert set(itermonomials([], 2)) == {S.One}
24
+
25
+ assert set(itermonomials([], 0, 0)) == {S.One}
26
+ assert set(itermonomials([], 1, 0)) == {S.One}
27
+ assert set(itermonomials([], 2, 0)) == {S.One}
28
+
29
+ raises(StopIteration, lambda: next(itermonomials([], 0, 1)))
30
+ raises(StopIteration, lambda: next(itermonomials([], 0, 2)))
31
+ raises(StopIteration, lambda: next(itermonomials([], 0, 3)))
32
+
33
+ assert set(itermonomials([], 0, 1)) == set()
34
+ assert set(itermonomials([], 0, 2)) == set()
35
+ assert set(itermonomials([], 0, 3)) == set()
36
+
37
+ raises(ValueError, lambda: set(itermonomials([], -1)))
38
+ raises(ValueError, lambda: set(itermonomials([x], -1)))
39
+ raises(ValueError, lambda: set(itermonomials([x, y], -1)))
40
+
41
+ assert set(itermonomials([x], 0)) == {S.One}
42
+ assert set(itermonomials([x], 1)) == {S.One, x}
43
+ assert set(itermonomials([x], 2)) == {S.One, x, x**2}
44
+ assert set(itermonomials([x], 3)) == {S.One, x, x**2, x**3}
45
+
46
+ assert set(itermonomials([x, y], 0)) == {S.One}
47
+ assert set(itermonomials([x, y], 1)) == {S.One, x, y}
48
+ assert set(itermonomials([x, y], 2)) == {S.One, x, y, x**2, y**2, x*y}
49
+ assert set(itermonomials([x, y], 3)) == \
50
+ {S.One, x, y, x**2, x**3, y**2, y**3, x*y, x*y**2, y*x**2}
51
+
52
+ i, j, k = symbols('i j k', commutative=False)
53
+ assert set(itermonomials([i, j, k], 0)) == {S.One}
54
+ assert set(itermonomials([i, j, k], 1)) == {S.One, i, j, k}
55
+ assert set(itermonomials([i, j, k], 2)) == \
56
+ {S.One, i, j, k, i**2, j**2, k**2, i*j, i*k, j*i, j*k, k*i, k*j}
57
+
58
+ assert set(itermonomials([i, j, k], 3)) == \
59
+ {S.One, i, j, k, i**2, j**2, k**2, i*j, i*k, j*i, j*k, k*i, k*j,
60
+ i**3, j**3, k**3,
61
+ i**2 * j, i**2 * k, j * i**2, k * i**2,
62
+ j**2 * i, j**2 * k, i * j**2, k * j**2,
63
+ k**2 * i, k**2 * j, i * k**2, j * k**2,
64
+ i*j*i, i*k*i, j*i*j, j*k*j, k*i*k, k*j*k,
65
+ i*j*k, i*k*j, j*i*k, j*k*i, k*i*j, k*j*i,
66
+ }
67
+
68
+ assert set(itermonomials([x, i, j], 0)) == {S.One}
69
+ assert set(itermonomials([x, i, j], 1)) == {S.One, x, i, j}
70
+ assert set(itermonomials([x, i, j], 2)) == {S.One, x, i, j, x*i, x*j, i*j, j*i, x**2, i**2, j**2}
71
+ assert set(itermonomials([x, i, j], 3)) == \
72
+ {S.One, x, i, j, x*i, x*j, i*j, j*i, x**2, i**2, j**2,
73
+ x**3, i**3, j**3,
74
+ x**2 * i, x**2 * j,
75
+ x * i**2, j * i**2, i**2 * j, i*j*i,
76
+ x * j**2, i * j**2, j**2 * i, j*i*j,
77
+ x * i * j, x * j * i
78
+ }
79
+
80
+ # degree_list tests
81
+ assert set(itermonomials([], [])) == {S.One}
82
+
83
+ raises(ValueError, lambda: set(itermonomials([], [0])))
84
+ raises(ValueError, lambda: set(itermonomials([], [1])))
85
+ raises(ValueError, lambda: set(itermonomials([], [2])))
86
+
87
+ raises(ValueError, lambda: set(itermonomials([x], [1], [])))
88
+ raises(ValueError, lambda: set(itermonomials([x], [1, 2], [])))
89
+ raises(ValueError, lambda: set(itermonomials([x], [1, 2, 3], [])))
90
+
91
+ raises(ValueError, lambda: set(itermonomials([x], [], [1])))
92
+ raises(ValueError, lambda: set(itermonomials([x], [], [1, 2])))
93
+ raises(ValueError, lambda: set(itermonomials([x], [], [1, 2, 3])))
94
+
95
+ raises(ValueError, lambda: set(itermonomials([x, y], [1, 2], [1, 2, 3])))
96
+ raises(ValueError, lambda: set(itermonomials([x, y, z], [1, 2, 3], [0, 1])))
97
+
98
+ raises(ValueError, lambda: set(itermonomials([x], [1], [-1])))
99
+ raises(ValueError, lambda: set(itermonomials([x, y], [1, 2], [1, -1])))
100
+
101
+ raises(ValueError, lambda: set(itermonomials([], [], 1)))
102
+ raises(ValueError, lambda: set(itermonomials([], [], 2)))
103
+ raises(ValueError, lambda: set(itermonomials([], [], 3)))
104
+
105
+ raises(ValueError, lambda: set(itermonomials([x, y], [0, 1], [1, 2])))
106
+ raises(ValueError, lambda: set(itermonomials([x, y, z], [0, 0, 3], [0, 1, 2])))
107
+
108
+ assert set(itermonomials([x], [0])) == {S.One}
109
+ assert set(itermonomials([x], [1])) == {S.One, x}
110
+ assert set(itermonomials([x], [2])) == {S.One, x, x**2}
111
+ assert set(itermonomials([x], [3])) == {S.One, x, x**2, x**3}
112
+
113
+ assert set(itermonomials([x], [3], [1])) == {x, x**3, x**2}
114
+ assert set(itermonomials([x], [3], [2])) == {x**3, x**2}
115
+
116
+ assert set(itermonomials([x, y], 3, 3)) == {x**3, x**2*y, x*y**2, y**3}
117
+ assert set(itermonomials([x, y], 3, 2)) == {x**2, x*y, y**2, x**3, x**2*y, x*y**2, y**3}
118
+
119
+ assert set(itermonomials([x, y], [0, 0])) == {S.One}
120
+ assert set(itermonomials([x, y], [0, 1])) == {S.One, y}
121
+ assert set(itermonomials([x, y], [0, 2])) == {S.One, y, y**2}
122
+ assert set(itermonomials([x, y], [0, 2], [0, 1])) == {y, y**2}
123
+ assert set(itermonomials([x, y], [0, 2], [0, 2])) == {y**2}
124
+
125
+ assert set(itermonomials([x, y], [1, 0])) == {S.One, x}
126
+ assert set(itermonomials([x, y], [1, 1])) == {S.One, x, y, x*y}
127
+ assert set(itermonomials([x, y], [1, 2])) == {S.One, x, y, x*y, y**2, x*y**2}
128
+ assert set(itermonomials([x, y], [1, 2], [1, 1])) == {x*y, x*y**2}
129
+ assert set(itermonomials([x, y], [1, 2], [1, 2])) == {x*y**2}
130
+
131
+ assert set(itermonomials([x, y], [2, 0])) == {S.One, x, x**2}
132
+ assert set(itermonomials([x, y], [2, 1])) == {S.One, x, y, x*y, x**2, x**2*y}
133
+ assert set(itermonomials([x, y], [2, 2])) == \
134
+ {S.One, y**2, x*y**2, x, x*y, x**2, x**2*y**2, y, x**2*y}
135
+
136
+ i, j, k = symbols('i j k', commutative=False)
137
+ assert set(itermonomials([i, j, k], 2, 2)) == \
138
+ {k*i, i**2, i*j, j*k, j*i, k**2, j**2, k*j, i*k}
139
+ assert set(itermonomials([i, j, k], 3, 2)) == \
140
+ {j*k**2, i*k**2, k*i*j, k*i**2, k**2, j*k*j, k*j**2, i*k*i, i*j,
141
+ j**2*k, i**2*j, j*i*k, j**3, i**3, k*j*i, j*k*i, j*i,
142
+ k**2*j, j*i**2, k*j, k*j*k, i*j*i, j*i*j, i*j**2, j**2,
143
+ k*i*k, i**2, j*k, i*k, i*k*j, k**3, i**2*k, j**2*i, k**2*i,
144
+ i*j*k, k*i
145
+ }
146
+ assert set(itermonomials([i, j, k], [0, 0, 0])) == {S.One}
147
+ assert set(itermonomials([i, j, k], [0, 0, 1])) == {1, k}
148
+ assert set(itermonomials([i, j, k], [0, 1, 0])) == {1, j}
149
+ assert set(itermonomials([i, j, k], [1, 0, 0])) == {i, 1}
150
+ assert set(itermonomials([i, j, k], [0, 0, 2])) == {k**2, 1, k}
151
+ assert set(itermonomials([i, j, k], [0, 2, 0])) == {1, j, j**2}
152
+ assert set(itermonomials([i, j, k], [2, 0, 0])) == {i, 1, i**2}
153
+ assert set(itermonomials([i, j, k], [1, 1, 1])) == {1, k, j, j*k, i*k, i, i*j, i*j*k}
154
+ assert set(itermonomials([i, j, k], [2, 2, 2])) == \
155
+ {1, k, i**2*k**2, j*k, j**2, i, i*k, j*k**2, i*j**2*k**2,
156
+ i**2*j, i**2*j**2, k**2, j**2*k, i*j**2*k,
157
+ j**2*k**2, i*j, i**2*k, i**2*j**2*k, j, i**2*j*k,
158
+ i*j**2, i*k**2, i*j*k, i**2*j**2*k**2, i*j*k**2, i**2, i**2*j*k**2
159
+ }
160
+
161
+ assert set(itermonomials([x, j, k], [0, 0, 0])) == {S.One}
162
+ assert set(itermonomials([x, j, k], [0, 0, 1])) == {1, k}
163
+ assert set(itermonomials([x, j, k], [0, 1, 0])) == {1, j}
164
+ assert set(itermonomials([x, j, k], [1, 0, 0])) == {x, 1}
165
+ assert set(itermonomials([x, j, k], [0, 0, 2])) == {k**2, 1, k}
166
+ assert set(itermonomials([x, j, k], [0, 2, 0])) == {1, j, j**2}
167
+ assert set(itermonomials([x, j, k], [2, 0, 0])) == {x, 1, x**2}
168
+ assert set(itermonomials([x, j, k], [1, 1, 1])) == {1, k, j, j*k, x*k, x, x*j, x*j*k}
169
+ assert set(itermonomials([x, j, k], [2, 2, 2])) == \
170
+ {1, k, x**2*k**2, j*k, j**2, x, x*k, j*k**2, x*j**2*k**2,
171
+ x**2*j, x**2*j**2, k**2, j**2*k, x*j**2*k,
172
+ j**2*k**2, x*j, x**2*k, x**2*j**2*k, j, x**2*j*k,
173
+ x*j**2, x*k**2, x*j*k, x**2*j**2*k**2, x*j*k**2, x**2, x**2*j*k**2
174
+ }
175
+
176
+ def test_monomial_count():
177
+ assert monomial_count(2, 2) == 6
178
+ assert monomial_count(2, 3) == 10
179
+
180
+ def test_monomial_mul():
181
+ assert monomial_mul((3, 4, 1), (1, 2, 0)) == (4, 6, 1)
182
+
183
+ def test_monomial_div():
184
+ assert monomial_div((3, 4, 1), (1, 2, 0)) == (2, 2, 1)
185
+
186
+ def test_monomial_gcd():
187
+ assert monomial_gcd((3, 4, 1), (1, 2, 0)) == (1, 2, 0)
188
+
189
+ def test_monomial_lcm():
190
+ assert monomial_lcm((3, 4, 1), (1, 2, 0)) == (3, 4, 1)
191
+
192
+ def test_monomial_max():
193
+ assert monomial_max((3, 4, 5), (0, 5, 1), (6, 3, 9)) == (6, 5, 9)
194
+
195
+ def test_monomial_pow():
196
+ assert monomial_pow((1, 2, 3), 3) == (3, 6, 9)
197
+
198
+ def test_monomial_min():
199
+ assert monomial_min((3, 4, 5), (0, 5, 1), (6, 3, 9)) == (0, 3, 1)
200
+
201
+ def test_monomial_divides():
202
+ assert monomial_divides((1, 2, 3), (4, 5, 6)) is True
203
+ assert monomial_divides((1, 2, 3), (0, 5, 6)) is False
204
+
205
+ def test_Monomial():
206
+ m = Monomial((3, 4, 1), (x, y, z))
207
+ n = Monomial((1, 2, 0), (x, y, z))
208
+
209
+ assert m.as_expr() == x**3*y**4*z
210
+ assert n.as_expr() == x**1*y**2
211
+
212
+ assert m.as_expr(a, b, c) == a**3*b**4*c
213
+ assert n.as_expr(a, b, c) == a**1*b**2
214
+
215
+ assert m.exponents == (3, 4, 1)
216
+ assert m.gens == (x, y, z)
217
+
218
+ assert n.exponents == (1, 2, 0)
219
+ assert n.gens == (x, y, z)
220
+
221
+ assert m == (3, 4, 1)
222
+ assert n != (3, 4, 1)
223
+ assert m != (1, 2, 0)
224
+ assert n == (1, 2, 0)
225
+ assert (m == 1) is False
226
+
227
+ assert m[0] == m[-3] == 3
228
+ assert m[1] == m[-2] == 4
229
+ assert m[2] == m[-1] == 1
230
+
231
+ assert n[0] == n[-3] == 1
232
+ assert n[1] == n[-2] == 2
233
+ assert n[2] == n[-1] == 0
234
+
235
+ assert m[:2] == (3, 4)
236
+ assert n[:2] == (1, 2)
237
+
238
+ assert m*n == Monomial((4, 6, 1))
239
+ assert m/n == Monomial((2, 2, 1))
240
+
241
+ assert m*(1, 2, 0) == Monomial((4, 6, 1))
242
+ assert m/(1, 2, 0) == Monomial((2, 2, 1))
243
+
244
+ assert m.gcd(n) == Monomial((1, 2, 0))
245
+ assert m.lcm(n) == Monomial((3, 4, 1))
246
+
247
+ assert m.gcd((1, 2, 0)) == Monomial((1, 2, 0))
248
+ assert m.lcm((1, 2, 0)) == Monomial((3, 4, 1))
249
+
250
+ assert m**0 == Monomial((0, 0, 0))
251
+ assert m**1 == m
252
+ assert m**2 == Monomial((6, 8, 2))
253
+ assert m**3 == Monomial((9, 12, 3))
254
+ _a = Monomial((0, 0, 0))
255
+ for n in range(10):
256
+ assert _a == m**n
257
+ _a *= m
258
+
259
+ raises(ExactQuotientFailed, lambda: m/Monomial((5, 2, 0)))
260
+
261
+ mm = Monomial((1, 2, 3))
262
+ raises(ValueError, lambda: mm.as_expr())
263
+ assert str(mm) == 'Monomial((1, 2, 3))'
264
+ assert str(m) == 'x**3*y**4*z**1'
265
+ raises(NotImplementedError, lambda: m*1)
266
+ raises(NotImplementedError, lambda: m/1)
267
+ raises(ValueError, lambda: m**-1)
268
+ raises(TypeError, lambda: m.gcd(3))
269
+ raises(TypeError, lambda: m.lcm(3))
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_multivariate_resultants.py ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for Dixon's and Macaulay's classes. """
2
+
3
+ from sympy.matrices.dense import Matrix
4
+ from sympy.polys.polytools import factor
5
+ from sympy.core import symbols
6
+ from sympy.tensor.indexed import IndexedBase
7
+
8
+ from sympy.polys.multivariate_resultants import (DixonResultant,
9
+ MacaulayResultant)
10
+
11
+ c, d = symbols("a, b")
12
+ x, y = symbols("x, y")
13
+
14
+ p = c * x + y
15
+ q = x + d * y
16
+
17
+ dixon = DixonResultant(polynomials=[p, q], variables=[x, y])
18
+ macaulay = MacaulayResultant(polynomials=[p, q], variables=[x, y])
19
+
20
+ def test_dixon_resultant_init():
21
+ """Test init method of DixonResultant."""
22
+ a = IndexedBase("alpha")
23
+
24
+ assert dixon.polynomials == [p, q]
25
+ assert dixon.variables == [x, y]
26
+ assert dixon.n == 2
27
+ assert dixon.m == 2
28
+ assert dixon.dummy_variables == [a[0], a[1]]
29
+
30
+ def test_get_dixon_polynomial_numerical():
31
+ """Test Dixon's polynomial for a numerical example."""
32
+ a = IndexedBase("alpha")
33
+
34
+ p = x + y
35
+ q = x ** 2 + y **3
36
+ h = x ** 2 + y
37
+
38
+ dixon = DixonResultant([p, q, h], [x, y])
39
+ polynomial = -x * y ** 2 * a[0] - x * y ** 2 * a[1] - x * y * a[0] \
40
+ * a[1] - x * y * a[1] ** 2 - x * a[0] * a[1] ** 2 + x * a[0] - \
41
+ y ** 2 * a[0] * a[1] + y ** 2 * a[1] - y * a[0] * a[1] ** 2 + y * \
42
+ a[1] ** 2
43
+
44
+ assert dixon.get_dixon_polynomial().as_expr().expand() == polynomial
45
+
46
+ def test_get_max_degrees():
47
+ """Tests max degrees function."""
48
+
49
+ p = x + y
50
+ q = x ** 2 + y **3
51
+ h = x ** 2 + y
52
+
53
+ dixon = DixonResultant(polynomials=[p, q, h], variables=[x, y])
54
+ dixon_polynomial = dixon.get_dixon_polynomial()
55
+
56
+ assert dixon.get_max_degrees(dixon_polynomial) == [1, 2]
57
+
58
+ def test_get_dixon_matrix():
59
+ """Test Dixon's resultant for a numerical example."""
60
+
61
+ x, y = symbols('x, y')
62
+
63
+ p = x + y
64
+ q = x ** 2 + y ** 3
65
+ h = x ** 2 + y
66
+
67
+ dixon = DixonResultant([p, q, h], [x, y])
68
+ polynomial = dixon.get_dixon_polynomial()
69
+
70
+ assert dixon.get_dixon_matrix(polynomial).det() == 0
71
+
72
+ def test_get_dixon_matrix_example_two():
73
+ """Test Dixon's matrix for example from [Palancz08]_."""
74
+ x, y, z = symbols('x, y, z')
75
+
76
+ f = x ** 2 + y ** 2 - 1 + z * 0
77
+ g = x ** 2 + z ** 2 - 1 + y * 0
78
+ h = y ** 2 + z ** 2 - 1
79
+
80
+ example_two = DixonResultant([f, g, h], [y, z])
81
+ poly = example_two.get_dixon_polynomial()
82
+ matrix = example_two.get_dixon_matrix(poly)
83
+
84
+ expr = 1 - 8 * x ** 2 + 24 * x ** 4 - 32 * x ** 6 + 16 * x ** 8
85
+ assert (matrix.det() - expr).expand() == 0
86
+
87
+ def test_KSY_precondition():
88
+ """Tests precondition for KSY Resultant."""
89
+ A, B, C = symbols('A, B, C')
90
+
91
+ m1 = Matrix([[1, 2, 3],
92
+ [4, 5, 12],
93
+ [6, 7, 18]])
94
+
95
+ m2 = Matrix([[0, C**2],
96
+ [-2 * C, -C ** 2]])
97
+
98
+ m3 = Matrix([[1, 0],
99
+ [0, 1]])
100
+
101
+ m4 = Matrix([[A**2, 0, 1],
102
+ [A, 1, 1 / A]])
103
+
104
+ m5 = Matrix([[5, 1],
105
+ [2, B],
106
+ [0, 1],
107
+ [0, 0]])
108
+
109
+ assert dixon.KSY_precondition(m1) == False
110
+ assert dixon.KSY_precondition(m2) == True
111
+ assert dixon.KSY_precondition(m3) == True
112
+ assert dixon.KSY_precondition(m4) == False
113
+ assert dixon.KSY_precondition(m5) == True
114
+
115
+ def test_delete_zero_rows_and_columns():
116
+ """Tests method for deleting rows and columns containing only zeros."""
117
+ A, B, C = symbols('A, B, C')
118
+
119
+ m1 = Matrix([[0, 0],
120
+ [0, 0],
121
+ [1, 2]])
122
+
123
+ m2 = Matrix([[0, 1, 2],
124
+ [0, 3, 4],
125
+ [0, 5, 6]])
126
+
127
+ m3 = Matrix([[0, 0, 0, 0],
128
+ [0, 1, 2, 0],
129
+ [0, 3, 4, 0],
130
+ [0, 0, 0, 0]])
131
+
132
+ m4 = Matrix([[1, 0, 2],
133
+ [0, 0, 0],
134
+ [3, 0, 4]])
135
+
136
+ m5 = Matrix([[0, 0, 0, 1],
137
+ [0, 0, 0, 2],
138
+ [0, 0, 0, 3],
139
+ [0, 0, 0, 4]])
140
+
141
+ m6 = Matrix([[0, 0, A],
142
+ [B, 0, 0],
143
+ [0, 0, C]])
144
+
145
+ assert dixon.delete_zero_rows_and_columns(m1) == Matrix([[1, 2]])
146
+
147
+ assert dixon.delete_zero_rows_and_columns(m2) == Matrix([[1, 2],
148
+ [3, 4],
149
+ [5, 6]])
150
+
151
+ assert dixon.delete_zero_rows_and_columns(m3) == Matrix([[1, 2],
152
+ [3, 4]])
153
+
154
+ assert dixon.delete_zero_rows_and_columns(m4) == Matrix([[1, 2],
155
+ [3, 4]])
156
+
157
+ assert dixon.delete_zero_rows_and_columns(m5) == Matrix([[1],
158
+ [2],
159
+ [3],
160
+ [4]])
161
+
162
+ assert dixon.delete_zero_rows_and_columns(m6) == Matrix([[0, A],
163
+ [B, 0],
164
+ [0, C]])
165
+
166
+ def test_product_leading_entries():
167
+ """Tests product of leading entries method."""
168
+ A, B = symbols('A, B')
169
+
170
+ m1 = Matrix([[1, 2, 3],
171
+ [0, 4, 5],
172
+ [0, 0, 6]])
173
+
174
+ m2 = Matrix([[0, 0, 1],
175
+ [2, 0, 3]])
176
+
177
+ m3 = Matrix([[0, 0, 0],
178
+ [1, 2, 3],
179
+ [0, 0, 0]])
180
+
181
+ m4 = Matrix([[0, 0, A],
182
+ [1, 2, 3],
183
+ [B, 0, 0]])
184
+
185
+ assert dixon.product_leading_entries(m1) == 24
186
+ assert dixon.product_leading_entries(m2) == 2
187
+ assert dixon.product_leading_entries(m3) == 1
188
+ assert dixon.product_leading_entries(m4) == A * B
189
+
190
+ def test_get_KSY_Dixon_resultant_example_one():
191
+ """Tests the KSY Dixon resultant for example one"""
192
+ x, y, z = symbols('x, y, z')
193
+
194
+ p = x * y * z
195
+ q = x**2 - z**2
196
+ h = x + y + z
197
+ dixon = DixonResultant([p, q, h], [x, y])
198
+ dixon_poly = dixon.get_dixon_polynomial()
199
+ dixon_matrix = dixon.get_dixon_matrix(dixon_poly)
200
+ D = dixon.get_KSY_Dixon_resultant(dixon_matrix)
201
+
202
+ assert D == -z**3
203
+
204
+ def test_get_KSY_Dixon_resultant_example_two():
205
+ """Tests the KSY Dixon resultant for example two"""
206
+ x, y, A = symbols('x, y, A')
207
+
208
+ p = x * y + x * A + x - A**2 - A + y**2 + y
209
+ q = x**2 + x * A - x + x * y + y * A - y
210
+ h = x**2 + x * y + 2 * x - x * A - y * A - 2 * A
211
+
212
+ dixon = DixonResultant([p, q, h], [x, y])
213
+ dixon_poly = dixon.get_dixon_polynomial()
214
+ dixon_matrix = dixon.get_dixon_matrix(dixon_poly)
215
+ D = factor(dixon.get_KSY_Dixon_resultant(dixon_matrix))
216
+
217
+ assert D == -8*A*(A - 1)*(A + 2)*(2*A - 1)**2
218
+
219
+ def test_macaulay_resultant_init():
220
+ """Test init method of MacaulayResultant."""
221
+
222
+ assert macaulay.polynomials == [p, q]
223
+ assert macaulay.variables == [x, y]
224
+ assert macaulay.n == 2
225
+ assert macaulay.degrees == [1, 1]
226
+ assert macaulay.degree_m == 1
227
+ assert macaulay.monomials_size == 2
228
+
229
+ def test_get_degree_m():
230
+ assert macaulay._get_degree_m() == 1
231
+
232
+ def test_get_size():
233
+ assert macaulay.get_size() == 2
234
+
235
+ def test_macaulay_example_one():
236
+ """Tests the Macaulay for example from [Bruce97]_"""
237
+
238
+ x, y, z = symbols('x, y, z')
239
+ a_1_1, a_1_2, a_1_3 = symbols('a_1_1, a_1_2, a_1_3')
240
+ a_2_2, a_2_3, a_3_3 = symbols('a_2_2, a_2_3, a_3_3')
241
+ b_1_1, b_1_2, b_1_3 = symbols('b_1_1, b_1_2, b_1_3')
242
+ b_2_2, b_2_3, b_3_3 = symbols('b_2_2, b_2_3, b_3_3')
243
+ c_1, c_2, c_3 = symbols('c_1, c_2, c_3')
244
+
245
+ f_1 = a_1_1 * x ** 2 + a_1_2 * x * y + a_1_3 * x * z + \
246
+ a_2_2 * y ** 2 + a_2_3 * y * z + a_3_3 * z ** 2
247
+ f_2 = b_1_1 * x ** 2 + b_1_2 * x * y + b_1_3 * x * z + \
248
+ b_2_2 * y ** 2 + b_2_3 * y * z + b_3_3 * z ** 2
249
+ f_3 = c_1 * x + c_2 * y + c_3 * z
250
+
251
+ mac = MacaulayResultant([f_1, f_2, f_3], [x, y, z])
252
+
253
+ assert mac.degrees == [2, 2, 1]
254
+ assert mac.degree_m == 3
255
+
256
+ assert mac.monomial_set == [x ** 3, x ** 2 * y, x ** 2 * z,
257
+ x * y ** 2,
258
+ x * y * z, x * z ** 2, y ** 3,
259
+ y ** 2 *z, y * z ** 2, z ** 3]
260
+ assert mac.monomials_size == 10
261
+ assert mac.get_row_coefficients() == [[x, y, z], [x, y, z],
262
+ [x * y, x * z, y * z, z ** 2]]
263
+
264
+ matrix = mac.get_matrix()
265
+ assert matrix.shape == (mac.monomials_size, mac.monomials_size)
266
+ assert mac.get_submatrix(matrix) == Matrix([[a_1_1, a_2_2],
267
+ [b_1_1, b_2_2]])
268
+
269
+ def test_macaulay_example_two():
270
+ """Tests the Macaulay formulation for example from [Stiller96]_."""
271
+
272
+ x, y, z = symbols('x, y, z')
273
+ a_0, a_1, a_2 = symbols('a_0, a_1, a_2')
274
+ b_0, b_1, b_2 = symbols('b_0, b_1, b_2')
275
+ c_0, c_1, c_2, c_3, c_4 = symbols('c_0, c_1, c_2, c_3, c_4')
276
+
277
+ f = a_0 * y - a_1 * x + a_2 * z
278
+ g = b_1 * x ** 2 + b_0 * y ** 2 - b_2 * z ** 2
279
+ h = c_0 * y - c_1 * x ** 3 + c_2 * x ** 2 * z - c_3 * x * z ** 2 + \
280
+ c_4 * z ** 3
281
+
282
+ mac = MacaulayResultant([f, g, h], [x, y, z])
283
+
284
+ assert mac.degrees == [1, 2, 3]
285
+ assert mac.degree_m == 4
286
+ assert mac.monomials_size == 15
287
+ assert len(mac.get_row_coefficients()) == mac.n
288
+
289
+ matrix = mac.get_matrix()
290
+ assert matrix.shape == (mac.monomials_size, mac.monomials_size)
291
+ assert mac.get_submatrix(matrix) == Matrix([[-a_1, a_0, a_2, 0],
292
+ [0, -a_1, 0, 0],
293
+ [0, 0, -a_1, 0],
294
+ [0, 0, 0, -a_1]])
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_orderings.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests of monomial orderings. """
2
+
3
+ from sympy.polys.orderings import (
4
+ monomial_key, lex, grlex, grevlex, ilex, igrlex,
5
+ LexOrder, InverseOrder, ProductOrder, build_product_order,
6
+ )
7
+
8
+ from sympy.abc import x, y, z, t
9
+ from sympy.core import S
10
+ from sympy.testing.pytest import raises
11
+
12
+ def test_lex_order():
13
+ assert lex((1, 2, 3)) == (1, 2, 3)
14
+ assert str(lex) == 'lex'
15
+
16
+ assert lex((1, 2, 3)) == lex((1, 2, 3))
17
+
18
+ assert lex((2, 2, 3)) > lex((1, 2, 3))
19
+ assert lex((1, 3, 3)) > lex((1, 2, 3))
20
+ assert lex((1, 2, 4)) > lex((1, 2, 3))
21
+
22
+ assert lex((0, 2, 3)) < lex((1, 2, 3))
23
+ assert lex((1, 1, 3)) < lex((1, 2, 3))
24
+ assert lex((1, 2, 2)) < lex((1, 2, 3))
25
+
26
+ assert lex.is_global is True
27
+ assert lex == LexOrder()
28
+ assert lex != grlex
29
+
30
+ def test_grlex_order():
31
+ assert grlex((1, 2, 3)) == (6, (1, 2, 3))
32
+ assert str(grlex) == 'grlex'
33
+
34
+ assert grlex((1, 2, 3)) == grlex((1, 2, 3))
35
+
36
+ assert grlex((2, 2, 3)) > grlex((1, 2, 3))
37
+ assert grlex((1, 3, 3)) > grlex((1, 2, 3))
38
+ assert grlex((1, 2, 4)) > grlex((1, 2, 3))
39
+
40
+ assert grlex((0, 2, 3)) < grlex((1, 2, 3))
41
+ assert grlex((1, 1, 3)) < grlex((1, 2, 3))
42
+ assert grlex((1, 2, 2)) < grlex((1, 2, 3))
43
+
44
+ assert grlex((2, 2, 3)) > grlex((1, 2, 4))
45
+ assert grlex((1, 3, 3)) > grlex((1, 2, 4))
46
+
47
+ assert grlex((0, 2, 3)) < grlex((1, 2, 2))
48
+ assert grlex((1, 1, 3)) < grlex((1, 2, 2))
49
+
50
+ assert grlex((0, 1, 1)) > grlex((0, 0, 2))
51
+ assert grlex((0, 3, 1)) < grlex((2, 2, 1))
52
+
53
+ assert grlex.is_global is True
54
+
55
+ def test_grevlex_order():
56
+ assert grevlex((1, 2, 3)) == (6, (-3, -2, -1))
57
+ assert str(grevlex) == 'grevlex'
58
+
59
+ assert grevlex((1, 2, 3)) == grevlex((1, 2, 3))
60
+
61
+ assert grevlex((2, 2, 3)) > grevlex((1, 2, 3))
62
+ assert grevlex((1, 3, 3)) > grevlex((1, 2, 3))
63
+ assert grevlex((1, 2, 4)) > grevlex((1, 2, 3))
64
+
65
+ assert grevlex((0, 2, 3)) < grevlex((1, 2, 3))
66
+ assert grevlex((1, 1, 3)) < grevlex((1, 2, 3))
67
+ assert grevlex((1, 2, 2)) < grevlex((1, 2, 3))
68
+
69
+ assert grevlex((2, 2, 3)) > grevlex((1, 2, 4))
70
+ assert grevlex((1, 3, 3)) > grevlex((1, 2, 4))
71
+
72
+ assert grevlex((0, 2, 3)) < grevlex((1, 2, 2))
73
+ assert grevlex((1, 1, 3)) < grevlex((1, 2, 2))
74
+
75
+ assert grevlex((0, 1, 1)) > grevlex((0, 0, 2))
76
+ assert grevlex((0, 3, 1)) < grevlex((2, 2, 1))
77
+
78
+ assert grevlex.is_global is True
79
+
80
+ def test_InverseOrder():
81
+ ilex = InverseOrder(lex)
82
+ igrlex = InverseOrder(grlex)
83
+
84
+ assert ilex((1, 2, 3)) > ilex((2, 0, 3))
85
+ assert igrlex((1, 2, 3)) < igrlex((0, 2, 3))
86
+ assert str(ilex) == "ilex"
87
+ assert str(igrlex) == "igrlex"
88
+ assert ilex.is_global is False
89
+ assert igrlex.is_global is False
90
+ assert ilex != igrlex
91
+ assert ilex == InverseOrder(LexOrder())
92
+
93
+ def test_ProductOrder():
94
+ P = ProductOrder((grlex, lambda m: m[:2]), (grlex, lambda m: m[2:]))
95
+ assert P((1, 3, 3, 4, 5)) > P((2, 1, 5, 5, 5))
96
+ assert str(P) == "ProductOrder(grlex, grlex)"
97
+ assert P.is_global is True
98
+ assert ProductOrder((grlex, None), (ilex, None)).is_global is None
99
+ assert ProductOrder((igrlex, None), (ilex, None)).is_global is False
100
+
101
+ def test_monomial_key():
102
+ assert monomial_key() == lex
103
+
104
+ assert monomial_key('lex') == lex
105
+ assert monomial_key('grlex') == grlex
106
+ assert monomial_key('grevlex') == grevlex
107
+
108
+ raises(ValueError, lambda: monomial_key('foo'))
109
+ raises(ValueError, lambda: monomial_key(1))
110
+
111
+ M = [x, x**2*z**2, x*y, x**2, S.One, y**2, x**3, y, z, x*y**2*z, x**2*y**2]
112
+ assert sorted(M, key=monomial_key('lex', [z, y, x])) == \
113
+ [S.One, x, x**2, x**3, y, x*y, y**2, x**2*y**2, z, x*y**2*z, x**2*z**2]
114
+ assert sorted(M, key=monomial_key('grlex', [z, y, x])) == \
115
+ [S.One, x, y, z, x**2, x*y, y**2, x**3, x**2*y**2, x*y**2*z, x**2*z**2]
116
+ assert sorted(M, key=monomial_key('grevlex', [z, y, x])) == \
117
+ [S.One, x, y, z, x**2, x*y, y**2, x**3, x**2*y**2, x**2*z**2, x*y**2*z]
118
+
119
+ def test_build_product_order():
120
+ assert build_product_order((("grlex", x, y), ("grlex", z, t)), [x, y, z, t])((4, 5, 6, 7)) == \
121
+ ((9, (4, 5)), (13, (6, 7)))
122
+
123
+ assert build_product_order((("grlex", x, y), ("grlex", z, t)), [x, y, z, t]) == \
124
+ build_product_order((("grlex", x, y), ("grlex", z, t)), [x, y, z, t])
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_orthopolys.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for efficient functions for generating orthogonal polynomials. """
2
+
3
+ from sympy.core.numbers import Rational as Q
4
+ from sympy.core.singleton import S
5
+ from sympy.core.symbol import symbols
6
+ from sympy.polys.polytools import Poly
7
+ from sympy.testing.pytest import raises
8
+
9
+ from sympy.polys.orthopolys import (
10
+ jacobi_poly,
11
+ gegenbauer_poly,
12
+ chebyshevt_poly,
13
+ chebyshevu_poly,
14
+ hermite_poly,
15
+ hermite_prob_poly,
16
+ legendre_poly,
17
+ laguerre_poly,
18
+ spherical_bessel_fn,
19
+ )
20
+
21
+ from sympy.abc import x, a, b
22
+
23
+
24
+ def test_jacobi_poly():
25
+ raises(ValueError, lambda: jacobi_poly(-1, a, b, x))
26
+
27
+ assert jacobi_poly(1, a, b, x, polys=True) == Poly(
28
+ (a/2 + b/2 + 1)*x + a/2 - b/2, x, domain='ZZ(a,b)')
29
+
30
+ assert jacobi_poly(0, a, b, x) == 1
31
+ assert jacobi_poly(1, a, b, x) == a/2 - b/2 + x*(a/2 + b/2 + 1)
32
+ assert jacobi_poly(2, a, b, x) == (a**2/8 - a*b/4 - a/8 + b**2/8 - b/8 +
33
+ x**2*(a**2/8 + a*b/4 + a*Q(7, 8) + b**2/8 +
34
+ b*Q(7, 8) + Q(3, 2)) + x*(a**2/4 +
35
+ a*Q(3, 4) - b**2/4 - b*Q(3, 4)) - S.Half)
36
+
37
+ assert jacobi_poly(1, a, b, polys=True) == Poly(
38
+ (a/2 + b/2 + 1)*x + a/2 - b/2, x, domain='ZZ(a,b)')
39
+
40
+
41
+ def test_gegenbauer_poly():
42
+ raises(ValueError, lambda: gegenbauer_poly(-1, a, x))
43
+
44
+ assert gegenbauer_poly(
45
+ 1, a, x, polys=True) == Poly(2*a*x, x, domain='ZZ(a)')
46
+
47
+ assert gegenbauer_poly(0, a, x) == 1
48
+ assert gegenbauer_poly(1, a, x) == 2*a*x
49
+ assert gegenbauer_poly(2, a, x) == -a + x**2*(2*a**2 + 2*a)
50
+ assert gegenbauer_poly(
51
+ 3, a, x) == x**3*(4*a**3/3 + 4*a**2 + a*Q(8, 3)) + x*(-2*a**2 - 2*a)
52
+
53
+ assert gegenbauer_poly(1, S.Half).dummy_eq(x)
54
+ assert gegenbauer_poly(1, a, polys=True) == Poly(2*a*x, x, domain='ZZ(a)')
55
+
56
+
57
+ def test_chebyshevt_poly():
58
+ raises(ValueError, lambda: chebyshevt_poly(-1, x))
59
+
60
+ assert chebyshevt_poly(1, x, polys=True) == Poly(x)
61
+
62
+ assert chebyshevt_poly(0, x) == 1
63
+ assert chebyshevt_poly(1, x) == x
64
+ assert chebyshevt_poly(2, x) == 2*x**2 - 1
65
+ assert chebyshevt_poly(3, x) == 4*x**3 - 3*x
66
+ assert chebyshevt_poly(4, x) == 8*x**4 - 8*x**2 + 1
67
+ assert chebyshevt_poly(5, x) == 16*x**5 - 20*x**3 + 5*x
68
+ assert chebyshevt_poly(6, x) == 32*x**6 - 48*x**4 + 18*x**2 - 1
69
+ assert chebyshevt_poly(75, x) == (2*chebyshevt_poly(37, x)*chebyshevt_poly(38, x) - x).expand()
70
+ assert chebyshevt_poly(100, x) == (2*chebyshevt_poly(50, x)**2 - 1).expand()
71
+
72
+ assert chebyshevt_poly(1).dummy_eq(x)
73
+ assert chebyshevt_poly(1, polys=True) == Poly(x)
74
+
75
+
76
+ def test_chebyshevu_poly():
77
+ raises(ValueError, lambda: chebyshevu_poly(-1, x))
78
+
79
+ assert chebyshevu_poly(1, x, polys=True) == Poly(2*x)
80
+
81
+ assert chebyshevu_poly(0, x) == 1
82
+ assert chebyshevu_poly(1, x) == 2*x
83
+ assert chebyshevu_poly(2, x) == 4*x**2 - 1
84
+ assert chebyshevu_poly(3, x) == 8*x**3 - 4*x
85
+ assert chebyshevu_poly(4, x) == 16*x**4 - 12*x**2 + 1
86
+ assert chebyshevu_poly(5, x) == 32*x**5 - 32*x**3 + 6*x
87
+ assert chebyshevu_poly(6, x) == 64*x**6 - 80*x**4 + 24*x**2 - 1
88
+
89
+ assert chebyshevu_poly(1).dummy_eq(2*x)
90
+ assert chebyshevu_poly(1, polys=True) == Poly(2*x)
91
+
92
+
93
+ def test_hermite_poly():
94
+ raises(ValueError, lambda: hermite_poly(-1, x))
95
+
96
+ assert hermite_poly(1, x, polys=True) == Poly(2*x)
97
+
98
+ assert hermite_poly(0, x) == 1
99
+ assert hermite_poly(1, x) == 2*x
100
+ assert hermite_poly(2, x) == 4*x**2 - 2
101
+ assert hermite_poly(3, x) == 8*x**3 - 12*x
102
+ assert hermite_poly(4, x) == 16*x**4 - 48*x**2 + 12
103
+ assert hermite_poly(5, x) == 32*x**5 - 160*x**3 + 120*x
104
+ assert hermite_poly(6, x) == 64*x**6 - 480*x**4 + 720*x**2 - 120
105
+
106
+ assert hermite_poly(1).dummy_eq(2*x)
107
+ assert hermite_poly(1, polys=True) == Poly(2*x)
108
+
109
+
110
+ def test_hermite_prob_poly():
111
+ raises(ValueError, lambda: hermite_prob_poly(-1, x))
112
+
113
+ assert hermite_prob_poly(1, x, polys=True) == Poly(x)
114
+
115
+ assert hermite_prob_poly(0, x) == 1
116
+ assert hermite_prob_poly(1, x) == x
117
+ assert hermite_prob_poly(2, x) == x**2 - 1
118
+ assert hermite_prob_poly(3, x) == x**3 - 3*x
119
+ assert hermite_prob_poly(4, x) == x**4 - 6*x**2 + 3
120
+ assert hermite_prob_poly(5, x) == x**5 - 10*x**3 + 15*x
121
+ assert hermite_prob_poly(6, x) == x**6 - 15*x**4 + 45*x**2 - 15
122
+
123
+ assert hermite_prob_poly(1).dummy_eq(x)
124
+ assert hermite_prob_poly(1, polys=True) == Poly(x)
125
+
126
+
127
+ def test_legendre_poly():
128
+ raises(ValueError, lambda: legendre_poly(-1, x))
129
+
130
+ assert legendre_poly(1, x, polys=True) == Poly(x, domain='QQ')
131
+
132
+ assert legendre_poly(0, x) == 1
133
+ assert legendre_poly(1, x) == x
134
+ assert legendre_poly(2, x) == Q(3, 2)*x**2 - Q(1, 2)
135
+ assert legendre_poly(3, x) == Q(5, 2)*x**3 - Q(3, 2)*x
136
+ assert legendre_poly(4, x) == Q(35, 8)*x**4 - Q(30, 8)*x**2 + Q(3, 8)
137
+ assert legendre_poly(5, x) == Q(63, 8)*x**5 - Q(70, 8)*x**3 + Q(15, 8)*x
138
+ assert legendre_poly(6, x) == Q(
139
+ 231, 16)*x**6 - Q(315, 16)*x**4 + Q(105, 16)*x**2 - Q(5, 16)
140
+
141
+ assert legendre_poly(1).dummy_eq(x)
142
+ assert legendre_poly(1, polys=True) == Poly(x)
143
+
144
+
145
+ def test_laguerre_poly():
146
+ raises(ValueError, lambda: laguerre_poly(-1, x))
147
+
148
+ assert laguerre_poly(1, x, polys=True) == Poly(-x + 1, domain='QQ')
149
+
150
+ assert laguerre_poly(0, x) == 1
151
+ assert laguerre_poly(1, x) == -x + 1
152
+ assert laguerre_poly(2, x) == Q(1, 2)*x**2 - Q(4, 2)*x + 1
153
+ assert laguerre_poly(3, x) == -Q(1, 6)*x**3 + Q(9, 6)*x**2 - Q(18, 6)*x + 1
154
+ assert laguerre_poly(4, x) == Q(
155
+ 1, 24)*x**4 - Q(16, 24)*x**3 + Q(72, 24)*x**2 - Q(96, 24)*x + 1
156
+ assert laguerre_poly(5, x) == -Q(1, 120)*x**5 + Q(25, 120)*x**4 - Q(
157
+ 200, 120)*x**3 + Q(600, 120)*x**2 - Q(600, 120)*x + 1
158
+ assert laguerre_poly(6, x) == Q(1, 720)*x**6 - Q(36, 720)*x**5 + Q(450, 720)*x**4 - Q(2400, 720)*x**3 + Q(5400, 720)*x**2 - Q(4320, 720)*x + 1
159
+
160
+ assert laguerre_poly(0, x, a) == 1
161
+ assert laguerre_poly(1, x, a) == -x + a + 1
162
+ assert laguerre_poly(2, x, a) == x**2/2 + (-a - 2)*x + a**2/2 + a*Q(3, 2) + 1
163
+ assert laguerre_poly(3, x, a) == -x**3/6 + (a/2 + Q(
164
+ 3)/2)*x**2 + (-a**2/2 - a*Q(5, 2) - 3)*x + a**3/6 + a**2 + a*Q(11, 6) + 1
165
+
166
+ assert laguerre_poly(1).dummy_eq(-x + 1)
167
+ assert laguerre_poly(1, polys=True) == Poly(-x + 1)
168
+
169
+
170
+ def test_spherical_bessel_fn():
171
+ x, z = symbols("x z")
172
+ assert spherical_bessel_fn(1, z) == 1/z**2
173
+ assert spherical_bessel_fn(2, z) == -1/z + 3/z**3
174
+ assert spherical_bessel_fn(3, z) == -6/z**2 + 15/z**4
175
+ assert spherical_bessel_fn(4, z) == 1/z - 45/z**3 + 105/z**5
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_partfrac.py ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for algorithms for partial fraction decomposition of rational
2
+ functions. """
3
+
4
+ from sympy.polys.partfrac import (
5
+ apart_undetermined_coeffs,
6
+ apart,
7
+ apart_list, assemble_partfrac_list
8
+ )
9
+
10
+ from sympy.core.expr import Expr
11
+ from sympy.core.function import Lambda
12
+ from sympy.core.numbers import (E, I, Rational, pi, all_close)
13
+ from sympy.core.relational import Eq
14
+ from sympy.core.singleton import S
15
+ from sympy.core.symbol import (Dummy, Symbol)
16
+ from sympy.functions.elementary.miscellaneous import sqrt
17
+ from sympy.matrices.dense import Matrix
18
+ from sympy.polys.polytools import (Poly, factor)
19
+ from sympy.polys.rationaltools import together
20
+ from sympy.polys.rootoftools import RootSum
21
+ from sympy.testing.pytest import raises, XFAIL
22
+ from sympy.abc import x, y, a, b, c
23
+
24
+
25
+ def test_apart():
26
+ assert apart(1) == 1
27
+ assert apart(1, x) == 1
28
+
29
+ f, g = (x**2 + 1)/(x + 1), 2/(x + 1) + x - 1
30
+
31
+ assert apart(f, full=False) == g
32
+ assert apart(f, full=True) == g
33
+
34
+ f, g = 1/(x + 2)/(x + 1), 1/(1 + x) - 1/(2 + x)
35
+
36
+ assert apart(f, full=False) == g
37
+ assert apart(f, full=True) == g
38
+
39
+ f, g = 1/(x + 1)/(x + 5), -1/(5 + x)/4 + 1/(1 + x)/4
40
+
41
+ assert apart(f, full=False) == g
42
+ assert apart(f, full=True) == g
43
+
44
+ assert apart((E*x + 2)/(x - pi)*(x - 1), x) == \
45
+ 2 - E + E*pi + E*x + (E*pi + 2)*(pi - 1)/(x - pi)
46
+
47
+ assert apart(Eq((x**2 + 1)/(x + 1), x), x) == Eq(x - 1 + 2/(x + 1), x)
48
+
49
+ assert apart(x/2, y) == x/2
50
+
51
+ f, g = (x+y)/(2*x - y), Rational(3, 2)*y/(2*x - y) + S.Half
52
+
53
+ assert apart(f, x, full=False) == g
54
+ assert apart(f, x, full=True) == g
55
+
56
+ f, g = (x+y)/(2*x - y), 3*x/(2*x - y) - 1
57
+
58
+ assert apart(f, y, full=False) == g
59
+ assert apart(f, y, full=True) == g
60
+
61
+ raises(NotImplementedError, lambda: apart(1/(x + 1)/(y + 2)))
62
+
63
+
64
+ def test_apart_matrix():
65
+ M = Matrix(2, 2, lambda i, j: 1/(x + i + 1)/(x + j))
66
+
67
+ assert apart(M) == Matrix([
68
+ [1/x - 1/(x + 1), (x + 1)**(-2)],
69
+ [1/(2*x) - (S.Half)/(x + 2), 1/(x + 1) - 1/(x + 2)],
70
+ ])
71
+
72
+
73
+ def test_apart_symbolic():
74
+ f = a*x**4 + (2*b + 2*a*c)*x**3 + (4*b*c - a**2 + a*c**2)*x**2 + \
75
+ (-2*a*b + 2*b*c**2)*x - b**2
76
+ g = a**2*x**4 + (2*a*b + 2*c*a**2)*x**3 + (4*a*b*c + b**2 +
77
+ a**2*c**2)*x**2 + (2*c*b**2 + 2*a*b*c**2)*x + b**2*c**2
78
+
79
+ assert apart(f/g, x) == 1/a - 1/(x + c)**2 - b**2/(a*(a*x + b)**2)
80
+
81
+ assert apart(1/((x + a)*(x + b)*(x + c)), x) == \
82
+ 1/((a - c)*(b - c)*(c + x)) - 1/((a - b)*(b - c)*(b + x)) + \
83
+ 1/((a - b)*(a - c)*(a + x))
84
+
85
+
86
+ def _make_extension_example():
87
+ # https://github.com/sympy/sympy/issues/18531
88
+ from sympy.core import Mul
89
+ def mul2(expr):
90
+ # 2-arg mul hack...
91
+ return Mul(2, expr, evaluate=False)
92
+
93
+ f = ((x**2 + 1)**3/((x - 1)**2*(x + 1)**2*(-x**2 + 2*x + 1)*(x**2 + 2*x - 1)))
94
+ g = (1/mul2(x - sqrt(2) + 1)
95
+ - 1/mul2(x - sqrt(2) - 1)
96
+ + 1/mul2(x + 1 + sqrt(2))
97
+ - 1/mul2(x - 1 + sqrt(2))
98
+ + 1/mul2((x + 1)**2)
99
+ + 1/mul2((x - 1)**2))
100
+ return f, g
101
+
102
+
103
+ def test_apart_extension():
104
+ f = 2/(x**2 + 1)
105
+ g = I/(x + I) - I/(x - I)
106
+
107
+ assert apart(f, extension=I) == g
108
+ assert apart(f, gaussian=True) == g
109
+
110
+ f = x/((x - 2)*(x + I))
111
+
112
+ assert factor(together(apart(f)).expand()) == f
113
+
114
+ f, g = _make_extension_example()
115
+
116
+ # XXX: Only works with dotprodsimp. See test_apart_extension_xfail below
117
+ from sympy.matrices import dotprodsimp
118
+ with dotprodsimp(True):
119
+ assert apart(f, x, extension={sqrt(2)}) == g
120
+
121
+
122
+ def test_apart_extension_xfail():
123
+ f, g = _make_extension_example()
124
+ assert apart(f, x, extension={sqrt(2)}) == g
125
+
126
+
127
+ def test_apart_full():
128
+ f = 1/(x**2 + 1)
129
+
130
+ assert apart(f, full=False) == f
131
+ assert apart(f, full=True).dummy_eq(
132
+ -RootSum(x**2 + 1, Lambda(a, a/(x - a)), auto=False)/2)
133
+
134
+ f = 1/(x**3 + x + 1)
135
+
136
+ assert apart(f, full=False) == f
137
+ assert apart(f, full=True).dummy_eq(
138
+ RootSum(x**3 + x + 1,
139
+ Lambda(a, (a**2*Rational(6, 31) - a*Rational(9, 31) + Rational(4, 31))/(x - a)), auto=False))
140
+
141
+ f = 1/(x**5 + 1)
142
+
143
+ assert apart(f, full=False) == \
144
+ (Rational(-1, 5))*((x**3 - 2*x**2 + 3*x - 4)/(x**4 - x**3 + x**2 -
145
+ x + 1)) + (Rational(1, 5))/(x + 1)
146
+ assert apart(f, full=True).dummy_eq(
147
+ -RootSum(x**4 - x**3 + x**2 - x + 1,
148
+ Lambda(a, a/(x - a)), auto=False)/5 + (Rational(1, 5))/(x + 1))
149
+
150
+
151
+ def test_apart_full_floats():
152
+ # https://github.com/sympy/sympy/issues/26648
153
+ f = (
154
+ 6.43369157032015e-9*x**3 + 1.35203404799555e-5*x**2
155
+ + 0.00357538393743079*x + 0.085
156
+ )/(
157
+ 4.74334912634438e-11*x**4 + 4.09576274286244e-6*x**3
158
+ + 0.00334241812250921*x**2 + 0.15406018058983*x + 1.0
159
+ )
160
+
161
+ expected = (
162
+ 133.599202650992/(x + 85524.0054884464)
163
+ + 1.07757928431867/(x + 774.88576677949)
164
+ + 0.395006955518971/(x + 40.7977016133126)
165
+ + 0.564264854137341/(x + 7.79746609204661)
166
+ )
167
+
168
+ f_apart = apart(f, full=True).evalf()
169
+
170
+ # There is a significant floating point error in this operation.
171
+ assert all_close(f_apart, expected, rtol=1e-3, atol=1e-5)
172
+
173
+
174
+ def test_apart_undetermined_coeffs():
175
+ p = Poly(2*x - 3)
176
+ q = Poly(x**9 - x**8 - x**6 + x**5 - 2*x**2 + 3*x - 1)
177
+ r = (-x**7 - x**6 - x**5 + 4)/(x**8 - x**5 - 2*x + 1) + 1/(x - 1)
178
+
179
+ assert apart_undetermined_coeffs(p, q) == r
180
+
181
+ p = Poly(1, x, domain='ZZ[a,b]')
182
+ q = Poly((x + a)*(x + b), x, domain='ZZ[a,b]')
183
+ r = 1/((a - b)*(b + x)) - 1/((a - b)*(a + x))
184
+
185
+ assert apart_undetermined_coeffs(p, q) == r
186
+
187
+
188
+ def test_apart_list():
189
+ from sympy.utilities.iterables import numbered_symbols
190
+ def dummy_eq(i, j):
191
+ if type(i) in (list, tuple):
192
+ return all(dummy_eq(i, j) for i, j in zip(i, j))
193
+ return i == j or i.dummy_eq(j)
194
+
195
+ w0, w1, w2 = Symbol("w0"), Symbol("w1"), Symbol("w2")
196
+ _a = Dummy("a")
197
+
198
+ f = (-2*x - 2*x**2) / (3*x**2 - 6*x)
199
+ got = apart_list(f, x, dummies=numbered_symbols("w"))
200
+ ans = (-1, Poly(Rational(2, 3), x, domain='QQ'),
201
+ [(Poly(w0 - 2, w0, domain='ZZ'), Lambda(_a, 2), Lambda(_a, -_a + x), 1)])
202
+ assert dummy_eq(got, ans)
203
+
204
+ got = apart_list(2/(x**2-2), x, dummies=numbered_symbols("w"))
205
+ ans = (1, Poly(0, x, domain='ZZ'), [(Poly(w0**2 - 2, w0, domain='ZZ'),
206
+ Lambda(_a, _a/2),
207
+ Lambda(_a, -_a + x), 1)])
208
+ assert dummy_eq(got, ans)
209
+
210
+ f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
211
+ got = apart_list(f, x, dummies=numbered_symbols("w"))
212
+ ans = (1, Poly(0, x, domain='ZZ'),
213
+ [(Poly(w0 - 2, w0, domain='ZZ'), Lambda(_a, 4), Lambda(_a, -_a + x), 1),
214
+ (Poly(w1**2 - 1, w1, domain='ZZ'), Lambda(_a, -3*_a - 6), Lambda(_a, -_a + x), 2),
215
+ (Poly(w2 + 1, w2, domain='ZZ'), Lambda(_a, -4), Lambda(_a, -_a + x), 1)])
216
+ assert dummy_eq(got, ans)
217
+
218
+
219
+ def test_assemble_partfrac_list():
220
+ f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
221
+ pfd = apart_list(f)
222
+ assert assemble_partfrac_list(pfd) == -4/(x + 1) - 3/(x + 1)**2 - 9/(x - 1)**2 + 4/(x - 2)
223
+
224
+ a = Dummy("a")
225
+ pfd = (1, Poly(0, x, domain='ZZ'), [([sqrt(2),-sqrt(2)], Lambda(a, a/2), Lambda(a, -a + x), 1)])
226
+ assert assemble_partfrac_list(pfd) == -1/(sqrt(2)*(x + sqrt(2))) + 1/(sqrt(2)*(x - sqrt(2)))
227
+
228
+
229
+ @XFAIL
230
+ def test_noncommutative_pseudomultivariate():
231
+ # apart doesn't go inside noncommutative expressions
232
+ class foo(Expr):
233
+ is_commutative=False
234
+ e = x/(x + x*y)
235
+ c = 1/(1 + y)
236
+ assert apart(e + foo(e)) == c + foo(c)
237
+ assert apart(e*foo(e)) == c*foo(c)
238
+
239
+ def test_noncommutative():
240
+ class foo(Expr):
241
+ is_commutative=False
242
+ e = x/(x + x*y)
243
+ c = 1/(1 + y)
244
+ assert apart(e + foo()) == c + foo()
245
+
246
+ def test_issue_5798():
247
+ assert apart(
248
+ 2*x/(x**2 + 1) - (x - 1)/(2*(x**2 + 1)) + 1/(2*(x + 1)) - 2/x) == \
249
+ (3*x + 1)/(x**2 + 1)/2 + 1/(x + 1)/2 - 2/x
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polyclasses.py ADDED
@@ -0,0 +1,601 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for OO layer of several polynomial representations. """
2
+
3
+ from sympy.functions.elementary.miscellaneous import sqrt
4
+ from sympy.polys.domains import ZZ, QQ
5
+ from sympy.polys.polyclasses import DMP, DMF, ANP
6
+ from sympy.polys.polyerrors import (CoercionFailed, ExactQuotientFailed,
7
+ NotInvertible)
8
+ from sympy.polys.specialpolys import f_polys
9
+ from sympy.testing.pytest import raises, warns_deprecated_sympy
10
+
11
+ f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
12
+
13
+ def test_DMP___init__():
14
+ f = DMP([[ZZ(0)], [], [ZZ(0), ZZ(1), ZZ(2)], [ZZ(3)]], ZZ)
15
+
16
+ assert f._rep == [[1, 2], [3]]
17
+ assert f.dom == ZZ
18
+ assert f.lev == 1
19
+
20
+ f = DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ, 1)
21
+
22
+ assert f._rep == [[1, 2], [3]]
23
+ assert f.dom == ZZ
24
+ assert f.lev == 1
25
+
26
+ f = DMP.from_dict({(1, 1): ZZ(1), (0, 0): ZZ(2)}, 1, ZZ)
27
+
28
+ assert f._rep == [[1, 0], [2]]
29
+ assert f.dom == ZZ
30
+ assert f.lev == 1
31
+
32
+
33
+ def test_DMP_rep_deprecation():
34
+ f = DMP([1, 2, 3], ZZ)
35
+
36
+ with warns_deprecated_sympy():
37
+ assert f.rep == [1, 2, 3]
38
+
39
+
40
+ def test_DMP___eq__():
41
+ assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ) == \
42
+ DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ)
43
+
44
+ assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ) == \
45
+ DMP([[QQ(1), QQ(2)], [QQ(3)]], QQ)
46
+ assert DMP([[QQ(1), QQ(2)], [QQ(3)]], QQ) == \
47
+ DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ)
48
+
49
+ assert DMP([[[ZZ(1)]]], ZZ) != DMP([[ZZ(1)]], ZZ)
50
+ assert DMP([[ZZ(1)]], ZZ) != DMP([[[ZZ(1)]]], ZZ)
51
+
52
+
53
+ def test_DMP___bool__():
54
+ assert bool(DMP([[]], ZZ)) is False
55
+ assert bool(DMP([[ZZ(1)]], ZZ)) is True
56
+
57
+
58
+ def test_DMP_to_dict():
59
+ f = DMP([[ZZ(3)], [], [ZZ(2)], [], [ZZ(8)]], ZZ)
60
+
61
+ assert f.to_dict() == \
62
+ {(4, 0): 3, (2, 0): 2, (0, 0): 8}
63
+ assert f.to_sympy_dict() == \
64
+ {(4, 0): ZZ.to_sympy(3), (2, 0): ZZ.to_sympy(2), (0, 0):
65
+ ZZ.to_sympy(8)}
66
+
67
+
68
+ def test_DMP_properties():
69
+ assert DMP([[]], ZZ).is_zero is True
70
+ assert DMP([[ZZ(1)]], ZZ).is_zero is False
71
+
72
+ assert DMP([[ZZ(1)]], ZZ).is_one is True
73
+ assert DMP([[ZZ(2)]], ZZ).is_one is False
74
+
75
+ assert DMP([[ZZ(1)]], ZZ).is_ground is True
76
+ assert DMP([[ZZ(1)], [ZZ(2)], [ZZ(1)]], ZZ).is_ground is False
77
+
78
+ assert DMP([[ZZ(1)], [ZZ(2), ZZ(0)], [ZZ(1), ZZ(0)]], ZZ).is_sqf is True
79
+ assert DMP([[ZZ(1)], [ZZ(2), ZZ(0)], [ZZ(1), ZZ(0), ZZ(0)]], ZZ).is_sqf is False
80
+
81
+ assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ).is_monic is True
82
+ assert DMP([[ZZ(2), ZZ(2)], [ZZ(3)]], ZZ).is_monic is False
83
+
84
+ assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ).is_primitive is True
85
+ assert DMP([[ZZ(2), ZZ(4)], [ZZ(6)]], ZZ).is_primitive is False
86
+
87
+
88
+ def test_DMP_arithmetics():
89
+ f = DMP([[ZZ(2)], [ZZ(2), ZZ(0)]], ZZ)
90
+
91
+ assert f.mul_ground(2) == DMP([[ZZ(4)], [ZZ(4), ZZ(0)]], ZZ)
92
+ assert f.quo_ground(2) == DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
93
+
94
+ raises(ExactQuotientFailed, lambda: f.exquo_ground(3))
95
+
96
+ f = DMP([[ZZ(-5)]], ZZ)
97
+ g = DMP([[ZZ(5)]], ZZ)
98
+
99
+ assert f.abs() == g
100
+ assert abs(f) == g
101
+
102
+ assert g.neg() == f
103
+ assert -g == f
104
+
105
+ h = DMP([[]], ZZ)
106
+
107
+ assert f.add(g) == h
108
+ assert f + g == h
109
+ assert g + f == h
110
+ assert f + 5 == h
111
+ assert 5 + f == h
112
+
113
+ h = DMP([[ZZ(-10)]], ZZ)
114
+
115
+ assert f.sub(g) == h
116
+ assert f - g == h
117
+ assert g - f == -h
118
+ assert f - 5 == h
119
+ assert 5 - f == -h
120
+
121
+ h = DMP([[ZZ(-25)]], ZZ)
122
+
123
+ assert f.mul(g) == h
124
+ assert f * g == h
125
+ assert g * f == h
126
+ assert f * 5 == h
127
+ assert 5 * f == h
128
+
129
+ h = DMP([[ZZ(25)]], ZZ)
130
+
131
+ assert f.sqr() == h
132
+ assert f.pow(2) == h
133
+ assert f**2 == h
134
+
135
+ raises(TypeError, lambda: f.pow('x'))
136
+
137
+ f = DMP([[ZZ(1)], [], [ZZ(1), ZZ(0), ZZ(0)]], ZZ)
138
+ g = DMP([[ZZ(2)], [ZZ(-2), ZZ(0)]], ZZ)
139
+
140
+ q = DMP([[ZZ(2)], [ZZ(2), ZZ(0)]], ZZ)
141
+ r = DMP([[ZZ(8), ZZ(0), ZZ(0)]], ZZ)
142
+
143
+ assert f.pdiv(g) == (q, r)
144
+ assert f.pquo(g) == q
145
+ assert f.prem(g) == r
146
+
147
+ raises(ExactQuotientFailed, lambda: f.pexquo(g))
148
+
149
+ f = DMP([[ZZ(1)], [], [ZZ(1), ZZ(0), ZZ(0)]], ZZ)
150
+ g = DMP([[ZZ(1)], [ZZ(-1), ZZ(0)]], ZZ)
151
+
152
+ q = DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
153
+ r = DMP([[ZZ(2), ZZ(0), ZZ(0)]], ZZ)
154
+
155
+ assert f.div(g) == (q, r)
156
+ assert f.quo(g) == q
157
+ assert f.rem(g) == r
158
+
159
+ assert divmod(f, g) == (q, r)
160
+ assert f // g == q
161
+ assert f % g == r
162
+
163
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
164
+
165
+ f = DMP([ZZ(1), ZZ(0), ZZ(-1)], ZZ)
166
+ g = DMP([ZZ(2), ZZ(-2)], ZZ)
167
+
168
+ q = DMP([], ZZ)
169
+ r = f
170
+
171
+ pq = DMP([ZZ(2), ZZ(2)], ZZ)
172
+ pr = DMP([], ZZ)
173
+
174
+ assert f.div(g) == (q, r)
175
+ assert f.quo(g) == q
176
+ assert f.rem(g) == r
177
+
178
+ assert divmod(f, g) == (q, r)
179
+ assert f // g == q
180
+ assert f % g == r
181
+
182
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
183
+
184
+ assert f.pdiv(g) == (pq, pr)
185
+ assert f.pquo(g) == pq
186
+ assert f.prem(g) == pr
187
+ assert f.pexquo(g) == pq
188
+
189
+
190
+ def test_DMP_functionality():
191
+ f = DMP([[ZZ(1)], [ZZ(2), ZZ(0)], [ZZ(1), ZZ(0), ZZ(0)]], ZZ)
192
+ g = DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
193
+ h = DMP([[ZZ(1)]], ZZ)
194
+
195
+ assert f.degree() == 2
196
+ assert f.degree_list() == (2, 2)
197
+ assert f.total_degree() == 2
198
+
199
+ assert f.LC() == ZZ(1)
200
+ assert f.TC() == ZZ(0)
201
+ assert f.nth(1, 1) == ZZ(2)
202
+
203
+ raises(TypeError, lambda: f.nth(0, 'x'))
204
+
205
+ assert f.max_norm() == 2
206
+ assert f.l1_norm() == 4
207
+
208
+ u = DMP([[ZZ(2)], [ZZ(2), ZZ(0)]], ZZ)
209
+
210
+ assert f.diff(m=1, j=0) == u
211
+ assert f.diff(m=1, j=1) == u
212
+
213
+ raises(TypeError, lambda: f.diff(m='x', j=0))
214
+
215
+ u = DMP([ZZ(1), ZZ(2), ZZ(1)], ZZ)
216
+ v = DMP([ZZ(1), ZZ(2), ZZ(1)], ZZ)
217
+
218
+ assert f.eval(a=1, j=0) == u
219
+ assert f.eval(a=1, j=1) == v
220
+
221
+ assert f.eval(1).eval(1) == ZZ(4)
222
+
223
+ assert f.cofactors(g) == (g, g, h)
224
+ assert f.gcd(g) == g
225
+ assert f.lcm(g) == f
226
+
227
+ u = DMP([[QQ(45), QQ(30), QQ(5)]], QQ)
228
+ v = DMP([[QQ(1), QQ(2, 3), QQ(1, 9)]], QQ)
229
+
230
+ assert u.monic() == v
231
+
232
+ assert (4*f).content() == ZZ(4)
233
+ assert (4*f).primitive() == (ZZ(4), f)
234
+
235
+ f = DMP([QQ(1,3), QQ(1)], QQ)
236
+ g = DMP([QQ(1,7), QQ(1)], QQ)
237
+
238
+ assert f.cancel(g) == f.cancel(g, include=True) == (
239
+ DMP([QQ(7), QQ(21)], QQ),
240
+ DMP([QQ(3), QQ(21)], QQ)
241
+ )
242
+ assert f.cancel(g, include=False) == (
243
+ QQ(7),
244
+ QQ(3),
245
+ DMP([QQ(1), QQ(3)], QQ),
246
+ DMP([QQ(1), QQ(7)], QQ)
247
+ )
248
+
249
+ f = DMP([[ZZ(1)], [ZZ(2)], [ZZ(3)], [ZZ(4)], [ZZ(5)], [ZZ(6)]], ZZ)
250
+
251
+ assert f.trunc(3) == DMP([[ZZ(1)], [ZZ(-1)], [], [ZZ(1)], [ZZ(-1)], []], ZZ)
252
+
253
+ f = DMP(f_4, ZZ)
254
+
255
+ assert f.sqf_part() == -f
256
+ assert f.sqf_list() == (ZZ(-1), [(-f, 1)])
257
+
258
+ f = DMP([[ZZ(-1)], [], [], [ZZ(5)]], ZZ)
259
+ g = DMP([[ZZ(3), ZZ(1)], [], []], ZZ)
260
+ h = DMP([[ZZ(45), ZZ(30), ZZ(5)]], ZZ)
261
+
262
+ r = DMP([ZZ(675), ZZ(675), ZZ(225), ZZ(25)], ZZ)
263
+
264
+ assert f.subresultants(g) == [f, g, h]
265
+ assert f.resultant(g) == r
266
+
267
+ f = DMP([ZZ(1), ZZ(3), ZZ(9), ZZ(-13)], ZZ)
268
+
269
+ assert f.discriminant() == -11664
270
+
271
+ f = DMP([QQ(2), QQ(0)], QQ)
272
+ g = DMP([QQ(1), QQ(0), QQ(-16)], QQ)
273
+
274
+ s = DMP([QQ(1, 32), QQ(0)], QQ)
275
+ t = DMP([QQ(-1, 16)], QQ)
276
+ h = DMP([QQ(1)], QQ)
277
+
278
+ assert f.half_gcdex(g) == (s, h)
279
+ assert f.gcdex(g) == (s, t, h)
280
+
281
+ assert f.invert(g) == s
282
+
283
+ f = DMP([[QQ(1)], [QQ(2)], [QQ(3)]], QQ)
284
+
285
+ raises(ValueError, lambda: f.half_gcdex(f))
286
+ raises(ValueError, lambda: f.gcdex(f))
287
+
288
+ raises(ValueError, lambda: f.invert(f))
289
+
290
+ f = DMP(ZZ.map([1, 0, 20, 0, 150, 0, 500, 0, 625, -2, 0, -10, 9]), ZZ)
291
+ g = DMP([ZZ(1), ZZ(0), ZZ(0), ZZ(-2), ZZ(9)], ZZ)
292
+ h = DMP([ZZ(1), ZZ(0), ZZ(5), ZZ(0)], ZZ)
293
+
294
+ assert g.compose(h) == f
295
+ assert f.decompose() == [g, h]
296
+
297
+ f = DMP([[QQ(1)], [QQ(2)], [QQ(3)]], QQ)
298
+
299
+ raises(ValueError, lambda: f.decompose())
300
+ raises(ValueError, lambda: f.sturm())
301
+
302
+
303
+ def test_DMP_exclude():
304
+ f = [[[[[[[[[[[[[[[[[[[[[[[[[[ZZ(1)]], [[]]]]]]]]]]]]]]]]]]]]]]]]]]
305
+ J = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
306
+ 18, 19, 20, 21, 22, 24, 25]
307
+
308
+ assert DMP(f, ZZ).exclude() == (J, DMP([ZZ(1), ZZ(0)], ZZ))
309
+ assert DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ).exclude() ==\
310
+ ([], DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ))
311
+
312
+
313
+ def test_DMF__init__():
314
+ f = DMF(([[0], [], [0, 1, 2], [3]], [[1, 2, 3]]), ZZ)
315
+
316
+ assert f.num == [[1, 2], [3]]
317
+ assert f.den == [[1, 2, 3]]
318
+ assert f.lev == 1
319
+ assert f.dom == ZZ
320
+
321
+ f = DMF(([[1, 2], [3]], [[1, 2, 3]]), ZZ, 1)
322
+
323
+ assert f.num == [[1, 2], [3]]
324
+ assert f.den == [[1, 2, 3]]
325
+ assert f.lev == 1
326
+ assert f.dom == ZZ
327
+
328
+ f = DMF(([[-1], [-2]], [[3], [-4]]), ZZ)
329
+
330
+ assert f.num == [[-1], [-2]]
331
+ assert f.den == [[3], [-4]]
332
+ assert f.lev == 1
333
+ assert f.dom == ZZ
334
+
335
+ f = DMF(([[1], [2]], [[-3], [4]]), ZZ)
336
+
337
+ assert f.num == [[-1], [-2]]
338
+ assert f.den == [[3], [-4]]
339
+ assert f.lev == 1
340
+ assert f.dom == ZZ
341
+
342
+ f = DMF(([[1], [2]], [[-3], [4]]), ZZ)
343
+
344
+ assert f.num == [[-1], [-2]]
345
+ assert f.den == [[3], [-4]]
346
+ assert f.lev == 1
347
+ assert f.dom == ZZ
348
+
349
+ f = DMF(([[]], [[-3], [4]]), ZZ)
350
+
351
+ assert f.num == [[]]
352
+ assert f.den == [[1]]
353
+ assert f.lev == 1
354
+ assert f.dom == ZZ
355
+
356
+ f = DMF(17, ZZ, 1)
357
+
358
+ assert f.num == [[17]]
359
+ assert f.den == [[1]]
360
+ assert f.lev == 1
361
+ assert f.dom == ZZ
362
+
363
+ f = DMF(([[1], [2]]), ZZ)
364
+
365
+ assert f.num == [[1], [2]]
366
+ assert f.den == [[1]]
367
+ assert f.lev == 1
368
+ assert f.dom == ZZ
369
+
370
+ f = DMF([[0], [], [0, 1, 2], [3]], ZZ)
371
+
372
+ assert f.num == [[1, 2], [3]]
373
+ assert f.den == [[1]]
374
+ assert f.lev == 1
375
+ assert f.dom == ZZ
376
+
377
+ f = DMF({(1, 1): 1, (0, 0): 2}, ZZ, 1)
378
+
379
+ assert f.num == [[1, 0], [2]]
380
+ assert f.den == [[1]]
381
+ assert f.lev == 1
382
+ assert f.dom == ZZ
383
+
384
+ f = DMF(([[QQ(1)], [QQ(2)]], [[-QQ(3)], [QQ(4)]]), QQ)
385
+
386
+ assert f.num == [[-QQ(1)], [-QQ(2)]]
387
+ assert f.den == [[QQ(3)], [-QQ(4)]]
388
+ assert f.lev == 1
389
+ assert f.dom == QQ
390
+
391
+ f = DMF(([[QQ(1, 5)], [QQ(2, 5)]], [[-QQ(3, 7)], [QQ(4, 7)]]), QQ)
392
+
393
+ assert f.num == [[-QQ(7)], [-QQ(14)]]
394
+ assert f.den == [[QQ(15)], [-QQ(20)]]
395
+ assert f.lev == 1
396
+ assert f.dom == QQ
397
+
398
+ raises(ValueError, lambda: DMF(([1], [[1]]), ZZ))
399
+ raises(ZeroDivisionError, lambda: DMF(([1], []), ZZ))
400
+
401
+
402
+ def test_DMF__bool__():
403
+ assert bool(DMF([[]], ZZ)) is False
404
+ assert bool(DMF([[1]], ZZ)) is True
405
+
406
+
407
+ def test_DMF_properties():
408
+ assert DMF([[]], ZZ).is_zero is True
409
+ assert DMF([[]], ZZ).is_one is False
410
+
411
+ assert DMF([[1]], ZZ).is_zero is False
412
+ assert DMF([[1]], ZZ).is_one is True
413
+
414
+ assert DMF(([[1]], [[2]]), ZZ).is_one is False
415
+
416
+
417
+ def test_DMF_arithmetics():
418
+ f = DMF([[7], [-9]], ZZ)
419
+ g = DMF([[-7], [9]], ZZ)
420
+
421
+ assert f.neg() == -f == g
422
+
423
+ f = DMF(([[1]], [[1], []]), ZZ)
424
+ g = DMF(([[1]], [[1, 0]]), ZZ)
425
+
426
+ h = DMF(([[1], [1, 0]], [[1, 0], []]), ZZ)
427
+
428
+ assert f.add(g) == f + g == h
429
+ assert g.add(f) == g + f == h
430
+
431
+ h = DMF(([[-1], [1, 0]], [[1, 0], []]), ZZ)
432
+
433
+ assert f.sub(g) == f - g == h
434
+
435
+ h = DMF(([[1]], [[1, 0], []]), ZZ)
436
+
437
+ assert f.mul(g) == f*g == h
438
+ assert g.mul(f) == g*f == h
439
+
440
+ h = DMF(([[1, 0]], [[1], []]), ZZ)
441
+
442
+ assert f.quo(g) == f/g == h
443
+
444
+ h = DMF(([[1]], [[1], [], [], []]), ZZ)
445
+
446
+ assert f.pow(3) == f**3 == h
447
+
448
+ h = DMF(([[1]], [[1, 0, 0, 0]]), ZZ)
449
+
450
+ assert g.pow(3) == g**3 == h
451
+
452
+ h = DMF(([[1, 0]], [[1]]), ZZ)
453
+
454
+ assert g.pow(-1) == g**-1 == h
455
+
456
+
457
+ def test_ANP___init__():
458
+ rep = [QQ(1), QQ(1)]
459
+ mod = [QQ(1), QQ(0), QQ(1)]
460
+
461
+ f = ANP(rep, mod, QQ)
462
+
463
+ assert f.to_list() == [QQ(1), QQ(1)]
464
+ assert f.mod_to_list() == [QQ(1), QQ(0), QQ(1)]
465
+ assert f.dom == QQ
466
+
467
+ rep = {1: QQ(1), 0: QQ(1)}
468
+ mod = {2: QQ(1), 0: QQ(1)}
469
+
470
+ f = ANP(rep, mod, QQ)
471
+
472
+ assert f.to_list() == [QQ(1), QQ(1)]
473
+ assert f.mod_to_list() == [QQ(1), QQ(0), QQ(1)]
474
+ assert f.dom == QQ
475
+
476
+ f = ANP(1, mod, QQ)
477
+
478
+ assert f.to_list() == [QQ(1)]
479
+ assert f.mod_to_list() == [QQ(1), QQ(0), QQ(1)]
480
+ assert f.dom == QQ
481
+
482
+ f = ANP([1, 0.5], mod, QQ)
483
+
484
+ assert all(QQ.of_type(a) for a in f.to_list())
485
+
486
+ raises(CoercionFailed, lambda: ANP([sqrt(2)], mod, QQ))
487
+
488
+
489
+ def test_ANP___eq__():
490
+ a = ANP([QQ(1), QQ(1)], [QQ(1), QQ(0), QQ(1)], QQ)
491
+ b = ANP([QQ(1), QQ(1)], [QQ(1), QQ(0), QQ(2)], QQ)
492
+
493
+ assert (a == a) is True
494
+ assert (a != a) is False
495
+
496
+ assert (a == b) is False
497
+ assert (a != b) is True
498
+
499
+ b = ANP([QQ(1), QQ(2)], [QQ(1), QQ(0), QQ(1)], QQ)
500
+
501
+ assert (a == b) is False
502
+ assert (a != b) is True
503
+
504
+
505
+ def test_ANP___bool__():
506
+ assert bool(ANP([], [QQ(1), QQ(0), QQ(1)], QQ)) is False
507
+ assert bool(ANP([QQ(1)], [QQ(1), QQ(0), QQ(1)], QQ)) is True
508
+
509
+
510
+ def test_ANP_properties():
511
+ mod = [QQ(1), QQ(0), QQ(1)]
512
+
513
+ assert ANP([QQ(0)], mod, QQ).is_zero is True
514
+ assert ANP([QQ(1)], mod, QQ).is_zero is False
515
+
516
+ assert ANP([QQ(1)], mod, QQ).is_one is True
517
+ assert ANP([QQ(2)], mod, QQ).is_one is False
518
+
519
+
520
+ def test_ANP_arithmetics():
521
+ mod = [QQ(1), QQ(0), QQ(0), QQ(-2)]
522
+
523
+ a = ANP([QQ(2), QQ(-1), QQ(1)], mod, QQ)
524
+ b = ANP([QQ(1), QQ(2)], mod, QQ)
525
+
526
+ c = ANP([QQ(-2), QQ(1), QQ(-1)], mod, QQ)
527
+
528
+ assert a.neg() == -a == c
529
+
530
+ c = ANP([QQ(2), QQ(0), QQ(3)], mod, QQ)
531
+
532
+ assert a.add(b) == a + b == c
533
+ assert b.add(a) == b + a == c
534
+
535
+ c = ANP([QQ(2), QQ(-2), QQ(-1)], mod, QQ)
536
+
537
+ assert a.sub(b) == a - b == c
538
+
539
+ c = ANP([QQ(-2), QQ(2), QQ(1)], mod, QQ)
540
+
541
+ assert b.sub(a) == b - a == c
542
+
543
+ c = ANP([QQ(3), QQ(-1), QQ(6)], mod, QQ)
544
+
545
+ assert a.mul(b) == a*b == c
546
+ assert b.mul(a) == b*a == c
547
+
548
+ c = ANP([QQ(-1, 43), QQ(9, 43), QQ(5, 43)], mod, QQ)
549
+
550
+ assert a.pow(0) == a**(0) == ANP(1, mod, QQ)
551
+ assert a.pow(1) == a**(1) == a
552
+
553
+ assert a.pow(-1) == a**(-1) == c
554
+
555
+ assert a.quo(a) == a.mul(a.pow(-1)) == a*a**(-1) == ANP(1, mod, QQ)
556
+
557
+ c = ANP([], [1, 0, 0, -2], QQ)
558
+ r1 = a.rem(b)
559
+
560
+ (q, r2) = a.div(b)
561
+
562
+ assert r1 == r2 == c == a % b
563
+
564
+ raises(NotInvertible, lambda: a.div(c))
565
+ raises(NotInvertible, lambda: a.rem(c))
566
+
567
+ # Comparison with "hard-coded" value fails despite looking identical
568
+ # from sympy import Rational
569
+ # c = ANP([Rational(11, 10), Rational(-1, 5), Rational(-3, 5)], [1, 0, 0, -2], QQ)
570
+
571
+ assert q == a/b # == c
572
+
573
+ def test_ANP_unify():
574
+ mod_z = [ZZ(1), ZZ(0), ZZ(-2)]
575
+ mod_q = [QQ(1), QQ(0), QQ(-2)]
576
+
577
+ a = ANP([QQ(1)], mod_q, QQ)
578
+ b = ANP([ZZ(1)], mod_z, ZZ)
579
+
580
+ assert a.unify(b)[0] == QQ
581
+ assert b.unify(a)[0] == QQ
582
+ assert a.unify(a)[0] == QQ
583
+ assert b.unify(b)[0] == ZZ
584
+
585
+ assert a.unify_ANP(b)[-1] == QQ
586
+ assert b.unify_ANP(a)[-1] == QQ
587
+ assert a.unify_ANP(a)[-1] == QQ
588
+ assert b.unify_ANP(b)[-1] == ZZ
589
+
590
+
591
+ def test_zero_poly():
592
+ from sympy import Symbol
593
+ x = Symbol('x')
594
+
595
+ R_old = ZZ.old_poly_ring(x)
596
+ zero_poly_old = R_old(0)
597
+ cont_old, prim_old = zero_poly_old.primitive()
598
+
599
+ assert cont_old == 0
600
+ assert prim_old == zero_poly_old
601
+ assert prim_old.is_primitive is False
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polyfuncs.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for high-level polynomials manipulation functions. """
2
+
3
+ from sympy.polys.polyfuncs import (
4
+ symmetrize, horner, interpolate, rational_interpolate, viete,
5
+ )
6
+
7
+ from sympy.polys.polyerrors import (
8
+ MultivariatePolynomialError,
9
+ )
10
+
11
+ from sympy.core.singleton import S
12
+ from sympy.core.symbol import symbols
13
+ from sympy.testing.pytest import raises
14
+
15
+ from sympy.abc import a, b, c, d, e, x, y, z
16
+
17
+
18
+ def test_symmetrize():
19
+ assert symmetrize(0, x, y, z) == (0, 0)
20
+ assert symmetrize(1, x, y, z) == (1, 0)
21
+
22
+ s1 = x + y + z
23
+ s2 = x*y + x*z + y*z
24
+
25
+ assert symmetrize(1) == (1, 0)
26
+ assert symmetrize(1, formal=True) == (1, 0, [])
27
+
28
+ assert symmetrize(x) == (x, 0)
29
+ assert symmetrize(x + 1) == (x + 1, 0)
30
+
31
+ assert symmetrize(x, x, y) == (x + y, -y)
32
+ assert symmetrize(x + 1, x, y) == (x + y + 1, -y)
33
+
34
+ assert symmetrize(x, x, y, z) == (s1, -y - z)
35
+ assert symmetrize(x + 1, x, y, z) == (s1 + 1, -y - z)
36
+
37
+ assert symmetrize(x**2, x, y, z) == (s1**2 - 2*s2, -y**2 - z**2)
38
+
39
+ assert symmetrize(x**2 + y**2) == (-2*x*y + (x + y)**2, 0)
40
+ assert symmetrize(x**2 - y**2) == (-2*x*y + (x + y)**2, -2*y**2)
41
+
42
+ assert symmetrize(x**3 + y**2 + a*x**2 + b*y**3, x, y) == \
43
+ (-3*x*y*(x + y) - 2*a*x*y + a*(x + y)**2 + (x + y)**3,
44
+ y**2*(1 - a) + y**3*(b - 1))
45
+
46
+ U = [u0, u1, u2] = symbols('u:3')
47
+
48
+ assert symmetrize(x + 1, x, y, z, formal=True, symbols=U) == \
49
+ (u0 + 1, -y - z, [(u0, x + y + z), (u1, x*y + x*z + y*z), (u2, x*y*z)])
50
+
51
+ assert symmetrize([1, 2, 3]) == [(1, 0), (2, 0), (3, 0)]
52
+ assert symmetrize([1, 2, 3], formal=True) == ([(1, 0), (2, 0), (3, 0)], [])
53
+
54
+ assert symmetrize([x + y, x - y]) == [(x + y, 0), (x + y, -2*y)]
55
+
56
+
57
+ def test_horner():
58
+ assert horner(0) == 0
59
+ assert horner(1) == 1
60
+ assert horner(x) == x
61
+
62
+ assert horner(x + 1) == x + 1
63
+ assert horner(x**2 + 1) == x**2 + 1
64
+ assert horner(x**2 + x) == (x + 1)*x
65
+ assert horner(x**2 + x + 1) == (x + 1)*x + 1
66
+
67
+ assert horner(
68
+ 9*x**4 + 8*x**3 + 7*x**2 + 6*x + 5) == (((9*x + 8)*x + 7)*x + 6)*x + 5
69
+ assert horner(
70
+ a*x**4 + b*x**3 + c*x**2 + d*x + e) == (((a*x + b)*x + c)*x + d)*x + e
71
+
72
+ assert horner(4*x**2*y**2 + 2*x**2*y + 2*x*y**2 + x*y, wrt=x) == ((
73
+ 4*y + 2)*x*y + (2*y + 1)*y)*x
74
+ assert horner(4*x**2*y**2 + 2*x**2*y + 2*x*y**2 + x*y, wrt=y) == ((
75
+ 4*x + 2)*y*x + (2*x + 1)*x)*y
76
+
77
+
78
+ def test_interpolate():
79
+ assert interpolate([1, 4, 9, 16], x) == x**2
80
+ assert interpolate([1, 4, 9, 25], x) == S(3)*x**3/2 - S(8)*x**2 + S(33)*x/2 - 9
81
+ assert interpolate([(1, 1), (2, 4), (3, 9)], x) == x**2
82
+ assert interpolate([(1, 2), (2, 5), (3, 10)], x) == 1 + x**2
83
+ assert interpolate({1: 2, 2: 5, 3: 10}, x) == 1 + x**2
84
+ assert interpolate({5: 2, 7: 5, 8: 10, 9: 13}, x) == \
85
+ -S(13)*x**3/24 + S(12)*x**2 - S(2003)*x/24 + 187
86
+ assert interpolate([(1, 3), (0, 6), (2, 5), (5, 7), (-2, 4)], x) == \
87
+ S(-61)*x**4/280 + S(247)*x**3/210 + S(139)*x**2/280 - S(1871)*x/420 + 6
88
+ assert interpolate((9, 4, 9), 3) == 9
89
+ assert interpolate((1, 9, 16), 1) is S.One
90
+ assert interpolate(((x, 1), (2, 3)), x) is S.One
91
+ assert interpolate({x: 1, 2: 3}, x) is S.One
92
+ assert interpolate(((2, x), (1, 3)), x) == x**2 - 4*x + 6
93
+
94
+
95
+ def test_rational_interpolate():
96
+ x, y = symbols('x,y')
97
+ xdata = [1, 2, 3, 4, 5, 6]
98
+ ydata1 = [120, 150, 200, 255, 312, 370]
99
+ ydata2 = [-210, -35, 105, 231, 350, 465]
100
+ assert rational_interpolate(list(zip(xdata, ydata1)), 2) == (
101
+ (60*x**2 + 60)/x )
102
+ assert rational_interpolate(list(zip(xdata, ydata1)), 3) == (
103
+ (60*x**2 + 60)/x )
104
+ assert rational_interpolate(list(zip(xdata, ydata2)), 2, X=y) == (
105
+ (105*y**2 - 525)/(y + 1) )
106
+ xdata = list(range(1,11))
107
+ ydata = [-1923885361858460, -5212158811973685, -9838050145867125,
108
+ -15662936261217245, -22469424125057910, -30073793365223685,
109
+ -38332297297028735, -47132954289530109, -56387719094026320,
110
+ -66026548943876885]
111
+ assert rational_interpolate(list(zip(xdata, ydata)), 5) == (
112
+ (-12986226192544605*x**4 +
113
+ 8657484128363070*x**3 - 30301194449270745*x**2 + 4328742064181535*x
114
+ - 4328742064181535)/(x**3 + 9*x**2 - 3*x + 11))
115
+
116
+
117
+ def test_viete():
118
+ r1, r2 = symbols('r1, r2')
119
+
120
+ assert viete(
121
+ a*x**2 + b*x + c, [r1, r2], x) == [(r1 + r2, -b/a), (r1*r2, c/a)]
122
+
123
+ raises(ValueError, lambda: viete(1, [], x))
124
+ raises(ValueError, lambda: viete(x**2 + 1, [r1]))
125
+
126
+ raises(MultivariatePolynomialError, lambda: viete(x + y, [r1]))
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polymatrix.py ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.testing.pytest import raises
2
+
3
+ from sympy.polys.polymatrix import PolyMatrix
4
+ from sympy.polys import Poly
5
+
6
+ from sympy.core.singleton import S
7
+ from sympy.matrices.dense import Matrix
8
+ from sympy.polys.domains.integerring import ZZ
9
+ from sympy.polys.domains.rationalfield import QQ
10
+
11
+ from sympy.abc import x, y
12
+
13
+
14
+ def _test_polymatrix():
15
+ pm1 = PolyMatrix([[Poly(x**2, x), Poly(-x, x)], [Poly(x**3, x), Poly(-1 + x, x)]])
16
+ v1 = PolyMatrix([[1, 0], [-1, 0]], ring='ZZ[x]')
17
+ m1 = PolyMatrix([[1, 0], [-1, 0]], ring='ZZ[x]')
18
+ A = PolyMatrix([[Poly(x**2 + x, x), Poly(0, x)], \
19
+ [Poly(x**3 - x + 1, x), Poly(0, x)]])
20
+ B = PolyMatrix([[Poly(x**2, x), Poly(-x, x)], [Poly(-x**2, x), Poly(x, x)]])
21
+ assert A.ring == ZZ[x]
22
+ assert isinstance(pm1*v1, PolyMatrix)
23
+ assert pm1*v1 == A
24
+ assert pm1*m1 == A
25
+ assert v1*pm1 == B
26
+
27
+ pm2 = PolyMatrix([[Poly(x**2, x, domain='QQ'), Poly(0, x, domain='QQ'), Poly(-x**2, x, domain='QQ'), \
28
+ Poly(x**3, x, domain='QQ'), Poly(0, x, domain='QQ'), Poly(-x**3, x, domain='QQ')]])
29
+ assert pm2.ring == QQ[x]
30
+ v2 = PolyMatrix([1, 0, 0, 0, 0, 0], ring='ZZ[x]')
31
+ m2 = PolyMatrix([1, 0, 0, 0, 0, 0], ring='ZZ[x]')
32
+ C = PolyMatrix([[Poly(x**2, x, domain='QQ')]])
33
+ assert pm2*v2 == C
34
+ assert pm2*m2 == C
35
+
36
+ pm3 = PolyMatrix([[Poly(x**2, x), S.One]], ring='ZZ[x]')
37
+ v3 = S.Half*pm3
38
+ assert v3 == PolyMatrix([[Poly(S.Half*x**2, x, domain='QQ'), S.Half]], ring='QQ[x]')
39
+ assert pm3*S.Half == v3
40
+ assert v3.ring == QQ[x]
41
+
42
+ pm4 = PolyMatrix([[Poly(x**2, x, domain='ZZ'), Poly(-x**2, x, domain='ZZ')]])
43
+ v4 = PolyMatrix([1, -1], ring='ZZ[x]')
44
+ assert pm4*v4 == PolyMatrix([[Poly(2*x**2, x, domain='ZZ')]])
45
+
46
+ assert len(PolyMatrix(ring=ZZ[x])) == 0
47
+ assert PolyMatrix([1, 0, 0, 1], x)/(-1) == PolyMatrix([-1, 0, 0, -1], x)
48
+
49
+
50
+ def test_polymatrix_constructor():
51
+ M1 = PolyMatrix([[x, y]], ring=QQ[x,y])
52
+ assert M1.ring == QQ[x,y]
53
+ assert M1.domain == QQ
54
+ assert M1.gens == (x, y)
55
+ assert M1.shape == (1, 2)
56
+ assert M1.rows == 1
57
+ assert M1.cols == 2
58
+ assert len(M1) == 2
59
+ assert list(M1) == [Poly(x, (x, y), domain=QQ), Poly(y, (x, y), domain=QQ)]
60
+
61
+ M2 = PolyMatrix([[x, y]], ring=QQ[x][y])
62
+ assert M2.ring == QQ[x][y]
63
+ assert M2.domain == QQ[x]
64
+ assert M2.gens == (y,)
65
+ assert M2.shape == (1, 2)
66
+ assert M2.rows == 1
67
+ assert M2.cols == 2
68
+ assert len(M2) == 2
69
+ assert list(M2) == [Poly(x, (y,), domain=QQ[x]), Poly(y, (y,), domain=QQ[x])]
70
+
71
+ assert PolyMatrix([[x, y]], y) == PolyMatrix([[x, y]], ring=ZZ.frac_field(x)[y])
72
+ assert PolyMatrix([[x, y]], ring='ZZ[x,y]') == PolyMatrix([[x, y]], ring=ZZ[x,y])
73
+
74
+ assert PolyMatrix([[x, y]], (x, y)) == PolyMatrix([[x, y]], ring=QQ[x,y])
75
+ assert PolyMatrix([[x, y]], x, y) == PolyMatrix([[x, y]], ring=QQ[x,y])
76
+ assert PolyMatrix([x, y]) == PolyMatrix([[x], [y]], ring=QQ[x,y])
77
+ assert PolyMatrix(1, 2, [x, y]) == PolyMatrix([[x, y]], ring=QQ[x,y])
78
+ assert PolyMatrix(1, 2, lambda i,j: [x,y][j]) == PolyMatrix([[x, y]], ring=QQ[x,y])
79
+ assert PolyMatrix(0, 2, [], x, y).shape == (0, 2)
80
+ assert PolyMatrix(2, 0, [], x, y).shape == (2, 0)
81
+ assert PolyMatrix([[], []], x, y).shape == (2, 0)
82
+ assert PolyMatrix(ring=QQ[x,y]) == PolyMatrix(0, 0, [], ring=QQ[x,y]) == PolyMatrix([], ring=QQ[x,y])
83
+ raises(TypeError, lambda: PolyMatrix())
84
+ raises(TypeError, lambda: PolyMatrix(1))
85
+
86
+ assert PolyMatrix([Poly(x), Poly(y)]) == PolyMatrix([[x], [y]], ring=ZZ[x,y])
87
+
88
+ # XXX: Maybe a bug in parallel_poly_from_expr (x lost from gens and domain):
89
+ assert PolyMatrix([Poly(y, x), 1]) == PolyMatrix([[y], [1]], ring=QQ[y])
90
+
91
+
92
+ def test_polymatrix_eq():
93
+ assert (PolyMatrix([x]) == PolyMatrix([x])) is True
94
+ assert (PolyMatrix([y]) == PolyMatrix([x])) is False
95
+ assert (PolyMatrix([x]) != PolyMatrix([x])) is False
96
+ assert (PolyMatrix([y]) != PolyMatrix([x])) is True
97
+
98
+ assert PolyMatrix([[x, y]]) != PolyMatrix([x, y]) == PolyMatrix([[x], [y]])
99
+
100
+ assert PolyMatrix([x], ring=QQ[x]) != PolyMatrix([x], ring=ZZ[x])
101
+
102
+ assert PolyMatrix([x]) != Matrix([x])
103
+ assert PolyMatrix([x]).to_Matrix() == Matrix([x])
104
+
105
+ assert PolyMatrix([1], x) == PolyMatrix([1], x)
106
+ assert PolyMatrix([1], x) != PolyMatrix([1], y)
107
+
108
+
109
+ def test_polymatrix_from_Matrix():
110
+ assert PolyMatrix.from_Matrix(Matrix([1, 2]), x) == PolyMatrix([1, 2], x, ring=QQ[x])
111
+ assert PolyMatrix.from_Matrix(Matrix([1]), ring=QQ[x]) == PolyMatrix([1], x)
112
+ pmx = PolyMatrix([1, 2], x)
113
+ pmy = PolyMatrix([1, 2], y)
114
+ assert pmx != pmy
115
+ assert pmx.set_gens(y) == pmy
116
+
117
+
118
+ def test_polymatrix_repr():
119
+ assert repr(PolyMatrix([[1, 2]], x)) == 'PolyMatrix([[1, 2]], ring=QQ[x])'
120
+ assert repr(PolyMatrix(0, 2, [], x)) == 'PolyMatrix(0, 2, [], ring=QQ[x])'
121
+
122
+
123
+ def test_polymatrix_getitem():
124
+ M = PolyMatrix([[1, 2], [3, 4]], x)
125
+ assert M[:, :] == M
126
+ assert M[0, :] == PolyMatrix([[1, 2]], x)
127
+ assert M[:, 0] == PolyMatrix([1, 3], x)
128
+ assert M[0, 0] == Poly(1, x, domain=QQ)
129
+ assert M[0] == Poly(1, x, domain=QQ)
130
+ assert M[:2] == [Poly(1, x, domain=QQ), Poly(2, x, domain=QQ)]
131
+
132
+
133
+ def test_polymatrix_arithmetic():
134
+ M = PolyMatrix([[1, 2], [3, 4]], x)
135
+ assert M + M == PolyMatrix([[2, 4], [6, 8]], x)
136
+ assert M - M == PolyMatrix([[0, 0], [0, 0]], x)
137
+ assert -M == PolyMatrix([[-1, -2], [-3, -4]], x)
138
+ raises(TypeError, lambda: M + 1)
139
+ raises(TypeError, lambda: M - 1)
140
+ raises(TypeError, lambda: 1 + M)
141
+ raises(TypeError, lambda: 1 - M)
142
+
143
+ assert M * M == PolyMatrix([[7, 10], [15, 22]], x)
144
+ assert 2 * M == PolyMatrix([[2, 4], [6, 8]], x)
145
+ assert M * 2 == PolyMatrix([[2, 4], [6, 8]], x)
146
+ assert S(2) * M == PolyMatrix([[2, 4], [6, 8]], x)
147
+ assert M * S(2) == PolyMatrix([[2, 4], [6, 8]], x)
148
+ raises(TypeError, lambda: [] * M)
149
+ raises(TypeError, lambda: M * [])
150
+ M2 = PolyMatrix([[1, 2]], ring=ZZ[x])
151
+ assert S.Half * M2 == PolyMatrix([[S.Half, 1]], ring=QQ[x])
152
+ assert M2 * S.Half == PolyMatrix([[S.Half, 1]], ring=QQ[x])
153
+
154
+ assert M / 2 == PolyMatrix([[S(1)/2, 1], [S(3)/2, 2]], x)
155
+ assert M / Poly(2, x) == PolyMatrix([[S(1)/2, 1], [S(3)/2, 2]], x)
156
+ raises(TypeError, lambda: M / [])
157
+
158
+
159
+ def test_polymatrix_manipulations():
160
+ M1 = PolyMatrix([[1, 2], [3, 4]], x)
161
+ assert M1.transpose() == PolyMatrix([[1, 3], [2, 4]], x)
162
+ M2 = PolyMatrix([[5, 6], [7, 8]], x)
163
+ assert M1.row_join(M2) == PolyMatrix([[1, 2, 5, 6], [3, 4, 7, 8]], x)
164
+ assert M1.col_join(M2) == PolyMatrix([[1, 2], [3, 4], [5, 6], [7, 8]], x)
165
+ assert M1.applyfunc(lambda e: 2*e) == PolyMatrix([[2, 4], [6, 8]], x)
166
+
167
+
168
+ def test_polymatrix_ones_zeros():
169
+ assert PolyMatrix.zeros(1, 2, x) == PolyMatrix([[0, 0]], x)
170
+ assert PolyMatrix.eye(2, x) == PolyMatrix([[1, 0], [0, 1]], x)
171
+
172
+
173
+ def test_polymatrix_rref():
174
+ M = PolyMatrix([[1, 2], [3, 4]], x)
175
+ assert M.rref() == (PolyMatrix.eye(2, x), (0, 1))
176
+ raises(ValueError, lambda: PolyMatrix([1, 2], ring=ZZ[x]).rref())
177
+ raises(ValueError, lambda: PolyMatrix([1, x], ring=QQ[x]).rref())
178
+
179
+
180
+ def test_polymatrix_nullspace():
181
+ M = PolyMatrix([[1, 2], [3, 6]], x)
182
+ assert M.nullspace() == [PolyMatrix([-2, 1], x)]
183
+ raises(ValueError, lambda: PolyMatrix([1, 2], ring=ZZ[x]).nullspace())
184
+ raises(ValueError, lambda: PolyMatrix([1, x], ring=QQ[x]).nullspace())
185
+ assert M.rank() == 1
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polyoptions.py ADDED
@@ -0,0 +1,485 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for options manager for :class:`Poly` and public API functions. """
2
+
3
+ from sympy.polys.polyoptions import (
4
+ Options, Expand, Gens, Wrt, Sort, Order, Field, Greedy, Domain,
5
+ Split, Gaussian, Extension, Modulus, Symmetric, Strict, Auto,
6
+ Frac, Formal, Polys, Include, All, Gen, Symbols, Method)
7
+
8
+ from sympy.polys.orderings import lex
9
+ from sympy.polys.domains import FF, GF, ZZ, QQ, QQ_I, RR, CC, EX
10
+
11
+ from sympy.polys.polyerrors import OptionError, GeneratorsError
12
+
13
+ from sympy.core.numbers import (I, Integer)
14
+ from sympy.core.symbol import Symbol
15
+ from sympy.functions.elementary.miscellaneous import sqrt
16
+ from sympy.testing.pytest import raises
17
+ from sympy.abc import x, y, z
18
+
19
+
20
+ def test_Options_clone():
21
+ opt = Options((x, y, z), {'domain': 'ZZ'})
22
+
23
+ assert opt.gens == (x, y, z)
24
+ assert opt.domain == ZZ
25
+ assert ('order' in opt) is False
26
+
27
+ new_opt = opt.clone({'gens': (x, y), 'order': 'lex'})
28
+
29
+ assert opt.gens == (x, y, z)
30
+ assert opt.domain == ZZ
31
+ assert ('order' in opt) is False
32
+
33
+ assert new_opt.gens == (x, y)
34
+ assert new_opt.domain == ZZ
35
+ assert ('order' in new_opt) is True
36
+
37
+
38
+ def test_Expand_preprocess():
39
+ assert Expand.preprocess(False) is False
40
+ assert Expand.preprocess(True) is True
41
+
42
+ assert Expand.preprocess(0) is False
43
+ assert Expand.preprocess(1) is True
44
+
45
+ raises(OptionError, lambda: Expand.preprocess(x))
46
+
47
+
48
+ def test_Expand_postprocess():
49
+ opt = {'expand': True}
50
+ Expand.postprocess(opt)
51
+
52
+ assert opt == {'expand': True}
53
+
54
+
55
+ def test_Gens_preprocess():
56
+ assert Gens.preprocess((None,)) == ()
57
+ assert Gens.preprocess((x, y, z)) == (x, y, z)
58
+ assert Gens.preprocess(((x, y, z),)) == (x, y, z)
59
+
60
+ a = Symbol('a', commutative=False)
61
+
62
+ raises(GeneratorsError, lambda: Gens.preprocess((x, x, y)))
63
+ raises(GeneratorsError, lambda: Gens.preprocess((x, y, a)))
64
+
65
+
66
+ def test_Gens_postprocess():
67
+ opt = {'gens': (x, y)}
68
+ Gens.postprocess(opt)
69
+
70
+ assert opt == {'gens': (x, y)}
71
+
72
+
73
+ def test_Wrt_preprocess():
74
+ assert Wrt.preprocess(x) == ['x']
75
+ assert Wrt.preprocess('') == []
76
+ assert Wrt.preprocess(' ') == []
77
+ assert Wrt.preprocess('x,y') == ['x', 'y']
78
+ assert Wrt.preprocess('x y') == ['x', 'y']
79
+ assert Wrt.preprocess('x, y') == ['x', 'y']
80
+ assert Wrt.preprocess('x , y') == ['x', 'y']
81
+ assert Wrt.preprocess(' x, y') == ['x', 'y']
82
+ assert Wrt.preprocess(' x, y') == ['x', 'y']
83
+ assert Wrt.preprocess([x, y]) == ['x', 'y']
84
+
85
+ raises(OptionError, lambda: Wrt.preprocess(','))
86
+ raises(OptionError, lambda: Wrt.preprocess(0))
87
+
88
+
89
+ def test_Wrt_postprocess():
90
+ opt = {'wrt': ['x']}
91
+ Wrt.postprocess(opt)
92
+
93
+ assert opt == {'wrt': ['x']}
94
+
95
+
96
+ def test_Sort_preprocess():
97
+ assert Sort.preprocess([x, y, z]) == ['x', 'y', 'z']
98
+ assert Sort.preprocess((x, y, z)) == ['x', 'y', 'z']
99
+
100
+ assert Sort.preprocess('x > y > z') == ['x', 'y', 'z']
101
+ assert Sort.preprocess('x>y>z') == ['x', 'y', 'z']
102
+
103
+ raises(OptionError, lambda: Sort.preprocess(0))
104
+ raises(OptionError, lambda: Sort.preprocess({x, y, z}))
105
+
106
+
107
+ def test_Sort_postprocess():
108
+ opt = {'sort': 'x > y'}
109
+ Sort.postprocess(opt)
110
+
111
+ assert opt == {'sort': 'x > y'}
112
+
113
+
114
+ def test_Order_preprocess():
115
+ assert Order.preprocess('lex') == lex
116
+
117
+
118
+ def test_Order_postprocess():
119
+ opt = {'order': True}
120
+ Order.postprocess(opt)
121
+
122
+ assert opt == {'order': True}
123
+
124
+
125
+ def test_Field_preprocess():
126
+ assert Field.preprocess(False) is False
127
+ assert Field.preprocess(True) is True
128
+
129
+ assert Field.preprocess(0) is False
130
+ assert Field.preprocess(1) is True
131
+
132
+ raises(OptionError, lambda: Field.preprocess(x))
133
+
134
+
135
+ def test_Field_postprocess():
136
+ opt = {'field': True}
137
+ Field.postprocess(opt)
138
+
139
+ assert opt == {'field': True}
140
+
141
+
142
+ def test_Greedy_preprocess():
143
+ assert Greedy.preprocess(False) is False
144
+ assert Greedy.preprocess(True) is True
145
+
146
+ assert Greedy.preprocess(0) is False
147
+ assert Greedy.preprocess(1) is True
148
+
149
+ raises(OptionError, lambda: Greedy.preprocess(x))
150
+
151
+
152
+ def test_Greedy_postprocess():
153
+ opt = {'greedy': True}
154
+ Greedy.postprocess(opt)
155
+
156
+ assert opt == {'greedy': True}
157
+
158
+
159
+ def test_Domain_preprocess():
160
+ assert Domain.preprocess(ZZ) == ZZ
161
+ assert Domain.preprocess(QQ) == QQ
162
+ assert Domain.preprocess(EX) == EX
163
+ assert Domain.preprocess(FF(2)) == FF(2)
164
+ assert Domain.preprocess(ZZ[x, y]) == ZZ[x, y]
165
+
166
+ assert Domain.preprocess('Z') == ZZ
167
+ assert Domain.preprocess('Q') == QQ
168
+
169
+ assert Domain.preprocess('ZZ') == ZZ
170
+ assert Domain.preprocess('QQ') == QQ
171
+
172
+ assert Domain.preprocess('EX') == EX
173
+
174
+ assert Domain.preprocess('FF(23)') == FF(23)
175
+ assert Domain.preprocess('GF(23)') == GF(23)
176
+
177
+ raises(OptionError, lambda: Domain.preprocess('Z[]'))
178
+
179
+ assert Domain.preprocess('Z[x]') == ZZ[x]
180
+ assert Domain.preprocess('Q[x]') == QQ[x]
181
+ assert Domain.preprocess('R[x]') == RR[x]
182
+ assert Domain.preprocess('C[x]') == CC[x]
183
+
184
+ assert Domain.preprocess('ZZ[x]') == ZZ[x]
185
+ assert Domain.preprocess('QQ[x]') == QQ[x]
186
+ assert Domain.preprocess('RR[x]') == RR[x]
187
+ assert Domain.preprocess('CC[x]') == CC[x]
188
+
189
+ assert Domain.preprocess('Z[x,y]') == ZZ[x, y]
190
+ assert Domain.preprocess('Q[x,y]') == QQ[x, y]
191
+ assert Domain.preprocess('R[x,y]') == RR[x, y]
192
+ assert Domain.preprocess('C[x,y]') == CC[x, y]
193
+
194
+ assert Domain.preprocess('ZZ[x,y]') == ZZ[x, y]
195
+ assert Domain.preprocess('QQ[x,y]') == QQ[x, y]
196
+ assert Domain.preprocess('RR[x,y]') == RR[x, y]
197
+ assert Domain.preprocess('CC[x,y]') == CC[x, y]
198
+
199
+ raises(OptionError, lambda: Domain.preprocess('Z()'))
200
+
201
+ assert Domain.preprocess('Z(x)') == ZZ.frac_field(x)
202
+ assert Domain.preprocess('Q(x)') == QQ.frac_field(x)
203
+
204
+ assert Domain.preprocess('ZZ(x)') == ZZ.frac_field(x)
205
+ assert Domain.preprocess('QQ(x)') == QQ.frac_field(x)
206
+
207
+ assert Domain.preprocess('Z(x,y)') == ZZ.frac_field(x, y)
208
+ assert Domain.preprocess('Q(x,y)') == QQ.frac_field(x, y)
209
+
210
+ assert Domain.preprocess('ZZ(x,y)') == ZZ.frac_field(x, y)
211
+ assert Domain.preprocess('QQ(x,y)') == QQ.frac_field(x, y)
212
+
213
+ assert Domain.preprocess('Q<I>') == QQ.algebraic_field(I)
214
+ assert Domain.preprocess('QQ<I>') == QQ.algebraic_field(I)
215
+
216
+ assert Domain.preprocess('Q<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I)
217
+ assert Domain.preprocess(
218
+ 'QQ<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I)
219
+
220
+ raises(OptionError, lambda: Domain.preprocess('abc'))
221
+
222
+
223
+ def test_Domain_postprocess():
224
+ raises(GeneratorsError, lambda: Domain.postprocess({'gens': (x, y),
225
+ 'domain': ZZ[y, z]}))
226
+
227
+ raises(GeneratorsError, lambda: Domain.postprocess({'gens': (),
228
+ 'domain': EX}))
229
+ raises(GeneratorsError, lambda: Domain.postprocess({'domain': EX}))
230
+
231
+
232
+ def test_Split_preprocess():
233
+ assert Split.preprocess(False) is False
234
+ assert Split.preprocess(True) is True
235
+
236
+ assert Split.preprocess(0) is False
237
+ assert Split.preprocess(1) is True
238
+
239
+ raises(OptionError, lambda: Split.preprocess(x))
240
+
241
+
242
+ def test_Split_postprocess():
243
+ raises(NotImplementedError, lambda: Split.postprocess({'split': True}))
244
+
245
+
246
+ def test_Gaussian_preprocess():
247
+ assert Gaussian.preprocess(False) is False
248
+ assert Gaussian.preprocess(True) is True
249
+
250
+ assert Gaussian.preprocess(0) is False
251
+ assert Gaussian.preprocess(1) is True
252
+
253
+ raises(OptionError, lambda: Gaussian.preprocess(x))
254
+
255
+
256
+ def test_Gaussian_postprocess():
257
+ opt = {'gaussian': True}
258
+ Gaussian.postprocess(opt)
259
+
260
+ assert opt == {
261
+ 'gaussian': True,
262
+ 'domain': QQ_I,
263
+ }
264
+
265
+
266
+ def test_Extension_preprocess():
267
+ assert Extension.preprocess(True) is True
268
+ assert Extension.preprocess(1) is True
269
+
270
+ assert Extension.preprocess([]) is None
271
+
272
+ assert Extension.preprocess(sqrt(2)) == {sqrt(2)}
273
+ assert Extension.preprocess([sqrt(2)]) == {sqrt(2)}
274
+
275
+ assert Extension.preprocess([sqrt(2), I]) == {sqrt(2), I}
276
+
277
+ raises(OptionError, lambda: Extension.preprocess(False))
278
+ raises(OptionError, lambda: Extension.preprocess(0))
279
+
280
+
281
+ def test_Extension_postprocess():
282
+ opt = {'extension': {sqrt(2)}}
283
+ Extension.postprocess(opt)
284
+
285
+ assert opt == {
286
+ 'extension': {sqrt(2)},
287
+ 'domain': QQ.algebraic_field(sqrt(2)),
288
+ }
289
+
290
+ opt = {'extension': True}
291
+ Extension.postprocess(opt)
292
+
293
+ assert opt == {'extension': True}
294
+
295
+
296
+ def test_Modulus_preprocess():
297
+ assert Modulus.preprocess(23) == 23
298
+ assert Modulus.preprocess(Integer(23)) == 23
299
+
300
+ raises(OptionError, lambda: Modulus.preprocess(0))
301
+ raises(OptionError, lambda: Modulus.preprocess(x))
302
+
303
+
304
+ def test_Modulus_postprocess():
305
+ opt = {'modulus': 5}
306
+ Modulus.postprocess(opt)
307
+
308
+ assert opt == {
309
+ 'modulus': 5,
310
+ 'domain': FF(5),
311
+ }
312
+
313
+ opt = {'modulus': 5, 'symmetric': False}
314
+ Modulus.postprocess(opt)
315
+
316
+ assert opt == {
317
+ 'modulus': 5,
318
+ 'domain': FF(5, False),
319
+ 'symmetric': False,
320
+ }
321
+
322
+
323
+ def test_Symmetric_preprocess():
324
+ assert Symmetric.preprocess(False) is False
325
+ assert Symmetric.preprocess(True) is True
326
+
327
+ assert Symmetric.preprocess(0) is False
328
+ assert Symmetric.preprocess(1) is True
329
+
330
+ raises(OptionError, lambda: Symmetric.preprocess(x))
331
+
332
+
333
+ def test_Symmetric_postprocess():
334
+ opt = {'symmetric': True}
335
+ Symmetric.postprocess(opt)
336
+
337
+ assert opt == {'symmetric': True}
338
+
339
+
340
+ def test_Strict_preprocess():
341
+ assert Strict.preprocess(False) is False
342
+ assert Strict.preprocess(True) is True
343
+
344
+ assert Strict.preprocess(0) is False
345
+ assert Strict.preprocess(1) is True
346
+
347
+ raises(OptionError, lambda: Strict.preprocess(x))
348
+
349
+
350
+ def test_Strict_postprocess():
351
+ opt = {'strict': True}
352
+ Strict.postprocess(opt)
353
+
354
+ assert opt == {'strict': True}
355
+
356
+
357
+ def test_Auto_preprocess():
358
+ assert Auto.preprocess(False) is False
359
+ assert Auto.preprocess(True) is True
360
+
361
+ assert Auto.preprocess(0) is False
362
+ assert Auto.preprocess(1) is True
363
+
364
+ raises(OptionError, lambda: Auto.preprocess(x))
365
+
366
+
367
+ def test_Auto_postprocess():
368
+ opt = {'auto': True}
369
+ Auto.postprocess(opt)
370
+
371
+ assert opt == {'auto': True}
372
+
373
+
374
+ def test_Frac_preprocess():
375
+ assert Frac.preprocess(False) is False
376
+ assert Frac.preprocess(True) is True
377
+
378
+ assert Frac.preprocess(0) is False
379
+ assert Frac.preprocess(1) is True
380
+
381
+ raises(OptionError, lambda: Frac.preprocess(x))
382
+
383
+
384
+ def test_Frac_postprocess():
385
+ opt = {'frac': True}
386
+ Frac.postprocess(opt)
387
+
388
+ assert opt == {'frac': True}
389
+
390
+
391
+ def test_Formal_preprocess():
392
+ assert Formal.preprocess(False) is False
393
+ assert Formal.preprocess(True) is True
394
+
395
+ assert Formal.preprocess(0) is False
396
+ assert Formal.preprocess(1) is True
397
+
398
+ raises(OptionError, lambda: Formal.preprocess(x))
399
+
400
+
401
+ def test_Formal_postprocess():
402
+ opt = {'formal': True}
403
+ Formal.postprocess(opt)
404
+
405
+ assert opt == {'formal': True}
406
+
407
+
408
+ def test_Polys_preprocess():
409
+ assert Polys.preprocess(False) is False
410
+ assert Polys.preprocess(True) is True
411
+
412
+ assert Polys.preprocess(0) is False
413
+ assert Polys.preprocess(1) is True
414
+
415
+ raises(OptionError, lambda: Polys.preprocess(x))
416
+
417
+
418
+ def test_Polys_postprocess():
419
+ opt = {'polys': True}
420
+ Polys.postprocess(opt)
421
+
422
+ assert opt == {'polys': True}
423
+
424
+
425
+ def test_Include_preprocess():
426
+ assert Include.preprocess(False) is False
427
+ assert Include.preprocess(True) is True
428
+
429
+ assert Include.preprocess(0) is False
430
+ assert Include.preprocess(1) is True
431
+
432
+ raises(OptionError, lambda: Include.preprocess(x))
433
+
434
+
435
+ def test_Include_postprocess():
436
+ opt = {'include': True}
437
+ Include.postprocess(opt)
438
+
439
+ assert opt == {'include': True}
440
+
441
+
442
+ def test_All_preprocess():
443
+ assert All.preprocess(False) is False
444
+ assert All.preprocess(True) is True
445
+
446
+ assert All.preprocess(0) is False
447
+ assert All.preprocess(1) is True
448
+
449
+ raises(OptionError, lambda: All.preprocess(x))
450
+
451
+
452
+ def test_All_postprocess():
453
+ opt = {'all': True}
454
+ All.postprocess(opt)
455
+
456
+ assert opt == {'all': True}
457
+
458
+
459
+ def test_Gen_postprocess():
460
+ opt = {'gen': x}
461
+ Gen.postprocess(opt)
462
+
463
+ assert opt == {'gen': x}
464
+
465
+
466
+ def test_Symbols_preprocess():
467
+ raises(OptionError, lambda: Symbols.preprocess(x))
468
+
469
+
470
+ def test_Symbols_postprocess():
471
+ opt = {'symbols': [x, y, z]}
472
+ Symbols.postprocess(opt)
473
+
474
+ assert opt == {'symbols': [x, y, z]}
475
+
476
+
477
+ def test_Method_preprocess():
478
+ raises(OptionError, lambda: Method.preprocess(10))
479
+
480
+
481
+ def test_Method_postprocess():
482
+ opt = {'method': 'f5b'}
483
+ Method.postprocess(opt)
484
+
485
+ assert opt == {'method': 'f5b'}
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polyroots.py ADDED
@@ -0,0 +1,758 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for algorithms for computing symbolic roots of polynomials. """
2
+
3
+ from sympy.core.numbers import (I, Rational, pi)
4
+ from sympy.core.singleton import S
5
+ from sympy.core.symbol import (Symbol, Wild, symbols)
6
+ from sympy.functions.elementary.complexes import (conjugate, im, re)
7
+ from sympy.functions.elementary.exponential import exp
8
+ from sympy.functions.elementary.miscellaneous import (root, sqrt)
9
+ from sympy.functions.elementary.piecewise import Piecewise
10
+ from sympy.functions.elementary.trigonometric import (acos, cos, sin)
11
+ from sympy.polys.domains.integerring import ZZ
12
+ from sympy.sets.sets import Interval
13
+ from sympy.simplify.powsimp import powsimp
14
+
15
+ from sympy.polys import Poly, cyclotomic_poly, intervals, nroots, rootof
16
+
17
+ from sympy.polys.polyroots import (root_factors, roots_linear,
18
+ roots_quadratic, roots_cubic, roots_quartic, roots_quintic,
19
+ roots_cyclotomic, roots_binomial, preprocess_roots, roots)
20
+
21
+ from sympy.polys.orthopolys import legendre_poly
22
+ from sympy.polys.polyerrors import PolynomialError, \
23
+ UnsolvableFactorError
24
+ from sympy.polys.polyutils import _nsort
25
+
26
+ from sympy.testing.pytest import raises, slow
27
+ from sympy.core.random import verify_numerically
28
+ import mpmath
29
+ from itertools import product
30
+
31
+
32
+
33
+ a, b, c, d, e, q, t, x, y, z = symbols('a,b,c,d,e,q,t,x,y,z')
34
+
35
+
36
+ def _check(roots):
37
+ # this is the desired invariant for roots returned
38
+ # by all_roots. It is trivially true for linear
39
+ # polynomials.
40
+ nreal = sum(1 if i.is_real else 0 for i in roots)
41
+ assert sorted(roots[:nreal]) == list(roots[:nreal])
42
+ for ix in range(nreal, len(roots), 2):
43
+ if not (
44
+ roots[ix + 1] == roots[ix] or
45
+ roots[ix + 1] == conjugate(roots[ix])):
46
+ return False
47
+ return True
48
+
49
+
50
+ def test_roots_linear():
51
+ assert roots_linear(Poly(2*x + 1, x)) == [Rational(-1, 2)]
52
+
53
+
54
+ def test_roots_quadratic():
55
+ assert roots_quadratic(Poly(2*x**2, x)) == [0, 0]
56
+ assert roots_quadratic(Poly(2*x**2 + 3*x, x)) == [Rational(-3, 2), 0]
57
+ assert roots_quadratic(Poly(2*x**2 + 3, x)) == [-I*sqrt(6)/2, I*sqrt(6)/2]
58
+ assert roots_quadratic(Poly(2*x**2 + 4*x + 3, x)) == [-1 - I*sqrt(2)/2, -1 + I*sqrt(2)/2]
59
+ _check(Poly(2*x**2 + 4*x + 3, x).all_roots())
60
+
61
+ f = x**2 + (2*a*e + 2*c*e)/(a - c)*x + (d - b + a*e**2 - c*e**2)/(a - c)
62
+ assert roots_quadratic(Poly(f, x)) == \
63
+ [-e*(a + c)/(a - c) - sqrt(a*b + c*d - a*d - b*c + 4*a*c*e**2)/(a - c),
64
+ -e*(a + c)/(a - c) + sqrt(a*b + c*d - a*d - b*c + 4*a*c*e**2)/(a - c)]
65
+
66
+ # check for simplification
67
+ f = Poly(y*x**2 - 2*x - 2*y, x)
68
+ assert roots_quadratic(f) == \
69
+ [-sqrt(2*y**2 + 1)/y + 1/y, sqrt(2*y**2 + 1)/y + 1/y]
70
+ f = Poly(x**2 + (-y**2 - 2)*x + y**2 + 1, x)
71
+ assert roots_quadratic(f) == \
72
+ [1,y**2 + 1]
73
+
74
+ f = Poly(sqrt(2)*x**2 - 1, x)
75
+ r = roots_quadratic(f)
76
+ assert r == _nsort(r)
77
+
78
+ # issue 8255
79
+ f = Poly(-24*x**2 - 180*x + 264)
80
+ assert [w.n(2) for w in f.all_roots(radicals=True)] == \
81
+ [w.n(2) for w in f.all_roots(radicals=False)]
82
+ for _a, _b, _c in product((-2, 2), (-2, 2), (0, -1)):
83
+ f = Poly(_a*x**2 + _b*x + _c)
84
+ roots = roots_quadratic(f)
85
+ assert roots == _nsort(roots)
86
+
87
+
88
+ def test_issue_7724():
89
+ eq = Poly(x**4*I + x**2 + I, x)
90
+ assert roots(eq) == {
91
+ sqrt(I/2 + sqrt(5)*I/2): 1,
92
+ sqrt(-sqrt(5)*I/2 + I/2): 1,
93
+ -sqrt(I/2 + sqrt(5)*I/2): 1,
94
+ -sqrt(-sqrt(5)*I/2 + I/2): 1}
95
+
96
+
97
+ def test_issue_8438():
98
+ p = Poly([1, y, -2, -3], x).as_expr()
99
+ roots = roots_cubic(Poly(p, x), x)
100
+ z = Rational(-3, 2) - I*7/2 # this will fail in code given in commit msg
101
+ post = [r.subs(y, z) for r in roots]
102
+ assert set(post) == \
103
+ set(roots_cubic(Poly(p.subs(y, z), x)))
104
+ # /!\ if p is not made an expression, this is *very* slow
105
+ assert all(p.subs({y: z, x: i}).n(2, chop=True) == 0 for i in post)
106
+
107
+
108
+ def test_issue_8285():
109
+ roots = (Poly(4*x**8 - 1, x)*Poly(x**2 + 1)).all_roots()
110
+ assert _check(roots)
111
+ f = Poly(x**4 + 5*x**2 + 6, x)
112
+ ro = [rootof(f, i) for i in range(4)]
113
+ roots = Poly(x**4 + 5*x**2 + 6, x).all_roots()
114
+ assert roots == ro
115
+ assert _check(roots)
116
+ # more than 2 complex roots from which to identify the
117
+ # imaginary ones
118
+ roots = Poly(2*x**8 - 1).all_roots()
119
+ assert _check(roots)
120
+ assert len(Poly(2*x**10 - 1).all_roots()) == 10 # doesn't fail
121
+
122
+
123
+ def test_issue_8289():
124
+ roots = (Poly(x**2 + 2)*Poly(x**4 + 2)).all_roots()
125
+ assert _check(roots)
126
+ roots = Poly(x**6 + 3*x**3 + 2, x).all_roots()
127
+ assert _check(roots)
128
+ roots = Poly(x**6 - x + 1).all_roots()
129
+ assert _check(roots)
130
+ # all imaginary roots with multiplicity of 2
131
+ roots = Poly(x**4 + 4*x**2 + 4, x).all_roots()
132
+ assert _check(roots)
133
+
134
+
135
+ def test_issue_14291():
136
+ assert Poly(((x - 1)**2 + 1)*((x - 1)**2 + 2)*(x - 1)
137
+ ).all_roots() == [1, 1 - I, 1 + I, 1 - sqrt(2)*I, 1 + sqrt(2)*I]
138
+ p = x**4 + 10*x**2 + 1
139
+ ans = [rootof(p, i) for i in range(4)]
140
+ assert Poly(p).all_roots() == ans
141
+ _check(ans)
142
+
143
+
144
+ def test_issue_13340():
145
+ eq = Poly(y**3 + exp(x)*y + x, y, domain='EX')
146
+ roots_d = roots(eq)
147
+ assert len(roots_d) == 3
148
+
149
+
150
+ def test_issue_14522():
151
+ eq = Poly(x**4 + x**3*(16 + 32*I) + x**2*(-285 + 386*I) + x*(-2824 - 448*I) - 2058 - 6053*I, x)
152
+ roots_eq = roots(eq)
153
+ assert all(eq(r) == 0 for r in roots_eq)
154
+
155
+
156
+ def test_issue_15076():
157
+ sol = roots_quartic(Poly(t**4 - 6*t**2 + t/x - 3, t))
158
+ assert sol[0].has(x)
159
+
160
+
161
+ def test_issue_16589():
162
+ eq = Poly(x**4 - 8*sqrt(2)*x**3 + 4*x**3 - 64*sqrt(2)*x**2 + 1024*x, x)
163
+ roots_eq = roots(eq)
164
+ assert 0 in roots_eq
165
+
166
+
167
+ def test_roots_cubic():
168
+ assert roots_cubic(Poly(2*x**3, x)) == [0, 0, 0]
169
+ assert roots_cubic(Poly(x**3 - 3*x**2 + 3*x - 1, x)) == [1, 1, 1]
170
+
171
+ # valid for arbitrary y (issue 21263)
172
+ r = root(y, 3)
173
+ assert roots_cubic(Poly(x**3 - y, x)) == [r,
174
+ r*(-S.Half + sqrt(3)*I/2),
175
+ r*(-S.Half - sqrt(3)*I/2)]
176
+ # simpler form when y is negative
177
+ assert roots_cubic(Poly(x**3 - -1, x)) == \
178
+ [-1, S.Half - I*sqrt(3)/2, S.Half + I*sqrt(3)/2]
179
+ assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \
180
+ S.Half + 3**Rational(1, 3)/2 + 3**Rational(2, 3)/2
181
+ eq = -x**3 + 2*x**2 + 3*x - 2
182
+ assert roots(eq, trig=True, multiple=True) == \
183
+ roots_cubic(Poly(eq, x), trig=True) == [
184
+ Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3,
185
+ -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3),
186
+ -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3),
187
+ ]
188
+
189
+
190
+ def test_roots_quartic():
191
+ assert roots_quartic(Poly(x**4, x)) == [0, 0, 0, 0]
192
+ assert roots_quartic(Poly(x**4 + x**3, x)) in [
193
+ [-1, 0, 0, 0],
194
+ [0, -1, 0, 0],
195
+ [0, 0, -1, 0],
196
+ [0, 0, 0, -1]
197
+ ]
198
+ assert roots_quartic(Poly(x**4 - x**3, x)) in [
199
+ [1, 0, 0, 0],
200
+ [0, 1, 0, 0],
201
+ [0, 0, 1, 0],
202
+ [0, 0, 0, 1]
203
+ ]
204
+
205
+ lhs = roots_quartic(Poly(x**4 + x, x))
206
+ rhs = [S.Half + I*sqrt(3)/2, S.Half - I*sqrt(3)/2, S.Zero, -S.One]
207
+
208
+ assert sorted(lhs, key=hash) == sorted(rhs, key=hash)
209
+
210
+ # test of all branches of roots quartic
211
+ for i, (a, b, c, d) in enumerate([(1, 2, 3, 0),
212
+ (3, -7, -9, 9),
213
+ (1, 2, 3, 4),
214
+ (1, 2, 3, 4),
215
+ (-7, -3, 3, -6),
216
+ (-3, 5, -6, -4),
217
+ (6, -5, -10, -3)]):
218
+ if i == 2:
219
+ c = -a*(a**2/S(8) - b/S(2))
220
+ elif i == 3:
221
+ d = a*(a*(a**2*Rational(3, 256) - b/S(16)) + c/S(4))
222
+ eq = x**4 + a*x**3 + b*x**2 + c*x + d
223
+ ans = roots_quartic(Poly(eq, x))
224
+ assert all(eq.subs(x, ai).n(chop=True) == 0 for ai in ans)
225
+
226
+ # not all symbolic quartics are unresolvable
227
+ eq = Poly(q*x + q/4 + x**4 + x**3 + 2*x**2 - Rational(1, 3), x)
228
+ sol = roots_quartic(eq)
229
+ assert all(verify_numerically(eq.subs(x, i), 0) for i in sol)
230
+ z = symbols('z', negative=True)
231
+ eq = x**4 + 2*x**3 + 3*x**2 + x*(z + 11) + 5
232
+ zans = roots_quartic(Poly(eq, x))
233
+ assert all(verify_numerically(eq.subs(((x, i), (z, -1))), 0) for i in zans)
234
+ # but some are (see also issue 4989)
235
+ # it's ok if the solution is not Piecewise, but the tests below should pass
236
+ eq = Poly(y*x**4 + x**3 - x + z, x)
237
+ ans = roots_quartic(eq)
238
+ assert all(type(i) == Piecewise for i in ans)
239
+ reps = (
240
+ {"y": Rational(-1, 3), "z": Rational(-1, 4)}, # 4 real
241
+ {"y": Rational(-1, 3), "z": Rational(-1, 2)}, # 2 real
242
+ {"y": Rational(-1, 3), "z": -2}) # 0 real
243
+ for rep in reps:
244
+ sol = roots_quartic(Poly(eq.subs(rep), x))
245
+ assert all(verify_numerically(w.subs(rep) - s, 0) for w, s in zip(ans, sol))
246
+
247
+
248
+ def test_issue_21287():
249
+ assert not any(isinstance(i, Piecewise) for i in roots_quartic(
250
+ Poly(x**4 - x**2*(3 + 5*I) + 2*x*(-1 + I) - 1 + 3*I, x)))
251
+
252
+
253
+ def test_roots_quintic():
254
+ eqs = (x**5 - 2,
255
+ (x/2 + 1)**5 - 5*(x/2 + 1) + 12,
256
+ x**5 - 110*x**3 - 55*x**2 + 2310*x + 979)
257
+ for eq in eqs:
258
+ roots = roots_quintic(Poly(eq))
259
+ assert len(roots) == 5
260
+ assert all(eq.subs(x, r.n(10)).n(chop = 1e-5) == 0 for r in roots)
261
+
262
+
263
+ def test_roots_cyclotomic():
264
+ assert roots_cyclotomic(cyclotomic_poly(1, x, polys=True)) == [1]
265
+ assert roots_cyclotomic(cyclotomic_poly(2, x, polys=True)) == [-1]
266
+ assert roots_cyclotomic(cyclotomic_poly(
267
+ 3, x, polys=True)) == [Rational(-1, 2) - I*sqrt(3)/2, Rational(-1, 2) + I*sqrt(3)/2]
268
+ assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True)) == [-I, I]
269
+ assert roots_cyclotomic(cyclotomic_poly(
270
+ 6, x, polys=True)) == [S.Half - I*sqrt(3)/2, S.Half + I*sqrt(3)/2]
271
+
272
+ assert roots_cyclotomic(cyclotomic_poly(7, x, polys=True)) == [
273
+ -cos(pi/7) - I*sin(pi/7),
274
+ -cos(pi/7) + I*sin(pi/7),
275
+ -cos(pi*Rational(3, 7)) - I*sin(pi*Rational(3, 7)),
276
+ -cos(pi*Rational(3, 7)) + I*sin(pi*Rational(3, 7)),
277
+ cos(pi*Rational(2, 7)) - I*sin(pi*Rational(2, 7)),
278
+ cos(pi*Rational(2, 7)) + I*sin(pi*Rational(2, 7)),
279
+ ]
280
+
281
+ assert roots_cyclotomic(cyclotomic_poly(8, x, polys=True)) == [
282
+ -sqrt(2)/2 - I*sqrt(2)/2,
283
+ -sqrt(2)/2 + I*sqrt(2)/2,
284
+ sqrt(2)/2 - I*sqrt(2)/2,
285
+ sqrt(2)/2 + I*sqrt(2)/2,
286
+ ]
287
+
288
+ assert roots_cyclotomic(cyclotomic_poly(12, x, polys=True)) == [
289
+ -sqrt(3)/2 - I/2,
290
+ -sqrt(3)/2 + I/2,
291
+ sqrt(3)/2 - I/2,
292
+ sqrt(3)/2 + I/2,
293
+ ]
294
+
295
+ assert roots_cyclotomic(
296
+ cyclotomic_poly(1, x, polys=True), factor=True) == [1]
297
+ assert roots_cyclotomic(
298
+ cyclotomic_poly(2, x, polys=True), factor=True) == [-1]
299
+
300
+ assert roots_cyclotomic(cyclotomic_poly(3, x, polys=True), factor=True) == \
301
+ [-root(-1, 3), -1 + root(-1, 3)]
302
+ assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True), factor=True) == \
303
+ [-I, I]
304
+ assert roots_cyclotomic(cyclotomic_poly(5, x, polys=True), factor=True) == \
305
+ [-root(-1, 5), -root(-1, 5)**3, root(-1, 5)**2, -1 - root(-1, 5)**2 + root(-1, 5) + root(-1, 5)**3]
306
+
307
+ assert roots_cyclotomic(cyclotomic_poly(6, x, polys=True), factor=True) == \
308
+ [1 - root(-1, 3), root(-1, 3)]
309
+
310
+
311
+ def test_roots_binomial():
312
+ assert roots_binomial(Poly(5*x, x)) == [0]
313
+ assert roots_binomial(Poly(5*x**4, x)) == [0, 0, 0, 0]
314
+ assert roots_binomial(Poly(5*x + 2, x)) == [Rational(-2, 5)]
315
+
316
+ A = 10**Rational(3, 4)/10
317
+
318
+ assert roots_binomial(Poly(5*x**4 + 2, x)) == \
319
+ [-A - A*I, -A + A*I, A - A*I, A + A*I]
320
+ _check(roots_binomial(Poly(x**8 - 2)))
321
+
322
+ a1 = Symbol('a1', nonnegative=True)
323
+ b1 = Symbol('b1', nonnegative=True)
324
+
325
+ r0 = roots_quadratic(Poly(a1*x**2 + b1, x))
326
+ r1 = roots_binomial(Poly(a1*x**2 + b1, x))
327
+
328
+ assert powsimp(r0[0]) == powsimp(r1[0])
329
+ assert powsimp(r0[1]) == powsimp(r1[1])
330
+ for a, b, s, n in product((1, 2), (1, 2), (-1, 1), (2, 3, 4, 5)):
331
+ if a == b and a != 1: # a == b == 1 is sufficient
332
+ continue
333
+ p = Poly(a*x**n + s*b)
334
+ ans = roots_binomial(p)
335
+ assert ans == _nsort(ans)
336
+
337
+ # issue 8813
338
+ assert roots(Poly(2*x**3 - 16*y**3, x)) == {
339
+ 2*y*(Rational(-1, 2) - sqrt(3)*I/2): 1,
340
+ 2*y: 1,
341
+ 2*y*(Rational(-1, 2) + sqrt(3)*I/2): 1}
342
+
343
+
344
+ def test_roots_preprocessing():
345
+ f = a*y*x**2 + y - b
346
+
347
+ coeff, poly = preprocess_roots(Poly(f, x))
348
+
349
+ assert coeff == 1
350
+ assert poly == Poly(a*y*x**2 + y - b, x)
351
+
352
+ f = c**3*x**3 + c**2*x**2 + c*x + a
353
+
354
+ coeff, poly = preprocess_roots(Poly(f, x))
355
+
356
+ assert coeff == 1/c
357
+ assert poly == Poly(x**3 + x**2 + x + a, x)
358
+
359
+ f = c**3*x**3 + c**2*x**2 + a
360
+
361
+ coeff, poly = preprocess_roots(Poly(f, x))
362
+
363
+ assert coeff == 1/c
364
+ assert poly == Poly(x**3 + x**2 + a, x)
365
+
366
+ f = c**3*x**3 + c*x + a
367
+
368
+ coeff, poly = preprocess_roots(Poly(f, x))
369
+
370
+ assert coeff == 1/c
371
+ assert poly == Poly(x**3 + x + a, x)
372
+
373
+ f = c**3*x**3 + a
374
+
375
+ coeff, poly = preprocess_roots(Poly(f, x))
376
+
377
+ assert coeff == 1/c
378
+ assert poly == Poly(x**3 + a, x)
379
+
380
+ E, F, J, L = symbols("E,F,J,L")
381
+
382
+ f = -21601054687500000000*E**8*J**8/L**16 + \
383
+ 508232812500000000*F*x*E**7*J**7/L**14 - \
384
+ 4269543750000000*E**6*F**2*J**6*x**2/L**12 + \
385
+ 16194716250000*E**5*F**3*J**5*x**3/L**10 - \
386
+ 27633173750*E**4*F**4*J**4*x**4/L**8 + \
387
+ 14840215*E**3*F**5*J**3*x**5/L**6 + \
388
+ 54794*E**2*F**6*J**2*x**6/(5*L**4) - \
389
+ 1153*E*J*F**7*x**7/(80*L**2) + \
390
+ 633*F**8*x**8/160000
391
+
392
+ coeff, poly = preprocess_roots(Poly(f, x))
393
+
394
+ assert coeff == 20*E*J/(F*L**2)
395
+ assert poly == 633*x**8 - 115300*x**7 + 4383520*x**6 + 296804300*x**5 - 27633173750*x**4 + \
396
+ 809735812500*x**3 - 10673859375000*x**2 + 63529101562500*x - 135006591796875
397
+
398
+ f = Poly(-y**2 + x**2*exp(x), y, domain=ZZ[x, exp(x)])
399
+ g = Poly(-y**2 + exp(x), y, domain=ZZ[exp(x)])
400
+
401
+ assert preprocess_roots(f) == (x, g)
402
+
403
+
404
+ def test_roots0():
405
+ assert roots(1, x) == {}
406
+ assert roots(x, x) == {S.Zero: 1}
407
+ assert roots(x**9, x) == {S.Zero: 9}
408
+ assert roots(((x - 2)*(x + 3)*(x - 4)).expand(), x) == {-S(3): 1, S(2): 1, S(4): 1}
409
+
410
+ assert roots(2*x + 1, x) == {Rational(-1, 2): 1}
411
+ assert roots((2*x + 1)**2, x) == {Rational(-1, 2): 2}
412
+ assert roots((2*x + 1)**5, x) == {Rational(-1, 2): 5}
413
+ assert roots((2*x + 1)**10, x) == {Rational(-1, 2): 10}
414
+
415
+ assert roots(x**4 - 1, x) == {I: 1, S.One: 1, -S.One: 1, -I: 1}
416
+ assert roots((x**4 - 1)**2, x) == {I: 2, S.One: 2, -S.One: 2, -I: 2}
417
+
418
+ assert roots(((2*x - 3)**2).expand(), x) == {Rational( 3, 2): 2}
419
+ assert roots(((2*x + 3)**2).expand(), x) == {Rational(-3, 2): 2}
420
+
421
+ assert roots(((2*x - 3)**3).expand(), x) == {Rational( 3, 2): 3}
422
+ assert roots(((2*x + 3)**3).expand(), x) == {Rational(-3, 2): 3}
423
+
424
+ assert roots(((2*x - 3)**5).expand(), x) == {Rational( 3, 2): 5}
425
+ assert roots(((2*x + 3)**5).expand(), x) == {Rational(-3, 2): 5}
426
+
427
+ assert roots(((a*x - b)**5).expand(), x) == { b/a: 5}
428
+ assert roots(((a*x + b)**5).expand(), x) == {-b/a: 5}
429
+
430
+ assert roots(x**2 + (-a - 1)*x + a, x) == {a: 1, S.One: 1}
431
+
432
+ assert roots(x**4 - 2*x**2 + 1, x) == {S.One: 2, S.NegativeOne: 2}
433
+
434
+ assert roots(x**6 - 4*x**4 + 4*x**3 - x**2, x) == \
435
+ {S.One: 2, -1 - sqrt(2): 1, S.Zero: 2, -1 + sqrt(2): 1}
436
+
437
+ assert roots(x**8 - 1, x) == {
438
+ sqrt(2)/2 + I*sqrt(2)/2: 1,
439
+ sqrt(2)/2 - I*sqrt(2)/2: 1,
440
+ -sqrt(2)/2 + I*sqrt(2)/2: 1,
441
+ -sqrt(2)/2 - I*sqrt(2)/2: 1,
442
+ S.One: 1, -S.One: 1, I: 1, -I: 1
443
+ }
444
+
445
+ f = -2016*x**2 - 5616*x**3 - 2056*x**4 + 3324*x**5 + 2176*x**6 - \
446
+ 224*x**7 - 384*x**8 - 64*x**9
447
+
448
+ assert roots(f) == {S.Zero: 2, -S(2): 2, S(2): 1, Rational(-7, 2): 1,
449
+ Rational(-3, 2): 1, Rational(-1, 2): 1, Rational(3, 2): 1}
450
+
451
+ assert roots((a + b + c)*x - (a + b + c + d), x) == {(a + b + c + d)/(a + b + c): 1}
452
+
453
+ assert roots(x**3 + x**2 - x + 1, x, cubics=False) == {}
454
+ assert roots(((x - 2)*(
455
+ x + 3)*(x - 4)).expand(), x, cubics=False) == {-S(3): 1, S(2): 1, S(4): 1}
456
+ assert roots(((x - 2)*(x + 3)*(x - 4)*(x - 5)).expand(), x, cubics=False) == \
457
+ {-S(3): 1, S(2): 1, S(4): 1, S(5): 1}
458
+ assert roots(x**3 + 2*x**2 + 4*x + 8, x) == {-S(2): 1, -2*I: 1, 2*I: 1}
459
+ assert roots(x**3 + 2*x**2 + 4*x + 8, x, cubics=True) == \
460
+ {-2*I: 1, 2*I: 1, -S(2): 1}
461
+ assert roots((x**2 - x)*(x**3 + 2*x**2 + 4*x + 8), x ) == \
462
+ {S.One: 1, S.Zero: 1, -S(2): 1, -2*I: 1, 2*I: 1}
463
+
464
+ r1_2, r1_3 = S.Half, Rational(1, 3)
465
+
466
+ x0 = (3*sqrt(33) + 19)**r1_3
467
+ x1 = 4/x0/3
468
+ x2 = x0/3
469
+ x3 = sqrt(3)*I/2
470
+ x4 = x3 - r1_2
471
+ x5 = -x3 - r1_2
472
+ assert roots(x**3 + x**2 - x + 1, x, cubics=True) == {
473
+ -x1 - x2 - r1_3: 1,
474
+ -x1/x4 - x2*x4 - r1_3: 1,
475
+ -x1/x5 - x2*x5 - r1_3: 1,
476
+ }
477
+
478
+ f = (x**2 + 2*x + 3).subs(x, 2*x**2 + 3*x).subs(x, 5*x - 4)
479
+
480
+ r13_20, r1_20 = [ Rational(*r)
481
+ for r in ((13, 20), (1, 20)) ]
482
+
483
+ s2 = sqrt(2)
484
+ assert roots(f, x) == {
485
+ r13_20 + r1_20*sqrt(1 - 8*I*s2): 1,
486
+ r13_20 - r1_20*sqrt(1 - 8*I*s2): 1,
487
+ r13_20 + r1_20*sqrt(1 + 8*I*s2): 1,
488
+ r13_20 - r1_20*sqrt(1 + 8*I*s2): 1,
489
+ }
490
+
491
+ f = x**4 + x**3 + x**2 + x + 1
492
+
493
+ r1_4, r1_8, r5_8 = [ Rational(*r) for r in ((1, 4), (1, 8), (5, 8)) ]
494
+
495
+ assert roots(f, x) == {
496
+ -r1_4 + r1_4*5**r1_2 + I*(r5_8 + r1_8*5**r1_2)**r1_2: 1,
497
+ -r1_4 + r1_4*5**r1_2 - I*(r5_8 + r1_8*5**r1_2)**r1_2: 1,
498
+ -r1_4 - r1_4*5**r1_2 + I*(r5_8 - r1_8*5**r1_2)**r1_2: 1,
499
+ -r1_4 - r1_4*5**r1_2 - I*(r5_8 - r1_8*5**r1_2)**r1_2: 1,
500
+ }
501
+
502
+ f = z**3 + (-2 - y)*z**2 + (1 + 2*y - 2*x**2)*z - y + 2*x**2
503
+
504
+ assert roots(f, z) == {
505
+ S.One: 1,
506
+ S.Half + S.Half*y + S.Half*sqrt(1 - 2*y + y**2 + 8*x**2): 1,
507
+ S.Half + S.Half*y - S.Half*sqrt(1 - 2*y + y**2 + 8*x**2): 1,
508
+ }
509
+
510
+ assert roots(a*b*c*x**3 + 2*x**2 + 4*x + 8, x, cubics=False) == {}
511
+ assert roots(a*b*c*x**3 + 2*x**2 + 4*x + 8, x, cubics=True) != {}
512
+
513
+ assert roots(x**4 - 1, x, filter='Z') == {S.One: 1, -S.One: 1}
514
+ assert roots(x**4 - 1, x, filter='I') == {I: 1, -I: 1}
515
+
516
+ assert roots((x - 1)*(x + 1), x) == {S.One: 1, -S.One: 1}
517
+ assert roots(
518
+ (x - 1)*(x + 1), x, predicate=lambda r: r.is_positive) == {S.One: 1}
519
+
520
+ assert roots(x**4 - 1, x, filter='Z', multiple=True) == [-S.One, S.One]
521
+ assert roots(x**4 - 1, x, filter='I', multiple=True) == [I, -I]
522
+
523
+ ar, br = symbols('a, b', real=True)
524
+ p = x**2*(ar-br)**2 + 2*x*(br-ar) + 1
525
+ assert roots(p, x, filter='R') == {1/(ar - br): 2}
526
+
527
+ assert roots(x**3, x, multiple=True) == [S.Zero, S.Zero, S.Zero]
528
+ assert roots(1234, x, multiple=True) == []
529
+
530
+ f = x**6 - x**5 + x**4 - x**3 + x**2 - x + 1
531
+
532
+ assert roots(f) == {
533
+ -I*sin(pi/7) + cos(pi/7): 1,
534
+ -I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 1,
535
+ -I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 1,
536
+ I*sin(pi/7) + cos(pi/7): 1,
537
+ I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 1,
538
+ I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 1,
539
+ }
540
+
541
+ g = ((x**2 + 1)*f**2).expand()
542
+
543
+ assert roots(g) == {
544
+ -I*sin(pi/7) + cos(pi/7): 2,
545
+ -I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 2,
546
+ -I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 2,
547
+ I*sin(pi/7) + cos(pi/7): 2,
548
+ I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 2,
549
+ I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 2,
550
+ -I: 1, I: 1,
551
+ }
552
+
553
+ r = roots(x**3 + 40*x + 64)
554
+ real_root = [rx for rx in r if rx.is_real][0]
555
+ cr = 108 + 6*sqrt(1074)
556
+ assert real_root == -2*root(cr, 3)/3 + 20/root(cr, 3)
557
+
558
+ eq = Poly((7 + 5*sqrt(2))*x**3 + (-6 - 4*sqrt(2))*x**2 + (-sqrt(2) - 1)*x + 2, x, domain='EX')
559
+ assert roots(eq) == {-1 + sqrt(2): 1, -2 + 2*sqrt(2): 1, -sqrt(2) + 1: 1}
560
+
561
+ eq = Poly(41*x**5 + 29*sqrt(2)*x**5 - 153*x**4 - 108*sqrt(2)*x**4 +
562
+ 175*x**3 + 125*sqrt(2)*x**3 - 45*x**2 - 30*sqrt(2)*x**2 - 26*sqrt(2)*x -
563
+ 26*x + 24, x, domain='EX')
564
+ assert roots(eq) == {-sqrt(2) + 1: 1, -2 + 2*sqrt(2): 1, -1 + sqrt(2): 1,
565
+ -4 + 4*sqrt(2): 1, -3 + 3*sqrt(2): 1}
566
+
567
+ eq = Poly(x**3 - 2*x**2 + 6*sqrt(2)*x**2 - 8*sqrt(2)*x + 23*x - 14 +
568
+ 14*sqrt(2), x, domain='EX')
569
+ assert roots(eq) == {-2*sqrt(2) + 2: 1, -2*sqrt(2) + 1: 1, -2*sqrt(2) - 1: 1}
570
+
571
+ assert roots(Poly((x + sqrt(2))**3 - 7, x, domain='EX')) == \
572
+ {-sqrt(2) + root(7, 3)*(-S.Half - sqrt(3)*I/2): 1,
573
+ -sqrt(2) + root(7, 3)*(-S.Half + sqrt(3)*I/2): 1,
574
+ -sqrt(2) + root(7, 3): 1}
575
+
576
+ def test_roots_slow():
577
+ """Just test that calculating these roots does not hang. """
578
+ a, b, c, d, x = symbols("a,b,c,d,x")
579
+
580
+ f1 = x**2*c + (a/b) + x*c*d - a
581
+ f2 = x**2*(a + b*(c - d)*a) + x*a*b*c/(b*d - d) + (a*d - c/d)
582
+
583
+ assert list(roots(f1, x).values()) == [1, 1]
584
+ assert list(roots(f2, x).values()) == [1, 1]
585
+
586
+ (zz, yy, xx, zy, zx, yx, k) = symbols("zz,yy,xx,zy,zx,yx,k")
587
+
588
+ e1 = (zz - k)*(yy - k)*(xx - k) + zy*yx*zx + zx - zy - yx
589
+ e2 = (zz - k)*yx*yx + zx*(yy - k)*zx + zy*zy*(xx - k)
590
+
591
+ assert list(roots(e1 - e2, k).values()) == [1, 1, 1]
592
+
593
+ f = x**3 + 2*x**2 + 8
594
+ R = list(roots(f).keys())
595
+
596
+ assert not any(i for i in [f.subs(x, ri).n(chop=True) for ri in R])
597
+
598
+
599
+ def test_roots_inexact():
600
+ R1 = roots(x**2 + x + 1, x, multiple=True)
601
+ R2 = roots(x**2 + x + 1.0, x, multiple=True)
602
+
603
+ for r1, r2 in zip(R1, R2):
604
+ assert abs(r1 - r2) < 1e-12
605
+
606
+ f = x**4 + 3.0*sqrt(2.0)*x**3 - (78.0 + 24.0*sqrt(3.0))*x**2 \
607
+ + 144.0*(2*sqrt(3.0) + 9.0)
608
+
609
+ R1 = roots(f, multiple=True)
610
+ R2 = (-12.7530479110482, -3.85012393732929,
611
+ 4.89897948556636, 7.46155167569183)
612
+
613
+ for r1, r2 in zip(R1, R2):
614
+ assert abs(r1 - r2) < 1e-10
615
+
616
+
617
+ def test_roots_preprocessed():
618
+ E, F, J, L = symbols("E,F,J,L")
619
+
620
+ f = -21601054687500000000*E**8*J**8/L**16 + \
621
+ 508232812500000000*F*x*E**7*J**7/L**14 - \
622
+ 4269543750000000*E**6*F**2*J**6*x**2/L**12 + \
623
+ 16194716250000*E**5*F**3*J**5*x**3/L**10 - \
624
+ 27633173750*E**4*F**4*J**4*x**4/L**8 + \
625
+ 14840215*E**3*F**5*J**3*x**5/L**6 + \
626
+ 54794*E**2*F**6*J**2*x**6/(5*L**4) - \
627
+ 1153*E*J*F**7*x**7/(80*L**2) + \
628
+ 633*F**8*x**8/160000
629
+
630
+ assert roots(f, x) == {}
631
+
632
+ R1 = roots(f.evalf(), x, multiple=True)
633
+ R2 = [-1304.88375606366, 97.1168816800648, 186.946430171876, 245.526792947065,
634
+ 503.441004174773, 791.549343830097, 1273.16678129348, 1850.10650616851]
635
+
636
+ w = Wild('w')
637
+ p = w*E*J/(F*L**2)
638
+
639
+ assert len(R1) == len(R2)
640
+
641
+ for r1, r2 in zip(R1, R2):
642
+ match = r1.match(p)
643
+ assert match is not None and abs(match[w] - r2) < 1e-10
644
+
645
+
646
+ def test_roots_strict():
647
+ assert roots(x**2 - 2*x + 1, strict=False) == {1: 2}
648
+ assert roots(x**2 - 2*x + 1, strict=True) == {1: 2}
649
+
650
+ assert roots(x**6 - 2*x**5 - x**2 + 3*x - 2, strict=False) == {2: 1}
651
+ raises(UnsolvableFactorError, lambda: roots(x**6 - 2*x**5 - x**2 + 3*x - 2, strict=True))
652
+
653
+
654
+ def test_roots_mixed():
655
+ f = -1936 - 5056*x - 7592*x**2 + 2704*x**3 - 49*x**4
656
+
657
+ _re, _im = intervals(f, all=True)
658
+ _nroots = nroots(f)
659
+ _sroots = roots(f, multiple=True)
660
+
661
+ _re = [ Interval(a, b) for (a, b), _ in _re ]
662
+ _im = [ Interval(re(a), re(b))*Interval(im(a), im(b)) for (a, b),
663
+ _ in _im ]
664
+
665
+ _intervals = _re + _im
666
+ _sroots = [ r.evalf() for r in _sroots ]
667
+
668
+ _nroots = sorted(_nroots, key=lambda x: x.sort_key())
669
+ _sroots = sorted(_sroots, key=lambda x: x.sort_key())
670
+
671
+ for _roots in (_nroots, _sroots):
672
+ for i, r in zip(_intervals, _roots):
673
+ if r.is_real:
674
+ assert r in i
675
+ else:
676
+ assert (re(r), im(r)) in i
677
+
678
+
679
+ def test_root_factors():
680
+ assert root_factors(Poly(1, x)) == [Poly(1, x)]
681
+ assert root_factors(Poly(x, x)) == [Poly(x, x)]
682
+
683
+ assert root_factors(x**2 - 1, x) == [x + 1, x - 1]
684
+ assert root_factors(x**2 - y, x) == [x - sqrt(y), x + sqrt(y)]
685
+
686
+ assert root_factors((x**4 - 1)**2) == \
687
+ [x + 1, x + 1, x - 1, x - 1, x - I, x - I, x + I, x + I]
688
+
689
+ assert root_factors(Poly(x**4 - 1, x), filter='Z') == \
690
+ [Poly(x + 1, x), Poly(x - 1, x), Poly(x**2 + 1, x)]
691
+ assert root_factors(8*x**2 + 12*x**4 + 6*x**6 + x**8, x, filter='Q') == \
692
+ [x, x, x**6 + 6*x**4 + 12*x**2 + 8]
693
+
694
+
695
+ @slow
696
+ def test_nroots1():
697
+ n = 64
698
+ p = legendre_poly(n, x, polys=True)
699
+
700
+ raises(mpmath.mp.NoConvergence, lambda: p.nroots(n=3, maxsteps=5))
701
+
702
+ roots = p.nroots(n=3)
703
+ # The order of roots matters. They are ordered from smallest to the
704
+ # largest.
705
+ assert [str(r) for r in roots] == \
706
+ ['-0.999', '-0.996', '-0.991', '-0.983', '-0.973', '-0.961',
707
+ '-0.946', '-0.930', '-0.911', '-0.889', '-0.866', '-0.841',
708
+ '-0.813', '-0.784', '-0.753', '-0.720', '-0.685', '-0.649',
709
+ '-0.611', '-0.572', '-0.531', '-0.489', '-0.446', '-0.402',
710
+ '-0.357', '-0.311', '-0.265', '-0.217', '-0.170', '-0.121',
711
+ '-0.0730', '-0.0243', '0.0243', '0.0730', '0.121', '0.170',
712
+ '0.217', '0.265', '0.311', '0.357', '0.402', '0.446', '0.489',
713
+ '0.531', '0.572', '0.611', '0.649', '0.685', '0.720', '0.753',
714
+ '0.784', '0.813', '0.841', '0.866', '0.889', '0.911', '0.930',
715
+ '0.946', '0.961', '0.973', '0.983', '0.991', '0.996', '0.999']
716
+
717
+ def test_nroots2():
718
+ p = Poly(x**5 + 3*x + 1, x)
719
+
720
+ roots = p.nroots(n=3)
721
+ # The order of roots matters. The roots are ordered by their real
722
+ # components (if they agree, then by their imaginary components),
723
+ # with real roots appearing first.
724
+ assert [str(r) for r in roots] == \
725
+ ['-0.332', '-0.839 - 0.944*I', '-0.839 + 0.944*I',
726
+ '1.01 - 0.937*I', '1.01 + 0.937*I']
727
+
728
+ roots = p.nroots(n=5)
729
+ assert [str(r) for r in roots] == \
730
+ ['-0.33199', '-0.83907 - 0.94385*I', '-0.83907 + 0.94385*I',
731
+ '1.0051 - 0.93726*I', '1.0051 + 0.93726*I']
732
+
733
+
734
+ def test_roots_composite():
735
+ assert len(roots(Poly(y**3 + y**2*sqrt(x) + y + x, y, composite=True))) == 3
736
+
737
+
738
+ def test_issue_19113():
739
+ eq = cos(x)**3 - cos(x) + 1
740
+ raises(PolynomialError, lambda: roots(eq))
741
+
742
+
743
+ def test_issue_17454():
744
+ assert roots([1, -3*(-4 - 4*I)**2/8 + 12*I, 0], multiple=True) == [0, 0]
745
+
746
+
747
+ def test_issue_20913():
748
+ assert Poly(x + 9671406556917067856609794, x).real_roots() == [-9671406556917067856609794]
749
+ assert Poly(x**3 + 4, x).real_roots() == [-2**(S(2)/3)]
750
+
751
+
752
+ def test_issue_22768():
753
+ e = Rational(1, 3)
754
+ r = (-1/a)**e*(a + 1)**(5*e)
755
+ assert roots(Poly(a*x**3 + (a + 1)**5, x)) == {
756
+ r: 1,
757
+ -r*(1 + sqrt(3)*I)/2: 1,
758
+ r*(-1 + sqrt(3)*I)/2: 1}
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polytools.py ADDED
The diff for this file is too large to render. See raw diff
 
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_polyutils.py ADDED
@@ -0,0 +1,300 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for useful utilities for higher level polynomial classes. """
2
+
3
+ from sympy.core.mul import Mul
4
+ from sympy.core.numbers import (Integer, pi)
5
+ from sympy.core.relational import Eq
6
+ from sympy.core.singleton import S
7
+ from sympy.core.symbol import (Symbol, symbols)
8
+ from sympy.functions.elementary.exponential import exp
9
+ from sympy.functions.elementary.miscellaneous import sqrt
10
+ from sympy.functions.elementary.trigonometric import (cos, sin)
11
+ from sympy.integrals.integrals import Integral
12
+ from sympy.testing.pytest import raises
13
+
14
+ from sympy.polys.polyutils import (
15
+ _nsort,
16
+ _sort_gens,
17
+ _unify_gens,
18
+ _analyze_gens,
19
+ _sort_factors,
20
+ parallel_dict_from_expr,
21
+ dict_from_expr,
22
+ )
23
+
24
+ from sympy.polys.polyerrors import PolynomialError
25
+
26
+ from sympy.polys.domains import ZZ
27
+
28
+ x, y, z, p, q, r, s, t, u, v, w = symbols('x,y,z,p,q,r,s,t,u,v,w')
29
+ A, B = symbols('A,B', commutative=False)
30
+
31
+
32
+ def test__nsort():
33
+ # issue 6137
34
+ r = S('''[3/2 + sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) - 4/sqrt(-7/3 +
35
+ 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3)) -
36
+ 61/(18*(-415/216 + 13*I/12)**(1/3)))/2 - sqrt(-7/3 + 61/(18*(-415/216
37
+ + 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3))/2, 3/2 - sqrt(-7/3
38
+ + 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
39
+ 13*I/12)**(1/3))/2 - sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) -
40
+ 4/sqrt(-7/3 + 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
41
+ 13*I/12)**(1/3)) - 61/(18*(-415/216 + 13*I/12)**(1/3)))/2, 3/2 +
42
+ sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) + 4/sqrt(-7/3 +
43
+ 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3)) -
44
+ 61/(18*(-415/216 + 13*I/12)**(1/3)))/2 + sqrt(-7/3 + 61/(18*(-415/216
45
+ + 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3))/2, 3/2 + sqrt(-7/3
46
+ + 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
47
+ 13*I/12)**(1/3))/2 - sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) +
48
+ 4/sqrt(-7/3 + 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
49
+ 13*I/12)**(1/3)) - 61/(18*(-415/216 + 13*I/12)**(1/3)))/2]''')
50
+ ans = [r[1], r[0], r[-1], r[-2]]
51
+ assert _nsort(r) == ans
52
+ assert len(_nsort(r, separated=True)[0]) == 0
53
+ b, c, a = exp(-1000), exp(-999), exp(-1001)
54
+ assert _nsort((b, c, a)) == [a, b, c]
55
+ # issue 12560
56
+ a = cos(1)**2 + sin(1)**2 - 1
57
+ assert _nsort([a]) == [a]
58
+
59
+
60
+ def test__sort_gens():
61
+ assert _sort_gens([]) == ()
62
+
63
+ assert _sort_gens([x]) == (x,)
64
+ assert _sort_gens([p]) == (p,)
65
+ assert _sort_gens([q]) == (q,)
66
+
67
+ assert _sort_gens([x, p]) == (x, p)
68
+ assert _sort_gens([p, x]) == (x, p)
69
+ assert _sort_gens([q, p]) == (p, q)
70
+
71
+ assert _sort_gens([q, p, x]) == (x, p, q)
72
+
73
+ assert _sort_gens([x, p, q], wrt=x) == (x, p, q)
74
+ assert _sort_gens([x, p, q], wrt=p) == (p, x, q)
75
+ assert _sort_gens([x, p, q], wrt=q) == (q, x, p)
76
+
77
+ assert _sort_gens([x, p, q], wrt='x') == (x, p, q)
78
+ assert _sort_gens([x, p, q], wrt='p') == (p, x, q)
79
+ assert _sort_gens([x, p, q], wrt='q') == (q, x, p)
80
+
81
+ assert _sort_gens([x, p, q], wrt='x,q') == (x, q, p)
82
+ assert _sort_gens([x, p, q], wrt='q,x') == (q, x, p)
83
+ assert _sort_gens([x, p, q], wrt='p,q') == (p, q, x)
84
+ assert _sort_gens([x, p, q], wrt='q,p') == (q, p, x)
85
+
86
+ assert _sort_gens([x, p, q], wrt='x, q') == (x, q, p)
87
+ assert _sort_gens([x, p, q], wrt='q, x') == (q, x, p)
88
+ assert _sort_gens([x, p, q], wrt='p, q') == (p, q, x)
89
+ assert _sort_gens([x, p, q], wrt='q, p') == (q, p, x)
90
+
91
+ assert _sort_gens([x, p, q], wrt=[x, 'q']) == (x, q, p)
92
+ assert _sort_gens([x, p, q], wrt=[q, 'x']) == (q, x, p)
93
+ assert _sort_gens([x, p, q], wrt=[p, 'q']) == (p, q, x)
94
+ assert _sort_gens([x, p, q], wrt=[q, 'p']) == (q, p, x)
95
+
96
+ assert _sort_gens([x, p, q], wrt=['x', 'q']) == (x, q, p)
97
+ assert _sort_gens([x, p, q], wrt=['q', 'x']) == (q, x, p)
98
+ assert _sort_gens([x, p, q], wrt=['p', 'q']) == (p, q, x)
99
+ assert _sort_gens([x, p, q], wrt=['q', 'p']) == (q, p, x)
100
+
101
+ assert _sort_gens([x, p, q], sort='x > p > q') == (x, p, q)
102
+ assert _sort_gens([x, p, q], sort='p > x > q') == (p, x, q)
103
+ assert _sort_gens([x, p, q], sort='p > q > x') == (p, q, x)
104
+
105
+ assert _sort_gens([x, p, q], wrt='x', sort='q > p') == (x, q, p)
106
+ assert _sort_gens([x, p, q], wrt='p', sort='q > x') == (p, q, x)
107
+ assert _sort_gens([x, p, q], wrt='q', sort='p > x') == (q, p, x)
108
+
109
+ # https://github.com/sympy/sympy/issues/19353
110
+ n1 = Symbol('\n1')
111
+ assert _sort_gens([n1]) == (n1,)
112
+ assert _sort_gens([x, n1]) == (x, n1)
113
+
114
+ X = symbols('x0,x1,x2,x10,x11,x12,x20,x21,x22')
115
+
116
+ assert _sort_gens(X) == X
117
+
118
+
119
+ def test__unify_gens():
120
+ assert _unify_gens([], []) == ()
121
+
122
+ assert _unify_gens([x], [x]) == (x,)
123
+ assert _unify_gens([y], [y]) == (y,)
124
+
125
+ assert _unify_gens([x, y], [x]) == (x, y)
126
+ assert _unify_gens([x], [x, y]) == (x, y)
127
+
128
+ assert _unify_gens([x, y], [x, y]) == (x, y)
129
+ assert _unify_gens([y, x], [y, x]) == (y, x)
130
+
131
+ assert _unify_gens([x], [y]) == (x, y)
132
+ assert _unify_gens([y], [x]) == (y, x)
133
+
134
+ assert _unify_gens([x], [y, x]) == (y, x)
135
+ assert _unify_gens([y, x], [x]) == (y, x)
136
+
137
+ assert _unify_gens([x, y, z], [x, y, z]) == (x, y, z)
138
+ assert _unify_gens([z, y, x], [x, y, z]) == (z, y, x)
139
+ assert _unify_gens([x, y, z], [z, y, x]) == (x, y, z)
140
+ assert _unify_gens([z, y, x], [z, y, x]) == (z, y, x)
141
+
142
+ assert _unify_gens([x, y, z], [t, x, p, q, z]) == (t, x, y, p, q, z)
143
+
144
+
145
+ def test__analyze_gens():
146
+ assert _analyze_gens((x, y, z)) == (x, y, z)
147
+ assert _analyze_gens([x, y, z]) == (x, y, z)
148
+
149
+ assert _analyze_gens(([x, y, z],)) == (x, y, z)
150
+ assert _analyze_gens(((x, y, z),)) == (x, y, z)
151
+
152
+
153
+ def test__sort_factors():
154
+ assert _sort_factors([], multiple=True) == []
155
+ assert _sort_factors([], multiple=False) == []
156
+
157
+ F = [[1, 2, 3], [1, 2], [1]]
158
+ G = [[1], [1, 2], [1, 2, 3]]
159
+
160
+ assert _sort_factors(F, multiple=False) == G
161
+
162
+ F = [[1, 2], [1, 2, 3], [1, 2], [1]]
163
+ G = [[1], [1, 2], [1, 2], [1, 2, 3]]
164
+
165
+ assert _sort_factors(F, multiple=False) == G
166
+
167
+ F = [[2, 2], [1, 2, 3], [1, 2], [1]]
168
+ G = [[1], [1, 2], [2, 2], [1, 2, 3]]
169
+
170
+ assert _sort_factors(F, multiple=False) == G
171
+
172
+ F = [([1, 2, 3], 1), ([1, 2], 1), ([1], 1)]
173
+ G = [([1], 1), ([1, 2], 1), ([1, 2, 3], 1)]
174
+
175
+ assert _sort_factors(F, multiple=True) == G
176
+
177
+ F = [([1, 2], 1), ([1, 2, 3], 1), ([1, 2], 1), ([1], 1)]
178
+ G = [([1], 1), ([1, 2], 1), ([1, 2], 1), ([1, 2, 3], 1)]
179
+
180
+ assert _sort_factors(F, multiple=True) == G
181
+
182
+ F = [([2, 2], 1), ([1, 2, 3], 1), ([1, 2], 1), ([1], 1)]
183
+ G = [([1], 1), ([1, 2], 1), ([2, 2], 1), ([1, 2, 3], 1)]
184
+
185
+ assert _sort_factors(F, multiple=True) == G
186
+
187
+ F = [([2, 2], 1), ([1, 2, 3], 1), ([1, 2], 2), ([1], 1)]
188
+ G = [([1], 1), ([2, 2], 1), ([1, 2], 2), ([1, 2, 3], 1)]
189
+
190
+ assert _sort_factors(F, multiple=True) == G
191
+
192
+
193
+ def test__dict_from_expr_if_gens():
194
+ assert dict_from_expr(
195
+ Integer(17), gens=(x,)) == ({(0,): Integer(17)}, (x,))
196
+ assert dict_from_expr(
197
+ Integer(17), gens=(x, y)) == ({(0, 0): Integer(17)}, (x, y))
198
+ assert dict_from_expr(
199
+ Integer(17), gens=(x, y, z)) == ({(0, 0, 0): Integer(17)}, (x, y, z))
200
+
201
+ assert dict_from_expr(
202
+ Integer(-17), gens=(x,)) == ({(0,): Integer(-17)}, (x,))
203
+ assert dict_from_expr(
204
+ Integer(-17), gens=(x, y)) == ({(0, 0): Integer(-17)}, (x, y))
205
+ assert dict_from_expr(Integer(
206
+ -17), gens=(x, y, z)) == ({(0, 0, 0): Integer(-17)}, (x, y, z))
207
+
208
+ assert dict_from_expr(
209
+ Integer(17)*x, gens=(x,)) == ({(1,): Integer(17)}, (x,))
210
+ assert dict_from_expr(
211
+ Integer(17)*x, gens=(x, y)) == ({(1, 0): Integer(17)}, (x, y))
212
+ assert dict_from_expr(Integer(
213
+ 17)*x, gens=(x, y, z)) == ({(1, 0, 0): Integer(17)}, (x, y, z))
214
+
215
+ assert dict_from_expr(
216
+ Integer(17)*x**7, gens=(x,)) == ({(7,): Integer(17)}, (x,))
217
+ assert dict_from_expr(
218
+ Integer(17)*x**7*y, gens=(x, y)) == ({(7, 1): Integer(17)}, (x, y))
219
+ assert dict_from_expr(Integer(17)*x**7*y*z**12, gens=(
220
+ x, y, z)) == ({(7, 1, 12): Integer(17)}, (x, y, z))
221
+
222
+ assert dict_from_expr(x + 2*y + 3*z, gens=(x,)) == \
223
+ ({(1,): Integer(1), (0,): 2*y + 3*z}, (x,))
224
+ assert dict_from_expr(x + 2*y + 3*z, gens=(x, y)) == \
225
+ ({(1, 0): Integer(1), (0, 1): Integer(2), (0, 0): 3*z}, (x, y))
226
+ assert dict_from_expr(x + 2*y + 3*z, gens=(x, y, z)) == \
227
+ ({(1, 0, 0): Integer(
228
+ 1), (0, 1, 0): Integer(2), (0, 0, 1): Integer(3)}, (x, y, z))
229
+
230
+ assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x,)) == \
231
+ ({(1,): y + 2*z, (0,): 3*y*z}, (x,))
232
+ assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y)) == \
233
+ ({(1, 1): Integer(1), (1, 0): 2*z, (0, 1): 3*z}, (x, y))
234
+ assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y, z)) == \
235
+ ({(1, 1, 0): Integer(
236
+ 1), (1, 0, 1): Integer(2), (0, 1, 1): Integer(3)}, (x, y, z))
237
+
238
+ assert dict_from_expr(2**y*x, gens=(x,)) == ({(1,): 2**y}, (x,))
239
+ assert dict_from_expr(Integral(x, (x, 1, 2)) + x) == (
240
+ {(0, 1): 1, (1, 0): 1}, (x, Integral(x, (x, 1, 2))))
241
+ raises(PolynomialError, lambda: dict_from_expr(2**y*x, gens=(x, y)))
242
+
243
+
244
+ def test__dict_from_expr_no_gens():
245
+ assert dict_from_expr(Integer(17)) == ({(): Integer(17)}, ())
246
+
247
+ assert dict_from_expr(x) == ({(1,): Integer(1)}, (x,))
248
+ assert dict_from_expr(y) == ({(1,): Integer(1)}, (y,))
249
+
250
+ assert dict_from_expr(x*y) == ({(1, 1): Integer(1)}, (x, y))
251
+ assert dict_from_expr(
252
+ x + y) == ({(1, 0): Integer(1), (0, 1): Integer(1)}, (x, y))
253
+
254
+ assert dict_from_expr(sqrt(2)) == ({(1,): Integer(1)}, (sqrt(2),))
255
+ assert dict_from_expr(sqrt(2), greedy=False) == ({(): sqrt(2)}, ())
256
+
257
+ assert dict_from_expr(x*y, domain=ZZ[x]) == ({(1,): x}, (y,))
258
+ assert dict_from_expr(x*y, domain=ZZ[y]) == ({(1,): y}, (x,))
259
+
260
+ assert dict_from_expr(3*sqrt(
261
+ 2)*pi*x*y, extension=None) == ({(1, 1, 1, 1): 3}, (x, y, pi, sqrt(2)))
262
+ assert dict_from_expr(3*sqrt(
263
+ 2)*pi*x*y, extension=True) == ({(1, 1, 1): 3*sqrt(2)}, (x, y, pi))
264
+
265
+ assert dict_from_expr(3*sqrt(
266
+ 2)*pi*x*y, extension=True) == ({(1, 1, 1): 3*sqrt(2)}, (x, y, pi))
267
+
268
+ f = cos(x)*sin(x) + cos(x)*sin(y) + cos(y)*sin(x) + cos(y)*sin(y)
269
+
270
+ assert dict_from_expr(f) == ({(0, 1, 0, 1): 1, (0, 1, 1, 0): 1,
271
+ (1, 0, 0, 1): 1, (1, 0, 1, 0): 1}, (cos(x), cos(y), sin(x), sin(y)))
272
+
273
+
274
+ def test__parallel_dict_from_expr_if_gens():
275
+ assert parallel_dict_from_expr([x + 2*y + 3*z, Integer(7)], gens=(x,)) == \
276
+ ([{(1,): Integer(1), (0,): 2*y + 3*z}, {(0,): Integer(7)}], (x,))
277
+
278
+
279
+ def test__parallel_dict_from_expr_no_gens():
280
+ assert parallel_dict_from_expr([x*y, Integer(3)]) == \
281
+ ([{(1, 1): Integer(1)}, {(0, 0): Integer(3)}], (x, y))
282
+ assert parallel_dict_from_expr([x*y, 2*z, Integer(3)]) == \
283
+ ([{(1, 1, 0): Integer(
284
+ 1)}, {(0, 0, 1): Integer(2)}, {(0, 0, 0): Integer(3)}], (x, y, z))
285
+ assert parallel_dict_from_expr((Mul(x, x**2, evaluate=False),)) == \
286
+ ([{(3,): 1}], (x,))
287
+
288
+
289
+ def test_parallel_dict_from_expr():
290
+ assert parallel_dict_from_expr([Eq(x, 1), Eq(
291
+ x**2, 2)]) == ([{(0,): -Integer(1), (1,): Integer(1)},
292
+ {(0,): -Integer(2), (2,): Integer(1)}], (x,))
293
+ raises(PolynomialError, lambda: parallel_dict_from_expr([A*B - B*A]))
294
+
295
+
296
+ def test_dict_from_expr():
297
+ assert dict_from_expr(Eq(x, 1)) == \
298
+ ({(0,): -Integer(1), (1,): Integer(1)}, (x,))
299
+ raises(PolynomialError, lambda: dict_from_expr(A*B - B*A))
300
+ raises(PolynomialError, lambda: dict_from_expr(S.true))
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_puiseux.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Tests for PuiseuxRing and PuiseuxPoly
3
+ #
4
+
5
+ from sympy.testing.pytest import raises
6
+
7
+ from sympy import ZZ, QQ, ring
8
+ from sympy.polys.puiseux import PuiseuxRing, PuiseuxPoly, puiseux_ring
9
+
10
+ from sympy.abc import x, y
11
+
12
+
13
+ def test_puiseux_ring():
14
+ R, px = puiseux_ring('x', QQ)
15
+ R2, px2 = puiseux_ring([x], QQ)
16
+ assert isinstance(R, PuiseuxRing)
17
+ assert isinstance(px, PuiseuxPoly)
18
+ assert R == R2
19
+ assert px == px2
20
+ assert R == PuiseuxRing('x', QQ)
21
+ assert R == PuiseuxRing([x], QQ)
22
+ assert R != PuiseuxRing('y', QQ)
23
+ assert R != PuiseuxRing('x', ZZ)
24
+ assert R != PuiseuxRing('x, y', QQ)
25
+ assert R != QQ
26
+ assert str(R) == 'PuiseuxRing((x,), QQ)'
27
+
28
+
29
+ def test_puiseux_ring_attributes():
30
+ R1, px1, py1 = ring('x, y', QQ)
31
+ R2, px2, py2 = puiseux_ring('x, y', QQ)
32
+ assert R2.domain == QQ
33
+ assert R2.symbols == (x, y)
34
+ assert R2.gens == (px2, py2)
35
+ assert R2.ngens == 2
36
+ assert R2.poly_ring == R1
37
+ assert R2.zero == PuiseuxPoly(R1.zero, R2)
38
+ assert R2.one == PuiseuxPoly(R1.one, R2)
39
+ assert R2.zero_monom == R1.zero_monom == (0, 0) # type: ignore
40
+ assert R2.monomial_mul((1, 2), (3, 4)) == (4, 6)
41
+
42
+
43
+ def test_puiseux_ring_methods():
44
+ R1, px1, py1 = ring('x, y', QQ)
45
+ R2, px2, py2 = puiseux_ring('x, y', QQ)
46
+ assert R2({(1, 2): 3}) == 3*px2*py2**2
47
+ assert R2(px1) == px2
48
+ assert R2(1) == R2.one
49
+ assert R2(QQ(1,2)) == QQ(1,2)*R2.one
50
+ assert R2.from_poly(px1) == px2
51
+ assert R2.from_poly(px1) != py2
52
+ assert R2.from_dict({(1, 2): QQ(3)}) == 3*px2*py2**2
53
+ assert R2.from_dict({(QQ(1,2), 2): QQ(3)}) == 3*px2**QQ(1,2)*py2**2
54
+ assert R2.from_int(3) == 3*R2.one
55
+ assert R2.domain_new(3) == QQ(3)
56
+ assert QQ.of_type(R2.domain_new(3))
57
+ assert R2.ground_new(3) == 3*R2.one
58
+ assert isinstance(R2.ground_new(3), PuiseuxPoly)
59
+ assert R2.index(px2) == 0
60
+ assert R2.index(py2) == 1
61
+
62
+
63
+ def test_puiseux_poly():
64
+ R1, px1 = ring('x', QQ)
65
+ R2, px2 = puiseux_ring('x', QQ)
66
+ assert PuiseuxPoly(px1, R2) == px2
67
+ assert px2.ring == R2
68
+ assert px2.as_expr() == px1.as_expr() == x
69
+ assert px1 != px2
70
+ assert R2.one == px2**0 == 1
71
+ assert px2 == px1
72
+ assert px2 != 2.0
73
+ assert px2**QQ(1,2) != px1
74
+
75
+
76
+ def test_puiseux_poly_normalization():
77
+ R, x = puiseux_ring('x', QQ)
78
+ assert (x**2 + 1) / x == x + 1/x == R({(1,): 1, (-1,): 1})
79
+ assert (x**QQ(1,6))**2 == x**QQ(1,3) == R({(QQ(1,3),): 1})
80
+ assert (x**QQ(1,6))**(-2) == x**(-QQ(1,3)) == R({(-QQ(1,3),): 1})
81
+ assert (x**QQ(1,6))**QQ(1,2) == x**QQ(1,12) == R({(QQ(1,12),): 1})
82
+ assert (x**QQ(1,6))**6 == x == R({(1,): 1})
83
+ assert x**QQ(1,6) * x**QQ(1,3) == x**QQ(1,2) == R({(QQ(1,2),): 1})
84
+ assert 1/x * x**2 == x == R({(1,): 1})
85
+ assert 1/x**QQ(1,3) * x**QQ(1,3) == 1 == R({(0,): 1})
86
+
87
+
88
+ def test_puiseux_poly_monoms():
89
+ R, x = puiseux_ring('x', QQ)
90
+ assert x.monoms() == [(1,)]
91
+ assert list(x) == [(1,)]
92
+ assert (x**2 + 1).monoms() == [(2,), (0,)]
93
+ assert R({(1,): 1, (-1,): 1}).monoms() == [(1,), (-1,)]
94
+ assert R({(QQ(1,3),): 1}).monoms() == [(QQ(1,3),)]
95
+ assert R({(-QQ(1,3),): 1}).monoms() == [(-QQ(1,3),)]
96
+ p = x**QQ(1,6)
97
+ assert p[(QQ(1,6),)] == 1
98
+ raises(KeyError, lambda: p[(1,)])
99
+ assert p.to_dict() == {(QQ(1,6),): 1}
100
+ assert R(p.to_dict()) == p
101
+ assert PuiseuxPoly.from_dict({(QQ(1,6),): 1}, R) == p
102
+
103
+
104
+ def test_puiseux_poly_repr():
105
+ R, x = puiseux_ring('x', QQ)
106
+ assert repr(x) == 'x'
107
+ assert repr(x**QQ(1,2)) == 'x**(1/2)'
108
+ assert repr(1/x) == 'x**(-1)'
109
+ assert repr(2*x**2 + 1) == '1 + 2*x**2'
110
+ assert repr(R.one) == '1'
111
+ assert repr(2*R.one) == '2'
112
+
113
+
114
+ def test_puiseux_poly_unify():
115
+ R, x = puiseux_ring('x', QQ)
116
+ assert 1/x + x == x + 1/x == R({(1,): 1, (-1,): 1})
117
+ assert repr(1/x + x) == 'x**(-1) + x'
118
+ assert 1/x + 1/x == 2/x == R({(-1,): 2})
119
+ assert repr(1/x + 1/x) == '2*x**(-1)'
120
+ assert x**QQ(1,2) + x**QQ(1,2) == 2*x**QQ(1,2) == R({(QQ(1,2),): 2})
121
+ assert repr(x**QQ(1,2) + x**QQ(1,2)) == '2*x**(1/2)'
122
+ assert x**QQ(1,2) + x**QQ(1,3) == R({(QQ(1,2),): 1, (QQ(1,3),): 1})
123
+ assert repr(x**QQ(1,2) + x**QQ(1,3)) == 'x**(1/3) + x**(1/2)'
124
+ assert x + x**QQ(1,2) == R({(1,): 1, (QQ(1,2),): 1})
125
+ assert repr(x + x**QQ(1,2)) == 'x**(1/2) + x'
126
+ assert 1/x**QQ(1,2) + 1/x**QQ(1,3) == R({(-QQ(1,2),): 1, (-QQ(1,3),): 1})
127
+ assert repr(1/x**QQ(1,2) + 1/x**QQ(1,3)) == 'x**(-1/2) + x**(-1/3)'
128
+ assert 1/x + x**QQ(1,2) == x**QQ(1,2) + 1/x == R({(-1,): 1, (QQ(1,2),): 1})
129
+ assert repr(1/x + x**QQ(1,2)) == 'x**(-1) + x**(1/2)'
130
+
131
+
132
+ def test_puiseux_poly_arit():
133
+ R, x = puiseux_ring('x', QQ)
134
+ R2, y = puiseux_ring('y', QQ)
135
+ p = x**2 + 1
136
+ assert +p == p
137
+ assert -p == -1 - x**2
138
+ assert p + p == 2*p == 2*x**2 + 2
139
+ assert p + 1 == 1 + p == x**2 + 2
140
+ assert p + QQ(1,2) == QQ(1,2) + p == x**2 + QQ(3,2)
141
+ assert p - p == 0
142
+ assert p - 1 == -1 + p == x**2
143
+ assert p - QQ(1,2) == -QQ(1,2) + p == x**2 + QQ(1,2)
144
+ assert 1 - p == -p + 1 == -x**2
145
+ assert QQ(1,2) - p == -p + QQ(1,2) == -x**2 - QQ(1,2)
146
+ assert p * p == x**4 + 2*x**2 + 1
147
+ assert p * 1 == 1 * p == p
148
+ assert 2 * p == p * 2 == 2*x**2 + 2
149
+ assert p * QQ(1,2) == QQ(1,2) * p == QQ(1,2)*x**2 + QQ(1,2)
150
+ assert x**QQ(1,2) * x**QQ(1,2) == x
151
+ raises(ValueError, lambda: x + y)
152
+ raises(ValueError, lambda: x - y)
153
+ raises(ValueError, lambda: x * y)
154
+ raises(TypeError, lambda: x + None)
155
+ raises(TypeError, lambda: x - None)
156
+ raises(TypeError, lambda: x * None)
157
+ raises(TypeError, lambda: None + x)
158
+ raises(TypeError, lambda: None - x)
159
+ raises(TypeError, lambda: None * x)
160
+
161
+
162
+ def test_puiseux_poly_div():
163
+ R, x = puiseux_ring('x', QQ)
164
+ R2, y = puiseux_ring('y', QQ)
165
+ p = x**2 - 1
166
+ assert p / 1 == p
167
+ assert p / QQ(1,2) == 2*p == 2*x**2 - 2
168
+ assert p / x == x - 1/x == R({(1,): 1, (-1,): -1})
169
+ assert 2 / x == 2*x**-1 == R({(-1,): 2})
170
+ assert QQ(1,2) / x == QQ(1,2)*x**-1 == 1/(2*x) == 1/x/2 == R({(-1,): QQ(1,2)})
171
+ raises(ZeroDivisionError, lambda: p / 0)
172
+ raises(ValueError, lambda: (x + 1) / (x + 2))
173
+ raises(ValueError, lambda: (x + 1) / (x + 1))
174
+ raises(ValueError, lambda: x / y)
175
+ raises(TypeError, lambda: x / None)
176
+ raises(TypeError, lambda: None / x)
177
+
178
+
179
+ def test_puiseux_poly_pow():
180
+ R, x = puiseux_ring('x', QQ)
181
+ Rz, xz = puiseux_ring('x', ZZ)
182
+ assert x**0 == 1 == R({(0,): 1})
183
+ assert x**1 == x == R({(1,): 1})
184
+ assert x**2 == x*x == R({(2,): 1})
185
+ assert x**QQ(1,2) == R({(QQ(1,2),): 1})
186
+ assert x**-1 == 1/x == R({(-1,): 1})
187
+ assert x**-QQ(1,2) == 1/x**QQ(1,2) == R({(-QQ(1,2),): 1})
188
+ assert (2*x)**-1 == 1/(2*x) == QQ(1,2)/x == QQ(1,2)*x**-1 == R({(-1,): QQ(1,2)})
189
+ assert 2/x**2 == 2*x**-2 == R({(-2,): 2})
190
+ assert 2/xz**2 == 2*xz**-2 == Rz({(-2,): 2})
191
+ raises(TypeError, lambda: x**None)
192
+ raises(ValueError, lambda: (x + 1)**-1)
193
+ raises(ValueError, lambda: (x + 1)**QQ(1,2))
194
+ raises(ValueError, lambda: (2*x)**QQ(1,2))
195
+ raises(ValueError, lambda: (2*xz)**-1)
196
+
197
+
198
+ def test_puiseux_poly_diff():
199
+ R, x, y = puiseux_ring('x, y', QQ)
200
+ assert (x**2 + 1).diff(x) == 2*x
201
+ assert (x**2 + 1).diff(y) == 0
202
+ assert (x**2 + y**2).diff(x) == 2*x
203
+ assert (x**QQ(1,2) + y**QQ(1,2)).diff(x) == QQ(1,2)*x**-QQ(1,2)
204
+ assert ((x*y)**QQ(1,2)).diff(x) == QQ(1,2)*y**QQ(1,2)*x**-QQ(1,2)
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_pythonrational.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for PythonRational type. """
2
+
3
+ from sympy.polys.domains import PythonRational as QQ
4
+ from sympy.testing.pytest import raises
5
+
6
+ def test_PythonRational__init__():
7
+ assert QQ(0).numerator == 0
8
+ assert QQ(0).denominator == 1
9
+ assert QQ(0, 1).numerator == 0
10
+ assert QQ(0, 1).denominator == 1
11
+ assert QQ(0, -1).numerator == 0
12
+ assert QQ(0, -1).denominator == 1
13
+
14
+ assert QQ(1).numerator == 1
15
+ assert QQ(1).denominator == 1
16
+ assert QQ(1, 1).numerator == 1
17
+ assert QQ(1, 1).denominator == 1
18
+ assert QQ(-1, -1).numerator == 1
19
+ assert QQ(-1, -1).denominator == 1
20
+
21
+ assert QQ(-1).numerator == -1
22
+ assert QQ(-1).denominator == 1
23
+ assert QQ(-1, 1).numerator == -1
24
+ assert QQ(-1, 1).denominator == 1
25
+ assert QQ( 1, -1).numerator == -1
26
+ assert QQ( 1, -1).denominator == 1
27
+
28
+ assert QQ(1, 2).numerator == 1
29
+ assert QQ(1, 2).denominator == 2
30
+ assert QQ(3, 4).numerator == 3
31
+ assert QQ(3, 4).denominator == 4
32
+
33
+ assert QQ(2, 2).numerator == 1
34
+ assert QQ(2, 2).denominator == 1
35
+ assert QQ(2, 4).numerator == 1
36
+ assert QQ(2, 4).denominator == 2
37
+
38
+ def test_PythonRational__hash__():
39
+ assert hash(QQ(0)) == hash(0)
40
+ assert hash(QQ(1)) == hash(1)
41
+ assert hash(QQ(117)) == hash(117)
42
+
43
+ def test_PythonRational__int__():
44
+ assert int(QQ(-1, 4)) == 0
45
+ assert int(QQ( 1, 4)) == 0
46
+ assert int(QQ(-5, 4)) == -1
47
+ assert int(QQ( 5, 4)) == 1
48
+
49
+ def test_PythonRational__float__():
50
+ assert float(QQ(-1, 2)) == -0.5
51
+ assert float(QQ( 1, 2)) == 0.5
52
+
53
+ def test_PythonRational__abs__():
54
+ assert abs(QQ(-1, 2)) == QQ(1, 2)
55
+ assert abs(QQ( 1, 2)) == QQ(1, 2)
56
+
57
+ def test_PythonRational__pos__():
58
+ assert +QQ(-1, 2) == QQ(-1, 2)
59
+ assert +QQ( 1, 2) == QQ( 1, 2)
60
+
61
+ def test_PythonRational__neg__():
62
+ assert -QQ(-1, 2) == QQ( 1, 2)
63
+ assert -QQ( 1, 2) == QQ(-1, 2)
64
+
65
+ def test_PythonRational__add__():
66
+ assert QQ(-1, 2) + QQ( 1, 2) == QQ(0)
67
+ assert QQ( 1, 2) + QQ(-1, 2) == QQ(0)
68
+
69
+ assert QQ(1, 2) + QQ(1, 2) == QQ(1)
70
+ assert QQ(1, 2) + QQ(3, 2) == QQ(2)
71
+ assert QQ(3, 2) + QQ(1, 2) == QQ(2)
72
+ assert QQ(3, 2) + QQ(3, 2) == QQ(3)
73
+
74
+ assert 1 + QQ(1, 2) == QQ(3, 2)
75
+ assert QQ(1, 2) + 1 == QQ(3, 2)
76
+
77
+ def test_PythonRational__sub__():
78
+ assert QQ(-1, 2) - QQ( 1, 2) == QQ(-1)
79
+ assert QQ( 1, 2) - QQ(-1, 2) == QQ( 1)
80
+
81
+ assert QQ(1, 2) - QQ(1, 2) == QQ( 0)
82
+ assert QQ(1, 2) - QQ(3, 2) == QQ(-1)
83
+ assert QQ(3, 2) - QQ(1, 2) == QQ( 1)
84
+ assert QQ(3, 2) - QQ(3, 2) == QQ( 0)
85
+
86
+ assert 1 - QQ(1, 2) == QQ( 1, 2)
87
+ assert QQ(1, 2) - 1 == QQ(-1, 2)
88
+
89
+ def test_PythonRational__mul__():
90
+ assert QQ(-1, 2) * QQ( 1, 2) == QQ(-1, 4)
91
+ assert QQ( 1, 2) * QQ(-1, 2) == QQ(-1, 4)
92
+
93
+ assert QQ(1, 2) * QQ(1, 2) == QQ(1, 4)
94
+ assert QQ(1, 2) * QQ(3, 2) == QQ(3, 4)
95
+ assert QQ(3, 2) * QQ(1, 2) == QQ(3, 4)
96
+ assert QQ(3, 2) * QQ(3, 2) == QQ(9, 4)
97
+
98
+ assert 2 * QQ(1, 2) == QQ(1)
99
+ assert QQ(1, 2) * 2 == QQ(1)
100
+
101
+ def test_PythonRational__truediv__():
102
+ assert QQ(-1, 2) / QQ( 1, 2) == QQ(-1)
103
+ assert QQ( 1, 2) / QQ(-1, 2) == QQ(-1)
104
+
105
+ assert QQ(1, 2) / QQ(1, 2) == QQ(1)
106
+ assert QQ(1, 2) / QQ(3, 2) == QQ(1, 3)
107
+ assert QQ(3, 2) / QQ(1, 2) == QQ(3)
108
+ assert QQ(3, 2) / QQ(3, 2) == QQ(1)
109
+
110
+ assert 2 / QQ(1, 2) == QQ(4)
111
+ assert QQ(1, 2) / 2 == QQ(1, 4)
112
+
113
+ raises(ZeroDivisionError, lambda: QQ(1, 2) / QQ(0))
114
+ raises(ZeroDivisionError, lambda: QQ(1, 2) / 0)
115
+
116
+ def test_PythonRational__pow__():
117
+ assert QQ(1)**10 == QQ(1)
118
+ assert QQ(2)**10 == QQ(1024)
119
+
120
+ assert QQ(1)**(-10) == QQ(1)
121
+ assert QQ(2)**(-10) == QQ(1, 1024)
122
+
123
+ def test_PythonRational__eq__():
124
+ assert (QQ(1, 2) == QQ(1, 2)) is True
125
+ assert (QQ(1, 2) != QQ(1, 2)) is False
126
+
127
+ assert (QQ(1, 2) == QQ(1, 3)) is False
128
+ assert (QQ(1, 2) != QQ(1, 3)) is True
129
+
130
+ def test_PythonRational__lt_le_gt_ge__():
131
+ assert (QQ(1, 2) < QQ(1, 4)) is False
132
+ assert (QQ(1, 2) <= QQ(1, 4)) is False
133
+ assert (QQ(1, 2) > QQ(1, 4)) is True
134
+ assert (QQ(1, 2) >= QQ(1, 4)) is True
135
+
136
+ assert (QQ(1, 4) < QQ(1, 2)) is True
137
+ assert (QQ(1, 4) <= QQ(1, 2)) is True
138
+ assert (QQ(1, 4) > QQ(1, 2)) is False
139
+ assert (QQ(1, 4) >= QQ(1, 2)) is False
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_rationaltools.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for tools for manipulation of rational expressions. """
2
+
3
+ from sympy.polys.rationaltools import together
4
+
5
+ from sympy.core.mul import Mul
6
+ from sympy.core.numbers import Rational
7
+ from sympy.core.relational import Eq
8
+ from sympy.core.singleton import S
9
+ from sympy.core.symbol import symbols
10
+ from sympy.functions.elementary.exponential import exp
11
+ from sympy.functions.elementary.trigonometric import sin
12
+ from sympy.integrals.integrals import Integral
13
+ from sympy.abc import x, y, z
14
+
15
+ A, B = symbols('A,B', commutative=False)
16
+
17
+
18
+ def test_together():
19
+ assert together(0) == 0
20
+ assert together(1) == 1
21
+
22
+ assert together(x*y*z) == x*y*z
23
+ assert together(x + y) == x + y
24
+
25
+ assert together(1/x) == 1/x
26
+
27
+ assert together(1/x + 1) == (x + 1)/x
28
+ assert together(1/x + 3) == (3*x + 1)/x
29
+ assert together(1/x + x) == (x**2 + 1)/x
30
+
31
+ assert together(1/x + S.Half) == (x + 2)/(2*x)
32
+ assert together(S.Half + x/2) == Mul(S.Half, x + 1, evaluate=False)
33
+
34
+ assert together(1/x + 2/y) == (2*x + y)/(y*x)
35
+ assert together(1/(1 + 1/x)) == x/(1 + x)
36
+ assert together(x/(1 + 1/x)) == x**2/(1 + x)
37
+
38
+ assert together(1/x + 1/y + 1/z) == (x*y + x*z + y*z)/(x*y*z)
39
+ assert together(1/(1 + x + 1/y + 1/z)) == y*z/(y + z + y*z + x*y*z)
40
+
41
+ assert together(1/(x*y) + 1/(x*y)**2) == y**(-2)*x**(-2)*(1 + x*y)
42
+ assert together(1/(x*y) + 1/(x*y)**4) == y**(-4)*x**(-4)*(1 + x**3*y**3)
43
+ assert together(1/(x**7*y) + 1/(x*y)**4) == y**(-4)*x**(-7)*(x**3 + y**3)
44
+
45
+ assert together(5/(2 + 6/(3 + 7/(4 + 8/(5 + 9/x))))) == \
46
+ Rational(5, 2)*((171 + 119*x)/(279 + 203*x))
47
+
48
+ assert together(1 + 1/(x + 1)**2) == (1 + (x + 1)**2)/(x + 1)**2
49
+ assert together(1 + 1/(x*(1 + x))) == (1 + x*(1 + x))/(x*(1 + x))
50
+ assert together(
51
+ 1/(x*(x + 1)) + 1/(x*(x + 2))) == (3 + 2*x)/(x*(1 + x)*(2 + x))
52
+ assert together(1 + 1/(2*x + 2)**2) == (4*(x + 1)**2 + 1)/(4*(x + 1)**2)
53
+
54
+ assert together(sin(1/x + 1/y)) == sin(1/x + 1/y)
55
+ assert together(sin(1/x + 1/y), deep=True) == sin((x + y)/(x*y))
56
+
57
+ assert together(1/exp(x) + 1/(x*exp(x))) == (1 + x)/(x*exp(x))
58
+ assert together(1/exp(2*x) + 1/(x*exp(3*x))) == (1 + exp(x)*x)/(x*exp(3*x))
59
+
60
+ assert together(Integral(1/x + 1/y, x)) == Integral((x + y)/(x*y), x)
61
+ assert together(Eq(1/x + 1/y, 1 + 1/z)) == Eq((x + y)/(x*y), (z + 1)/z)
62
+
63
+ assert together((A*B)**-1 + (B*A)**-1) == (A*B)**-1 + (B*A)**-1
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_ring_series.py ADDED
@@ -0,0 +1,831 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.polys.domains import ZZ, QQ, EX, RR
2
+ from sympy.polys.rings import ring
3
+ from sympy.polys.puiseux import puiseux_ring
4
+ from sympy.polys.ring_series import (_invert_monoms, rs_integrate,
5
+ rs_trunc, rs_mul, rs_square, rs_pow, _has_constant_term, rs_hadamard_exp,
6
+ rs_series_from_list, rs_exp, rs_log, rs_newton, rs_series_inversion,
7
+ rs_compose_add, rs_asin, _atan, rs_atan, _atanh, rs_atanh, rs_asinh, rs_tan,
8
+ rs_cot, rs_sin, rs_cos, rs_cos_sin, rs_sinh, rs_cosh, rs_cosh_sinh, rs_tanh,
9
+ _tan1, rs_fun, rs_nth_root, rs_LambertW, rs_series_reversion, rs_is_puiseux,
10
+ rs_series)
11
+ from sympy.testing.pytest import raises, slow
12
+ from sympy.core.symbol import symbols
13
+ from sympy.functions import (sin, cos, exp, tan, cot, sinh, cosh, atan, atanh,
14
+ asinh, tanh, log, sqrt)
15
+ from sympy.core.numbers import Rational, pi
16
+ from sympy.core import expand, S
17
+
18
+ def is_close(a, b):
19
+ tol = 10**(-10)
20
+ assert abs(a - b) < tol
21
+
22
+
23
+ def test_ring_series1():
24
+ R, x = ring('x', QQ)
25
+ p = x**4 + 2*x**3 + 3*x + 4
26
+ assert _invert_monoms(p) == 4*x**4 + 3*x**3 + 2*x + 1
27
+ assert rs_hadamard_exp(p) == x**4/24 + x**3/3 + 3*x + 4
28
+ R, x = ring('x', QQ)
29
+ p = x**4 + 2*x**3 + 3*x + 4
30
+ assert rs_integrate(p, x) == x**5/5 + x**4/2 + 3*x**2/2 + 4*x
31
+ R, x, y = ring('x, y', QQ)
32
+ p = x**2*y**2 + x + 1
33
+ assert rs_integrate(p, x) == x**3*y**2/3 + x**2/2 + x
34
+ assert rs_integrate(p, y) == x**2*y**3/3 + x*y + y
35
+
36
+
37
+ def test_trunc():
38
+ R, x, y, t = ring('x, y, t', QQ)
39
+ p = (y + t*x)**4
40
+ p1 = rs_trunc(p, x, 3)
41
+ assert p1 == y**4 + 4*y**3*t*x + 6*y**2*t**2*x**2
42
+
43
+
44
+ def test_mul_trunc():
45
+ R, x, y, t = ring('x, y, t', QQ)
46
+ p = 1 + t*x + t*y
47
+ for i in range(2):
48
+ p = rs_mul(p, p, t, 3)
49
+
50
+ assert p == 6*x**2*t**2 + 12*x*y*t**2 + 6*y**2*t**2 + 4*x*t + 4*y*t + 1
51
+ p = 1 + t*x + t*y + t**2*x*y
52
+ p1 = rs_mul(p, p, t, 2)
53
+ assert p1 == 1 + 2*t*x + 2*t*y
54
+ R1, z = ring('z', QQ)
55
+ raises(ValueError, lambda: rs_mul(p, z, x, 2))
56
+
57
+ p1 = 2 + 2*x + 3*x**2
58
+ p2 = 3 + x**2
59
+ assert rs_mul(p1, p2, x, 4) == 2*x**3 + 11*x**2 + 6*x + 6
60
+
61
+
62
+ def test_square_trunc():
63
+ R, x, y, t = ring('x, y, t', QQ)
64
+ p = (1 + t*x + t*y)*2
65
+ p1 = rs_mul(p, p, x, 3)
66
+ p2 = rs_square(p, x, 3)
67
+ assert p1 == p2
68
+ p = 1 + x + x**2 + x**3
69
+ assert rs_square(p, x, 4) == 4*x**3 + 3*x**2 + 2*x + 1
70
+
71
+
72
+ def test_pow_trunc():
73
+ R, x, y, z = ring('x, y, z', QQ)
74
+ p0 = y + x*z
75
+ p = p0**16
76
+ for xx in (x, y, z):
77
+ p1 = rs_trunc(p, xx, 8)
78
+ p2 = rs_pow(p0, 16, xx, 8)
79
+ assert p1 == p2
80
+
81
+ p = 1 + x
82
+ p1 = rs_pow(p, 3, x, 2)
83
+ assert p1 == 1 + 3*x
84
+ assert rs_pow(p, 0, x, 2) == 1
85
+ assert rs_pow(p, -2, x, 2) == 1 - 2*x
86
+ p = x + y
87
+ assert rs_pow(p, 3, y, 3) == x**3 + 3*x**2*y + 3*x*y**2
88
+ assert rs_pow(1 + x, Rational(2, 3), x, 4) == 4*x**3/81 - x**2/9 + x*Rational(2, 3) + 1
89
+
90
+
91
+ def test_has_constant_term():
92
+ R, x, y, z = ring('x, y, z', QQ)
93
+ p = y + x*z
94
+ assert _has_constant_term(p, x)
95
+ p = x + x**4
96
+ assert not _has_constant_term(p, x)
97
+ p = 1 + x + x**4
98
+ assert _has_constant_term(p, x)
99
+ p = x + y + x*z
100
+
101
+
102
+ def test_inversion():
103
+ R, x = ring('x', QQ)
104
+ p = 2 + x + 2*x**2
105
+ n = 5
106
+ p1 = rs_series_inversion(p, x, n)
107
+ assert rs_trunc(p*p1, x, n) == 1
108
+ R, x, y = ring('x, y', QQ)
109
+ p = 2 + x + 2*x**2 + y*x + x**2*y
110
+ p1 = rs_series_inversion(p, x, n)
111
+ assert rs_trunc(p*p1, x, n) == 1
112
+
113
+ R, x, y = ring('x, y', QQ)
114
+ p = 1 + x + y
115
+ raises(NotImplementedError, lambda: rs_series_inversion(p, x, 4))
116
+ p = R.zero
117
+ raises(ZeroDivisionError, lambda: rs_series_inversion(p, x, 3))
118
+
119
+ R, x = ring('x', ZZ)
120
+ p = 2 + x
121
+ raises(ValueError, lambda: rs_series_inversion(p, x, 3))
122
+
123
+
124
+ def test_series_reversion():
125
+ R, x, y = ring('x, y', QQ)
126
+
127
+ p = rs_tan(x, x, 10)
128
+ assert rs_series_reversion(p, x, 8, y) == rs_atan(y, y, 8)
129
+
130
+ p = rs_sin(x, x, 10)
131
+ assert rs_series_reversion(p, x, 8, y) == 5*y**7/112 + 3*y**5/40 + \
132
+ y**3/6 + y
133
+
134
+
135
+ def test_series_from_list():
136
+ R, x = ring('x', QQ)
137
+ p = 1 + 2*x + x**2 + 3*x**3
138
+ c = [1, 2, 0, 4, 4]
139
+ r = rs_series_from_list(p, c, x, 5)
140
+ pc = R.from_list(list(reversed(c)))
141
+ r1 = rs_trunc(pc.compose(x, p), x, 5)
142
+ assert r == r1
143
+ R, x, y = ring('x, y', QQ)
144
+ c = [1, 3, 5, 7]
145
+ p1 = rs_series_from_list(x + y, c, x, 3, concur=0)
146
+ p2 = rs_trunc((1 + 3*(x+y) + 5*(x+y)**2 + 7*(x+y)**3), x, 3)
147
+ assert p1 == p2
148
+
149
+ R, x = ring('x', QQ)
150
+ h = 25
151
+ p = rs_exp(x, x, h) - 1
152
+ p1 = rs_series_from_list(p, c, x, h)
153
+ p2 = 0
154
+ for i, cx in enumerate(c):
155
+ p2 += cx*rs_pow(p, i, x, h)
156
+ assert p1 == p2
157
+
158
+
159
+ def test_log():
160
+ R, x = ring('x', QQ)
161
+ p = 1 + x
162
+ assert rs_log(p, x, 4) == x - x**2/2 + x**3/3
163
+ p = 1 + x +2*x**2/3
164
+ p1 = rs_log(p, x, 9)
165
+ assert p1 == -17*x**8/648 + 13*x**7/189 - 11*x**6/162 - x**5/45 + \
166
+ 7*x**4/36 - x**3/3 + x**2/6 + x
167
+ p2 = rs_series_inversion(p, x, 9)
168
+ p3 = rs_log(p2, x, 9)
169
+ assert p3 == -p1
170
+
171
+ R, x, y = ring('x, y', QQ)
172
+ p = 1 + x + 2*y*x**2
173
+ p1 = rs_log(p, x, 6)
174
+ assert p1 == (4*x**5*y**2 - 2*x**5*y - 2*x**4*y**2 + x**5/5 + 2*x**4*y -
175
+ x**4/4 - 2*x**3*y + x**3/3 + 2*x**2*y - x**2/2 + x)
176
+
177
+ # Constant term in series
178
+ a = symbols('a')
179
+ R, x, y = ring('x, y', EX)
180
+ assert rs_log(x + a, x, 5) == -EX(1/(4*a**4))*x**4 + EX(1/(3*a**3))*x**3 \
181
+ - EX(1/(2*a**2))*x**2 + EX(1/a)*x + EX(log(a))
182
+ assert rs_log(x + x**2*y + a, x, 4) == -EX(a**(-2))*x**3*y + \
183
+ EX(1/(3*a**3))*x**3 + EX(1/a)*x**2*y - EX(1/(2*a**2))*x**2 + \
184
+ EX(1/a)*x + EX(log(a))
185
+
186
+ p = x + x**2 + 3
187
+ assert rs_log(p, x, 10).compose(x, 5) == EX(log(3) + Rational(19281291595, 9920232))
188
+
189
+
190
+ def test_exp():
191
+ R, x = ring('x', QQ)
192
+ p = x + x**4
193
+ for h in [10, 30]:
194
+ q = rs_series_inversion(1 + p, x, h) - 1
195
+ p1 = rs_exp(q, x, h)
196
+ q1 = rs_log(p1, x, h)
197
+ assert q1 == q
198
+ p1 = rs_exp(p, x, 30)
199
+ assert p1.coeff(x**29) == QQ(74274246775059676726972369, 353670479749588078181744640000)
200
+ prec = 21
201
+ p = rs_log(1 + x, x, prec)
202
+ p1 = rs_exp(p, x, prec)
203
+ assert p1 == x + 1
204
+
205
+ # Constant term in series
206
+ a = symbols('a')
207
+ R, x, y = ring('x, y', QQ[exp(a), a])
208
+ assert rs_exp(x + a, x, 5) == exp(a)*x**4/24 + exp(a)*x**3/6 + \
209
+ exp(a)*x**2/2 + exp(a)*x + exp(a)
210
+ assert rs_exp(x + x**2*y + a, x, 5) == exp(a)*x**4*y**2/2 + \
211
+ exp(a)*x**4*y/2 + exp(a)*x**4/24 + exp(a)*x**3*y + \
212
+ exp(a)*x**3/6 + exp(a)*x**2*y + exp(a)*x**2/2 + exp(a)*x + exp(a)
213
+
214
+ R, x, y = ring('x, y', EX)
215
+ assert rs_exp(x + a, x, 5) == EX(exp(a)/24)*x**4 + EX(exp(a)/6)*x**3 + \
216
+ EX(exp(a)/2)*x**2 + EX(exp(a))*x + EX(exp(a))
217
+ assert rs_exp(x + x**2*y + a, x, 5) == EX(exp(a)/2)*x**4*y**2 + \
218
+ EX(exp(a)/2)*x**4*y + EX(exp(a)/24)*x**4 + EX(exp(a))*x**3*y + \
219
+ EX(exp(a)/6)*x**3 + EX(exp(a))*x**2*y + EX(exp(a)/2)*x**2 + \
220
+ EX(exp(a))*x + EX(exp(a))
221
+
222
+
223
+ def test_newton():
224
+ R, x = ring('x', QQ)
225
+ p = x**2 - 2
226
+ r = rs_newton(p, x, 4)
227
+ assert r == 8*x**4 + 4*x**2 + 2
228
+
229
+
230
+ def test_compose_add():
231
+ R, x = ring('x', QQ)
232
+ p1 = x**3 - 1
233
+ p2 = x**2 - 2
234
+ assert rs_compose_add(p1, p2) == x**6 - 6*x**4 - 2*x**3 + 12*x**2 - 12*x - 7
235
+
236
+
237
+ def test_fun():
238
+ R, x, y = ring('x, y', QQ)
239
+ p = x*y + x**2*y**3 + x**5*y
240
+ assert rs_fun(p, rs_tan, x, 10) == rs_tan(p, x, 10)
241
+ assert rs_fun(p, _tan1, x, 10) == _tan1(p, x, 10)
242
+
243
+
244
+ def test_nth_root():
245
+ R, x, y = puiseux_ring('x, y', QQ)
246
+ assert rs_nth_root(1 + x**2*y, 4, x, 10) == -77*x**8*y**4/2048 + \
247
+ 7*x**6*y**3/128 - 3*x**4*y**2/32 + x**2*y/4 + 1
248
+ assert rs_nth_root(1 + x*y + x**2*y**3, 3, x, 5) == -x**4*y**6/9 + \
249
+ 5*x**4*y**5/27 - 10*x**4*y**4/243 - 2*x**3*y**4/9 + 5*x**3*y**3/81 + \
250
+ x**2*y**3/3 - x**2*y**2/9 + x*y/3 + 1
251
+ assert rs_nth_root(8*x, 3, x, 3) == 2*x**QQ(1, 3)
252
+ assert rs_nth_root(8*x + x**2 + x**3, 3, x, 3) == x**QQ(4,3)/12 + 2*x**QQ(1,3)
253
+ r = rs_nth_root(8*x + x**2*y + x**3, 3, x, 4)
254
+ assert r == -x**QQ(7,3)*y**2/288 + x**QQ(7,3)/12 + x**QQ(4,3)*y/12 + 2*x**QQ(1,3)
255
+
256
+ # Constant term in series
257
+ a = symbols('a')
258
+ R, x, y = puiseux_ring('x, y', EX)
259
+ assert rs_nth_root(x + EX(a), 3, x, 4) == EX(5/(81*a**QQ(8, 3)))*x**3 - \
260
+ EX(1/(9*a**QQ(5, 3)))*x**2 + EX(1/(3*a**QQ(2, 3)))*x + EX(a**QQ(1, 3))
261
+ assert rs_nth_root(x**QQ(2, 3) + x**2*y + 5, 2, x, 3) == -EX(sqrt(5)/100)*\
262
+ x**QQ(8, 3)*y - EX(sqrt(5)/16000)*x**QQ(8, 3) + EX(sqrt(5)/10)*x**2*y + \
263
+ EX(sqrt(5)/2000)*x**2 - EX(sqrt(5)/200)*x**QQ(4, 3) + \
264
+ EX(sqrt(5)/10)*x**QQ(2, 3) + EX(sqrt(5))
265
+
266
+
267
+ def test_atan():
268
+ R, x, y = ring('x, y', QQ)
269
+ assert rs_atan(x, x, 9) == -x**7/7 + x**5/5 - x**3/3 + x
270
+ assert rs_atan(x*y + x**2*y**3, x, 9) == 2*x**8*y**11 - x**8*y**9 + \
271
+ 2*x**7*y**9 - x**7*y**7/7 - x**6*y**9/3 + x**6*y**7 - x**5*y**7 + \
272
+ x**5*y**5/5 - x**4*y**5 - x**3*y**3/3 + x**2*y**3 + x*y
273
+
274
+ # Constant term in series
275
+ a = symbols('a')
276
+ R, x, y = ring('x, y', EX)
277
+ assert rs_atan(x + a, x, 5) == -EX((a**3 - a)/(a**8 + 4*a**6 + 6*a**4 + \
278
+ 4*a**2 + 1))*x**4 + EX((3*a**2 - 1)/(3*a**6 + 9*a**4 + \
279
+ 9*a**2 + 3))*x**3 - EX(a/(a**4 + 2*a**2 + 1))*x**2 + \
280
+ EX(1/(a**2 + 1))*x + EX(atan(a))
281
+ assert rs_atan(x + x**2*y + a, x, 4) == -EX(2*a/(a**4 + 2*a**2 + 1)) \
282
+ *x**3*y + EX((3*a**2 - 1)/(3*a**6 + 9*a**4 + 9*a**2 + 3))*x**3 + \
283
+ EX(1/(a**2 + 1))*x**2*y - EX(a/(a**4 + 2*a**2 + 1))*x**2 + EX(1/(a**2 \
284
+ + 1))*x + EX(atan(a))
285
+
286
+ # Test for _atan faster for small and univariate series
287
+ R, x = ring('x', QQ)
288
+ p = x**2 + 2*x
289
+ assert _atan(p, x, 5) == rs_atan(p, x, 5)
290
+
291
+ R, x = ring('x', EX)
292
+ p = x**2 + 2*x
293
+ assert _atan(p, x, 9) == rs_atan(p, x, 9)
294
+
295
+
296
+ def test_asin():
297
+ R, x, y = ring('x, y', QQ)
298
+ assert rs_asin(x + x*y, x, 5) == x**3*y**3/6 + x**3*y**2/2 + x**3*y/2 + \
299
+ x**3/6 + x*y + x
300
+ assert rs_asin(x*y + x**2*y**3, x, 6) == x**5*y**7/2 + 3*x**5*y**5/40 + \
301
+ x**4*y**5/2 + x**3*y**3/6 + x**2*y**3 + x*y
302
+
303
+
304
+ def test_tan():
305
+ R, x, y = ring('x, y', QQ)
306
+ assert rs_tan(x, x, 9) == x + x**3/3 + QQ(2,15)*x**5 + QQ(17,315)*x**7
307
+ assert rs_tan(x*y + x**2*y**3, x, 9) == 4*x**8*y**11/3 + 17*x**8*y**9/45 + \
308
+ 4*x**7*y**9/3 + 17*x**7*y**7/315 + x**6*y**9/3 + 2*x**6*y**7/3 + \
309
+ x**5*y**7 + 2*x**5*y**5/15 + x**4*y**5 + x**3*y**3/3 + x**2*y**3 + x*y
310
+
311
+ # Constant term in series
312
+ a = symbols('a')
313
+ R, x, y = ring('x, y', QQ[tan(a), a])
314
+ assert rs_tan(x + a, x, 5) == (tan(a)**5 + 5*tan(a)**3/3 +
315
+ 2*tan(a)/3)*x**4 + (tan(a)**4 + 4*tan(a)**2/3 + Rational(1, 3))*x**3 + \
316
+ (tan(a)**3 + tan(a))*x**2 + (tan(a)**2 + 1)*x + tan(a)
317
+ assert rs_tan(x + x**2*y + a, x, 4) == (2*tan(a)**3 + 2*tan(a))*x**3*y + \
318
+ (tan(a)**4 + Rational(4, 3)*tan(a)**2 + Rational(1, 3))*x**3 + (tan(a)**2 + 1)*x**2*y + \
319
+ (tan(a)**3 + tan(a))*x**2 + (tan(a)**2 + 1)*x + tan(a)
320
+
321
+ R, x, y = ring('x, y', EX)
322
+ assert rs_tan(x + a, x, 5) == EX(tan(a)**5 + 5*tan(a)**3/3 +
323
+ 2*tan(a)/3)*x**4 + EX(tan(a)**4 + 4*tan(a)**2/3 + EX(1)/3)*x**3 + \
324
+ EX(tan(a)**3 + tan(a))*x**2 + EX(tan(a)**2 + 1)*x + EX(tan(a))
325
+ assert rs_tan(x + x**2*y + a, x, 4) == EX(2*tan(a)**3 +
326
+ 2*tan(a))*x**3*y + EX(tan(a)**4 + 4*tan(a)**2/3 + EX(1)/3)*x**3 + \
327
+ EX(tan(a)**2 + 1)*x**2*y + EX(tan(a)**3 + tan(a))*x**2 + \
328
+ EX(tan(a)**2 + 1)*x + EX(tan(a))
329
+
330
+ p = x + x**2 + 5
331
+ assert rs_atan(p, x, 10).compose(x, 10) == EX(atan(5) + S(67701870330562640) / \
332
+ 668083460499)
333
+
334
+
335
+ def test_cot():
336
+ R, x, y = puiseux_ring('x, y', QQ)
337
+ assert rs_cot(x**6 + x**7, x, 8) == x**(-6) - x**(-5) + x**(-4) - \
338
+ x**(-3) + x**(-2) - x**(-1) + 1 - x + x**2 - x**3 + x**4 - x**5 + \
339
+ 2*x**6/3 - 4*x**7/3
340
+ assert rs_cot(x + x**2*y, x, 5) == -x**4*y**5 - x**4*y/15 + x**3*y**4 - \
341
+ x**3/45 - x**2*y**3 - x**2*y/3 + x*y**2 - x/3 - y + x**(-1)
342
+
343
+
344
+ def test_sin():
345
+ R, x, y = ring('x, y', QQ)
346
+ assert rs_sin(x, x, 9) == x - x**3/6 + x**5/120 - x**7/5040
347
+ assert rs_sin(x*y + x**2*y**3, x, 9) == x**8*y**11/12 - \
348
+ x**8*y**9/720 + x**7*y**9/12 - x**7*y**7/5040 - x**6*y**9/6 + \
349
+ x**6*y**7/24 - x**5*y**7/2 + x**5*y**5/120 - x**4*y**5/2 - \
350
+ x**3*y**3/6 + x**2*y**3 + x*y
351
+
352
+ # Constant term in series
353
+ a = symbols('a')
354
+ R, x, y = ring('x, y', QQ[sin(a), cos(a), a])
355
+ assert rs_sin(x + a, x, 5) == sin(a)*x**4/24 - cos(a)*x**3/6 - \
356
+ sin(a)*x**2/2 + cos(a)*x + sin(a)
357
+ assert rs_sin(x + x**2*y + a, x, 5) == -sin(a)*x**4*y**2/2 - \
358
+ cos(a)*x**4*y/2 + sin(a)*x**4/24 - sin(a)*x**3*y - cos(a)*x**3/6 + \
359
+ cos(a)*x**2*y - sin(a)*x**2/2 + cos(a)*x + sin(a)
360
+
361
+ R, x, y = ring('x, y', EX)
362
+ assert rs_sin(x + a, x, 5) == EX(sin(a)/24)*x**4 - EX(cos(a)/6)*x**3 - \
363
+ EX(sin(a)/2)*x**2 + EX(cos(a))*x + EX(sin(a))
364
+ assert rs_sin(x + x**2*y + a, x, 5) == -EX(sin(a)/2)*x**4*y**2 - \
365
+ EX(cos(a)/2)*x**4*y + EX(sin(a)/24)*x**4 - EX(sin(a))*x**3*y - \
366
+ EX(cos(a)/6)*x**3 + EX(cos(a))*x**2*y - EX(sin(a)/2)*x**2 + \
367
+ EX(cos(a))*x + EX(sin(a))
368
+
369
+
370
+ def test_cos():
371
+ R, x, y = ring('x, y', QQ)
372
+ assert rs_cos(x, x, 9) == 1 - x**2/2 + x**4/24 - x**6/720 + x**8/40320
373
+ assert rs_cos(x*y + x**2*y**3, x, 9) == x**8*y**12/24 - \
374
+ x**8*y**10/48 + x**8*y**8/40320 + x**7*y**10/6 - \
375
+ x**7*y**8/120 + x**6*y**8/4 - x**6*y**6/720 + x**5*y**6/6 - \
376
+ x**4*y**6/2 + x**4*y**4/24 - x**3*y**4 - x**2*y**2/2 + 1
377
+
378
+ # Constant term in series
379
+ a = symbols('a')
380
+ R, x, y = ring('x, y', QQ[sin(a), cos(a), a])
381
+ assert rs_cos(x + a, x, 5) == cos(a)*x**4/24 + sin(a)*x**3/6 - \
382
+ cos(a)*x**2/2 - sin(a)*x + cos(a)
383
+ assert rs_cos(x + x**2*y + a, x, 5) == -cos(a)*x**4*y**2/2 + \
384
+ sin(a)*x**4*y/2 + cos(a)*x**4/24 - cos(a)*x**3*y + sin(a)*x**3/6 - \
385
+ sin(a)*x**2*y - cos(a)*x**2/2 - sin(a)*x + cos(a)
386
+
387
+ R, x, y = ring('x, y', EX)
388
+ assert rs_cos(x + a, x, 5) == EX(cos(a)/24)*x**4 + EX(sin(a)/6)*x**3 - \
389
+ EX(cos(a)/2)*x**2 - EX(sin(a))*x + EX(cos(a))
390
+ assert rs_cos(x + x**2*y + a, x, 5) == -EX(cos(a)/2)*x**4*y**2 + \
391
+ EX(sin(a)/2)*x**4*y + EX(cos(a)/24)*x**4 - EX(cos(a))*x**3*y + \
392
+ EX(sin(a)/6)*x**3 - EX(sin(a))*x**2*y - EX(cos(a)/2)*x**2 - \
393
+ EX(sin(a))*x + EX(cos(a))
394
+
395
+
396
+ def test_cos_sin():
397
+ R, x, y = ring('x, y', QQ)
398
+ c, s = rs_cos_sin(x, x, 9)
399
+ assert c == rs_cos(x, x, 9)
400
+ assert s == rs_sin(x, x, 9)
401
+ c, s = rs_cos_sin(x + x*y, x, 5)
402
+ assert c == rs_cos(x + x*y, x, 5)
403
+ assert s == rs_sin(x + x*y, x, 5)
404
+
405
+ # constant term in series
406
+ c, s = rs_cos_sin(1 + x + x**2, x, 5)
407
+ assert c == rs_cos(1 + x + x**2, x, 5)
408
+ assert s == rs_sin(1 + x + x**2, x, 5)
409
+
410
+ a = symbols('a')
411
+ R, x, y = ring('x, y', QQ[sin(a), cos(a), a])
412
+ c, s = rs_cos_sin(x + a, x, 5)
413
+ assert c == rs_cos(x + a, x, 5)
414
+ assert s == rs_sin(x + a, x, 5)
415
+
416
+ R, x, y = ring('x, y', EX)
417
+ c, s = rs_cos_sin(x + a, x, 5)
418
+ assert c == rs_cos(x + a, x, 5)
419
+ assert s == rs_sin(x + a, x, 5)
420
+
421
+
422
+ def test_atanh():
423
+ R, x, y = ring('x, y', QQ)
424
+ assert rs_atanh(x, x, 9) == x + x**3/3 + x**5/5 + x**7/7
425
+ assert rs_atanh(x*y + x**2*y**3, x, 9) == 2*x**8*y**11 + x**8*y**9 + \
426
+ 2*x**7*y**9 + x**7*y**7/7 + x**6*y**9/3 + x**6*y**7 + x**5*y**7 + \
427
+ x**5*y**5/5 + x**4*y**5 + x**3*y**3/3 + x**2*y**3 + x*y
428
+
429
+ # Constant term in series
430
+ a = symbols('a')
431
+ R, x, y = ring('x, y', EX)
432
+ assert rs_atanh(x + a, x, 5) == EX((a**3 + a)/(a**8 - 4*a**6 + 6*a**4 - \
433
+ 4*a**2 + 1))*x**4 - EX((3*a**2 + 1)/(3*a**6 - 9*a**4 + \
434
+ 9*a**2 - 3))*x**3 + EX(a/(a**4 - 2*a**2 + 1))*x**2 - EX(1/(a**2 - \
435
+ 1))*x + EX(atanh(a))
436
+ assert rs_atanh(x + x**2*y + a, x, 4) == EX(2*a/(a**4 - 2*a**2 + \
437
+ 1))*x**3*y - EX((3*a**2 + 1)/(3*a**6 - 9*a**4 + 9*a**2 - 3))*x**3 - \
438
+ EX(1/(a**2 - 1))*x**2*y + EX(a/(a**4 - 2*a**2 + 1))*x**2 - \
439
+ EX(1/(a**2 - 1))*x + EX(atanh(a))
440
+
441
+ p = x + x**2 + 5
442
+ assert rs_atanh(p, x, 10).compose(x, 10) == EX(Rational(-733442653682135, 5079158784) \
443
+ + atanh(5))
444
+
445
+ # Test for _atanh faster for small and univariate series
446
+ R,x = ring('x', QQ)
447
+ p = x**2 + 2*x
448
+ assert _atanh(p, x, 5) == rs_atanh(p, x, 5)
449
+
450
+ R,x = ring('x', EX)
451
+ p = x**2 + 2*x
452
+ assert _atanh(p, x, 9) == rs_atanh(p, x, 9)
453
+
454
+
455
+ def test_asinh():
456
+ R, x, y = ring('x, y', QQ)
457
+ assert rs_asinh(x, x, 9) == -5/112*x**7 + 3/40*x**5 - 1/6*x**3 + x
458
+ assert rs_asinh(x*y + x**2*y**3, x, 9) == 3/4*x**8*y**11 - 5/16*x**8*y**9 + \
459
+ 3/4*x**7*y**9 - 5/112*x**7*y**7 - 1/6*x**6*y**9 + 3/8*x**6*y**7 - 1/2*x \
460
+ **5*y**7 + 3/40*x**5*y**5 - 1/2*x**4*y**5 - 1/6*x**3*y**3 + x**2*y**3 + x*y
461
+
462
+ # Constant term in series
463
+ a = symbols('a')
464
+ R, x, y = ring('x, y', EX)
465
+ assert rs_asinh(x + a, x, 3) == -EX(a/(2*a**2*sqrt(a**2 + 1) + 2*sqrt(a**2 + 1))) \
466
+ *x**2 + EX(1/sqrt(a**2 + 1))*x + EX(asinh(a))
467
+ assert rs_asinh(x + x**2*y + a, x, 3) == EX(1/sqrt(a**2 + 1))*x**2*y - EX(a/(2*a**2 \
468
+ *sqrt(a**2 + 1) + 2*sqrt(a**2 + 1)))*x**2 + EX(1/sqrt(a**2 + 1))*x + EX(asinh(a))
469
+
470
+ p = x + x ** 2 + 5
471
+ assert rs_asinh(p, x, 10).compose(x, 10) == EX(asinh(5) + 4643789843094995*sqrt(26)/\
472
+ 205564141692)
473
+
474
+
475
+ def test_sinh():
476
+ R, x, y = ring('x, y', QQ)
477
+ assert rs_sinh(x, x, 9) == x + x**3/6 + x**5/120 + x**7/5040
478
+ assert rs_sinh(x*y + x**2*y**3, x, 9) == x**8*y**11/12 + \
479
+ x**8*y**9/720 + x**7*y**9/12 + x**7*y**7/5040 + x**6*y**9/6 + \
480
+ x**6*y**7/24 + x**5*y**7/2 + x**5*y**5/120 + x**4*y**5/2 + \
481
+ x**3*y**3/6 + x**2*y**3 + x*y
482
+
483
+ # constant term in series
484
+ a = symbols('a')
485
+ R, x, y = ring('x, y', QQ[sinh(a), cosh(a), a])
486
+ assert rs_sinh(x + a, x, 5) == 1/24*x**4*(sinh(a)) + 1/6*x**3*(cosh(a)) + 1/\
487
+ 2*x**2*(sinh(a)) + x*(cosh(a)) + (sinh(a))
488
+ assert rs_sinh(x + x**2*y + a, x, 5) == 1/2*(sinh(a))*x**4*y**2 + 1/2*(cosh(a))\
489
+ *x**4*y + 1/24*(sinh(a))*x**4 + (sinh(a))*x**3*y + 1/6*(cosh(a))*x**3 + \
490
+ (cosh(a))*x**2*y + 1/2*(sinh(a))*x**2 + (cosh(a))*x + (sinh(a))
491
+
492
+ R, x, y = ring('x, y', EX)
493
+ assert rs_sinh(x + a, x, 5) == EX(sinh(a)/24)*x**4 + EX(cosh(a)/6)*x**3 + \
494
+ EX(sinh(a)/2)*x**2 + EX(cosh(a))*x + EX(sinh(a))
495
+ assert rs_sinh(x + x**2*y + a, x, 5) == EX(sinh(a)/2)*x**4*y**2 + EX(cosh(a)/\
496
+ 2)*x**4*y + EX(sinh(a)/24)*x**4 + EX(sinh(a))*x**3*y + EX(cosh(a)/6)*x**3 \
497
+ + EX(cosh(a))*x**2*y + EX(sinh(a)/2)*x**2 + EX(cosh(a))*x + EX(sinh(a))
498
+
499
+
500
+ def test_cosh():
501
+ R, x, y = ring('x, y', QQ)
502
+ assert rs_cosh(x, x, 9) == 1 + x**2/2 + x**4/24 + x**6/720 + x**8/40320
503
+ assert rs_cosh(x*y + x**2*y**3, x, 9) == x**8*y**12/24 + \
504
+ x**8*y**10/48 + x**8*y**8/40320 + x**7*y**10/6 + \
505
+ x**7*y**8/120 + x**6*y**8/4 + x**6*y**6/720 + x**5*y**6/6 + \
506
+ x**4*y**6/2 + x**4*y**4/24 + x**3*y**4 + x**2*y**2/2 + 1
507
+
508
+ # constant term in series
509
+ a = symbols('a')
510
+ R, x, y = ring('x, y', QQ[sinh(a), cosh(a), a])
511
+ assert rs_cosh(x + a, x, 5) == 1/24*(cosh(a))*x**4 + 1/6*(sinh(a))*x**3 + \
512
+ 1/2*(cosh(a))*x**2 + (sinh(a))*x + (cosh(a))
513
+ assert rs_cosh(x + x**2*y + a, x, 5) == 1/2*(cosh(a))*x**4*y**2 + 1/2*(sinh(a))\
514
+ *x**4*y + 1/24*(cosh(a))*x**4 + (cosh(a))*x**3*y + 1/6*(sinh(a))*x**3 + \
515
+ (sinh(a))*x**2*y + 1/2*(cosh(a))*x**2 + (sinh(a))*x + (cosh(a))
516
+ R, x, y = ring('x, y', EX)
517
+ assert rs_cosh(x + a, x, 5) == EX(cosh(a)/24)*x**4 + EX(sinh(a)/6)*x**3 + \
518
+ EX(cosh(a)/2)*x**2 + EX(sinh(a))*x + EX(cosh(a))
519
+ assert rs_cosh(x + x**2*y + a, x, 5) == EX(cosh(a)/2)*x**4*y**2 + EX(sinh(a)/\
520
+ 2)*x**4*y + EX(cosh(a)/24)*x**4 + EX(cosh(a))*x**3*y + EX(sinh(a)/6)*x**3 \
521
+ + EX(sinh(a))*x**2*y + EX(cosh(a)/2)*x**2 + EX(sinh(a))*x + EX(cosh(a))
522
+
523
+
524
+ def test_cosh_sinh():
525
+ R, x, y = ring('x, y', QQ)
526
+ ch, sh = rs_cosh_sinh(x, x, 9)
527
+ assert ch == rs_cosh(x, x, 9)
528
+ assert sh == rs_sinh(x, x, 9)
529
+ ch, sh = rs_cosh_sinh(x + x*y, x, 5)
530
+ assert ch == rs_cosh(x + x*y, x, 5)
531
+ assert sh == rs_sinh(x + x*y, x, 5)
532
+
533
+ # constant term in series
534
+ c, s = rs_cosh_sinh(1 + x + x**2, x, 5)
535
+ assert c == rs_cosh(1 + x + x**2, x, 5)
536
+ assert s == rs_sinh(1 + x + x**2, x, 5)
537
+
538
+ a = symbols('a')
539
+ R, x, y = ring('x, y', QQ[sinh(a), cosh(a), a])
540
+ ch, sh = rs_cosh_sinh(x + a, x, 5)
541
+ assert ch == rs_cosh(x + a, x, 5)
542
+ assert sh == rs_sinh(x + a, x, 5)
543
+ R, x, y = ring('x, y', EX)
544
+ ch, sh = rs_cosh_sinh(x + a, x, 5)
545
+ assert ch == rs_cosh(x + a, x, 5)
546
+ assert sh == rs_sinh(x + a, x, 5)
547
+
548
+
549
+ def test_tanh():
550
+ R, x, y = ring('x, y', QQ)
551
+ assert rs_tanh(x, x, 9) == x - QQ(1,3)*x**3 + QQ(2,15)*x**5 - QQ(17,315)*x**7
552
+ assert rs_tanh(x*y + x**2*y**3, x, 9) == 4*x**8*y**11/3 - \
553
+ 17*x**8*y**9/45 + 4*x**7*y**9/3 - 17*x**7*y**7/315 - x**6*y**9/3 + \
554
+ 2*x**6*y**7/3 - x**5*y**7 + 2*x**5*y**5/15 - x**4*y**5 - \
555
+ x**3*y**3/3 + x**2*y**3 + x*y
556
+
557
+ # Constant term in series
558
+ a = symbols('a')
559
+ R, x, y = ring('x, y', EX)
560
+ assert rs_tanh(x + a, x, 5) == EX(tanh(a)**5 - 5*tanh(a)**3/3 +
561
+ 2*tanh(a)/3)*x**4 + EX(-tanh(a)**4 + 4*tanh(a)**2/3 - QQ(1, 3))*x**3 + \
562
+ EX(tanh(a)**3 - tanh(a))*x**2 + EX(-tanh(a)**2 + 1)*x + EX(tanh(a))
563
+
564
+ p = rs_tanh(x + x**2*y + a, x, 4)
565
+ assert (p.compose(x, 10)).compose(y, 5) == EX(-1000*tanh(a)**4 + \
566
+ 10100*tanh(a)**3 + 2470*tanh(a)**2/3 - 10099*tanh(a) + QQ(530, 3))
567
+
568
+
569
+ def test_RR():
570
+ rs_funcs = [rs_sin, rs_cos, rs_tan, rs_cot, rs_atan, rs_tanh]
571
+ sympy_funcs = [sin, cos, tan, cot, atan, tanh]
572
+ R, x, y = ring('x, y', RR)
573
+ a = symbols('a')
574
+ for rs_func, sympy_func in zip(rs_funcs, sympy_funcs):
575
+ p = rs_func(2 + x, x, 5).compose(x, 5)
576
+ q = sympy_func(2 + a).series(a, 0, 5).removeO()
577
+ is_close(p.as_expr(), q.subs(a, 5).n())
578
+
579
+ p = rs_nth_root(2 + x, 5, x, 5).compose(x, 5)
580
+ q = ((2 + a)**QQ(1, 5)).series(a, 0, 5).removeO()
581
+ is_close(p.as_expr(), q.subs(a, 5).n())
582
+
583
+
584
+ def test_is_regular():
585
+ R, x, y = puiseux_ring('x, y', QQ)
586
+ p = 1 + 2*x + x**2 + 3*x**3
587
+ assert not rs_is_puiseux(p, x)
588
+
589
+ p = x + x**QQ(1,5)*y
590
+ assert rs_is_puiseux(p, x)
591
+ assert not rs_is_puiseux(p, y)
592
+
593
+ p = x + x**2*y**QQ(1,5)*y
594
+ assert not rs_is_puiseux(p, x)
595
+
596
+
597
+ def test_puiseux():
598
+ R, x, y = puiseux_ring('x, y', QQ)
599
+ p = x**QQ(2,5) + x**QQ(2,3) + x
600
+
601
+ r = rs_series_inversion(p, x, 1)
602
+ r1 = -x**QQ(14,15) + x**QQ(4,5) - 3*x**QQ(11,15) + x**QQ(2,3) + \
603
+ 2*x**QQ(7,15) - x**QQ(2,5) - x**QQ(1,5) + x**QQ(2,15) - x**QQ(-2,15) \
604
+ + x**QQ(-2,5)
605
+ assert r == r1
606
+
607
+ r = rs_nth_root(1 + p, 3, x, 1)
608
+ assert r == -x**QQ(4,5)/9 + x**QQ(2,3)/3 + x**QQ(2,5)/3 + 1
609
+
610
+ r = rs_log(1 + p, x, 1)
611
+ assert r == -x**QQ(4,5)/2 + x**QQ(2,3) + x**QQ(2,5)
612
+
613
+ r = rs_LambertW(p, x, 1)
614
+ assert r == -x**QQ(4,5) + x**QQ(2,3) + x**QQ(2,5)
615
+
616
+ p1 = x + x**QQ(1,5)*y
617
+ r = rs_exp(p1, x, 1)
618
+ assert r == x**QQ(4,5)*y**4/24 + x**QQ(3,5)*y**3/6 + x**QQ(2,5)*y**2/2 + \
619
+ x**QQ(1,5)*y + 1
620
+
621
+ r = rs_atan(p, x, 2)
622
+ assert r == -x**QQ(9,5) - x**QQ(26,15) - x**QQ(22,15) - x**QQ(6,5)/3 + \
623
+ x + x**QQ(2,3) + x**QQ(2,5)
624
+
625
+ r = rs_atan(p1, x, 2)
626
+ assert r == x**QQ(9,5)*y**9/9 + x**QQ(9,5)*y**4 - x**QQ(7,5)*y**7/7 - \
627
+ x**QQ(7,5)*y**2 + x*y**5/5 + x - x**QQ(3,5)*y**3/3 + x**QQ(1,5)*y
628
+
629
+ r = rs_tan(p, x, 2)
630
+ assert r == x**QQ(2,5) + x**QQ(2,3) + x + QQ(1,3)*x**QQ(6,5) + x**QQ(22,15)\
631
+ + x**QQ(26,15) + x**QQ(9,5)
632
+
633
+ r = rs_sin(p, x, 2)
634
+ assert r == x**QQ(2,5) + x**QQ(2,3) + x - QQ(1,6)*x**QQ(6,5) - QQ(1,2)*x**\
635
+ QQ(22,15) - QQ(1,2)*x**QQ(26,15) - QQ(1,2)*x**QQ(9,5)
636
+
637
+ r = rs_cos(p, x, 2)
638
+ assert r == 1 - QQ(1,2)*x**QQ(4,5) - x**QQ(16,15) - QQ(1,2)*x**QQ(4,3) - \
639
+ x**QQ(7,5) + QQ(1,24)*x**QQ(8,5) - x**QQ(5,3) + QQ(1,6)*x**QQ(28,15)
640
+
641
+ r = rs_asin(p, x, 2)
642
+ assert r == x**QQ(9,5)/2 + x**QQ(26,15)/2 + x**QQ(22,15)/2 + \
643
+ x**QQ(6,5)/6 + x + x**QQ(2,3) + x**QQ(2,5)
644
+
645
+ r = rs_cot(p, x, 1)
646
+ assert r == -x**QQ(14,15) + x**QQ(4,5) - 3*x**QQ(11,15) + \
647
+ 2*x**QQ(2,3)/3 + 2*x**QQ(7,15) - 4*x**QQ(2,5)/3 - x**QQ(1,5) + \
648
+ x**QQ(2,15) - x**QQ(-2,15) + x**QQ(-2,5)
649
+
650
+ r = rs_cos_sin(p, x, 2)
651
+ assert r[0] == x**QQ(28,15)/6 - x**QQ(5,3) + x**QQ(8,5)/24 - x**QQ(7,5) - \
652
+ x**QQ(4,3)/2 - x**QQ(16,15) - x**QQ(4,5)/2 + 1
653
+ assert r[1] == -x**QQ(9,5)/2 - x**QQ(26,15)/2 - x**QQ(22,15)/2 - \
654
+ x**QQ(6,5)/6 + x + x**QQ(2,3) + x**QQ(2,5)
655
+
656
+ r = rs_atanh(p, x, 2)
657
+ assert r == x**QQ(9,5) + x**QQ(26,15) + x**QQ(22,15) + x**QQ(6,5)/3 + x + \
658
+ x**QQ(2,3) + x**QQ(2,5)
659
+
660
+ r = rs_asinh(p, x, 2)
661
+ assert r == x**QQ(2,5) + x**QQ(2,3) + x - QQ(1,6)*x**QQ(6,5) - QQ(1,2)*x**\
662
+ QQ(22,15) - QQ(1,2)*x**QQ(26,15) - QQ(1,2)*x**QQ(9,5)
663
+
664
+ r = rs_cosh(p, x, 2)
665
+ assert r == x**QQ(28,15)/6 + x**QQ(5,3) + x**QQ(8,5)/24 + x**QQ(7,5) + \
666
+ x**QQ(4,3)/2 + x**QQ(16,15) + x**QQ(4,5)/2 + 1
667
+
668
+ r = rs_sinh(p, x, 2)
669
+ assert r == x**QQ(9,5)/2 + x**QQ(26,15)/2 + x**QQ(22,15)/2 + \
670
+ x**QQ(6,5)/6 + x + x**QQ(2,3) + x**QQ(2,5)
671
+
672
+ r = rs_cosh_sinh(p, x, 2)
673
+ assert r[0] == x**QQ(28,15)/6 + x**QQ(5,3) + x**QQ(8,5)/24 + x**QQ(7,5) + \
674
+ x**QQ(4,3)/2 + x**QQ(16,15) + x**QQ(4,5)/2 + 1
675
+ assert r[1] == x**QQ(9,5)/2 + x**QQ(26,15)/2 + x**QQ(22,15)/2 + \
676
+ x**QQ(6,5)/6 + x + x**QQ(2,3) + x**QQ(2,5)
677
+
678
+ r = rs_tanh(p, x, 2)
679
+ assert r == -x**QQ(9,5) - x**QQ(26,15) - x**QQ(22,15) - x**QQ(6,5)/3 + \
680
+ x + x**QQ(2,3) + x**QQ(2,5)
681
+
682
+
683
+ def test_puiseux_algebraic(): # https://github.com/sympy/sympy/issues/24395
684
+
685
+ K = QQ.algebraic_field(sqrt(2))
686
+ sqrt2 = K.from_sympy(sqrt(2))
687
+ x, y = symbols('x, y')
688
+ R, xr, yr = puiseux_ring([x, y], K)
689
+ p = (1+sqrt2)*xr**QQ(1,2) + (1-sqrt2)*yr**QQ(2,3)
690
+
691
+ assert p.to_dict() == {(QQ(1,2),QQ(0)):1+sqrt2, (QQ(0),QQ(2,3)):1-sqrt2}
692
+ assert p.as_expr() == (1 + sqrt(2))*x**(S(1)/2) + (1 - sqrt(2))*y**(S(2)/3)
693
+
694
+
695
+ def test1():
696
+ R, x = puiseux_ring('x', QQ)
697
+ r = rs_sin(x, x, 15)*x**(-5)
698
+ assert r == x**8/6227020800 - x**6/39916800 + x**4/362880 - x**2/5040 + \
699
+ QQ(1,120) - x**-2/6 + x**-4
700
+
701
+ p = rs_sin(x, x, 10)
702
+ r = rs_nth_root(p, 2, x, 10)
703
+ assert r == -67*x**QQ(17,2)/29030400 - x**QQ(13,2)/24192 + \
704
+ x**QQ(9,2)/1440 - x**QQ(5,2)/12 + x**QQ(1,2)
705
+
706
+ p = rs_sin(x, x, 10)
707
+ r = rs_nth_root(p, 7, x, 10)
708
+ r = rs_pow(r, 5, x, 10)
709
+ assert r == -97*x**QQ(61,7)/124467840 - x**QQ(47,7)/16464 + \
710
+ 11*x**QQ(33,7)/3528 - 5*x**QQ(19,7)/42 + x**QQ(5,7)
711
+
712
+ r = rs_exp(x**QQ(1,2), x, 10)
713
+ assert r == x**QQ(19,2)/121645100408832000 + x**9/6402373705728000 + \
714
+ x**QQ(17,2)/355687428096000 + x**8/20922789888000 + \
715
+ x**QQ(15,2)/1307674368000 + x**7/87178291200 + \
716
+ x**QQ(13,2)/6227020800 + x**6/479001600 + x**QQ(11,2)/39916800 + \
717
+ x**5/3628800 + x**QQ(9,2)/362880 + x**4/40320 + x**QQ(7,2)/5040 + \
718
+ x**3/720 + x**QQ(5,2)/120 + x**2/24 + x**QQ(3,2)/6 + x/2 + \
719
+ x**QQ(1,2) + 1
720
+
721
+
722
+ def test_puiseux2():
723
+ R, y = ring('y', QQ)
724
+ S, x = puiseux_ring('x', R.to_domain())
725
+
726
+ p = x + x**QQ(1,5)*y
727
+ r = rs_atan(p, x, 3)
728
+ assert r == (y**13/13 + y**8 + 2*y**3)*x**QQ(13,5) - (y**11/11 + y**6 +
729
+ y)*x**QQ(11,5) + (y**9/9 + y**4)*x**QQ(9,5) - (y**7/7 +
730
+ y**2)*x**QQ(7,5) + (y**5/5 + 1)*x - y**3*x**QQ(3,5)/3 + y*x**QQ(1,5)
731
+
732
+
733
+ @slow
734
+ def test_rs_series():
735
+ x, a, b, c = symbols('x, a, b, c')
736
+
737
+ assert rs_series(a, a, 5).as_expr() == a
738
+ assert rs_series(sin(a), a, 5).as_expr() == (sin(a).series(a, 0,
739
+ 5)).removeO()
740
+ assert rs_series(sin(a) + cos(a), a, 5).as_expr() == ((sin(a) +
741
+ cos(a)).series(a, 0, 5)).removeO()
742
+ assert rs_series(sin(a)*cos(a), a, 5).as_expr() == ((sin(a)*
743
+ cos(a)).series(a, 0, 5)).removeO()
744
+
745
+ p = (sin(a) - a)*(cos(a**2) + a**4/2)
746
+ assert expand(rs_series(p, a, 10).as_expr()) == expand(p.series(a, 0,
747
+ 10).removeO())
748
+
749
+ p = sin(a**2/2 + a/3) + cos(a/5)*sin(a/2)**3
750
+ assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0,
751
+ 5).removeO())
752
+
753
+ p = sin(x**2 + a)*(cos(x**3 - 1) - a - a**2)
754
+ assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0,
755
+ 5).removeO())
756
+
757
+ p = sin(a**2 - a/3 + 2)**5*exp(a**3 - a/2)
758
+ assert expand(rs_series(p, a, 10).as_expr()) == expand(p.series(a, 0,
759
+ 10).removeO())
760
+
761
+ p = sin(a + b + c)
762
+ assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0,
763
+ 5).removeO())
764
+
765
+ p = tan(sin(a**2 + 4) + b + c)
766
+ assert expand(rs_series(p, a, 6).as_expr()) == expand(p.series(a, 0,
767
+ 6).removeO())
768
+
769
+ p = a**QQ(2,5) + a**QQ(2,3) + a
770
+
771
+ r = rs_series(tan(p), a, 2)
772
+ assert r.as_expr() == a**QQ(9,5) + a**QQ(26,15) + a**QQ(22,15) + a**QQ(6,5)/3 + \
773
+ a + a**QQ(2,3) + a**QQ(2,5)
774
+
775
+ r = rs_series(exp(p), a, 1)
776
+ assert r.as_expr() == a**QQ(4,5)/2 + a**QQ(2,3) + a**QQ(2,5) + 1
777
+
778
+ r = rs_series(sin(p), a, 2)
779
+ assert r.as_expr() == -a**QQ(9,5)/2 - a**QQ(26,15)/2 - a**QQ(22,15)/2 - \
780
+ a**QQ(6,5)/6 + a + a**QQ(2,3) + a**QQ(2,5)
781
+
782
+ r = rs_series(cos(p), a, 2)
783
+ assert r.as_expr() == a**QQ(28,15)/6 - a**QQ(5,3) + a**QQ(8,5)/24 - a**QQ(7,5) - \
784
+ a**QQ(4,3)/2 - a**QQ(16,15) - a**QQ(4,5)/2 + 1
785
+
786
+ assert rs_series(sin(a)/7, a, 5).as_expr() == (sin(a)/7).series(a, 0,
787
+ 5).removeO()
788
+
789
+
790
+ def test_rs_series_ConstantInExpr():
791
+ x, a = symbols('x a')
792
+ assert rs_series(log(1 + x), x, 5).as_expr() == -x**4/4 + x**3/3 - \
793
+ x**2/2 + x
794
+ assert rs_series(log(1 + 4*x), x, 5).as_expr() == -64*x**4 + 64*x**3/3 - \
795
+ 8*x**2 + 4*x
796
+ assert rs_series(log(1 + x + x**2), x, 10).as_expr() == -2*x**9/9 + \
797
+ x**8/8 + x**7/7 - x**6/3 + x**5/5 + x**4/4 - 2*x**3/3 + x**2/2 + x
798
+ assert rs_series(log(1 + x*a**2), x, 7).as_expr() == -x**6*a**12/6 + \
799
+ x**5*a**10/5 - x**4*a**8/4 + x**3*a**6/3 - x**2*a**4/2 + x*a**2
800
+
801
+ assert rs_series(atan(1 + x), x, 9).as_expr() == -x**7/112 + x**6/48 - x**5/40 \
802
+ + x**3/12 - x**2/4 + x/2 + pi/4
803
+ assert rs_series(atan(1 + x + x**2),x, 9).as_expr() == -15*x**7/112 - x**6/48 + \
804
+ 9*x**5/40 - 5*x**3/12 + x**2/4 + x/2 + pi/4
805
+ assert rs_series(atan(1 + x * a), x, 9).as_expr() == -a**7*x**7/112 + a**6*x**6/48 \
806
+ - a**5*x**5/40 + a**3*x**3/12 - a**2*x**2/4 + a*x/2 + pi/4
807
+
808
+ assert rs_series(tanh(1 + x), x, 5).as_expr() == -5*x**4*tanh(1)**3/3 + x**4* \
809
+ tanh(1)**5 + 2*x**4*tanh(1)/3 - x**3*tanh(1)**4 - x**3/3 + 4*x**3*tanh(1) \
810
+ **2/3 - x**2*tanh(1) + x**2*tanh(1)**3 - x*tanh(1)**2 + x + tanh(1)
811
+ assert rs_series(tanh(1 + x * a), x, 3).as_expr() == -a**2*x**2*tanh(1) + a**2*x** \
812
+ 2*tanh(1)**3 - a*x*tanh(1)**2 + a*x + tanh(1)
813
+
814
+ assert rs_series(sinh(1 + x), x, 5).as_expr() == x**4*sinh(1)/24 + x**3*cosh(1)/6 + \
815
+ x**2*sinh(1)/2 + x*cosh(1) + sinh(1)
816
+ assert rs_series(sinh(1 + x * a), x, 5).as_expr() == a**4*x**4*sinh(1)/24 + \
817
+ a**3*x**3*cosh(1)/6 + a**2*x**2*sinh(1)/2 + a*x*cosh(1) + sinh(1)
818
+
819
+ assert rs_series(cosh(1 + x), x, 5).as_expr() == x**4*cosh(1)/24 + x**3*sinh(1)/6 + \
820
+ x**2*cosh(1)/2 + x*sinh(1) + cosh(1)
821
+ assert rs_series(cosh(1 + x * a), x, 5).as_expr() == a**4*x**4*cosh(1)/24 + \
822
+ a**3*x**3*sinh(1)/6 + a**2*x**2*cosh(1)/2 + a*x*sinh(1) + cosh(1)
823
+
824
+
825
+ def test_issue():
826
+ # https://github.com/sympy/sympy/issues/10191
827
+ # https://github.com/sympy/sympy/issues/19543
828
+
829
+ a, b = symbols('a b')
830
+ assert rs_series(sin(a**QQ(3,7))*exp(a + b**QQ(6,7)), a,2).as_expr() == \
831
+ a**QQ(10,7)*exp(b**QQ(6,7)) - a**QQ(9,7)*exp(b**QQ(6,7))/6 + a**QQ(3,7)*exp(b**QQ(6,7))
miniconda3/envs/ladir/lib/python3.10/site-packages/sympy/polys/tests/test_rings.py ADDED
@@ -0,0 +1,1591 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Test sparse polynomials. """
2
+
3
+ from functools import reduce
4
+ from operator import add, mul
5
+
6
+ from sympy.polys.rings import ring, xring, sring, PolyRing, PolyElement
7
+ from sympy.polys.fields import field, FracField
8
+ from sympy.polys.densebasic import ninf
9
+ from sympy.polys.domains import ZZ, QQ, RR, FF, EX
10
+ from sympy.polys.orderings import lex, grlex
11
+ from sympy.polys.polyerrors import GeneratorsError, \
12
+ ExactQuotientFailed, MultivariatePolynomialError, CoercionFailed
13
+
14
+ from sympy.testing.pytest import raises
15
+ from sympy.core import Symbol, symbols
16
+ from sympy.core.singleton import S
17
+ from sympy.core.numbers import pi
18
+ from sympy.functions.elementary.exponential import exp
19
+ from sympy.functions.elementary.miscellaneous import sqrt
20
+
21
+ def test_PolyRing___init__():
22
+ x, y, z, t = map(Symbol, "xyzt")
23
+
24
+ assert len(PolyRing("x,y,z", ZZ, lex).gens) == 3
25
+ assert len(PolyRing(x, ZZ, lex).gens) == 1
26
+ assert len(PolyRing(("x", "y", "z"), ZZ, lex).gens) == 3
27
+ assert len(PolyRing((x, y, z), ZZ, lex).gens) == 3
28
+ assert len(PolyRing("", ZZ, lex).gens) == 0
29
+ assert len(PolyRing([], ZZ, lex).gens) == 0
30
+
31
+ raises(GeneratorsError, lambda: PolyRing(0, ZZ, lex))
32
+
33
+ assert PolyRing("x", ZZ[t], lex).domain == ZZ[t]
34
+ assert PolyRing("x", 'ZZ[t]', lex).domain == ZZ[t]
35
+ assert PolyRing("x", PolyRing("t", ZZ, lex), lex).domain == ZZ[t]
36
+
37
+ raises(GeneratorsError, lambda: PolyRing("x", PolyRing("x", ZZ, lex), lex))
38
+
39
+ _lex = Symbol("lex")
40
+ assert PolyRing("x", ZZ, lex).order == lex
41
+ assert PolyRing("x", ZZ, _lex).order == lex
42
+ assert PolyRing("x", ZZ, 'lex').order == lex
43
+
44
+ R1 = PolyRing("x,y", ZZ, lex)
45
+ R2 = PolyRing("x,y", ZZ, lex)
46
+ R3 = PolyRing("x,y,z", ZZ, lex)
47
+
48
+ assert R1.x == R1.gens[0]
49
+ assert R1.y == R1.gens[1]
50
+ assert R1.x == R2.x
51
+ assert R1.y == R2.y
52
+ assert R1.x != R3.x
53
+ assert R1.y != R3.y
54
+
55
+ def test_PolyRing___hash__():
56
+ R, x, y, z = ring("x,y,z", QQ)
57
+ assert hash(R)
58
+
59
+ def test_PolyRing___eq__():
60
+ assert ring("x,y,z", QQ)[0] == ring("x,y,z", QQ)[0]
61
+ assert ring("x,y,z", QQ)[0] != ring("x,y,z", ZZ)[0]
62
+ assert ring("x,y,z", ZZ)[0] != ring("x,y,z", QQ)[0]
63
+ assert ring("x,y,z", QQ)[0] != ring("x,y", QQ)[0]
64
+ assert ring("x,y", QQ)[0] != ring("x,y,z", QQ)[0]
65
+
66
+ def test_PolyRing_ring_new():
67
+ R, x, y, z = ring("x,y,z", QQ)
68
+
69
+ assert R.ring_new(7) == R(7)
70
+ assert R.ring_new(7*x*y*z) == 7*x*y*z
71
+
72
+ f = x**2 + 2*x*y + 3*x + 4*z**2 + 5*z + 6
73
+
74
+ assert R.ring_new([[[1]], [[2], [3]], [[4, 5, 6]]]) == f
75
+ assert R.ring_new({(2, 0, 0): 1, (1, 1, 0): 2, (1, 0, 0): 3, (0, 0, 2): 4, (0, 0, 1): 5, (0, 0, 0): 6}) == f
76
+ assert R.ring_new([((2, 0, 0), 1), ((1, 1, 0), 2), ((1, 0, 0), 3), ((0, 0, 2), 4), ((0, 0, 1), 5), ((0, 0, 0), 6)]) == f
77
+
78
+ R, = ring("", QQ)
79
+ assert R.ring_new([((), 7)]) == R(7)
80
+
81
+ def test_PolyRing_drop():
82
+ R, x,y,z = ring("x,y,z", ZZ)
83
+
84
+ assert R.drop(x) == PolyRing("y,z", ZZ, lex)
85
+ assert R.drop(y) == PolyRing("x,z", ZZ, lex)
86
+ assert R.drop(z) == PolyRing("x,y", ZZ, lex)
87
+
88
+ assert R.drop(0) == PolyRing("y,z", ZZ, lex)
89
+ assert R.drop(0).drop(0) == PolyRing("z", ZZ, lex)
90
+ assert R.drop(0).drop(0).drop(0) == ZZ
91
+
92
+ assert R.drop(1) == PolyRing("x,z", ZZ, lex)
93
+
94
+ assert R.drop(2) == PolyRing("x,y", ZZ, lex)
95
+ assert R.drop(2).drop(1) == PolyRing("x", ZZ, lex)
96
+ assert R.drop(2).drop(1).drop(0) == ZZ
97
+
98
+ raises(ValueError, lambda: R.drop(3))
99
+ raises(ValueError, lambda: R.drop(x).drop(y))
100
+
101
+ def test_PolyRing___getitem__():
102
+ R, x,y,z = ring("x,y,z", ZZ)
103
+
104
+ assert R[0:] == PolyRing("x,y,z", ZZ, lex)
105
+ assert R[1:] == PolyRing("y,z", ZZ, lex)
106
+ assert R[2:] == PolyRing("z", ZZ, lex)
107
+ assert R[3:] == ZZ
108
+
109
+ def test_PolyRing_is_():
110
+ R = PolyRing("x", QQ, lex)
111
+
112
+ assert R.is_univariate is True
113
+ assert R.is_multivariate is False
114
+
115
+ R = PolyRing("x,y,z", QQ, lex)
116
+
117
+ assert R.is_univariate is False
118
+ assert R.is_multivariate is True
119
+
120
+ R = PolyRing("", QQ, lex)
121
+
122
+ assert R.is_univariate is False
123
+ assert R.is_multivariate is False
124
+
125
+ def test_PolyRing_add():
126
+ R, x = ring("x", ZZ)
127
+ F = [ x**2 + 2*i + 3 for i in range(4) ]
128
+
129
+ assert R.add(F) == reduce(add, F) == 4*x**2 + 24
130
+
131
+ R, = ring("", ZZ)
132
+
133
+ assert R.add([2, 5, 7]) == 14
134
+
135
+ def test_PolyRing_mul():
136
+ R, x = ring("x", ZZ)
137
+ F = [ x**2 + 2*i + 3 for i in range(4) ]
138
+
139
+ assert R.mul(F) == reduce(mul, F) == x**8 + 24*x**6 + 206*x**4 + 744*x**2 + 945
140
+
141
+ R, = ring("", ZZ)
142
+
143
+ assert R.mul([2, 3, 5]) == 30
144
+
145
+ def test_PolyRing_symmetric_poly():
146
+ R, x, y, z, t = ring("x,y,z,t", ZZ)
147
+
148
+ raises(ValueError, lambda: R.symmetric_poly(-1))
149
+ raises(ValueError, lambda: R.symmetric_poly(5))
150
+
151
+ assert R.symmetric_poly(0) == R.one
152
+ assert R.symmetric_poly(1) == x + y + z + t
153
+ assert R.symmetric_poly(2) == x*y + x*z + x*t + y*z + y*t + z*t
154
+ assert R.symmetric_poly(3) == x*y*z + x*y*t + x*z*t + y*z*t
155
+ assert R.symmetric_poly(4) == x*y*z*t
156
+
157
+ def test_sring():
158
+ x, y, z, t = symbols("x,y,z,t")
159
+
160
+ R = PolyRing("x,y,z", ZZ, lex)
161
+ assert sring(x + 2*y + 3*z) == (R, R.x + 2*R.y + 3*R.z)
162
+
163
+ R = PolyRing("x,y,z", QQ, lex)
164
+ assert sring(x + 2*y + z/3) == (R, R.x + 2*R.y + R.z/3)
165
+ assert sring([x, 2*y, z/3]) == (R, [R.x, 2*R.y, R.z/3])
166
+
167
+ Rt = PolyRing("t", ZZ, lex)
168
+ R = PolyRing("x,y,z", Rt, lex)
169
+ assert sring(x + 2*t*y + 3*t**2*z, x, y, z) == (R, R.x + 2*Rt.t*R.y + 3*Rt.t**2*R.z)
170
+
171
+ Rt = PolyRing("t", QQ, lex)
172
+ R = PolyRing("x,y,z", Rt, lex)
173
+ assert sring(x + t*y/2 + t**2*z/3, x, y, z) == (R, R.x + Rt.t*R.y/2 + Rt.t**2*R.z/3)
174
+
175
+ Rt = FracField("t", ZZ, lex)
176
+ R = PolyRing("x,y,z", Rt, lex)
177
+ assert sring(x + 2*y/t + t**2*z/3, x, y, z) == (R, R.x + 2*R.y/Rt.t + Rt.t**2*R.z/3)
178
+
179
+ r = sqrt(2) - sqrt(3)
180
+ R, a = sring(r, extension=True)
181
+ assert R.domain == QQ.algebraic_field(sqrt(2) + sqrt(3))
182
+ assert R.gens == ()
183
+ assert a == R.domain.from_sympy(r)
184
+
185
+ def test_PolyElement___hash__():
186
+ R, x, y, z = ring("x,y,z", QQ)
187
+ assert hash(x*y*z)
188
+
189
+ def test_PolyElement___eq__():
190
+ R, x, y = ring("x,y", ZZ, lex)
191
+
192
+ assert ((x*y + 5*x*y) == 6) == False
193
+ assert ((x*y + 5*x*y) == 6*x*y) == True
194
+ assert (6 == (x*y + 5*x*y)) == False
195
+ assert (6*x*y == (x*y + 5*x*y)) == True
196
+
197
+ assert ((x*y - x*y) == 0) == True
198
+ assert (0 == (x*y - x*y)) == True
199
+
200
+ assert ((x*y - x*y) == 1) == False
201
+ assert (1 == (x*y - x*y)) == False
202
+
203
+ assert ((x*y - x*y) == 1) == False
204
+ assert (1 == (x*y - x*y)) == False
205
+
206
+ assert ((x*y + 5*x*y) != 6) == True
207
+ assert ((x*y + 5*x*y) != 6*x*y) == False
208
+ assert (6 != (x*y + 5*x*y)) == True
209
+ assert (6*x*y != (x*y + 5*x*y)) == False
210
+
211
+ assert ((x*y - x*y) != 0) == False
212
+ assert (0 != (x*y - x*y)) == False
213
+
214
+ assert ((x*y - x*y) != 1) == True
215
+ assert (1 != (x*y - x*y)) == True
216
+
217
+ assert R.one == QQ(1, 1) == R.one
218
+ assert R.one == 1 == R.one
219
+
220
+ Rt, t = ring("t", ZZ)
221
+ R, x, y = ring("x,y", Rt)
222
+
223
+ assert (t**3*x/x == t**3) == True
224
+ assert (t**3*x/x == t**4) == False
225
+
226
+ def test_PolyElement__lt_le_gt_ge__():
227
+ R, x, y = ring("x,y", ZZ)
228
+
229
+ assert R(1) < x < x**2 < x**3
230
+ assert R(1) <= x <= x**2 <= x**3
231
+
232
+ assert x**3 > x**2 > x > R(1)
233
+ assert x**3 >= x**2 >= x >= R(1)
234
+
235
+ def test_PolyElement__str__():
236
+ x, y = symbols('x, y')
237
+
238
+ for dom in [ZZ, QQ, ZZ[x], ZZ[x,y], ZZ[x][y]]:
239
+ R, t = ring('t', dom)
240
+ assert str(2*t**2 + 1) == '2*t**2 + 1'
241
+
242
+ for dom in [EX, EX[x]]:
243
+ R, t = ring('t', dom)
244
+ assert str(2*t**2 + 1) == 'EX(2)*t**2 + EX(1)'
245
+
246
+ def test_PolyElement_copy():
247
+ R, x, y, z = ring("x,y,z", ZZ)
248
+
249
+ f = x*y + 3*z
250
+ g = f.copy()
251
+
252
+ assert f == g
253
+ g[(1, 1, 1)] = 7
254
+ assert f != g
255
+
256
+ def test_PolyElement_as_expr():
257
+ R, x, y, z = ring("x,y,z", ZZ)
258
+ f = 3*x**2*y - x*y*z + 7*z**3 + 1
259
+
260
+ X, Y, Z = R.symbols
261
+ g = 3*X**2*Y - X*Y*Z + 7*Z**3 + 1
262
+
263
+ assert f != g
264
+ assert f.as_expr() == g
265
+
266
+ U, V, W = symbols("u,v,w")
267
+ g = 3*U**2*V - U*V*W + 7*W**3 + 1
268
+
269
+ assert f != g
270
+ assert f.as_expr(U, V, W) == g
271
+
272
+ raises(ValueError, lambda: f.as_expr(X))
273
+
274
+ R, = ring("", ZZ)
275
+ assert R(3).as_expr() == 3
276
+
277
+ def test_PolyElement_from_expr():
278
+ x, y, z = symbols("x,y,z")
279
+ R, X, Y, Z = ring((x, y, z), ZZ)
280
+
281
+ f = R.from_expr(1)
282
+ assert f == 1 and R.is_element(f)
283
+
284
+ f = R.from_expr(x)
285
+ assert f == X and R.is_element(f)
286
+
287
+ f = R.from_expr(x*y*z)
288
+ assert f == X*Y*Z and R.is_element(f)
289
+
290
+ f = R.from_expr(x*y*z + x*y + x)
291
+ assert f == X*Y*Z + X*Y + X and R.is_element(f)
292
+
293
+ f = R.from_expr(x**3*y*z + x**2*y**7 + 1)
294
+ assert f == X**3*Y*Z + X**2*Y**7 + 1 and R.is_element(f)
295
+
296
+ r, F = sring([exp(2)])
297
+ f = r.from_expr(exp(2))
298
+ assert f == F[0] and r.is_element(f)
299
+
300
+ raises(ValueError, lambda: R.from_expr(1/x))
301
+ raises(ValueError, lambda: R.from_expr(2**x))
302
+ raises(ValueError, lambda: R.from_expr(7*x + sqrt(2)))
303
+
304
+ R, = ring("", ZZ)
305
+ f = R.from_expr(1)
306
+ assert f == 1 and R.is_element(f)
307
+
308
+ def test_PolyElement_degree():
309
+ R, x,y,z = ring("x,y,z", ZZ)
310
+
311
+ assert ninf == float('-inf')
312
+
313
+ assert R(0).degree() is ninf
314
+ assert R(1).degree() == 0
315
+ assert (x + 1).degree() == 1
316
+ assert (2*y**3 + z).degree() == 0
317
+ assert (x*y**3 + z).degree() == 1
318
+ assert (x**5*y**3 + z).degree() == 5
319
+
320
+ assert R(0).degree(x) is ninf
321
+ assert R(1).degree(x) == 0
322
+ assert (x + 1).degree(x) == 1
323
+ assert (2*y**3 + z).degree(x) == 0
324
+ assert (x*y**3 + z).degree(x) == 1
325
+ assert (7*x**5*y**3 + z).degree(x) == 5
326
+
327
+ assert R(0).degree(y) is ninf
328
+ assert R(1).degree(y) == 0
329
+ assert (x + 1).degree(y) == 0
330
+ assert (2*y**3 + z).degree(y) == 3
331
+ assert (x*y**3 + z).degree(y) == 3
332
+ assert (7*x**5*y**3 + z).degree(y) == 3
333
+
334
+ assert R(0).degree(z) is ninf
335
+ assert R(1).degree(z) == 0
336
+ assert (x + 1).degree(z) == 0
337
+ assert (2*y**3 + z).degree(z) == 1
338
+ assert (x*y**3 + z).degree(z) == 1
339
+ assert (7*x**5*y**3 + z).degree(z) == 1
340
+
341
+ R, = ring("", ZZ)
342
+ assert R(0).degree() is ninf
343
+ assert R(1).degree() == 0
344
+
345
+ def test_PolyElement_tail_degree():
346
+ R, x,y,z = ring("x,y,z", ZZ)
347
+
348
+ assert R(0).tail_degree() is ninf
349
+ assert R(1).tail_degree() == 0
350
+ assert (x + 1).tail_degree() == 0
351
+ assert (2*y**3 + x**3*z).tail_degree() == 0
352
+ assert (x*y**3 + x**3*z).tail_degree() == 1
353
+ assert (x**5*y**3 + x**3*z).tail_degree() == 3
354
+
355
+ assert R(0).tail_degree(x) is ninf
356
+ assert R(1).tail_degree(x) == 0
357
+ assert (x + 1).tail_degree(x) == 0
358
+ assert (2*y**3 + x**3*z).tail_degree(x) == 0
359
+ assert (x*y**3 + x**3*z).tail_degree(x) == 1
360
+ assert (7*x**5*y**3 + x**3*z).tail_degree(x) == 3
361
+
362
+ assert R(0).tail_degree(y) is ninf
363
+ assert R(1).tail_degree(y) == 0
364
+ assert (x + 1).tail_degree(y) == 0
365
+ assert (2*y**3 + x**3*z).tail_degree(y) == 0
366
+ assert (x*y**3 + x**3*z).tail_degree(y) == 0
367
+ assert (7*x**5*y**3 + x**3*z).tail_degree(y) == 0
368
+
369
+ assert R(0).tail_degree(z) is ninf
370
+ assert R(1).tail_degree(z) == 0
371
+ assert (x + 1).tail_degree(z) == 0
372
+ assert (2*y**3 + x**3*z).tail_degree(z) == 0
373
+ assert (x*y**3 + x**3*z).tail_degree(z) == 0
374
+ assert (7*x**5*y**3 + x**3*z).tail_degree(z) == 0
375
+
376
+ R, = ring("", ZZ)
377
+ assert R(0).tail_degree() is ninf
378
+ assert R(1).tail_degree() == 0
379
+
380
+ def test_PolyElement_degrees():
381
+ R, x,y,z = ring("x,y,z", ZZ)
382
+
383
+ assert R(0).degrees() == (ninf, ninf, ninf)
384
+ assert R(1).degrees() == (0, 0, 0)
385
+ assert (x**2*y + x**3*z**2).degrees() == (3, 1, 2)
386
+
387
+ def test_PolyElement_tail_degrees():
388
+ R, x,y,z = ring("x,y,z", ZZ)
389
+
390
+ assert R(0).tail_degrees() == (ninf, ninf, ninf)
391
+ assert R(1).tail_degrees() == (0, 0, 0)
392
+ assert (x**2*y + x**3*z**2).tail_degrees() == (2, 0, 0)
393
+
394
+ def test_PolyElement_coeff():
395
+ R, x, y, z = ring("x,y,z", ZZ, lex)
396
+ f = 3*x**2*y - x*y*z + 7*z**3 + 23
397
+
398
+ assert f.coeff(1) == 23
399
+ raises(ValueError, lambda: f.coeff(3))
400
+
401
+ assert f.coeff(x) == 0
402
+ assert f.coeff(y) == 0
403
+ assert f.coeff(z) == 0
404
+
405
+ assert f.coeff(x**2*y) == 3
406
+ assert f.coeff(x*y*z) == -1
407
+ assert f.coeff(z**3) == 7
408
+
409
+ raises(ValueError, lambda: f.coeff(3*x**2*y))
410
+ raises(ValueError, lambda: f.coeff(-x*y*z))
411
+ raises(ValueError, lambda: f.coeff(7*z**3))
412
+
413
+ R, = ring("", ZZ)
414
+ assert R(3).coeff(1) == 3
415
+
416
+ def test_PolyElement_LC():
417
+ R, x, y = ring("x,y", QQ, lex)
418
+ assert R(0).LC == QQ(0)
419
+ assert (QQ(1,2)*x).LC == QQ(1, 2)
420
+ assert (QQ(1,4)*x*y + QQ(1,2)*x).LC == QQ(1, 4)
421
+
422
+ def test_PolyElement_LM():
423
+ R, x, y = ring("x,y", QQ, lex)
424
+ assert R(0).LM == (0, 0)
425
+ assert (QQ(1,2)*x).LM == (1, 0)
426
+ assert (QQ(1,4)*x*y + QQ(1,2)*x).LM == (1, 1)
427
+
428
+ def test_PolyElement_LT():
429
+ R, x, y = ring("x,y", QQ, lex)
430
+ assert R(0).LT == ((0, 0), QQ(0))
431
+ assert (QQ(1,2)*x).LT == ((1, 0), QQ(1, 2))
432
+ assert (QQ(1,4)*x*y + QQ(1,2)*x).LT == ((1, 1), QQ(1, 4))
433
+
434
+ R, = ring("", ZZ)
435
+ assert R(0).LT == ((), 0)
436
+ assert R(1).LT == ((), 1)
437
+
438
+ def test_PolyElement_leading_monom():
439
+ R, x, y = ring("x,y", QQ, lex)
440
+ assert R(0).leading_monom() == 0
441
+ assert (QQ(1,2)*x).leading_monom() == x
442
+ assert (QQ(1,4)*x*y + QQ(1,2)*x).leading_monom() == x*y
443
+
444
+ def test_PolyElement_leading_term():
445
+ R, x, y = ring("x,y", QQ, lex)
446
+ assert R(0).leading_term() == 0
447
+ assert (QQ(1,2)*x).leading_term() == QQ(1,2)*x
448
+ assert (QQ(1,4)*x*y + QQ(1,2)*x).leading_term() == QQ(1,4)*x*y
449
+
450
+ def test_PolyElement_terms():
451
+ R, x,y,z = ring("x,y,z", QQ)
452
+ terms = (x**2/3 + y**3/4 + z**4/5).terms()
453
+ assert terms == [((2,0,0), QQ(1,3)), ((0,3,0), QQ(1,4)), ((0,0,4), QQ(1,5))]
454
+
455
+ R, x,y = ring("x,y", ZZ, lex)
456
+ f = x*y**7 + 2*x**2*y**3
457
+
458
+ assert f.terms() == f.terms(lex) == f.terms('lex') == [((2, 3), 2), ((1, 7), 1)]
459
+ assert f.terms(grlex) == f.terms('grlex') == [((1, 7), 1), ((2, 3), 2)]
460
+
461
+ R, x,y = ring("x,y", ZZ, grlex)
462
+ f = x*y**7 + 2*x**2*y**3
463
+
464
+ assert f.terms() == f.terms(grlex) == f.terms('grlex') == [((1, 7), 1), ((2, 3), 2)]
465
+ assert f.terms(lex) == f.terms('lex') == [((2, 3), 2), ((1, 7), 1)]
466
+
467
+ R, = ring("", ZZ)
468
+ assert R(3).terms() == [((), 3)]
469
+
470
+ def test_PolyElement_monoms():
471
+ R, x,y,z = ring("x,y,z", QQ)
472
+ monoms = (x**2/3 + y**3/4 + z**4/5).monoms()
473
+ assert monoms == [(2,0,0), (0,3,0), (0,0,4)]
474
+
475
+ R, x,y = ring("x,y", ZZ, lex)
476
+ f = x*y**7 + 2*x**2*y**3
477
+
478
+ assert f.monoms() == f.monoms(lex) == f.monoms('lex') == [(2, 3), (1, 7)]
479
+ assert f.monoms(grlex) == f.monoms('grlex') == [(1, 7), (2, 3)]
480
+
481
+ R, x,y = ring("x,y", ZZ, grlex)
482
+ f = x*y**7 + 2*x**2*y**3
483
+
484
+ assert f.monoms() == f.monoms(grlex) == f.monoms('grlex') == [(1, 7), (2, 3)]
485
+ assert f.monoms(lex) == f.monoms('lex') == [(2, 3), (1, 7)]
486
+
487
+ def test_PolyElement_coeffs():
488
+ R, x,y,z = ring("x,y,z", QQ)
489
+ coeffs = (x**2/3 + y**3/4 + z**4/5).coeffs()
490
+ assert coeffs == [QQ(1,3), QQ(1,4), QQ(1,5)]
491
+
492
+ R, x,y = ring("x,y", ZZ, lex)
493
+ f = x*y**7 + 2*x**2*y**3
494
+
495
+ assert f.coeffs() == f.coeffs(lex) == f.coeffs('lex') == [2, 1]
496
+ assert f.coeffs(grlex) == f.coeffs('grlex') == [1, 2]
497
+
498
+ R, x,y = ring("x,y", ZZ, grlex)
499
+ f = x*y**7 + 2*x**2*y**3
500
+
501
+ assert f.coeffs() == f.coeffs(grlex) == f.coeffs('grlex') == [1, 2]
502
+ assert f.coeffs(lex) == f.coeffs('lex') == [2, 1]
503
+
504
+ def test_PolyElement___add__():
505
+ Rt, t = ring("t", ZZ)
506
+ Ruv, u,v = ring("u,v", ZZ)
507
+ Rxyz, x,y,z = ring("x,y,z", Ruv)
508
+
509
+ assert dict(x + 3*y) == {(1, 0, 0): 1, (0, 1, 0): 3}
510
+
511
+ assert dict(u + x) == dict(x + u) == {(1, 0, 0): 1, (0, 0, 0): u}
512
+ assert dict(u + x*y) == dict(x*y + u) == {(1, 1, 0): 1, (0, 0, 0): u}
513
+ assert dict(u + x*y + z) == dict(x*y + z + u) == {(1, 1, 0): 1, (0, 0, 1): 1, (0, 0, 0): u}
514
+
515
+ assert dict(u*x + x) == dict(x + u*x) == {(1, 0, 0): u + 1}
516
+ assert dict(u*x + x*y) == dict(x*y + u*x) == {(1, 1, 0): 1, (1, 0, 0): u}
517
+ assert dict(u*x + x*y + z) == dict(x*y + z + u*x) == {(1, 1, 0): 1, (0, 0, 1): 1, (1, 0, 0): u}
518
+
519
+ raises(TypeError, lambda: t + x)
520
+ raises(TypeError, lambda: x + t)
521
+ raises(TypeError, lambda: t + u)
522
+ raises(TypeError, lambda: u + t)
523
+
524
+ Fuv, u,v = field("u,v", ZZ)
525
+ Rxyz, x,y,z = ring("x,y,z", Fuv)
526
+
527
+ assert dict(u + x) == dict(x + u) == {(1, 0, 0): 1, (0, 0, 0): u}
528
+
529
+ Rxyz, x,y,z = ring("x,y,z", EX)
530
+
531
+ assert dict(EX(pi) + x*y*z) == dict(x*y*z + EX(pi)) == {(1, 1, 1): EX(1), (0, 0, 0): EX(pi)}
532
+
533
+ def test_PolyElement___sub__():
534
+ Rt, t = ring("t", ZZ)
535
+ Ruv, u,v = ring("u,v", ZZ)
536
+ Rxyz, x,y,z = ring("x,y,z", Ruv)
537
+
538
+ assert dict(x - 3*y) == {(1, 0, 0): 1, (0, 1, 0): -3}
539
+
540
+ assert dict(-u + x) == dict(x - u) == {(1, 0, 0): 1, (0, 0, 0): -u}
541
+ assert dict(-u + x*y) == dict(x*y - u) == {(1, 1, 0): 1, (0, 0, 0): -u}
542
+ assert dict(-u + x*y + z) == dict(x*y + z - u) == {(1, 1, 0): 1, (0, 0, 1): 1, (0, 0, 0): -u}
543
+
544
+ assert dict(-u*x + x) == dict(x - u*x) == {(1, 0, 0): -u + 1}
545
+ assert dict(-u*x + x*y) == dict(x*y - u*x) == {(1, 1, 0): 1, (1, 0, 0): -u}
546
+ assert dict(-u*x + x*y + z) == dict(x*y + z - u*x) == {(1, 1, 0): 1, (0, 0, 1): 1, (1, 0, 0): -u}
547
+
548
+ raises(TypeError, lambda: t - x)
549
+ raises(TypeError, lambda: x - t)
550
+ raises(TypeError, lambda: t - u)
551
+ raises(TypeError, lambda: u - t)
552
+
553
+ Fuv, u,v = field("u,v", ZZ)
554
+ Rxyz, x,y,z = ring("x,y,z", Fuv)
555
+
556
+ assert dict(-u + x) == dict(x - u) == {(1, 0, 0): 1, (0, 0, 0): -u}
557
+
558
+ Rxyz, x,y,z = ring("x,y,z", EX)
559
+
560
+ assert dict(-EX(pi) + x*y*z) == dict(x*y*z - EX(pi)) == {(1, 1, 1): EX(1), (0, 0, 0): -EX(pi)}
561
+
562
+ def test_PolyElement___mul__():
563
+ Rt, t = ring("t", ZZ)
564
+ Ruv, u,v = ring("u,v", ZZ)
565
+ Rxyz, x,y,z = ring("x,y,z", Ruv)
566
+
567
+ assert dict(u*x) == dict(x*u) == {(1, 0, 0): u}
568
+
569
+ assert dict(2*u*x + z) == dict(x*2*u + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}
570
+ assert dict(u*2*x + z) == dict(2*x*u + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}
571
+ assert dict(2*u*x + z) == dict(x*2*u + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}
572
+ assert dict(u*x*2 + z) == dict(x*u*2 + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}
573
+
574
+ assert dict(2*u*x*y + z) == dict(x*y*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
575
+ assert dict(u*2*x*y + z) == dict(2*x*y*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
576
+ assert dict(2*u*x*y + z) == dict(x*y*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
577
+ assert dict(u*x*y*2 + z) == dict(x*y*u*2 + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
578
+
579
+ assert dict(2*u*y*x + z) == dict(y*x*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
580
+ assert dict(u*2*y*x + z) == dict(2*y*x*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
581
+ assert dict(2*u*y*x + z) == dict(y*x*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
582
+ assert dict(u*y*x*2 + z) == dict(y*x*u*2 + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
583
+
584
+ assert dict(3*u*(x + y) + z) == dict((x + y)*3*u + z) == {(1, 0, 0): 3*u, (0, 1, 0): 3*u, (0, 0, 1): 1}
585
+
586
+ raises(TypeError, lambda: t*x + z)
587
+ raises(TypeError, lambda: x*t + z)
588
+ raises(TypeError, lambda: t*u + z)
589
+ raises(TypeError, lambda: u*t + z)
590
+
591
+ Fuv, u,v = field("u,v", ZZ)
592
+ Rxyz, x,y,z = ring("x,y,z", Fuv)
593
+
594
+ assert dict(u*x) == dict(x*u) == {(1, 0, 0): u}
595
+
596
+ Rxyz, x,y,z = ring("x,y,z", EX)
597
+
598
+ assert dict(EX(pi)*x*y*z) == dict(x*y*z*EX(pi)) == {(1, 1, 1): EX(pi)}
599
+
600
+ def test_PolyElement___truediv__():
601
+ R, x,y,z = ring("x,y,z", ZZ)
602
+
603
+ assert (2*x**2 - 4)/2 == x**2 - 2
604
+ assert (2*x**2 - 3)/2 == x**2
605
+
606
+ assert (x**2 - 1).quo(x) == x
607
+ assert (x**2 - x).quo(x) == x - 1
608
+
609
+ raises(ExactQuotientFailed, lambda: (x**2 - 1)/x)
610
+ assert (x**2 - x)/x == x - 1
611
+ raises(ExactQuotientFailed, lambda: (x**2 - 1)/(2*x))
612
+
613
+ assert (x**2 - 1).quo(2*x) == 0
614
+ assert (x**2 - x)/(x - 1) == (x**2 - x).quo(x - 1) == x
615
+
616
+
617
+ R, x,y,z = ring("x,y,z", ZZ)
618
+ assert len((x**2/3 + y**3/4 + z**4/5).terms()) == 0
619
+
620
+ R, x,y,z = ring("x,y,z", QQ)
621
+ assert len((x**2/3 + y**3/4 + z**4/5).terms()) == 3
622
+
623
+ Rt, t = ring("t", ZZ)
624
+ Ruv, u,v = ring("u,v", ZZ)
625
+ Rxyz, x,y,z = ring("x,y,z", Ruv)
626
+
627
+ assert dict((u**2*x + u)/u) == {(1, 0, 0): u, (0, 0, 0): 1}
628
+ raises(ExactQuotientFailed, lambda: u/(u**2*x + u))
629
+
630
+ raises(TypeError, lambda: t/x)
631
+ raises(TypeError, lambda: x/t)
632
+ raises(TypeError, lambda: t/u)
633
+ raises(TypeError, lambda: u/t)
634
+
635
+ R, x = ring("x", ZZ)
636
+ f, g = x**2 + 2*x + 3, R(0)
637
+
638
+ raises(ZeroDivisionError, lambda: f.div(g))
639
+ raises(ZeroDivisionError, lambda: divmod(f, g))
640
+ raises(ZeroDivisionError, lambda: f.rem(g))
641
+ raises(ZeroDivisionError, lambda: f % g)
642
+ raises(ZeroDivisionError, lambda: f.quo(g))
643
+ raises(ZeroDivisionError, lambda: f / g)
644
+ raises(ZeroDivisionError, lambda: f.exquo(g))
645
+
646
+ R, x, y = ring("x,y", ZZ)
647
+ f, g = x*y + 2*x + 3, R(0)
648
+
649
+ raises(ZeroDivisionError, lambda: f.div(g))
650
+ raises(ZeroDivisionError, lambda: divmod(f, g))
651
+ raises(ZeroDivisionError, lambda: f.rem(g))
652
+ raises(ZeroDivisionError, lambda: f % g)
653
+ raises(ZeroDivisionError, lambda: f.quo(g))
654
+ raises(ZeroDivisionError, lambda: f / g)
655
+ raises(ZeroDivisionError, lambda: f.exquo(g))
656
+
657
+ R, x = ring("x", ZZ)
658
+
659
+ f, g = x**2 + 1, 2*x - 4
660
+ q, r = R(0), x**2 + 1
661
+
662
+ assert f.div(g) == divmod(f, g) == (q, r)
663
+ assert f.rem(g) == f % g == r
664
+ assert f.quo(g) == q
665
+ raises(ExactQuotientFailed, lambda: f / g)
666
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
667
+
668
+ f, g = 3*x**3 + x**2 + x + 5, 5*x**2 - 3*x + 1
669
+ q, r = R(0), f
670
+
671
+ assert f.div(g) == divmod(f, g) == (q, r)
672
+ assert f.rem(g) == f % g == r
673
+ assert f.quo(g) == q
674
+ raises(ExactQuotientFailed, lambda: f / g)
675
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
676
+
677
+ f, g = 5*x**4 + 4*x**3 + 3*x**2 + 2*x + 1, x**2 + 2*x + 3
678
+ q, r = 5*x**2 - 6*x, 20*x + 1
679
+
680
+ assert f.div(g) == divmod(f, g) == (q, r)
681
+ assert f.rem(g) == f % g == r
682
+ assert f.quo(g) == q
683
+ raises(ExactQuotientFailed, lambda: f / g)
684
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
685
+
686
+ f, g = 5*x**5 + 4*x**4 + 3*x**3 + 2*x**2 + x, x**4 + 2*x**3 + 9
687
+ q, r = 5*x - 6, 15*x**3 + 2*x**2 - 44*x + 54
688
+
689
+ assert f.div(g) == divmod(f, g) == (q, r)
690
+ assert f.rem(g) == f % g == r
691
+ assert f.quo(g) == q
692
+ raises(ExactQuotientFailed, lambda: f / g)
693
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
694
+
695
+ R, x = ring("x", QQ)
696
+
697
+ f, g = x**2 + 1, 2*x - 4
698
+ q, r = x/2 + 1, R(5)
699
+
700
+ assert f.div(g) == divmod(f, g) == (q, r)
701
+ assert f.rem(g) == f % g == r
702
+ assert f.quo(g) == q
703
+ raises(ExactQuotientFailed, lambda: f / g)
704
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
705
+
706
+ f, g = 3*x**3 + x**2 + x + 5, 5*x**2 - 3*x + 1
707
+ q, r = QQ(3, 5)*x + QQ(14, 25), QQ(52, 25)*x + QQ(111, 25)
708
+
709
+ assert f.div(g) == divmod(f, g) == (q, r)
710
+ assert f.rem(g) == f % g == r
711
+ assert f.quo(g) == q
712
+ raises(ExactQuotientFailed, lambda: f / g)
713
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
714
+
715
+ R, x,y = ring("x,y", ZZ)
716
+
717
+ f, g = x**2 - y**2, x - y
718
+ q, r = x + y, R(0)
719
+
720
+ assert f.div(g) == divmod(f, g) == (q, r)
721
+ assert f.rem(g) == f % g == r
722
+ assert f.quo(g) == q
723
+ assert f.exquo(g) == f / g == q
724
+
725
+ f, g = x**2 + y**2, x - y
726
+ q, r = x + y, 2*y**2
727
+
728
+ assert f.div(g) == divmod(f, g) == (q, r)
729
+ assert f.rem(g) == f % g == r
730
+ assert f.quo(g) == q
731
+ raises(ExactQuotientFailed, lambda: f / g)
732
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
733
+
734
+ f, g = x**2 + y**2, -x + y
735
+ q, r = -x - y, 2*y**2
736
+
737
+ assert f.div(g) == divmod(f, g) == (q, r)
738
+ assert f.rem(g) == f % g == r
739
+ assert f.quo(g) == q
740
+ raises(ExactQuotientFailed, lambda: f / g)
741
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
742
+
743
+ f, g = x**2 + y**2, 2*x - 2*y
744
+ q, r = R(0), f
745
+
746
+ assert f.div(g) == divmod(f, g) == (q, r)
747
+ assert f.rem(g) == f % g == r
748
+ assert f.quo(g) == q
749
+ raises(ExactQuotientFailed, lambda: f / g)
750
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
751
+
752
+ R, x,y = ring("x,y", QQ)
753
+
754
+ f, g = x**2 - y**2, x - y
755
+ q, r = x + y, R(0)
756
+
757
+ assert f.div(g) == divmod(f, g) == (q, r)
758
+ assert f.rem(g) == f % g == r
759
+ assert f.quo(g) == q
760
+ assert f.exquo(g) == f / g == q
761
+
762
+ f, g = x**2 + y**2, x - y
763
+ q, r = x + y, 2*y**2
764
+
765
+ assert f.div(g) == divmod(f, g) == (q, r)
766
+ assert f.rem(g) == f % g == r
767
+ assert f.quo(g) == q
768
+ raises(ExactQuotientFailed, lambda: f / g)
769
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
770
+
771
+ f, g = x**2 + y**2, -x + y
772
+ q, r = -x - y, 2*y**2
773
+
774
+ assert f.div(g) == divmod(f, g) == (q, r)
775
+ assert f.rem(g) == f % g == r
776
+ assert f.quo(g) == q
777
+ raises(ExactQuotientFailed, lambda: f / g)
778
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
779
+
780
+ f, g = x**2 + y**2, 2*x - 2*y
781
+ q, r = x/2 + y/2, 2*y**2
782
+
783
+ assert f.div(g) == divmod(f, g) == (q, r)
784
+ assert f.rem(g) == f % g == r
785
+ assert f.quo(g) == q
786
+ raises(ExactQuotientFailed, lambda: f / g)
787
+ raises(ExactQuotientFailed, lambda: f.exquo(g))
788
+
789
+ def test_PolyElement___pow__():
790
+ R, x = ring("x", ZZ, grlex)
791
+ f = 2*x + 3
792
+
793
+ assert f**0 == 1
794
+ assert f**1 == f
795
+ raises(ValueError, lambda: f**(-1))
796
+
797
+ assert f**2 == f._pow_generic(2) == f._pow_multinomial(2) == 4*x**2 + 12*x + 9
798
+ assert f**3 == f._pow_generic(3) == f._pow_multinomial(3) == 8*x**3 + 36*x**2 + 54*x + 27
799
+ assert f**4 == f._pow_generic(4) == f._pow_multinomial(4) == 16*x**4 + 96*x**3 + 216*x**2 + 216*x + 81
800
+ assert f**5 == f._pow_generic(5) == f._pow_multinomial(5) == 32*x**5 + 240*x**4 + 720*x**3 + 1080*x**2 + 810*x + 243
801
+
802
+ R, x,y,z = ring("x,y,z", ZZ, grlex)
803
+ f = x**3*y - 2*x*y**2 - 3*z + 1
804
+ g = x**6*y**2 - 4*x**4*y**3 - 6*x**3*y*z + 2*x**3*y + 4*x**2*y**4 + 12*x*y**2*z - 4*x*y**2 + 9*z**2 - 6*z + 1
805
+
806
+ assert f**2 == f._pow_generic(2) == f._pow_multinomial(2) == g
807
+
808
+ R, t = ring("t", ZZ)
809
+ f = -11200*t**4 - 2604*t**2 + 49
810
+ g = 15735193600000000*t**16 + 14633730048000000*t**14 + 4828147466240000*t**12 \
811
+ + 598976863027200*t**10 + 3130812416256*t**8 - 2620523775744*t**6 \
812
+ + 92413760096*t**4 - 1225431984*t**2 + 5764801
813
+
814
+ assert f**4 == f._pow_generic(4) == f._pow_multinomial(4) == g
815
+
816
+ def test_PolyElement_div():
817
+ R, x = ring("x", ZZ, grlex)
818
+
819
+ f = x**3 - 12*x**2 - 42
820
+ g = x - 3
821
+
822
+ q = x**2 - 9*x - 27
823
+ r = -123
824
+
825
+ assert f.div([g]) == ([q], r)
826
+
827
+ R, x = ring("x", ZZ, grlex)
828
+ f = x**2 + 2*x + 2
829
+ assert f.div([R(1)]) == ([f], 0)
830
+
831
+ R, x = ring("x", QQ, grlex)
832
+ f = x**2 + 2*x + 2
833
+ assert f.div([R(2)]) == ([QQ(1,2)*x**2 + x + 1], 0)
834
+
835
+ R, x,y = ring("x,y", ZZ, grlex)
836
+ f = 4*x**2*y - 2*x*y + 4*x - 2*y + 8
837
+
838
+ assert f.div([R(2)]) == ([2*x**2*y - x*y + 2*x - y + 4], 0)
839
+ assert f.div([2*y]) == ([2*x**2 - x - 1], 4*x + 8)
840
+
841
+ f = x - 1
842
+ g = y - 1
843
+
844
+ assert f.div([g]) == ([0], f)
845
+
846
+ f = x*y**2 + 1
847
+ G = [x*y + 1, y + 1]
848
+
849
+ Q = [y, -1]
850
+ r = 2
851
+
852
+ assert f.div(G) == (Q, r)
853
+
854
+ f = x**2*y + x*y**2 + y**2
855
+ G = [x*y - 1, y**2 - 1]
856
+
857
+ Q = [x + y, 1]
858
+ r = x + y + 1
859
+
860
+ assert f.div(G) == (Q, r)
861
+
862
+ G = [y**2 - 1, x*y - 1]
863
+
864
+ Q = [x + 1, x]
865
+ r = 2*x + 1
866
+
867
+ assert f.div(G) == (Q, r)
868
+
869
+ R, = ring("", ZZ)
870
+ assert R(3).div(R(2)) == (0, 3)
871
+
872
+ R, = ring("", QQ)
873
+ assert R(3).div(R(2)) == (QQ(3, 2), 0)
874
+
875
+ def test_PolyElement_rem():
876
+ R, x = ring("x", ZZ, grlex)
877
+
878
+ f = x**3 - 12*x**2 - 42
879
+ g = x - 3
880
+ r = -123
881
+
882
+ assert f.rem([g]) == f.div([g])[1] == r
883
+
884
+ R, x,y = ring("x,y", ZZ, grlex)
885
+
886
+ f = 4*x**2*y - 2*x*y + 4*x - 2*y + 8
887
+
888
+ assert f.rem([R(2)]) == f.div([R(2)])[1] == 0
889
+ assert f.rem([2*y]) == f.div([2*y])[1] == 4*x + 8
890
+
891
+ f = x - 1
892
+ g = y - 1
893
+
894
+ assert f.rem([g]) == f.div([g])[1] == f
895
+
896
+ f = x*y**2 + 1
897
+ G = [x*y + 1, y + 1]
898
+ r = 2
899
+
900
+ assert f.rem(G) == f.div(G)[1] == r
901
+
902
+ f = x**2*y + x*y**2 + y**2
903
+ G = [x*y - 1, y**2 - 1]
904
+ r = x + y + 1
905
+
906
+ assert f.rem(G) == f.div(G)[1] == r
907
+
908
+ G = [y**2 - 1, x*y - 1]
909
+ r = 2*x + 1
910
+
911
+ assert f.rem(G) == f.div(G)[1] == r
912
+
913
+ def test_PolyElement_deflate():
914
+ R, x = ring("x", ZZ)
915
+
916
+ assert (2*x**2).deflate(x**4 + 4*x**2 + 1) == ((2,), [2*x, x**2 + 4*x + 1])
917
+
918
+ R, x,y = ring("x,y", ZZ)
919
+
920
+ assert R(0).deflate(R(0)) == ((1, 1), [0, 0])
921
+ assert R(1).deflate(R(0)) == ((1, 1), [1, 0])
922
+ assert R(1).deflate(R(2)) == ((1, 1), [1, 2])
923
+ assert R(1).deflate(2*y) == ((1, 1), [1, 2*y])
924
+ assert (2*y).deflate(2*y) == ((1, 1), [2*y, 2*y])
925
+ assert R(2).deflate(2*y**2) == ((1, 2), [2, 2*y])
926
+ assert (2*y**2).deflate(2*y**2) == ((1, 2), [2*y, 2*y])
927
+
928
+ f = x**4*y**2 + x**2*y + 1
929
+ g = x**2*y**3 + x**2*y + 1
930
+
931
+ assert f.deflate(g) == ((2, 1), [x**2*y**2 + x*y + 1, x*y**3 + x*y + 1])
932
+
933
+ def test_PolyElement_clear_denoms():
934
+ R, x,y = ring("x,y", QQ)
935
+
936
+ assert R(1).clear_denoms() == (ZZ(1), 1)
937
+ assert R(7).clear_denoms() == (ZZ(1), 7)
938
+
939
+ assert R(QQ(7,3)).clear_denoms() == (3, 7)
940
+ assert R(QQ(7,3)).clear_denoms() == (3, 7)
941
+
942
+ assert (3*x**2 + x).clear_denoms() == (1, 3*x**2 + x)
943
+ assert (x**2 + QQ(1,2)*x).clear_denoms() == (2, 2*x**2 + x)
944
+
945
+ rQQ, x,t = ring("x,t", QQ, lex)
946
+ rZZ, X,T = ring("x,t", ZZ, lex)
947
+
948
+ F = [x - QQ(17824537287975195925064602467992950991718052713078834557692023531499318507213727406844943097,413954288007559433755329699713866804710749652268151059918115348815925474842910720000)*t**7
949
+ - QQ(4882321164854282623427463828745855894130208215961904469205260756604820743234704900167747753,12936071500236232304854053116058337647210926633379720622441104650497671088840960000)*t**6
950
+ - QQ(36398103304520066098365558157422127347455927422509913596393052633155821154626830576085097433,25872143000472464609708106232116675294421853266759441244882209300995342177681920000)*t**5
951
+ - QQ(168108082231614049052707339295479262031324376786405372698857619250210703675982492356828810819,58212321751063045371843239022262519412449169850208742800984970927239519899784320000)*t**4
952
+ - QQ(5694176899498574510667890423110567593477487855183144378347226247962949388653159751849449037,1617008937529529038106756639507292205901365829172465077805138081312208886105120000)*t**3
953
+ - QQ(154482622347268833757819824809033388503591365487934245386958884099214649755244381307907779,60637835157357338929003373981523457721301218593967440417692678049207833228942000)*t**2
954
+ - QQ(2452813096069528207645703151222478123259511586701148682951852876484544822947007791153163,2425513406294293557160134959260938308852048743758697616707707121968313329157680)*t
955
+ - QQ(34305265428126440542854669008203683099323146152358231964773310260498715579162112959703,202126117191191129763344579938411525737670728646558134725642260164026110763140),
956
+ t**8 + QQ(693749860237914515552,67859264524169150569)*t**7
957
+ + QQ(27761407182086143225024,610733380717522355121)*t**6
958
+ + QQ(7785127652157884044288,67859264524169150569)*t**5
959
+ + QQ(36567075214771261409792,203577793572507451707)*t**4
960
+ + QQ(36336335165196147384320,203577793572507451707)*t**3
961
+ + QQ(7452455676042754048000,67859264524169150569)*t**2
962
+ + QQ(2593331082514399232000,67859264524169150569)*t
963
+ + QQ(390399197427343360000,67859264524169150569)]
964
+
965
+ G = [3725588592068034903797967297424801242396746870413359539263038139343329273586196480000*X -
966
+ 160420835591776763325581422211936558925462474417709511019228211783493866564923546661604487873*T**7 -
967
+ 1406108495478033395547109582678806497509499966197028487131115097902188374051595011248311352864*T**6 -
968
+ 5241326875850889518164640374668786338033653548841427557880599579174438246266263602956254030352*T**5 -
969
+ 10758917262823299139373269714910672770004760114329943852726887632013485035262879510837043892416*T**4 -
970
+ 13119383576444715672578819534846747735372132018341964647712009275306635391456880068261130581248*T**3 -
971
+ 9491412317016197146080450036267011389660653495578680036574753839055748080962214787557853941760*T**2 -
972
+ 3767520915562795326943800040277726397326609797172964377014046018280260848046603967211258368000*T -
973
+ 632314652371226552085897259159210286886724229880266931574701654721512325555116066073245696000,
974
+ 610733380717522355121*T**8 +
975
+ 6243748742141230639968*T**7 +
976
+ 27761407182086143225024*T**6 +
977
+ 70066148869420956398592*T**5 +
978
+ 109701225644313784229376*T**4 +
979
+ 109009005495588442152960*T**3 +
980
+ 67072101084384786432000*T**2 +
981
+ 23339979742629593088000*T +
982
+ 3513592776846090240000]
983
+
984
+ assert [ f.clear_denoms()[1].set_ring(rZZ) for f in F ] == G
985
+
986
+ def test_PolyElement_cofactors():
987
+ R, x, y = ring("x,y", ZZ)
988
+
989
+ f, g = R(0), R(0)
990
+ assert f.cofactors(g) == (0, 0, 0)
991
+
992
+ f, g = R(2), R(0)
993
+ assert f.cofactors(g) == (2, 1, 0)
994
+
995
+ f, g = R(-2), R(0)
996
+ assert f.cofactors(g) == (2, -1, 0)
997
+
998
+ f, g = R(0), R(-2)
999
+ assert f.cofactors(g) == (2, 0, -1)
1000
+
1001
+ f, g = R(0), 2*x + 4
1002
+ assert f.cofactors(g) == (2*x + 4, 0, 1)
1003
+
1004
+ f, g = 2*x + 4, R(0)
1005
+ assert f.cofactors(g) == (2*x + 4, 1, 0)
1006
+
1007
+ f, g = R(2), R(2)
1008
+ assert f.cofactors(g) == (2, 1, 1)
1009
+
1010
+ f, g = R(-2), R(2)
1011
+ assert f.cofactors(g) == (2, -1, 1)
1012
+
1013
+ f, g = R(2), R(-2)
1014
+ assert f.cofactors(g) == (2, 1, -1)
1015
+
1016
+ f, g = R(-2), R(-2)
1017
+ assert f.cofactors(g) == (2, -1, -1)
1018
+
1019
+ f, g = x**2 + 2*x + 1, R(1)
1020
+ assert f.cofactors(g) == (1, x**2 + 2*x + 1, 1)
1021
+
1022
+ f, g = x**2 + 2*x + 1, R(2)
1023
+ assert f.cofactors(g) == (1, x**2 + 2*x + 1, 2)
1024
+
1025
+ f, g = 2*x**2 + 4*x + 2, R(2)
1026
+ assert f.cofactors(g) == (2, x**2 + 2*x + 1, 1)
1027
+
1028
+ f, g = R(2), 2*x**2 + 4*x + 2
1029
+ assert f.cofactors(g) == (2, 1, x**2 + 2*x + 1)
1030
+
1031
+ f, g = 2*x**2 + 4*x + 2, x + 1
1032
+ assert f.cofactors(g) == (x + 1, 2*x + 2, 1)
1033
+
1034
+ f, g = x + 1, 2*x**2 + 4*x + 2
1035
+ assert f.cofactors(g) == (x + 1, 1, 2*x + 2)
1036
+
1037
+ R, x, y, z, t = ring("x,y,z,t", ZZ)
1038
+
1039
+ f, g = t**2 + 2*t + 1, 2*t + 2
1040
+ assert f.cofactors(g) == (t + 1, t + 1, 2)
1041
+
1042
+ f, g = z**2*t**2 + 2*z**2*t + z**2 + z*t + z, t**2 + 2*t + 1
1043
+ h, cff, cfg = t + 1, z**2*t + z**2 + z, t + 1
1044
+
1045
+ assert f.cofactors(g) == (h, cff, cfg)
1046
+ assert g.cofactors(f) == (h, cfg, cff)
1047
+
1048
+ R, x, y = ring("x,y", QQ)
1049
+
1050
+ f = QQ(1,2)*x**2 + x + QQ(1,2)
1051
+ g = QQ(1,2)*x + QQ(1,2)
1052
+
1053
+ h = x + 1
1054
+
1055
+ assert f.cofactors(g) == (h, g, QQ(1,2))
1056
+ assert g.cofactors(f) == (h, QQ(1,2), g)
1057
+
1058
+ R, x, y = ring("x,y", RR)
1059
+
1060
+ f = 2.1*x*y**2 - 2.1*x*y + 2.1*x
1061
+ g = 2.1*x**3
1062
+ h = 1.0*x
1063
+
1064
+ assert f.cofactors(g) == (h, f/h, g/h)
1065
+ assert g.cofactors(f) == (h, g/h, f/h)
1066
+
1067
+ def test_PolyElement_gcd():
1068
+ R, x, y = ring("x,y", QQ)
1069
+
1070
+ f = QQ(1,2)*x**2 + x + QQ(1,2)
1071
+ g = QQ(1,2)*x + QQ(1,2)
1072
+
1073
+ assert f.gcd(g) == x + 1
1074
+
1075
+ def test_PolyElement_cancel():
1076
+ R, x, y = ring("x,y", ZZ)
1077
+
1078
+ f = 2*x**3 + 4*x**2 + 2*x
1079
+ g = 3*x**2 + 3*x
1080
+ F = 2*x + 2
1081
+ G = 3
1082
+
1083
+ assert f.cancel(g) == (F, G)
1084
+
1085
+ assert (-f).cancel(g) == (-F, G)
1086
+ assert f.cancel(-g) == (-F, G)
1087
+
1088
+ R, x, y = ring("x,y", QQ)
1089
+
1090
+ f = QQ(1,2)*x**3 + x**2 + QQ(1,2)*x
1091
+ g = QQ(1,3)*x**2 + QQ(1,3)*x
1092
+ F = 3*x + 3
1093
+ G = 2
1094
+
1095
+ assert f.cancel(g) == (F, G)
1096
+
1097
+ assert (-f).cancel(g) == (-F, G)
1098
+ assert f.cancel(-g) == (-F, G)
1099
+
1100
+ Fx, x = field("x", ZZ)
1101
+ Rt, t = ring("t", Fx)
1102
+
1103
+ f = (-x**2 - 4)/4*t
1104
+ g = t**2 + (x**2 + 2)/2
1105
+
1106
+ assert f.cancel(g) == ((-x**2 - 4)*t, 4*t**2 + 2*x**2 + 4)
1107
+
1108
+ def test_PolyElement_max_norm():
1109
+ R, x, y = ring("x,y", ZZ)
1110
+
1111
+ assert R(0).max_norm() == 0
1112
+ assert R(1).max_norm() == 1
1113
+
1114
+ assert (x**3 + 4*x**2 + 2*x + 3).max_norm() == 4
1115
+
1116
+ def test_PolyElement_l1_norm():
1117
+ R, x, y = ring("x,y", ZZ)
1118
+
1119
+ assert R(0).l1_norm() == 0
1120
+ assert R(1).l1_norm() == 1
1121
+
1122
+ assert (x**3 + 4*x**2 + 2*x + 3).l1_norm() == 10
1123
+
1124
+ def test_PolyElement_diff():
1125
+ R, X = xring("x:11", QQ)
1126
+
1127
+ f = QQ(288,5)*X[0]**8*X[1]**6*X[4]**3*X[10]**2 + 8*X[0]**2*X[2]**3*X[4]**3 +2*X[0]**2 - 2*X[1]**2
1128
+
1129
+ assert f.diff(X[0]) == QQ(2304,5)*X[0]**7*X[1]**6*X[4]**3*X[10]**2 + 16*X[0]*X[2]**3*X[4]**3 + 4*X[0]
1130
+ assert f.diff(X[4]) == QQ(864,5)*X[0]**8*X[1]**6*X[4]**2*X[10]**2 + 24*X[0]**2*X[2]**3*X[4]**2
1131
+ assert f.diff(X[10]) == QQ(576,5)*X[0]**8*X[1]**6*X[4]**3*X[10]
1132
+
1133
+ def test_PolyElement___call__():
1134
+ R, x = ring("x", ZZ)
1135
+ f = 3*x + 1
1136
+
1137
+ assert f(0) == 1
1138
+ assert f(1) == 4
1139
+
1140
+ raises(ValueError, lambda: f())
1141
+ raises(ValueError, lambda: f(0, 1))
1142
+
1143
+ raises(CoercionFailed, lambda: f(QQ(1,7)))
1144
+
1145
+ R, x,y = ring("x,y", ZZ)
1146
+ f = 3*x + y**2 + 1
1147
+
1148
+ assert f(0, 0) == 1
1149
+ assert f(1, 7) == 53
1150
+
1151
+ Ry = R.drop(x)
1152
+
1153
+ assert f(0) == Ry.y**2 + 1
1154
+ assert f(1) == Ry.y**2 + 4
1155
+
1156
+ raises(ValueError, lambda: f())
1157
+ raises(ValueError, lambda: f(0, 1, 2))
1158
+
1159
+ raises(CoercionFailed, lambda: f(1, QQ(1,7)))
1160
+ raises(CoercionFailed, lambda: f(QQ(1,7), 1))
1161
+ raises(CoercionFailed, lambda: f(QQ(1,7), QQ(1,7)))
1162
+
1163
+ def test_PolyElement_evaluate():
1164
+ R, x = ring("x", ZZ)
1165
+ f = x**3 + 4*x**2 + 2*x + 3
1166
+
1167
+ r = f.evaluate(x, 0)
1168
+ assert r == 3 and not isinstance(r, PolyElement)
1169
+
1170
+ raises(CoercionFailed, lambda: f.evaluate(x, QQ(1,7)))
1171
+
1172
+ R, x, y, z = ring("x,y,z", ZZ)
1173
+ f = (x*y)**3 + 4*(x*y)**2 + 2*x*y + 3
1174
+
1175
+ r = f.evaluate(x, 0)
1176
+ assert r == 3 and R.drop(x).is_element(r)
1177
+ r = f.evaluate([(x, 0), (y, 0)])
1178
+ assert r == 3 and R.drop(x, y).is_element(r)
1179
+ r = f.evaluate(y, 0)
1180
+ assert r == 3 and R.drop(y).is_element(r)
1181
+ r = f.evaluate([(y, 0), (x, 0)])
1182
+ assert r == 3 and R.drop(y, x).is_element(r)
1183
+
1184
+ r = f.evaluate([(x, 0), (y, 0), (z, 0)])
1185
+ assert r == 3 and not isinstance(r, PolyElement)
1186
+
1187
+ raises(CoercionFailed, lambda: f.evaluate([(x, 1), (y, QQ(1,7))]))
1188
+ raises(CoercionFailed, lambda: f.evaluate([(x, QQ(1,7)), (y, 1)]))
1189
+ raises(CoercionFailed, lambda: f.evaluate([(x, QQ(1,7)), (y, QQ(1,7))]))
1190
+
1191
+ def test_PolyElement_subs():
1192
+ R, x = ring("x", ZZ)
1193
+ f = x**3 + 4*x**2 + 2*x + 3
1194
+
1195
+ r = f.subs(x, 0)
1196
+ assert r == 3 and R.is_element(r)
1197
+
1198
+ raises(CoercionFailed, lambda: f.subs(x, QQ(1,7)))
1199
+
1200
+ R, x, y, z = ring("x,y,z", ZZ)
1201
+ f = x**3 + 4*x**2 + 2*x + 3
1202
+
1203
+ r = f.subs(x, 0)
1204
+ assert r == 3 and R.is_element(r)
1205
+ r = f.subs([(x, 0), (y, 0)])
1206
+ assert r == 3 and R.is_element(r)
1207
+
1208
+ raises(CoercionFailed, lambda: f.subs([(x, 1), (y, QQ(1,7))]))
1209
+ raises(CoercionFailed, lambda: f.subs([(x, QQ(1,7)), (y, 1)]))
1210
+ raises(CoercionFailed, lambda: f.subs([(x, QQ(1,7)), (y, QQ(1,7))]))
1211
+
1212
+ def test_PolyElement_symmetrize():
1213
+ R, x, y = ring("x,y", ZZ)
1214
+
1215
+ # Homogeneous, symmetric
1216
+ f = x**2 + y**2
1217
+ sym, rem, m = f.symmetrize()
1218
+ assert rem == 0
1219
+ assert sym.compose(m) + rem == f
1220
+
1221
+ # Homogeneous, asymmetric
1222
+ f = x**2 - y**2
1223
+ sym, rem, m = f.symmetrize()
1224
+ assert rem != 0
1225
+ assert sym.compose(m) + rem == f
1226
+
1227
+ # Inhomogeneous, symmetric
1228
+ f = x*y + 7
1229
+ sym, rem, m = f.symmetrize()
1230
+ assert rem == 0
1231
+ assert sym.compose(m) + rem == f
1232
+
1233
+ # Inhomogeneous, asymmetric
1234
+ f = y + 7
1235
+ sym, rem, m = f.symmetrize()
1236
+ assert rem != 0
1237
+ assert sym.compose(m) + rem == f
1238
+
1239
+ # Constant
1240
+ f = R.from_expr(3)
1241
+ sym, rem, m = f.symmetrize()
1242
+ assert rem == 0
1243
+ assert sym.compose(m) + rem == f
1244
+
1245
+ # Constant constructed from sring
1246
+ R, f = sring(3)
1247
+ sym, rem, m = f.symmetrize()
1248
+ assert rem == 0
1249
+ assert sym.compose(m) + rem == f
1250
+
1251
+ def test_PolyElement_compose():
1252
+ R, x = ring("x", ZZ)
1253
+ f = x**3 + 4*x**2 + 2*x + 3
1254
+
1255
+ r = f.compose(x, 0)
1256
+ assert r == 3 and R.is_element(r)
1257
+
1258
+ assert f.compose(x, x) == f
1259
+ assert f.compose(x, x**2) == x**6 + 4*x**4 + 2*x**2 + 3
1260
+
1261
+ raises(CoercionFailed, lambda: f.compose(x, QQ(1,7)))
1262
+
1263
+ R, x, y, z = ring("x,y,z", ZZ)
1264
+ f = x**3 + 4*x**2 + 2*x + 3
1265
+
1266
+ r = f.compose(x, 0)
1267
+ assert r == 3 and R.is_element(r)
1268
+ r = f.compose([(x, 0), (y, 0)])
1269
+ assert r == 3 and R.is_element(r)
1270
+
1271
+ r = (x**3 + 4*x**2 + 2*x*y*z + 3).compose(x, y*z**2 - 1)
1272
+ q = (y*z**2 - 1)**3 + 4*(y*z**2 - 1)**2 + 2*(y*z**2 - 1)*y*z + 3
1273
+ assert r == q and R.is_element(r)
1274
+
1275
+ def test_PolyElement_is_():
1276
+ R, x,y,z = ring("x,y,z", QQ)
1277
+
1278
+ assert (x - x).is_generator == False
1279
+ assert (x - x).is_ground == True
1280
+ assert (x - x).is_monomial == True
1281
+ assert (x - x).is_term == True
1282
+
1283
+ assert (x - x + 1).is_generator == False
1284
+ assert (x - x + 1).is_ground == True
1285
+ assert (x - x + 1).is_monomial == True
1286
+ assert (x - x + 1).is_term == True
1287
+
1288
+ assert x.is_generator == True
1289
+ assert x.is_ground == False
1290
+ assert x.is_monomial == True
1291
+ assert x.is_term == True
1292
+
1293
+ assert (x*y).is_generator == False
1294
+ assert (x*y).is_ground == False
1295
+ assert (x*y).is_monomial == True
1296
+ assert (x*y).is_term == True
1297
+
1298
+ assert (3*x).is_generator == False
1299
+ assert (3*x).is_ground == False
1300
+ assert (3*x).is_monomial == False
1301
+ assert (3*x).is_term == True
1302
+
1303
+ assert (3*x + 1).is_generator == False
1304
+ assert (3*x + 1).is_ground == False
1305
+ assert (3*x + 1).is_monomial == False
1306
+ assert (3*x + 1).is_term == False
1307
+
1308
+ assert R(0).is_zero is True
1309
+ assert R(1).is_zero is False
1310
+
1311
+ assert R(0).is_one is False
1312
+ assert R(1).is_one is True
1313
+
1314
+ assert (x - 1).is_monic is True
1315
+ assert (2*x - 1).is_monic is False
1316
+
1317
+ assert (3*x + 2).is_primitive is True
1318
+ assert (4*x + 2).is_primitive is False
1319
+
1320
+ assert (x + y + z + 1).is_linear is True
1321
+ assert (x*y*z + 1).is_linear is False
1322
+
1323
+ assert (x*y + z + 1).is_quadratic is True
1324
+ assert (x*y*z + 1).is_quadratic is False
1325
+
1326
+ assert (x - 1).is_squarefree is True
1327
+ assert ((x - 1)**2).is_squarefree is False
1328
+
1329
+ assert (x**2 + x + 1).is_irreducible is True
1330
+ assert (x**2 + 2*x + 1).is_irreducible is False
1331
+
1332
+ _, t = ring("t", FF(11))
1333
+
1334
+ assert (7*t + 3).is_irreducible is True
1335
+ assert (7*t**2 + 3*t + 1).is_irreducible is False
1336
+
1337
+ _, u = ring("u", ZZ)
1338
+ f = u**16 + u**14 - u**10 - u**8 - u**6 + u**2
1339
+
1340
+ assert f.is_cyclotomic is False
1341
+ assert (f + 1).is_cyclotomic is True
1342
+
1343
+ raises(MultivariatePolynomialError, lambda: x.is_cyclotomic)
1344
+
1345
+ R, = ring("", ZZ)
1346
+ assert R(4).is_squarefree is True
1347
+ assert R(6).is_irreducible is True
1348
+
1349
+ def test_PolyElement_drop():
1350
+ R, x,y,z = ring("x,y,z", ZZ)
1351
+
1352
+ assert R(1).drop(0).ring == PolyRing("y,z", ZZ, lex)
1353
+ assert R(1).drop(0).drop(0).ring == PolyRing("z", ZZ, lex)
1354
+ assert R.is_element(R(1).drop(0).drop(0).drop(0)) is False
1355
+
1356
+ raises(ValueError, lambda: z.drop(0).drop(0).drop(0))
1357
+ raises(ValueError, lambda: x.drop(0))
1358
+
1359
+ def test_PolyElement_coeff_wrt():
1360
+ R, x, y, z = ring("x, y, z", ZZ)
1361
+
1362
+ p = 4*x**3 + 5*y**2 + 6*y**2*z + 7
1363
+ assert p.coeff_wrt(1, 2) == 6*z + 5 # using generator index
1364
+ assert p.coeff_wrt(x, 3) == 4 # using generator
1365
+
1366
+ p = 2*x**4 + 3*x*y**2*z + 10*y**2 + 10*x*z**2
1367
+ assert p.coeff_wrt(x, 1) == 3*y**2*z + 10*z**2
1368
+ assert p.coeff_wrt(y, 2) == 3*x*z + 10
1369
+
1370
+ p = 4*x**2 + 2*x*y + 5
1371
+ assert p.coeff_wrt(z, 1) == R(0)
1372
+ assert p.coeff_wrt(y, 2) == R(0)
1373
+
1374
+ def test_PolyElement_prem():
1375
+ R, x, y = ring("x, y", ZZ)
1376
+
1377
+ f, g = x**2 + x*y, 2*x + 2
1378
+ assert f.prem(g) == -4*y + 4 # first generator is chosen by default if it is not given
1379
+
1380
+ f, g = x**2 + 1, 2*x - 4
1381
+ assert f.prem(g) == f.prem(g, x) == 20
1382
+ assert f.prem(g, 1) == R(0)
1383
+
1384
+ f, g = x*y + 2*x + 1, x + y
1385
+ assert f.prem(g) == -y**2 - 2*y + 1
1386
+ assert f.prem(g, 1) == f.prem(g, y) == -x**2 + 2*x + 1
1387
+
1388
+ raises(ZeroDivisionError, lambda: f.prem(R(0)))
1389
+
1390
+ def test_PolyElement_pdiv():
1391
+ R, x, y = ring("x,y", ZZ)
1392
+
1393
+ f, g = x**4 + 5*x**3 + 7*x**2, 2*x**2 + 3
1394
+ assert f.pdiv(g) == f.pdiv(g, x) == (4*x**2 + 20*x + 22, -60*x - 66)
1395
+
1396
+ f, g = x**2 - y**2, x - y
1397
+ assert f.pdiv(g) == f.pdiv(g, 0) == (x + y, 0)
1398
+
1399
+ f, g = x*y + 2*x + 1, x + y
1400
+ assert f.pdiv(g) == (y + 2, -y**2 - 2*y + 1)
1401
+ assert f.pdiv(g, y) == f.pdiv(g, 1) == (x + 1, -x**2 + 2*x + 1)
1402
+
1403
+ assert R(0).pdiv(g) == (0, 0)
1404
+ raises(ZeroDivisionError, lambda: f.prem(R(0)))
1405
+
1406
+ def test_PolyElement_pquo():
1407
+ R, x, y = ring("x, y", ZZ)
1408
+
1409
+ f, g = x**4 - 4*x**2*y + 4*y**2, x**2 - 2*y
1410
+ assert f.pquo(g) == f.pquo(g, x) == x**2 - 2*y
1411
+ assert f.pquo(g, y) == 4*x**2 - 8*y + 4
1412
+
1413
+ f, g = x**4 - y**4, x**2 - y**2
1414
+ assert f.pquo(g) == f.pquo(g, 0) == x**2 + y**2
1415
+
1416
+ def test_PolyElement_pexquo():
1417
+ R, x, y = ring("x, y", ZZ)
1418
+
1419
+ f, g = x**2 - y**2, x - y
1420
+ assert f.pexquo(g) == f.pexquo(g, x) == x + y
1421
+ assert f.pexquo(g, y) == f.pexquo(g, 1) == x + y + 1
1422
+
1423
+ f, g = x**2 + 3*x + 6, x + 2
1424
+ raises(ExactQuotientFailed, lambda: f.pexquo(g))
1425
+
1426
+ def test_PolyElement_gcdex():
1427
+ _, x = ring("x", QQ)
1428
+
1429
+ f, g = 2*x, x**2 - 16
1430
+ s, t, h = x/32, -QQ(1, 16), 1
1431
+
1432
+ assert f.half_gcdex(g) == (s, h)
1433
+ assert f.gcdex(g) == (s, t, h)
1434
+
1435
+ def test_PolyElement_subresultants():
1436
+ R, x, y = ring("x, y", ZZ)
1437
+
1438
+ f, g = x**2*y + x*y, x + y # degree(f, x) > degree(g, x)
1439
+ h = y**3 - y**2
1440
+ assert f.subresultants(g) == [f, g, h] # first generator is chosen default
1441
+
1442
+ # generator index or generator is given
1443
+ assert f.subresultants(g, 0) == f.subresultants(g, x) == [f, g, h]
1444
+
1445
+ assert f.subresultants(g, y) == [x**2*y + x*y, x + y, x**3 + x**2]
1446
+
1447
+ f, g = 2*x - y, x**2 + 2*y + x # degree(f, x) < degree(g, x)
1448
+ assert f.subresultants(g) == [x**2 + x + 2*y, 2*x - y, y**2 + 10*y]
1449
+
1450
+ f, g = R(0), y**3 - y**2 # f = 0
1451
+ assert f.subresultants(g) == [y**3 - y**2, 1]
1452
+
1453
+ f, g = x**2*y + x*y, R(0) # g = 0
1454
+ assert f.subresultants(g) == [x**2*y + x*y, 1]
1455
+
1456
+ f, g = R(0), R(0) # f = 0 and g = 0
1457
+ assert f.subresultants(g) == [0, 0]
1458
+
1459
+ f, g = x**2 + x, x**2 + x # f and g are same polynomial
1460
+ assert f.subresultants(g) == [x**2 + x, x**2 + x]
1461
+
1462
+ def test_PolyElement_resultant():
1463
+ _, x = ring("x", ZZ)
1464
+ f, g, h = x**2 - 2*x + 1, x**2 - 1, 0
1465
+
1466
+ assert f.resultant(g) == h
1467
+
1468
+ def test_PolyElement_discriminant():
1469
+ _, x = ring("x", ZZ)
1470
+ f, g = x**3 + 3*x**2 + 9*x - 13, -11664
1471
+
1472
+ assert f.discriminant() == g
1473
+
1474
+ F, a, b, c = ring("a,b,c", ZZ)
1475
+ _, x = ring("x", F)
1476
+
1477
+ f, g = a*x**2 + b*x + c, b**2 - 4*a*c
1478
+
1479
+ assert f.discriminant() == g
1480
+
1481
+ def test_PolyElement_decompose():
1482
+ _, x = ring("x", ZZ)
1483
+
1484
+ f = x**12 + 20*x**10 + 150*x**8 + 500*x**6 + 625*x**4 - 2*x**3 - 10*x + 9
1485
+ g = x**4 - 2*x + 9
1486
+ h = x**3 + 5*x
1487
+
1488
+ assert g.compose(x, h) == f
1489
+ assert f.decompose() == [g, h]
1490
+
1491
+ def test_PolyElement_shift():
1492
+ _, x = ring("x", ZZ)
1493
+ assert (x**2 - 2*x + 1).shift(2) == x**2 + 2*x + 1
1494
+ assert (x**2 - 2*x + 1).shift_list([2]) == x**2 + 2*x + 1
1495
+
1496
+ R, x, y = ring("x, y", ZZ)
1497
+ assert (x*y).shift_list([1, 2]) == (x+1)*(y+2)
1498
+
1499
+ raises(MultivariatePolynomialError, lambda: (x*y).shift(1))
1500
+
1501
+ def test_PolyElement_sturm():
1502
+ F, t = field("t", ZZ)
1503
+ _, x = ring("x", F)
1504
+
1505
+ f = 1024/(15625*t**8)*x**5 - 4096/(625*t**8)*x**4 + 32/(15625*t**4)*x**3 - 128/(625*t**4)*x**2 + F(1)/62500*x - F(1)/625
1506
+
1507
+ assert f.sturm() == [
1508
+ x**3 - 100*x**2 + t**4/64*x - 25*t**4/16,
1509
+ 3*x**2 - 200*x + t**4/64,
1510
+ (-t**4/96 + F(20000)/9)*x + 25*t**4/18,
1511
+ (-9*t**12 - 11520000*t**8 - 3686400000000*t**4)/(576*t**8 - 245760000*t**4 + 26214400000000),
1512
+ ]
1513
+
1514
+ def test_PolyElement_gff_list():
1515
+ _, x = ring("x", ZZ)
1516
+
1517
+ f = x**5 + 2*x**4 - x**3 - 2*x**2
1518
+ assert f.gff_list() == [(x, 1), (x + 2, 4)]
1519
+
1520
+ f = x*(x - 1)**3*(x - 2)**2*(x - 4)**2*(x - 5)
1521
+ assert f.gff_list() == [(x**2 - 5*x + 4, 1), (x**2 - 5*x + 4, 2), (x, 3)]
1522
+
1523
+ def test_PolyElement_norm():
1524
+ k = QQ
1525
+ K = QQ.algebraic_field(sqrt(2))
1526
+ sqrt2 = K.unit
1527
+ _, X, Y = ring("x,y", k)
1528
+ _, x, y = ring("x,y", K)
1529
+
1530
+ assert (x*y + sqrt2).norm() == X**2*Y**2 - 2
1531
+
1532
+ def test_PolyElement_sqf_norm():
1533
+ R, x = ring("x", QQ.algebraic_field(sqrt(3)))
1534
+ X = R.to_ground().x
1535
+
1536
+ assert (x**2 - 2).sqf_norm() == ([1], x**2 - 2*sqrt(3)*x + 1, X**4 - 10*X**2 + 1)
1537
+
1538
+ R, x = ring("x", QQ.algebraic_field(sqrt(2)))
1539
+ X = R.to_ground().x
1540
+
1541
+ assert (x**2 - 3).sqf_norm() == ([1], x**2 - 2*sqrt(2)*x - 1, X**4 - 10*X**2 + 1)
1542
+
1543
+ def test_PolyElement_sqf_list():
1544
+ _, x = ring("x", ZZ)
1545
+
1546
+ f = x**5 - x**3 - x**2 + 1
1547
+ g = x**3 + 2*x**2 + 2*x + 1
1548
+ h = x - 1
1549
+ p = x**4 + x**3 - x - 1
1550
+
1551
+ assert f.sqf_part() == p
1552
+ assert f.sqf_list() == (1, [(g, 1), (h, 2)])
1553
+
1554
+ def test_issue_18894():
1555
+ items = [S(3)/16 + sqrt(3*sqrt(3) + 10)/8, S(1)/8 + 3*sqrt(3)/16, S(1)/8 + 3*sqrt(3)/16, -S(3)/16 + sqrt(3*sqrt(3) + 10)/8]
1556
+ R, a = sring(items, extension=True)
1557
+ assert R.domain == QQ.algebraic_field(sqrt(3)+sqrt(3*sqrt(3)+10))
1558
+ assert R.gens == ()
1559
+ result = []
1560
+ for item in items:
1561
+ result.append(R.domain.from_sympy(item))
1562
+ assert a == result
1563
+
1564
+ def test_PolyElement_factor_list():
1565
+ _, x = ring("x", ZZ)
1566
+
1567
+ f = x**5 - x**3 - x**2 + 1
1568
+
1569
+ u = x + 1
1570
+ v = x - 1
1571
+ w = x**2 + x + 1
1572
+
1573
+ assert f.factor_list() == (1, [(u, 1), (v, 2), (w, 1)])
1574
+
1575
+
1576
+ def test_issue_21410():
1577
+ R, x = ring('x', FF(2))
1578
+ p = x**6 + x**5 + x**4 + x**3 + 1
1579
+ assert p._pow_multinomial(4) == x**24 + x**20 + x**16 + x**12 + 1
1580
+
1581
+
1582
+ def test_zero_polynomial_primitive():
1583
+
1584
+ x = symbols('x')
1585
+
1586
+ R = ZZ[x]
1587
+ zero_poly = R(0)
1588
+ cont, prim = zero_poly.primitive()
1589
+ assert cont == 0
1590
+ assert prim == zero_poly
1591
+ assert prim.is_primitive is False