Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/structure/gens_py.py
4057 views
1
"""
2
Pure python code for abstract base class for objects with generators
3
"""
4
5
#*****************************************************************************
6
# Copyright (C) 2005 William Stein <[email protected]>
7
#
8
# Distributed under the terms of the GNU General Public License (GPL)
9
#
10
# This code is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
# General Public License for more details.
14
#
15
# The full text of the GPL is available at:
16
#
17
# http://www.gnu.org/licenses/
18
#*****************************************************************************
19
20
21
def multiplicative_iterator(M):
22
from sage.rings.all import infinity
23
G = M.gens()
24
if len(G) == 0:
25
yield M(1)
26
return
27
28
stop = list(M.generator_orders())
29
for i in range(len(stop)):
30
if stop[i] is infinity:
31
raise ArithmeticError, "%s is not finite."%M
32
stop[i] = stop[i] - 1
33
n = 0
34
z = M(1)
35
yield z
36
cnt = [0] * len(G)
37
while cnt != stop:
38
z = z * G[0]
39
cnt[0] = cnt[0] + 1
40
i = 0
41
while i < len(cnt)-1 and cnt[i] > stop[i]:
42
cnt[i] = 0
43
cnt[i+1] = cnt[i+1] + 1
44
z = z * G[i+1]
45
i = i + 1
46
yield z
47
48
def abelian_iterator(M):
49
from sage.rings.all import infinity
50
G = M.gens()
51
if len(G) == 0:
52
yield M(0)
53
return
54
55
stop = list(M.generator_orders())
56
for i in range(len(stop)):
57
if stop[i] is infinity:
58
raise ArithmeticError, "%s is not finite."%M
59
stop[i] = stop[i] - 1
60
n = 0
61
z = M(0)
62
yield z
63
cnt = [0] * len(G)
64
while cnt != stop:
65
cnt[0] = cnt[0] + 1
66
z = z + G[0]
67
i = 0
68
while i < len(cnt)-1 and cnt[i] > stop[i]:
69
cnt[i] = 0
70
cnt[i+1] = cnt[i+1] + 1
71
z = z + G[i+1]
72
i = i + 1
73
yield z
74
75
76
77