Path: blob/master/src/sage/libs/ntl/ntl_ZZ_pEContext.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'1920ZZ_pEContextDict = {}2122from sage.libs.ntl.ntl_ZZ_pX cimport ntl_ZZ_pX23from sage.libs.ntl.ntl_ZZ_pContext import ntl_ZZ_pContext24from sage.libs.ntl.ntl_ZZ cimport ntl_ZZ2526cdef class ntl_ZZ_pEContext_class:27def __init__(self, ntl_ZZ_pX f):28"""29EXAMPLES:30# You can construct contexts manually.31sage: c=ntl.ZZ_pEContext(ntl.ZZ_pX([4,1,6],25))32sage: n1=c.ZZ_pE([10,17,12])33sage: n134[2 15]3536# or You can construct contexts implicitly.37sage: n2=ntl.ZZ_pE(12, ntl.ZZ_pX([1,1,1],7))38sage: n239[5]40sage: n2+n1 # Mismatched moduli: It will go BOOM!41Traceback (most recent call last):42...43ValueError: You can not perform arithmetic with elements of different moduli.44"""45pass4647def __cinit__(self, ntl_ZZ_pX f):48self.pc = f.c49self.pc.restore_c()50ZZ_pEContext_construct_ZZ_pX(&self.x, &f.x)51ZZ_pEContextDict[(repr(f),repr(f.c.p))] = self52self.f = f5354def __dealloc__(self):55ZZ_pEContext_destruct(&self.x)5657def __reduce__(self):58"""59sage: c=ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1],7))60sage: loads(dumps(c)) is c61True62"""63return ntl_ZZ_pEContext, (self.f,)6465def __repr__(self):66"""67Returns a string representation of self.6869EXAMPLES:70sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7)); c71NTL modulus [1 1 1] (mod 7)72"""73return "NTL modulus %s (mod %s)"%(self.f, self.pc.p)7475def get_pc(self):76"""77Returns the ZZ_pContext contained within self.7879EXAMPLES:80sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7)); c81NTL modulus [1 1 1] (mod 7)82sage: c.get_pc()83NTL modulus 784"""85return self.pc8687def polynomial(self):88"""89Returns the ZZ_pX polynomial defining self.9091EXAMPLES:92sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7))93sage: c.polynomial()94[1 1 1]95"""96return self.f9798def restore(self):99"""100Manually sets the global NTL modulus to be self.101102This should be done automatically by all of the NTL wrapper classes.103104CRUCIAL: If you are writing your own classes that use ZZ_p_c, ZZ_pX_c, ZZ_pE_c, ZZ_pEX_c105then you MUST restore the context before calling off to NTL for anything. If the context has been106switched by other code then behavior of operations is undefined. See the NTL documentation for107more details (or the wrappers in sage.libs.ntl)108"""109self.restore_c()110111cdef void restore_c(self):112"""113Sets the global NTL modulus to be self.114115CRUCIAL: If you are writing your own classes that use ZZ_p_c, ZZ_pX_c, ZZ_pE_c, ZZ_pEX_c116then you MUST restore the context before calling off to NTL for anything. If the context has been117switched by other code then behavior of operations is undefined. See the NTL documentation for118more details (or the wrappers in sage.libs.ntl)119"""120self.pc.restore_c()121ZZ_pEContext_restore(&self.x)122123#def ZZ_pX(self,v = None):124# from ntl_ZZ_pX import ntl_ZZ_pX125# return ntl_ZZ_pX(v,modulus=self)126127def ZZ_pE(self,v = None):128"""129Returns a ZZ_pE object with modulus self out of the data v.130131EXAMPLES:132sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7))133sage: c.ZZ_pE([4,3])134[4 3]135"""136from ntl_ZZ_pE import ntl_ZZ_pE137return ntl_ZZ_pE(v,modulus=self)138139def ZZ_pEX(self, v = None):140"""141Returns a ZZ_pE object with modulus self out of the data v.142143EXAMPLES:144sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7))145sage: c.ZZ_pEX([4,3])146[[4] [3]]147"""148from ntl_ZZ_pEX import ntl_ZZ_pEX149return ntl_ZZ_pEX(v, modulus=self)150151def ntl_ZZ_pEContext( ntl_ZZ_pX f):152"""153Creates an ntl_ZZ_pEContext.154155Such an object must be created before any ZZ_pE or ZZ_pEX objects can be used.156157The context handling should be taken care of by the wrapper classes.158EXAMPLES:159sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7)); c160NTL modulus [1 1 1] (mod 7)161"""162try:163return ZZ_pEContextDict[repr(f), repr(f.c.p)]164except KeyError:165# Creating the following object caches it.166return ntl_ZZ_pEContext_class(f)167168169