From 1f8c715cc5a6a1b5c7b74149b2d1b4a7cef81321 Mon Sep 17 00:00:00 2001 From: Martin Rubey Date: Fri, 10 Oct 2025 11:50:43 +0200 Subject: [PATCH 1/2] fix cardinality of Permutations_setk --- src/sage/combinat/permutation.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/sage/combinat/permutation.py b/src/sage/combinat/permutation.py index eba7f54cbad..bb08db3e039 100644 --- a/src/sage/combinat/permutation.py +++ b/src/sage/combinat/permutation.py @@ -243,7 +243,7 @@ import operator from typing import TYPE_CHECKING -from sage.arith.misc import factorial, multinomial +from sage.arith.misc import factorial, multinomial, falling_factorial from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets from sage.categories.finite_permutation_groups import FinitePermutationGroups from sage.categories.finite_weyl_groups import FiniteWeylGroups @@ -2144,7 +2144,7 @@ def ishift(self, i): Return the ``i``-shift of ``self``. If an ``i``-shift of ``self`` can't be performed, then ``self`` is returned. - An `i`-shift can be applied when `i` is not inbetween `i-1` and + An `i`-shift can be applied when `i` is not between `i-1` and `i+1`. The `i`-shift moves `i` to the other side, and leaves the relative positions of `i-1` and `i+1` in place. All other entries of the permutations are also left in place. @@ -6243,7 +6243,7 @@ def __init__(self, n, k): """ TESTS:: - sage: P = Permutations(3,2) + sage: P = Permutations(5, 3) sage: TestSuite(P).run() """ self.n = ZZ(n) @@ -6334,7 +6334,7 @@ def cardinality(self) -> Integer: 0 """ if 0 <= self._k <= self.n: - return factorial(self.n) // factorial(self.n - self._k) + return falling_factorial(self.n, self._k) return ZZ.zero() def random_element(self): @@ -6409,7 +6409,7 @@ def __init__(self, mset): """ TESTS:: - sage: S = Permutations(['c','a','c']) + sage: S = Permutations(['c','a','c','d']) sage: TestSuite(S).run() """ self.mset = mset @@ -6898,7 +6898,7 @@ def __init__(self, mset, k): """ TESTS:: - sage: P = Permutations([1,2,2],2) + sage: P = Permutations([1,2,2,3], 2) sage: TestSuite(P).run() # needs sage.libs.gap """ Permutations_mset.__init__(self, mset) @@ -7014,12 +7014,24 @@ def __init__(self, s, k): """ TESTS:: - sage: P = Permutations([1,2,4],2) + sage: P = Permutations([1,2,4,5], 2) sage: TestSuite(P).run() """ Permutations_set.__init__(self, s) self._k = k + def cardinality(self) -> Integer: + """ + EXAMPLES:: + + sage: Permutations([1,2,4,5], 2).cardinality() + 12 + """ + n = len(self._set) + if 0 <= self._k <= n: + return falling_factorial(n, self._k) + return ZZ.zero() + def __contains__(self, x): """ EXAMPLES:: From 4db2e93d1f66bc3bd3197035fbc07cf85d21e677 Mon Sep 17 00:00:00 2001 From: Martin Rubey Date: Sat, 11 Oct 2025 13:00:00 +0200 Subject: [PATCH 2/2] add minimal docstring to the cardinality methods --- src/sage/combinat/permutation.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/sage/combinat/permutation.py b/src/sage/combinat/permutation.py index bb08db3e039..86029f32592 100644 --- a/src/sage/combinat/permutation.py +++ b/src/sage/combinat/permutation.py @@ -6320,6 +6320,8 @@ def __iter__(self) -> Iterator[Permutation]: def cardinality(self) -> Integer: """ + Return the cardinality of the set. + EXAMPLES:: sage: Permutations(3,0).cardinality() @@ -7022,6 +7024,8 @@ def __init__(self, s, k): def cardinality(self) -> Integer: """ + Return the cardinality of the set. + EXAMPLES:: sage: Permutations([1,2,4,5], 2).cardinality() @@ -9853,6 +9857,8 @@ def __init__(self, n): def cardinality(self): """ + Return the cardinality of the set. + EXAMPLES:: sage: Permutations(5, avoiding=[1, 3, 2]).cardinality() @@ -9930,6 +9936,8 @@ def __init__(self, n): def cardinality(self) -> Integer: """ + Return the cardinality of the set. + EXAMPLES:: sage: Permutations(5, avoiding=[1, 2, 3]).cardinality() @@ -10002,6 +10010,8 @@ def __init__(self, n): def cardinality(self): """ + Return the cardinality of the set. + EXAMPLES:: sage: Permutations(5, avoiding=[3, 2, 1]).cardinality() @@ -10034,6 +10044,8 @@ def __init__(self, n): def cardinality(self): """ + Return the cardinality of the set. + EXAMPLES:: sage: Permutations(5, avoiding=[2, 3, 1]).cardinality() @@ -10066,6 +10078,8 @@ def __init__(self, n): def cardinality(self): """ + Return the cardinality of the set. + EXAMPLES:: sage: Permutations(5, avoiding=[3, 1, 2]).cardinality() @@ -10098,6 +10112,8 @@ def __init__(self, n): def cardinality(self): """ + Return the cardinality of the set. + EXAMPLES:: sage: Permutations(5, avoiding=[2, 1, 3]).cardinality()