Skupovi u SAGE-u

verzija: SageMath 9.4

Skupovi brojeva kao algebarske strukture

In [1]:
NN
Out[1]:
Non negative integer semiring
In [2]:
[0 in NN, 3 in NN, -4 in NN, 2/3 in NN]
Out[2]:
[True, True, False, False]
In [3]:
ZZ
Out[3]:
Integer Ring
In [4]:
[0 in ZZ, 3 in ZZ, -4 in ZZ, 2/3 in ZZ]
Out[4]:
[True, True, True, False]
In [5]:
QQ
Out[5]:
Rational Field
In [6]:
[2 in QQ, -3 in QQ, pi in QQ, 2/3 in QQ,-2.3 in QQ,2^(1/3) in QQ]
Out[6]:
[True, True, False, True, True, False]
In [7]:
RR
Out[7]:
Real Field with 53 bits of precision
In [8]:
[2 in RR, -3 in RR, pi in RR, 2/3 in RR,-2.3 in RR, 2^(1/3) in RR, 2+i in RR]
Out[8]:
[True, True, True, True, True, True, False]
In [9]:
CC
Out[9]:
Complex Field with 53 bits of precision
In [10]:
[-3 in CC, pi in CC, 2/3 in CC,-2.3 in CC, 2^(1/3) in CC, 2+i in CC]
Out[10]:
[True, True, True, True, True, True]

Na njima nisu definirane skupovske operacije

In [11]:
NN.intersection(ZZ) 
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/lib/python3.9/site-packages/sage/structure/category_object.pyx in sage.structure.category_object.CategoryObject.getattr_from_category (build/cythonized/sage/structure/category_object.c:7089)()
    838         try:
--> 839             return self.__cached_methods[name]
    840         except KeyError:

KeyError: 'intersection'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_20608/624017604.py in <module>
----> 1 NN.intersection(ZZ)

/usr/lib/python3.9/site-packages/sage/misc/lazy_import.pyx in sage.misc.lazy_import.LazyImport.__getattr__ (build/cythonized/sage/misc/lazy_import.c:3897)()
    328             True
    329         """
--> 330         return getattr(self.get_object(), attr)
    331 
    332     # We need to wrap all the slot methods, as they are not forwarded

/usr/lib/python3.9/site-packages/sage/structure/category_object.pyx in sage.structure.category_object.CategoryObject.__getattr__ (build/cythonized/sage/structure/category_object.c:7008)()
    831             AttributeError: 'PrimeNumbers_with_category' object has no attribute 'sadfasdf'
    832         """
--> 833         return self.getattr_from_category(name)
    834 
    835     cdef getattr_from_category(self, name):

/usr/lib/python3.9/site-packages/sage/structure/category_object.pyx in sage.structure.category_object.CategoryObject.getattr_from_category (build/cythonized/sage/structure/category_object.c:7174)()
    846                 cls = self._category.parent_class
    847 
--> 848             attr = getattr_from_other_class(self, cls, name)
    849             self.__cached_methods[name] = attr
    850             return attr

/usr/lib/python3.9/site-packages/sage/cpython/getattr.pyx in sage.cpython.getattr.getattr_from_other_class (build/cythonized/sage/cpython/getattr.c:2566)()
    365         dummy_error_message.cls = type(self)
    366         dummy_error_message.name = name
--> 367         raise AttributeError(dummy_error_message)
    368     cdef PyObject* attr = instance_getattr(cls, name)
    369     if attr is NULL:

AttributeError: 'NonNegativeIntegerSemiring_with_category' object has no attribute 'intersection'

Skupovi brojeva kao SAGE skupovi

In [12]:
Set(PositiveIntegers())
Out[12]:
Set of elements of Positive integers
In [13]:
Set(ZZ)
Out[13]:
Set of elements of Integer Ring
In [14]:
Set(QQ)
Out[14]:
Set of elements of Rational Field
In [15]:
Set(RR)
Out[15]:
Set of elements of Real Field with 53 bits of precision
In [16]:
Set(CC)
Out[16]:
Set of elements of Complex Field with 53 bits of precision
In [17]:
[0 in Set(PositiveIntegers()), 2/3 in Set(ZZ), pi in Set(CC)]
Out[17]:
[False, False, True]

na njima su definirane skupovske operacije

