Skip to content

Commit 7a61d6a

Browse files
committed
review comments
1 parent 2ce4ddd commit 7a61d6a

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

src/sage/rings/polynomial/polynomial_element.pyx

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ from sage.categories.morphism cimport Morphism
133133

134134
from sage.misc.superseded import deprecation_cython as deprecation, deprecated_function_alias
135135
from sage.misc.cachefunc import cached_method
136-
136+
from sage.misc.prandom import choice
137137

138138
cpdef is_Polynomial(f) noexcept:
139139
"""
@@ -2214,19 +2214,19 @@ cdef class Polynomial(CommutativePolynomial):
22142214

22152215
def _any_irreducible_factor_squarefree(self, degree=None, ext_degree=None):
22162216
"""
2217-
Helper function for any_irreducible_factor which computes
2217+
Helper function for ``any_irreducible_factor()`` which computes
22182218
an irreducible factor from self, assuming the input is
22192219
squarefree.
22202220
22212221
Does this by first computing the distinct degree factorisations
22222222
of self and then finds a factor with Cantor-Zassenhaus
22232223
splitting.
22242224
2225-
If degree is not ``None``, then only irreducible factors of degree
2225+
If ``degree`` is not ``None``, then only irreducible factors of degree
22262226
``degree`` are searched for, otherwise the smallest degree factor
22272227
is found.
22282228
2229-
If ext_degree is not ``None``, then only irreducible factors whose
2229+
If ``ext_degree`` is not ``None``, then only irreducible factors whose
22302230
degree divides ``ext_degree`` are returned.
22312231
22322232
EXAMPLES::
@@ -2304,10 +2304,9 @@ cdef class Polynomial(CommutativePolynomial):
23042304
this polynomial is assumed to be the product of irreducible
23052305
polynomials of degree equal to ``degree``.
23062306
2307-
- ``ext_degree`` (None or positive integer) -- (default: ``None``).
2308-
Used for polynomials over finite fields. If not ``None`` only returns
2307+
- ``ext_degree`` -- positive integer or ``None`` (default);
2308+
used for polynomials over finite fields. If not ``None`` only returns
23092309
irreducible factors of ``self`` whose degree divides ``ext_degree``.
2310-
Assumes that ``degree`` is ``None``.
23112310
23122311
EXAMPLES::
23132312
@@ -2577,28 +2576,19 @@ cdef class Polynomial(CommutativePolynomial):
25772576
# When not working over a finite field, do the simple thing of factoring for
25782577
# roots and picking the first root. If none are available, raise an error.
25792578
from sage.categories.finite_fields import FiniteFields
2580-
if not self.base_ring() in FiniteFields():
2581-
rs = self.roots(ring=ring, multiplicities=False)
2582-
if rs:
2583-
return rs[0]
2584-
raise ValueError(f"polynomial {self} has no roots")
2585-
2586-
# Ensure that a provided ring is appropriate for the function
2587-
if ring is not None:
2588-
# If the new ring is not a finite field, attempt to coerce the polynomial
2589-
# and call the function to use naive factoring
2579+
if self.base_ring() not in FiniteFields():
25902580
if ring not in FiniteFields():
2591-
try:
2592-
f = self.change_ring(ring)
2593-
except ValueError:
2594-
raise(f"cannot coerce polynomial {self} to the new ring: {ring}")
2595-
return f.any_root()
2596-
2597-
# If the ring is a finite field, ensure it's an extension of the base ring
2598-
if self.base_ring().characteristic() != ring.characteristic():
2599-
raise ValueError("ring must have the same characteristic as the base ring")
2600-
if not self.base_ring().degree().divides(ring.degree()):
2601-
raise ValueError("ring must be an extension of the base ring")
2581+
rs = self.roots(ring=ring, multiplicities=False)
2582+
if rs:
2583+
return choice(rs)
2584+
raise ValueError(f"polynomial {self} has no roots")
2585+
2586+
# Ensure that a provided ring is appropriate for the function. From the
2587+
# above we know it is either None or a finite field. When it's a finite
2588+
# field we ensure there's a coercion from the base ring to ring.
2589+
if ring is not None:
2590+
if ring.coerce_map_from(self.base_ring()) is None:
2591+
raise ValueError(f"no coercion map can be computed from {self.base_ring()} to {ring}")
26022592

26032593
# When the degree is none, we only look for a linear factor
26042594
if degree is None:
@@ -2645,7 +2635,8 @@ cdef class Polynomial(CommutativePolynomial):
26452635
# C library bindings for all finite fields.
26462636
# Until the coercion system for finite fields works better,
26472637
# this will be the most performant
2648-
return f.roots(ring, multiplicities=False)[0]
2638+
roots = f.roots(ring, multiplicities=False)
2639+
return choice(roots)
26492640

26502641
# The old version of `any_root()` allowed degree < 0 to indicate that the input polynomial
26512642
# had a distinct degree factorisation, we pass this to any_irreducible_factor as a bool and
@@ -2697,7 +2688,8 @@ cdef class Polynomial(CommutativePolynomial):
26972688
# C library bindings for all finite fields.
26982689
# Until the coercion system for finite fields works better,
26992690
# this will be the most performant
2700-
return f.roots(ring, multiplicities=False)[0]
2691+
roots = f.roots(ring, multiplicities=False)
2692+
return choice(roots)
27012693

27022694
def __truediv__(left, right):
27032695
r"""

0 commit comments

Comments
 (0)