Skip to content

Commit 864d842

Browse files
committed
recognizable series: properly deal with zero
1 parent 6ba0eaf commit 864d842

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

src/sage/combinat/recognizable_series.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,11 @@ def minimized(self):
11051105
sage: all(c == d and v == w
11061106
....: for (c, v), (d, w) in islice(zip(iter(S), iter(M)), 20))
11071107
True
1108+
1109+
TESTS::
1110+
1111+
sage: Rec((Matrix([[0]]), Matrix([[0]])), vector([1]), vector([0])).minimized().linear_representation()
1112+
((), Finite family {0: [], 1: []}, ())
11081113
"""
11091114
return self._minimized_right_()._minimized_left_()
11101115

@@ -1341,6 +1346,11 @@ def _rmul_(self, other):
13411346
sage: 1 * E is E
13421347
True
13431348
1349+
::
1350+
1351+
sage: 0 * E is Seq2.zero()
1352+
True
1353+
13441354
We test that ``_rmul_`` and ``_lmul_`` are actually called::
13451355
13461356
sage: def print_name(f):
@@ -1359,9 +1369,11 @@ def _rmul_(self, other):
13591369
_lmul_
13601370
2-regular sequence 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, ...
13611371
"""
1372+
P = self.parent()
1373+
if other.is_zero():
1374+
return P._zero_()
13621375
if other.is_one():
13631376
return self
1364-
P = self.parent()
13651377
return P.element_class(P, self.mu, other*self.left, self.right)
13661378

13671379
def _lmul_(self, other):
@@ -1398,6 +1410,11 @@ def _lmul_(self, other):
13981410
sage: E * 1 is E
13991411
True
14001412
1413+
::
1414+
1415+
sage: E * 0 is Seq2.zero()
1416+
True
1417+
14011418
The following is not tested, as `MS^i` for integers `i` does
14021419
not work, thus ``vector([m])`` fails. (See :trac:`21317` for
14031420
details.)
@@ -1414,9 +1431,11 @@ def _lmul_(self, other):
14141431
sage: M # not tested
14151432
sage: M.linear_representation() # not tested
14161433
"""
1434+
P = self.parent()
1435+
if other.is_zero():
1436+
return P._zero_()
14171437
if other.is_one():
14181438
return self
1419-
P = self.parent()
14201439
return P.element_class(P, self.mu, self.left, self.right*other)
14211440

14221441
@minimize_result
@@ -1913,6 +1932,28 @@ def some_elements(self):
19131932
break
19141933
yield self(mu, *LR)
19151934

1935+
@cached_method
1936+
def _zero_(self):
1937+
r"""
1938+
Return the zero element of this :class:`RecognizableSeriesSpace`,
1939+
i.e. the unique neutral element for `+`.
1940+
1941+
TESTS::
1942+
1943+
sage: Rec = RecognizableSeriesSpace(ZZ, [0, 1])
1944+
sage: Z = Rec._zero_(); Z
1945+
0
1946+
sage: Z.linear_representation()
1947+
((), Finite family {0: [], 1: []}, ())
1948+
"""
1949+
from sage.matrix.constructor import Matrix
1950+
from sage.modules.free_module_element import vector
1951+
from sage.sets.family import Family
1952+
1953+
return self.element_class(
1954+
self, Family(self.alphabet(), lambda a: Matrix()),
1955+
vector([]), vector([]))
1956+
19161957
@cached_method
19171958
def one_hadamard(self):
19181959
r"""
@@ -1985,13 +2026,7 @@ def _element_constructor_(self, data,
19852026
ValueError: left or right vector is None
19862027
"""
19872028
if isinstance(data, int) and data == 0:
1988-
from sage.matrix.constructor import Matrix
1989-
from sage.modules.free_module_element import vector
1990-
from sage.sets.family import Family
1991-
1992-
return self.element_class(
1993-
self, Family(self.alphabet(), lambda a: Matrix()),
1994-
vector([]), vector([]))
2029+
return self._zero_()
19952030

19962031
if type(data) == self.element_class and data.parent() == self:
19972032
element = data

0 commit comments

Comments
 (0)