Ticket #4965: polynomial_template.patch

File polynomial_template.patch, 21.4 KB (added by Martin Albrecht, 14 years ago)

apply this first

  • sage/libs/ntl/ntl_GF2X_linkage.pxi

    # HG changeset patch
    # User Martin Albrecht <malb@informatik.uni-bremen.de>
    # Date 1231624756 0
    # Node ID 49ef6eab9e04b10351a8d5435b685214ae0632f2
    # Parent  bd3e75c83235a0530e21af479a2d9ee31faa7a8c
    better handling of parents in polynomial_template
    
    diff -r bd3e75c83235 -r 49ef6eab9e04 sage/libs/ntl/ntl_GF2X_linkage.pxi
    a b  
    1717from sage.libs.ntl.ntl_GF2_decl cimport *, GF2_c
    1818from sage.libs.ntl.ntl_GF2X_decl cimport *, GF2X_c, GF2XModulus_c
    1919
    20 cdef int celement_construct(GF2X_c *e, parent):
     20cdef int celement_construct(GF2X_c *e, long parent):
    2121    """
    2222    EXAMPLE:
    2323        sage: P.<x> = GF(2)[]
    2424    """
    2525    GF2X_construct(e)
    2626
    27 cdef int celement_destruct(GF2X_c *e, parent):
     27cdef int celement_destruct(GF2X_c *e, long parent):
    2828    """
    2929    EXAMPLE:
    3030        sage: P.<x> = GF(2)[]
     
    3232    """
    3333    GF2X_destruct(e)
    3434
    35 cdef int celement_gen(GF2X_c *e, long i, parent) except -2:
     35cdef int celement_gen(GF2X_c *e, long i, long parent) except -2:
    3636    """
    3737    EXAMPLE:
    3838        sage: P.<x> = GF(2)[]
     
    4040    cdef unsigned char g = 2
    4141    GF2XFromBytes(e[0], <unsigned char *>(&g), 1)
    4242
    43 cdef object celement_repr(GF2X_c *e, parent):
     43cdef object celement_repr(GF2X_c *e, long parent):
    4444    """
    4545    We ignore NTL's printing.
    4646
     
    5252    #return GF2X_to_PyString(e)
    5353    raise NotImplementedError
    5454
    55 cdef inline int celement_set(GF2X_c* res, GF2X_c* a, parent) except -2:
     55cdef inline int celement_set(GF2X_c* res, GF2X_c* a, long parent) except -2:
    5656    """
    5757    EXAMPLE:
    5858        sage: P.<x> = GF(2)[]
     
    6161    """
    6262    res[0] = a[0]
    6363
    64 cdef inline int celement_set_si(GF2X_c* res, long i, parent) except -2:
     64cdef inline int celement_set_si(GF2X_c* res, long i, long parent) except -2:
    6565    """
    6666    EXAMPLE:
    6767        sage: P.<x> = GF(2)[]
     
    7474    """
    7575    GF2X_conv_long(res[0], i)
    7676
    77 cdef inline long celement_get_si(GF2X_c* res, parent) except -2:
     77cdef inline long celement_get_si(GF2X_c* res, long parent) except -2:
    7878    raise NotImplementedError
    7979   
    80 cdef inline bint celement_is_zero(GF2X_c* a, parent) except -2:
     80cdef inline bint celement_is_zero(GF2X_c* a, long parent) except -2:
    8181    """
    8282    EXAMPLE:
    8383        sage: P.<x> = GF(2)[]
     
    8888    """
    8989    return GF2X_IsZero(a[0])
    9090
    91 cdef inline bint celement_is_one(GF2X_c *a, parent) except -2:
     91cdef inline bint celement_is_one(GF2X_c *a, long parent) except -2:
    9292    """
    9393    EXAMPLE:
    9494        sage: P.<x> = GF(2)[]
     
    9999    """
    100100    return GF2X_IsOne(a[0])
    101101
    102 cdef inline bint celement_equal(GF2X_c *a, GF2X_c *b, parent) except -2:
     102cdef inline bint celement_equal(GF2X_c *a, GF2X_c *b, long parent) except -2:
    103103    """
    104104    EXAMPLE:
    105105        sage: P.<x> = GF(2)[]
     
    112112    """
    113113    return GF2X_equal(a[0], b[0])
    114114
    115 cdef inline int celement_cmp(GF2X_c *a, GF2X_c *b, parent) except -2:
     115cdef inline int celement_cmp(GF2X_c *a, GF2X_c *b, long parent) except -2:
    116116    """
    117117    EXAMPLE:
    118118        sage: P.<x> = GF(2)[]
     
    134134    else:
    135135        return -1
    136136
    137 cdef inline long celement_hash(GF2X_c *a, parent) except -2:
     137cdef inline long celement_hash(GF2X_c *a, long parent) except -2:
    138138    """
    139139    EXAMPLE:
    140140        sage: P.<x> = GF(2)[]
     
    147147    GF2XHexOutput_c[0] = _hex
    148148    return hash(s)
    149149
    150 cdef long celement_len(GF2X_c *a, parent) except -2:
     150cdef long celement_len(GF2X_c *a, long parent) except -2:
    151151    """
    152152    EXAMPLE:
    153153        sage: P.<x> = GF(2)[]
     
    158158    """
    159159    return int(GF2X_NumBits(a[0]))
    160160
    161 cdef inline int celement_add(GF2X_c *res, GF2X_c *a, GF2X_c *b, parent) except -2:
     161cdef inline int celement_add(GF2X_c *res, GF2X_c *a, GF2X_c *b, long parent) except -2:
    162162    """
    163163    EXAMPLE:
    164164        sage: P.<x> = GF(2)[]
     
    167167    """
    168168    GF2X_add(res[0], a[0], b[0])
    169169
    170 cdef inline int celement_sub(GF2X_c* res, GF2X_c* a, GF2X_c* b, parent) except -2:
     170cdef inline int celement_sub(GF2X_c* res, GF2X_c* a, GF2X_c* b, long parent) except -2:
    171171    """
    172172    EXAMPLE:
    173173        sage: P.<x> = GF(2)[]
     
    176176    """
    177177    GF2X_sub(res[0], a[0], b[0])
    178178
    179 cdef inline int celement_neg(GF2X_c* res, GF2X_c* a, parent) except -2:
     179cdef inline int celement_neg(GF2X_c* res, GF2X_c* a, long parent) except -2:
    180180    """
    181181    EXAMPLE:
    182182        sage: P.<x> = GF(2)[]
     
    185185    """
    186186    res[0] = a[0]
    187187
    188 cdef inline int celement_mul(GF2X_c* res, GF2X_c* a, GF2X_c* b, parent) except -2:
     188cdef inline int celement_mul(GF2X_c* res, GF2X_c* a, GF2X_c* b, long parent) except -2:
    189189    """
    190190    EXAMPLE:
    191191        sage: P.<x> = GF(2)[]
     
    194194    """
    195195    GF2X_mul(res[0], a[0], b[0])
    196196
    197 cdef inline int celement_div(GF2X_c* res, GF2X_c* a, GF2X_c* b, parent) except -2:
     197cdef inline int celement_div(GF2X_c* res, GF2X_c* a, GF2X_c* b, long parent) except -2:
    198198    """
    199199    EXAMPLE:
    200200        sage: P.<x> = GF(2)[]
    201201    """
    202202    return GF2X_divide(res[0], a[0], b[0])
    203203
    204 cdef inline int celement_floordiv(GF2X_c* res, GF2X_c* a, GF2X_c* b, parent) except -2:
     204cdef inline int celement_floordiv(GF2X_c* res, GF2X_c* a, GF2X_c* b, long parent) except -2:
    205205    """
    206206    EXAMPLE:
    207207        sage: P.<x> = GF(2)[]
     
    212212    """
    213213    GF2X_div(res[0], a[0], b[0])
    214214
    215 cdef inline int celement_mod(GF2X_c* res, GF2X_c* a, GF2X_c* b, parent) except -2:
     215cdef inline int celement_mod(GF2X_c* res, GF2X_c* a, GF2X_c* b, long parent) except -2:
    216216    """
    217217    EXAMPLE:
    218218        sage: P.<x> = GF(2)[]
     
    221221    """
    222222    GF2X_rem(res[0], a[0], b[0])
    223223
    224 cdef inline int celement_quorem(GF2X_c* q, GF2X_c* r, GF2X_c* a, GF2X_c* b, parent) except -2:
     224cdef inline int celement_quorem(GF2X_c* q, GF2X_c* r, GF2X_c* a, GF2X_c* b, long parent) except -2:
    225225    """
    226226    EXAMPLE:
    227227        sage: P.<x> = GF(2)[]
     
    231231    """
    232232    GF2X_DivRem(q[0], r[0], a[0], b[0])
    233233
    234 cdef inline int celement_inv(GF2X_c* res, GF2X_c* a, parent) except -2:
     234cdef inline int celement_inv(GF2X_c* res, GF2X_c* a, long parent) except -2:
    235235    """
    236236    We ignore NTL here and use the fraction field constructor.
    237237
     
    240240    """
    241241    raise NotImplementedError
    242242
    243 cdef inline int celement_pow(GF2X_c* res, GF2X_c* x, long e, GF2X_c *modulus, parent) except -2:
     243cdef inline int celement_pow(GF2X_c* res, GF2X_c* x, long e, GF2X_c *modulus, long parent) except -2:
    244244    """
    245245    EXAMPLE:
    246246        sage: P.<x> = GF(2)[]
     
    275275        GF2X_PowerMod_long_pre(res[0], x[0], e, mod)
    276276        if do_sig: _sig_off
    277277
    278 cdef inline int celement_gcd(GF2X_c* res, GF2X_c* a, GF2X_c *b, parent) except -2:
     278cdef inline int celement_gcd(GF2X_c* res, GF2X_c* a, GF2X_c *b, long parent) except -2:
    279279    """
    280280    EXAMPLE:
    281281        sage: P.<x> = GF(2)[]
  • sage/rings/polynomial/polynomial_gf2x.pyx

    diff -r bd3e75c83235 -r 49ef6eab9e04 sage/rings/polynomial/polynomial_gf2x.pyx
    a b  
    55    -- Martin Albrecht (2008-10) initial implementation
    66"""
    77
     8
     9# We need to define this stuff before including the templating stuff
     10# to make sure the function get_cparent is found since it is used in
     11# 'polynomial_template.pxi'.
     12
     13ctypedef long cparent
     14cdef cparent get_cparent(parent):
     15    return 0
     16
     17# first we include the definitions
    818include "../../libs/ntl/ntl_GF2X_linkage.pxi"
    919
     20# and then the interface
    1021include "polynomial_template.pxi"
    1122
    1223from sage.libs.all import pari
    13 
    14 #ctypedef unsigned long long word "word"
    1524
    1625from sage.matrix.matrix_mod2_dense cimport mzd_write_bit, mzd_read_bit, Matrix_mod2_dense, word
    1726
  • sage/rings/polynomial/polynomial_template.pxi

    diff -r bd3e75c83235 -r 49ef6eab9e04 sage/rings/polynomial/polynomial_template.pxi
    a b  
    5555            sage: P(map(GF(2),[1,0,1]))
    5656            x^2 + 1
    5757        """
    58         cdef celement gen, monomial, coeff
     58        cdef celement gen, monomial, *coeff
    5959        cdef Py_ssize_t deg
     60        cdef cparent _parent
    6061
    6162        Polynomial.__init__(self, parent, is_gen=is_gen)
    6263
     64        celement_construct(&self.x, get_cparent(parent))
     65
    6366        if is_gen:
    64             celement_gen(&self.x, 0, parent)
     67            celement_gen(&self.x, 0, get_cparent(parent))
    6568
    6669        elif PY_TYPE_CHECK(x, Polynomial_template):
    6770            try:
    68                 celement_set(&self.x, &(<Polynomial_template>x).x, parent)
     71                celement_set(&self.x, &(<Polynomial_template>x).x, get_cparent(parent))
    6972            except NotImplementedError:
    7073                raise TypeError("%s not understood."%x)
    7174
    7275        elif PY_TYPE_CHECK(x, int) or PY_TYPE_CHECK(x, Integer):
    7376            try:
    74                 celement_set_si(&self.x, int(x), parent)
     77                celement_set_si(&self.x, int(x), get_cparent(parent))
    7578            except NotImplementedError:
    7679                raise TypeError("%s not understood."%x)
    7780
    7881        elif PY_TYPE_CHECK(x, list) or PY_TYPE_CHECK(x, tuple):
    7982            parent = (<Polynomial_template>self)._parent
     83            _parent = get_cparent(parent)
    8084
    81             celement_set_si(&self.x, 0, parent)
    82             celement_gen(&gen, 0, parent)
     85            celement_set_si(&self.x, 0, _parent)
     86            celement_gen(&gen, 0, _parent)
    8387
    8488            deg = 0
    8589            for e in x:
    8690                # r += parent(e)*power
    87                 celement_pow(&monomial, &gen, deg, NULL, parent)
    88                 coeff = (<Polynomial_template>parent(e)).x
    89                 celement_mul(&monomial, &coeff, &monomial, parent)
    90                 celement_add(&self.x, &self.x, &monomial, parent)
     91                celement_pow(&monomial, &gen, deg, NULL, _parent)
     92                coeff = &(<Polynomial_template>parent(e)).x
     93                celement_mul(&monomial, coeff, &monomial, _parent)
     94                celement_add(&self.x, &self.x, &monomial, _parent)
    9195                deg += 1
    9296
    9397        elif PY_TYPE_CHECK(x, pari_gen):
     
    120124            [0, 1]
    121125        """
    122126        cdef Py_ssize_t i
    123         return [self[i] for i in range(celement_len(&self.x,(<Polynomial_template>self)._parent))]
     127        cdef cparent _parent = get_cparent((<Polynomial_template>self)._parent)
     128        return [self[i] for i in range(celement_len(&self.x, _parent))]
    124129
    125     def __cinit__(self):
    126         """
    127         EXAMPLE:
    128             sage: P.<x> = GF(2)[]
    129         """
    130         celement_construct(&self.x, (<Polynomial_template>self)._parent)
     130#     def __cinit__(self):
     131#         """
     132#         EXAMPLE:
     133#             sage: P.<x> = GF(2)[]
     134#         """
     135#         self.x = <celement>NULL
    131136
    132137    def __dealloc__(self):
    133138        """
     
    135140            sage: P.<x> = GF(2)[]
    136141            sage: del x
    137142        """
    138         celement_destruct(&self.x, (<Polynomial_template>self)._parent)
     143        celement_destruct(&self.x, get_cparent((<Polynomial_template>self)._parent))
    139144
    140145    cpdef ModuleElement _add_(self, ModuleElement right):
    141146        """
     
    146151        """
    147152        cdef Polynomial_template r = <Polynomial_template>PY_NEW(self.__class__)
    148153        r._parent = (<Polynomial_template>self)._parent
    149         celement_add(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._parent)
     154        celement_add(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, get_cparent((<Polynomial_template>self)._parent))
    150155        return r
    151156
    152157    cpdef ModuleElement _sub_(self, ModuleElement right):
     
    158163        """
    159164        cdef Polynomial_template r = <Polynomial_template>PY_NEW(self.__class__)
    160165        r._parent = (<Polynomial_template>self)._parent
    161         celement_add(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._parent)
     166        celement_add(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, get_cparent((<Polynomial_template>self)._parent))
    162167        return r
    163168
    164169    def __neg__(self):
     
    170175        """
    171176        cdef Polynomial_template r = <Polynomial_template>PY_NEW(self.__class__)
    172177        r._parent = (<Polynomial_template>self)._parent
    173         celement_neg(&r.x, &self.x, (<Polynomial_template>self)._parent)
     178        celement_neg(&r.x, &self.x, get_cparent((<Polynomial_template>self)._parent))
    174179        return r
    175180
    176181    cpdef RingElement _mul_(self, RingElement right):
     
    182187        """
    183188        cdef Polynomial_template r = <Polynomial_template>PY_NEW(self.__class__)
    184189        r._parent = (<Polynomial_template>self)._parent
    185         celement_mul(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._parent)
     190        celement_mul(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, get_cparent((<Polynomial_template>self)._parent))
    186191        return r
    187192
    188193    def gcd(self, Polynomial_template other):
     
    197202        """
    198203        cdef Polynomial_template r = <Polynomial_template>PY_NEW(self.__class__)
    199204        r._parent = (<Polynomial_template>self)._parent
    200         celement_gcd(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>other).x, (<Polynomial_template>self)._parent)
     205        celement_gcd(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>other).x, get_cparent((<Polynomial_template>self)._parent))
    201206        return r
    202207
    203208    def __floordiv__(self, right):
     
    212217        right = (<Polynomial_template>self)._parent._coerce_(right)
    213218        cdef Polynomial_template r = <Polynomial_template>PY_NEW(self.__class__)
    214219        r._parent = (<Polynomial_template>self)._parent
    215         celement_floordiv(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._parent)
     220        celement_floordiv(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, get_cparent((<Polynomial_template>self)._parent))
    216221        return r
    217222
    218223    def __mod__(self, other):
     
    225230        other = (<Polynomial_template>self)._parent._coerce_(other)
    226231        cdef Polynomial_template r = <Polynomial_template>PY_NEW(self.__class__)
    227232        r._parent = (<Polynomial_template>self)._parent
    228         celement_mod(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>other).x, (<Polynomial_template>self)._parent)
     233        celement_mod(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>other).x, get_cparent((<Polynomial_template>self)._parent))
    229234        return r
    230235
    231236    def quo_rem(self, right):
     
    241246        q._parent = (<Polynomial_template>self)._parent
    242247        cdef Polynomial_template r = <Polynomial_template>PY_NEW(self.__class__)
    243248        r._parent = (<Polynomial_template>self)._parent
    244         celement_quorem(&q.x, &r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._parent)
     249        celement_quorem(&q.x, &r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, get_cparent((<Polynomial_template>self)._parent))
    245250        return q,r
    246251       
    247252    def __long__(self):
     
    256261            sage: int(P(1))
    257262            1
    258263        """
    259         if celement_len(&self.x, (<Polynomial_template>self)._parent) > 1:
     264        if celement_len(&self.x, get_cparent((<Polynomial_template>self)._parent)) > 1:
    260265            raise ValueError("Cannot coerce polynomial with degree %d to integer."%(self.degree()))
    261266        return int(self[0])
    262267
     
    269274            sage: bool(P(0)), P(0).is_zero()
    270275            (False, True)
    271276        """
    272         return not celement_is_zero(&self.x, (<Polynomial_template>self)._parent)
     277        return not celement_is_zero(&self.x, get_cparent((<Polynomial_template>self)._parent))
    273278
    274279    def __richcmp__(left, right, int op):
    275280        """
     
    289294        EXAMPLE:
    290295            sage: P.<x> = GF(2)[]
    291296        """
    292         return celement_cmp(&(<Polynomial_template>left).x, &(<Polynomial_template>right).x, (<Polynomial_template>left)._parent)
     297        return celement_cmp(&(<Polynomial_template>left).x, &(<Polynomial_template>right).x, get_cparent((<Polynomial_template>left)._parent))
    293298
    294299    def __hash__(self):
    295300        """
     
    298303            sage: {x:1}
    299304            {x: 1}
    300305        """
    301         return celement_hash(&self.x, (<Polynomial_template>self)._parent)
     306        return celement_hash(&self.x, get_cparent((<Polynomial_template>self)._parent))
    302307
    303308    def __len__(self):
    304309        """
     
    310315            sage: len(x+1)
    311316            2
    312317        """
    313         return celement_len(&self.x, (<Polynomial_template>self)._parent)
     318        return celement_len(&self.x, get_cparent((<Polynomial_template>self)._parent))
    314319
    315320    def __pow__(self, ee, modulus):
    316321        """
     
    346351        r._parent = (<Polynomial_template>self)._parent
    347352
    348353        if modulus is None:
    349             celement_pow(&r.x, &(<Polynomial_template>self).x, e, NULL, (<Polynomial_template>self)._parent)
     354            celement_pow(&r.x, &(<Polynomial_template>self).x, e, NULL, get_cparent((<Polynomial_template>self)._parent))
    350355        else:
    351356            modulus = (<Polynomial_template>self)._parent._coerce_(modulus)
    352             celement_pow(&r.x, &(<Polynomial_template>self).x, e, &(<Polynomial_template>modulus).x, (<Polynomial_template>self)._parent)
     357            celement_pow(&r.x, &(<Polynomial_template>self).x, e, &(<Polynomial_template>modulus).x, get_cparent((<Polynomial_template>self)._parent))
    353358        if recip:
    354359            return ~r
    355360        else:
     
    365370            False
    366371        """
    367372        cdef celement gen
    368         celement_gen(&gen, 0, (<Polynomial_template>self)._parent)
    369         return celement_equal(&self.x, &gen, (<Polynomial_template>self)._parent)
     373        celement_gen(&gen, 0, get_cparent((<Polynomial_template>self)._parent))
     374        return celement_equal(&self.x, &gen, get_cparent((<Polynomial_template>self)._parent))
    370375
    371376    def shift(self, int n):
    372377        """
     
    384389            return self
    385390
    386391        parent = (<Polynomial_template>self)._parent
    387         celement_gen(&gen, 0, parent)
    388         celement_pow(&gen, &gen, abs(n), NULL, parent)
     392        cdef cparent _parent = get_cparent(parent)
     393        celement_gen(&gen, 0, _parent)
     394        celement_pow(&gen, &gen, abs(n), NULL, _parent)
    389395        r = <Polynomial_template>PY_NEW(self.__class__)
    390396        r._parent = parent
    391397       
    392398        if n > 0:
    393             celement_mul(&r.x, &self.x, &gen, parent)
     399            celement_mul(&r.x, &self.x, &gen, _parent)
    394400        else:
    395             celement_floordiv(&r.x, &self.x, &gen, parent)
     401            celement_floordiv(&r.x, &self.x, &gen, _parent)
    396402        return r
    397403
    398404    def __lshift__(self, int n):
     
    413419            return self >> -n
    414420
    415421        parent = (<Polynomial_template>self)._parent
    416         celement_gen(&gen, 0, parent)
    417         celement_pow(&gen, &gen, n, NULL, parent)
     422        cdef cparent _parent = get_cparent(parent)
     423        celement_gen(&gen, 0, _parent)
     424        celement_pow(&gen, &gen, n, NULL, _parent)
    418425        r = <Polynomial_template>PY_NEW(self.__class__)
    419426        r._parent = parent
    420         celement_mul(&r.x, &(<Polynomial_template>self).x, &gen, parent)
     427        celement_mul(&r.x, &(<Polynomial_template>self).x, &gen, _parent)
    421428        return r
    422429
    423430    def __rshift__(self, int n):
     
    435442            return self >> -n
    436443
    437444        parent = (<Polynomial_template>self)._parent
    438         celement_gen(&gen, 0, parent)
    439         celement_pow(&gen, &gen, n, NULL, parent)
     445        cdef cparent _parent = get_cparent(parent)
     446        celement_gen(&gen, 0, _parent)
     447        celement_pow(&gen, &gen, n, NULL, _parent)
    440448        r = <Polynomial_template>PY_NEW(self.__class__)
    441449        r._parent = parent
    442450       
    443         celement_floordiv(&r.x, &(<Polynomial_template>self).x, &gen, parent)
     451        celement_floordiv(&r.x, &(<Polynomial_template>self).x, &gen, _parent)
    444452        return r
    445453
    446454    def is_zero(self):
     
    448456        EXAMPLE:
    449457            sage: P.<x> = GF(2)[]
    450458        """
    451         return celement_is_zero(&self.x, (<Polynomial_template>self)._parent)
     459        return celement_is_zero(&self.x, get_cparent((<Polynomial_template>self)._parent))
    452460
    453461    def is_one(self):
    454462        """
    455463        EXAMPLE:
    456464            sage: P.<x> = GF(2)[]
    457465        """
    458         return celement_is_one(&self.x, (<Polynomial_template>self)._parent)
     466        return celement_is_one(&self.x, get_cparent((<Polynomial_template>self)._parent))
    459467
    460468    def degree(self):
    461469        """
    462470        EXAMPLE:
    463471            sage: P.<x> = GF(2)[]
    464472        """
    465         return Integer(celement_len(&self.x, (<Polynomial_template>self)._parent)-1)
     473        return Integer(celement_len(&self.x, get_cparent((<Polynomial_template>self)._parent))-1)
    466474
    467475    cpdef Polynomial truncate(self, long n):
    468476        """
     
    478486        if n < 0:
    479487            raise ValueError(" n must be >= 0.")
    480488        parent = (<Polynomial_template>self)._parent
     489        cdef cparent _parent = get_cparent(parent)
    481490        cdef celement gen
    482         celement_gen(&gen, 0, parent)
    483         celement_pow(&gen, &gen, n, NULL, parent)
     491        celement_gen(&gen, 0, _parent)
     492        celement_pow(&gen, &gen, n, NULL, _parent)
    484493
    485494        cdef Polynomial_template r = <Polynomial_template>PY_NEW(self.__class__)
    486495        r._parent = parent
    487         celement_mod(&r.x, &self.x, &gen, parent)
     496        celement_mod(&r.x, &self.x, &gen, _parent)
    488497        return r