In [18]:
Set(ZZ).intersection(Set(QQ))
Out[18]:
Set-theoretic intersection of Set of elements of Integer Ring and Set of elements of Rational Field
In [19]:
Set(RR).union(Set(QQ))
Out[19]:
Set-theoretic union of Set of elements of Real Field with 53 bits of precision and Set of elements of Rational Field

definicija skupa negativnih cijelih brojeva

In [20]:
skup=Set(ZZ).difference(Set(IntegerRange(0,Infinity)))
skup
Out[20]:
Set-theoretic difference of Set of elements of Integer Ring and Set of elements of {0, 1, ...}
In [21]:
[-2 in skup, 0 in skup, 5 in skup] 
Out[21]:
[True, False, False]

kardinalni brojevi

In [22]:
Set(QQ).cardinality()
Out[22]:
+Infinity
In [23]:
Set(RR).cardinality()
Out[23]:
+Infinity

Prazan skup

kao SAGE skup

In [24]:
Set([])
Out[24]:
{}
In [25]:
Set([]).cardinality()
Out[25]:
0

kao python skup

In [26]:
set([])
Out[26]:
set()
In [27]:
set([]).cardinality()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_20608/1614630090.py in <module>
----> 1 set([]).cardinality()

AttributeError: 'set' object has no attribute 'cardinality'
In [28]:
len(set([]))
Out[28]:
0

Bitna razlika između Set i set: set ne zna raditi s beskonačnim skupovima, dok Set ima tu mogućnost.

Zadavanje skupa

nabrajanjem svih elemenata

kao SAGE skup

In [29]:
A=Set([2,4,6])
In [30]:
A
Out[30]:
{2, 4, 6}
In [31]:
A.cardinality()
Out[31]:
3
In [32]:
len(A)
Out[32]:
3
In [33]:
1 in A
Out[33]:
False
In [34]:
6 in A
Out[34]:
True

kao python skup

In [35]:
A1=set([2,4,6])
In [36]:
A1
Out[36]:
{2, 4, 6}
In [37]:
A1.cardinality()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_20608/2399393749.py in <module>
----> 1 A1.cardinality()

AttributeError: 'set' object has no attribute 'cardinality'
In [38]:
len(A1)
Out[38]:
3
In [39]:
1 in A1
Out[39]:
False
In [40]:
6 in A1
Out[40]:
True

definiranjem karakterističnog svojstva

kao SAGE skup

In [41]:
B=Set([x for x in range(1,7) if x%2==0])
In [42]:
B
Out[42]:
{2, 4, 6}
In [43]:
B.cardinality()
Out[43]:
3
In [44]:
len(B)
Out[44]:
3
In [45]:
1 in B
Out[45]:
False
In [46]:
6 in B
Out[46]:
True

kao python skup

In [47]:
B1=set([x for x in range(1,7) if x%2==0])
In [48]:
B1
Out[48]:
{2, 4, 6}
In [49]:
B1.cardinality()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_20608/1106999396.py in <module>
----> 1 B1.cardinality()

AttributeError: 'set' object has no attribute 'cardinality'
In [50]:
len(B1)
Out[50]:
3
In [51]:
1 in B1
Out[51]:
False
In [52]:
6 in B1
Out[52]:
True

skupovi $A$ i $B$ su jednaki

In [53]:
A==B
Out[53]:
True
In [54]:
A1==B1
Out[54]:
True
In [55]:
A==A1
Out[55]:
True
In [56]:
A.set()==A1
Out[56]:
True

Jednakost skupova

In [57]:
Set([1,2,3,2,2,6])==Set([1,2,6,3])
Out[57]:
True
In [58]:
Set([1,2,3,2,2,6])==set([1,2,6,3])
Out[58]:
True
In [59]:
set([1,2,3,2,2,6])==set([1,2,6,3])
Out[59]:
True
In [60]:
Set([1,2,3,2,2,6]).set()==set([1,2,6,3])
Out[60]:
True

"biti podskup" $\subseteq$

In [61]:
A=Set([3,2,'a',5])
B=Set([2,'a'])
In [62]:
B.issubset(A)
Out[62]:
True
In [63]:
B.set().issubset(A.set())
Out[63]:
True
In [64]:
A1=set([3,2,'a',5])
B1=set([2,'a'])
In [65]:
B1.issubset(A1)
Out[65]:
True
In [66]:
A1.issubset(B1)
Out[66]:
False

Zadatak

