@@ -133,7 +133,7 @@ from sage.categories.morphism cimport Morphism
133
133
134
134
from sage.misc.superseded import deprecation_cython as deprecation, deprecated_function_alias
135
135
from sage.misc.cachefunc import cached_method
136
-
136
+ from sage.misc.prandom import choice
137
137
138
138
cpdef is_Polynomial(f) noexcept:
139
139
"""
@@ -2214,19 +2214,19 @@ cdef class Polynomial(CommutativePolynomial):
2214
2214
2215
2215
def _any_irreducible_factor_squarefree (self , degree = None , ext_degree = None ):
2216
2216
"""
2217
- Helper function for any_irreducible_factor which computes
2217
+ Helper function for `` any_irreducible_factor()`` which computes
2218
2218
an irreducible factor from self, assuming the input is
2219
2219
squarefree.
2220
2220
2221
2221
Does this by first computing the distinct degree factorisations
2222
2222
of self and then finds a factor with Cantor-Zassenhaus
2223
2223
splitting.
2224
2224
2225
- If degree is not ``None``, then only irreducible factors of degree
2225
+ If `` degree`` is not ``None``, then only irreducible factors of degree
2226
2226
``degree`` are searched for, otherwise the smallest degree factor
2227
2227
is found.
2228
2228
2229
- If ext_degree is not ``None``, then only irreducible factors whose
2229
+ If `` ext_degree`` is not ``None``, then only irreducible factors whose
2230
2230
degree divides ``ext_degree`` are returned.
2231
2231
2232
2232
EXAMPLES::
@@ -2304,10 +2304,9 @@ cdef class Polynomial(CommutativePolynomial):
2304
2304
this polynomial is assumed to be the product of irreducible
2305
2305
polynomials of degree equal to ``degree``.
2306
2306
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
2309
2309
irreducible factors of ``self`` whose degree divides ``ext_degree``.
2310
- Assumes that ``degree`` is ``None``.
2311
2310
2312
2311
EXAMPLES::
2313
2312
@@ -2577,28 +2576,19 @@ cdef class Polynomial(CommutativePolynomial):
2577
2576
# When not working over a finite field, do the simple thing of factoring for
2578
2577
# roots and picking the first root. If none are available, raise an error.
2579
2578
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():
2590
2580
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} " )
2602
2592
2603
2593
# When the degree is none, we only look for a linear factor
2604
2594
if degree is None :
@@ -2645,7 +2635,8 @@ cdef class Polynomial(CommutativePolynomial):
2645
2635
# C library bindings for all finite fields.
2646
2636
# Until the coercion system for finite fields works better,
2647
2637
# 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)
2649
2640
2650
2641
# The old version of `any_root()` allowed degree < 0 to indicate that the input polynomial
2651
2642
# had a distinct degree factorisation, we pass this to any_irreducible_factor as a bool and
@@ -2697,7 +2688,8 @@ cdef class Polynomial(CommutativePolynomial):
2697
2688
# C library bindings for all finite fields.
2698
2689
# Until the coercion system for finite fields works better,
2699
2690
# 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)
2701
2693
2702
2694
def __truediv__ (left , right ):
2703
2695
r """
0 commit comments