Ticket #3634: 3634-gf2e-minpoly.patch

File 3634-gf2e-minpoly.patch, 6.0 kB (added by robertwb, 5 months ago)
  • a/sage/libs/ntl/decl.pxi

    old new  
    828828 
    829829    GF2X_c GF2XModulus_GF2X "GF2X" (GF2XModulus_c m) 
    830830 
     831    GF2X_c GF2X_IrredPolyMod "IrredPolyMod" (GF2X_c g, GF2XModulus_c F) 
     832 
    831833    #### GF2EContext_c 
    832834 
    833835    ctypedef struct GF2EContext_c "struct GF2EContext": 
  • a/sage/rings/finite_field_ntl_gf2e.pyx

    old new  
    3131include "../ext/stdsage.pxi" 
    3232 
    3333from sage.structure.sage_object cimport SageObject 
    34  
    3534 
    3635from sage.structure.parent  cimport Parent 
    3736from sage.structure.parent_base cimport ParentWithBase 
     
    6766cdef object Polynomial 
    6867cdef object FreeModuleElement 
    6968cdef object GF 
     69cdef object GF2, GF2_0, GF2_1 
    7070 
    7171cdef void late_import(): 
    7272    """ 
     
    8484           MPolynomial, \ 
    8585           Polynomial, \ 
    8686           FreeModuleElement, \ 
    87            GF 
     87           GF, \ 
     88           GF2, GF2_0, GF2_1 
    8889 
    8990    if is_IntegerMod is not None: 
    9091        return 
     
    121122 
    122123    import sage.rings.finite_field 
    123124    GF = sage.rings.finite_field.FiniteField 
     125    GF2 = GF(2) 
     126    GF2_0 = GF2(0) 
     127    GF2_1 = GF2(1) 
    124128 
    125129cdef extern from "arpa/inet.h": 
    126130    unsigned int htonl(unsigned int) 
     
    337341            if e.parent() == self: 
    338342                res.x = (<FiniteField_ntl_gf2eElement>e).x 
    339343                return res 
    340             if e.parent() is GF(2) or e.parent() == GF(2)
     344            if e.parent() is GF2 or e.parent() == GF2
    341345                GF2E_conv_long(res.x,int(e)) 
    342346                return res 
    343347 
     
    466470            sage: F.prime_subfield() 
    467471            Finite Field of size 2 
    468472        """ 
    469         return GF(2) 
    470  
     473        return GF2 
     474     
    471475    def fetch_int(FiniteField_ntl_gf2e self, number): 
    472476        r""" 
    473477        Given an integer $n$ return a finite field element in self 
     
    11081112        for i from 0 <= i <= GF2X_deg(r): 
    11091113            C.append(GF2_conv_to_long(GF2X_coeff(r,i))) 
    11101114        return self._parent.polynomial_ring(name)(C) 
     1115 
     1116    def minpoly(self, var='x'): 
     1117        """ 
     1118        Return the minimal polynomial of self, which is 
     1119        the smallest degree polynomial $f \in \F_{2}[x]$ 
     1120        such that $f(self) = 0$.  
     1121         
     1122        EXAMPLES: 
     1123            sage: K.<a> = GF(2^100) 
     1124            sage: f = a.minpoly(); f 
     1125            x^100 + x^57 + x^56 + x^55 + x^52 + x^48 + x^47 + x^46 + x^45 + x^44 + x^43 + x^41 + x^37 + x^36 + x^35 + x^34 + x^31 + x^30 + x^27 + x^25 + x^24 + x^22 + x^20 + x^19 + x^16 + x^15 + x^11 + x^9 + x^8 + x^6 + x^5 + x^3 + 1 
     1126            sage: f(a) 
     1127            0 
     1128            sage: g = K.random_element() 
     1129            sage: g.minpoly()(g) 
     1130            0 
     1131        """ 
     1132        cdef GF2X_c r = GF2X_IrredPolyMod(GF2E_rep(self.x), GF2E_modulus()) 
     1133        cdef int i 
     1134        C = [] 
     1135        for i from 0 <= i <= GF2X_deg(r): 
     1136            C.append(GF2_conv_to_long(GF2X_coeff(r,i))) 
     1137        return self._parent.polynomial_ring(var)(C) 
     1138 
     1139    def trace(self): 
     1140        """ 
     1141        Return the trace of self. 
     1142 
     1143        EXAMPLES: 
     1144            sage: K.<a> = GF(2^25) 
     1145            sage: a.trace() 
     1146            0 
     1147            sage: a.charpoly() 
     1148            x^25 + x^8 + x^6 + x^2 + 1 
     1149            sage: parent(a.trace()) 
     1150            Finite Field of size 2 
     1151             
     1152            sage: b = a+1 
     1153            sage: b.trace() 
     1154            1 
     1155            sage: b.charpoly()[1] 
     1156            1 
     1157        """ 
     1158        if GF2_IsOne(GF2E_trace(self.x)): 
     1159            return GF2_1 
     1160        else: 
     1161            return GF2_0 
     1162 
     1163    def weight(self): 
     1164        """ 
     1165        Returns the number of non-zero coefficients in the polynomial 
     1166        representation of self. 
     1167 
     1168        EXAMPLES: 
     1169            sage: K.<a> = GF(2^21) 
     1170            sage: a.weight() 
     1171            1 
     1172            sage: (a^5+a^2+1).weight() 
     1173            3 
     1174            sage: b = 1/(a+1); b 
     1175            a^20 + a^19 + a^18 + a^17 + a^16 + a^15 + a^14 + a^13 + a^12 + a^11 + a^10 + a^9 + a^8 + a^7 + a^6 + a^4 + a^3 + a^2  
     1176            sage: b.weight() 
     1177            18 
     1178        """ 
     1179        return GF2X_weight(GF2E_rep(self.x)) 
    11111180             
    11121181    def _finite_field_ext_pari_element(FiniteField_ntl_gf2eElement self, k=None): 
    11131182        r""" 
  • a/sage/rings/polynomial/polynomial_element.pyx

    old new  
    39093909         
    39103910        return RR(sum([abs(i)**p for i in coeffs]))**(1/p) 
    39113911 
     3912    def hamming_weight(self): 
     3913        """ 
     3914        Returns the number of non-zero coefficients of self. 
     3915 
     3916        EXAMPLES: 
     3917            sage: R.<x> = ZZ[] 
     3918            sage: f = x^3 - x 
     3919            sage: f.hamming_weight() 
     3920            2 
     3921            sage: R(0).hamming_weight() 
     3922            0 
     3923            sage: f = (x+1)^100 
     3924            sage: f.hamming_weight() 
     3925            101 
     3926            sage: S = GF(5)['y'] 
     3927            sage: S(f).hamming_weight() 
     3928            5 
     3929            sage: cyclotomic_polynomial(105).hamming_weight() 
     3930            33    
     3931        """ 
     3932        cdef long w = 0 
     3933        for a in self.coeffs(): 
     3934            if a: 
     3935                w += 1 
     3936        return w 
     3937 
    39123938# ----------------- inner functions ------------- 
    39133939# Sagex can't handle function definitions inside other function 
    39143940