Sets¶
Set¶

class
diofant.sets.sets.
Set
[source]¶ 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
; seeFiniteSet
for that.Real intervals are represented by the
Interval
class and unions of sets by theUnion
class. The empty set is represented by theEmptySet
class and available as a singleton asS.EmptySet
.
boundary
¶ The boundary or frontier of a set
A point x is on the boundary of a set S if
 x is in the closure of S. I.e. Every neighborhood of x contains a point in S.
 x is not in the interior of S. I.e. There does not exist an open set centered on x contained entirely within S.
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
>>> Interval(0, 1).boundary {0, 1} >>> Interval(0, 1, True, False).boundary {0, 1}

complement
(universe)[source]¶ The complement of ‘self’ w.r.t the given the universe.
Examples
>>> Interval(0, 1).complement(S.Reals) (oo, 0) U (1, oo)
>>> Interval(0, 1).complement(S.UniversalSet) UniversalSet() \ [0, 1]

contains
(other)[source]¶ Returns True if ‘other’ is contained in ‘self’ as an element.
As a shortcut it is possible to use the ‘in’ operator:
Examples
>>> Interval(0, 1).contains(0.5) true >>> 0.5 in Interval(0, 1) True

inf
¶ The infimum of ‘self’
Examples
>>> Interval(0, 1).inf 0 >>> Union(Interval(0, 1), Interval(2, 3)).inf 0

intersection
(other)[source]¶ Returns the intersection of ‘self’ and ‘other’.
>>> Interval(1, 3).intersection(Interval(1, 2)) [1, 2]

is_disjoint
(other)[source]¶ Returns True if ‘self’ and ‘other’ are disjoint
References
[R485] https//en.wikipedia.org/wiki/Disjoint_sets Examples
>>> Interval(0, 2).is_disjoint(Interval(1, 2)) False >>> Interval(0, 2).is_disjoint(Interval(3, 4)) True

is_open
¶ Test if a set is open.
A set is open if it has an empty intersection with its boundary.
See also
Examples
>>> S.Reals.is_open True

is_proper_subset
(other)[source]¶ Returns True if ‘self’ is a proper subset of ‘other’.
Examples
>>> Interval(0, 0.5).is_proper_subset(Interval(0, 1)) True >>> Interval(0, 1).is_proper_subset(Interval(0, 1)) False

is_proper_superset
(other)[source]¶ Returns True if ‘self’ is a proper superset of ‘other’.
Examples
>>> Interval(0, 1).is_proper_superset(Interval(0, 0.5)) True >>> Interval(0, 1).is_proper_superset(Interval(0, 1)) False

is_subset
(other)[source]¶ Returns True if ‘self’ is a subset of ‘other’.
Examples
>>> Interval(0, 0.5).is_subset(Interval(0, 1)) True >>> Interval(0, 1).is_subset(Interval(0, 1, left_open=True)) False

is_superset
(other)[source]¶ Returns True if ‘self’ is a superset of ‘other’.
Examples
>>> Interval(0, 0.5).is_superset(Interval(0, 1)) False >>> Interval(0, 1).is_superset(Interval(0, 1, left_open=True)) True

isdisjoint
(other)[source]¶ Alias for
is_disjoint()

issubset
(other)[source]¶ Alias for
is_subset()

issuperset
(other)[source]¶ Alias for
is_superset()

measure
¶ The (Lebesgue) measure of ‘self’
Examples
>>> Interval(0, 1).measure 1 >>> Union(Interval(0, 1), Interval(2, 3)).measure 2

powerset
()[source]¶ Find the Power set of ‘self’.
References
[R486] https//en.wikipedia.org/wiki/Power_set Examples
>>> 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

sup
¶ The supremum of ‘self’
Examples
>>> Interval(0, 1).sup 1 >>> Union(Interval(0, 1), Interval(2, 3)).sup 3

union
(other)[source]¶ Returns the union of ‘self’ and ‘other’.
Examples
As a shortcut it is possible to use the ‘+’ operator:
>>> 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]


diofant.sets.sets.
imageset
(*args)[source]¶ Image of set under transformation
f
.If this function can’t compute the image, it returns an unevaluated ImageSet object.
\[{ f(x)  x \in self }\]See also
Examples
>>> 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])
Elementary Sets¶
Interval¶

class
diofant.sets.sets.
Interval
[source]¶ 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
 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
[R487] https//en.wikipedia.org/wiki/Interval_%28mathematics%29 Examples
>>> 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', extended_real=True) >>> Interval(0, a) [0, a]

