Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/coding/decoder.py
8815 views
1
"""
2
Decoding methods for linear error-correcting codes.
3
Methods implemented:
4
5
* nearest neighbor
6
* syndrome
7
8
AUTHOR:
9
-- David Joyner (2009-02-01): initial version
10
11
TODO:
12
Add lots more methods!
13
"""
14
#*****************************************************************************
15
# Copyright (C) 2009 David Joyner <[email protected]>
16
#
17
# Distributed under the terms of the GNU General Public License (GPL),
18
# version 2 or later (at your preference).
19
#
20
# http://www.gnu.org/licenses/
21
#*****************************************************************************
22
23
from sage.misc.decorators import rename_keyword
24
25
def syndrome(C, v):
26
"""
27
The vector v represents a received word, so should
28
be in the same ambient space V as C. Returns the
29
elements in V (including v) which belong to the
30
syndrome of v (ie, the coset v+C, sorted by weight).
31
32
EXAMPLES:
33
sage: C = codes.HammingCode(2,GF(3)); C
34
Linear code of length 4, dimension 2 over Finite Field of size 3
35
sage: V = VectorSpace(GF(3), 4)
36
sage: v = V([0, 2, 0, 1])
37
sage: from sage.coding.decoder import syndrome
38
sage: syndrome(C, v)
39
[(0, 0, 1, 0), (0, 2, 0, 1), (2, 0, 0, 2), (1, 1, 0, 0), (2, 2, 2, 0), (1, 0, 2, 1), (0, 1, 2, 2), (1, 2, 1, 2), (2, 1, 1, 1)]
40
41
"""
42
V = C.ambient_space()
43
if not isinstance(v, list):
44
v = v.list()
45
v = V(v)
46
coset = [[c + v, (c + v).hamming_weight()] for c in C]
47
coset.sort(lambda x, y: x[1] - y[1])
48
return [x[0] for x in coset]
49
50
def coset_leader(C, v):
51
"""
52
The vector v represents a received word, so should
53
be in the same ambient space V as C. Returns an
54
element of the syndrome of v of lowest weight.
55
56
EXAMPLES:
57
sage: C = codes.HammingCode(2,GF(3)); C
58
Linear code of length 4, dimension 2 over Finite Field of size 3
59
sage: V = VectorSpace(GF(3), 4)
60
sage: v = V([0, 2, 0, 1])
61
sage: from sage.coding.decoder import coset_leader
62
sage: coset_leader(C, v)
63
((0, 0, 1, 0), 1)
64
sage: coset_leader(C, v)[0]-v in C
65
True
66
67
"""
68
coset = [[c + v, (c + v).hamming_weight()] for c in C]
69
wts = [x[1] for x in coset]
70
min_wt = min(wts)
71
s = C[0] # initializing
72
w = v.hamming_weight() # initializing
73
for x in coset:
74
if x[1] == min_wt:
75
w = x[1]
76
s = x[0]
77
break
78
return s, w
79
80
@rename_keyword(deprecation=6094, method="algorithm")
81
def decode(C, v, algorithm="syndrome"):
82
"""
83
The vector v represents a received word, so should
84
be in the same ambient space V as C. Returns an
85
element in C which is closest to v in the Hamming
86
metric.
87
88
Methods implemented include "nearest neighbor" (essentially
89
a brute force search) and "syndrome".
90
91
EXAMPLES:
92
sage: C = codes.HammingCode(2,GF(3))
93
sage: V = VectorSpace(GF(3), 4)
94
sage: v = V([0, 2, 0, 1])
95
sage: v in C
96
False
97
sage: from sage.coding.decoder import decode
98
sage: c = decode(C, v);c
99
(0, 2, 2, 1)
100
sage: c in C
101
True
102
sage: c = decode(C, v, algorithm="nearest neighbor");c
103
(0, 2, 2, 1)
104
sage: C = codes.HammingCode(3,GF(3)); C
105
Linear code of length 13, dimension 10 over Finite Field of size 3
106
sage: V = VectorSpace(GF(3), 13)
107
sage: v = V([2]+[0]*12)
108
sage: decode(C, v) # long time (9s on sage.math, 2011)
109
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
110
"""
111
V = C.ambient_space()
112
if not isinstance(v, list):
113
v = v.list()
114
v = V(v)
115
if algorithm == "nearest neighbor":
116
diffs = [[c - v, (c - v).hamming_weight()] for c in C]
117
diffs.sort(lambda x, y: x[1] - y[1])
118
return diffs[0][0] + v
119
if algorithm == "syndrome":
120
return -V(syndrome(C, v)[0]) + v
121
122