galactic.algebras.relational.core#
Defines relations and their basic operations.
Protocols
General relation type:
Binary relation types:
Concrete classes
General relation types:
Selectionwhich is a view on a relationProjectionwhich is a view on a relation
Binary relation types:
Mixin classes
- class Relation(*args, **kwargs)#
Bases:
Collection[tuple[Any,...]],ProtocolRepresents a general relation.
A relation is a set of tuples, where each tuple contains elements from one or more universes.
See https://en.wikipedia.org/wiki/Finitary_relation
- __eq__(other)#
Check if this relation is equal to another.
- __bool__()#
Check if the relation is non-empty.
- Returns:
True if the relation is non-empty, False otherwise
- Return type:
- __lt__(other)#
Test if the relation is a proper subset of the other.
- Parameters:
other (
object) â The other relation to compare against- Returns:
True if the relation is a proper subset of the other
- Return type:
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- __gt__(other)#
Test if the relation is a proper superset of the other.
- Parameters:
other (
object) â The other relation to compare against- Returns:
True if the relation is a proper superset of the other
- Return type:
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- __le__(other)#
Test if the relation is a subset of the other.
- Parameters:
other (
object) â The other relation to compare against- Returns:
True if the relation is a subset of the other
- Return type:
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- __ge__(other)#
Test if the relation is a superset of the other.
- Parameters:
other (
object) â The other relation to compare against- Returns:
True if the relation is a superset of the other
- Return type:
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- 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 the relation.
- Returns:
The universes of the relation
- Return type:
tuple[Collection[Any], âŚ]
- class BinaryRelation[_E, _F](*args, **kwargs)#
Bases:
Relation,Collection[tuple[_E,_F]],Protocol,Generic[_E,_F]Represents a binary relation.
A binary relation is a relation that relates elements from two universes. It is a set of ordered pairs, where each pair consists of an element from the first universe and an element from the second universe. Binary relations are fundamental in mathematics and computer science, often used to represent relationships between elements of two sets, such as orderings, mappings, or connections between entities.
See https://en.wikipedia.org/wiki/Binary_relation
- abstract property universes: tuple[Collection[_E], Collection[_F]]#
Get the universes of the binary relation.
- Returns:
The universes of the binary relation
- Return type:
tuple[Collection[_E], Collection[_F]]
- property domain: Collection[_E]#
Get the domain of this binary relation (the first universe).
- Returns:
The domain of the binary relation
- Return type:
Collection[_E]
- property co_domain: Collection[_F]#
Get the co-domain of this binary relation (the second universe).
- Returns:
The co-domain of the binary relation
- Return type:
Collection[_F]
- abstractmethod successors(elem)#
Compute the successors of an element.
- Parameters:
elem (
_E) â The element whose successors are requested- Returns:
The successors of the element
- Return type:
Collection[_F]- Raises:
- abstractmethod predecessors(elem)#
Compute the predecessors of an element.
- Parameters:
elem (
_F) â The element whose predecessors are requested- Returns:
The predecessors of the element
- Return type:
Collection[_E]- Raises:
- class EndoRelation[_E](*args, **kwargs)#
Bases:
BinaryRelation[_E,_E],ProtocolRepresents a binary relation from a universe to itself.
An endo-relation is a specific type of binary relation where both the domain and co-domain are the same universe. This means that the relation relates elements within a single set, allowing for relationships such as orderings, mappings, or connections between elements of that set. Endo-relations are commonly used in mathematics and computer science to represent relationships that are defined within a single universe, such as reflexive, symmetric, or transitive relations.
- class FrozenRelation(universes, *elements)#
Bases:
RelationMixinRepresents a frozen relation.
- Parameters:
- Raises:
ValueError â If a tuple element is not of the correct size
ValueError â If an element is not in the corresponding universe
Examples
>>> from galactic.algebras.relational.core import FrozenRelation >>> relation = FrozenRelation( ... ([1, 2, 3], [2, 3, 4], [3, 4, 5]), ... [(1, 2, 3), (2, 3, 4), (3, 4, 5)], ... ) >>> relation <galactic.algebras.relational.core.FrozenRelation object at 0x...> >>> list(relation) [(1, 2, 3), (2, 3, 4), (3, 4, 5)] >>> relation.universes[0] <galactic...Universe object at 0x...> >>> list(relation.universes[0]) [1, 2, 3] >>> relation.universes[1] <galactic...Universe object at 0x...> >>> list(relation.universes[1]) [2, 3, 4] >>> relation.universes[2] <galactic...Universe object at 0x...> >>> list(relation.universes[2]) [3, 4, 5]
Notes
The relation is immutable after creation.
The universes are sets, and the relation is a subset of the Cartesian product of the universes.
The elements of the relation are tuples, where each tuple has the same length as the number of universes.
The universes are represented as collections of unique elements, and the relation elements are stored in a FIFO set to maintain insertion order.
Hint
Use
MutableRelationfor a mutable version of this class.- __hash__()#
Compute the hash of the relation.
- Returns:
The hash of the relation
- Return type:
Notes
The hash is computed based on the elements of the relation and its universes. It is cached after the first computation for efficiency.
- __contains__(nuple)#
Check if a tuple is in the relation.
- __iter__()#
Iterate over the tuples in the relation.
- __len__()#
Get the number of tuples in the relation.
- Returns:
The number of tuples in the relation
- Return type:
- property universes: tuple[Collection[Any], ...]#
Get the universes of this relation.
- Returns:
The universes of this relation
- Return type:
tuple[Collection[Any], âŚ]
- class MutableRelation(universes, *elements)#
Bases:
MutableRelationMixin,FrozenRelationRepresents a mutable relation.
A relation is a subset of the Cartesian product of universes.
Examples
>>> from galactic.algebras.relational.core import MutableRelation >>> relation = MutableRelation( ... ([1, 2, 3], [2, 3, 4], [3, 4, 5]), ... [(1, 2, 3), (2, 3, 4), (3, 4, 5)] ... ) >>> relation <galactic.algebras.relational.core.MutableRelation object at 0x...> >>> 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]
Notes
The relation is mutable after creation.
The universes are sets, and the relation is a subset of the Cartesian product of the universes.
The elements of the relation are tuples, where each tuple has the same length as the number of universes.
The universes are represented as collections of unique elements, and the relation elements are stored in a FIFO set to maintain insertion order.
Hint
Use
FrozenRelationfor an immutable version of this class.- property universes: tuple[MutableCollection[Any], ...]#
Get the universes of this relation.
- Returns:
The universes of this relation
- Return type:
tuple[MutableCollection[Any], âŚ]
- class Selection(relation, criteria)#
Bases:
RelationSelects some tuples from a relation using criteria.
- Parameters:
Examples
>>> from galactic.algebras.relational.core import MutableRelation >>> from galactic.algebras.relational.core import Selection >>> relation = MutableRelation( ... ([1, 2, 3], [2, 3, 4], [3, 4, 5]), ... [(1, 2, 3), (2, 3, 3), (1, 4, 5)] ... ) >>> relation <galactic.algebras.relational.core.MutableRelation object at 0x...> >>> selection = Selection(relation, lambda nuple: nuple[2] == 3) >>> selection <galactic.algebras.relational.core.Selection object at 0x...> >>> list(selection) [(1, 2, 3), (2, 3, 3)]
Notes
The selection criteria are a callable that takes a tuple and returns a boolean.
If the criteria are empty, all tuples in the relation are selected.
- __bool__()#
Check if the selection is non-empty.
- Returns:
True if the selection is non-empty, False otherwise
- Return type:
- __contains__(nuple)#
Check if a tuple is in the selection.
- __iter__()#
Iterate over the tuples in the selection.
- __len__()#
Get the number of tuples in the selection.
- Returns:
The number of tuples in the selection
- Return type:
- property universes: tuple[Collection[Any], ...]#
Get the universe of this relation.
- Returns:
The universes of the relation
- Return type:
tuple[Collection[Any], âŚ]
- class Projection(relation, indexes)#
Bases:
RelationProjects a relation onto specified indexes.
- Parameters:
Examples
>>> from galactic.algebras.relational.core import MutableRelation >>> from galactic.algebras.relational.core import Projection >>> relation = MutableRelation( ... ([1, 2, 3], [2, 3, 4], [3, 4, 5]), ... [(1, 2, 3), (2, 3, 3), (1, 4, 5)]) >>> relation <galactic.algebras.relational.core.MutableRelation object at 0x...> >>> projection = Projection(relation, [1, 2]) >>> projection <galactic.algebras.relational.core.Projection object at 0x...> >>> list(projection) [(2, 3), (3, 3), (4, 5)]
Notes
The indexes are the positions in the tuples of the relation that will be included in the projected tuples. If the indexes are empty, the projection will yield an empty relation.
If the indexes are out of bounds, they will be ignored.
- __eq__(other)#
Check equality with another relation.
- __bool__()#
Check if the projection is non-empty.
- Returns:
True if the projection is non-empty, False otherwise
- Return type:
- __contains__(nuple)#
Check if a tuple is in the projection.
- __iter__()#
Iterate over the tuples in the projection.
- __len__()#
Get the number of tuples in the projection.
- Returns:
The number of tuples in the projection
- Return type:
- property universes: tuple[Collection[Any], ...]#
Get the universe of this relation.
- Returns:
The universes of the relation
- Return type:
tuple[Collection[Any], âŚ]
- class FrozenBinaryRelation[_E, _F](universes, *elements)#
Bases:
BinaryRelation[_E,_F],RelationMixinRepresents a concrete immutable binary relation.
- Parameters:
Examples
>>> from galactic.algebras.relational.core import FrozenBinaryRelation >>> binary = FrozenBinaryRelation[int, int]( ... ([1, 2, 3], [2, 3, 4]), ... [(1, 2), (2, 3)], ... ) >>> binary <galactic.algebras.relational.core.FrozenBinaryRelation object at 0x...> >>> list(binary) [(1, 2), (2, 3)] >>> list(binary.domain) [1, 2, 3] >>> list(binary.co_domain) [2, 3, 4]
Notes
The domain and co-domain are different.
The relation is immutable after creation.
The elements are pairs of elements from the domain and co-domain.
Hint
The domain and co-domain can be the same, but they are not required to be.
The elements can be added after the creation of the relation, but they must be pairs of elements from the domain and co-domain.
- __hash__()#
Compute the hash of the relation.
- Returns:
The hash of the relation
- Return type:
Notes
The hash is computed as the XOR of the hashes of the two universes and the relation itself. It is cached for efficiency.
- __bool__()#
Check if the relation is non-empty.
- Returns:
True if the relation is non-empty, False otherwise
- Return type:
- __contains__(elem)#
Compute if a pair belongs to the relation.
- __iter__()#
Iterate over the pairs of the relation.
- property universes: tuple[Collection[_E], Collection[_F]]#
Return the universes of this relation.
- Returns:
The universes of this relation.
- Return type:
tuple[Collection[_E], Collection[_F]]
- property domain: Collection[_E]#
Return the domain of this binary relation.
It returns the first universe.
- Returns:
The domain of this binary relation.
- Return type:
Collection[_E]
- property co_domain: Collection[_F]#
Return the co-domain of this binary relation.
It returns the second universe.
- Returns:
The co-domain of this binary relation.
- Return type:
Collection[_F]
- successors(elem)#
Return the successors of an element.
- Parameters:
elem (
_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(elem)#
Return the predecessors of an element.
- Parameters:
elem (
_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
- class MutableBinaryRelation[_E, _F](universes, *elements)#
Bases:
FrozenBinaryRelation[_E,_F],MutableRelationMixinRepresents a concrete mutable binary relation.
Examples
>>> from galactic.algebras.relational.core import MutableBinaryRelation >>> binary = MutableBinaryRelation[int, int](([1, 2, 3], [2, 3, 4])) >>> binary <galactic.algebras.relational.core.MutableBinaryRelation object at 0x...> >>> list(binary) [] >>> list(binary.domain) [1, 2, 3] >>> list(binary.co_domain) [2, 3, 4] >>> list(binary.universes[0]) [1, 2, 3] >>> binary.update([(1, 4), (3, 4)]) >>> list(binary) [(1, 4), (3, 4)] >>> list(binary.successors(1)) [4] >>> list(binary.predecessors(4)) [1, 3] >>> len(binary.predecessors(4)) 2
Notes
The domain and co-domain are different.
The relation is mutable after creation.
The elements are pairs of elements from the domain and co-domain.
Hint
The domain and co-domain can be the same, but they are not required to be.
The elements can be added after the creation of the relation, but they must be pairs of elements from the domain and co-domain.
- property universes: tuple[MutableCollection[_E], MutableCollection[_F]]#
Return the universes of this relation.
- Returns:
The universes of this relation.
- Return type:
tuple[MutableCollection[_E], MutableCollection[_F]]
- property domain: MutableCollection[_E]#
Return the domain of this binary relation.
It returns the first universe.
- Returns:
The domain of this binary relation.
- Return type:
- property co_domain: MutableCollection[_F]#
Return the co-domain of this binary relation.
It returns the second universe.
- Returns:
The co-domain of this binary relation.
- Return type:
- class FrozenEndoRelation[_E](universes, *elements)#
Bases:
FrozenBinaryRelation[_E,_E]Represents a concrete immutable endo relation.
- Parameters:
Notes
The domain and co-domain are the same.
The relation is immutable after creation.
- class MutableEndoRelation[_E](universes, *elements)#
Bases:
MutableBinaryRelation[_E,_E],FrozenEndoRelation[_E]Represents a concrete mutable endo relation.
Examples
>>> from galactic.algebras.relational.core import MutableEndoRelation >>> endo = MutableEndoRelation[int](([1, 2, 3],)) >>> endo <galactic.algebras.relational.core.MutableEndoRelation object at 0x...> >>> 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
Notes
The domain and co-domain are the same.
The relation is mutable and supports adding and removing elements.
Hint
The domain and co-domain are the same, so you can use the same iterable for both parameters. The relation is defined as a set of pairs of elements, where the first element is a predecessor and the second element is a successor.
- class RelationMixin(universes, *elements)#
-
Mixin providing set-like operations for concrete relations.
- Parameters:
- Raises:
Notes
This mixin is used to provide set-like operations for concrete relations.
It is not meant to be used directly, but rather as a base class for other relation classes.
It is used to provide set-like operations for relations, such as union, intersection, difference, and symmetric difference.
- __or__(other)#
Compute the union of the relation and the other.
- Parameters:
other (
object) â Another relation- Returns:
The union of the relation and the other
- Return type:
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- __and__(other)#
Compute the intersection between the relation and the other.
- Parameters:
other (
object) â Another relation- Returns:
The intersection between the relation and the other
- Return type:
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- __sub__(other)#
Compute the difference between the relation and the other.
- Parameters:
other (
object) â Another relation- Returns:
The difference between the relation and the other
- Return type:
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- __xor__(other)#
Compute the symmetric difference between the relation and the other.
- Parameters:
other (
object) â Another relation- Returns:
The symmetric difference between the relation and the other
- Return type:
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- 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.
- class MutableRelationMixin(universes, *elements)#
Bases:
RelationMixin,ABCMixin providing mutability for relations.
Notes
This mixin is used to provide mutability for relations.
It is not meant to be used directly, but rather as a base class for other relation classes.
It provides methods for adding, removing, and discarding elements.
It also provides methods for popping and clearing elements.
- add(nuple)#
Add a tuple to the relation.
- remove(nuple)#
Remove a tuple from the relation if it is present.
- discard(nuple)#
Discard a tuple from the relation.
- pop()#
Remove and return an arbitrary tuple from the relation.
- extend(iterable)#
Extend the relation.
- __ior__(other)#
Update a relation with the union of itself and the other.
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- __iand__(other)#
Update a relation with the intersection of itself and the other.
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- __isub__(other)#
Update a relation with the difference of itself and the other.
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- __ixor__(other)#
Update a relation with the symmetric difference of itself and the other.
Notes
If other is not an instance of Relation or the universes are not compatible, NotImplemented is returned.
- 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.