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