Path: blob/master/src/sage/libs/ntl/ntl_lzz_pContext.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'1920from sage.rings.integer import Integer21from sage.rings.integer cimport Integer2223zz_pContextDict = {}2425cdef class ntl_zz_pContext_class:26def __init__(self, long v):27"""28EXAMPLES:29# You can construct contexts manually.30sage: c = ntl.zz_pContext(11)31sage: n1 = ntl.zz_p(12,c)32sage: n13313435# or You can construct contexts implicitly.36sage: n2=ntl.zz_p(12, 7)37sage: n238539sage: ntl.zz_p(2,3)+ntl.zz_p(1,3)40041sage: n2+n1 # Mismatched moduli: It will go BOOM!42Traceback (most recent call last):43...44ValueError: arithmetic operands must have the same modulus.45"""46pass4748def __cinit__(self, long v):49if v > NTL_SP_BOUND:50raise ValueError, "Modulus (=%s) is too big"%v51zz_pContext_construct_long(&self.x, v)52zz_pContextDict[repr(v)] = self53self.p = v5455def __dealloc__(self):56zz_pContext_destruct(&self.x)5758def __reduce__(self):59"""60sage: c=ntl.zz_pContext(13)61sage: loads(dumps(c)) is c62True63"""64return ntl_zz_pContext, (self.p,)6566def modulus(self):67"""68Print the modulus for self.6970EXAMPLES:71sage: c1 = ntl.zz_pContext(36)72sage: c1.modulus()733674"""75return self.p7677def restore(self):78"""79Restore a zz_pContext.8081EXAMPLES:82sage: c = ntl.zz_pContext(5)83sage: m = ntl.zz_p(4,7)84sage: c.restore()85"""86self.restore_c()8788cdef void restore_c(self):89"""90Actual code for the above.9192EXAMPLES:93sage: n = ntl.zz_p(3,5)94sage: m = ntl.zz_p(4,7)95sage: n*n ## indirect doctest96497"""98zz_pContext_restore(&self.x)99100def ntl_zz_pContext( v ):101"""102Creation function for a zz_p context.103104EXAMPLES:105sage: f = ntl.zz_pContext(26)106sage: f = ntl.zz_pContext(10^100)107Traceback (most recent call last):108...109ValueError: Modulus (=10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) is too big110"""111if v > NTL_SP_BOUND:112raise ValueError, "Modulus (=%s) is too big"%v113if PY_TYPE_CHECK(v, Integer):114v = mpz_get_si((<Integer>v).value)115try:116return zz_pContextDict[repr(v)]117except KeyError:118return ntl_zz_pContext_class(v)119120121