diff --git a/src/sage/sets/set.py b/src/sage/sets/set.py index 8c0668724d0..b76e6b064ef 100644 --- a/src/sage/sets/set.py +++ b/src/sage/sets/set.py @@ -693,7 +693,7 @@ def cardinality(self): except TypeError: pass - raise NotImplementedError("computation of cardinality of %s not yet implemented" % self.__object) + return super().cardinality() def is_empty(self): """ @@ -1526,9 +1526,16 @@ def __init__(self, X, Y, category=None): sage: X = Set(IntegerRange(100)).intersection(Primes()) sage: X.is_finite() True + sage: X.cardinality() + 25 sage: X.category() Category of finite enumerated sets sage: TestSuite(X).run() + + sage: X = Set(Primes(), category=Sets()).intersection(Set(IntegerRange(200))) + sage: X.cardinality() + 46 + sage: TestSuite(X).run() """ if category is None: category = Sets() @@ -1538,27 +1545,6 @@ def __init__(self, X, Y, category=None): category = category.Enumerated() Set_object_binary.__init__(self, X, Y, "intersection", "\\cap", category=category) - def cardinality(self): - r""" - Return the cardinality of this set. - - EXAMPLES:: - - sage: X = Set(IntegerRange(100)).intersection(Primes()) - sage: X.cardinality() - 25 - - sage: X = Set(Primes(), category=Sets()).intersection(Set(IntegerRange(200))) - sage: X.cardinality() - 46 - """ - if self in Sets().Infinite(): - return Infinity - if self in Sets().Finite(): - from sage.rings.integer import Integer - return Integer(len(list(iter(self)))) - return super().cardinality() - def is_finite(self): r""" Return whether this set is finite.