The base class for any kind of set.
This is not meant to be used directly as a container of items. It does not behave like the builtin set; see FiniteSet for that.
Real intervals are represented by the Interval class and unions of sets by the Union class. The empty set is represented by the EmptySet class and available as a singleton as S.EmptySet.
Attributes
is_Complement | |
is_EmptySet | |
is_Intersection | |
is_UniversalSet |
The boundary or frontier of a set
A point x is on the boundary of a set S if
There are the points on the outer rim of S. If S is open then these points need not actually be contained within S.
For example, the boundary of an interval is its start and end points. This is true regardless of whether or not the interval is open.
Examples
>>> from sympy import Interval
>>> Interval(0, 1).boundary
{0, 1}
>>> Interval(0, 1, True, False).boundary
{0, 1}
The complement of ‘self’ w.r.t the given the universe.
Examples
>>> from sympy import Interval, S
>>> Interval(0, 1).complement(S.Reals)
(-oo, 0) U (1, oo)
>>> Interval(0, 1).complement(S.UniversalSet)
UniversalSet() \ [0, 1]
Returns True if ‘other’ is contained in ‘self’ as an element.
As a shortcut it is possible to use the ‘in’ operator:
Examples
>>> from sympy import Interval
>>> Interval(0, 1).contains(0.5)
True
>>> 0.5 in Interval(0, 1)
True
The infimum of ‘self’
Examples
>>> from sympy import Interval, Union
>>> Interval(0, 1).inf
0
>>> Union(Interval(0, 1), Interval(2, 3)).inf
0
Returns the intersection of ‘self’ and ‘other’.
>>> from sympy import Interval
>>> Interval(1, 3).intersect(Interval(1, 2))
[1, 2]
>>> from sympy import imageset, Lambda, symbols, S
>>> n, m = symbols('n m')
>>> a = imageset(Lambda(n, 2*n), S.Integers)
>>> a.intersect(imageset(Lambda(m, 2*m + 1), S.Integers))
EmptySet()
Alias for intersect()
Returns True if ‘self’ and ‘other’ are disjoint
References
[R435] | http://en.wikipedia.org/wiki/Disjoint_sets |
Examples
>>> from sympy import Interval
>>> Interval(0, 2).is_disjoint(Interval(1, 2))
False
>>> Interval(0, 2).is_disjoint(Interval(3, 4))
True
Returns True if ‘self’ is a proper subset of ‘other’.
Examples
>>> from sympy import Interval
>>> Interval(0, 0.5).is_proper_subset(Interval(0, 1))
True
>>> Interval(0, 1).is_proper_subset(Interval(0, 1))
False
Returns True if ‘self’ is a proper superset of ‘other’.
Examples
>>> from sympy import Interval
>>> Interval(0, 1).is_proper_superset(Interval(0, 0.5))
True
>>> Interval(0, 1).is_proper_superset(Interval(0, 1))
False
Returns True if ‘self’ is a subset of ‘other’.
Examples
>>> from sympy import Interval
>>> Interval(0, 0.5).is_subset(Interval(0, 1))
True
>>> Interval(0, 1).is_subset(Interval(0, 1, left_open=True))
False
Returns True if ‘self’ is a superset of ‘other’.
Examples
>>> from sympy import Interval
>>> Interval(0, 0.5).is_superset(Interval(0, 1))
False
>>> Interval(0, 1).is_superset(Interval(0, 1, left_open=True))
True
Alias for is_disjoint()
Alias for is_subset()
Alias for is_superset()
The (Lebesgue) measure of ‘self’
Examples
>>> from sympy import Interval, Union
>>> Interval(0, 1).measure
1
>>> Union(Interval(0, 1), Interval(2, 3)).measure
2
Find the Power set of ‘self’.
References
[R436] | http://en.wikipedia.org/wiki/Power_set |
Examples
>>> from sympy import FiniteSet, EmptySet
>>> A = EmptySet()
>>> A.powerset()
{EmptySet()}
>>> A = FiniteSet(1, 2)
>>> a, b, c = FiniteSet(1), FiniteSet(2), FiniteSet(1, 2)
>>> A.powerset() == FiniteSet(a, b, c, EmptySet())
True
The supremum of ‘self’
Examples
>>> from sympy import Interval, Union
>>> Interval(0, 1).sup
1
>>> Union(Interval(0, 1), Interval(2, 3)).sup
3
Returns the union of ‘self’ and ‘other’.
Examples
As a shortcut it is possible to use the ‘+’ operator:
>>> from sympy import Interval, FiniteSet
>>> Interval(0, 1).union(Interval(2, 3))
[0, 1] U [2, 3]
>>> Interval(0, 1) + Interval(2, 3)
[0, 1] U [2, 3]
>>> Interval(1, 2, True, True) + FiniteSet(2, 3)
(1, 2] U {3}
Similarly it is possible to use the ‘-‘ operator for set differences:
>>> Interval(0, 2) - Interval(0, 1)
(1, 2]
>>> Interval(1, 3) - FiniteSet(2)
[1, 2) U (2, 3]
Image of set under transformation f.
If this function can’t compute the image, it returns an unevaluated ImageSet object.
See also
Examples
>>> from sympy import Interval, Symbol, imageset, sin, Lambda
>>> x = Symbol('x')
>>> imageset(x, 2*x, Interval(0, 2))
[0, 4]
>>> imageset(lambda x: 2*x, Interval(0, 2))
[0, 4]
>>> imageset(Lambda(x, sin(x)), Interval(-2, 1))
ImageSet(Lambda(x, sin(x)), [-2, 1])
Represents a real interval as a Set.
Returns an interval with end points “start” and “end”.
For left_open=True (default left_open is False) the interval will be open on the left. Similarly, for right_open=True the interval will be open on the right.
Notes
References
[R437] | http://en.wikipedia.org/wiki/Interval_%28mathematics%29 |
Examples
>>> from sympy import Symbol, Interval
>>> Interval(0, 1)
[0, 1]
>>> Interval(0, 1, False, True)
[0, 1)
>>> Interval.Ropen(0, 1)
[0, 1)
>>> Interval.Lopen(0, 1)
(0, 1]
>>> Interval.open(0, 1)
(0, 1)
>>> a = Symbol('a', real=True)
>>> Interval(0, a)
[0, a]
Attributes
is_Complement | |
is_EmptySet | |
is_Intersection | |
is_UniversalSet |
The right end point of ‘self’.
This property takes the same value as the ‘sup’ property.
Examples
>>> from sympy import Interval
>>> Interval(0, 1).end
1
The left end point of ‘self’.
This property takes the same value as the ‘inf’ property.
Examples
>>> from sympy import Interval
>>> Interval(0, 1).start
0
True if ‘self’ is left-open.
Examples
>>> from sympy import Interval
>>> Interval(0, 1, left_open=True).left_open
True
>>> Interval(0, 1, left_open=False).left_open
False
The right end point of ‘self’.
This property takes the same value as the ‘sup’ property.
Examples
>>> from sympy import Interval
>>> Interval(0, 1).end
1
Represents a finite set of discrete numbers
References
[R438] | http://en.wikipedia.org/wiki/Finite_set |
Examples
>>> from sympy import FiniteSet
>>> FiniteSet(1, 2, 3, 4)
{1, 2, 3, 4}
>>> 3 in FiniteSet(1, 2, 3, 4)
True
>>> members = [1, 2, 3, 4]
>>> FiniteSet(*members)
{1, 2, 3, 4}
Attributes
is_Complement | |
is_EmptySet | |
is_Intersection | |
is_UniversalSet |
Represents a union of sets as a Set.
See also
References
[R439] | http://en.wikipedia.org/wiki/Union_%28set_theory%29 |
Examples
>>> from sympy import Union, Interval
>>> Union(Interval(1, 2), Interval(3, 4))
[1, 2] U [3, 4]
The Union constructor will always try to merge overlapping intervals, if possible. For example:
>>> Union(Interval(1, 2), Interval(2, 3))
[1, 3]
Attributes
is_Complement | |
is_EmptySet | |
is_Intersection | |
is_UniversalSet |
Represents an intersection of sets as a Set.
See also
References
[R440] | http://en.wikipedia.org/wiki/Intersection_%28set_theory%29 |
Examples
>>> from sympy import Intersection, Interval
>>> Intersection(Interval(1, 3), Interval(2, 4))
[2, 3]
We often use the .intersect method
>>> Interval(1,3).intersect(Interval(2,4))
[2, 3]
Attributes
is_Complement | |
is_EmptySet | |
is_UniversalSet |
Represents a Cartesian Product of Sets.
Returns a Cartesian product given several sets as either an iterable or individual arguments.
Can use ‘*’ operator on any sets for convenient shorthand.
Notes
References
[R441] | http://en.wikipedia.org/wiki/Cartesian_product |
Examples
>>> from sympy import Interval, FiniteSet, ProductSet
>>> I = Interval(0, 5); S = FiniteSet(1, 2, 3)
>>> ProductSet(I, S)
[0, 5] x {1, 2, 3}
>>> (2, 2) in ProductSet(I, S)
True
>>> Interval(0, 1) * Interval(0, 1) # The unit square
[0, 1] x [0, 1]
>>> coin = FiniteSet('H', 'T')
>>> set(coin**2)
set([(H, H), (H, T), (T, H), (T, T)])
Attributes
is_Complement | |
is_EmptySet | |
is_Intersection | |
is_UniversalSet |
Represents the set difference or relative complement of a set with another set.
\(A - B = \{x \in A| x \notin B\}\)
See also
References
[R442] | http://mathworld.wolfram.com/ComplementSet.html |
Examples
>>> from sympy import Complement, FiniteSet
>>> Complement(FiniteSet(0, 1, 2), FiniteSet(1))
{0, 2}
Attributes
is_EmptySet | |
is_Intersection | |
is_UniversalSet |
Simplify a Complement.
Represents the empty set. The empty set is available as a singleton as S.EmptySet.
See also
References
[R443] | http://en.wikipedia.org/wiki/Empty_set |
Examples
>>> from sympy import S, Interval
>>> S.EmptySet
EmptySet()
>>> Interval(1, 2).intersect(S.EmptySet)
EmptySet()
Attributes
is_Complement | |
is_Intersection | |
is_UniversalSet |
Represents the set of all things. The universal set is available as a singleton as S.UniversalSet
See also
References
[R444] | http://en.wikipedia.org/wiki/Universal_set |
Examples
>>> from sympy import S, Interval
>>> S.UniversalSet
UniversalSet()
>>> Interval(1, 2).intersect(S.UniversalSet)
[1, 2]
Attributes
is_Complement | |
is_EmptySet | |
is_Intersection |
Represents the natural numbers (or counting numbers) which are all positive integers starting from 1. This set is also available as the Singleton, S.Naturals.
See also
Examples
>>> from sympy import S, Interval, pprint
>>> 5 in S.Naturals
True
>>> iterable = iter(S.Naturals)
>>> next(iterable)
1
>>> next(iterable)
2
>>> next(iterable)
3
>>> pprint(S.Naturals.intersect(Interval(0, 10)))
{1, 2, ..., 10}
Attributes
is_Complement | |
is_EmptySet | |
is_Intersection | |
is_UniversalSet |
Represents all integers: positive, negative and zero. This set is also available as the Singleton, S.Integers.
Examples
>>> from sympy import S, Interval, pprint
>>> 5 in S.Naturals
True
>>> iterable = iter(S.Integers)
>>> next(iterable)
0
>>> next(iterable)
1
>>> next(iterable)
-1
>>> next(iterable)
2
>>> pprint(S.Integers.intersect(Interval(-4, 4)))
{-4, -3, ..., 4}
Attributes
is_Complement | |
is_EmptySet | |
is_Intersection | |
is_UniversalSet |
Image of a set under a mathematical function
Examples
>>> from sympy import Symbol, S, ImageSet, FiniteSet, Lambda
>>> x = Symbol('x')
>>> N = S.Naturals
>>> squares = ImageSet(Lambda(x, x**2), N) # {x**2 for x in N}
>>> 4 in squares
True
>>> 5 in squares
False
>>> FiniteSet(0, 1, 2, 3, 4, 5, 6, 7, 9, 10).intersect(squares)
{1, 4, 9}
>>> square_iterable = iter(squares)
>>> for i in range(4):
... next(square_iterable)
1
4
9
16
Attributes
is_Complement | |
is_EmptySet | |
is_Intersection | |
is_UniversalSet |
Represents a range of integers.
Examples
>>> from sympy import Range
>>> list(Range(5)) # 0 to 5
[0, 1, 2, 3, 4]
>>> list(Range(10, 15)) # 10 to 15
[10, 11, 12, 13, 14]
>>> list(Range(10, 20, 2)) # 10 to 20 in steps of 2
[10, 12, 14, 16, 18]
>>> list(Range(20, 10, -2)) # 20 to 10 backward in steps of 2
[12, 14, 16, 18, 20]
Attributes
is_Complement | |
is_EmptySet | |
is_Intersection | |
is_UniversalSet |
Represents the Set of all Complex Numbers. It can represent a region of Complex Plane in both the standard forms Polar and Rectangular coordinates.
Z = {z in C | z = r*[cos(theta) + I*sin(theta)], r in [r], theta in [theta]}
Z = {z in C | z = x + I*y, x in [Re(z)], y in [Im(z)]}
See also
Reals
Examples
>>> from sympy.sets.fancysets import ComplexRegion
>>> from sympy.sets import Interval
>>> from sympy import S, I, Union
>>> a = Interval(2, 3)
>>> b = Interval(4, 6)
>>> c = Interval(1, 8)
>>> c1 = ComplexRegion(a*b) # Rectangular Form
>>> c1
ComplexRegion([2, 3] x [4, 6], False)
>>> c2 = ComplexRegion(Union(a*b, b*c))
>>> c2
ComplexRegion([2, 3] x [4, 6] U [4, 6] x [1, 8], False)
>>> 2.5 + 4.5*I in c1
True
>>> 2.5 + 6.5*I in c1
False
>>> r = Interval(0, 1)
>>> theta = Interval(0, 2*S.Pi)
>>> c2 = ComplexRegion(r*theta, polar=True) # Polar Form
>>> c2 # unit Disk
ComplexRegion([0, 1] x [0, 2*pi), True)
>>> 0.5 + 0.5*I in c2
True
>>> 1 + 2*I in c2
False
>>> unit_disk = ComplexRegion(Interval(0, 1)*Interval(0, 2*S.Pi), polar=True)
>>> upper_half_unit_disk = ComplexRegion(Interval(0, 1)*Interval(0, S.Pi), polar=True)
>>> intersection = unit_disk.intersect(upper_half_unit_disk)
>>> intersection
ComplexRegion([0, 1] x [0, pi], True)
>>> intersection == upper_half_unit_disk
True
Attributes
is_Complement | |
is_EmptySet | |
is_Intersection | |
is_UniversalSet |
Return the union of intervals of \(x\) when, self is in rectangular form, or the union of intervals of \(r\) when self is in polar form.
Examples
>>> from sympy import Interval, ComplexRegion, Union
>>> a = Interval(2, 3)
>>> b = Interval(4, 5)
>>> c = Interval(1, 7)
>>> C1 = ComplexRegion(a*b)
>>> C1.a_interval
[2, 3]
>>> C2 = ComplexRegion(Union(a*b, b*c))
>>> C2.a_interval
[2, 3] U [4, 5]
Return the union of intervals of \(y\) when, self is in rectangular form, or the union of intervals of \(theta\) when self is in polar form.
Examples
>>> from sympy import Interval, ComplexRegion, Union
>>> a = Interval(2, 3)
>>> b = Interval(4, 5)
>>> c = Interval(1, 7)
>>> C1 = ComplexRegion(a*b)
>>> C1.b_interval
[4, 5]
>>> C2 = ComplexRegion(Union(a*b, b*c))
>>> C2.b_interval
[1, 7]
Returns True if self is in polar form.
Examples
>>> from sympy import Interval, ComplexRegion, Union, S
>>> a = Interval(2, 3)
>>> b = Interval(4, 5)
>>> theta = Interval(0, 2*S.Pi)
>>> C1 = ComplexRegion(a*b)
>>> C1.polar
False
>>> C2 = ComplexRegion(a*theta, polar=True)
>>> C2.polar
True
Return a tuple of sets (ProductSets) input of the self.
Examples
>>> from sympy import Interval, ComplexRegion, Union
>>> a = Interval(2, 3)
>>> b = Interval(4, 5)
>>> c = Interval(1, 7)
>>> C1 = ComplexRegion(a*b)
>>> C1.psets
([2, 3] x [4, 5],)
>>> C2 = ComplexRegion(Union(a*b, b*c))
>>> C2.psets
([2, 3] x [4, 5], [4, 5] x [1, 7])
Return raw input sets to the self.
Examples
>>> from sympy import Interval, ComplexRegion, Union
>>> a = Interval(2, 3)
>>> b = Interval(4, 5)
>>> c = Interval(1, 7)
>>> C1 = ComplexRegion(a*b)
>>> C1.sets
[2, 3] x [4, 5]
>>> C2 = ComplexRegion(Union(a*b, b*c))
>>> C2.sets
[2, 3] x [4, 5] U [4, 5] x [1, 7]
Normalize a Real Set \(theta\) in the Interval [0, 2*pi). It returns a normalized value of theta in the Set. For Interval, a maximum of one cycle [0, 2*pi], is returned i.e. for theta equal to [0, 10*pi], returned normalized value would be [0, 2*pi). As of now intervals with end points as non-multiples of \(pi\) is not supported.
Raises : | NotImplementedError
ValueError
RuntimeError
|
---|
Examples
>>> from sympy.sets.fancysets import normalize_theta_set
>>> from sympy import Interval, FiniteSet, pi
>>> normalize_theta_set(Interval(9*pi/2, 5*pi))
[pi/2, pi]
>>> normalize_theta_set(Interval(-3*pi/2, pi/2))
[0, 2*pi)
>>> normalize_theta_set(Interval(-pi/2, pi/2))
[0, pi/2] U [3*pi/2, 2*pi)
>>> normalize_theta_set(Interval(-4*pi, 3*pi))
[0, 2*pi)
>>> normalize_theta_set(Interval(-3*pi/2, -pi/2))
[pi/2, 3*pi/2]
>>> normalize_theta_set(FiniteSet(0, pi, 3*pi))
{0, pi}