Enumerated Sets

class sage.categories.enumerated_sets.EnumeratedSets(s=None)

Bases: sage.categories.category.Category

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 element 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 equivanlent 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]

TESTS:

sage: C = EnumeratedSets()
sage: TestSuite(C).run()
class ElementMethods
rank()

Returns the rank of self in its parent.

See also EnumeratedSets.ElementMethods.rank()

EXAMPLES:

sage: F = FiniteSemigroups().example(('a','b','c'))
sage: L = list(F); L
['a', 'c', 'ac', 'b', 'ba', 'bc', 'cb', 'ca', 'bca', 'ab', 'bac', 'cab', 'acb', 'cba', 'abc']
sage: L[7].rank()
7
class EnumeratedSets.ParentMethods
cardinality()

The cardinality of self.

self.cardinality() should return the cardinality of the set self as a sage Integer or as infinity.

This if the default implementation from the category EnumeratedSets() it returns NotImplementedError since one does not know whether the set is finite or not.

EXAMPLES:

sage: class broken(UniqueRepresentation, Parent):
...    def __init__(self):
...        Parent.__init__(self, category = EnumeratedSets())
...
sage: broken().cardinality()
...
NotImplementedError: unknown cardinality
first()

The “first” element of self.

self.first() returns the first element of the set self. This is a generic implementation from the category EnumeratedSets() which can be used when the method __iter__ is provided.

EXAMPLES:

sage: C = FiniteEnumeratedSets().example()
sage: C.first() # indirect doctest
1
list()

Returns an error since the cardinality of self is not known.

EXAMPLES:

sage: class broken(UniqueRepresentation, Parent):
...    def __init__(self):
...        Parent.__init__(self, category = EnumeratedSets())
...
sage: broken().list()
...
NotImplementedError: unknown cardinality
next(obj)

The “next” element after obj in self.

self.next(e) returns the element of the set self which follows e. This is a generic implementation from the category EnumeratedSets() which can be used when the method __iter__ is provided.

Remark: this is the default (brute force) implementation of the category EnumeratedSets(). Its complexity is O(r), where r is the rank of obj.

EXAMPLES:

sage: C = InfiniteEnumeratedSets().example()
sage: C._next_from_iterator(10) # indirect doctest
11

TODO: specify the behavior when obj is not in self.

random_element()

Returns a random element in self.

Unless otherwise stated, and for finite enumerated sets, the probability is uniform.

This is a generic implementation from the category EnumeratedSets(). It raise a NotImplementedError since one does not know whether the set is finite.

EXAMPLES:

sage: class broken(UniqueRepresentation, Parent):
...    def __init__(self):
...        Parent.__init__(self, category = EnumeratedSets())
...
sage: broken().random_element()
...
NotImplementedError: unknown cardinality
rank(x)

The rank of an element of self

self.unrank(x) returns the rank of x, that is its position in the enumeration of self. This is an integer between 0 and n-1 where n is the cardinality of self, or None if x is not in self.

This is the default (brute force) implementation from the category EnumeratedSets() which can be used when the method __iter__ is provided. Its complexity is O(r), where r is the rank of obj. For infinite enumerated sets, this won’t terminate when x is not in self

EXAMPLES:

sage: C = FiniteEnumeratedSets().example()
sage: list(C)
[1, 2, 3]
sage: C.rank(3) # indirect doctest
2
sage: C.rank(5) # indirect doctest
some_elements()

Returns some elements in self.

See TestSuite for a typical use case.

This is a generic implementation from the category EnumeratedSets() which can be used when the method __iter__ is provided. It returns an iterator for up to the first 100 elements of self

EXAMPLES:

sage: C = FiniteEnumeratedSets().example()
sage: list(C.some_elements()) # indirect doctest
[1, 2, 3]
unrank(r)

The r-th element of self

self.unrank(r) returns the r-th element of self where r is an integer between 0 and n-1 where n is the cardinality of self.

This is the default (brute force) implementation from the category EnumeratedSets() which can be used when the method __iter__ is provided. Its complexity is O(r), where r is the rank of obj.

EXAMPLES:

sage: C = FiniteEnumeratedSets().example()
sage: C.unrank(2) # indirect doctest
3
sage: C.unrank(5) # indirect doctest
...
ValueError: the value must be between 0 and 2 inclusive
EnumeratedSets.super_categories(*args, **kwds)

EXAMPLES:

sage: EnumeratedSets().super_categories()
[Category of sets]

Previous topic

Domains

Next topic

EuclideanDomains

This Page