Ticket #11239: trac_11239-polynomial_zz_pex.patch

File trac_11239-polynomial_zz_pex.patch, 2.9 KB (added by pbruin, 6 years ago)

fix coercion of polynomials over finite fields

  • sage/rings/polynomial/polynomial_zz_pex.pyx

    # HG changeset patch
    # User Peter Bruin <peter.bruin@math.uzh.ch>
    # Date 1375736213 -7200
    # Node ID cebb1a7b1d5b752fb15b4cae6a8140f51ca029ca
    # Parent  c207130fe25465501d8bcb45b7751a346751c0a1
    Trac 11239: fix coercion of polynomials over finite fields
    
    diff --git a/sage/rings/polynomial/polynomial_zz_pex.pyx b/sage/rings/polynomial/polynomial_zz_pex.pyx
    a b  
    8989            sage: R([3,'12e34'])
    9090            Traceback (most recent call last):
    9191            ...
    92             TypeError: unable to convert '12e34' into the base ring
     92            TypeError: unable to convert x (=12e34) to an integer
    9393            sage: R([3,x])
    9494            Traceback (most recent call last):
    9595            ...
    96             TypeError: unable to convert x into the base ring
     96            TypeError: not a constant polynomial
    9797
     98        Check that :trac:`11239` is fixed::
     99
     100            sage: Fq.<a> = GF(2^4); Fqq.<b> = GF(3^7)
     101            sage: PFq.<x> = Fq[]; PFqq.<y> = Fqq[]
     102            sage: f = x^3 + (a^3 + 1)*x
     103            sage: sage.rings.polynomial.polynomial_zz_pex.Polynomial_ZZ_pEX(PFqq, f)
     104            Traceback (most recent call last):
     105            ...
     106            TypeError: unable to coerce from a finite field other than the prime subfield
    98107        """
    99108        cdef ntl_ZZ_pE d
    100109        try:       
     
    108117        except AttributeError:
    109118            pass
    110119
     120        if isinstance(x, Polynomial):
     121            x = x.list()
     122
    111123        if PY_TYPE_CHECK(x, list) or PY_TYPE_CHECK(x, tuple):
    112124            Polynomial.__init__(self, parent, is_gen=is_gen)
    113125            (<Polynomial_template>self)._cparent = get_cparent(parent)
    114126            celement_construct(&self.x, (<Polynomial_template>self)._cparent)
    115127            K = parent.base_ring()
    116128            for i,e in enumerate(x):
    117                 try:
    118                     e_polynomial = e.polynomial()
    119                 except (AttributeError, TypeError):
    120                     # A type error may occur, since sometimes
    121                     # e.polynomial expects an additional argument
    122                     try:
    123                         # self(x) is supposed to be a conversion,
    124                         # not necessarily a coercion. So, we must
    125                         # not do K.coerce(e) but K(e).
    126                         e = K(e) # K.coerce(e)
    127                         e_polynomial = e.polynomial()
    128                     except TypeError:
    129                         raise TypeError("unable to convert %s into the base ring"%repr(e))
    130                 d = parent._modulus.ZZ_pE(list(e_polynomial))
     129                # self(x) is supposed to be a conversion,
     130                # not necessarily a coercion. So, we must
     131                # not do K.coerce(e) but K(e).
     132                e = K(e)
     133                d = parent._modulus.ZZ_pE(list(e.polynomial()))
    131134                ZZ_pEX_SetCoeff(self.x, i, d.x)
    132135            return
    133136