Working with Sets, Finite Sets, Intervals and Boundaries

Sets and different ways to express sets and manipulate them.

First with the builtin sets, and then using symbolic expressions.

In [65]:
from sympy import init_printing, latex
init_printing()
In [66]:
import sympy as sp
A,B,C = sp.symbols('A,B,C',positive=True)
x = sp.Symbol('x',positive=True)
y = sp.Symbol('y',positive=True)

Sets using the Python builtin sets.

In [4]:
s1 = set([A,B])
s2 = set([C])
s1.isdisjoint(s2)
Out[4]:
True

Basic Sets using Sympy, class sympy.sets.sets Set is not meant to be used as a container of items like the Python builtin sets. Several classes related to intervals are based on Sets. As can be seen below, the boundary of an interval is a FiniteSet(). It might be instructive to understand a little more about intervals, this is a topology subject. https://docs.sympy.org/latest/modules/sets.html

In [5]:
sp.Interval(0,1).boundary
Out[5]:
$\displaystyle \left\{0, 1\right\}$
In [17]:
# the full real line is Interval(-oo,oo)
from sympy import oo
SS = sp.Interval(-oo,oo)
SS
Out[17]:
$\displaystyle \left(-\infty, \infty\right)$
In [14]:
A = sp.Interval(1,oo, left_open=True)
A
Out[14]:
$\displaystyle \left(1, \infty\right)$
In [16]:
B = sp.Interval(-oo,2)
B
Out[16]:
$\displaystyle \left(-\infty, 2\right]$
In [18]:
Ac = A.complement(SS)
Ac
Out[18]:
$\displaystyle \left(-\infty, 1\right]$
In [19]:
Bc = B.complement(SS)
Bc
Out[19]:
$\displaystyle \left(2, \infty\right)$
In [10]:
A = sp.ConditionSet(x,x>1,sp.Interval(-oo,oo))

Example of the Product Set that can be used to create an Nd set.

In [28]:
PP = sp.ProductSet(sp.Interval(0,1),sp.Interval(0,1))
PP
Out[28]:
$\displaystyle \left[0, 1\right]^{2}$
In [29]:
(0.5,0.5) in PP
Out[29]:
True
In [30]:
SS = sp.ProductSet(sp.Interval(-oo, oo), sp.Interval(-oo, oo))
SS
Out[30]:
$\displaystyle \left(-\infty, \infty\right)^{2}$
In [58]:
(0,1) in SS
Out[58]:
True
In [106]:
x = sp.Symbol('x')
y = sp.Symbol('y')
In [108]:
(0<x) & (0<y)
Out[108]:
$\displaystyle x > 0 \wedge y > 0$
In [109]:
AA = sp.ConditionSet((x,y), (x>1) & (y<1), SS)
AA
Out[109]:
$\displaystyle \left\{\left( x, \ y\right) \mid \left( x, \ y\right) \in \left(-\infty, \infty\right)^{2} \wedge x > 1 \wedge y < 1 \right\}$
In [110]:
test_interval = sp.Interval(0,1).as_relational(x)
test_interval
Out[110]:
$\displaystyle 0 \leq x \wedge x \leq 1$
In [111]:
sp.ConditionSet(x,x>1,sp.Interval(-oo,oo))
Out[111]:
$\displaystyle \left\{x \mid x \in \left(-\infty, \infty\right) \wedge x > 1 \right\}$
In [112]:
BB = sp.ConditionSet((x,y), (0<x) & (0<y), SS)
BB
Out[112]:
$\displaystyle \left\{\left( x, \ y\right) \mid \left( x, \ y\right) \in \left(-\infty, \infty\right)^{2} \wedge x > 0 \wedge y > 0 \right\}$
In [113]:
sp.Lambda(BB.sym, BB.condition)(*(0,0))
Out[113]:
$\displaystyle \text{False}$
In [44]:
ppi = sp.ProductSet({0},{2})
ppi
Out[44]:
$\displaystyle \left\{0\right\} \times \left\{2\right\}$
In [46]:
AA.sym
Out[46]:
$\displaystyle \left( x, \ y\right)$
In [55]:
sp.Lambda(AA.sym, AA.condition)(*(1,2))
Out[55]:
$\displaystyle \text{True}$
In [47]:
BB = sp.ConditionSet(x,x>0)
BB
Out[47]:
$\displaystyle \mathbb{U}$
In [48]:
1 in BB
Out[48]:
True

FiniteSet represents a finite set of discrete numbers. This can take the numbers directly or unpack from a list.https://docs.sympy.org/latest/modules/sets.html#sympy.sets.sets.FiniteSet

In [6]:
sp.FiniteSet(1,2,3,4)
Out[6]:
$\displaystyle \left\{1, 2, 3, 4\right\}$
In [118]:
import math
math.factorial(6)
Out[118]:
$\displaystyle 720$
In [21]:
A = sp.Interval(0,1)
for i in range(2,10):
    A = A.union(sp.Interval(0,i))
