Sets

AUTHORS:

  • William Stein (2005) - first version

  • William Stein (2006-02-16) - large number of documentation and examples; improved code

  • Mike Hansen (2007-3-25) - added differences and symmetric differences; fixed operators

  • Florent Hivert (2010-06-17) - Adapted to categories

  • Nicolas M. Thiery (2011-03-15) - Added subset and superset methods

  • Julian Rueth (2013-04-09) - Collected common code in Set_object_binary, fixed __hash__.

sage.sets.set.Set(X=None, category=None)[source]

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 immutable frozenset type with extra functionality. Otherwise it returns a more formal wrapper.

If you need the functionality of mutable sets, use Python’s builtin set type.

EXAMPLES:

sage: # needs sage.rings.finite_rings
sage: X = Set(GF(9, 'a'))
sage: X
{0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2}
sage: type(X)
<class 'sage.sets.set.Set_object_enumerated_with_category'>
sage: Y = X.union(Set(QQ))
sage: Y
Set-theoretic union of
 {0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} and
 Set of elements of Rational Field
sage: type(Y)
<class 'sage.sets.set.Set_object_union_with_category'>
>>> from sage.all import *
>>> # needs sage.rings.finite_rings
>>> X = Set(GF(Integer(9), 'a'))
>>> X
{0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2}
>>> type(X)
<class 'sage.sets.set.Set_object_enumerated_with_category'>
>>> Y = X.union(Set(QQ))
>>> Y
Set-theoretic union of
 {0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} and
 Set of elements of Rational Field
>>> type(Y)
<class 'sage.sets.set.Set_object_union_with_category'>

Usually sets can be used as dictionary keys.

sage: # needs sage.symbolic
sage: d = {Set([2*I, 1 + I]): 10}
sage: d                  # key is randomly ordered
{{I + 1, 2*I}: 10}
sage: d[Set([1+I,2*I])]
10
sage: d[Set((1+I,2*I))]
10
>>> from sage.all import *
>>> # needs sage.symbolic
>>> d = {Set([Integer(2)*I, Integer(1) + I]): Integer(10)}
>>> d                  # key is randomly ordered
{{I + 1, 2*I}: 10}
>>> d[Set([Integer(1)+I,Integer(2)*I])]
10
>>> d[Set((Integer(1)+I,Integer(2)*I))]
10

The original object is often forgotten.

sage: v = [1,2,3]
sage: X = Set(v)
sage: X
{1, 2, 3}
sage: v.append(5)
sage: X
{1, 2, 3}
sage: 5 in X
False
>>> from sage.all import *
>>> v = [Integer(1),Integer(2),Integer(3)]
>>> X = Set(v)
>>> X
{1, 2, 3}
>>> v.append(Integer(5))
>>> X
{1, 2, 3}
>>> Integer(5) in X
False

Set also accepts iterators, but be careful to only give finite sets:

sage: sorted(Set(range(1,6)))
[1, 2, 3, 4, 5]
sage: sorted(Set(list(range(1,6))))
[1, 2, 3, 4, 5]
sage: sorted(Set(iter(range(1,6))))
[1, 2, 3, 4, 5]
>>> from sage.all import *
>>> sorted(Set(range(Integer(1),Integer(6))))
[1, 2, 3, 4, 5]
>>> sorted(Set(list(range(Integer(1),Integer(6)))))
[1, 2, 3, 4, 5]
>>> sorted(Set(iter(range(Integer(1),Integer(6)))))
[1, 2, 3, 4, 5]

We can also create sets from different types:

sage: sorted(Set([Sequence([3,1], immutable=True), 5, QQ, Partition([3,1,1])]), key=str)    # needs sage.combinat
[5, Rational Field, [3, 1, 1], [3, 1]]
>>> from sage.all import *
>>> sorted(Set([Sequence([Integer(3),Integer(1)], immutable=True), Integer(5), QQ, Partition([Integer(3),Integer(1),Integer(1)])]), key=str)    # needs sage.combinat
[5, Rational Field, [3, 1, 1], [3, 1]]

Sets with unhashable objects work, but with less functionality:

