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

Commit b8746ab

Browse files
committed
Trac #21295 review issue 12: fix inconsistency in __iter__
1 parent eaa7af5 commit b8746ab

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

src/sage/combinat/recognizable_series.py

+35-6
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,13 @@ def _repr_(self, latex=False):
442442
....: vector([0, 1]), vector([1, 0])).transposed()
443443
sage: repr(S) # indirect doctest
444444
'[1] + 3*[01] + [10] + 5*[11] + 9*[001] + 3*[010] + ...'
445+
446+
TESTS::
447+
448+
sage: S = Rec((Matrix([[0]]), Matrix([[0]])),
449+
....: vector([1]), vector([1]))
450+
sage: repr(S) # indirect doctest
451+
'[] + ...'
445452
"""
446453
if not self:
447454
return '0'
@@ -463,8 +470,22 @@ def summand(w, c):
463470
return '[{w}]'.format(w=fs(w))
464471
return '{c}{times}[{w}]'.format(c=fr(c), times=times, w=fs(w))
465472

473+
def all_coefficients():
474+
number_of_nonzeros = 0
475+
for w in self.parent().indices():
476+
c = self[w]
477+
if c != 0:
478+
number_of_nonzeros = 0
479+
yield (w, self[w])
480+
else:
481+
number_of_nonzeros += 1
482+
if number_of_nonzeros >= 100:
483+
return
484+
485+
coefficients = islice(all_coefficients(), 10)
486+
466487
s = ' + '.join(summand(w, c)
467-
for w, c in islice(self, 10))
488+
for w, c in coefficients)
468489
s = s.replace('+ -', '- ')
469490
return s + ' + ...'
470491

@@ -598,6 +619,17 @@ def __iter__(self):
598619
....: left=vector([0, 1]), right=vector([1, 0]))
599620
sage: from itertools import islice
600621
sage: list(islice(S, 10))
622+
[(word: , 0),
623+
(word: 0, 0),
624+
(word: 1, 1),
625+
(word: 00, 0),
626+
(word: 01, 1),
627+
(word: 10, 1),
628+
(word: 11, 2),
629+
(word: 000, 0),
630+
(word: 001, 1),
631+
(word: 010, 1)]
632+
sage: list(islice((s for s in S if s[1] != 0), 10))
601633
[(word: 1, 1),
602634
(word: 01, 1),
603635
(word: 10, 1),
@@ -611,7 +643,7 @@ def __iter__(self):
611643
612644
sage: S = Rec((Matrix([[1, 0], [0, 1]]), Matrix([[0, -1], [1, 2]])),
613645
....: left=vector([1, 0]), right=vector([1, 0]))
614-
sage: list(islice(S, 10))
646+
sage: list(islice((s for s in S if s[1] != 0), 10))
615647
[(word: , 1),
616648
(word: 0, 1),
617649
(word: 00, 1),
@@ -631,10 +663,7 @@ def __iter__(self):
631663
sage: iter(S) is not it
632664
True
633665
"""
634-
if not self:
635-
return iter([])
636-
return iter((w, self[w])
637-
for w in self.parent().indices() if self[w] != 0)
666+
return iter((w, self[w]) for w in self.parent().indices())
638667

639668
def is_trivial_zero(self):
640669
r"""

0 commit comments

Comments
 (0)