Skip to content

Issue #40513, #40514: Allow zero code and add intersection method #40564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
33 changes: 19 additions & 14 deletions src/sage/coding/abstract_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def __init__(self, length, default_encoder_name=None,
INPUT:
- ``length`` -- the length of ``self`` (a Python int or a Sage Integer,
must be > 0)
must be >= 0)
- ``default_encoder_name`` -- (default: ``None``) the name of
the default encoder of ``self``
Expand Down Expand Up @@ -281,19 +281,24 @@ def __init__(self, length, default_encoder_name=None,
...
ValueError: length must be a Python int or a Sage Integer
If the length of the code is not a nonzero positive integer
(See :issue:`21326`), it will raise an exception::
If the length of the code is negative, it will raise an exception::
sage: C = MyCodeFamily(0)
sage: C = MyCodeFamily(-1)
Traceback (most recent call last):
...
ValueError: length must be a nonzero positive integer
ValueError: length must be a non-negative integer
Codes of length 0 are allowed::
sage: C = MyCodeFamily(0)
sage: C.length()
0
"""

if not isinstance(length, (int, Integer)):
raise ValueError("length must be a Python int or a Sage Integer")
if length <= 0:
raise ValueError("length must be a nonzero positive integer")
if length < 0:
raise ValueError("length must be a non-negative integer")

self._length = length
self._metric = metric
Expand Down Expand Up @@ -417,7 +422,7 @@ def __call__(self, m):
sage: C = LinearCode(G)
sage: word = vector((0, 1, 1, 0))
sage: C(word)
(1, 1, 0, 0, 1, 1, 0)
(0, 1, 1, 0, 0, 1, 1)
sage: c = C.random_element()
sage: C(c) == c
Expand Down Expand Up @@ -723,14 +728,14 @@ def decode_to_message(self, word, decoder_name=None, *args, **kwargs):
sage: C = LinearCode(G)
sage: word = vector(GF(2), (1, 1, 0, 0, 1, 1, 0))
sage: C.decode_to_message(word)
(0, 1, 1, 0)
(1, 1, 0, 0)
It is possible to manually choose the decoder amongst the list of the available ones::
sage: sorted(C.decoders_available())
['InformationSet', 'NearestNeighbor', 'Syndrome']
sage: C.decode_to_message(word, 'NearestNeighbor')
(0, 1, 1, 0)
(1, 1, 0, 0)
"""
return self.unencode(self.decode_to_code(word, decoder_name, *args, **kwargs), **kwargs)

Expand Down Expand Up @@ -882,17 +887,17 @@ def encode(self, word, encoder_name=None, *args, **kwargs):
sage: C = LinearCode(G)
sage: word = vector((0, 1, 1, 0))
sage: C.encode(word)
(1, 1, 0, 0, 1, 1, 0)
(0, 1, 1, 0, 0, 1, 1)
sage: C(word)
(1, 1, 0, 0, 1, 1, 0)
(0, 1, 1, 0, 0, 1, 1)
It is possible to manually choose the encoder amongst the list of the available ones::
sage: sorted(C.encoders_available())
['GeneratorMatrix', 'Systematic']
sage: word = vector((0, 1, 1, 0))
sage: C.encode(word, 'GeneratorMatrix')
(1, 1, 0, 0, 1, 1, 0)
(0, 1, 1, 0, 0, 1, 1)
"""
E = self.encoder(encoder_name, *args, **kwargs)
return E.encode(word)
Expand Down Expand Up @@ -1057,7 +1062,7 @@ def unencode(self, c, encoder_name=None, nocheck=False, **kwargs):
sage: G = Matrix(GF(2), [[1,1,1,0,0,0,0], [1,0,0,1,1,0,0],
....: [0,1,0,1,0,1,0], [1,1,0,1,0,0,1]])
sage: C = LinearCode(G)
sage: c = vector(GF(2), (1, 1, 0, 0, 1, 1, 0))
sage: c = vector(GF(2), (0, 1, 1, 0, 0, 1, 1))
sage: C.unencode(c)
(0, 1, 1, 0)
"""
Expand Down
10 changes: 5 additions & 5 deletions src/sage/coding/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ def self_orthogonal_binary_codes(n, k, b=2, parent=None, BC=None, equal=False,
[4, 1] linear code over GF(2)
[1 1 1 1]
[6, 2] linear code over GF(2)
[1 1 1 1 0 0]
[1 0 1 0 1 1]
[0 1 0 1 1 1]
[7, 3] linear code over GF(2)
[1 0 1 1 0 1 0]
[1 0 0 1 1 0 1]
[0 1 0 1 1 1 0]
[0 0 1 0 1 1 1]
Expand All @@ -248,7 +248,7 @@ def self_orthogonal_binary_codes(n, k, b=2, parent=None, BC=None, equal=False,
[4, 1] linear code over GF(2)
[1 1 1 1]
[6, 2] linear code over GF(2)
[1 1 1 1 0 0]
[1 0 1 0 1 1]
[0 1 0 1 1 1]
Generate all self-orthogonal codes of length equal to 8 and
Expand All @@ -263,8 +263,8 @@ def self_orthogonal_binary_codes(n, k, b=2, parent=None, BC=None, equal=False,
[0 0 1 0 0 1 0 0]
[0 0 0 0 0 0 1 1]
[8, 4] linear code over GF(2)
[1 0 0 1 1 0 1 0]
[0 1 0 1 1 1 0 0]
[1 0 0 0 1 1 0 1]
[0 1 0 0 1 0 1 1]
[0 0 1 0 1 1 1 0]
[0 0 0 1 0 1 1 1]
Expand Down
2 changes: 1 addition & 1 deletion src/sage/coding/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def decode_to_message(self, r):
sage: w_err = word + vector(GF(2), (1, 0, 0, 0, 0, 0, 0))
sage: D = C.decoder()
sage: D.decode_to_message(w_err)
(0, 1, 1, 0)
(1, 1, 0, 0)
"""
self.defaulting_decode_to_message = True
return self.code().unencode(self.decode_to_code(r))
Expand Down
33 changes: 16 additions & 17 deletions src/sage/coding/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def encode(self, word):
sage: word = vector(GF(2), (0, 1, 1, 0))
sage: E = codes.encoders.LinearCodeGeneratorMatrixEncoder(C)
sage: E.encode(word)
(1, 1, 0, 0, 1, 1, 0)
(0, 1, 1, 0, 0, 1, 1)
If ``word`` is not in the message space of ``self``, it will return an exception::
Expand Down Expand Up @@ -183,7 +183,7 @@ def __call__(self, m):
sage: word = vector(GF(2), (0, 1, 1, 0))
sage: E = codes.encoders.LinearCodeGeneratorMatrixEncoder(C)
sage: E(word)
(1, 1, 0, 0, 1, 1, 0)
(0, 1, 1, 0, 0, 1, 1)
sage: F = GF(11)
sage: Fx.<x> = F[]
Expand Down Expand Up @@ -224,7 +224,7 @@ def unencode(self, c, nocheck=False):
True
sage: E = codes.encoders.LinearCodeGeneratorMatrixEncoder(C)
sage: E.unencode(c)
(0, 1, 1, 0)
(1, 1, 0, 0)
TESTS:
Expand All @@ -241,11 +241,10 @@ def unencode(self, c, nocheck=False):
Note that since :issue:`21326`, codes cannot be of length zero::
sage: G = Matrix(GF(17), [])
sage: G = matrix(GF(2), 0, 0)
sage: C = LinearCode(G)
Traceback (most recent call last):
...
ValueError: length must be a nonzero positive integer
sage: C
[0, 0] linear code over GF(2)
"""
if not nocheck and c not in self.code():
raise EncodingError("Given word is not in the code")
Expand All @@ -268,10 +267,10 @@ def _unencoder_matrix(self):
sage: E = C.encoder()
sage: E._unencoder_matrix()
(
[0 0 1 1]
[0 1 0 1]
[1 1 1 0]
[0 1 1 1], (0, 1, 2, 3)
[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1], (0, 1, 2, 3)
)
"""
info_set = self.code().information_set()
Expand Down Expand Up @@ -306,7 +305,7 @@ def unencode_nocheck(self, c):
True
sage: E = codes.encoders.LinearCodeGeneratorMatrixEncoder(C)
sage: E.unencode_nocheck(c)
(0, 1, 1, 0)
(1, 1, 0, 0)
Taking a vector that does not belong to ``C`` will not raise an error but
probably just give a non-sensical result::
Expand All @@ -316,7 +315,7 @@ def unencode_nocheck(self, c):
False
sage: E = codes.encoders.LinearCodeGeneratorMatrixEncoder(C)
sage: E.unencode_nocheck(c)
(0, 1, 1, 0)
(1, 1, 0, 0)
sage: m = vector(GF(2), (0, 1, 1, 0))
sage: c1 = E.encode(m)
sage: c == c1
Expand Down Expand Up @@ -375,10 +374,10 @@ def generator_matrix(self):
sage: C = LinearCode(G)
sage: E = C.encoder()
sage: E.generator_matrix()
[1 1 1 0 0 0 0]
[1 0 0 1 1 0 0]
[0 1 0 1 0 1 0]
[1 1 0 1 0 0 1]
[1 0 0 0 0 1 1]
[0 1 0 0 1 0 1]
[0 0 1 0 1 1 0]
[0 0 0 1 1 1 1]
"""


Expand Down
Loading