Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/coding/decoder.py
4034 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.coding.linear_code import hamming_weight
24
from sage.misc.decorators import rename_keyword
25
26
def syndrome(C, v):
27
"""
28
The vector v represents a received word, so should
29
be in the same ambient space V as C. Returns the
30
elements in V (including v) which belong to the
31
syndrome of v (ie, the coset v+C, sorted by weight).
32
33
EXAMPLES:
34
sage: C = HammingCode(2,GF(3)); C
35
Linear code of length 4, dimension 2 over Finite Field of size 3
36
sage: V = VectorSpace(GF(3), 4)
37
sage: v = V([0, 2, 0, 1])
38
sage: from sage.coding.decoder import syndrome
39
sage: syndrome(C, v)
40
[(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)]
41
42
"""
43
V = C.ambient_space()
44
if not(type(v)==list):
45
v = v.list()
46
v = V(v)
47
coset = [[c+v,hamming_weight(c+v)] for c in C]
48
coset.sort(lambda x,y: x[1]-y[1])
49
return [x[0] for x in coset]
50
51
def coset_leader(C, v):
52
"""
53
The vector v represents a received word, so should
54
be in the same ambient space V as C. Returns an
55
element of the syndrome of v of lowest weight.
56
57
EXAMPLES:
58
sage: C = HammingCode(2,GF(3)); C
59
Linear code of length 4, dimension 2 over Finite Field of size 3
60
sage: V = VectorSpace(GF(3), 4)
61
sage: v = V([0, 2, 0, 1])
62
sage: from sage.coding.decoder import coset_leader
63
sage: coset_leader(C, v)
64
((0, 0, 1, 0), 1)
65
sage: coset_leader(C, v)[0]-v in C
66
True
67
68
"""
69
coset = [[c+v, hamming_weight(c+v)] for c in C]
70
wts = [x[1] for x in coset]
71
min_wt = min(wts)
72
s = C[0] # initializing
73
w = hamming_weight(v) # initializing
74
for x in coset:
75
if x[1]==min_wt:
76
w = x[1]
77
s = x[0]
78
break
79
return s,w
80
81
@rename_keyword(deprecated='Sage version 4.6', method="algorithm")
82
def decode(C, v, algorithm="syndrome"):
83
"""
84
The vector v represents a received word, so should
85
be in the same ambient space V as C. Returns an
86
element in C which is closest to v in the Hamming
87
metric.
88
89
Methods implemented include "nearest neighbor" (essentially
90
a brute force search) and "syndrome".
91
92
EXAMPLES:
93
sage: C = HammingCode(2,GF(3))
94
sage: V = VectorSpace(GF(3), 4)
95
sage: v = V([0, 2, 0, 1])
96
sage: v in C
97
False
98
sage: from sage.coding.decoder import decode
99
sage: c = decode(C, v);c
100
(0, 2, 2, 1)
101
sage: c in C
102
True
103
sage: c = decode(C, v, algorithm="nearest neighbor");c
104
(0, 2, 2, 1)
105
sage: C = HammingCode(3,GF(3)); C
106
Linear code of length 13, dimension 10 over Finite Field of size 3
107
sage: V = VectorSpace(GF(3), 13)
108
sage: v = V([2]+[0]*12)
109
sage: decode(C, v) # long time (9s on sage.math, 2011)
110
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
111
"""
112
V = C.ambient_space()
113
if not(type(v)==list):
114
v = v.list()
115
v = V(v)
116
if algorithm=="nearest neighbor":
117
diffs = [[c-v,hamming_weight(c-v)] for c in C]
118
diffs.sort(lambda x,y: x[1]-y[1])
119
return diffs[0][0]+v
120
if algorithm=="syndrome":
121
return -V(syndrome(C, v)[0])+v
122
123