Skip to content

Commit 95adbfc

Browse files
author
Release Manager
committed
gh-40170: generic implementation of _element_of_factored_order in finite-fields… Fix #40168 ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. URL: #40170 Reported by: Frédéric Chapoton Reviewer(s): Travis Scrimshaw
2 parents 581aae7 + 8b6baec commit 95adbfc

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/sage/categories/finite_fields.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from sage.categories.category_with_axiom import CategoryWithAxiom
1616
from sage.categories.enumerated_sets import EnumeratedSets
17+
from sage.rings.integer import Integer
1718

1819

1920
class FiniteFields(CategoryWithAxiom):
@@ -192,5 +193,55 @@ def zeta(self, n=None):
192193
return mg() ** co_order
193194
return self._element_of_factored_order(n.factor())
194195

196+
def _element_of_factored_order(self, F):
197+
"""
198+
Return an element of ``self`` of order ``n`` where ``n`` is
199+
given in factored form.
200+
201+
This is copied from the cython implementation in
202+
``finite_field_base.pyx`` which is kept as it may be faster.
203+
204+
INPUT:
205+
206+
- ``F`` -- the factorization of the required order. The order
207+
must be a divisor of ``self.order() - 1`` but this is not
208+
checked.
209+
210+
EXAMPLES::
211+
212+
sage: k = Zmod(1913)
213+
sage: k in Fields() # to let k be a finite field
214+
True
215+
sage: k._element_of_factored_order(factor(1912))
216+
3
217+
"""
218+
n = Integer(1)
219+
primes = []
220+
for p, e in F:
221+
primes.append(p)
222+
n *= p**e
223+
224+
N = self.order() - 1
225+
c = N // n
226+
227+
# We check whether (x + g)^c has the required order, where
228+
# x runs through the finite field.
229+
# This has the advantage that g is the first element we try,
230+
# so if that was a chosen to be a multiplicative generator,
231+
# we are done immediately. Second, the PARI finite field
232+
# iterator gives all the constant elements first, so we try
233+
# (g+(constant))^c before anything else.
234+
g = self.gen()
235+
if g == self.one():
236+
# this allows to handle the ring Integers(prime)
237+
g = self.multiplicative_generator()
238+
for x in self:
239+
a = (g + x)**c
240+
if not a:
241+
continue
242+
if all(a**(n // p) != 1 for p in primes):
243+
return a
244+
raise AssertionError("no element found")
245+
195246
class ElementMethods:
196247
pass

0 commit comments

Comments
 (0)