Sets and different ways to express sets and manipulate them.
First with the builtin sets, and then using symbolic expressions.
from sympy import init_printing, latex
init_printing()
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.
s1 = set([A,B])
s2 = set([C])
s1.isdisjoint(s2)
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
sp.Interval(0,1).boundary
# the full real line is Interval(-oo,oo)
from sympy import oo
SS = sp.Interval(-oo,oo)
SS
A = sp.Interval(1,oo, left_open=True)
A
B = sp.Interval(-oo,2)
B
Ac = A.complement(SS)
Ac
Bc = B.complement(SS)
Bc
A = sp.ConditionSet(x,x>1,sp.Interval(-oo,oo))
Example of the Product Set that can be used to create an Nd set.
PP = sp.ProductSet(sp.Interval(0,1),sp.Interval(0,1))
PP
(0.5,0.5) in PP
SS = sp.ProductSet(sp.Interval(-oo, oo), sp.Interval(-oo, oo))
SS
(0,1) in SS
x = sp.Symbol('x')
y = sp.Symbol('y')
(0<x) & (0<y)
AA = sp.ConditionSet((x,y), (x>1) & (y<1), SS)
AA
test_interval = sp.Interval(0,1).as_relational(x)
test_interval
sp.ConditionSet(x,x>1,sp.Interval(-oo,oo))
BB = sp.ConditionSet((x,y), (0<x) & (0<y), SS)
BB
sp.Lambda(BB.sym, BB.condition)(*(0,0))
ppi = sp.ProductSet({0},{2})
ppi
AA.sym
sp.Lambda(AA.sym, AA.condition)(*(1,2))
BB = sp.ConditionSet(x,x>0)
BB
1 in BB
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
sp.FiniteSet(1,2,3,4)
import math
math.factorial(6)
A = sp.Interval(0,1)
for i in range(2,10):
A = A.union(sp.Interval(0,i))
A
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
A = sp.Interval(0,1)
for i in range(2,10):
A = A.intersection(sp.Interval(0,i))
A
members = [1,2,3,4]
f1 = sp.FiniteSet(*members)
f1
len(f1)
import itertools as it
members = range(1,7)
l1 = [xx+yy for xx,yy in it.product(members,members)]
l1
s1 = set(l1)
s1
len(s1)
SS = sp.Interval(30,100)
SS
E1 = sp.ConditionSet(x,x<60,SS)
E1
E2 = sp.ConditionSet(x, x>=60, SS)
E2
E1.intersection(E2)
members = range(1,6)
l1 = list(it.permutations(members,3))
l1
len(l1)
import math
math.factorial(5)/math.factorial(2)
3*(3*3*5)
R = set([2*i+1 for i in range(0,18)])
R
B = set([2*i for i in range(1,19)])
B
Bp = sp.ConditionSet(x, x>24, B)
Bp
len(Bp)/36
C = sp.ConditionSet(x, x>24, R.union(B))
C
len(C)
f1-sp.FiniteSet(2)
Rewrite a FiniteSet in terms of equalities and logic operators. Not really sure what this means but maybe it will be useful.
s1f = sp.FiniteSet(1,2,3,4)
s1f.as_relational(A)
$\{x | condition(x) is True for x in S\}$ This gives the set of elements which satisfies a given condition.
x = sp.Symbol('x',positive=True)
sp.ConditionSet(x,x>=3,s1f)
sp.ConditionSet(x,x**2>4,sp.Interval(2,4))
Now for the intersection of some sets.
s1 = sp.FiniteSet(1,2,3,4)
s2 = sp.FiniteSet(4,5,6,7)
sp.Intersection(s1,s2)
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.
x,y = sp.symbols('x,y')
y | (x & y)
x,y = sp.symbols('x,y',positive=True)
y | (x & y)
x,y = sp.symbols('x,y',real=True)
y | (x & y)
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.
x >> y
(x & y).subs({x:True,y:True})
(x >> y).subs({x:True,y:False})
(x >> y).subs({x:True,y:True})
sp.S.true