Skip to content

Commit 749254a

Browse files
Release Managervbraun
Release Manager
authored andcommitted
Trac #21156: AbstractLinearCode.dimension() is wrong if _dimension is not set
The current implementation of `AbstractLinearCode.dimension()` simply accesses `self._dimension`. But there is no (longer a) requirement that this be set at construction time. The method should instead check if the special variable is set, and if it is not, compute the dimension as the number of rows in a generator matrix. URL: https://trac.sagemath.org/21156 Reported by: jsrn Ticket author(s): Bruno Grenet Reviewer(s): Johan Rosenkilde
2 parents da7d7d0 + b72b705 commit 749254a

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/sage/coding/linear_code.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ class AbstractLinearCode(Module):
326326
You need of course to complete the constructor by adding any additional parameter
327327
needed to describe properly the code defined in the subclass.
328328
329+
- Add the following two lines on the class level::
330+
331+
_registered_encoders = {}
332+
_registered_decoders = {}
333+
334+
329335
- fill the dictionary of its encoders in ``sage.coding.__init__.py`` file. Example:
330336
I want to link the encoder ``MyEncoderClass`` to ``MyNewCodeClass``
331337
under the name ``MyEncoderName``.
@@ -1554,8 +1560,38 @@ def dimension(self):
15541560
sage: C = LinearCode(G)
15551561
sage: C.dimension()
15561562
2
1563+
1564+
TESTS:
1565+
1566+
Check that :trac:`21156` is fixed::
1567+
1568+
sage: from sage.coding.linear_code import AbstractLinearCode
1569+
sage: from sage.coding.encoder import Encoder
1570+
sage: class MonkeyCode(AbstractLinearCode):
1571+
....: _registered_encoders = {}
1572+
....: _registered_decoders = {}
1573+
....: def __init__(self):
1574+
....: super(MonkeyCode, self).__init__(GF(5), 10, "Monkey", "Syndrome")
1575+
....:
1576+
sage: class MonkeyEncoder(Encoder):
1577+
....: def __init__(self, code):
1578+
....: super(MonkeyEncoder, self).__init__(C)
1579+
....: @cached_method
1580+
....: def generator_matrix(self):
1581+
....: G = identity_matrix(GF(5), 5).augment(matrix(GF(5), 5, 7))
1582+
....: return G
1583+
....:
1584+
sage: MonkeyCode._registered_encoders["Monkey"] = MonkeyEncoder
1585+
sage: C = MonkeyCode()
1586+
sage: C.dimension()
1587+
5
15571588
"""
1558-
return self._dimension
1589+
try:
1590+
return self._dimension
1591+
except AttributeError:
1592+
dimension = self.generator_matrix().nrows()
1593+
self._dimension = dimension
1594+
return self._dimension
15591595

15601596
def direct_sum(self, other):
15611597
"""

0 commit comments

Comments
 (0)