end
¶ The right end point of ‘self’.
This property takes the same value as the ‘sup’ property.
Examples
>>> 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.
Examples
>>> Interval(0, 1).start 0

left_open
¶ True if ‘self’ is leftopen.
Examples
>>> 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.
Examples
>>> Interval(0, 1).end 1

right_open
¶ True if ‘self’ is rightopen.
Examples
>>> 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.
Examples
>>> Interval(0, 1).start 0
FiniteSet¶

class
diofant.sets.sets.
FiniteSet
[source]¶ Represents a finite set of discrete numbers
References
[R488] https//en.wikipedia.org/wiki/Finite_set Examples
>>> FiniteSet(1, 2, 3, 4) {1, 2, 3, 4} >>> 3 in FiniteSet(1, 2, 3, 4) True

measure
¶ The (Lebesgue) measure of ‘self’
Examples
>>> Interval(0, 1).measure 1 >>> Union(Interval(0, 1), Interval(2, 3)).measure 2

Compound Sets¶
Union¶

class
diofant.sets.sets.
Union
[source]¶ Represents a union of sets as a
Set
.See also
References
[R489] https//en.wikipedia.org/wiki/Union_%28set_theory%29 Examples
>>> 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]

is_iterable
¶ bool(x) > bool
Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.

Intersection¶

class
diofant.sets.sets.
Intersection
[source]¶ Represents an intersection of sets as a
Set
.See also
References
[R490] https//en.wikipedia.org/wiki/Intersection_%28set_theory%29 Examples
>>> Intersection(Interval(1, 3), Interval(2, 4)) [2, 3]
We often use the .intersect method
>>> Interval(1, 3).intersection(Interval(2, 4)) [2, 3]

is_iterable
¶ bool(x) > bool
Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.

ProductSet¶

class
diofant.sets.sets.
ProductSet
[source]¶ 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
[R491] https//en.wikipedia.org/wiki/Cartesian_product Examples
>>> 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]
>>> H, T = Symbol('H'), Symbol('T') >>> coin = FiniteSet(H, T) >>> set(coin**2) {(H, H), (H, T), (T, H), (T, T)}

is_iterable
¶ bool(x) > bool
Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.
Complement¶

class
diofant.sets.sets.
Complement
[source]¶ Represents relative complement of a set with another set.
\(A  B = \{x \in A x \notin B\}\)
See also
References
[R492] http://mathworld.wolfram.com/ComplementSet.html Examples
>>> Complement(FiniteSet(0, 1, 2), FiniteSet(1)) {0, 2}

static
reduce
(B)[source]¶ Simplify a
Complement
.

static
Singleton Sets¶
EmptySet¶
UniversalSet¶

class
diofant.sets.sets.
UniversalSet
[source]¶ Represents the set of all things.
The universal set is available as a singleton as S.UniversalSet
See also
References
[R494] https//en.wikipedia.org/wiki/Universal_set Examples
>>> S.UniversalSet UniversalSet()
>>> Interval(1, 2).intersection(S.UniversalSet) [1, 2]
Special Sets¶
Naturals¶

class
diofant.sets.fancysets.
Naturals
[source]¶ The set of natural numbers.
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.
Examples
>>> 5 in S.Naturals True >>> iterable = iter(S.Naturals) >>> next(iterable) 1 >>> next(iterable) 2 >>> next(iterable) 3 >>> pprint(S.Naturals.intersection(Interval(0, 10))) {1, 2, ..., 10}
Naturals0¶
Integers¶

class
diofant.sets.fancysets.
Integers
[source]¶ The set of all integers.
Represents all integers: positive, negative and zero. This set is also available as the Singleton, S.Integers.
Examples
>>> 5 in S.Naturals True >>> iterable = iter(S.Integers) >>> next(iterable) 0 >>> next(iterable) 1 >>> next(iterable) 1 >>> next(iterable) 2
>>> pprint(S.Integers.intersection(Interval(4, 4))) {4, 3, ..., 4}
ImageSet¶

class
diofant.sets.fancysets.
ImageSet
[source]¶ Image of a set under a mathematical function.
Examples
>>> 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).intersection(squares) {1, 4, 9}
>>> square_iterable = iter(squares) >>> for i in range(4): ... next(square_iterable) 1 4 9 16
If you want to get value for \(x\) = 2, 1/2 etc. (Please check whether the \(x\) value is in \(base_set\) or not before passing it as args)
>>> squares.lamda(2) 4 >>> squares.lamda(S.One/2) 1/4

is_iterable
¶ bool(x) > bool
Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.

Range¶

class
diofant.sets.fancysets.
Range
[source]¶ Represents a range of integers.
Examples
>>> 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]