Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/libs/ntl/ntl_ZZ_pEContext.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
ZZ_pEContextDict = {}
22
23
from sage.libs.ntl.ntl_ZZ_pX cimport ntl_ZZ_pX
24
from sage.libs.ntl.ntl_ZZ_pContext import ntl_ZZ_pContext
25
from sage.libs.ntl.ntl_ZZ cimport ntl_ZZ
26
27
cdef class ntl_ZZ_pEContext_class:
28
def __init__(self, ntl_ZZ_pX f):
29
"""
30
EXAMPLES:
31
# You can construct contexts manually.
32
sage: c=ntl.ZZ_pEContext(ntl.ZZ_pX([4,1,6],25))
33
sage: n1=c.ZZ_pE([10,17,12])
34
sage: n1
35
[2 15]
36
37
# or You can construct contexts implicitly.
38
sage: n2=ntl.ZZ_pE(12, ntl.ZZ_pX([1,1,1],7))
39
sage: n2
40
[5]
41
sage: n2+n1 # Mismatched moduli: It will go BOOM!
42
Traceback (most recent call last):
43
...
44
ValueError: You can not perform arithmetic with elements of different moduli.
45
"""
46
pass
47
48
def __cinit__(self, ntl_ZZ_pX f):
49
self.pc = f.c
50
self.pc.restore_c()
51
ZZ_pEContext_construct_ZZ_pX(&self.x, &f.x)
52
ZZ_pEContextDict[(repr(f),repr(f.c.p))] = self
53
self.f = f
54
55
def __dealloc__(self):
56
ZZ_pEContext_destruct(&self.x)
57
58
def __reduce__(self):
59
"""
60
sage: c=ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1],7))
61
sage: loads(dumps(c)) is c
62
True
63
"""
64
return ntl_ZZ_pEContext, (self.f,)
65
66
def __repr__(self):
67
"""
68
Returns a string representation of self.
69
70
EXAMPLES:
71
sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7)); c
72
NTL modulus [1 1 1] (mod 7)
73
"""
74
return "NTL modulus %s (mod %s)"%(self.f, self.pc.p)
75
76
def get_pc(self):
77
"""
78
Returns the ZZ_pContext contained within self.
79
80
EXAMPLES:
81
sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7)); c
82
NTL modulus [1 1 1] (mod 7)
83
sage: c.get_pc()
84
NTL modulus 7
85
"""
86
return self.pc
87
88
def polynomial(self):
89
"""
90
Returns the ZZ_pX polynomial defining self.
91
92
EXAMPLES:
93
sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7))
94
sage: c.polynomial()
95
[1 1 1]
96
"""
97
return self.f
98
99
def restore(self):
100
"""
101
Manually sets the global NTL modulus to be self.
102
103
This should be done automatically by all of the NTL wrapper classes.
104
105
CRUCIAL: If you are writing your own classes that use ZZ_p_c, ZZ_pX_c, ZZ_pE_c, ZZ_pEX_c
106
then you MUST restore the context before calling off to NTL for anything. If the context has been
107
switched by other code then behavior of operations is undefined. See the NTL documentation for
108
more details (or the wrappers in sage.libs.ntl)
109
"""
110
self.restore_c()
111
112
cdef void restore_c(self):
113
"""
114
Sets the global NTL modulus to be self.
115
116
CRUCIAL: If you are writing your own classes that use ZZ_p_c, ZZ_pX_c, ZZ_pE_c, ZZ_pEX_c
117
then you MUST restore the context before calling off to NTL for anything. If the context has been
118
switched by other code then behavior of operations is undefined. See the NTL documentation for
119
more details (or the wrappers in sage.libs.ntl)
120
"""
121
self.pc.restore_c()
122
ZZ_pEContext_restore(&self.x)
123
124
#def ZZ_pX(self,v = None):
125
# from ntl_ZZ_pX import ntl_ZZ_pX
126
# return ntl_ZZ_pX(v,modulus=self)
127
128
def ZZ_pE(self,v = None):
129
"""
130
Returns a ZZ_pE object with modulus self out of the data v.
131
132
EXAMPLES:
133
sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7))
134
sage: c.ZZ_pE([4,3])
135
[4 3]
136
"""
137
from ntl_ZZ_pE import ntl_ZZ_pE
138
return ntl_ZZ_pE(v,modulus=self)
139
140
def ZZ_pEX(self, v = None):
141
"""
142
Returns a ZZ_pE object with modulus self out of the data v.
143
144
EXAMPLES:
145
sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7))
146
sage: c.ZZ_pEX([4,3])
147
[[4] [3]]
148
"""
149
from ntl_ZZ_pEX import ntl_ZZ_pEX
150
return ntl_ZZ_pEX(v, modulus=self)
151
152
def ntl_ZZ_pEContext( ntl_ZZ_pX f):
153
"""
154
Creates an ntl_ZZ_pEContext.
155
156
Such an object must be created before any ZZ_pE or ZZ_pEX objects can be used.
157
158
The context handling should be taken care of by the wrapper classes.
159
EXAMPLES:
160
sage: c = ntl.ZZ_pEContext(ntl.ZZ_pX([1,1,1], 7)); c
161
NTL modulus [1 1 1] (mod 7)
162
"""
163
try:
164
return ZZ_pEContextDict[repr(f), repr(f.c.p)]
165
except KeyError:
166
# Creating the following object caches it.
167
return ntl_ZZ_pEContext_class(f)
168
169