Skip to content

Commit

Permalink
Trac #30602: Partitions_n.__iter__ creates partitions with int (inste…
Browse files Browse the repository at this point in the history
…ad of Integer) parts

Several iterators over partitions return partitions whose parts are of
type `int` instead of `Integer`:
{{{
sage: type(Partitions(3)[0][0])
<class 'int'>
sage: type(Partitions(3, length=3)[0][0])
<class 'int'>

sage: type(Partitions(3, max_part=3)[0][0])
<class 'sage.rings.integer.Integer'>
}}}

URL: https://trac.sagemath.org/30602
Reported by: mantepse
Ticket author(s): Mike Hansen
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Oct 10, 2021
2 parents 0580800 + 82e30dc commit 38556c2
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/sage/combinat/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -6655,9 +6655,15 @@ def __iter__(self):
sage: [x for x in Partitions(4)]
[[4], [3, 1], [2, 2], [2, 1, 1], [1, 1, 1, 1]]
TESTS::
sage: all(isinstance(i, Integer) for p in Partitions(4) for i in p)
True
"""
for p in ZS1_iterator(self.n):
yield self.element_class(self, p)
yield self.element_class(self, [Integer(i) for i in p])

def subset(self, **kwargs):
r"""
Expand Down Expand Up @@ -6787,10 +6793,17 @@ def __iter__(self):
....: == number_of_partitions_length(n, k)
....: for n in range(9) for k in range(n+2) )
True
TESTS::
sage: partitions = Partitions(9, length=3)
sage: all(isinstance(i, Integer) for p in partitions for i in p)
True
"""
for p in ZS1_iterator_nk(self.n - self.k, self.k):
v = [i + 1 for i in p]
adds = [1] * (self.k - len(v))
v = [Integer(i + 1) for i in p]
adds = [Integer(1)] * (self.k - len(v))
yield self.element_class(self, v + adds)

def cardinality(self, algorithm='hybrid'):
Expand Down

0 comments on commit 38556c2

Please sign in to comment.