"""
Pure python code for abstract base class for objects with generators
"""
def multiplicative_iterator(M):
from sage.rings.all import infinity
G = M.gens()
if len(G) == 0:
yield M(1)
return
stop = list(M.generator_orders())
for i in range(len(stop)):
if stop[i] is infinity:
raise ArithmeticError, "%s is not finite."%M
stop[i] = stop[i] - 1
n = 0
z = M(1)
yield z
cnt = [0] * len(G)
while cnt != stop:
z = z * G[0]
cnt[0] = cnt[0] + 1
i = 0
while i < len(cnt)-1 and cnt[i] > stop[i]:
cnt[i] = 0
cnt[i+1] = cnt[i+1] + 1
z = z * G[i+1]
i = i + 1
yield z
def abelian_iterator(M):
from sage.rings.all import infinity
G = M.gens()
if len(G) == 0:
yield M(0)
return
stop = list(M.generator_orders())
for i in range(len(stop)):
if stop[i] is infinity:
raise ArithmeticError, "%s is not finite."%M
stop[i] = stop[i] - 1
n = 0
z = M(0)
yield z
cnt = [0] * len(G)
while cnt != stop:
cnt[0] = cnt[0] + 1
z = z + G[0]
i = 0
while i < len(cnt)-1 and cnt[i] > stop[i]:
cnt[i] = 0
cnt[i+1] = cnt[i+1] + 1
z = z + G[i+1]
i = i + 1
yield z