Ticket #14833: trac_14833-FiniteField_polynomial_choice.patch

File trac_14833-FiniteField_polynomial_choice.patch, 9.7 KB (added by Peter Bruin, 9 years ago)

based on 5.11.beta3 + #14832, use @cached_method

  • sage/rings/finite_rings/constructor.py

    # HG changeset patch
    # User Peter Bruin <peter.bruin@math.uzh.ch>
    # Date 1372287553 -7200
    # Node ID 6b1cb7ccbfd6f9203b68df9a0afa7d3f27349f28
    # Parent  b588bbbce03b83a765915113c01d502adf962345
    Trac 14833: implementation-independent choice of irreducible polynomials
    
    diff --git a/sage/rings/finite_rings/constructor.py b/sage/rings/finite_rings/constructor.py
    a b  
    342342        EXAMPLES::
    343343       
    344344            sage: GF.create_key_and_extra_args(9, 'a')
    345             ((9, ('a',), 'conway', None, '{}', 3, 2, True), {})
     345            ((9, ('a',), x^2 + 2*x + 2, None, '{}', 3, 2, True), {})
    346346            sage: GF.create_key_and_extra_args(9, 'a', foo='value')
    347             ((9, ('a',), 'conway', None, "{'foo': 'value'}", 3, 2, True), {'foo': 'value'})
     347            ((9, ('a',), x^2 + 2*x + 2, None, "{'foo': 'value'}", 3, 2, True), {'foo': 'value'})
    348348        """
    349349        from sage.structure.proof.all import WithProof, arithmetic
    350350        if proof is None: proof = arithmetic()
     
    364364
    365365                p, n = arith.factor(order)[0]
    366366
    367                 if modulus is None or modulus == "default":
    368                     if exists_conway_polynomial(p, n):
    369                         modulus = "conway"
    370                     else:
    371                         if p == 2:
    372                             modulus = "minimal_weight"
    373                         else:
    374                             modulus = "random"
    375                 elif modulus == "random":
    376                     modulus += str(random.randint(0, 1<<128))
    377 
    378                 if isinstance(modulus, (list, tuple)):
    379                     modulus = FiniteField(p)['x'](modulus)
    380                 # some classes use 'random' as the modulus to
    381                 # generate a random modulus, but we don't want
    382                 # to cache it
     367                if modulus is None or isinstance(modulus, str):
     368                    # A string specifies an algorithm to find a suitable modulus.
     369                    if modulus == "default":    # for backward compatibility
     370                        modulus = None
     371                    modulus = GF(p)['x'].irreducible_element(n, algorithm=modulus)
     372                elif isinstance(modulus, (list, tuple)):
     373                    modulus = GF(p)['x'](modulus)
    383374                elif sage.rings.polynomial.polynomial_element.is_Polynomial(modulus):
    384375                    modulus = modulus.change_variable_name('x')
    385                 elif not isinstance(modulus, str):
    386                     raise ValueError("Modulus parameter not understood.")
    387             else:  # Neither a prime, nor a prime power
     376                else:
     377                    raise TypeError("wrong type for modulus parameter")
     378            else:
    388379                raise ValueError("the order of a finite field must be a prime power.")
    389380
    390381            return (order, name, modulus, impl, str(kwds), p, n, proof), kwds
     
    417408        else:
    418409            order, name, modulus, impl, _, p, n, proof = key
    419410       
    420         if isinstance(modulus, str) and modulus.startswith("random"):
    421             modulus = "random"
    422        
    423411        if elem_cache is None:
    424412            elem_cache = order < 500
    425413
     
    466454        EXAMPLES::
    467455       
    468456            sage: key, extra = GF.create_key_and_extra_args(9, 'a'); key
    469             (9, ('a',), 'conway', None, '{}', 3, 2, True)
     457            (9, ('a',), x^2 + 2*x + 2, None, '{}', 3, 2, True)
    470458            sage: K = GF.create_object(0, key); K
    471459            Finite Field in a of size 3^2
    472460            sage: GF.other_keys(key, K)
  • sage/rings/finite_rings/element_givaro.pyx

    diff --git a/sage/rings/finite_rings/element_givaro.pyx b/sage/rings/finite_rings/element_givaro.pyx
    a b  
    18151815            g = int(gap.eval('Int(Z(%s))'%cache.order_c()))
    18161816            n = self.log(mod(g, cache.order_c()))
    18171817            return 'Z(%s)^%s'%(cache.order_c(), n)
    1818         if not F._is_conway:
     1818        if not F.is_conway():
    18191819            raise NotImplementedError, "conversion of (Givaro) finite field element to GAP not implemented except for fields defined by Conway polynomials."
    18201820        if cache.order_c() > 65536:
    18211821            raise TypeError, "order (=%s) must be at most 65536."%F.order_c()
  • sage/rings/finite_rings/element_ntl_gf2e.pyx

    diff --git a/sage/rings/finite_rings/element_ntl_gf2e.pyx b/sage/rings/finite_rings/element_ntl_gf2e.pyx
    a b  
    139139        (<unsigned char*>&ret)[j] = (<unsigned char*>&i)[sizeof(int)-j-1]
    140140    return ret
    141141
    142 
    143142cdef class Cache_ntl_gf2e(SageObject):
    144143    """
    145144    This class stores information for an NTL finite field in a Cython
     
    12661265            'Z(65536)^1'
    12671266        """
    12681267        F = self._parent
    1269         if not F._is_conway:
     1268        if not F.is_conway():
    12701269            raise NotImplementedError, "conversion of (NTL) finite field element to GAP not implemented except for fields defined by Conway polynomials."
    12711270        if F.order() > 65536:
    12721271            raise TypeError, "order (=%s) must be at most 65536."%F.order()
  • sage/rings/finite_rings/finite_field_base.pyx

    diff --git a/sage/rings/finite_rings/finite_field_base.pyx b/sage/rings/finite_rings/finite_field_base.pyx
    a b  
    1111include "sage/ext/stdsage.pxi"
    1212
    1313from sage.structure.parent cimport Parent
     14from sage.misc.cachefunc import cached_method
    1415from sage.misc.prandom import randrange
    1516from sage.rings.finite_rings.homset import FiniteFieldHomset
    1617
     
    442443            a
    443444            sage: k.<b> = GF(19^32)
    444445            sage: k.multiplicative_generator()
    445             b + 5
     446            b + 4
    446447
    447448        TESTS:
    448449
     
    762763        """
    763764        raise NotImplementedError, "Algebraic closures of finite fields not implemented."
    764765
     766    @cached_method
     767    def is_conway(self):
     768        """
     769        Return ``True`` if self is defined by a Conway polynomial.
     770
     771        EXAMPLES:
     772
     773            sage: GF(5^3, 'a').is_conway()
     774            True
     775            sage: GF(5^3, 'a', modulus='adleman-lenstra').is_conway()
     776            False
     777            sage: GF(next_prime(2^16, 2), 'a').is_conway()
     778            False
     779        """
     780        from constructor import conway_polynomial, exists_conway_polynomial
     781        p = self.characteristic()
     782        n = self.degree()
     783        return (exists_conway_polynomial(p, n)
     784                and self.polynomial() == self.polynomial_ring()(conway_polynomial(p, n)))
     785
    765786
    766787def unpickle_FiniteField_ext(_type, order, variable_name, modulus, kwargs):
    767788    r"""
  • sage/rings/finite_rings/finite_field_givaro.py

    diff --git a/sage/rings/finite_rings/finite_field_givaro.py b/sage/rings/finite_rings/finite_field_givaro.py
    a b  
    124124        self._kwargs['repr'] = repr
    125125        self._kwargs['cache'] = cache
    126126
    127         self._is_conway = False
    128127        if modulus is None or modulus == 'conway':
    129128            if k == 1:
    130129                modulus = 'random' # this will use the gfq_factory_pk function.
    131130            elif ConwayPolynomials().has_polynomial(p, k):
    132131                from sage.rings.finite_rings.constructor import conway_polynomial
    133132                modulus = conway_polynomial(p, k)
    134                 self._is_conway = True
    135133            elif modulus is None:
    136134                modulus = 'random'
    137135            else:
  • sage/rings/finite_rings/finite_field_ntl_gf2e.py

    diff --git a/sage/rings/finite_rings/finite_field_ntl_gf2e.py b/sage/rings/finite_rings/finite_field_ntl_gf2e.py
    a b  
    111111            sage: k.<a> = GF(2^100, modulus='strangeinput')
    112112            Traceback (most recent call last):
    113113            ...
    114             ValueError: Modulus parameter not understood
     114            ValueError: no such algorithm for finding an irreducible polynomial: strangeinput
    115115            sage: k.<a> = GF(2^20) ; type(k)
    116116            <class 'sage.rings.finite_rings.finite_field_ntl_gf2e.FiniteField_ntl_gf2e_with_category'>
    117117            sage: loads(dumps(k)) is k
     
    144144        FiniteField.__init__(self, GF(p), names, normalize=True)
    145145
    146146        self._kwargs = {'repr':repr}
    147         self._is_conway = False
    148147
    149148        if modulus is None or modulus == 'default':
    150149            if exists_conway_polynomial(p, k):
     
    153152                modulus = "minimal_weight"
    154153        if modulus == "conway":
    155154            modulus = conway_polynomial(p, k)
    156             self._is_conway = True
    157155        if is_Polynomial(modulus):
    158156            modulus = modulus.list()
    159157        self._cache = Cache_ntl_gf2e(self, k, modulus)
  • sage/rings/polynomial/polynomial_zz_pex.pyx

    diff --git a/sage/rings/polynomial/polynomial_zz_pex.pyx b/sage/rings/polynomial/polynomial_zz_pex.pyx
    a b  
    328328
    329329            sage: K.<a>=GF(next_prime(2**60)**3)
    330330            sage: R.<x> = PolynomialRing(K,implementation='NTL')
    331             sage: P = x^3+(a+3)*x+1
     331            sage: P = x^3+(2-a)*x+1
    332332            sage: P.is_irreducible(algorithm="fast_when_false")
    333333            True
    334334            sage: P.is_irreducible(algorithm="fast_when_true")
  • sage/structure/factory.pyx

    diff --git a/sage/structure/factory.pyx b/sage/structure/factory.pyx
    a b  
    267267        EXAMPLES::
    268268
    269269            sage: key, _ = GF.create_key_and_extra_args(27, 'k'); key
    270             (27, ('k',), 'conway', None, '{}', 3, 3, True)
     270            (27, ('k',), x^3 + 2*x + 1, None, '{}', 3, 3, True)
    271271            sage: K = GF.create_object(0, key); K
    272272            Finite Field in k of size 3^3
    273273            sage: GF.other_keys(key, K)