sage: A = Set([QQ, (3, 1), 5])  # hashable
sage: sorted(A.list(), key=repr)
[(3, 1), 5, Rational Field]
sage: type(A)
<class 'sage.sets.set.Set_object_enumerated_with_category'>
sage: B = Set([QQ, [3, 1], 5])  # unhashable
sage: sorted(B.list(), key=repr)
Traceback (most recent call last):
...
AttributeError: 'Set_object_with_category' object has no attribute 'list'...
sage: type(B)
<class 'sage.sets.set.Set_object_with_category'>
>>> from sage.all import *
>>> A = Set([QQ, (Integer(3), Integer(1)), Integer(5)])  # hashable
>>> sorted(A.list(), key=repr)
[(3, 1), 5, Rational Field]
>>> type(A)
<class 'sage.sets.set.Set_object_enumerated_with_category'>
>>> B = Set([QQ, [Integer(3), Integer(1)], Integer(5)])  # unhashable
>>> sorted(B.list(), key=repr)
Traceback (most recent call last):
...
AttributeError: 'Set_object_with_category' object has no attribute 'list'...
>>> type(B)
<class 'sage.sets.set.Set_object_with_category'>
class sage.sets.set.Set_add_sub_operators[source]

Bases: object

Mix-in class providing the operators __add__ and __sub__.

The operators delegate to the methods union and intersection, which need to be implemented by the class.

class sage.sets.set.Set_base[source]

Bases: object

Abstract base class for sets, not necessarily parents.

difference(X)[source]

Return the set difference self - X.

EXAMPLES:

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

sage: 4/1 in X
True

sage: X = Set(GF(9,'b')).difference(Set(GF(27,'c'))); X                     # needs sage.rings.finite_rings
{0, 1, 2, b, b + 1, b + 2, 2*b, 2*b + 1, 2*b + 2}

sage: X = Set(GF(9,'b')).difference(Set(GF(27,'b'))); X                     # needs sage.rings.finite_rings
{0, 1, 2, b, b + 1, b + 2, 2*b, 2*b + 1, 2*b + 2}
>>> from sage.all import *
>>> X = Set(ZZ).difference(Primes())
>>> Integer(4) in X
True
>>> Integer(3) in X
False

>>> Integer(4)/Integer(1) in X
True

>>> X = Set(GF(Integer(9),'b')).difference(Set(GF(Integer(27),'c'))); X                     # needs sage.rings.finite_rings
{0, 1, 2, b, b + 1, b + 2, 2*b, 2*b + 1, 2*b + 2}

>>> X = Set(GF(Integer(9),'b')).difference(Set(GF(Integer(27),'b'))); X                     # needs sage.rings.finite_rings
{0, 1, 2, b, b + 1, b + 2, 2*b, 2*b + 1, 2*b + 2}
intersection(X)[source]

Return the intersection of self and X.

EXAMPLES:

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

sage: 2/1 in X
True

sage: X = Set(GF(9,'b')).intersection(Set(GF(27,'c'))); X                   # needs sage.rings.finite_rings
{}

sage: X = Set(GF(9,'b')).intersection(Set(GF(27,'b'))); X                   # needs sage.rings.finite_rings
{}
>>> from sage.all import *
>>> X = Set(ZZ).intersection(Primes())
>>> Integer(4) in X
False
>>> Integer(3) in X
True

>>> Integer(2)/Integer(1) in X
True

>>> X = Set(GF(Integer(9),'b')).intersection(Set(GF(Integer(27),'c'))); X                   # needs sage.rings.finite_rings
{}

>>> X = Set(GF(Integer(9),'b')).intersection(Set(GF(Integer(27),'b'))); X                   # needs sage.rings.finite_rings
{}
symmetric_difference(X)[source]

Return the symmetric difference of self and X.

EXAMPLES:

sage: X = Set([1,2,3]).symmetric_difference(Set([3,4]))
sage: X
{1, 2, 4}
>>> from sage.all import *
>>> X = Set([Integer(1),Integer(2),Integer(3)]).symmetric_difference(Set([Integer(3),Integer(4)]))
>>> X
{1, 2, 4}
union(X)[source]

Return the union of self and X.

EXAMPLES:

