def test_finite_basic(): x = Symbol('x') A = FiniteSet(1, 2, 3) B = FiniteSet(3, 4, 5) AorB = Union(A, B) AandB = A.intersect(B) assert A.is_subset(AorB) and B.is_subset(AorB) assert AandB.is_subset(A) assert AandB == FiniteSet(3) assert A.inf == 1 and A.sup == 3 assert AorB.inf == 1 and AorB.sup == 5 assert FiniteSet(x, 1, 5).sup == Max(x, 5) assert FiniteSet(x, 1, 5).inf == Min(x, 1) # issue 7335 assert FiniteSet(S.EmptySet) != S.EmptySet assert FiniteSet(FiniteSet(1, 2, 3)) != FiniteSet(1, 2, 3) assert FiniteSet((1, 2, 3)) != FiniteSet(1, 2, 3) # Ensure a variety of types can exist in a FiniteSet s = FiniteSet((1, 2), Float, A, -5, x, 'eggs', x**2, Interval) assert (A > B) is False assert (A >= B) is False assert (A < B) is False assert (A <= B) is False assert AorB > A and AorB > B assert AorB >= A and AorB >= B assert A >= A and A <= A assert A >= AandB and B >= AandB assert A > AandB and B > AandB
def test_finite_basic(): x = Symbol('x') A = FiniteSet(1, 2, 3) B = FiniteSet(3, 4, 5) AorB = Union(A, B) AandB = A.intersect(B) assert A.is_subset(AorB) and B.is_subset(AorB) assert AandB.is_subset(A) assert AandB == FiniteSet(3) assert A.inf == 1 and A.sup == 3 assert AorB.inf == 1 and AorB.sup == 5 assert FiniteSet(x, 1, 5).sup == Max(x, 5) assert FiniteSet(x, 1, 5).inf == Min(x, 1) # issue 7335 assert FiniteSet(S.EmptySet) != S.EmptySet assert FiniteSet(FiniteSet(1, 2, 3)) != FiniteSet(1, 2, 3) assert FiniteSet((1, 2, 3)) != FiniteSet(1, 2, 3) # Ensure a variety of types can exist in a FiniteSet s = FiniteSet((1, 2), Float, A, -5, x, 'eggs', x**2, Interval) assert (A > B) is False assert (A >= B) is False assert (A < B) is False assert (A <= B) is False assert AorB > A and AorB > B assert AorB >= A and AorB >= B assert A >= A and A <= A assert A >= AandB and B >= AandB assert A > AandB and B > AandB
def test_finite_basic(): x = Symbol('x') A = FiniteSet(1, 2, 3) B = FiniteSet(3, 4, 5) AorB = Union(A, B) AandB = A.intersect(B) assert AorB.subset(A) and AorB.subset(B) assert A.subset(AandB) assert AandB == FiniteSet(3) assert A.inf == 1 and A.sup == 3 assert AorB.inf == 1 and AorB.sup == 5 assert FiniteSet(x, 1, 5).sup == Max(x, 5) assert FiniteSet(x, 1, 5).inf == Min(x, 1) # Ensure a variety of types can exist in a FiniteSet S = FiniteSet((1, 2), Float, A, -5, x, 'eggs', x**2, Interval)
def test_finite_basic(): x = Symbol('x') A = FiniteSet(1,2,3) B = FiniteSet(3,4,5) AorB = Union(A,B) AandB = A.intersect(B) assert AorB.subset(A) and AorB.subset(B) assert A.subset(AandB) assert AandB == FiniteSet(3) assert A.inf == 1 and A.sup == 3 assert AorB.inf == 1 and AorB.sup ==5 assert FiniteSet(x, 1, 5).sup == Max(x,5) assert FiniteSet(x, 1, 5).inf == Min(x,1) # Ensure a variety of types can exist in a FiniteSet S = FiniteSet((1,2), Float, A, -5, x, 'eggs', x**2, Interval)
# A == B. El test de subconjunto propio da falso print('-' * 30) B = FiniteSet(2, 1, 3) print(A.is_proper_subset(B)) # Union de dos conjuntos print('-' * 30) A = FiniteSet(1, 2, 3) B = FiniteSet(2, 4, 6) print(A.union(B)) # Interseccion de dos conjuntos print('-' * 30) A = FiniteSet(1, 2) B = FiniteSet(2, 3) print(A.intersect(B)) # Diferencia entre conjuntos print('-' * 30) print(A - B) # Calculando el producto cartesiano. Con el conjunto por # defecto de python no podemos hacer esto con el operador * print('-' * 30) A = FiniteSet(1, 2) B = FiniteSet(3, 4) P = A * B print(P) for elem in P: print(elem)
# Union s = FiniteSet(1, 2, 3) t = FiniteSet(2, 4, 6) s.union(t) # {1, 2, 3, 4, 6,} # Union of three Sets s = FiniteSet(1, 2, 3) t = FiniteSet(2, 4, 6) u = FiniteSet(3, 5, 7) s.union(t).union(u) # {1, 2, 3, 4, 5, 6, 7} # Intersection s = FiniteSet(1, 2) t = FiniteSet(2, 3) s.intersect(t) # 2 # Intersection of three Sets s.intersect(t).interset(u) # EmpySet() # Cartesian Product s = FiniteSet(1, 2) t = FiniteSet(3, 4) p = s*t p # {1, 2} x {3, 4} for elem in p: print(elem) # (1, 3), (1,4), (2, 3), (2, 4) # Cardinality of the Cartesian Product len(p) == len(s)*len(t) # True
event = FiniteSet( *primes ) p = probability( space, event ) print( 'Sample Space: {0}'.format( space ) ) print( 'Event: {0}'.format( event ) ) print( 'Probability of rolling a prime: {0:.5f}'.format( p ) ) # P( A | B ) s = FiniteSet( 1, 2, 3, 4, 5, 6 ) a = FiniteSet( 2, 3, 5 ) b = FiniteSet( 1, 3, 5 ) e = a.union( b ) probability( s, e ) # P( A & B ) s = FiniteSet( 1, 2, 3, 4, 5, 6 ) a = FiniteSet( 2, 3, 5 ) b = FiniteSet( 1, 3, 5 ) e = a.intersect( b ) probability( s, e )
from sympy import FiniteSet six_sided = FiniteSet(1, 2, 3, 4, 5, 6) roll_one = FiniteSet(2, 3, 5) roll_two = FiniteSet(1, 3, 5) event = roll_one.intersect(roll_two) prob = len(event) / len(six_sided) print(prob)
s = FiniteSet(1, 15 iFraction(1, 5) for member in s: print(member) t = FiniteSet(Fraction(1, 5), 1.5, 1, 1) if s == t: print("True") s.intersect(t) s.unson(t) p = s ** 2 p1 = s ** 3 A = FiniteSet(1, 10, 20) B = FiniteSet(5, 8) if A == B: var = A.intersect(B).union A ** 2, list1 = [1, 4, 7, 84, 3, 62, 23] myd = dict() # myd1={} myd = {1: 'bir', 2: 2, '3': 'three'} print(myd) for key in myd.keys(): print(key, myd[key]) var = 1 in myd if -41 not in myd: myd[-40] = 50 print(myd)
from sympy import FiniteSet t = FiniteSet(1, 2, 3) s = FiniteSet(2, 4, 6) t == s #burası biraz saçma t.union(s) #t kümesi ile s kümesini birleştirir. (t U s) t.intersect(s) #t kümesi ile s kümesini ayirir t kesişim s demek t**2 #burası biraz saçma def probability(space, event): return len(event) / len(space) def check_prime(number): #asal sayilari bulur if number != 1: for factor in range(2, number): if number % factor == 0: return False else: return False return True space = FiniteSet(*range(1, 21)) primes = [] for num in space: if check_prime(num): primes.append(num)
True >>> t.is_proper_superset(s) True #Set Operations #Union and intersection >>> s=FiniteSet(1,2,3) >>> t=FiniteSet(2,4,6) >>> s.union(t) {1, 2, 3, 4, 6} >>> s=FiniteSet(1,2) >>> t=FiniteSet(2,3) >>> s.intersect(t) {2} #We can make an union and intersection of more than 2 sets >>> s=FiniteSet(1,2,3) >>> t=FiniteSet(2,4,6) >>> u=FiniteSet(3,5,7) >>> s.union(t).union(u) {1, 2, 3, 4, 5, 6, 7} >>> s.intersect(t).intersect(u) EmptySet() #Cartesian product
# ある集合の濃度をsとすれば、部分集合の個数は、2^sとなることが知られている。 print(FiniteSet(3, 1, 2).powerset()) # 集合の和集合や積集合を求める。 s = FiniteSet(1, 2, 3) t = FiniteSet(3, 4, 5) u = FiniteSet(3, 6) # 和集合を求める。 print(s.union(t)) # unionはメソッドチェーンして使用可能。 # 後述するintersectについても同様。 print(s.union(t).union(u)) # 積集合を求める。 print(s.intersect(t).intersect(u)) # 直積を求める。 # ※直積:それぞれの集合から要素を一つづつ選んでできる、全ての集合。 p = s * t * u # そのままの状態では、各集合を表示できない(ProductSetオブジェクトが返る)。 print(p) print(type(p)) # forで列挙すると、各集合を表示できる。 for elem in p: print(elem) print(len(p))
t = FiniteSet(1, 2, 3) s.is_proper_subset(t) t = FiniteSet(1, 2, 3, 4) t.is_proper_subset(s) s.is_proper_subset(t) s = FiniteSet(1, 2, 3) t = FiniteSet(2, 4, 6) s.union(t) s = FiniteSet(1, 2, 3) t = FiniteSet(2, 4, 6) s.intersect(t) s = FiniteSet(1, 2, 3) t = FiniteSet(2, 4, 6) u = FiniteSet(3, 5, 7) s.union(t).union(u) s.intersect(t).intersect(u) s = FiniteSet(1, 2) t = FiniteSet(3, 4) p = s * t p
event = primes(20) len(event)/len(range(1,21)) import matplotlib.pyplot as plt plt.plot(range(1,len(prime1)+1), prime1) from sympy import FiniteSet s = FiniteSet() for i in range(1,7): s1 = FiniteSet(i) s = s.union(s1) a = FiniteSet(2,3,5) b = FiniteSet(1,3,5) PrimeOrOdd = a.union(b) PrimeAndOdd = a.intersect(b) ProbPrimeOrOdd = len(PrimeOrOdd)/len(s) ProbPrimeAndOdd = len(PrimeAndOdd)/len(s) import random random.randint(1,6)
from sympy import FiniteSet s = FiniteSet(1, 1.5, Fraction(1, 5)) for member in s: print(member) t = FiniteSet(Fraction(1, 5), 1.5, 1, 1) if s == t: print("True") s.intersect(t) s.uninon(t) p = s**2 p1 = s**3
from sympy import FiniteSet from fractions import Fraction s = FiniteSet(1, 2, 3) t = FiniteSet(2, 4, 6) print("Union ", s.union(t)) s = FiniteSet(1, 2) t = FiniteSet(2, 3) print("intersect ", s.intersect(t)) s = FiniteSet(1, 2) t = FiniteSet(2, 3) u = FiniteSet(3, 5, 7) print("Union ", s.union(t).union(u)) s = FiniteSet(1, 2) t = FiniteSet(2, 3) p = s * t print("Cartesian ", p) for elem in p: print("Cartesian ", elem) s = FiniteSet(1, 2) p = s**3 print("carteisna ** ", p) for elem in p: print("Cartesian ", elem)
#ElifCelik Programlama Lab 3. Hafta Cuma #Zar için olasilik hesabi from sympy import FiniteSet t = FiniteSet(1, 2, 3) s = FiniteSet(2, 4, 6) t == s t.union(s) t.intersect(s) t**2 #probability calculation def probability(space, event): return len(event) / len(space) #is it prime number def check_prime(number): if number != 1: for factor in range(2, number): if number % factor == 0: return False else: return False return True space = FiniteSet(*range(1, 21))
from sympy import FiniteSet, pi # Unions & Intersections s = FiniteSet(1, 2, 3) t = FiniteSet(2, 4, 6) union = s.union(t) print(union) intersection = s.intersect(t) print(intersection) ### Cartesian Products cartesianProduct = s * t print(cartesianProduct) for elem in cartesianProduct: print(elem) # Raise set to the power (calculate triplets) cartesianProductCubed = s**3 for elem in cartesianProductCubed: print(elem) def time_period(length, g): T = 2 * pi * (length / g)**0.5 return T L = FiniteSet(15, 18, 21, 22.5, 25) g_values = FiniteSet(9.8, 9.78, 9.83)
from sympy import FiniteSet, pi # Unions & Intersections s = FiniteSet(1, 2, 3) t = FiniteSet(2, 4, 6) union = s.union(t) print(union) intersection = s.intersect(t) print(intersection) ### Cartesian Products cartesianProduct = s * t print(cartesianProduct) for elem in cartesianProduct: print(elem) # Raise set to the power (calculate triplets) cartesianProductCubed = s ** 3 for elem in cartesianProductCubed: print(elem) def time_period(length, g): T = 2*pi*(length/g)**0.5 return T L = FiniteSet(15, 18, 21, 22.5, 25)
from sympy import FiniteSet from fractions import Fraction t = FiniteSet(1, 2, 3) s = FiniteSet(2, 4, 6) if t == s: print("True") else: print("False") print(t.union(s)) print(t.intersect(s)) print(t**2) def probability(space, event): return len(event) / len(space) def check_prime(number): if number != 1: for factor in range(2, number): if number % factor == 0: return False else: return False return True space = FiniteSet(* range(1, 21))
tenthset = FiniteSet(20, 55, 41, 98) print(tenthset.powerset()) #print {EmptySet(), {20}, {41}, ..., {20, 55, 98}, {41, 55, 98}, {20, 41, 55, 98}} seventhset = FiniteSet(999, 439, 20984) eigthset = FiniteSet(999, 69, 48) ninthset = FiniteSet(999, 69) print(seventhset.is_proper_subset(eigthset)) #print False print(eigthset.is_proper_subset(seventhset)) #print False print(ninthset.is_proper_subset(eigthset)) #print True print(ninthset.is_proper_superset(eigthset)) #print False print(eigthset.is_proper_superset(ninthset)) #print True tenthset = FiniteSet(1, 2, 3) eleventhset = FiniteSet(2, 4, 6) print(tenthset.union(eleventhset)) #print {1, 2, 3, 4, 6} print(tenthset.intersect(eleventhset)) #print {2} #we can apply union and intersect to more than two sets. tenthset = FiniteSet(1, 2, 3) eleventhset = FiniteSet(2, 4, 6) twelthset = FiniteSet(3, 5, 7) print(tenthset.union(eleventhset).union(twelthset)) #print {1, 2, 3, 4, 5, 6, 7} print(tenthset.intersect(eleventhset).intersect(twelthset)) #print EmptySet() #The cartesian product creates a set that consists of all possible pairs made by taking an element from each set. print(tenthset*eleventhset) #print {1, 2, 3} x {2, 4, 6} tentheleventh = tenthset*eleventhset for eachtentheleventh in tentheleventh: print(eachtentheleventh) ''' (1, 2) (1, 4) (1, 6)
# Set Operations such as Union, Intresection and the cartesian product allow you to # combine sets in certain methodical ways. These set operations are extremely useful in real world # Problem solving situations when we have to consider multiple sets together. from sympy import FiniteSet s = FiniteSet(1, 2, 3) t = FiniteSet(2, 4, 6) #The result is the third set with all the distinct members of the two sets print(s.union(t)) # The intersection of two sets creates a new set from the elelments common to both sets # For Example, the intersection of the sets {1,2} and {2,3} will result in a new set with the only common element 2. s = FiniteSet(1, 2) t = FiniteSet(2, 3) print(s.intersect(t)) # Whereas the union operation finds memebers that are in one set or another, # the intersection operation finds elements that are present in both.Both of these operations # can also be applied to more than two sets. For Example- here's how you'd find the union of three sets s = FiniteSet(1, 2, 3) t = FiniteSet(2, 4, 6) u = FiniteSet(3, 5, 7) print(s.union(t).union(u)) # CARTESIAN PRODUCT # The cartesian product of two sets creates a set that consist all the possible pairs made by taking # an element from each set. For examples cartesian product of the sets {1,2} and {3,4} is {(1,3),(1,4),(2,3),(2,4)} # In SymPy you can find the cartesian product of two sets by simply using the multiplication operator p = s * t
# Subconjunto y subconjunto propio A = FiniteSet(1,2,3) B = FiniteSet(1,2,3,4,5) A.subset(B) # Union de dos conjuntos A = FiniteSet(1, 2, 3) B = FiniteSet(2, 4, 6) A.union(B) # Interseccion de dos conjuntos A = FiniteSet(1, 2) B = FiniteSet(2, 3) A.intersect(B) # Diferencia entre conjuntos print A - B # Calculando el producto cartesiano. A = FiniteSet(1, 2) B = FiniteSet(3, 4) P = A * B for elem in P: print(elem) # Calcula el n producto cartesiano del mismo conjunto.