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 or InfiniteEnumeratedSets.

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 for len on a list except that the return value is specified to be a Sage Integer or infinity, instead of a Python int.
  • 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): the n-th element of the set when n is a sage Integer. This is the equivalent for l[n] on a list.
  • S.rank(e): the position of the element e in the set; This is equivalent to l.index(e) for a list except that the return value is specified to be a Sage Integer, instead of a Python int.
  • S.first(): the first object of the set; it is equivalent to S.unrank(0).
  • S.next(e): the object of the set which follows e; It is equivalent to S.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]