3.8 Sets

Module: sage.sets.set

Author Log:

Module-level Functions

EnumeratedSet( X)

Return the enumerated set associated to $ X$ .

The input object $ X$ must be finite.

sage: EnumeratedSet([1,1,2,3])
{1, 2, 3}
sage: EnumeratedSet(ZZ)
Traceback (most recent call last):
...
ValueError: X (=Integer Ring) must be finite

Set( X)

Create the underlying set of $ X$ .

If $ X$ is a list, tuple, Python set, or X.is_finite() is true, this returns a wrapper around Python's enumerated set type with extra functionality. Otherwise it returns a more formal wrapper.

sage: X = Set(GF(9))
sage: X
{a, 2*a + 1, a + 2, a + 1, 2*a + 2, 1, 0, 2, 2*a}
sage: type(X)
<class 'sage.sets.set.Set_object_enumerated'>
sage: Y = X.union(Set(QQ))
sage: Y
Set-theoretic union of Finite Field in a of size 3^2 and Rational Field
sage: type(Y)
<class 'sage.sets.set.Set_object_union'>

is_Set( x)

Returns true if $ x$ is a SAGE Set (not to be confused with a Python 2.4 set).

sage: is_Set([1,2,3])
False
sage: is_Set(set([1,2,3]))
False
sage: is_Set(Set([1,2,3]))
True
sage: is_Set(Set(QQ))
True
sage: is_Set(Primes())
True

Class: Set_generic

class Set_generic
Abstract base class for sets.

Functions: category,$  $ object

category( self)

The category that this set belongs to, which is the category of all sets.

sage: Set(QQ).category()
Category of sets

Class: Set_object

class Set_object
A set attached to an almost arbitrary object.

sage: K = GF(19)
sage: Set(K)
{11, 10, 13, 12, 15, 14, 17, 16, 18, 1, 0, 3, 2, 5, 4, 7, 6, 9, 8}
sage: S = Set(K)

sage: latex(S)
\left\{11, 10, 13, 12, 15, 14, 17, 16, 18, 1, 0, 3, 2, 5, 4, 7, 6, 9,
8\right\}
sage: loads(S.dumps()) == S
True

sage: latex(Set(ZZ))
\mbox{\bf{}Z}
Set_object( self, X)

Create a Set_object

This function is called by the Set function; users shouldn't call this directly.

sage: type(Set(QQ))
<class 'sage.sets.set.Set_object'>

Functions: intersection,$  $ object,$  $ order,$  $ union

intersection( self, X)

Return the intersection of self and X.

sage: X = Set(ZZ).intersection(Primes())
sage: 4 in X
False
sage: 3 in X
True

This is false since $ \mathbf{Q}$ does not have a canonical coercion map to $ \mathbf{Z}$ .

sage: 2/1 in X
False

This intersection is empty because canonical coercions are only defined if they are defined on the whole parent.

sage: X = Set(GF(9)).intersection(Set(GF(27)))
sage: X
{}

object( self)

Return underlying object.

sage: X = Set(QQ)
sage: X.object()
Rational Field
sage: X = Primes()
sage: X.object()
Set of all prime numbers: 2, 3, 5, 7, ...

order( self)

Return the order of this set, which is either an integer or Infinity.

sage: Set(ZZ).order()
Infinity
sage: Primes().order()
Infinity
sage: Set(GF(5)).order()
5
sage: Set(GF(5^2)).order()
25

union( self, X)

Return the union of self and X.

sage: Set(QQ).union(Set(ZZ))
Set-theoretic union of Rational Field and Integer Ring
sage: Set(QQ) + Set(ZZ)
Set-theoretic union of Rational Field and Integer Ring
sage: X = Set(QQ).union(Set(GF(3))); X
Set-theoretic union of Rational Field and Finite Field of size 3
sage: 2/3 in X
True
sage: GF(3)(2) in X
True
sage: GF(5)(2) in X
False
sage: Set(GF(7)) + Set(GF(3))
{1, 0, 1, 0, 3, 2, 5, 4, 6, 2}

Special Functions: __add__,$  $ __cmp__,$  $ __contains__,$  $ __iter__,$  $ _latex_,$  $ _repr_

__add__( self, X)

Return the union of self and X.

sage: Set(RealField()) + Set(QQ^5)
Set-theoretic union of Real Field with 53 bits of precision and Vector
space of dimension 5 over Rational Field
sage: Set(GF(3)) + Set(GF(2))
{1, 0, 2, 1, 0}
sage: Set(GF(2)) + Set(GF(4))
{1, 0, a, a + 1}
sage: Set(GF(8)) + Set(GF(4))
{a, a^2 + 1, a + 1, a^2, 0, a + 1, 1, 0, 1, a, a^2 + a, a^2 + a + 1}

