Skip to content

Commit

Permalink
Trac #20317: bug in Permutations_msetk cardinality
Browse files Browse the repository at this point in the history
{{{
sage: P = Permutations([1,1,1,2,2,2,3,3,3], 3)
sage: P.cardinality()
1680
sage: _ = P.list()
sage: P.cardinality()
27
}}}

See the original report on [https://groups.google.com/forum/#!topic
/sage-support/rowWX-_gzFo this sage-support thread]

URL: https://trac.sagemath.org/20317
Reported by: vdelecroix
Ticket author(s): Frédéric Chapoton
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed May 20, 2020
2 parents 3c89214 + bdd22a2 commit 805ee2a
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/sage/combinat/permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
from sage.categories.finite_permutation_groups import FinitePermutationGroups
from sage.structure.list_clone import ClonableArray
from sage.structure.global_options import GlobalOptions
from sage.interfaces.all import gap
from sage.libs.gap.libgap import libgap
from sage.rings.all import ZZ, Integer, PolynomialRing
from sage.arith.all import factorial, multinomial
from sage.matrix.matrix_space import MatrixSpace
Expand Down Expand Up @@ -666,11 +666,11 @@ def _latex_(self):
redword = self.reduced_word()
if not redword:
return self.parent().options.latex_empty_str
return " ".join("%s_{%s}"%(let, i) for i in redword)
return " ".join("%s_{%s}" % (let, i) for i in redword)
if display == "twoline":
return "\\begin{pmatrix} %s \\\\ %s \\end{pmatrix}"%(
" & ".join("%s"%i for i in range(1, len(self._list)+1)),
" & ".join("%s"%i for i in self._list))
return "\\begin{pmatrix} %s \\\\ %s \\end{pmatrix}" % (
" & ".join("%s" % i for i in range(1, len(self._list)+1)),
" & ".join("%s" % i for i in self._list))
if display == "list":
return repr(self._list)
if display == "cycle":
Expand Down Expand Up @@ -1883,7 +1883,7 @@ def _icondition(self, i):
:meth:`ishift`, :meth:`iswitch`
"""
if i not in range(2, len(self)):
raise ValueError("i (= %s) must be between 2 and n-1"%i)
raise ValueError("i (= %s) must be between 2 and n-1" % i)
pos_i = self.index(i)
pos_ip1 = self.index(i+1)
pos_im1 = self.index(i-1)
Expand Down Expand Up @@ -5310,7 +5310,7 @@ def __classcall_private__(cls, n=None, k=None, **kwargs):
if k is None:
return Permutations_mset(n)
else:
return Permutations_msetk(n,k)
return Permutations_msetk(n, k)
elif 'descents' in kwargs:
#Descent positions specified
if isinstance(kwargs['descents'], tuple):
Expand Down Expand Up @@ -5518,6 +5518,7 @@ def random_element(self):
"""
return sample(range(1, self.n+1), self._k)


class Permutations_mset(Permutations):
r"""
Permutations of a multiset `M`.
Expand Down Expand Up @@ -5669,6 +5670,8 @@ def __iter__(self):

def cardinality(self):
"""
Return the cardinality of the set.
EXAMPLES::
sage: Permutations([1,2,2]).cardinality()
Expand Down Expand Up @@ -5791,6 +5794,8 @@ def __iter__(self):

def cardinality(self):
"""
Return the cardinality of the set.
EXAMPLES::
sage: Permutations([1,2,3]).cardinality()
Expand Down Expand Up @@ -5855,7 +5860,8 @@ def __contains__(self, x):
sage: [2,1] in p
True
"""
if len(x) != self._k: return False
if len(x) != self._k:
return False
s = list(self.mset)
for i in x:
if i in s:
Expand All @@ -5871,7 +5877,18 @@ def _repr_(self):
sage: Permutations([1,2,2],2)
Permutations of the multi-set [1, 2, 2] of length 2
"""
return "Permutations of the multi-set %s of length %s"%(list(self.mset), self._k)
return "Permutations of the multi-set %s of length %s" % (list(self.mset), self._k)

def cardinality(self):
"""
Return the cardinality of the set.
EXAMPLES::
sage: Permutations([1,2,2],2).cardinality()
3
"""
return ZZ.sum(1 for z in self)

def __iter__(self):
"""
Expand All @@ -5883,10 +5900,11 @@ def __iter__(self):
mset = self.mset
lmset = list(mset)
mset_list = [lmset.index(x) for x in lmset]
indices = eval(gap.eval('Arrangements(%s,%s)'%(mset_list, self._k)))
indices = libgap.Arrangements(mset_list, self._k).sage()
for ktuple in indices:
yield self.element_class(self, [lmset[x] for x in ktuple])


class Permutations_setk(Permutations_set):
"""
Length-`k` partial permutations of an arbitrary given finite set.
Expand Down

0 comments on commit 805ee2a

Please sign in to comment.