Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/libs/ntl/ntl_GF2EX.pyx
4096 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 ntl_ZZ import unpickle_class_args
22
from ntl_GF2EContext import ntl_GF2EContext
23
from ntl_GF2EContext cimport ntl_GF2EContext_class
24
from ntl_GF2E cimport ntl_GF2E
25
26
##############################################################################
27
#
28
# ntl_GF2EX: Polynomials over GF(2) via NTL
29
#
30
# AUTHORS:
31
# - Martin Albrecht <[email protected]> 2006-01: initial version
32
#
33
##############################################################################
34
35
cdef class ntl_GF2EX:
36
r"""
37
Minimal wrapper of NTL's GF2EX class.
38
"""
39
def __init__(self, modulus=None, x=[]):
40
"""
41
Minimal wrapper of NTL's GF2EX class.
42
43
EXAMPLES:
44
sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1]))
45
sage: ntl.GF2EX(ctx, '[[1 0] [2 1]]')
46
[[1] [0 1]]
47
"""
48
if modulus is None:
49
raise ValueError, "You must specify a modulus when creating a GF2E."
50
51
s = str(x)
52
sig_on()
53
GF2EX_from_str(&self.x, s)
54
sig_off()
55
56
def __cinit__(self, modulus=None, x=[]):
57
#################### WARNING ###################
58
## Before creating a GF2E, you must create a ##
59
## GF2EContext, and restore it. In Python, ##
60
## the error checking in __init__ will prevent##
61
## you from constructing an ntl_GF2E ##
62
## inappropriately. However, from Cython, you##
63
## could do r = PY_NEW(ntl_GF2E) without ##
64
## first restoring a GF2EContext, which could ##
65
## have unfortunate consequences. See _new ##
66
## defined below for an example of the right ##
67
## way to short-circuit __init__ (or just call##
68
## _new in your own code). ##
69
################################################
70
if modulus is None:
71
return
72
if PY_TYPE_CHECK( modulus, ntl_GF2EContext_class ):
73
self.c = <ntl_GF2EContext_class>modulus
74
self.c.restore_c()
75
GF2EX_construct(&self.x)
76
else:
77
self.c = <ntl_GF2EContext_class>ntl_GF2EContext(modulus)
78
self.c.restore_c()
79
GF2EX_construct(&self.x)
80
81
cdef ntl_GF2E _new_element(self):
82
cdef ntl_GF2E r
83
self.c.restore_c()
84
r = PY_NEW(ntl_GF2E)
85
r.c = self.c
86
return r
87
88
cdef ntl_GF2EX _new(self):
89
cdef ntl_GF2EX r
90
self.c.restore_c()
91
r = PY_NEW(ntl_GF2EX)
92
r.c = self.c
93
return r
94
95
def modulus_context(self):
96
return self.c
97
98
def __dealloc__(self):
99
if <object>self.c is not None:
100
self.c.restore_c()
101
GF2EX_destruct(&self.x)
102
103
def __reduce__(self):
104
"""
105
EXAMPLES:
106
sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1]))
107
sage: f = ntl.GF2EX(ctx, '[[1 0 1] [1 0 0 1] [1]]')
108
sage: f == loads(dumps(f))
109
True
110
"""
111
return unpickle_class_args, (ntl_GF2EX, (self.c, self.__repr__()))
112
113
def __cmp__(self, other):
114
"""
115
Compare self to other.
116
117
EXAMPLES:
118
sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1]))
119
sage: f = ntl.GF2EX(ctx, '[[1 0 1] [1 0 0 1] [1]]')
120
sage: g = ntl.GF2EX(ctx, '[[1 0 1] [1 1] [1] [0 0 1]]')
121
sage: f == f
122
True
123
sage: f == g
124
False
125
"""
126
## TODO: this is shady. fix that.
127
if (type(self) != type(other)):
128
return cmp(type(self), type(other))
129
return cmp(self.__repr__(), other.__repr__())
130
131
def __repr__(self):
132
"""
133
Return the string representation of self.
134
135
EXAMPLES:
136
sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1]))
137
sage: ntl.GF2EX(ctx, '[[1 0] [2 1]]').__repr__()
138
'[[1] [0 1]]'
139
"""
140
return GF2EX_to_PyString(&self.x)
141
142
def __mul__(ntl_GF2EX self, other):
143
"""
144
EXAMPLES:
145
sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1]))
146
sage: f = ntl.GF2EX(ctx, '[[1 0] [2 1]]')
147
sage: g = ntl.GF2EX(ctx, '[[1 0 1 1] [0 1 1 0 1] [1 0 1]]')
148
sage: f*g ## indirect doctest
149
[[1 0 1 1] [0 0 1 1] [1 0 0 1 0 1] [0 1 0 1]]
150
"""
151
cdef ntl_GF2EX y
152
cdef ntl_GF2EX r = self._new()
153
if not isinstance(other, ntl_GF2EX):
154
other = ntl_GF2EX(self.c, other)
155
y = other
156
sig_on()
157
GF2EX_mul(r.x, self.x, y.x)
158
sig_off()
159
return r
160
161
def __sub__(ntl_GF2EX self, other):
162
"""
163
EXAMPLES:
164
sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1]))
165
sage: f = ntl.GF2EX(ctx, '[[1 0] [2 1]]')
166
sage: g = ntl.GF2EX(ctx, '[[1 0 1 1] [0 1 1 0 1] [1 0 1]]')
167
sage: f-g ## indirect doctest
168
[[0 0 1 1] [0 0 1 0 1] [1 0 1]]
169
"""
170
cdef ntl_GF2EX y
171
cdef ntl_GF2EX r = self._new()
172
if not isinstance(other, ntl_GF2EX):
173
other = ntl_GF2EX(self.c, other)
174
y = other
175
sig_on()
176
GF2EX_sub(r.x, self.x, y.x)
177
sig_off()
178
return r
179
180
def __add__(ntl_GF2EX self, other):
181
"""
182
EXAMPLES:
183
sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1]))
184
sage: f = ntl.GF2EX(ctx, '[[1 0] [2 1]]')
185
sage: g = ntl.GF2EX(ctx, '[[1 0 1 1] [0 1 1 0 1] [1 0 1]]')
186
sage: f+g ## indirect doctest
187
[[0 0 1 1] [0 0 1 0 1] [1 0 1]]
188
"""
189
cdef ntl_GF2EX y
190
cdef ntl_GF2EX r = self._new()
191
if not isinstance(other, ntl_GF2EX):
192
other = ntl_GF2EX(self.c, other)
193
y = other
194
sig_on()
195
GF2EX_add(r.x, self.x, y.x)
196
sig_off()
197
return r
198
199
def __neg__(ntl_GF2EX self):
200
"""
201
EXAMPLES:
202
sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1]))
203
sage: f = ntl.GF2EX(ctx, '[[1 0] [2 1]]')
204
sage: -f ## indirect doctest
205
[[1] [0 1]]
206
"""
207
cdef ntl_GF2EX r = self._new()
208
sig_on()
209
GF2EX_negate(r.x, self.x)
210
sig_off()
211
return r
212
213
def __pow__(ntl_GF2EX self, long e, ignored):
214
"""
215
EXAMPLES:
216
sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1]))
217
sage: f = ntl.GF2EX(ctx, '[[1 0] [2 1]]')
218
sage: f**2 ## indirect doctest
219
[[1] [] [0 0 1]]
220
"""
221
cdef ntl_GF2EX r = self._new()
222
sig_on()
223
GF2EX_power(r.x, self.x, e)
224
sig_off()
225
return r
226
227