# Ticket #14832: trac_14832_make_irreducible_polynomial.patch

File trac_14832_make_irreducible_polynomial.patch, 10.0 KB (added by pbruin, 10 years ago)

based on 5.11.beta3

• ## sage/rings/polynomial/polynomial_gf2x.pyx

```# HG changeset patch
# User Peter Bruin <peter.bruin@math.uzh.ch>
# Date 1372186250 -7200
# Node ID 15c4954a1e738ebc79f550edab54282af3f50fa0
# Parent  739c6c4c4f751f2fe95f291c9b4e75cd7e51e4c2
Trac 14832: construct irreducible polynomials over finite fields

diff --git a/sage/rings/polynomial/polynomial_gf2x.pyx b/sage/rings/polynomial/polynomial_gf2x.pyx```
 a return False else: return True # The three functions below are used in polynomial_ring.py, but are in # this Cython file since they call C++ functions.  They return # polynomials as lists so that no variable has to be specified. # AUTHOR: Peter Bruin (June 2013) def GF2X_BuildIrred_list(n): """ Return the list of coefficients of the lexicographically smallest irreducible polynomial of degree `n` over the field of 2 elements. EXAMPLE: sage: from sage.rings.polynomial.polynomial_gf2x import GF2X_BuildIrred_list sage: GF2X_BuildIrred_list(2) [1, 1, 1] sage: GF2X_BuildIrred_list(3) [1, 1, 0, 1] sage: GF2X_BuildIrred_list(4) [1, 1, 0, 0, 1] sage: GF(2)['x'](GF2X_BuildIrred_list(33)) x^33 + x^6 + x^3 + x + 1 """ from sage.rings.finite_rings.constructor import FiniteField cdef GF2X_c f GF2 = FiniteField(2) GF2X_BuildIrred(f, int(n)) return [GF2(not GF2_IsZero(GF2X_coeff(f, i))) for i in xrange(n + 1)] def GF2X_BuildSparseIrred_list(n): """ Return the list of coefficients of an irreducible polynomial of degree `n` of minimal weight over the field of 2 elements. EXAMPLE: sage: from sage.rings.polynomial.polynomial_gf2x import GF2X_BuildIrred_list, GF2X_BuildSparseIrred_list sage: all([GF2X_BuildSparseIrred_list(n) == GF2X_BuildIrred_list(n) ....:      for n in range(1,33)]) True sage: GF(2)['x'](GF2X_BuildSparseIrred_list(33)) x^33 + x^10 + 1 """ from sage.rings.finite_rings.constructor import FiniteField cdef GF2X_c f GF2 = FiniteField(2) GF2X_BuildSparseIrred(f, int(n)) return [GF2(not GF2_IsZero(GF2X_coeff(f, i))) for i in xrange(n + 1)] def GF2X_BuildRandomIrred_list(n): """ Return the list of coefficients of an irreducible polynomial of degree `n` of minimal weight over the field of 2 elements. EXAMPLE: sage: from sage.rings.polynomial.polynomial_gf2x import GF2X_BuildRandomIrred_list sage: GF2X_BuildRandomIrred_list(2) [1, 1, 1] sage: GF2X_BuildRandomIrred_list(3) in [[1, 1, 0, 1], [1, 0, 1, 1]] True """ from sage.misc.randstate import current_randstate from sage.rings.finite_rings.constructor import FiniteField cdef GF2X_c tmp, f GF2 = FiniteField(2) current_randstate().set_seed_ntl(False) GF2X_BuildSparseIrred(tmp, int(n)) GF2X_BuildRandomIrred(f, tmp) return [GF2(not GF2_IsZero(GF2X_coeff(f, i))) for i in xrange(n + 1)]
• ## sage/rings/polynomial/polynomial_ring.py