Koje su od sljedećih tvrdnji istinite?

  1. $\{\emptyset\}\in\big\{\{\emptyset\}\big\}$
  2. $\emptyset\subseteq\big\{\{\emptyset\}\big\}$
  3. $\{\emptyset\}\subseteq\big\{\{\emptyset\}\big\}$

Rješenje

$\{\emptyset\}\in\big\{\{\emptyset\}\big\}$ je istinita tvrdnja

In [67]:
Set([Set([])]) in Set([Set([Set([])])])
Out[67]:
True
In [68]:
set([set([])]) in set([set([set([])])])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_20608/3673923403.py in <module>
----> 1 set([set([])]) in set([set([set([])])])

TypeError: unhashable type: 'set'

$\emptyset\subseteq\big\{\{\emptyset\}\big\}$ je istinita tvrdnja

In [69]:
set([]).issubset(set([set([set([])])]))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_20608/1268435732.py in <module>
----> 1 set([]).issubset(set([set([set([])])]))

TypeError: unhashable type: 'set'
In [70]:
set([]).issubset(Set([Set([Set([])])]).set())
Out[70]:
True

$\{\emptyset\}\subseteq\big\{\{\emptyset\}\big\}$ je lažna tvrdnja

In [71]:
Set([Set([])]).set().issubset(Set([Set([Set([])])]).set())
Out[71]:
False

Partitivni skup

podskupovi skupa $\{a,b,c\}$

In [72]:
var('a b c')
A=Set([a,b,c])
PA=A.subsets()
In [73]:
A
Out[73]:
{a, c, b}
In [74]:
PA
Out[74]:
Subsets of {a, c, b}
In [75]:
Set(PA)
Out[75]:
{{a, c, b}, {c}, {a, c}, {a}, {}, {a, b}, {c, b}, {b}}
In [76]:
PA.list()
Out[76]:
[{}, {a}, {c}, {b}, {a, c}, {a, b}, {c, b}, {a, c, b}]

Kartezijev produkt skupova

Kartezijev produkt skupova $\{1,2,3\}$ i $\{a,b\}$

In [77]:
A=[1,2,3]
B=[a,b]
C=['jedan','dva','osam']
In [78]:
cartesian_product((A,B))
Out[78]:
The Cartesian product of ({1, 2, 3}, {a, b})
In [79]:
list(cartesian_product([A,B]))
Out[79]:
[(1, a), (1, b), (2, a), (2, b), (3, a), (3, b)]
In [80]:
Set(map(tuple,list(cartesian_product([A,B]))))
Out[80]:
{(1, b), (2, a), (2, b), (3, a), (1, a), (3, b)}

možemo definirati svoju funkciju koja će nam dati kartezijev produkt kao skup uređenih n-torki. Parametar lista u donjoj funkciji je uređena n-torka skupova (zadanih kao liste) čiji Kartezijev produkt želimo izračunati.

In [81]:
def kartezijev_produkt(lista):
    pr = list(cartesian_product(lista))
    pr = map(tuple, pr)
    return Set(pr)

$A\times B$

In [82]:
kartezijev_produkt((A,B))
Out[82]:
{(1, b), (2, a), (2, b), (3, a), (1, a), (3, b)}

$B\times A$

In [83]:
kartezijev_produkt((B,A))
Out[83]:
{(b, 1), (a, 1), (b, 2), (b, 3), (a, 2), (a, 3)}

$(A\times B)\cap(B\times A)=\emptyset$

In [84]:
kartezijev_produkt((A,B)).intersection(kartezijev_produkt((B,A)))
Out[84]:
{}

$A\times C\times B$

In [85]:
kartezijev_produkt((A,C,B))
Out[85]:
{(1, 'dva', b), (2, 'jedan', b), (1, 'osam', b), (3, 'jedan', b), (2, 'osam', b), (1, 'jedan', a), (2, 'dva', a), (1, 'dva', a), (3, 'osam', b), (2, 'jedan', a), (3, 'dva', b), (1, 'osam', a), (3, 'jedan', a), (2, 'osam', a), (3, 'dva', a), (3, 'osam', a), (2, 'dva', b), (1, 'jedan', b)}

$B\times A\times B$

In [86]:
kartezijev_produkt((B,A,B))
Out[86]:
{(a, 3, a), (b, 3, b), (a, 3, b), (a, 1, a), (a, 2, a), (a, 1, b), (b, 2, a), (a, 2, b), (b, 2, b), (b, 3, a), (b, 1, a), (b, 1, b)}

