Sets

Set

class sympy.core.sets.Set

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_EmptySet  
is_Intersection  
is_UniversalSet  
complement

The complement of ‘self’.

As a shortcut it is possible to use the ‘~’ or ‘-‘ operators:

>>> from sympy import Interval
>>> Interval(0, 1).complement
(-oo, 0) U (1, oo)
>>> ~Interval(0, 1)
(-oo, 0) U (1, oo)
>>> -Interval(0, 1)
(-oo, 0) U (1, oo)
contains(other)

Returns True if ‘other’ is contained in ‘self’ as an element.

As a shortcut it is possible to use the ‘in’ operator:

>>> from sympy import Interval
>>> Interval(0, 1).contains(0.5)
True
>>> 0.5 in Interval(0, 1)
True
inf

The infimum of ‘self’

>>> from sympy import Interval, Union
>>> Interval(0, 1).inf
0
>>> Union(Interval(0, 1), Interval(2, 3)).inf
0
intersect(other)

Returns the intersection of ‘self’ and ‘other’.

>>> from sympy import Interval
>>> Interval(1, 3).intersect(Interval(1, 2))
[1, 2]
measure

The (Lebesgue) measure of ‘self’

>>> from sympy import Interval, Union
>>> Interval(0, 1).measure
1
>>> Union(Interval(0, 1), Interval(2, 3)).measure
2
sort_key(order=None)

Give sort_key of infimum (if possible) else sort_key of the set.

subset(other)

Returns True if ‘other’ is a subset of ‘self’.

>>> from sympy import Interval
>>> Interval(0, 1).subset(Interval(0, 0.5))
True
>>> Interval(0, 1, left_open=True).subset(Interval(0, 1))
False
sup

The supremum of ‘self’

>>> from sympy import Interval, Union
>>> Interval(0, 1).sup
1
>>> Union(Interval(0, 1), Interval(2, 3)).sup
3
union(other)

Returns the union of ‘self’ and ‘other’.

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]

Elementary Sets

Interval

class sympy.core.sets.Interval

Represents a real interval as a Set.

Usage:

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

  • Only real end points are supported
  • Interval(a, b) with a > b will return the empty set
  • Use the evalf() method to turn an Interval into an mpmath ‘mpi’ interval instance

References

<http://en.wikipedia.org/wiki/Interval_(mathematics)>

Examples

>>> from sympy import Symbol, Interval
>>> Interval(0, 1)
[0, 1]
>>> Interval(0, 1, False, True)
[0, 1)
>>> a = Symbol('a', real=True)
>>> Interval(0, a)
[0, a]

Attributes

is_EmptySet  
is_Intersection  
is_UniversalSet  
as_relational(symbol)

Rewrite an interval in terms of inequalities and logic operators.

end

The right end point of ‘self’.

This property takes the same value as the ‘sup’ property.

>>> from sympy import Interval
>>> Interval(0, 1).end
1
is_left_unbounded

Return True if the left endpoint is negative infinity.

is_right_unbounded

Return True if the right endpoint is positive infinity.

left

The left end point of ‘self’.

This property takes the same value as the ‘inf’ property.

>>> from sympy import Interval
>>> Interval(0, 1).start
0
left_open

True if ‘self’ is left-open.

>>> from sympy import Interval
>>> Interval(0, 1, left_open=True).left_open
True
>>> Interval(0, 1, left_open=False).left_open
False
right

The right end point of ‘self’.

This property takes the same value as the ‘sup’ property.

>>> from sympy import Interval
>>> Interval(0, 1).end
1
right_open

True if ‘self’ is right-open.

>>> from sympy import Interval
>>> Interval(0, 1, right_open=True).right_open
True
>>> Interval(0, 1, right_open=False).right_open
False
start

The left end point of ‘self’.

This property takes the same value as the ‘inf’ property.

>>> from sympy import Interval
>>> Interval(0, 1).start
0

FiniteSet

class sympy.core.sets.FiniteSet

Represents a finite set of discrete numbers

References

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

Attributes

is_EmptySet  
is_Intersection  
is_UniversalSet  
as_relational(symbol)

Rewrite a FiniteSet in terms of equalities and logic operators.

Compound Sets

Union

class sympy.core.sets.Union

Represents a union of sets as a Set.

See also

Intersection

References

<http://en.wikipedia.org/wiki/Union_(set_theory)>

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_EmptySet  
is_Intersection  
is_UniversalSet  
as_relational(symbol)

Rewrite a Union in terms of equalities and logic operators.

static reduce(args)

Simplify a Union using known rules

We first start with global rules like ‘Merge all FiniteSets’

Then we iterate through all pairs and ask the constituent sets if they can simplify themselves with any other constituent

Intersection

class sympy.core.sets.Intersection

Represents an intersection of sets as a Set.

See also

Union

References

<http://en.wikipedia.org/wiki/Intersection_(set_theory)>

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_EmptySet  
is_UniversalSet  
as_relational(symbol)

Rewrite an Intersection in terms of equalities and logic operators

static reduce(args)

Simplify an intersection using known rules

We first start with global rules like ‘if any empty sets return empty set’ and ‘distribute any unions’

Then we iterate through all pairs and ask the constituent sets if they can simplify themselves with any other constituent

ProductSet

class sympy.core.sets.ProductSet

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

  • Passes most operations down to the argument sets
  • Flattens Products of ProductSets

References

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_EmptySet  
is_Intersection  
is_UniversalSet  

Singleton Sets

EmptySet

class sympy.core.sets.EmptySet

Represents the empty set. The empty set is available as a singleton as S.EmptySet.

See also

UniversalSet

References

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_Intersection  
is_UniversalSet  

UniversalSet

class sympy.core.sets.UniversalSet

Represents the set of all things. The universal set is available as a singleton as S.UniversalSet

See also

EmptySet

References

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_EmptySet  
is_Intersection  

Special Sets

Naturals

class sympy.sets.fancysets.Naturals

Represents the Natural Numbers. The Naturals are available as a singleton as S.Naturals

Examples

>>> from sympy import S, Interval, pprint
>>> 5 in S.Naturals
True
>>> iterable = iter(S.Naturals)
>>> print(next(iterable))
1
>>> print(next(iterable))
2
>>> print(next(iterable))
3
>>> pprint(S.Naturals.intersect(Interval(0, 10)))
{1, 2, ..., 10}

Attributes

is_EmptySet  
is_Intersection  
is_UniversalSet  

Integers

class sympy.sets.fancysets.Integers

Represents the Integers. The Integers are available as a singleton as S.Integers

Examples

>>> from sympy import S, Interval, pprint
>>> 5 in S.Naturals
True
>>> iterable = iter(S.Integers)
>>> print(next(iterable))
0
>>> print(next(iterable))
1
>>> print(next(iterable))
-1
>>> print(next(iterable))
2
>>> pprint(S.Integers.intersect(Interval(-4, 4)))
{-4, -3, ..., 4}

Attributes

is_EmptySet  
is_Intersection  
is_UniversalSet  

ImageSet

class sympy.sets.fancysets.ImageSet

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_EmptySet  
is_Intersection  
is_UniversalSet