Skip to content

Commit a62543d

Browse files
author
Release Manager
committed
gh-35582: Fix discrepancy in partitions between provided number and starting partition ### 📚 Description The behavior of `Partitions(n, starting=mu)` is confusing. It iterates over partitions starting with `mu`, but it is not required that `mu` be a partition of `n`. For example: ``` sage: P = Partitions(5, starting=[3,1]); print(P); list(P) Partitions of the integer 5 starting with [3, 1] [[3, 1], [2, 2], [2, 1, 1], [1, 1, 1, 1]] ``` These are partitions of `4` not of `5`. The change is to add a `ValueError` in `Partitions_starting` to assert that `n` must agree with the weight of `mu`. The example above was drawn from the tests, so a few tests are changed so that they now pass. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x ]`. --> - [x] The title is concise, informative, and self-explanatory. - [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 accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #35582 Reported by: trevorkarn Reviewer(s): Travis Scrimshaw, trevorkarn
2 parents cb2f3c0 + 4ec3ad4 commit a62543d

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

src/sage/categories/enumerated_sets.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -810,17 +810,12 @@ def _iterator_from_next(self):
810810
[0, 1, 2, 3, 4]
811811
"""
812812
f = self.first()
813-
yield f
814-
while True:
813+
while not (f is None or f is False):
814+
yield f
815815
try:
816816
f = self.next(f)
817-
except (TypeError, ValueError ):
818-
break
819-
820-
if f is None or f is False:
821-
break
822-
else:
823-
yield f
817+
except (TypeError, ValueError):
818+
f = None
824819

825820
def _iterator_from_unrank(self):
826821
"""

src/sage/combinat/partition.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7451,6 +7451,22 @@ def __init__(self, n, starting_partition):
74517451
sage: Partitions(3, starting=[2,1]).list()
74527452
[[2, 1], [1, 1, 1]]
74537453
7454+
sage: Partitions(7, starting=[2,2,1]).list()
7455+
[[2, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1]]
7456+
7457+
sage: Partitions(7, starting=[3,2]).list()
7458+
[[3, 1, 1, 1, 1],
7459+
[2, 2, 2, 1],
7460+
[2, 2, 1, 1, 1],
7461+
[2, 1, 1, 1, 1, 1],
7462+
[1, 1, 1, 1, 1, 1, 1]]
7463+
7464+
sage: Partitions(4, starting=[3,2]).list()
7465+
[[3, 1], [2, 2], [2, 1, 1], [1, 1, 1, 1]]
7466+
7467+
sage: Partitions(3, starting=[1,1]).list()
7468+
[]
7469+
74547470
TESTS::
74557471
74567472
sage: p = Partitions(3, starting=[2,1])
@@ -7497,8 +7513,24 @@ def first(self):
74977513
74987514
sage: Partitions(3, starting=[2,1]).first()
74997515
[2, 1]
7516+
sage: Partitions(3, starting=[1,1,1]).first()
7517+
[1, 1, 1]
7518+
sage: Partitions(3, starting=[1,1]).first()
7519+
False
7520+
sage: Partitions(3, starting=[3,1]).first()
7521+
[3]
7522+
sage: Partitions(3, starting=[2,2]).first()
7523+
[2, 1]
75007524
"""
7501-
return self._starting
7525+
if sum(self._starting) == self.n:
7526+
return self._starting
7527+
7528+
if (k := self._starting.size()) < self.n:
7529+
mu = list(self._starting) + [1] * (self.n - k)
7530+
return next(Partition(mu))
7531+
7532+
#if self._starting.size() > self.n:
7533+
return self.element_class(self, Partitions(self.n, outer=self._starting).first())
75027534

75037535
def next(self, part):
75047536
"""
@@ -7545,6 +7577,8 @@ def __init__(self, n, ending_partition):
75457577
[[4], [3, 1], [2, 2]]
75467578
sage: Partitions(4, ending=[4]).list()
75477579
[[4]]
7580+
sage: Partitions(4, ending=[5]).list()
7581+
[]
75487582
75497583
TESTS::
75507584
@@ -7554,6 +7588,7 @@ def __init__(self, n, ending_partition):
75547588
Partitions.__init__(self)
75557589
self.n = n
75567590
self._ending = ending_partition
7591+
self._ending_size_is_not_same = (n != sum(self._ending))
75577592

75587593
def _repr_(self):
75597594
"""
@@ -7590,23 +7625,38 @@ def first(self):
75907625
75917626
sage: Partitions(4, ending=[1,1,1,1]).first()
75927627
[4]
7628+
sage: Partitions(4, ending=[5]).first() is None
7629+
True
75937630
"""
7631+
if self._ending and self.n <= self._ending[0] and not (self.n == self._ending[0] and len(self._ending) == 1):
7632+
return None
75947633
return self.element_class(self, [self.n])
75957634

75967635
def next(self, part):
75977636
"""
75987637
Return the next partition after ``part`` in ``self``.
75997638
76007639
EXAMPLES::
7601-
7640+
sage: Partitions(4, ending=[1,1,1,1,1]).next(Partition([4]))
7641+
[3, 1]
7642+
sage: Partitions(4, ending=[3,2]).next(Partition([3,1])) is None
7643+
True
76027644
sage: Partitions(4, ending=[1,1,1,1]).next(Partition([4]))
76037645
[3, 1]
76047646
sage: Partitions(4, ending=[1,1,1,1]).next(Partition([1,1,1,1])) is None
76057647
True
7648+
sage: Partitions(4, ending=[3]).next(Partition([3,1])) is None
7649+
True
76067650
"""
7651+
# if we have passed the last Partition, there is no next partition
76077652
if part == self._ending:
76087653
return None
7609-
return next(part)
7654+
7655+
# if self._ending is a different size, we should make the comparison
7656+
mu = next(part)
7657+
if self._ending_size_is_not_same and mu < self._ending:
7658+
return None
7659+
return mu
76107660

76117661

76127662
class PartitionsInBox(Partitions):

0 commit comments

Comments
 (0)