Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/libs/ntl/ntl_lzz_pContext.pyx
4107 views
1
#*****************************************************************************
2
# Copyright (C) 2005 William Stein <[email protected]>
3
#
4
# Distributed under the terms of the GNU General Public License (GPL)
5
#
6
# This code is distributed in the hope that it will be useful,
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9
# General Public License for more details.
10
#
11
# The full text of the GPL is available at:
12
#
13
# http://www.gnu.org/licenses/
14
#*****************************************************************************
15
16
include "../../ext/interrupt.pxi"
17
include "../../ext/stdsage.pxi"
18
include 'misc.pxi'
19
include 'decl.pxi'
20
21
from sage.rings.integer import Integer
22
from sage.rings.integer cimport Integer
23
24
zz_pContextDict = {}
25
26
cdef class ntl_zz_pContext_class:
27
def __init__(self, long v):
28
"""
29
EXAMPLES:
30
# You can construct contexts manually.
31
sage: c = ntl.zz_pContext(11)
32
sage: n1 = ntl.zz_p(12,c)
33
sage: n1
34
1
35
36
# or You can construct contexts implicitly.
37
sage: n2=ntl.zz_p(12, 7)
38
sage: n2
39
5
40
sage: ntl.zz_p(2,3)+ntl.zz_p(1,3)
41
0
42
sage: n2+n1 # Mismatched moduli: It will go BOOM!
43
Traceback (most recent call last):
44
...
45
ValueError: arithmetic operands must have the same modulus.
46
"""
47
pass
48
49
def __cinit__(self, long v):
50
if v > NTL_SP_BOUND:
51
raise ValueError, "Modulus (=%s) is too big"%v
52
zz_pContext_construct_long(&self.x, v)
53
zz_pContextDict[repr(v)] = self
54
self.p = v
55
56
def __dealloc__(self):
57
zz_pContext_destruct(&self.x)
58
59
def __reduce__(self):
60
"""
61
sage: c=ntl.zz_pContext(13)
62
sage: loads(dumps(c)) is c
63
True
64
"""
65
return ntl_zz_pContext, (self.p,)
66
67
def modulus(self):
68
"""
69
Print the modulus for self.
70
71
EXAMPLES:
72
sage: c1 = ntl.zz_pContext(36)
73
sage: c1.modulus()
74
36
75
"""
76
return self.p
77
78
def restore(self):
79
"""
80
Restore a zz_pContext.
81
82
EXAMPLES:
83
sage: c = ntl.zz_pContext(5)
84
sage: m = ntl.zz_p(4,7)
85
sage: c.restore()
86
"""
87
self.restore_c()
88
89
cdef void restore_c(self):
90
"""
91
Actual code for the above.
92
93
EXAMPLES:
94
sage: n = ntl.zz_p(3,5)
95
sage: m = ntl.zz_p(4,7)
96
sage: n*n ## indirect doctest
97
4
98
"""
99
zz_pContext_restore(&self.x)
100
101
def ntl_zz_pContext( v ):
102
"""
103
Creation function for a zz_p context.
104
105
EXAMPLES:
106
sage: f = ntl.zz_pContext(26)
107
sage: f = ntl.zz_pContext(10^100)
108
Traceback (most recent call last):
109
...
110
ValueError: Modulus (=10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) is too big
111
"""
112
if v > NTL_SP_BOUND:
113
raise ValueError, "Modulus (=%s) is too big"%v
114
if PY_TYPE_CHECK(v, Integer):
115
v = mpz_get_si((<Integer>v).value)
116
try:
117
return zz_pContextDict[repr(v)]
118
except KeyError:
119
return ntl_zz_pContext_class(v)
120
121