Path: blob/master/src/sage/libs/ntl/ntl_GF2EContext.pyx
8815 views
#*****************************************************************************1# Copyright (C) 2005 William Stein <[email protected]>2#3# Distributed under the terms of the GNU General Public License (GPL)4#5# This code is distributed in the hope that it will be useful,6# but WITHOUT ANY WARRANTY; without even the implied warranty of7# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU8# General Public License for more details.9#10# The full text of the GPL is available at:11#12# http://www.gnu.org/licenses/13#*****************************************************************************1415include "sage/ext/interrupt.pxi"16include "sage/ext/stdsage.pxi"17include 'misc.pxi'18include 'decl.pxi'19import weakref2021GF2EContextDict = {}222324cdef class ntl_GF2EContext_class:25def __init__(self, ntl_GF2X v):26"""27EXAMPLES:28# You can construct contexts manually.29sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1]))30sage: n1 = ntl.GF2E([1,1],ctx)31sage: n132[1 1]3334# or You can construct contexts implicitly.35sage: n2 = ntl.GF2E([0,1], ntl.GF2X([1,1,0,1]))36sage: n237[0 1]38sage: ntl.GF2E(2, GF(2^8,'a'))+ntl.GF2E([0,1],ctx)39Traceback (most recent call last):40...41ValueError: You can not perform arithmetic with elements in different fields.4243sage: n2+n1 # Mismatched moduli: It will go BOOM!44[1]45"""46pass4748def __cinit__(self, ntl_GF2X v):49GF2EContext_construct_GF2X(&self.x, &((<ntl_GF2X>v).x))50self.m = v5152def __dealloc__(self):53GF2EContext_destruct(&self.x)5455def __reduce__(self):56"""57EXAMPLES:58sage: c = ntl.GF2EContext(GF(2^5,'b'))59sage: loads(dumps(c)) is c60True61"""62return ntl_GF2EContext, (self.m,)6364def __repr__(self):65"""66Returns a print representation of self.6768EXAMPLES:69sage: c = ntl.GF2EContext(GF(2^16,'a'))70sage: c71NTL modulus [1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1]72"""73return "NTL modulus %s"%(self.m)7475def modulus(self):76"""77Return the current modulus associated to this78context.7980EXAMPLES:81sage: c = ntl.GF2EContext(GF(2^7,'foo'))82sage: c.modulus()83[1 1 0 0 0 0 0 1]84"""85return self.m868788def restore(self):89"""90EXAMPLES:91sage: c1 = ntl.GF2E([0,1],GF(2^4,'a')) ; c2 = ntl.GF2E([1,0,1],GF(2^4,'a'))92sage: c1+c293[1 1 1]94sage: d1 = ntl.GF2E([0,1],GF(2^5,'a')) ; d2 = ntl.GF2E([0,0,1],GF(2^5,'a'))95sage: d1*d2 ## indirect doctest96[0 0 0 1]97"""98self.restore_c()99100cdef void restore_c(self):101self.x.restore()102103def ntl_GF2EContext( v ):104"""105Create a new GF2EContext.106EXAMPLES:107sage: c = ntl.GF2EContext(GF(2^2,'a'))108sage: n1 = ntl.GF2E([0,1],c)109sage: n1110[0 1]111"""112v = ntl_GF2X(v)113if (GF2X_deg((<ntl_GF2X>v).x) < 1):114raise ValueError, "%s is not a valid modulus."%v115key = hash(v)116if GF2EContextDict.has_key(key):117context = GF2EContextDict[key]()118if context is not None:119return context120context = ntl_GF2EContext_class(v)121GF2EContextDict[key] = weakref.ref(context)122return context123124125