Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Merge branch 'u/dlucas/abstract_lin_code_print_error_messages' of tra…
Browse files Browse the repository at this point in the history
…c.sagemath.org:sage into abstract_lin_code_print_error_messages
  • Loading branch information
david-lucas committed Feb 7, 2017
2 parents 375d4ee + 152c471 commit 601f3b1
Showing 1 changed file with 102 additions and 10 deletions.
112 changes: 102 additions & 10 deletions src/sage/coding/linear_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,20 +588,97 @@ def __init__(self, base_field, length, default_encoder_name, default_decoder_nam
self.Element = type(facade_for.an_element()) #for when we made this a non-facade parent
Parent.__init__(self, base=base_field, facade=facade_for, category=cat)

def _latex_(self):
def _repr_(self):
r"""
Returns an error message requiring to override ``_repr_`` in ``self``.
As one has to implement specific representation methods (`_repr_` and `_latex_`)
when writing a new code class which inherits from :class:`AbstractLinearCode`,
the generic call to `_repr_` has to fail.
EXAMPLES:
This was taken from :trac:20899 (and thus ensures this method fixes what was
described in this ticket).
We create a new code class, its dedicated encoder
and set appropriate parameters::
sage: from sage.coding.linear_code import AbstractLinearCode
sage: from sage.coding.encoder import Encoder
sage: class MyCode(AbstractLinearCode):
....: _registered_encoders = {}
....: _registered_decoders = {}
....: def __init__(self):
....: super(MyCode, self).__init__(GF(5), 10, "Monkey", "Syndrome")
....: self._dimension = 2
sage: class MonkeyEncoder(Encoder):
....: def __init__(self, C):
....: super(MonkeyEncoder, self).__init__(C)
....: @cached_method
....: def generator_matrix(self):
....: return matrix(GF(5), 2, 10, [ [1]*5 + [0]*5, [0]*5 + [1]*5 ])
sage: MyCode._registered_encoders["Monkey"] = MonkeyEncoder
sage: MyCode._registered_decoders["Syndrome"] = codes.decoders.LinearCodeSyndromeDecoder
We check we get a sensible error message while asking for a string
representation of an instance of our new class:
sage: C = MyCode()
sage: C #random
Traceback (most recent call last):
...
RuntimeError: Please override _repr_ in the implementation of <class '__main__.MyCode_with_category'>
"""
Return a latex representation of ``self``.
raise RuntimeError("Please override _repr_ in the implementation of %s"
% self.parent())

EXAMPLES::
def _latex_(self):
r"""
Returns an error message requiring to override ``_latex_`` in ``self``.
sage: MS = MatrixSpace(GF(2),4,7)
sage: G = MS([[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: latex(C)
[7, 4]\textnormal{ Linear code over }\Bold{F}_{2}
As one has to implement specific representation methods (`_repr_` and `_latex_`)
when writing a new code class which inherits from :class:`AbstractLinearCode`,
the generic call to `_latex_` has to fail.
EXAMPLES:
This was taken from :trac:20899 (and thus ensures this method fixes what was
described in this ticket).
We create a new code class, its dedicated encoder
and set appropriate parameters::
sage: from sage.coding.linear_code import AbstractLinearCode
sage: from sage.coding.encoder import Encoder
sage: class MyCode(AbstractLinearCode):
....: _registered_encoders = {}
....: _registered_decoders = {}
....: def __init__(self):
....: super(MyCode, self).__init__(GF(5), 10, "Monkey", "Syndrome")
....: self._dimension = 2
sage: class MonkeyEncoder(Encoder):
....: def __init__(self, C):
....: super(MonkeyEncoder, self).__init__(C)
....: @cached_method
....: def generator_matrix(self):
....: return matrix(GF(5), 2, 10, [ [1]*5 + [0]*5, [0]*5 + [1]*5 ])
sage: MyCode._registered_encoders["Monkey"] = MonkeyEncoder
sage: MyCode._registered_decoders["Syndrome"] = codes.decoders.LinearCodeSyndromeDecoder
We check we get a sensible error message while asking for a string
representation of an instance of our new class:
sage: C = MyCode()
sage: latex(C)
Traceback (most recent call last):
...
RuntimeError: Please override _latex_ in the implementation of <class '__main__.MyCode_with_category'>
"""
return "[%s, %s]\\textnormal{ Linear code over }%s"\
% (self.length(), self.dimension(), self.base_ring()._latex_())
raise RuntimeError("Please override _latex_ in the implementation of %s"
% self.parent())

def _an_element_(self):
r"""
Expand Down Expand Up @@ -3703,6 +3780,21 @@ def _repr_(self):
else:
return "[%s, %s] linear code over %s"%(self.length(), self.dimension(), R)

def _latex_(self):
"""
Return a latex representation of ``self``.
EXAMPLES::
sage: MS = MatrixSpace(GF(2),4,7)
sage: G = MS([[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: latex(C)
[7, 4]\textnormal{ Linear code over }\Bold{F}_{2}
"""
return "[%s, %s]\\textnormal{ Linear code over }%s"\
% (self.length(), self.dimension(), self.base_ring()._latex_())

def __hash__(self):
r"""
Returns the hash value of ``self``.
Expand Down

0 comments on commit 601f3b1

Please sign in to comment.