`diff --git a/sage/rings/polynomial/polynomial_ring.py b/sage/rings/polynomial/polynomial_ring.py`
 a self._fraction_field = FractionField_1poly_field(self) return self._fraction_field class PolynomialRing_dense_finite_field(PolynomialRing_field): """ Univariate polynomial ring over a finite field. """ def irreducible_element(self, n, algorithm=None): """ Construct an irreducible polynomial of degree `n`. INPUT: - ``n`` -- integer: degree of the polynomial to construct - ``algorithm`` -- string: algorithm to use, or ``None`` - ``'random'``: try random polynomials until an irreducible one is found.  This is currently the only algorithm available over non-prime finite fields. OUTPUT: A monic irreducible polynomial of degree `n` in ``self``. EXAMPLES: sage: GF(5^3, 'a')['x'].irreducible_element(2) x^2 + (4*a^2 + a + 4)*x + 2*a^2 + 2 AUTHOR: Peter Bruin (June 2013) """ if n < 1: raise ValueError("degree must be at least 1") if algorithm is None or algorithm == "random": while True: f = self.gen()**n + self.random_element(n - 1) if f.is_irreducible(): return f else: raise ValueError("no such algorithm for finding an irreducible polynomial: %s" % algorithm) class PolynomialRing_dense_padic_ring_generic(PolynomialRing_integral_domain): pass return s + self._implementation_repr class PolynomialRing_dense_mod_p(PolynomialRing_field, class PolynomialRing_dense_mod_p(PolynomialRing_dense_finite_field, PolynomialRing_dense_mod_n, PolynomialRing_singular_repr): def __init__(self, base_ring, name="x", implementation=None): from sage.rings.polynomial.polynomial_singular_interface import can_convert_to_singular self._has_singular = can_convert_to_singular(self) def irreducible_element(self, n, algorithm=None): """ Construct an irreducible polynomial of degree `n`. INPUT: - ``n`` -- integer: the degree of the polynomial to construct - ``algorithm`` -- string: algorithm to use, or ``None``. Currently available options are: - ``'adleman-lenstra'``: a variant of the Adleman--Lenstra algorithm as implemented in PARI. - ``'conway'``: look up the Conway polynomial of degree `n` over the field of `p` elements in the database; raise a ``RuntimeError`` if it is not found. - ``'first_lexicographic'``: return the lexicographically smallest irreducible polynomial of degree `n`.  Only implemented for `p = 2`. - ``'minimal_weight'``: return an irreducible polynomial of degree `n` with minimal number of non-zero coefficients. Only implemented for `p = 2`. - ``'random'``: try random polynomials until an irreducible one is found. If ``algorithm`` is ``None``, the Conway polynomial is used if it is found in the database.  If no Conway polynomial is found, the algorithm ``minimal_weight`` is used if `p = 2`, and the algorithm ``adleman-lenstra`` if `p > 2`. OUTPUT: A monic irreducible polynomial of degree `n` in ``self``. EXAMPLES: sage: GF(5)['x'].irreducible_element(2) x^2 + 4*x + 2 sage: GF(5)['x'].irreducible_element(2, algorithm="adleman-lenstra") x^2 + x + 1 sage: GF(2)['x'].irreducible_element(33) x^33 + x^13 + x^12 + x^11 + x^10 + x^8 + x^6 + x^3 + 1 sage: GF(2)['x'].irreducible_element(33, algorithm="minimal_weight") x^33 + x^10 + 1 AUTHOR: Peter Bruin (June 2013) """ from sage.libs.pari.all import pari from sage.rings.finite_rings.constructor import (conway_polynomial, exists_conway_polynomial) from polynomial_gf2x import (GF2X_BuildIrred_list, GF2X_BuildSparseIrred_list, GF2X_BuildRandomIrred_list) p = self.characteristic() n = int(n) if n < 1: raise ValueError("degree must be at least 1") if algorithm is None: if exists_conway_polynomial(p, n): algorithm = "conway" elif p == 2: algorithm = "minimal_weight" else: algorithm = "adleman-lenstra" if algorithm == "adleman-lenstra": return self(pari(p).ffinit(n)) elif algorithm == "conway": return self(conway_polynomial(p, n)) elif algorithm == "first_lexicographic": if p == 2: return self(GF2X_BuildIrred_list(n)) else: raise NotImplementedError("'first_lexicographic' option only implemented for p = 2") elif algorithm == "minimal_weight": if p == 2: return self(GF2X_BuildSparseIrred_list(n)) else: raise NotImplementedError("'minimal_weight' option only implemented for p = 2") elif algorithm == "random": if p == 2: return self(GF2X_BuildRandomIrred_list(n)) else: pass # No suitable algorithm found, try algorithms from the base class. return PolynomialRing_dense_finite_field.irreducible_element(self, n, algorithm) def polygen(ring_or_element, name="x"): """
• ## sage/rings/polynomial/polynomial_ring_constructor.py

`diff --git a/sage/rings/polynomial/polynomial_ring_constructor.py b/sage/rings/polynomial/polynomial_ring_constructor.py`
 a import sage.rings.padics.padic_base_leaves as padic_base_leaves from sage.rings.integer import Integer from sage.rings.finite_rings.constructor import is_FiniteField from sage.rings.finite_rings.integer_mod_ring import is_IntegerModRing from sage.misc.cachefunc import weak_cached_function else:  # n == 1! R = m.PolynomialRing_integral_domain(base_ring, name)   # specialized code breaks in this case. elif is_FiniteField(base_ring) and not sparse: R = m.PolynomialRing_dense_finite_field(base_ring, name) elif isinstance(base_ring, padic_base_leaves.pAdicFieldCappedRelative): R = m.PolynomialRing_dense_padic_field_capped_relative(base_ring, name)