A
Out[21]:
$\displaystyle \left[0, 9\right]$
In [23]:
A = sp.Interval(0,1)
for i in range(2,10):
    print("Is disjoint = ", A.is_disjoint(sp.Interval(0,i)))
    A = A.union(sp.Interval(0,i))
    
A
Is disjoint =  False
Is disjoint =  False
Is disjoint =  False
Is disjoint =  False
Is disjoint =  False
Is disjoint =  False
Is disjoint =  False
Is disjoint =  False
Out[23]:
$\displaystyle \left[0, 9\right]$
In [22]:
A = sp.Interval(0,1)
for i in range(2,10):
    A = A.intersection(sp.Interval(0,i))
A
Out[22]:
$\displaystyle \left[0, 1\right]$
In [114]:
members = [1,2,3,4]
f1 = sp.FiniteSet(*members)
f1
Out[114]:
$\displaystyle \left\{1, 2, 3, 4\right\}$
In [115]:
len(f1)
Out[115]:
$\displaystyle 4$
In [127]:
import itertools as it
members = range(1,7)
l1 = [xx+yy for xx,yy in it.product(members,members)]
l1
Out[127]:
$\displaystyle \left[ 2, \ 3, \ 4, \ 5, \ 6, \ 7, \ 3, \ 4, \ 5, \ 6, \ 7, \ 8, \ 4, \ 5, \ 6, \ 7, \ 8, \ 9, \ 5, \ 6, \ 7, \ 8, \ 9, \ 10, \ 6, \ 7, \ 8, \ 9, \ 10, \ 11, \ 7, \ 8, \ 9, \ 10, \ 11, \ 12\right]$
In [128]:
s1 = set(l1)
s1
Out[128]:
$\displaystyle \left\{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\right\}$
In [129]:
len(s1)
Out[129]:
$\displaystyle 11$
In [130]:
SS = sp.Interval(30,100)
SS
Out[130]:
$\displaystyle \left[30, 100\right]$
In [132]:
E1 = sp.ConditionSet(x,x<60,SS)
E1
Out[132]:
$\displaystyle \left\{x \mid x \in \left[30, 100\right] \wedge x < 60 \right\}$
In [133]:
E2 = sp.ConditionSet(x, x>=60, SS)
E2
Out[133]:
$\displaystyle \left\{x \mid x \in \left[30, 100\right] \wedge x \geq 60 \right\}$
In [137]:
E1.intersection(E2)
Out[137]:
$\displaystyle \left\{x \mid x \in \left[30, 100\right] \wedge x \geq 60 \right\} \cap \left\{x \mid x \in \left[30, 100\right] \wedge x < 60 \right\}$
In [140]:
members = range(1,6)
l1 = list(it.permutations(members,3))
l1
Out[140]:
$\displaystyle \left[ \left( 1, \ 2, \ 3\right), \ \left( 1, \ 2, \ 4\right), \ \left( 1, \ 2, \ 5\right), \ \left( 1, \ 3, \ 2\right), \ \left( 1, \ 3, \ 4\right), \ \left( 1, \ 3, \ 5\right), \ \left( 1, \ 4, \ 2\right), \ \left( 1, \ 4, \ 3\right), \ \left( 1, \ 4, \ 5\right), \ \left( 1, \ 5, \ 2\right), \ \left( 1, \ 5, \ 3\right), \ \left( 1, \ 5, \ 4\right), \ \left( 2, \ 1, \ 3\right), \ \left( 2, \ 1, \ 4\right), \ \left( 2, \ 1, \ 5\right), \ \left( 2, \ 3, \ 1\right), \ \left( 2, \ 3, \ 4\right), \ \left( 2, \ 3, \ 5\right), \ \left( 2, \ 4, \ 1\right), \ \left( 2, \ 4, \ 3\right), \ \left( 2, \ 4, \ 5\right), \ \left( 2, \ 5, \ 1\right), \ \left( 2, \ 5, \ 3\right), \ \left( 2, \ 5, \ 4\right), \ \left( 3, \ 1, \ 2\right), \ \left( 3, \ 1, \ 4\right), \ \left( 3, \ 1, \ 5\right), \ \left( 3, \ 2, \ 1\right), \ \left( 3, \ 2, \ 4\right), \ \left( 3, \ 2, \ 5\right), \ \left( 3, \ 4, \ 1\right), \ \left( 3, \ 4, \ 2\right), \ \left( 3, \ 4, \ 5\right), \ \left( 3, \ 5, \ 1\right), \ \left( 3, \ 5, \ 2\right), \ \left( 3, \ 5, \ 4\right), \ \left( 4, \ 1, \ 2\right), \ \left( 4, \ 1, \ 3\right), \ \left( 4, \ 1, \ 5\right), \ \left( 4, \ 2, \ 1\right), \ \left( 4, \ 2, \ 3\right), \ \left( 4, \ 2, \ 5\right), \ \left( 4, \ 3, \ 1\right), \ \left( 4, \ 3, \ 2\right), \ \left( 4, \ 3, \ 5\right), \ \left( 4, \ 5, \ 1\right), \ \left( 4, \ 5, \ 2\right), \ \left( 4, \ 5, \ 3\right), \ \left( 5, \ 1, \ 2\right), \ \left( 5, \ 1, \ 3\right), \ \left( 5, \ 1, \ 4\right), \ \left( 5, \ 2, \ 1\right), \ \left( 5, \ 2, \ 3\right), \ \left( 5, \ 2, \ 4\right), \ \left( 5, \ 3, \ 1\right), \ \left( 5, \ 3, \ 2\right), \ \left( 5, \ 3, \ 4\right), \ \left( 5, \ 4, \ 1\right), \ \left( 5, \ 4, \ 2\right), \ \left( 5, \ 4, \ 3\right)\right]$
In [141]:
len(l1)
Out[141]:
$\displaystyle 60$
In [142]:
import math
math.factorial(5)/math.factorial(2)
Out[142]:
$\displaystyle 60.0$
In [143]:
3*(3*3*5)
Out[143]:
$\displaystyle 135$
In [152]:
R = set([2*i+1 for i in range(0,18)])
R
Out[152]:
$\displaystyle \left\{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35\right\}$
In [154]:
B = set([2*i for i in range(1,19)])
B
Out[154]:
$\displaystyle \left\{2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36\right\}$
In [159]:
Bp = sp.ConditionSet(x, x>24, B)
Bp
Out[159]:
$\displaystyle \left\{26, 28, 30, 32, 34, 36\right\}$
In [161]:
len(Bp)/36
Out[161]:
$\displaystyle 0.16666666666666666$
In [164]:
C = sp.ConditionSet(x, x>24, R.union(B))
C
Out[164]:
$\displaystyle \left\{25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36\right\}$
In [165]:
len(C)
Out[165]:
$\displaystyle 12$
In [8]:
f1-sp.FiniteSet(2)
Out[8]:
$\displaystyle \left\{1, 3, 4\right\}$

