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
whereelement
is any (finite) iterable object.The main purpose is to provide a variant of
list
ortuple
, which is a parent with an interface consistent withEnumeratedSets
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() # random 1 sage: S.first() 1 sage: S.category() Category of facade finite enumerated sets sage: TestSuite(S).run()
Note that being an 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}
Finally, the elements are not aware of their parent:
sage: S.first().parent() Integer Ring
-
an_element
()¶
-
cardinality
()¶
-
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)¶
-
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
()¶
-
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: tuple 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
-