Binary Relation

MutableFiniteBinaryRelation can be used to store a mutable binary relation. You can use FrozenFiniteBinaryRelation for frozen binary relation.

[1]:
from galactic.algebras.relational import MutableFiniteBinaryRelation
from galactic.algebras.relational.renderer import SagittalDiagram

It can be initialized using an iterable of couples.

[2]:
relation = MutableFiniteBinaryRelation[int, int](
    (1, 2, 3),
    (1, 2, 3),
    elements=[(1, 2), (1, 3), (2, 3)],
)
SagittalDiagram(relation)
[2]:
../../../_images/notebooks_algebras_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, 3]
[6]:
list(relation.universes[0])
[6]:
[1, 2, 3]
[7]:
list(relation.co_domain)
[7]:
[1, 2, 3]
[8]:
list(relation.universes[1])
[8]:
[1, 2, 3]
[9]:
relation.add((2, 1))
SagittalDiagram(relation)
[9]:
../../../_images/notebooks_algebras_relational_notebook_12_0.png
[10]:
list(relation)
[10]:
[(1, 2), (1, 3), (2, 3), (2, 1)]
[11]:
list(relation.domain)
[11]:
[1, 2, 3]
[12]:
list(relation.co_domain)
[12]:
[1, 2, 3]

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]:
<MutableSuccessors object at 0x741d41510f80>
[14]:
list(relation.successors(1))
[14]:
[2, 3]
[15]:
relation.successors(1).add(1)
SagittalDiagram(relation)
[15]:
../../../_images/notebooks_algebras_relational_notebook_19_0.png
[16]:
list(relation)
[16]:
[(1, 2), (1, 3), (1, 1), (2, 3), (2, 1)]
[17]:
relation.successors(1).discard(3)
SagittalDiagram(relation)
[17]:
../../../_images/notebooks_algebras_relational_notebook_21_0.png
[18]:
list(relation)
[18]:
[(1, 2), (1, 1), (2, 3), (2, 1)]
[19]:
relation.domain.add(4)
list(relation.domain)
[19]:
[1, 2, 3, 4]
[20]:
list(relation.co_domain)
[20]:
[1, 2, 3]
[21]:
relation.successors(2).clear()
SagittalDiagram(relation)
[21]:
../../../_images/notebooks_algebras_relational_notebook_25_0.png
[22]:
list(relation)
[22]:
[(1, 2), (1, 1)]
[23]:
list(relation.predecessors(2))
[23]:
[1]
[24]:
predecessors = relation.predecessors(2)
predecessors.extend({2, 3, 4})
SagittalDiagram(relation)
[24]:
../../../_images/notebooks_algebras_relational_notebook_28_0.png
[25]:
list(relation)
[25]:
[(1, 2), (1, 1), (2, 2), (3, 2), (4, 2)]
[26]:
relation.domain.discard(1)
SagittalDiagram(relation)
[26]:
../../../_images/notebooks_algebras_relational_notebook_30_0.png
[27]:
list(relation)
[27]:
[(2, 2), (3, 2), (4, 2)]

Endo Relation

MutableFiniteEndoRelation can be used to store a mutable endo relation. You can use FrozenFiniteEndoRelation for frozen endo relation.

[28]:
from galactic.algebras.relational import MutableFiniteEndoRelation
from galactic.algebras.relational.renderer import EndoDiagram, SagittalDiagram

It can be initialized using an iterable of couples.

[29]:
relation = MutableFiniteEndoRelation[int, int](
    (1, 2, 3),
    elements=[(1, 2), (1, 3), (2, 3)],
)
EndoDiagram(relation)
[29]:
../../../_images/notebooks_algebras_relational_notebook_35_0.png
[30]:
SagittalDiagram(relation)
[30]:
../../../_images/notebooks_algebras_relational_notebook_36_0.png

All operations that are possible on binary relations are possible on endo relation.

[31]:
list(relation)
[31]:
[(1, 2), (1, 3), (2, 3)]
[32]:
list(relation.domain)
[32]:
[1, 2, 3]
[33]:
list(relation.co_domain)
[33]:
[1, 2, 3]
[34]:
relation.successors(1)
[34]:
<MutableSuccessors object at 0x741d41511d30>
[35]:
list(relation.successors(1))
[35]:
[2, 3]
[36]:
relation.successors(1).add(1)
EndoDiagram(relation)
[36]:
../../../_images/notebooks_algebras_relational_notebook_43_0.png
[37]:
list(relation)
[37]:
[(1, 2), (1, 3), (1, 1), (2, 3)]
[38]:
relation.successors(1).discard(3)
EndoDiagram(relation)
[38]:
../../../_images/notebooks_algebras_relational_notebook_45_0.png
[39]:
relation.domain.add(4)
list(relation.domain)
[39]:
[1, 2, 3, 4]
[40]:
list(relation.co_domain)
[40]:
[1, 2, 3, 4]
[41]:
EndoDiagram(relation)
[41]:
../../../_images/notebooks_algebras_relational_notebook_48_0.png
[42]:
relation.successors(1).add(4)
EndoDiagram(relation)
[42]:
../../../_images/notebooks_algebras_relational_notebook_49_0.png
[43]:
relation.successors(4).add(3)
EndoDiagram(relation)
[43]:
../../../_images/notebooks_algebras_relational_notebook_50_0.png
[44]:
list(relation)
[44]:
[(1, 2), (1, 1), (1, 4), (2, 3), (4, 3)]
[45]:
list(relation.successors(1))
[45]:
[2, 1, 4]
[46]:
list(relation.successors(2))
[46]:
[3]
[47]:
list(relation.successors(3))
[47]:
[]
[48]:
list(relation.successors(4))
[48]:
[3]
[49]:
list(relation.predecessors(1))
[49]:
[1]
[50]:
list(relation.predecessors(2))
[50]:
[1]
[51]:
list(relation.predecessors(3))
[51]:
[2, 4]
[52]:
list(relation.predecessors(4))
[52]:
[1]
[53]:
relation.co_domain.remove(3)
EndoDiagram(relation)
[53]:
../../../_images/notebooks_algebras_relational_notebook_60_0.png
[54]:
list(relation.domain)
[54]:
[1, 2, 4]
[55]:
list(relation.co_domain)
[55]:
[1, 2, 4]
[56]:
relation.pop()
[56]:
(1, 2)
[57]:
EndoDiagram(relation)
[57]:
../../../_images/notebooks_algebras_relational_notebook_64_0.png
[58]:
relation.pop()
[58]:
(1, 1)
[59]:
EndoDiagram(relation)
[59]:
../../../_images/notebooks_algebras_relational_notebook_66_0.png
[60]:
relation.pop()
[60]:
(1, 4)
[61]:
EndoDiagram(relation)
[61]:
../../../_images/notebooks_algebras_relational_notebook_68_0.png