Ticket #11784: trac-11784-Galois_fields.patch

File trac-11784-Galois_fields.patch, 5.6 KB (added by lftabera, 10 years ago)
  • sage/rings/finite_rings/constructor.py

    # HG changeset patch
    # User Luis Felipe Tabera Alonso <lftabera@yahoo.es>
    # Parent 9e29a3d84c48c399daaf3920bcb8b17273a0e876
    #11784 Add more sanity checks to Galois Field constructor.
    
    diff --git a/sage/rings/finite_rings/constructor.py b/sage/rings/finite_rings/constructor.py
    a b  
    250250        sage: K.<a> = GF(5**5, name='a', modulus=x^5 - x )
    251251        Traceback (most recent call last):
    252252        ...
    253         ValueError: finite field modulus must be irreducible but it is not
     253        ValueError: finite field modulus must be irreducible but it is not.
    254254   
    255255    You can't accidentally fool the constructor into thinking the modulus
    256256    is irreducible when it isn't mod p, since it actually tests
    257     irreducibility modulo p.
     257    irreducibility modulo p.  Also, the modulus has to be of the right degree.
    258258   
    259259    ::
    260260   
    261261        sage: F.<x> = QQ[]
    262         sage: factor(x^5+2)
     262        sage: factor(x^5 + 2)
    263263        x^5 + 2
    264264        sage: K.<a> = GF(5**5, name='a', modulus=x^5 + 2 )
    265265        Traceback (most recent call last):
    266266        ...
    267         ValueError: finite field modulus must be irreducible but it is not
     267        ValueError: finite field modulus must be irreducible but it is not.
     268        sage: K.<a> = GF(5**5, name='a', modulus=x^3 + 3*x + 3)
     269        Traceback (most recent call last):
     270        ...
     271        ValueError: The degree of the modulus does not correspond to the
     272        cardinality of the field.
    268273   
    269274    If you wish to live dangerously, you can tell the constructor not
    270275    to test irreducibility using check_irreducible=False, but this can
    271276    easily lead to crashes and hangs - so do not do it unless you know
    272     that the modulus really is irreducible!
     277    that the modulus really is irreducible and has the correct degree!
    273278   
    274279    ::
    275280   
    276281        sage: F.<x> = GF(5)[]
    277282        sage: K.<a> = GF(5**2, name='a', modulus=x^2 + 2, check_irreducible=False)
    278    
     283
     284    ::
     285
     286        sage: L = GF(3**2, name='a', modulus=QQ[x](x - 1), check_irreducible=False)
     287        sage: L.list()  # random
     288        [0, a, 1, 2, 1, 2, 1, 2, 1]
     289
    279290    The order of a finite field must be a prime power::
    280291   
    281292        sage: GF(100)
    282293        Traceback (most recent call last):
    283294        ...
    284         ValueError: the order of a finite field must be a prime power
     295        ValueError: the order of a finite field must be a prime power.
    285296   
    286297    Finite fields with explicit random modulus are not cached::
    287298   
     
    316327   
    317328    ::
    318329   
    319         sage: k.<a> = GF(2^8,repr='int')
     330        sage: k.<a> = GF(2^8, repr='int')
    320331        sage: a
    321332        2
    322333
     
    336347        with WithProof('arithmetic', proof):
    337348            order = int(order)
    338349            if order == 1:
    339                 raise ValueError("the order of a finite field must be > 1")
     350                raise ValueError("the order of a finite field must be > 1.")
    340351            if not arith.is_prime_power(order):
    341                     raise ValueError("the order of a finite field must be a prime power")
     352                    raise ValueError("the order of a finite field must be a prime power.")
    342353
    343354            if arith.is_prime(order):
    344355                name = None
     
    349360                if not names is None: name = names
    350361                name = normalize_names(1,name)
    351362
    352                 p,n = arith.factor(order)[0]
     363                p, n = arith.factor(order)[0]
    353364
    354365                if modulus is None or modulus == "default":
    355                     if exists_conway_polynomial(p,n):
     366                    if exists_conway_polynomial(p, n):
    356367                        modulus = "conway"
    357368                    else:
    358                         if p==2:
     369                        if p == 2:
    359370                            modulus = "minimal_weight"
    360371                        else:
    361372                            modulus = "random"
     
    370381                elif sage.rings.polynomial.polynomial_element.is_Polynomial(modulus):
    371382                    modulus = modulus.change_variable_name('x')
    372383                elif not isinstance(modulus, str):
    373                     raise ValueError("Modulus parameter not understood")
     384                    raise ValueError("Modulus parameter not understood.")
    374385
    375386            return (order, name, modulus, impl, str(kwds), p, n, proof), kwds
    376387   
     
    426437                    if modulus.parent().base_ring().characteristic() == 0:
    427438                        modulus = modulus.change_ring(FiniteField(p))
    428439                    if not modulus.is_irreducible():
    429                         raise ValueError, "finite field modulus must be irreducible but it is not"
     440                        raise ValueError("finite field modulus must be irreducible but it is not.")
     441                    if modulus.degree() != n:
     442                        raise ValueError("The degree of the modulus does not correspond to the cardinality of the field.")
    430443                if name is None:
    431                     raise TypeError, "you must specify the generator name"
     444                    raise TypeError("you must specify the generator name.")
    432445                if order < zech_log_bound: 
    433446                    # DO *NOT* use for prime subfield, since that would lead to
    434447                    # a circular reference in the call to ParentWithGens in the
     
    564577    try:
    565578        return R(sage.databases.conway.ConwayPolynomials()[p][n])
    566579    except KeyError:
    567         raise RuntimeError, "requested conway polynomial not in database."
     580        raise RuntimeError("requested conway polynomial not in database.")
    568581
    569582def exists_conway_polynomial(p, n):
    570583    r"""