sage: Set(QQ).union(Set(ZZ))
Set-theoretic union of
 Set of elements of Rational Field and
 Set of elements of Integer Ring
sage: Set(QQ) + Set(ZZ)
Set-theoretic union of
 Set of elements of Rational Field and
 Set of elements of Integer Ring
sage: X = Set(QQ).union(Set(GF(3))); X
Set-theoretic union of
 Set of elements of Rational Field and
 {0, 1, 2}
sage: 2/3 in X
True
sage: GF(3)(2) in X                                                         # needs sage.libs.pari
True
sage: GF(5)(2) in X
False
sage: sorted(Set(GF(7)) + Set(GF(3)), key=int)
[0, 0, 1, 1, 2, 2, 3, 4, 5, 6]
>>> from sage.all import *
>>> Set(QQ).union(Set(ZZ))
Set-theoretic union of
 Set of elements of Rational Field and
 Set of elements of Integer Ring
>>> Set(QQ) + Set(ZZ)
Set-theoretic union of
 Set of elements of Rational Field and
 Set of elements of Integer Ring
>>> X = Set(QQ).union(Set(GF(Integer(3)))); X
Set-theoretic union of
 Set of elements of Rational Field and
 {0, 1, 2}
>>> Integer(2)/Integer(3) in X
True
>>> GF(Integer(3))(Integer(2)) in X                                                         # needs sage.libs.pari
True
>>> GF(Integer(5))(Integer(2)) in X
False
>>> sorted(Set(GF(Integer(7))) + Set(GF(Integer(3))), key=int)
[0, 0, 1, 1, 2, 2, 3, 4, 5, 6]
class sage.sets.set.Set_boolean_operators[source]

Bases: object

Mix-in class providing the Boolean operators __or__, __and__, __xor__.

The operators delegate to the methods union, intersection, and symmetric_difference, which need to be implemented by the class.

class sage.sets.set.Set_object(X, category=None)[source]

Bases: Set_generic, Set_base, Set_boolean_operators, Set_add_sub_operators

A set attached to an almost arbitrary object.

EXAMPLES:

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

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

sage: latex(Set(ZZ))
\Bold{Z}
>>> from sage.all import *
>>> K = GF(Integer(19))
>>> Set(K)
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
>>> S = Set(K)

>>> latex(S)
\left\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18\right\}
>>> TestSuite(S).run()

>>> latex(Set(ZZ))
\Bold{Z}
cardinality()[source]

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

EXAMPLES:

sage: Set(ZZ).cardinality()
+Infinity
sage: Primes().cardinality()
+Infinity
sage: Set(GF(5)).cardinality()
5
sage: Set(GF(5^2,'a')).cardinality()                                        # needs sage.rings.finite_rings
25
>>> from sage.all import *
>>> Set(ZZ).cardinality()
+Infinity
>>> Primes().cardinality()
+Infinity
>>> Set(GF(Integer(5))).cardinality()
5
>>> Set(GF(Integer(5)**Integer(2),'a')).cardinality()                                        # needs sage.rings.finite_rings
25
is_empty()[source]

Return boolean representing emptiness of the set.

OUTPUT: True if the set is empty, False otherwise

EXAMPLES:

sage: Set([]).is_empty()
True
sage: Set([0]).is_empty()
False
sage: Set([1..100]).is_empty()
False
sage: Set(SymmetricGroup(2).list()).is_empty()                              # needs sage.groups
False
sage: Set(ZZ).is_empty()
False
>>> from sage.all import *
>>> Set([]).is_empty()
True
>>> Set([Integer(0)]).is_empty()
False
>>> Set((ellipsis_range(Integer(1),Ellipsis,Integer(100)))).is_empty()
False
>>> Set(SymmetricGroup(Integer(2)).list()).is_empty()                              # needs sage.groups
False
>>> Set(ZZ).is_empty()
False
is_finite()[source]

Return True if self is finite.

EXAMPLES:

