Binary Relation

FiniteBinaryRelation can be used to store a mutable binary relation.

[1]:
from galactic.algebras.relational import FiniteBinaryRelation, SagittalDiagram

It can be initialized using an iterable of couples.

[2]:
relation = FiniteBinaryRelation[int, int]([(1, 2), (1, 3), (2, 3)])
SagittalDiagram(relation)
[2]:
../../_images/notebooks_relational_notebook_3_0.png
[3]:
list(relation)
[3]:
[(1, 2), (1, 3), (2, 3)]

Binary relation are considered as sets.

[4]:
len(relation)
[4]:
3

They have a domain and a co-domain also accessible by a tuple of universes.

[5]:
list(relation.domain)
[5]:
[1, 2]
[6]:
list(relation.universes[0])
[6]:
[1, 2]
[7]:
list(relation.co_domain)
[7]:
[2, 3]
[8]:
list(relation.universes[1])
[8]:
[2, 3]
[9]:
relation.add((2, 4))
SagittalDiagram(relation)
[9]:
../../_images/notebooks_relational_notebook_12_0.png
[10]:
list(relation)
[10]:
[(1, 2), (1, 3), (2, 3), (2, 4)]
[11]:
list(relation.domain)
[11]:
[1, 2]
[12]:
list(relation.co_domain)
[12]:
[2, 3, 4]

Successors and predecessors of an element are considered as sets. They are equivalent to a combination of a selection and a projection.

[13]:
relation.successors(1)
[13]:
<MutableSet object at 0x7f10a02f9200>
[14]:
list(relation.successors(1))
[14]:
[2, 3]
[15]:
relation.successors(1).add(5)
SagittalDiagram(relation)
[15]:
../../_images/notebooks_relational_notebook_19_0.png
[16]:
list(relation)
[16]:
[(1, 2), (1, 3), (1, 5), (2, 3), (2, 4)]
[17]:
relation.successors(1).discard(3)
SagittalDiagram(relation)
[17]:
../../_images/notebooks_relational_notebook_21_0.png
[18]:
list(relation)
[18]:
[(1, 2), (1, 5), (2, 3), (2, 4)]
[19]:
list(relation.domain)
[19]:
[1, 2]
[20]:
list(relation.co_domain)
[20]:
[2, 3, 4, 5]
[21]:
relation.successors(2).clear()
SagittalDiagram(relation)
[21]:
../../_images/notebooks_relational_notebook_25_0.png
[22]:
list(relation)
[22]:
[(1, 2), (1, 5)]
[23]:
list(relation.predecessors(5))
[23]:
[1]
[24]:
predecessors = relation.predecessors(5)
predecessors |= {2, 3, 4}
SagittalDiagram(relation)
[24]:
../../_images/notebooks_relational_notebook_28_0.png
[25]:
list(relation)
[25]:
[(1, 2), (1, 5), (2, 5), (3, 5), (4, 5)]
[26]:
list(relation.domain)
[26]:
[1, 2, 3, 4]
[27]:
relation.domain.discard(1)
SagittalDiagram(relation)
[27]:
../../_images/notebooks_relational_notebook_31_0.png
[28]:
list(relation)
[28]:
[(2, 5), (3, 5), (4, 5)]