Module: sage.sets.set
Sets
Author Log:
Module-level Functions
X) |
Return the enumerated set associated to
.
The input object
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
X) |
Create the underlying set of
.
If
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.
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'> 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'>
Usually sets can be used as dictionary keys.
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
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
x) |
Returns true if
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_object
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: loads(S.dumps()) == S True
sage: latex(Set(ZZ)) \mathbf{Z}
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: cardinality,
difference,
intersection,
object,
subsets,
symmetric_difference,
union
self) |
Return the cardinality of this set, which is either an integer or Infinity.
sage: Set(ZZ).cardinality() +Infinity sage: Primes().cardinality() +Infinity sage: Set(GF(5)).cardinality() 5 sage: Set(GF(5^2,'a')).cardinality() 25
self, X) |
Return the intersection of self and X.
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'))) sage: X {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'))) sage: X {0, 1, 2, b, b + 1, b + 2, 2*b, 2*b + 1, 2*b + 2}
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
sage: 2/1 in X True
sage: X = Set(GF(9,'b')).intersection(Set(GF(27,'c'))) sage: X {}
sage: X = Set(GF(9,'b')).intersection(Set(GF(27,'b'))) sage: X {}
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, ...
self, [size=None]) |
Return the Subset object representing the subsets of a set. If size is specified, return the subsets of that size.
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}]
self, X) |
Returns the symmetric difference of self and X.
sage: X = Set([1,2,3]).symmetric_difference(Set([3,4])) sage: X {1, 2, 4}
self, X) |
Return the union of self and X.
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 True sage: GF(5)(2) in X False sage: Set(GF(7)) + Set(GF(3)) {0, 1, 2, 3, 4, 5, 6, 1, 2, 0}
Special Functions: __add__,
__and__,
__cmp__,
__contains__,
__init__,
__iter__,
__or__,
__sub__,
_latex_,
_repr_
self, X) |
Return the union of self and X.
sage: Set(RealField()) + Set(QQ^5) Set-theoretic union of Set of elements of Real Field with 53 bits of precision and Set of elements of Vector space of dimension 5 over Rational Field sage: Set(GF(3)) + Set(GF(2)) {0, 1, 2, 0, 1} sage: Set(GF(2)) + Set(GF(4,'a')) {0, 1, a, a + 1} sage: Set(GF(8,'b')) + Set(GF(4,'a')) {0, 1, b, b + 1, b^2, b^2 + 1, b^2 + b, b^2 + b + 1, a, a + 1, 1, 0}
self, X) |
Returns the intersection of self and X.
sage: Set([2,3]) \& Set([3,4]) {3} sage: Set(ZZ) \& Set(QQ) Set-theoretic intersection of Set of elements of Integer Ring and Set of elements of Rational Field
self, right) |
Compare self and right.
If right is not a Set compare types. If right is also a Set, returns comparison on the underlying objects.
Note:
If
is true this does not necessarily mean
that
is a subset of
. 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
sage: Set(QQ) == Primes() False
self, x) |
Return True if
is in self.
sage: X = Set(ZZ) sage: 5 in X True sage: GF(7)(3) in X True sage: 2/1 in X True 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 {0, 1, 2, 3, 4, 5, 6} sage: 5/3 in X False sage: 5/3 in GF(7) False sage: Set(GF(7)).union(Set(GF(5))) {0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 0} sage: Set(GF(7)).intersection(Set(GF(5))) {}
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
self, X) |
Return the union of self and X.
sage: Set([2,3]) | Set([3,4]) {2, 3, 4} sage: Set(ZZ) | Set(QQ) Set-theoretic union of Set of elements of Integer Ring and Set of elements of Rational Field
self, X) |
Return the difference of self and X.
sage: X = Set(ZZ).difference(Primes()) sage: Y = Set(ZZ) - Primes() sage: X == Y True
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)) \mathbf{Q}
When the object is finite or a special set then the latex representation can be more interesting.
sage: print latex(Primes()) \text{Set of all prime numbers: 2, 3, 5, 7, ...} sage: print latex(Set([1,1,1,5,6])) \left\{1, 5, 6\right\}
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_difference
self, X, Y) |
sage: S = Set(QQ) sage: T = Set(ZZ) sage: X = S.difference(T); X Set-theoretic difference between Set of elements of Rational Field and Set of elements of Integer Ring sage: latex(X) \mathbf{Q} - \mathbf{Z}
sage: loads(X.dumps()) == X True
Functions: cardinality
self) |
This tries to return the cardinality 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)).difference(Set(Primes())) sage: X.cardinality() 8
Special Functions: __cmp__,
__contains__,
__init__,
__iter__,
_latex_,
_repr_
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).difference(Set(QQ)) sage: Y == Set([]) False sage: X = Set(QQ).difference(Set(ZZ)) sage: Y == X False sage: Z = X.difference(Set(ZZ)) sage: Z == X False
This illustrates that equality testing for formal unions can be misleading in general.
sage: X == Set(QQ).difference(Set(ZZ)) True
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).difference(Set(ZZ)) sage: 5 in X False sage: ComplexField().0 in X False sage: sqrt(2) in X # since sqrt(2) is not a numerical approx False sage: sqrt(RR(2)) in X # since sqrt(RR(2)) is a numerical approx True sage: 5/2 in X True
self) |
Return iterator through elements of self.
Self is a formal difference of X and Y and this function is implemented by iterating through the elements of X and for each checking if it is not in Y, and if yielding it.
sage: X = Set(ZZ).difference(Primes()) sage: I = X.__iter__() sage: I.next() 0 sage: I.next() 1 sage: I.next() -1 sage: I.next() -2 sage: I.next() -3
self) |
Return latex representation of self.
sage: X = Set(QQ).difference(Set(ZZ)) sage: latex(X) \mathbf{Q} - \mathbf{Z}
self) |
Return string representation of self.
sage: X = Set(QQ).difference(Set(ZZ)); X Set-theoretic difference between Set of elements of Rational Field and Set of elements of Integer Ring sage: X.rename('Q - Z') sage: X Q - Z
Class: Set_object_enumerated
self, X) |
sage: S = EnumeratedSet(GF(19)); S {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18} sage: print latex(S) \left\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ight\} sage: loads(S.dumps()) == S True
Functions: cardinality,
difference,
frozenset,
intersection,
set,
symmetric_difference,
union
self) |
sage: Set([1,1]).cardinality() 1
self, other) |
Returns the set difference self-other.
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) {2.50000000000000}
self) |
Return the Python frozenset object associated to this set, which is an immutable set (hence hashable).
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 set([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: set objects are unhashable 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) -1390224788 # 32-bit 561411537695332972 # 64-bit sage: type(s) <type 'frozenset'>
self, other) |
Return the intersection of self and other.
sage: X = Set(GF(8,'c')) sage: Y = Set([GF(8,'c').0, 1, 2, 3]) sage: X.intersection(Y) {1, c}
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,'c')) sage: X {0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1} sage: X.set() set([0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1]) sage: type(X.set()) <type 'set'> sage: type(X) <class 'sage.sets.set.Set_object_enumerated'>
self, other) |
Returns the set difference self-other.
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
self, other) |
Return the union of self and other.
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: Y {1, c, 3, 2} sage: X.union(Y) {0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1, 2, 3}
Special Functions: __cmp__,
__init__,
__iter__,
__len__,
_latex_,
_repr_
self, other) |
Compare the sets self and other.
sage: X = Set(GF(8,'c')) sage: X == Set(GF(8,'c')) True sage: X == Set(GF(4,'a')) False sage: Set(QQ) == Set(ZZ) False
self) |
sage: len(Set([1,1])) 1
Class: 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 Set of elements of Vector space of dimension 2 over Rational Field and Set of elements of Integer Ring sage: latex(X) \mathbf{Q}^{2} \cap \mathbf{Z}
sage: loads(X.dumps()) == X True
Functions: cardinality
self) |
This tries to return the cardinality 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.cardinality() 13
Special Functions: __cmp__,
__contains__,
__init__,
__iter__,
_latex_,
_repr_
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
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
Floating-point numbers are rational.
sage: RR(sqrt(2)) in X True
Real constants are not rational:
sage: pi in X False
pi is not in RR either, since the comparison takes place in the symbolic ring.
sage: pi in RR False
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
self) |
Return latex representation of self.
sage: X = Set(ZZ).intersection(Set(QQ)) sage: latex(X) \mathbf{Z} \cap \mathbf{Q}
self) |
Return string representation of self.
sage: X = Set(ZZ).intersection(Set(QQ)); X Set-theoretic intersection of Set of elements of Integer Ring and Set of elements of Rational Field sage: X.rename('Z /\ Q') sage: X Z /\ Q
Class: Set_object_symmetric_difference
self, X, Y) |
sage: S = Set(QQ) sage: T = Set(ZZ) sage: X = S.symmetric_difference(T); X Set-theoretic symmetric difference of Set of elements of Rational Field and Set of elements of Integer Ring sage: latex(X) \mathbf{Q} \bigtriangleup \mathbf{Z}
sage: loads(X.dumps()) == X True
Functions: cardinality
self) |
This tries to return the cardinality of this formal symmetric difference.
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)).symmetric_difference(Set(range(5))) sage: X.cardinality() 8
Special Functions: __cmp__,
__contains__,
__init__,
__iter__,
_latex_,
_repr_
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 symmetric difference of sets in a meaningful manner. So be careful when using this.
sage: Y = Set(ZZ).symmetric_difference(Set(QQ)) sage: X = Set(QQ).symmetric_difference(Set(ZZ)) sage: X == Y True sage: Y == X True
self, x) |
Return true if self contains x.
Since self is the formal symmetric difference of X and Y this function returns true if either X or Y (but now both) contains x.
sage: X = Set(QQ).symmetric_difference(Primes()) sage: 4 in X True sage: ComplexField().0 in X False sage: sqrt(2) in X # since sqrt(2) is currently symbolic False sage: sqrt(RR(2)) in X # since sqrt(RR(2)) is currently approximated True sage: pi in X False sage: 5/2 in X True sage: 3 in X False
self) |
Return iterator through elements of self.
Self is the formal symmetric difference of X and Y. This function is implemented by first iterating through the elements of X and yielding it if it is not in Y. Then it will iterate throw all the elements of Y and yielding it if it is not in X.
sage: X = Set(ZZ).symmetric_difference(Primes()) sage: I = X.__iter__() sage: I.next() 0 sage: I.next() 1 sage: I.next() -1 sage: I.next() -2 sage: I.next() -3
self) |
Return latex representation of self.
sage: X = Set(ZZ).symmetric_difference(Set(QQ)) sage: latex(X) \mathbf{Z} \bigtriangleup \mathbf{Q}
self) |
Return string representation of self.
sage: X = Set(ZZ).symmetric_difference(Set(QQ)); X Set-theoretic symmetric difference of Set of elements of Integer Ring and Set of elements of Rational Field sage: X.rename('Z symdif Q') sage: X Z symdif Q
Class: 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 Set of elements of Vector space of dimension 2 over Rational Field and Set of elements of Integer Ring
sage: latex(X) \mathbf{Q}^{2} \cup \mathbf{Z}
sage: loads(X.dumps()) == X True
Functions: cardinality
self) |
Return the cardinality of this set.
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
Special Functions: __cmp__,
__contains__,
__init__,
__iter__,
_latex_,
_repr_
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
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
self) |
Return iterator over the elements of self.
sage: [x for x in Set(GF(3)).union(Set(GF(2)))] [0, 1, 2, 0, 1]
self) |
Return latex representation of self.
sage: latex(Set(ZZ).union(Set(GF(5)))) \mathbf{Z} \cup \left\{0, 1, 2, 3, 4\right\}
self) |
Return string representation of self.
sage: Set(ZZ).union(Set(GF(5))) Set-theoretic union of Set of elements of Integer Ring and {0, 1, 2, 3, 4}
See About this document... for information on suggesting changes.