Zadatak

Zadani su skupovi $A=\{1,2,3\},$ $B=\{1,2,\{3\}\},$ $C=\{1,\{2\},\{3\}\},$ $D=\{\{1\},\{2\},\{3\}\}$. Odredite:  $A\cup B,$   $C\cup D,$   $A\cap B,$   $(B\cap D)\cup A,$   $(A\cap D)\cup B,$   $A\setminus B,$   $B\setminus A,$   $A\mathrel{\triangle}B,$   $\mathcal{P}(A)$  i  $\mathcal{P}(C)$.

Rješenje

In [87]:
A=Set([1,2,3])
B=Set([1,2,Set([3])])
C=Set([1,Set([2]),Set([3])])
D=Set([Set([1]),Set([2]),Set([3])])
In [88]:
A,B,C,D
Out[88]:
({1, 2, 3}, {{3}, 1, 2}, {{3}, 1, {2}}, {{3}, {2}, {1}})

$A\cup B$

In [89]:
A.union(B)
Out[89]:
{{3}, 1, 2, 3}

$C\cup D$

In [90]:
C.union(D)
Out[90]:
{{3}, 1, {1}, {2}}

$A\cap B$

In [91]:
A.intersection(B)
Out[91]:
{1, 2}

$(B\cap D)\cup A$

In [92]:
B.intersection(D).union(A)
Out[92]:
{{3}, 1, 2, 3}

$(A\cap D)\cup B$

In [93]:
A.intersection(D).union(B)
Out[93]:
{{3}, 1, 2}

$A\setminus B$

In [94]:
A.difference(B)
Out[94]:
{3}

$B\setminus A$

In [95]:
B.difference(A)
Out[95]:
{{3}}

$A\mathrel{\triangle}B$

In [96]:
A.symmetric_difference(B)
Out[96]:
{{3}, 3}

$\mathcal{P}(A)$

In [97]:
Set(A.subsets())
Out[97]:
{{2}, {2, 3}, {1, 2}, {1, 2, 3}, {3}, {1}, {}, {1, 3}}

$\mathcal{P}(C)$

In [98]:
Set(C.subsets())
Out[98]:
{{{3}, 1}, {1, {2}}, {{3}, 1, {2}}, {{3}, {2}}, {{2}}, {{3}}, {1}, {}}

Zadatak

Zadani su skupovi

\begin{align*} A&=\big\{x\mid x\in\mathbb{Z}, -3 < x < 5\big\}\\[3pt] B&=\big\{x\in\mathbb{N}\mid x\text{ je višekratnik od }3\big\}\\[3pt] C&=\big\{x : (2x+1)(x-1)=0\big\}\\[3pt] D&=\big\{x\in\mathbb{Z} : (2x+1)(x-1)\geq0\big\}\\[3pt] E&=\big\{x\in\mathbb{N}\mid x\text{ je višekratnik od }6\big\} \end{align*}
  1. Ispišite elemente zadanih skupova.
  2. Odredite $A\cup B$, $A\cap D$, $B\cup E$, $B^c$ ako je $U=\mathbb{Z}$.

Rješenje

Zadavanje skupova

In [99]:
A=Set(IntegerRange(-2,5))
B=Set(IntegerRange(3,Infinity,3))
var('x')
rj=solve((2*x+1)*(x-1)==0,x,solution_dict=True)
C=Set(map(lambda y: y[x],rj))
E=Set(IntegerRange(6,Infinity,6))
In [100]:
solve((2*x+1)*(x-1)>=0,x)
Out[100]:
[[x <= (-1/2)], [x >= 1]]
In [101]:
D=Set(ZZ).difference(Set([0]))

Ispisivanje elemenata

In [102]:
A
Out[102]:
{0, 1, 2, 3, 4, -1, -2}
In [103]:
B
Out[103]:
Set of elements of {3, 6, ...}
In [104]:
C
Out[104]:
{1, -1/2}
In [105]:
D
Out[105]:
Set-theoretic difference of Set of elements of Integer Ring and {0}
In [106]:
E
Out[106]:
Set of elements of {6, 12, ...}

$A\cup C$

In [107]:
A.union(C)
Out[107]:
{0, 1, 2, 3, 4, -1/2, -1, -2}