__cmp__( self, right)

Compare self and right.

If right is not a Set always returns -1. If right is also a Set, returns comparison on the underlying objects.

Note: If $ X < Y$ is true this does not necessarily mean that $ X$ is a subset of $ Y$ . Also, any two sets can be compared, which is a general Python philosophy.

sage: Set(ZZ) == Set(QQ)
False
sage: Set(ZZ) < Set(QQ)
True
sage: Primes() == Set(QQ)
False
sage: Primes() < Set(QQ)
True

This illustrates that < is not the same as containment.

sage: Set(QQ) < Primes()
True
sage: Primes() < Set(QQ)
True

__contains__( self, x)

Return True if $ x$ is in self.

This usually means that x can be canonically coerced into self. Note that "x in G" for most objects besides sets means that x can be coerced into G, not necessarily canonically. But for sets the coercion has to be canonical (for objects that support a notion of canonical coercion, i.e., an _coerce_ method).

sage: X = Set(ZZ)
sage: 5 in X
True
sage: GF(7)(3) in X
False
sage: 2/1 in X
False
sage: 2/1 in ZZ
True
sage: 2/3 in X
False

Finite fields better illustrate the difference between __contains__ for objects and their underlying sets.

sage: X = Set(GF(7))
sage: X
{1, 0, 3, 2, 5, 4, 6}
sage: 5/3 in X
False
sage: 5/3 in GF(7)
True
sage: Set(GF(7)).union(Set(GF(5)))
{1, 0, 3, 1, 0, 3, 2, 5, 4, 6, 2, 4}
sage: Set(GF(7)).intersection(Set(GF(5)))
{}

__iter__( self)

Iterate over the elements of this set.

sage: X = Set(ZZ)
sage: I = X.__iter__()
sage: I.next()
0
sage: I.next()
1
sage: I.next()
-1
sage: I.next()
2

_latex_( self)

Return latex representation of this set.

This is often the same as the latex representation of this object when the object is infinite.

sage: latex(Set(QQ))
\mbox{\bf{}Q}

When the object is finite or a special set then the latex representation can be more interesting.

sage: print latex(Primes())
\mbox{\rm Set of all prime numbers: 2, 3, 5, 7, ...}
sage: print latex(Set([1,1,1,5,6]))
\left\{1, 5, 6\right\}

_repr_( self)

Print representation of this set.

sage: X = Set(ZZ)
sage: X
Set of elements of Integer Ring
sage: X.rename('{ integers }')
sage: X
{ integers }

Class: Set_object_enumerated

class Set_object_enumerated
A finite enumerated set.
Set_object_enumerated( self, X)

sage: S = EnumeratedSet(GF(19)); S
{11, 10, 13, 12, 15, 14, 17, 16, 18, 1, 0, 3, 2, 5, 4, 7, 6, 9, 8}
sage: print latex(S)
\left\{11, 10, 13, 12, 15, 14, 17, 16, 18, 1, 0, 3, 2, 5, 4, 7, 6, 9,
8
ight\}
sage: loads(S.dumps()) == S
True

Functions: intersection,$  $ order,$  $ set,$  $ union

intersection( self, other)

Return the intersection of self and other.

sage: X = Set(GF(8))
sage: Y = Set([GF(8).0, 1, 2, 3])
sage: X.intersection(Y)
{a}

order( self)

sage: Set([1,1]).order()
1

set( self)

Return the Python set object associated to this set.

Python has a notion of finite set, and often SAGE sets have an associated Python set. This function returns that set.

sage: X = Set(GF(8))
sage: X
{a, a^2 + 1, a + 1, a^2, 1, 0, a^2 + a, a^2 + a + 1}
sage: X.set()
set([a, a^2 + 1, a + 1, a^2, 1, 0, a^2 + a, a^2 + a + 1])
sage: type(X.set())
<type 'set'>
sage: type(X)
<class 'sage.sets.set.Set_object_enumerated'>

union( self, other)

Return the union of self and other.

sage: X = Set(GF(8))
sage: Y = Set([GF(8).0, 1, 2, 3])
sage: X
{a, a^2 + 1, a + 1, a^2, 1, 0, a^2 + a, a^2 + a + 1}
sage: Y
{a, 1, 2, 3}
sage: X.union(Y)
{a, a^2 + 1, 2, 3, a + 1, a^2, 1, 1, 0, a^2 + a, a^2 + a + 1}

Special Functions: __cmp__,$  $ __iter__,$  $ __len__,$  $ _latex_,$  $ _repr_

__cmp__( self, other)

Compare the sets self and other.

sage: X = Set(GF(8))
sage: X == Set(GF(8))
True
sage: X == Set(GF(4))
False
sage: Set(QQ) == Set(ZZ)
False

__len__( self)

sage: len(Set([1,1]))
1

