Finite Enumerated Sets

class sage.sets.finite_enumerated_set.FiniteEnumeratedSet(elements)

Bases: sage.structure.unique_representation.UniqueRepresentation, sage.structure.parent.Parent

A class for finite enumerated set.

Returns the finite enumerated set with elements in elements where element is any (finite) iterable object.

The main purpose is to provide a variant of list or tuple, which is a parent with an interface consistent with EnumeratedSets and has unique representation. The list of the elements is expanded in memory.

EXAMPLES:

sage: S = FiniteEnumeratedSet([1, 2, 3])
sage: S
{1, 2, 3}
sage: S.list()
[1, 2, 3]
sage: S.cardinality()
3
sage: S.random_element()
1
sage: S.first()
1
sage: S.category()
Category of facade finite enumerated sets
sage: TestSuite(S).run()

Note that being and enumerated set, the result depends on the order:

sage: S1 = FiniteEnumeratedSet((1, 2, 3))
sage: S1
{1, 2, 3}
sage: S1.list()
[1, 2, 3]
sage: S1 == S
True
sage: S2 = FiniteEnumeratedSet((2, 1, 3))
sage: S2 == S
False

As an abuse, repeated entries in elements are allowed to model multisets:

sage: S1 = FiniteEnumeratedSet((1, 2, 1, 2, 2, 3))
sage: S1
{1, 2, 1, 2, 2, 3}

Finaly the elements are not aware of their parent:

sage: S.first().parent()
Integer Ring
an_element()

TESTS:

sage: S = FiniteEnumeratedSet([1,2,3])
sage: S.an_element()
1
cardinality()

TESTS:

sage: S = FiniteEnumeratedSet([1,2,3])
sage: S.cardinality()
3
first()

Return the first element of the enumeration or raise an EmptySetError if the set is empty.

EXAMPLES:

sage: S = FiniteEnumeratedSet('abc')
sage: S.first()
'a'
index(x)

Returns the index of x in this finite enumerated set.

EXAMPLES:

sage: S = FiniteEnumeratedSet(['a','b','c'])
sage: S.index('b')
1
is_parent_of(x)

TESTS:

sage: S = FiniteEnumeratedSet([1,2,3])
sage: 1 in S
True
sage: 2 in S
True
sage: 4 in S
False
sage: ZZ in S
False

sage: S.is_parent_of(2)
True
sage: S.is_parent_of(4)
False
last()

Returns the last element of the iteration or raise an EmptySetError if the set is empty.

EXAMPLES:

sage: S = FiniteEnumeratedSet([0,'a',1.23, 'd'])
sage: S.last()
'd'
list()

TESTS:

sage: S = FiniteEnumeratedSet([1,2,3])
sage: S.list()
[1, 2, 3]
random_element()

Return a random element.

EXAMPLES:

sage: S = FiniteEnumeratedSet('abc')
sage: S.random_element()   # random
'b'
rank(x)

Returns the index of x in this finite enumerated set.

EXAMPLES:

sage: S = FiniteEnumeratedSet(['a','b','c'])
sage: S.index('b')
1
unrank(i)

Return the element at position i.

EXAMPLES:

sage: S = FiniteEnumeratedSet([1,'a',-51])
sage: S[0], S[1], S[2]
(1, 'a', -51)
sage: S[3]
Traceback (most recent call last):
...
IndexError: list index out of range
sage: S[-1], S[-2], S[-3]
(-51, 'a', 1)
sage: S[-4]
Traceback (most recent call last):
...
IndexError: list index out of range

Previous topic

Enumerated set from iterator

Next topic

Maps between finite sets

This Page