$A\cap D=\{-2,-1,1,2,3,4\}$

In [108]:
A.intersection(D)
Out[108]:
Set-theoretic intersection of {0, 1, 2, 3, 4, -1, -2} and Set-theoretic difference of Set of elements of Integer Ring and {0}
In [109]:
-2 in A.intersection(D),-1 in A.intersection(D),1 in A.intersection(D),2 in A.intersection(D),3 in A.intersection(D),4 in A.intersection(D)
Out[109]:
(True, True, True, True, True, True)
In [110]:
-3 in A.intersection(D),0 in A.intersection(D),5 in A.intersection(D)
Out[110]:
(False, False, False)

$B\cup E=B$

In [111]:
B.union(E)
Out[111]:
Set-theoretic union of Set of elements of {3, 6, ...} and Set of elements of {6, 12, ...}
In [112]:
1 in B.union(E), 2 in B.union(E), 3 in B.union(E), 4 in B.union(E), 5 in B.union(E), 6 in B.union(E), 7 in B.union(E)
Out[112]:
(False, False, True, False, False, True, False)

$B^c$ se sastoji od svih negativnih cijelih brojeva i nule, te od svih prirodnih brojeva koji nisu djeljivi s 3

In [113]:
Bc=Set(ZZ).difference(B);Bc
Out[113]:
Set-theoretic difference of Set of elements of Integer Ring and Set of elements of {3, 6, ...}
In [114]:
-3 in Bc, -2 in Bc, -1 in Bc, 0 in Bc, 1 in Bc, 2 in Bc, 3 in Bc, 4 in Bc, 5 in Bc, 6 in Bc, 7 in Bc
Out[114]:
(True, True, True, True, True, True, False, True, True, False, True)

Zadatak

Neka su $A=\big\{1,\{2,3\}\big\}$ i $B=\big\{1,2,\{3\}\big\}$. Odredite $\mathcal{P}(A)$,  $\mathcal{P}(B)$,  $\mathcal{P}(A)\cup \mathcal{P}(B)$,  $\mathcal{P}(A\cup B)$,  $\mathcal{P}(A)\cap \mathcal{P}(B)$,  $\mathcal{P}(A\cap B)$.

Rješenje

In [115]:
A=Set([1,Set([2,3])])
B=Set([1,2,Set([3])])
In [116]:
print(A,B)
{1, {2, 3}} {{3}, 1, 2}

$\mathcal{P}(A)$

In [117]:
PA=Set(A.subsets());PA
Out[117]:
{{}, {1}, {{2, 3}}, {1, {2, 3}}}

$\mathcal{P}(B)$

In [118]:
PB=Set(B.subsets());PB
Out[118]:
{{2}, {{3}, 1}, {1, 2}, {{3}, 1, 2}, {{3}}, {1}, {}, {{3}, 2}}

$\mathcal{P}(A)\cup\mathcal{P}(B)$

In [119]:
skup1=PA.union(PB);skup1
Out[119]:
{{2}, {{3}, 1}, {1, 2}, {{3}, 1, 2}, {{3}}, {1}, {{2, 3}}, {}, {{3}, 2}, {1, {2, 3}}}

$\mathcal{P}(A\cup B)$

In [120]:
skup2=Set(A.union(B).subsets());skup2
Out[120]:
{{2}, {{3}, 1}, {1, 2}, {2, {2, 3}}, {{3}, 1, 2}, {{3}, {2, 3}}, {{3}, 2, {2, 3}}, {{3}, 1, {2, 3}}, {{3}}, {1}, {{2, 3}}, {{3}, 1, 2, {2, 3}}, {}, {{3}, 2}, {1, 2, {2, 3}}, {1, {2, 3}}}

$\mathcal{P}(A\cup B)\neq\mathcal{P}(A)\cup\mathcal{P}(B)$

In [121]:
skup1==skup2
Out[121]:
False

$\mathcal{P}(A)\cup\mathcal{P}(B)\subseteq\mathcal{P}(A\cup B)$

In [122]:
skup1.set().issubset(skup2.set())
Out[122]:
True

$\mathcal{P}(A)\cap\mathcal{P}(B)$

In [123]:
skup3=PA.intersection(PB);skup3
Out[123]:
{{}, {1}}

$\mathcal{P}(A\cap B)$