Class: Set_object_intersection

class Set_object_intersection
Formal intersection of two sets.
Set_object_intersection( self, X, Y)

sage: S = Set(QQ^2)
sage: T = Set(ZZ)
sage: X = S.intersection(T); X
Set-theoretic intersection of Vector space of dimension 2 over Rational
Field and Integer Ring
sage: latex(X)
\mbox{\bf{}Q}^{2} \cap \mbox{\bf{}Z}

sage: loads(X.dumps()) == X
True

Functions: order

order( self)

This tries to return the order of this formal intersection.

Note that this is not likely to work in very much generality, and may just hang if either set involved is infinite.

sage: X = Set(GF(13)).intersection(Set(ZZ))
sage: X.order()
0

Special Functions: __cmp__,$  $ __contains__,$  $ __iter__,$  $ _latex_,$  $ _repr_

__cmp__( self, right)

Try to compare self and right.

Note: Comparison is basically not implemented, or rather it could say sets are not equal even though they are. I don't know how one could implement this for a generic intersection of sets in a meaningful manner. So be careful when using this.

sage: Y = Set(ZZ).intersection(Set(QQ))
sage: X = Set(QQ).intersection(Set(ZZ))
sage: X == Y
True
sage: Y == X
True

This illustrates that equality testing for formal unions can be misleading in general.

sage: Set(ZZ).intersection(Set(QQ)) == Set(QQ)
False

__contains__( self, x)

Return true if self contains x.

Since self is a formal intersection of X and Y this function returns true if both X and Y contains x.

sage: X = Set(QQ).intersection(Set(RealField()))
sage: 5 in X
True
sage: ComplexField().0 in X
False
sage: sqrt(2) in X
False
sage: pi in X
False
sage: pi in RR
True

__iter__( self)

Return iterator through elements of self.

Self is a formal intersection of X and Y and this function is implemented by iterating through the elements of X and for each checking if it is in Y, and if yielding it.

sage: X = Set(ZZ).intersection(Primes())
sage: I = X.__iter__()
sage: I.next()
2

_latex_( self)

Return latex representation of self.

sage: X = Set(ZZ).intersection(Set(QQ))
sage: latex(X)
\mbox{\bf{}Z} \cap \mbox{\bf{}Q}

_repr_( self)

Return string representation of self.

sage: X = Set(ZZ).intersection(Set(QQ)); X
Set-theoretic intersection of Integer Ring and Rational Field
sage: X.rename('Z /\ Q')
sage: X
Z /\ Q

Class: Set_object_union

class Set_object_union
A formal union of two sets.
Set_object_union( self, X, Y)

sage: S = Set(QQ^2)
sage: T = Set(ZZ)
sage: X = S.union(T); X
Set-theoretic union of Vector space of dimension 2 over Rational Field and
Integer Ring

sage: latex(X)
\mbox{\bf{}Q}^{2} \cup \mbox{\bf{}Z}

sage: loads(X.dumps()) == X
True

Functions: order

order( self)

Return the order of this set.

sage: X = Set(GF(3)).union(Set(GF(2)))
sage: X
{1, 0, 2, 1, 0}
sage: X.order()
5

sage: X = Set(GF(3)).union(Set(ZZ))
sage: X.order()
Infinity

Special Functions: __cmp__,$  $ __contains__,$  $ __iter__,$  $ _latex_,$  $ _repr_

__cmp__( self, right)

Try to compare self and right.

Note: Comparison is basically not implemented, or rather it could say sets are not equal even though they are. I don't know how one could implement this for a generic union of sets in a meaningful manner. So be careful when using this.

sage: Y = Set(ZZ^2).union(Set(ZZ^3))
sage: X = Set(ZZ^3).union(Set(ZZ^2))
sage: X == Y
True
sage: Y == X
True

This illustrates that equality testing for formal unions can be misleading in general.

sage: Set(ZZ).union(Set(QQ)) == Set(QQ)
False

__contains__( self, x)

Returns True if x is an element of self.

sage: X = Set(GF(3)).union(Set(GF(2)))
sage: GF(5)(1) in X
False
sage: GF(3)(2) in X
True
sage: GF(2)(0) in X
True
sage: GF(5)(0) in X
False

__iter__( self)

Return iterator over the elements of self.

sage: [x for x in Set(GF(3)).union(Set(GF(2)))]
[1, 0, 2, 1, 0]

_latex_( self)

Return latex representation of self.

sage: latex(Set(ZZ).union(Set(GF(5))))
\mbox{\bf{}Z} \cup \left\{1, 0, 3, 2, 4\right\}

_repr_( self)

Return string representation of self.

sage: Set(ZZ).union(Set(GF(5)))
Set-theoretic union of Integer Ring and Finite Field of size 5

See About this document... for information on suggesting changes.