Rewrite a FiniteSet in terms of equalities and logic operators. Not really sure what this means but maybe it will be useful.

In [9]:
s1f = sp.FiniteSet(1,2,3,4)
s1f.as_relational(A)
Out[9]:
$\displaystyle A = 1 \vee A = 2 \vee A = 3 \vee A = 4$

$\{x | condition(x) is True for x in S\}$ This gives the set of elements which satisfies a given condition.

In [10]:
x = sp.Symbol('x',positive=True)
sp.ConditionSet(x,x>=3,s1f)
Out[10]:
$\displaystyle \left\{3, 4\right\}$
In [11]:
sp.ConditionSet(x,x**2>4,sp.Interval(2,4))
Out[11]:
$\displaystyle \left\{x \mid x \in \left[2, 4\right] \wedge x^{2} > 4 \right\}$

Now for the intersection of some sets.

In [12]:
s1 = sp.FiniteSet(1,2,3,4)
s2 = sp.FiniteSet(4,5,6,7)
sp.Intersection(s1,s2)
Out[12]:
$\displaystyle \left\{4\right\}$

Logical propositions and statements

Logic in Sympy. https://docs.sympy.org/latest/modules/logic.html

A proposition is a statement proposing and idea that can be either true or false, and can be the result of logical connectives that combine propositions.

In [13]:
x,y = sp.symbols('x,y')
y | (x & y)
Out[13]:
$\displaystyle y \vee \left(x \wedge y\right)$
In [14]:
x,y = sp.symbols('x,y',positive=True)
y | (x & y)
Out[14]:
$\displaystyle \text{True}$
In [15]:
x,y = sp.symbols('x,y',real=True)
y | (x & y)
Out[15]:
$\displaystyle y \vee \left(x \wedge y\right)$

The implication is that x implies y, which means if x is true then y is true. So the statement x implies y is only true if both x and y are true. This does not specify a causal relationship between x and y, it simply means that whenever x is true, y must also be true. So the entire proposition is false if and only if x is true and y is false. A contingency is the status of propositions that are neither necessarily true nor necessarily false, that is they are not true for every possible valuation, nor false for every possible valuation. So the implication is a contingency, it depends on the variables.

In [16]:
x >> y
Out[16]:
$\displaystyle x \Rightarrow y$
In [17]:
(x & y).subs({x:True,y:True})
Out[17]:
$\displaystyle \text{True}$
In [18]:
(x >> y).subs({x:True,y:False})
Out[18]:
$\displaystyle \text{False}$
In [19]:
(x >> y).subs({x:True,y:True})
Out[19]:
$\displaystyle \text{True}$
In [20]:
sp.S.true
Out[20]:
$\displaystyle \text{True}$