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 828 828 829 829 GF2X_c GF2XModulus_GF2X "GF2X" (GF2XModulus_c m) 830 830 831 GF2X_c GF2X_IrredPolyMod "IrredPolyMod" (GF2X_c g, GF2XModulus_c F) 832 831 833 #### GF2EContext_c 832 834 833 835 ctypedef struct GF2EContext_c "struct GF2EContext": -
a/sage/rings/finite_field_ntl_gf2e.pyx
old new 31 31 include "../ext/stdsage.pxi" 32 32 33 33 from sage.structure.sage_object cimport SageObject 34 35 34 36 35 from sage.structure.parent cimport Parent 37 36 from sage.structure.parent_base cimport ParentWithBase … … 67 66 cdef object Polynomial 68 67 cdef object FreeModuleElement 69 68 cdef object GF 69 cdef object GF2, GF2_0, GF2_1 70 70 71 71 cdef void late_import(): 72 72 """ … … 84 84 MPolynomial, \ 85 85 Polynomial, \ 86 86 FreeModuleElement, \ 87 GF 87 GF, \ 88 GF2, GF2_0, GF2_1 88 89 89 90 if is_IntegerMod is not None: 90 91 return … … 121 122 122 123 import sage.rings.finite_field 123 124 GF = sage.rings.finite_field.FiniteField 125 GF2 = GF(2) 126 GF2_0 = GF2(0) 127 GF2_1 = GF2(1) 124 128 125 129 cdef extern from "arpa/inet.h": 126 130 unsigned int htonl(unsigned int) … … 337 341 if e.parent() == self: 338 342 res.x = (<FiniteField_ntl_gf2eElement>e).x 339 343 return res 340 if e.parent() is GF (2) or e.parent() == GF(2):344 if e.parent() is GF2 or e.parent() == GF2: 341 345 GF2E_conv_long(res.x,int(e)) 342 346 return res 343 347 … … 466 470 sage: F.prime_subfield() 467 471 Finite Field of size 2 468 472 """ 469 return GF (2)470 473 return GF2 474 471 475 def fetch_int(FiniteField_ntl_gf2e self, number): 472 476 r""" 473 477 Given an integer $n$ return a finite field element in self … … 1108 1112 for i from 0 <= i <= GF2X_deg(r): 1109 1113 C.append(GF2_conv_to_long(GF2X_coeff(r,i))) 1110 1114 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)) 1111 1180 1112 1181 def _finite_field_ext_pari_element(FiniteField_ntl_gf2eElement self, k=None): 1113 1182 r""" -
a/sage/rings/polynomial/polynomial_element.pyx
old new 3909 3909 3910 3910 return RR(sum([abs(i)**p for i in coeffs]))**(1/p) 3911 3911 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 3912 3938 # ----------------- inner functions ------------- 3913 3939 # Sagex can't handle function definitions inside other function 3914 3940