Skip to content

Commit e4c3645

Browse files
committed
add .__iter__() and .leaves() to ProductTree
1 parent 574d591 commit e4c3645

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/sage/arith/misc.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6369,6 +6369,23 @@ def __len__(self):
63696369
"""
63706370
return len(self.layers[0])
63716371

6372+
def __iter__(self):
6373+
r"""
6374+
Return an iterator over the leaves of this product tree.
6375+
6376+
EXAMPLES::
6377+
6378+
sage: from sage.arith.misc import ProductTree
6379+
sage: R.<x> = GF(101)[]
6380+
sage: vs = [x - i for i in range(1,10)]
6381+
sage: tree = ProductTree(vs)
6382+
sage: next(iter(tree)) == vs[0]
6383+
True
6384+
sage: list(tree) == vs
6385+
True
6386+
"""
6387+
return iter(self.layers[0])
6388+
63726389
def root(self):
63736390
r"""
63746391
Return the value at the root of this product tree (i.e., the product of all leaves).
@@ -6387,6 +6404,23 @@ def root(self):
63876404
assert len(self.layers[-1]) == 1
63886405
return self.layers[-1][0]
63896406

6407+
def leaves(self):
6408+
r"""
6409+
Return a tuple containing the leaves of this product tree.
6410+
6411+
EXAMPLES::
6412+
6413+
sage: from sage.arith.misc import ProductTree
6414+
sage: R.<x> = GF(101)[]
6415+
sage: vs = [x - i for i in range(1,10)]
6416+
sage: tree = ProductTree(vs)
6417+
sage: tree.leaves()
6418+
(x + 100, x + 99, x + 98, ..., x + 93, x + 92)
6419+
sage: tree.leaves() == tuple(vs)
6420+
True
6421+
"""
6422+
return self.layers[0]
6423+
63906424
def remainders(self, x):
63916425
r"""
63926426
Given a value `x`, return a list of all remainders of `x`

0 commit comments

Comments
 (0)