Skip to content

Commit

Permalink
some details in combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Nov 11, 2023
1 parent 429555a commit 11398b0
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/sage/combinat/combination.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ class of combinations of ``mset`` of size ``k``.
TESTS:
Run the test suites::
sage: C = Combinations([2,3])
sage: TestSuite(C).run()
sage: C = Combinations([2,3], 1)
sage: TestSuite(C).run()
We check that the code works even for non mutable objects::
sage: l = [vector((0,0)), vector((0,1))] # needs sage.modules
Expand Down Expand Up @@ -199,7 +206,7 @@ def __init__(self, mset):
self.mset = mset
Parent.__init__(self, category=FiniteEnumeratedSets())

def __contains__(self, x):
def __contains__(self, x) -> bool:
"""
EXAMPLES::
Expand All @@ -218,7 +225,7 @@ def __contains__(self, x):

return all(i in self.mset for i in x) and len(set(x)) == len(x)

def __eq__(self, other):
def __eq__(self, other) -> bool:
"""
Test for equality.
Expand All @@ -232,7 +239,7 @@ def __eq__(self, other):
"""
return isinstance(other, Combinations_mset) and self.mset == other.mset

def __ne__(self, other):
def __ne__(self, other) -> bool:
"""
Test for unequality.
Expand All @@ -244,7 +251,7 @@ def __ne__(self, other):
"""
return not (self == other)

def __repr__(self):
def __repr__(self) -> str:
"""
TESTS::
Expand All @@ -263,7 +270,7 @@ def __iter__(self):
for k in range(len(self.mset) + 1):
yield from Combinations_msetk(self.mset, k)

def cardinality(self):
def cardinality(self) -> Integer:
"""
TESTS::
Expand All @@ -272,10 +279,8 @@ def cardinality(self):
sage: Combinations(['a','a','b']).cardinality() # needs sage.libs.gap
6
"""
c = 0
for k in range(len(self.mset) + 1):
c += Combinations_msetk(self.mset, k).cardinality()
return c
return ZZ.sum(Combinations_msetk(self.mset, k).cardinality()
for k in range(len(self.mset) + 1))


class Combinations_set(Combinations_mset):
Expand Down Expand Up @@ -332,7 +337,7 @@ def cardinality(self):
sage: Combinations(range(16000)).cardinality() == 2^16000
True
"""
return 2**len(self.mset)
return ZZ(2)**len(self.mset)


class Combinations_msetk(Parent):
Expand All @@ -348,7 +353,7 @@ def __init__(self, mset, k):
self.k = k
Parent.__init__(self, category=FiniteEnumeratedSets())

def __contains__(self, x):
def __contains__(self, x) -> bool:
"""
EXAMPLES::
Expand All @@ -370,7 +375,7 @@ def __contains__(self, x):
return False
return x in Combinations_mset(self.mset) and len(x) == self.k

def __eq__(self, other):
def __eq__(self, other) -> bool:
"""
Test for equality.
Expand All @@ -385,7 +390,7 @@ def __eq__(self, other):
return (isinstance(other, Combinations_msetk) and
self.mset == other.mset and self.k == other.k)

def __ne__(self, other):
def __ne__(self, other) -> bool:
"""
Test for unequality.
Expand All @@ -397,7 +402,7 @@ def __ne__(self, other):
"""
return not (self == other)

def __repr__(self):
def __repr__(self) -> str:
"""
TESTS::
Expand All @@ -422,7 +427,7 @@ def __iter__(self):
yield sum([[self.mset[indices[i]]] * iv[i]
for i in range(len(indices))], [])

def cardinality(self):
def cardinality(self) -> Integer:
"""
Return the size of combinations(mset, k).
Expand Down Expand Up @@ -490,7 +495,7 @@ def __iter__(self):
else:
return self._iterator(self.mset, self.k)

def list(self):
def list(self) -> list:
"""
EXAMPLES::
Expand Down Expand Up @@ -529,7 +534,7 @@ def rank(self, x):
x = [self.mset.index(i) for i in x]
return rank(x, len(self.mset))

def cardinality(self):
def cardinality(self) -> Integer:
"""
Return the size of combinations(set, k).
Expand All @@ -538,7 +543,7 @@ def cardinality(self):
sage: Combinations(range(16000), 5).cardinality()
8732673194560003200
"""
return binomial(len(self.mset), self.k)
return ZZ(binomial(len(self.mset), self.k))


def rank(comb, n, check=True):
Expand Down

0 comments on commit 11398b0

Please sign in to comment.