Skip to content

Commit c007973

Browse files
author
Release Manager
committed
gh-38961: Iteration over infinite abelian groups Fixes #30751. This PR adds code to the `__iter__` method of `AbelianGroup` to handle infinite groups. For backward compatibility (and because the finite case is simpler and probably more efficient), the code for the finite case has been retained. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. URL: #38961 Reported by: DaveWitteMorris Reviewer(s): DaveWitteMorris, Martin Rubey
2 parents 09bcdd9 + 4c5a738 commit c007973

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/sage/groups/abelian_gps/abelian_group.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,10 +1301,26 @@ def __iter__(self):
13011301
(1,)
13021302
sage: list(G)
13031303
[1]
1304+
1305+
We can also iterate over infinite groups::
1306+
1307+
sage: A = AbelianGroup([3,0,5,0])
1308+
sage: for a in A:
1309+
....: if a^2 == A([1, 2, 3, 4]):
1310+
....: print(a, a^2)
1311+
....: break
1312+
f0^2*f1*f2^4*f3^2 f0*f1^2*f2^3*f3^4
13041313
"""
13051314
invs = self.gens_orders()
1306-
for t in mrange(invs):
1307-
yield self(t)
1315+
if 0 not in invs:
1316+
# The group is finite
1317+
yield from map(self, mrange(invs))
1318+
else:
1319+
# A similar approach works for infinite groups.
1320+
# (This would also work for finite groups, but is more complicated.)
1321+
from sage.misc.mrange import cantor_product
1322+
yield from map(self, cantor_product(*[range(n) if n
1323+
else ZZ for n in invs]))
13081324

13091325
def number_of_subgroups(self, order=None):
13101326
r"""

0 commit comments

Comments
 (0)