sage: Set(QQ).is_finite()
False
sage: Set(GF(250037)).is_finite()                                           # needs sage.rings.finite_rings
True
sage: Set(Integers(2^1000000)).is_finite()
True
sage: Set([1,'a',ZZ]).is_finite()
True
>>> from sage.all import *
>>> Set(QQ).is_finite()
False
>>> Set(GF(Integer(250037))).is_finite()                                           # needs sage.rings.finite_rings
True
>>> Set(Integers(Integer(2)**Integer(1000000))).is_finite()
True
>>> Set([Integer(1),'a',ZZ]).is_finite()
True
object()[source]

Return underlying object.

EXAMPLES:

sage: X = Set(QQ)
sage: X.object()
Rational Field
sage: X = Primes()
sage: X.object()
Set of all prime numbers: 2, 3, 5, 7, ...
>>> from sage.all import *
>>> X = Set(QQ)
>>> X.object()
Rational Field
>>> X = Primes()
>>> X.object()
Set of all prime numbers: 2, 3, 5, 7, ...
subsets(size=None)[source]

Return the Subsets object representing the subsets of a set. If size is specified, return the subsets of that size.

EXAMPLES:

sage: X = Set([1, 2, 3])
sage: list(X.subsets())
[{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]
sage: list(X.subsets(2))
[{1, 2}, {1, 3}, {2, 3}]
>>> from sage.all import *
>>> X = Set([Integer(1), Integer(2), Integer(3)])
>>> list(X.subsets())
[{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]
>>> list(X.subsets(Integer(2)))
[{1, 2}, {1, 3}, {2, 3}]
subsets_lattice()[source]

Return the lattice of subsets ordered by containment.

EXAMPLES:

sage: X = Set([1,2,3])
sage: X.subsets_lattice()                                                   # needs sage.graphs
Finite lattice containing 8 elements
sage: Y = Set()
sage: Y.subsets_lattice()                                                   # needs sage.graphs
Finite lattice containing 1 elements
>>> from sage.all import *
>>> X = Set([Integer(1),Integer(2),Integer(3)])
>>> X.subsets_lattice()                                                   # needs sage.graphs
Finite lattice containing 8 elements
>>> Y = Set()
>>> Y.subsets_lattice()                                                   # needs sage.graphs
Finite lattice containing 1 elements
class sage.sets.set.Set_object_binary(X, Y, op, latex_op, category=None)[source]

Bases: Set_object

An abstract common base class for sets defined by a binary operation (ex. Set_object_union, Set_object_intersection, Set_object_difference, and Set_object_symmetric_difference).

INPUT:

  • X, Y – sets, the operands to op

  • op – string describing the binary operation

  • latex_op – string used for rendering this object in LaTeX

EXAMPLES:

sage: X = Set(QQ^2)                                                             # needs sage.modules
sage: Y = Set(ZZ)
sage: from sage.sets.set import Set_object_binary
sage: S = Set_object_binary(X, Y, "union", "\\cup"); S                          # needs sage.modules
Set-theoretic union of
 Set of elements of Vector space of dimension 2 over Rational Field and
 Set of elements of Integer Ring
>>> from sage.all import *
>>> X = Set(QQ**Integer(2))                                                             # needs sage.modules
>>> Y = Set(ZZ)
>>> from sage.sets.set import Set_object_binary
>>> S = Set_object_binary(X, Y, "union", "\\cup"); S                          # needs sage.modules
Set-theoretic union of
 Set of elements of Vector space of dimension 2 over Rational Field and
 Set of elements of Integer Ring
class sage.sets.set.Set_object_difference(X, Y, category=None)[source]

Bases: Set_object_binary

Formal difference of two sets.

is_finite()[source]

Return whether this set is finite.

EXAMPLES:

sage: X = Set(range(10))
sage: Y = Set(range(-10,5))
sage: Z = Set(QQ)
sage: X.difference(Y).is_finite()
True
sage: X.difference(Z).is_finite()
True
sage: Z.difference(X).is_finite()
False
sage: Z.difference(Set(ZZ)).is_finite()
Traceback (most recent call last):
...
NotImplementedError
>>> from sage.all import *
>>> X = Set(range(Integer(10)))
>>> Y = Set(range(-Integer(10),Integer(5)))
>>> Z = Set(QQ)
>>> X.difference(Y).is_finite()
True
>>> X.difference(Z).is_finite()
True
>>> Z.difference(X).is_finite()
False
>>> Z.difference(Set(ZZ)).is_finite()
Traceback (most recent call last):
...
NotImplementedError
class sage.sets.set.Set_object_enumerated(X, category=None)[source]

Bases: Set_object

A finite enumerated set.

cardinality()[source]

Return the cardinality of self.

EXAMPLES:

sage: Set([1,1]).cardinality()
1
>>> from sage.all import *
>>> Set([Integer(1),Integer(1)]).cardinality()
1
difference(other)[source]

Return the set difference self - other.

EXAMPLES:

sage: X = Set([1,2,3,4])
sage: Y = Set([1,2])
sage: X.difference(Y)
{3, 4}
sage: Z = Set(ZZ)
sage: W = Set([2.5, 4, 5, 6])
sage: W.difference(Z)                                                       # needs sage.rings.real_mpfr
{2.50000000000000}
>>> from sage.all import *
>>> X = Set([Integer(1),Integer(2),Integer(3),Integer(4)])
>>> Y = Set([Integer(1),Integer(2)])
>>> X.difference(Y)
{3, 4}
>>> Z = Set(ZZ)
>>> W = Set([RealNumber('2.5'), Integer(4), Integer(5), Integer(6)])
>>> W.difference(Z)                                                       # needs sage.rings.real_mpfr
{2.50000000000000}
frozenset()[source]

Return the Python frozenset object associated to this set, which is an immutable set (hence hashable).

EXAMPLES:

sage: # needs sage.rings.finite_rings
sage: X = Set(GF(8,'c'))
sage: X
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
sage: s = X.set(); s
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
sage: hash(s)
Traceback (most recent call last):
...
TypeError: unhashable type: 'set'
sage: s = X.frozenset(); s
frozenset({0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1})

sage: hash(s) != hash(tuple(X.set()))                                       # needs sage.rings.finite_rings
True

sage: type(s)                                                               # needs sage.rings.finite_rings
<... 'frozenset'>
>>> from sage.all import *
>>> # needs sage.rings.finite_rings
>>> X = Set(GF(Integer(8),'c'))
>>> X
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
>>> s = X.set(); s
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
>>> hash(s)
Traceback (most recent call last):
...
TypeError: unhashable type: 'set'
>>> s = X.frozenset(); s
frozenset({0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1})

>>> hash(s) != hash(tuple(X.set()))                                       # needs sage.rings.finite_rings
True

>>> type(s)                                                               # needs sage.rings.finite_rings
<... 'frozenset'>
intersection(other)[source]

Return the intersection of self and other.

EXAMPLES:

sage: X = Set(GF(8,'c'))                                                    # needs sage.rings.finite_rings
sage: Y = Set([GF(8,'c').0, 1, 2, 3])                                       # needs sage.rings.finite_rings
sage: sorted(X.intersection(Y), key=str)                                    # needs sage.rings.finite_rings
[1, c]
>>> from sage.all import *
>>> X = Set(GF(Integer(8),'c'))                                                    # needs sage.rings.finite_rings
>>> Y = Set([GF(Integer(8),'c').gen(0), Integer(1), Integer(2), Integer(3)])                                       # needs sage.rings.finite_rings
>>> sorted(X.intersection(Y), key=str)                                    # needs sage.rings.finite_rings
[1, c]
is_finite()[source]

Return True as this is a finite set.

EXAMPLES:

sage: Set(GF(19)).is_finite()
True
>>> from sage.all import *
>>> Set(GF(Integer(19))).is_finite()
True
issubset(other)[source]

Return whether self is a subset of other.

INPUT:

  • other – a finite Set

EXAMPLES:

sage: X = Set([1,3,5])
sage: Y = Set([0,1,2,3,5,7])
sage: X.issubset(Y)
True
sage: Y.issubset(X)
False
sage: X.issubset(X)
True
>>> from sage.all import *
>>> X = Set([Integer(1),Integer(3),Integer(5)])
>>> Y = Set([Integer(0),Integer(1),Integer(2),Integer(3),Integer(5),Integer(7)])
>>> X.issubset(Y)
True
>>> Y.issubset(X)
False
>>> X.issubset(X)
True
issuperset(other)[source]

Return whether self is a superset of other.

INPUT:

  • other – a finite Set

EXAMPLES:

sage: X = Set([1,3,5])
sage: Y = Set([0,1,2,3,5])
sage: X.issuperset(Y)
False
sage: Y.issuperset(X)
True
sage: X.issuperset(X)
True
>>> from sage.all import *
>>> X = Set([Integer(1),Integer(3),Integer(5)])
>>> Y = Set([Integer(0),Integer(1),Integer(2),Integer(3),Integer(5)])
>>> X.issuperset(Y)
False
>>> Y.issuperset(X)
True
>>> X.issuperset(X)
True
list()[source]

Return the elements of self, as a list.

EXAMPLES:

sage: # needs sage.rings.finite_rings
sage: X = Set(GF(8,'c'))
sage: X
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
sage: X.list()
[0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1]
sage: type(X.list())
<... 'list'>
>>> from sage.all import *
>>> # needs sage.rings.finite_rings
>>> X = Set(GF(Integer(8),'c'))
>>> X
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
>>> X.list()
[0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1]
>>> type(X.list())
<... 'list'>

Todo

FIXME: What should be the order of the result? That of self.object()? Or the order given by set(self.object())? Note that __getitem__() is currently implemented in term of this list method, which is really inefficient …

random_element()[source]

Return a random element in this set.

EXAMPLES:

sage: Set([1,2,3]).random_element() # random
2
>>> from sage.all import *
>>> Set([Integer(1),Integer(2),Integer(3)]).random_element() # random
2
set()[source]

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.

EXAMPLES:

sage: # needs sage.rings.finite_rings
sage: X = Set(GF(8,'c'))
sage: X
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
sage: X.set()
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
sage: type(X.set())
<... 'set'>
sage: type(X)
<class 'sage.sets.set.Set_object_enumerated_with_category'>
>>> from sage.all import *
>>> # needs sage.rings.finite_rings
>>> X = Set(GF(Integer(8),'c'))
>>> X
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
>>> X.set()
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
>>> type(X.set())
<... 'set'>
>>> type(X)
<class 'sage.sets.set.Set_object_enumerated_with_category'>
symmetric_difference(other)[source]

Return the symmetric difference of self and other.

EXAMPLES:

sage: X = Set([1,2,3,4])
sage: Y = Set([1,2])
sage: X.symmetric_difference(Y)
{3, 4}
sage: Z = Set(ZZ)
sage: W = Set([2.5, 4, 5, 6])
sage: U = W.symmetric_difference(Z)
sage: 2.5 in U
True
sage: 4 in U
False
sage: V = Z.symmetric_difference(W)
sage: V == U
True
sage: 2.5 in V
True
sage: 6 in V
False
>>> from sage.all import *
>>> X = Set([Integer(1),Integer(2),Integer(3),Integer(4)])
>>> Y = Set([Integer(1),Integer(2)])
>>> X.symmetric_difference(Y)
{3, 4}
>>> Z = Set(ZZ)
>>> W = Set([RealNumber('2.5'), Integer(4), Integer(5), Integer(6)])
>>> U = W.symmetric_difference(Z)
>>> RealNumber('2.5') in U
True
>>> Integer(4) in U
False
>>> V = Z.symmetric_difference(W)
>>> V == U
True
>>> RealNumber('2.5') in V
True
>>> Integer(6) in V
False
union(other)[source]

Return the union of self and other.

EXAMPLES:

sage: # needs sage.rings.finite_rings
sage: X = Set(GF(8,'c'))
sage: Y = Set([GF(8,'c').0, 1, 2, 3])
sage: X
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
sage: sorted(Y)
[1, 2, 3, c]
sage: sorted(X.union(Y), key=str)
[0, 1, 2, 3, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1]
>>> from sage.all import *
>>> # needs sage.rings.finite_rings
>>> X = Set(GF(Integer(8),'c'))
>>> Y = Set([GF(Integer(8),'c').gen(0), Integer(1), Integer(2), Integer(3)])
>>> X
{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}
>>> sorted(Y)
[1, 2, 3, c]
>>> sorted(X.union(Y), key=str)
[0, 1, 2, 3, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1]
class sage.sets.set.Set_object_intersection(X, Y, category=None)[source]

Bases: Set_object_binary

Formal intersection of two sets.

is_finite()[source]

Return whether this set is finite.

EXAMPLES:

sage: X = Set(IntegerRange(100))
sage: Y = Set(ZZ)
sage: X.intersection(Y).is_finite()
True
sage: Y.intersection(X).is_finite()
True
sage: Y.intersection(Set(QQ)).is_finite()
Traceback (most recent call last):
...
NotImplementedError
>>> from sage.all import *
>>> X = Set(IntegerRange(Integer(100)))
>>> Y = Set(ZZ)
>>> X.intersection(Y).is_finite()
True
>>> Y.intersection(X).is_finite()
True
>>> Y.intersection(Set(QQ)).is_finite()
Traceback (most recent call last):
...
NotImplementedError
class sage.sets.set.Set_object_symmetric_difference(X, Y, category=None)[source]

Bases: Set_object_binary

Formal symmetric difference of two sets.

is_finite()[source]

Return whether this set is finite.

EXAMPLES:

sage: X = Set(range(10))
sage: Y = Set(range(-10,5))
sage: Z = Set(QQ)
sage: X.symmetric_difference(Y).is_finite()
True
sage: X.symmetric_difference(Z).is_finite()
False
sage: Z.symmetric_difference(X).is_finite()
False
sage: Z.symmetric_difference(Set(ZZ)).is_finite()
Traceback (most recent call last):
...
NotImplementedError
>>> from sage.all import *
>>> X = Set(range(Integer(10)))
>>> Y = Set(range(-Integer(10),Integer(5)))
>>> Z = Set(QQ)
>>> X.symmetric_difference(Y).is_finite()
True
>>> X.symmetric_difference(Z).is_finite()
False
>>> Z.symmetric_difference(X).is_finite()
False
>>> Z.symmetric_difference(Set(ZZ)).is_finite()
Traceback (most recent call last):
...
NotImplementedError
class sage.sets.set.Set_object_union(X, Y, category=None)[source]

Bases: Set_object_binary

A formal union of two sets.

cardinality()[source]

Return the cardinality of this set.

EXAMPLES:

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

sage: X = Set(GF(3)).union(Set(ZZ))
sage: X.cardinality()
+Infinity
>>> from sage.all import *
>>> X = Set(GF(Integer(3))).union(Set(GF(Integer(2))))
>>> X
{0, 1, 2, 0, 1}
>>> X.cardinality()
5

>>> X = Set(GF(Integer(3))).union(Set(ZZ))
>>> X.cardinality()
+Infinity
is_finite()[source]

Return whether this set is finite.

EXAMPLES:

sage: X = Set(range(10))
sage: Y = Set(range(-10,0))
sage: Z = Set(Primes())
sage: X.union(Y).is_finite()
True
sage: X.union(Z).is_finite()
False
>>> from sage.all import *
>>> X = Set(range(Integer(10)))
>>> Y = Set(range(-Integer(10),Integer(0)))
>>> Z = Set(Primes())
>>> X.union(Y).is_finite()
True
>>> X.union(Z).is_finite()
False
sage.sets.set.has_finite_length(obj)[source]

Return True if obj is known to have finite length.

This is mainly meant for pure Python types, so we do not call any Sage-specific methods.

EXAMPLES:

sage: from sage.sets.set import has_finite_length
sage: has_finite_length(tuple(range(10)))
True
sage: has_finite_length(list(range(10)))
True
sage: has_finite_length(set(range(10)))
True
sage: has_finite_length(iter(range(10)))
False
sage: has_finite_length(GF(17^127))                                             # needs sage.rings.finite_rings
True
sage: has_finite_length(ZZ)
False
>>> from sage.all import *
>>> from sage.sets.set import has_finite_length
>>> has_finite_length(tuple(range(Integer(10))))
True
>>> has_finite_length(list(range(Integer(10))))
True
>>> has_finite_length(set(range(Integer(10))))
True
>>> has_finite_length(iter(range(Integer(10))))
False
>>> has_finite_length(GF(Integer(17)**Integer(127)))                                             # needs sage.rings.finite_rings
True
>>> has_finite_length(ZZ)
False