Discrete Practical 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Relation as a ordered pair

In[3]:= Pair2Q[{_ , _}] := True;


Pair2Q[_] := False;
Pair2Q[{2, 1}]
Out[5]= True

In[6]:= Pair2Q[1]
Out[6]= False

In[7]:= Pair2Q[{1, 2, 3}]


Out[7]= False

In[8]:= Pair2Q[{{1, 2}, {3, 4}}]


Out[8]= True

In[9]:= Pair2Q[{1, 2, 3, 4}]


Out[9]= False

● // Tuples :
In[10]:= Tuples[{1, 2, 3, 4}, 2]
Out[10]=

{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {2, 1}, {2, 2}, {2, 3},
{2, 4}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {4, 1}, {4, 2}, {4, 3}, {4, 4}}

In[11]:= Tuples[{1, 2}, 3]


Out[11]=

{{1, 1, 1}, {1, 1, 2}, {1, 2, 1}, {1, 2, 2}, {2, 1, 1}, {2, 1, 2}, {2, 2, 1}, {2, 2, 2}}

// Relation based on Divisibility of natural numbers :

In[12]:= Pair2Q[{_ , _}] := True;


Pair2Q[_] := False;
Pair2Q[{2, 1}]
Out[14]=

True

In[15]:= relation2Q_ ? Pair2Q := True;


relation2Q[_] := False;

In[19]:= dividesRelation[A _ : {_Integer}] := Select[Tuples[A, 2], Divisible[#〚2〛, #〚1〛] &]


divideRelation[n_Integer] := Select[Tuples[Range[n], 2], Divisible[#〚2〛, #〚1〛] &]

In[21]:= divideRelation[5]
Out[21]=
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 2}, {2, 4}, {3, 3}, {4, 4}, {5, 5}}
2

In[22]:= A1 = {1, 2, 3, 4, 5}
Out[22]=
{1, 2, 3, 4, 5}

In[23]:= dividesRelation[A1]
Out[23]=
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 2}, {2, 4}, {3, 3}, {4, 4}, {5, 5}}

In[180]:=
div7 = divideRelation[9]
Out[180]=
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9}, {2, 2}, {2, 4},
{2, 6}, {2, 8}, {3, 3}, {3, 6}, {3, 9}, {4, 4}, {4, 8}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}

// divisibility relation (Set):

In[25]:= dividesRelation[A1]
Out[25]=
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 2}, {2, 4}, {3, 3}, {4, 4}, {5, 5}}

