diff --git a/src/sage/combinat/combination.py b/src/sage/combinat/combination.py index 8561935b403..916ca836c6b 100644 --- a/src/sage/combinat/combination.py +++ b/src/sage/combinat/combination.py @@ -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 @@ -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:: @@ -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. @@ -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. @@ -244,7 +251,7 @@ def __ne__(self, other): """ return not (self == other) - def __repr__(self): + def __repr__(self) -> str: """ TESTS:: @@ -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:: @@ -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): @@ -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): @@ -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:: @@ -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. @@ -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. @@ -397,7 +402,7 @@ def __ne__(self, other): """ return not (self == other) - def __repr__(self): + def __repr__(self) -> str: """ TESTS:: @@ -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). @@ -490,7 +495,7 @@ def __iter__(self): else: return self._iterator(self.mset, self.k) - def list(self): + def list(self) -> list: """ EXAMPLES:: @@ -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). @@ -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):