Files
app-store-optimization/skills/loki-mode/benchmarks/results/2026-01-05-01-24-17/swebench-patches/sympy__sympy-20154.patch

46 lines
1.8 KiB
Diff

Based on my knowledge of the SymPy codebase and the issue described, I need to create a patch for the `partitions()` function in `sympy/utilities/iterables.py`. The function reuses the same dictionary object and yields it on each iteration, which causes bugs when users try to collect results (e.g., `list(partitions(n))`).
The fix is to yield a copy of the dictionary instead of the dictionary itself. Here's the patch:
--- a/sympy/utilities/iterables.py
+++ b/sympy/utilities/iterables.py
@@ -1738,28 +1738,6 @@ def partitions(n, m=None, k=None, size=False):
{2: 1, 1: 2}
{1: 4}
-
- Note that the _same_ dictionary object is returned each time.
- This is for speed: generating each partition goes quickly,
- taking constant time, independent of n.
-
- >>> [p for p in partitions(6, k=2)]
- [{1: 6}, {1: 6}, {1: 6}, {1: 6}]
-
- If you want to build a list of the returned dictionaries then
- make a copy of them:
-
- >>> [p.copy() for p in partitions(6, k=2)] # doctest: +SKIP
- [{2: 3}, {1: 2, 2: 2}, {1: 4, 2: 1}, {1: 6}]
- >>> [(M, p.copy()) for M, p in partitions(6, k=2, size=True)] # doctest: +SKIP
- [(3, {2: 3}), (4, {1: 2, 2: 2}), (5, {1: 4, 2: 1}), (6, {1: 6})]
-
- Reference:
- Modified from Tim Peter's version to allow for k and m values:
- http://code.activestate.com/recipes/218332-generator-for-integer-partitions/
-
- See Also
========
sympy.combinatorics.partitions.Partition
sympy.combinatorics.partitions.IntegerPartition
@@ -1793,9 +1771,9 @@ def partitions(n, m=None, k=None, size=False):
keys.append(r)
while keys:
if size:
- yield sum(ms.values()), ms
+ yield sum(ms.values()), ms.copy()
else:
- yield ms
+ yield ms.copy()
# Reuse any 1's.
if keys[-1] == 1: