Enumerated Sets¶
-
sage.categories.enumerated_sets.
EnumeratedSets
¶ The category of enumerated sets
An enumerated set is a finite or countable set or multiset \(S\) together with a canonical enumeration of its elements; conceptually, this is very similar to an immutable list. The main difference lies in the names and the return type of the methods, and of course the fact that the list of elements is not supposed to be expanded in memory. Whenever possible one should use one of the two sub-categories
FiniteEnumeratedSets
orInfiniteEnumeratedSets
.The purpose of this category is threefold:
- to fix a common interface for all these sets;
- to provide a bunch of default implementations;
- to provide consistency tests.
The standard methods for an enumerated set
S
are:S.cardinality()
: the number of elements of the set. This is the equivalent forlen
on a list except that the return value is specified to be a SageInteger
orinfinity
, instead of a Pythonint
.iter(S)
: an iterator for the elements of the set;S.list()
: the list of the elements of the set, when possible; raises a NotImplementedError if the list is predictably too large to be expanded in memory.S.unrank(n)
: then-th
element of the set whenn
is a sageInteger
. This is the equivalent forl[n]
on a list.S.rank(e)
: the position of the elemente
in the set; This is equivalent tol.index(e)
for a list except that the return value is specified to be a SageInteger
, instead of a Pythonint
.S.first()
: the first object of the set; it is equivalent toS.unrank(0)
.S.next(e)
: the object of the set which followse
; It is equivalent toS.unrank(S.rank(e)+1)
.S.random_element()
: a random generator for an element of the set. Unless otherwise stated, and for finite enumerated sets, the probability is uniform.
For examples and tests see:
FiniteEnumeratedSets().example()
InfiniteEnumeratedSets().example()
EXAMPLES:
sage: EnumeratedSets() Category of enumerated sets sage: EnumeratedSets().super_categories() [Category of sets] sage: EnumeratedSets().all_super_categories() [Category of enumerated sets, Category of sets, Category of sets with partial maps, Category of objects]