In [124]:
skup4=Set(A.intersection(B).subsets());skup4
Out[124]:
{{}, {1}}

$\mathcal{P}(A\cap B)=\mathcal{P}(A)\cap\mathcal{P}(B)$

In [125]:
skup4==skup3
Out[125]:
True

Zadatak

Prikažite grafički skup $A\times B$ ako je  $A=\{1,3,5\}$,  $B=\langle 1,2]$.

Rješenje

In [126]:
tocke1=point(((1,2),(3,2),(5,2)),rgbcolor='black',size=30)
tocke2=circle((1,1),0.035,fill=True,edgecolor='black',facecolor='white')+circle((3,1),0.035,fill=True,edgecolor='black',
        facecolor='white')+circle((5,1),0.035,fill=True,edgecolor='black',facecolor='white')
linije=line([(1,1),(1,2)],rgbcolor='black',thickness=1.2)+line([(3,1),(3,2)],rgbcolor='black',thickness=1.2)+line([(5,1),(5,2)],
            rgbcolor='black',thickness=1.2)
show(linije+tocke1+tocke2,xmin=-1,xmax=6,ymin=-0.3,ymax=3,aspect_ratio=1,figsize=(6,4))

Zadatak

Prikažite grafički skup $A\times B$ ako je  $A=\langle 1,5]$,  $B=\langle 1,2]$.

Rješenje

In [127]:
pravokutnik=polygon([(1,1),(5,1),(5,2),(1,2)],rgbcolor='yellow')
crta1=line([(1,1),(5,1)],thickness=1.2,linestyle='--',rgbcolor='black')
crta2=line([(5,1),(5,2)],thickness=1.2,rgbcolor='black')
crta3=line([(1,2),(5,2)],thickness=1.2,rgbcolor='black')
crta4=line([(1,1),(1,2)],thickness=1.2,linestyle='--',rgbcolor='black')
show(pravokutnik+crta1+crta2+crta3+crta4,xmin=-1,xmax=6,ymin=-0.3,ymax=3,aspect_ratio=1,figsize=(6,4))

Zadatak

Prikažite grafički skup  $\big\{(x,y)\in\mathbb{R}^2\mid (x-1)^2+y^2\leq 1\big\}$.

Rješenje

In [128]:
var('y')
region_plot((x-1)^2+y^2<=1,(x,-1,3),(y,-2,2),aspect_ratio=1,incol='yellow',bordercol='black',figsize=(6,4))
Out[128]:

Zadatak

Neka je $A=\{1,2,3,4\}$,  $B=\{1,2,5\}$,  $C=\{7,8,9\}$. Provjerite da vrijedi:

  1. $(A\cap B)\times C=(A\times C)\cap(B\times C)$,
  2. $(A\cup B)\times C=(A\times C)\cup(B\times C)$.

Rješenje

In [129]:
A=Set([1,2,3,4])
B=Set([1,2,5])
C=Set([7,8,9])

$(A\cap B)\times C=(A\times C)\cap(B\times C)$

In [130]:
lijevo1 = kartezijev_produkt((A.intersection(B),C))
lijevo1
Out[130]:
{(2, 9), (1, 7), (2, 7), (1, 8), (1, 9), (2, 8)}
In [131]:
desno1 = kartezijev_produkt((A,C)).intersection(kartezijev_produkt((B,C)))
desno1
Out[131]:
{(2, 9), (1, 7), (2, 7), (1, 8), (1, 9), (2, 8)}
In [132]:
lijevo1 == desno1
Out[132]:
True

$(A\cup B)\times C=(A\times C)\cup(B\times C)$

In [133]:
lijevo2 = kartezijev_produkt([A.union(B),C])
lijevo2
Out[133]:
{(3, 8), (2, 7), (5, 8), (4, 9), (3, 7), (1, 8), (5, 7), (4, 7), (2, 9), (1, 7), (3, 9), (4, 8), (5, 9), (1, 9), (2, 8)}
In [134]:
desno2 = kartezijev_produkt([A,C]).union(kartezijev_produkt([B,C]))
desno2
Out[134]:
{(3, 8), (2, 7), (5, 8), (4, 9), (3, 7), (1, 9), (1, 8), (5, 7), (2, 9), (1, 7), (3, 9), (4, 8), (5, 9), (4, 7), (2, 8)}
In [135]:
lijevo2 == desno2
Out[135]:
True
In [ ]: