Skip to content

Commit

Permalink
Trac #20201: Improving Efficiency of LinearCode.NearestNeighborDecode…
Browse files Browse the repository at this point in the history
…r method

The `decode_to_code` method of the current implementation of the
`NearestNeighborDecoder` creates and stores the distances between the
received word and every codeword. It then sorts the entire list in order
to find the closest codeword. This takes asymptotic memory and time in
the size of the code which can be very inefficient for large input.
This should be improved.

URL: http://trac.sagemath.org/20201
Reported by: arpitdm
Ticket author(s): Arpit Merchant
Reviewer(s): David Lucas
  • Loading branch information
Release Manager authored and vbraun committed Apr 21, 2016
2 parents 950f0f5 + febfd7f commit 310dce0
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/sage/coding/linear_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -4562,16 +4562,15 @@ def _latex_(self):

def decode_to_code(self, r):
r"""
Decode the received word ``r`` to the nearest element in associated code of ``self``.
Corrects the errors in ``word`` and returns a codeword.
INPUT:
- ``r`` -- a vector of same length as the length of the associated
code of ``self`` and over the base field of the associated code of ``self``
- ``r`` -- a codeword of ``self``
OUTPUT:
- a codeword of the associated code of ``self``
- a vector of ``self``'s message space
EXAMPLES::
Expand All @@ -4580,18 +4579,17 @@ def decode_to_code(self, r):
sage: D = codes.decoders.LinearCodeNearestNeighborDecoder(C)
sage: word = vector(GF(2), (1, 1, 0, 0, 1, 1, 0))
sage: w_err = word + vector(GF(2), (1, 0, 0, 0, 0, 0, 0))
sage: D.decode_to_code(word)
sage: D.decode_to_code(w_err)
(1, 1, 0, 0, 1, 1, 0)
"""
V = self.input_space()
if not isinstance(r, list):
r = r.list()
r = V(r)
diffs = [[c - r, (c - r).hamming_weight()] for c in self.code()]
diffs.sort(key=lambda x: x[1])
c = diffs[0][0] + r
c.set_immutable()
return c
c_min = self.code().zero()
h_min = r.hamming_weight()
for c in self.code():
if (c-r).hamming_weight() < h_min:
h_min = (c-r).hamming_weight()
c_min = c
c_min.set_immutable()
return c_min

def decoding_radius(self):
r"""
Expand Down

0 comments on commit 310dce0

Please sign in to comment.