Relation
The galactic.algebras.relational
module defines types for relations.
and their implementations:
FiniteSelection
which is a view on a finite relation;FiniteProjection
which is a view on a finite relation;
Mixins are made public for sub-classes:
- class AbstractRelation(*args, **kwargs)
Bases:
Container
[tuple
[Any
, …]],Protocol
It represents finitary relation.
- class AbstractFiniteRelation(*args, **kwargs)
Bases:
Collection
[tuple
[Any
, …]],Protocol
It represents finitary relation.
See https://en.wikipedia.org/wiki/Finitary_relation
- abstract property universes: tuple[Collection[Any], ...]
Get the universes of this relation.
- Return type:
The universes of this relation.
- class AbstractBinaryRelation(*args, **kwargs)
Bases:
Container
[tuple
[_E
,_F
]],Protocol
[_E
,_F
]It represents binary relation.
See https://en.wikipedia.org/wiki/Binary_relation
- abstract property universes: tuple[Container[_E], Container[_F]]
Get the universes of this relation.
- Return type:
The universes of this relation.
- abstract property domain: Container[_E]
Get the domain of this binary relation.
It returns the first universe.
- Return type:
The domain of this binary relation.
- abstract property co_domain: Container[_F]
Get the co-domain of this binary relation.
It returns the second universe.
- Return type:
The co-domain of this binary relation.
- abstract successors(element)
Get the successors of an element.
- Parameters:
element (
TypeVar
(_E
)) – The element whose successors are requested- Returns:
The successors.
- Return type:
Container[_F]
- Raises:
- class AbstractFiniteBinaryRelation(*args, **kwargs)
Bases:
Collection
[tuple
[_E
,_F
]],Protocol
[_E
,_F
]It represents binary relation.
See https://en.wikipedia.org/wiki/Binary_relation
- abstract property universes: tuple[Collection[_E], Collection[_F]]
Get the universes of this relation.
- Return type:
The universes of this relation.
- abstract property domain: Collection[_E]
Get the domain of this binary relation.
It returns the first universe.
- Return type:
The domain of this binary relation.
- abstract property co_domain: Collection[_F]
Get the co-domain of this binary relation.
It returns the second universe.
- Return type:
The co-domain of this binary relation.
- abstract successors(element)
Get the successors of an element.
- Parameters:
element (
TypeVar
(_E
)) – The element whose successors are requested- Returns:
The successors.
- Return type:
Collection[_F]
- Raises:
- class AbstractEndoRelation(*args, **kwargs)
Bases:
AbstractBinaryRelation
[_E
,_E
],Protocol
[_E
]It represents 2-relation from a universe to itself.
- abstract property co_domain: Container[_F]
Get the co-domain of this binary relation.
It returns the second universe.
- Return type:
The co-domain of this binary relation.
- abstract property domain: Container[_E]
Get the domain of this binary relation.
It returns the first universe.
- Return type:
The domain of this binary relation.
- abstract predecessors(element)
Get the predecessors of an element.
- Parameters:
element (
TypeVar
(_F
)) – The element whose predecessors are requested- Returns:
The predecessors.
- Return type:
Container[_E]
- Raises:
- class AbstractFiniteEndoRelation(*args, **kwargs)
Bases:
AbstractFiniteBinaryRelation
[_E
,_E
],Protocol
[_E
]It represents 2-relation from a universe to itself.
- abstract property co_domain: Collection[_F]
Get the co-domain of this binary relation.
It returns the second universe.
- Return type:
The co-domain of this binary relation.
- abstract property domain: Collection[_E]
Get the domain of this binary relation.
It returns the first universe.
- Return type:
The domain of this binary relation.
- abstract predecessors(element)
Get the predecessors of an element.
- Parameters:
element (
TypeVar
(_F
)) – The element whose predecessors are requested- Returns:
The predecessors.
- Return type:
Collection[_E]
- Raises:
- abstract successors(element)
Get the successors of an element.
- Parameters:
element (
TypeVar
(_E
)) – The element whose successors are requested- Returns:
The successors.
- Return type:
Collection[_F]
- Raises:
- abstract property universes: tuple[Collection[_E], Collection[_F]]
Get the universes of this relation.
- Return type:
The universes of this relation.
- class AbstractDirectedAcyclicRelation(*args, **kwargs)
Bases:
AbstractBinaryRelation
[_E
,_E
],Protocol
[_E
]It represents directed acyclic graph.
- abstract property sinks: Container[_E]
Get the sinks of this DAG.
- Return type:
The sinks of this DAG.
- abstract property sources: Container[_E]
Get the sources of this DAG.
- Return type:
The sources of this DAG.
- abstract property co_domain: Container[_F]
Get the co-domain of this binary relation.
It returns the second universe.
- Return type:
The co-domain of this binary relation.
- abstract property domain: Container[_E]
Get the domain of this binary relation.
It returns the first universe.
- Return type:
The domain of this binary relation.
- abstract predecessors(element)
Get the predecessors of an element.
- Parameters:
element (
TypeVar
(_F
)) – The element whose predecessors are requested- Returns:
The predecessors.
- Return type:
Container[_E]
- Raises:
- class AbstractFiniteDirectedAcyclicRelation(*args, **kwargs)
Bases:
AbstractFiniteEndoRelation
[_E
],Protocol
[_E
]It represents directed acyclic graph.
- abstract property sinks: Collection[_E]
Get the sinks of this DAG.
- Return type:
The sinks of this DAG.
- abstract property sources: Collection[_E]
Get the sources of this DAG.
- Return type:
The sources of this DAG.
- abstract property co_domain: Collection[_F]
Get the co-domain of this binary relation.
It returns the second universe.
- Return type:
The co-domain of this binary relation.
- abstract property domain: Collection[_E]
Get the domain of this binary relation.
It returns the first universe.
- Return type:
The domain of this binary relation.
- abstract predecessors(element)
Get the predecessors of an element.
- Parameters:
element (
TypeVar
(_F
)) – The element whose predecessors are requested- Returns:
The predecessors.
- Return type:
Collection[_E]
- Raises:
- abstract successors(element)
Get the successors of an element.
- Parameters:
element (
TypeVar
(_E
)) – The element whose successors are requested- Returns:
The successors.
- Return type:
Collection[_F]
- Raises:
- abstract property universes: tuple[Collection[_E], Collection[_F]]
Get the universes of this relation.
- Return type:
The universes of this relation.
- class FrozenFiniteRelation(*universes, elements=None)
Bases:
ConcreteFiniteRelationMixin
It represents concrete finite relation.
- Parameters:
- Raises:
ValueError – If a tuple element is not of the correct size.
ValueError – If a value is not in the corresponding universe.
- property universes: tuple[Collection[Any], ...]
Get the universes of this relation.
- Return type:
The universes of this relation.
- copy()
Compute a copy of this relation.
- Return type:
Self
- Returns:
A copy of this relation.
- difference(*others)
Compute the difference between the relation and the others.
- intersection(*others)
Compute the intersection between the relation and the others.
- isdisjoint(other)
Test if the relation is disjoint from the other.
- issubset(other)
Test whether every element in the relation is in other.
- issuperset(other)
Test whether every element in the other relation is in the relation.
- symmetric_difference(other)
Compute the symmetric difference between the relation and the other.
- class MutableFiniteRelation(*universes, elements=None)
Bases:
FrozenFiniteRelation
,MutableFiniteRelationMixin
It represents concrete finite relation.
A relation is a subset of the Cartesian product between domains.
- Parameters:
Examples
>>> from galactic.algebras.relational import MutableFiniteRelation >>> relation = MutableFiniteRelation( ... [1, 2, 3], ... [2, 3, 4], ... [3, 4, 5], ... elements=[(1, 2, 3), (2, 3, 4), (3, 4, 5)] ... ) >>> list(relation) [(1, 2, 3), (2, 3, 4), (3, 4, 5)] >>> relation.discard((2, 3, 4)) >>> list(relation) [(1, 2, 3), (3, 4, 5)] >>> list(relation.universes[0]) [1, 2, 3] >>> list(relation.universes[1]) [2, 3, 4] >>> list(relation.universes[2]) [3, 4, 5]
- add(element)
Add a element to the relation.
- copy()
Compute a copy of this relation.
- Return type:
Self
- Returns:
A copy of this relation.
- difference(*others)
Compute the difference between the relation and the others.
- difference_update(*others)
Update a relation with the difference of itself and others.
- discard(element)
Discard a tuple from the relation.
- extend(iterable)
Extend the relation.
- intersection(*others)
Compute the intersection between the relation and the others.
- intersection_update(*others)
Update a relation with the intersection of itself and others.
- isdisjoint(other)
Test if the relation is disjoint from the other.
- issubset(other)
Test whether every element in the relation is in other.
- issuperset(other)
Test whether every element in the other relation is in the relation.
- pop()
Remove and return an arbitrary element from the relation.
- remove(element)
Remove element from the relation if it is present.
- symmetric_difference(other)
Compute the symmetric difference between the relation and the other.
- symmetric_difference_update(other)
Update a relation with the symmetric difference of itself and the other.
- union(*others)
Compute the union of the relation and the others.
- update(*others)
Update a relation with the union of itself and others.
- class FiniteSelection(relation, criteria)
Bases:
FiniteRelationMixin
Select some tuple from a relation using criteria.
- Parameters:
relation (
AbstractFiniteRelation
) – An relation.
Examples
>>> from galactic.algebras.relational import MutableFiniteRelation, FiniteSelection >>> relation = MutableFiniteRelation( ... [1, 2, 3], ... [2, 3, 4], ... [3, 4, 5], ... elements=[(1, 2, 3), (2, 3, 3), (1, 4, 5)] ... ) >>> selection = FiniteSelection(relation, {2: 3}) >>> list(selection) [(1, 2, 3), (2, 3, 3)]
- property universes: tuple[Collection[Any], ...]
Get the universe of this relation.
- Return type:
The universes.
- isdisjoint(other)
Test if the relation is disjoint from the other.
- issubset(other)
Test whether every element in the relation is in other.
- class FiniteProjection(relation, indexes)
Bases:
FiniteRelationMixin
Project a relation.
- Parameters:
relation (
AbstractFiniteRelation
) – A relation.
Examples
>>> from galactic.algebras.relational import MutableFiniteRelation, FiniteProjection >>> relation = MutableFiniteRelation( ... [1, 2, 3], ... [2, 3, 4], ... [3, 4, 5], ... elements=[(1, 2, 3), (2, 3, 3), (1, 4, 5)]) >>> projection = FiniteProjection(relation, [1, 2]) >>> list(projection) [(2, 3), (3, 3), (4, 5)]
- isdisjoint(other)
Test if the relation is disjoint from the other.
- issubset(other)
Test whether every element in the relation is in other.
- issuperset(other)
Test whether every element in the other relation is in the relation.
- property universes: tuple[Collection[Any], ...]
Get the universe of this relation.
- Return type:
The universes.
- class FrozenFiniteBinaryRelation(domain, co_domain, *, elements=None)
Bases:
ConcreteFiniteRelationMixin
,Generic
[_E
,_F
]It represents concrete frozen binary relation.
- Parameters:
- property universes: tuple[Collection[_E], Collection[_F]]
Get the universes of this relation.
- Return type:
The universes of this relation.
- property domain: Collection[_E]
Get the domain of this binary relation.
It returns the first universe.
- Return type:
The domain of this binary relation.
- property co_domain: Collection[_F]
Get the co-domain of this binary relation.
It returns the second universe.
- Return type:
The co-domain of this binary relation.
- successors(element)
Get the successors of an element.
- Parameters:
element (
TypeVar
(_E
)) – The element whose successors are requested- Returns:
The successors.
- Return type:
Collection[_F]
- Raises:
ValueError – If the element does not belong to the relation.
- predecessors(element)
Get the predecessors of an element.
- Parameters:
element (
TypeVar
(_F
)) – The element whose predecessors are requested- Returns:
The predecessors.
- Return type:
Collection[_E]
- Raises:
ValueError – If the element does not belong to the relation.
- copy()
Compute a copy of this relation.
- Return type:
Self
- Returns:
A copy of this relation.
- difference(*others)
Compute the difference between the relation and the others.
- intersection(*others)
Compute the intersection between the relation and the others.
- isdisjoint(other)
Test if the relation is disjoint from the other.
- issubset(other)
Test whether every element in the relation is in other.
- issuperset(other)
Test whether every element in the other relation is in the relation.
- symmetric_difference(other)
Compute the symmetric difference between the relation and the other.
- class MutableFiniteBinaryRelation(domain, co_domain, *, elements=None)
Bases:
FrozenFiniteBinaryRelation
[_E
,_F
],MutableFiniteRelationMixin
,Generic
[_E
,_F
]It represents concrete mutable binary relation.
- Parameters:
Examples
>>> from galactic.algebras.relational import MutableFiniteBinaryRelation >>> b = MutableFiniteBinaryRelation[int, int]( ... domain=[1, 2, 3], ... co_domain=[2, 3, 4] ... ) >>> list(b) [] >>> list(b.domain) [1, 2, 3] >>> list(b.co_domain) [2, 3, 4] >>> list(b.universes[0]) [1, 2, 3] >>> b.update([(1, 4), (3, 4)]) >>> list(b) [(1, 4), (3, 4)] >>> list(b.successors(1)) [4] >>> list(b.predecessors(4)) [1, 3] >>> len(b.predecessors(4)) 2
- add(element)
Add a element to the relation.
- copy()
Compute a copy of this relation.
- Return type:
Self
- Returns:
A copy of this relation.
- difference(*others)
Compute the difference between the relation and the others.
- difference_update(*others)
Update a relation with the difference of itself and others.
- discard(element)
Discard a tuple from the relation.
- extend(iterable)
Extend the relation.
- intersection(*others)
Compute the intersection between the relation and the others.
- intersection_update(*others)
Update a relation with the intersection of itself and others.
- isdisjoint(other)
Test if the relation is disjoint from the other.
- issubset(other)
Test whether every element in the relation is in other.
- issuperset(other)
Test whether every element in the other relation is in the relation.
- pop()
Remove and return an arbitrary element from the relation.
- predecessors(element)
Get the predecessors of an element.
- Parameters:
element (
TypeVar
(_F
)) – The element whose predecessors are requested- Returns:
The predecessors.
- Return type:
Collection[_E]
- Raises:
ValueError – If the element does not belong to the relation.
- remove(element)
Remove element from the relation if it is present.
- successors(element)
Get the successors of an element.
- Parameters:
element (
TypeVar
(_E
)) – The element whose successors are requested- Returns:
The successors.
- Return type:
Collection[_F]
- Raises:
ValueError – If the element does not belong to the relation.
- symmetric_difference(other)
Compute the symmetric difference between the relation and the other.
- symmetric_difference_update(other)
Update a relation with the symmetric difference of itself and the other.
- union(*others)
Compute the union of the relation and the others.
- update(*others)
Update a relation with the union of itself and others.
- property universes: tuple[MutableCollection[_E], MutableCollection[_F]]
Get the universes of this relation.
- Return type:
The universes of this relation.
- property domain: MutableCollection[_E]
Get the domain of this binary relation.
It returns the first universe.
- Return type:
The domain of this binary relation.
- property co_domain: MutableCollection[_F]
Get the co-domain of this binary relation.
It returns the second universe.
- Return type:
The co-domain of this binary relation.
- class FrozenFiniteEndoRelation(domain, *, elements=None)
Bases:
FrozenFiniteBinaryRelation
[_E
,_E
]It represents concrete frozen endo relation.
- Parameters:
- property co_domain: Collection[_F]
Get the co-domain of this binary relation.
It returns the second universe.
- Return type:
The co-domain of this binary relation.
- copy()
Compute a copy of this relation.
- Return type:
Self
- Returns:
A copy of this relation.
- difference(*others)
Compute the difference between the relation and the others.
- property domain: Collection[_E]
Get the domain of this binary relation.
It returns the first universe.
- Return type:
The domain of this binary relation.
- intersection(*others)
Compute the intersection between the relation and the others.
- isdisjoint(other)
Test if the relation is disjoint from the other.
- issubset(other)
Test whether every element in the relation is in other.
- issuperset(other)
Test whether every element in the other relation is in the relation.
- predecessors(element)
Get the predecessors of an element.
- Parameters:
element (
TypeVar
(_F
)) – The element whose predecessors are requested- Returns:
The predecessors.
- Return type:
Collection[_E]
- Raises:
ValueError – If the element does not belong to the relation.
- successors(element)
Get the successors of an element.
- Parameters:
element (
TypeVar
(_E
)) – The element whose successors are requested- Returns:
The successors.
- Return type:
Collection[_F]
- Raises:
ValueError – If the element does not belong to the relation.
- symmetric_difference(other)
Compute the symmetric difference between the relation and the other.
- union(*others)
Compute the union of the relation and the others.
- property universes: tuple[Collection[_E], Collection[_F]]
Get the universes of this relation.
- Return type:
The universes of this relation.
- class MutableFiniteEndoRelation(domain, *, elements=None)
Bases:
MutableFiniteBinaryRelation
[_E
,_E
]It represents concrete mutable endo relation.
- Parameters:
Examples
>>> from galactic.algebras.relational import MutableFiniteEndoRelation >>> endo = MutableFiniteEndoRelation[int, int](domain=[1, 2, 3]) >>> list(endo) [] >>> list(endo.domain) [1, 2, 3] >>> list(endo.co_domain) [1, 2, 3] >>> list(endo.universes[0]) [1, 2, 3] >>> endo.update([(1, 2), (2, 3)]) >>> list(endo) [(1, 2), (2, 3)] >>> list(endo.successors(1)) [2] >>> list(endo.predecessors(3)) [2] >>> len(endo.predecessors(3)) 1
- add(element)
Add a element to the relation.
- property co_domain: MutableCollection[_F]
Get the co-domain of this binary relation.
It returns the second universe.
- Return type:
The co-domain of this binary relation.
- copy()
Compute a copy of this relation.
- Return type:
Self
- Returns:
A copy of this relation.
- difference(*others)
Compute the difference between the relation and the others.
- difference_update(*others)
Update a relation with the difference of itself and others.
- discard(element)
Discard a tuple from the relation.
- property domain: MutableCollection[_E]
Get the domain of this binary relation.
It returns the first universe.
- Return type:
The domain of this binary relation.
- extend(iterable)
Extend the relation.
- intersection(*others)
Compute the intersection between the relation and the others.
- intersection_update(*others)
Update a relation with the intersection of itself and others.
- isdisjoint(other)
Test if the relation is disjoint from the other.
- issubset(other)
Test whether every element in the relation is in other.
- issuperset(other)
Test whether every element in the other relation is in the relation.
- pop()
Remove and return an arbitrary element from the relation.
- predecessors(element)
Get the predecessors of an element.
- Parameters:
element (
TypeVar
(_F
)) – The element whose predecessors are requested- Returns:
The predecessors.
- Return type:
Collection[_E]
- Raises:
ValueError – If the element does not belong to the relation.
- remove(element)
Remove element from the relation if it is present.
- successors(element)
Get the successors of an element.
- Parameters:
element (
TypeVar
(_E
)) – The element whose successors are requested- Returns:
The successors.
- Return type:
Collection[_F]
- Raises:
ValueError – If the element does not belong to the relation.
- symmetric_difference(other)
Compute the symmetric difference between the relation and the other.
- symmetric_difference_update(other)
Update a relation with the symmetric difference of itself and the other.
- union(*others)
Compute the union of the relation and the others.
- class FiniteRelationMixin(*args, **kwargs)
Bases:
AbstractFiniteRelation
,Protocol
It represents a mixin for set like comparison.
- isdisjoint(other)
Test if the relation is disjoint from the other.
- issubset(other)
Test whether every element in the relation is in other.
- issuperset(other)
Test whether every element in the other relation is in the relation.
- abstract property universes: tuple[Collection[Any], ...]
Get the universes of this relation.
- Return type:
The universes of this relation.
- class ConcreteFiniteRelationMixin(*universes, elements=None)
Bases:
FiniteRelationMixin
,Protocol
It represents a mixin for set like operations.
- Parameters:
- Raises:
- copy()
Compute a copy of this relation.
- Return type:
Self
- Returns:
A copy of this relation.
- union(*others)
Compute the union of the relation and the others.
- intersection(*others)
Compute the intersection between the relation and the others.
- difference(*others)
Compute the difference between the relation and the others.
- symmetric_difference(other)
Compute the symmetric difference between the relation and the other.
- isdisjoint(other)
Test if the relation is disjoint from the other.
- issubset(other)
Test whether every element in the relation is in other.
- issuperset(other)
Test whether every element in the other relation is in the relation.
- abstract property universes: tuple[Collection[Any], ...]
Get the universes of this relation.
- Return type:
The universes of this relation.
- class MutableFiniteRelationMixin(*universes, elements=None)
Bases:
ConcreteFiniteRelationMixin
,Protocol
It represents a mixin for mutable sets.
- add(element)
Add a element to the relation.
- remove(element)
Remove element from the relation if it is present.
- discard(element)
Discard a tuple from the relation.
- pop()
Remove and return an arbitrary element from the relation.
- extend(iterable)
Extend the relation.
- update(*others)
Update a relation with the union of itself and others.
- intersection_update(*others)
Update a relation with the intersection of itself and others.
- difference_update(*others)
Update a relation with the difference of itself and others.
- symmetric_difference_update(other)
Update a relation with the symmetric difference of itself and the other.
- copy()
Compute a copy of this relation.
- Return type:
Self
- Returns:
A copy of this relation.
- difference(*others)
Compute the difference between the relation and the others.
- intersection(*others)
Compute the intersection between the relation and the others.
- isdisjoint(other)
Test if the relation is disjoint from the other.
- issubset(other)
Test whether every element in the relation is in other.
- issuperset(other)
Test whether every element in the other relation is in the relation.
- symmetric_difference(other)
Compute the symmetric difference between the relation and the other.
- union(*others)
Compute the union of the relation and the others.
- abstract property universes: tuple[Collection[Any], ...]
Get the universes of this relation.
- Return type:
The universes of this relation.