In[26]:= LessRelation[A _ : {_Integer}] := Select[Tuples[A, 2], Less[#〚2〛, #〚1〛] &];


LessRelation[A1]
Out[27]=
{{2, 1}, {3, 1}, {3, 2}, {4, 1}, {4, 2}, {4, 3}, {5, 1}, {5, 2}, {5, 3}, {5, 4}}

In[28]:= GreaterRelation[A _ : {_Integer}] := Select[Tuples[A, 2], Greater[#〚2〛, #〚1〛] &];


GreaterRelation[A1]
Out[29]=
{{1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}, {3, 4}, {3, 5}, {4, 5}}

In[30]:= EqualRelation[A _ : {_Integer}] := Select[Tuples[A, 2], Equal[#〚2〛, #〚1〛] &];


EqualRelation[A1]
Out[31]=
{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}

// Divisibility Relation (Integer) :

In[32]:= div8 = divideRelation[8]


Out[32]=
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {2, 2},
{2, 4}, {2, 6}, {2, 8}, {3, 3}, {3, 6}, {4, 4}, {4, 8}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}

In[33]:= LessRelation[A _ : {_Integer}] := Select[Tuples[A, 2], Less[#〚2〛, #〚1〛] &];


A2 = {1, 2, 3, 4, 5, 6, 7, 8};
LessRelation[A2]

Out[35]=
{{2, 1}, {3, 1}, {3, 2}, {4, 1}, {4, 2}, {4, 3}, {5, 1}, {5, 2}, {5, 3}, {5, 4}, {6, 1}, {6, 2}, {6, 3}, {6, 4},
{6, 5}, {7, 1}, {7, 2}, {7, 3}, {7, 4}, {7, 5}, {7, 6}, {8, 1}, {8, 2}, {8, 3}, {8, 4}, {8, 5}, {8, 6}, {8, 7}}
3

In[36]:= GreaterRelationA _ {_Integer} := Select[Tuples[A, 2], Greater[#〚2〛, #〚1〛] &];


A2 = {1, 2, 3, 4, 5, 6, 7, 8};
GreaterRelation[A2]

Out[38]=
{{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {3, 4},
{3, 5}, {3, 6}, {3, 7}, {3, 8}, {4, 5}, {4, 6}, {4, 7}, {4, 8}, {5, 6}, {5, 7}, {5, 8}, {6, 7}, {6, 8}, {7, 8}}

In[39]:= EqualRelationA _ {_Integer} := Select[Tuples[A, 2], Greater[#〚2〛, #〚1〛] &];


A2 = {1, 2, 3, 4, 5, 6, 7, 8};
EqualRelation[A2]
Out[41]=

{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}

In[42]:= relation2Q[%]
Out[42]=

False

// Reverse function reverse the order of elements in the list :

In[43]:= Reverse[{1, 2, 3, 4, 5, 5, 6, 7, 8}]


Out[43]=
{8, 7, 6, 5, 5, 4, 3, 2, 1}

In[44]:= inverseRelation[R _ ? relation2Q] := Reverse[R, 2]

In[188]:=
A2 = {1, 2, 3};
div5 = dividesRelation[A2]
Out[189]=
{{1, 1}, {1, 2}, {1, 3}, {2, 2}, {3, 3}}

In[190]:=
A5 = Reverse[div5]
Out[190]=
{{3, 3}, {2, 2}, {1, 3}, {1, 2}, {1, 1}}

In[191]:=
Reverse[A5]
Out[191]=
{{1, 1}, {1, 2}, {1, 3}, {2, 2}, {3, 3}}

In[193]:=
inverseRelation[A5]
Out[193]=
{{3, 3}, {2, 2}, {3, 1}, {2, 1}, {1, 1}}

In[194]:=
inverseRelation[div8]
Out[194]=
{{1, 1}, {2, 1}, {3, 1}, {2, 2}, {3, 3}}
4

In[196]:=
div9 = divideRelation[7]
Out[196]=
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7},
{2, 2}, {2, 4}, {2, 6}, {3, 3}, {3, 6}, {4, 4}, {5, 5}, {6, 6}, {7, 7}}

In[197]:=

A6 = Reverse[div9]
Out[197]=

{{7, 7}, {6, 6}, {5, 5}, {4, 4}, {3, 6}, {3, 3}, {2, 6},
{2, 4}, {2, 2}, {1, 7}, {1, 6}, {1, 5}, {1, 4}, {1, 3}, {1, 2}, {1, 1}}

In[198]:=

inverseRelation[div9]
Out[198]=

{{1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1},
{2, 2}, {4, 2}, {6, 2}, {3, 3}, {6, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}}

In[199]:=
inverseRelation[A6]
Out[199]=
{{7, 7}, {6, 6}, {5, 5}, {4, 4}, {6, 3}, {3, 3}, {6, 2},
{4, 2}, {2, 2}, {7, 1}, {6, 1}, {5, 1}, {4, 1}, {3, 1}, {2, 1}, {1, 1}}

In[168]:=
A8 = {1, 2, 3, 4, 5, 6, 7, 8}
Out[168]=
{1, 2, 3, 4, 5, 6, 7, 8}

In[170]:=
div9 = dividesRelation[A8]
Out[170]=
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {2, 2},
{2, 4}, {2, 6}, {2, 8}, {3, 3}, {3, 6}, {4, 4}, {4, 8}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}

In[171]:=
div10 = Reverse[A8]
Out[171]=
{8, 7, 6, 5, 4, 3, 2, 1}

In[174]:=
div11 = divideRelation[8]
Out[174]=
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {2, 2},
{2, 4}, {2, 6}, {2, 8}, {3, 3}, {3, 6}, {4, 4}, {4, 8}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}

In[176]:=
mul9 = inverseRelation[div11]
Out[176]=
{{1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1}, {8, 1}, {2, 2},
{4, 2}, {6, 2}, {8, 2}, {3, 3}, {6, 3}, {4, 4}, {8, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}
5

In[58]:= mul10 = inverseRelation[div9]


Out[58]=
inverseRelation[{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {2, 2},
{2, 4}, {2, 6}, {2, 8}, {3, 3}, {3, 6}, {4, 4}, {4, 8}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}]

In[59]:= Reverse[{1, 2, 3, 4}, 2]


Out[59]=
{1, 2, 3, 4}

In[60]:= Reverse[{1, 2, 3, 4}, 1]


Out[60]=

{4, 3, 2, 1}

// Extracting Domain of given relation - Flatten function :

In[61]:= a1 = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}


Out[61]=

{{1, 2}, {1, 3}, {2, 3}, {3, 4}}

In[62]:= Flatten[a1, 1]
Out[62]=

{1, 2, 1, 3, 2, 3, 3, 4}

In[63]:= Union[Flatten[a1, 1]]


Out[63]=

{1, 2, 3, 4}

In[64]:= a2 = {{{}, {1}}, {{}, {2}}, {{}, {1, 2}}, {{1}, {1, 2}}, {{2}, {1, 2}}, {{1, 2}, {1, 2}}}
Out[64]=

{{{}, {1}}, {{}, {2}}, {{}, {1, 2}}, {{1}, {1, 2}}, {{2}, {1, 2}}, {{1, 2}, {1, 2}}}

In[65]:= Flatten[a2, 1]
Out[65]=

{{}, {1}, {}, {2}, {}, {1, 2}, {1}, {1, 2}, {2}, {1, 2}, {1, 2}, {1, 2}}

In[156]:=

Flatten[a2, 2]
Out[156]=

{1, 2, 1, 2, 1, 1, 2, 2, 1, 2, 1, 2, 1, 2}

In[67]:= Union[Flatten[a2, 1]]


Out[67]=

{{}, {1}, {2}, {1, 2}}

In[68]:= a8 = Union[Flatten[a2, 2]]


Out[68]=

{1, 2}

In[129]:=

relation2Q___ ? Pair2Q := True


relation2Q[___] := False
6

In[97]:= FindDomain[R _ ? relation2Q] := Union[Flatten[R, 1]]

In[98]:= a9 = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}


Out[98]=
{{1, 2}, {3, 4}, {5, 6}, {7, 8}}

In[99]:= Flatten[a9, 1]
Out[99]=
{1, 2, 3, 4, 5, 6, 7, 8}

In[158]:=
FindDomain[a9]
Out[158]=
{1, 2, 3, 4, 5, 6, 7, 8}

In[157]:=
FindDomain[a2]
Out[157]=
{{}, {1}, {2}, {1, 2}}

In[161]:=

a3 = {{{}, {1}}, {{1, 2}, {2, 2}}, {{3}, {1, 2}}, {{1}, {1, 2}}, {{2}, {1, 2}}, {1, 2}, {1, 2}, {{4, 5}, {6, 7}}}
Out[161]=

{{{}, {1}}, {{1, 2}, {2, 2}}, {{3}, {1, 2}}, {{1}, {1, 2}}, {{2}, {1, 2}}, {1, 2}, {1, 2}, {{4, 5}, {6, 7}}}

In[162]:=

FindDomain[a3]
Out[162]=

{1, 2, {}, {1}, {2}, {3}, {1, 2}, {2, 2}, {4, 5}, {6, 7}}

In[132]:=

reflexive2Q[R _ ? relation2Q] := Modulea, domain, domain = FindDomain[R];


CatchDoIf[! Member2Q[R, {a, a}], Throw[False]], a, domain;
Throw[True]

In[148]:=
a10 = divideRelation[3]
Out[148]=
{{1, 1}, {1, 2}, {1, 3}, {2, 2}, {3, 3}}

In[149]:=
reflexive2Q[a10]
Out[149]=
True

In[137]:=
A = {{1, 1}, {2, 2}, {3, 3}}
reflexive2Q[A]
Out[137]=
{{1, 1}, {2, 2}, {3, 3}}
Out[138]=
True
7

In[146]:=
A5 = divideRelation[8]
reflexive2Q[A5]
Out[146]=
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {2, 2},
{2, 4}, {2, 6}, {2, 8}, {3, 3}, {3, 6}, {4, 4}, {4, 8}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}
Out[147]=

True

In[151]:=

A8 = {{1, 2}, {1, 1}, {1, 9}, {2, 3}, {3, 1}, {2, 2}}
Out[151]=

{{1, 2}, {1, 1}, {1, 9}, {2, 3}, {3, 1}, {2, 2}}

In[152]:=

reflexive2Q[A8]
Out[152]=

True

In[163]:=

A9 = divideRelation[5]
reflexive2Q[A9]
Out[163]=
{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 2}, {2, 4}, {3, 3}, {4, 4}, {5, 5}}
Out[164]=
True

You might also like