Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/coding/guava.py
8815 views
1
r"""
2
Guava error-correcting code constructions.
3
4
This module only contains Guava wrappers (Guava is an optional GAP package).
5
6
AUTHORS:
7
8
- David Joyner (2005-11-22, 2006-12-03): initial version
9
10
- Nick Alexander (2006-12-10): factor GUAVA code to guava.py
11
12
- David Joyner (2007-05): removed Golay codes, toric and trivial codes and
13
placed them in code_constructions; renamed RandomLinearCode to
14
RandomLinearCodeGuava
15
16
- David Joyner (2008-03): removed QR, XQR, cyclic and ReedSolomon codes
17
18
- David Joyner (2009-05): added "optional package" comments, fixed some
19
docstrings to to be sphinx compatible
20
21
Functions
22
---------
23
"""
24
25
#*****************************************************************************
26
# Copyright (C) 2007 David Joyner <[email protected]>
27
# 2006 Nick Alexander <[email protected]>
28
#
29
# Distributed under the terms of the GNU General Public License (GPL)
30
#
31
# http://www.gnu.org/licenses/
32
#*****************************************************************************
33
34
from sage.interfaces.all import gap
35
from sage.misc.randstate import current_randstate
36
from sage.misc.preparser import *
37
from sage.matrix.matrix_space import MatrixSpace
38
from sage.rings.finite_rings.constructor import FiniteField as GF
39
from sage.interfaces.gap import gfq_gap_to_sage
40
from sage.groups.perm_gps.permgroup import *
41
from linear_code import *
42
43
def BinaryReedMullerCode(r,k):
44
r"""
45
The binary 'Reed-Muller code' with dimension k and
46
order r is a code with length `2^k` and minimum distance `2^k-r`
47
(see for example, section 1.10 in [HP]_). By definition, the
48
`r^{th}` order binary Reed-Muller code of length `n=2^m`, for
49
`0 \leq r \leq m`, is the set of all vectors `(f(p)\ |\ p \\in GF(2)^m)`,
50
where `f` is a multivariate polynomial of degree at most `r`
51
in `m` variables.
52
53
INPUT:
54
55
- ``r, k`` -- positive integers with `2^k>r`.
56
57
OUTPUT:
58
59
Returns the binary 'Reed-Muller code' with dimension `k` and order `r`.
60
61
EXAMPLE::
62
63
sage: C = codes.BinaryReedMullerCode(2,4); C # optional - gap_packages (Guava package)
64
Linear code of length 16, dimension 11 over Finite Field of size 2
65
sage: C.minimum_distance() # optional - gap_packages (Guava package)
66
4
67
sage: C.gen_mat() # optional - gap_packages (Guava package)
68
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
69
[0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1]
70
[0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1]
71
[0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1]
72
[0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1]
73
[0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1]
74
[0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1]
75
[0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1]
76
[0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1]
77
[0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1]
78
[0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1]
79
80
AUTHOR: David Joyner (11-2005)
81
"""
82
F = GF(2)
83
gap.eval("C:=ReedMullerCode("+str(r)+", "+str(k)+")")
84
gap.eval("G:=GeneratorMat(C)")
85
k = int(gap.eval("Length(G)"))
86
n = int(gap.eval("Length(G[1])"))
87
G = [[gfq_gap_to_sage(gap.eval("G["+str(i)+"]["+str(j)+"]"),F) for j in range(1,n+1)] for i in range(1,k+1)]
88
MS = MatrixSpace(F,k,n)
89
return LinearCode(MS(G))
90
91
def QuasiQuadraticResidueCode(p):
92
r"""
93
A (binary) quasi-quadratic residue code (or QQR code), as defined by
94
Proposition 2.2 in [BM]_, has a generator matrix in the block form `G=(Q,N)`.
95
Here `Q` is a `p \times p` circulant matrix whose top row
96
is `(0,x_1,...,x_{p-1})`, where `x_i=1` if and only if `i`
97
is a quadratic residue `\mod p`, and `N` is a `p \times p` circulant
98
matrix whose top row is `(0,y_1,...,y_{p-1})`, where `x_i+y_i=1` for all `i`.
99
100
INPUT:
101
102
- ``p`` -- a prime `>2`.
103
104
OUTPUT:
105
106
Returns a QQR code of length `2p`.
107
108
EXAMPLES::
109
110
sage: C = codes.QuasiQuadraticResidueCode(11); C # optional - gap_packages (Guava package)
111
Linear code of length 22, dimension 11 over Finite Field of size 2
112
113
REFERENCES:
114
115
.. [BM] Bazzi and Mitter, {\it Some constructions of codes from group actions}, (preprint
116
March 2003, available on Mitter's MIT website).
117
118
.. [Jresidue] D. Joyner, {\it On quadratic residue codes and hyperelliptic curves},
119
(preprint 2006)
120
121
These are self-orthogonal in general and self-dual when $p \\equiv 3 \\pmod 4$.
122
123
AUTHOR: David Joyner (11-2005)
124
"""
125
F = GF(2)
126
gap.eval("C:=QQRCode("+str(p)+")")
127
gap.eval("G:=GeneratorMat(C)")
128
k = int(gap.eval("Length(G)"))
129
n = int(gap.eval("Length(G[1])"))
130
G = [[gfq_gap_to_sage(gap.eval("G[%s][%s]" % (i,j)),F) for j in range(1,n+1)] for i in range(1,k+1)]
131
MS = MatrixSpace(F,k,n)
132
return LinearCode(MS(G))
133
134
def RandomLinearCodeGuava(n,k,F):
135
r"""
136
The method used is to first construct a `k \times n` matrix of the block
137
form `(I,A)`, where `I` is a `k \times k` identity matrix and `A` is a
138
`k \times (n-k)` matrix constructed using random elements of `F`. Then
139
the columns are permuted using a randomly selected element of the symmetric
140
group `S_n`.
141
142
INPUT:
143
144
- ``n,k`` -- integers with `n>k>1`.
145
146
OUTPUT:
147
148
Returns a "random" linear code with length `n`, dimension `k` over field `F`.
149
150
EXAMPLES::
151
152
sage: C = codes.RandomLinearCodeGuava(30,15,GF(2)); C # optional - gap_packages (Guava package)
153
Linear code of length 30, dimension 15 over Finite Field of size 2
154
sage: C = codes.RandomLinearCodeGuava(10,5,GF(4,'a')); C # optional - gap_packages (Guava package)
155
Linear code of length 10, dimension 5 over Finite Field in a of size 2^2
156
157
AUTHOR: David Joyner (11-2005)
158
"""
159
current_randstate().set_seed_gap()
160
161
q = F.order()
162
gap.eval("C:=RandomLinearCode("+str(n)+","+str(k)+", GF("+str(q)+"))")
163
gap.eval("G:=GeneratorMat(C)")
164
k = int(gap.eval("Length(G)"))
165
n = int(gap.eval("Length(G[1])"))
166
G = [[gfq_gap_to_sage(gap.eval("G[%s][%s]" % (i,j)),F) for j in range(1,n+1)] for i in range(1,k+1)]
167
MS = MatrixSpace(F,k,n)
168
return LinearCode(MS(G))
169
170