galactic.algebras.relational.core

Contents

galactic.algebras.relational.core#

Defines relations and their basic operations.

Protocols

General relation type:

Binary relation types:

Concrete classes

General relation types:

Binary relation types:

Mixin classes


class Relation(*args, **kwargs)#

Bases: Collection[tuple[Any, ...]], Protocol

Represents 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.

Parameters:

other (object) – The other relation to compare against

Returns:

True if the relations are equal, False otherwise

Return type:

bool

__bool__()#

Check if the relation is non-empty.

Returns:

True if the relation is non-empty, False otherwise

Return type:

bool

__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:

bool

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:

bool

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:

bool

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:

bool

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.

Parameters:

other (Iterable[tuple[Any, ...]]) – An iterable of ntuples representing the other relation

Returns:

True if the relation is disjoint from the other

Return type:

bool

issubset(other)#

Test whether every element in the relation is in other.

Parameters:

other (Iterable[tuple[Any, ...]]) – An iterable of ntuples representing the other relation

Returns:

True if the relation is a subset of the other

Return type:

bool

issuperset(other)#

Test whether every element in the other relation is in the relation.

Parameters:

other (Iterable[tuple[Any, ...]]) – An iterable of ntuples representing the other relation

Returns:

True if the relation is a superset of the other

Return type:

bool

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:

NotImplementedError –

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:

NotImplementedError –

class EndoRelation[_E](*args, **kwargs)#

Bases: BinaryRelation[_E, _E], Protocol

Represents 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.

See https://en.wikipedia.org/wiki/Homogeneous_relation

class FrozenRelation(universes, *elements)#

Bases: RelationMixin

Represents 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 MutableRelation for a mutable version of this class.

__hash__()#

Compute the hash of the relation.

Returns:

The hash of the relation

Return type:

int

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.

Parameters:

nuple (object) – The tuple to check for membership

Returns:

True if the tuple is in the relation, False otherwise

Return type:

bool

__iter__()#

Iterate over the tuples in the relation.

Returns:

An iterator over the tuples in the relation

Return type:

Iterator[tuple[Any, ...]]

__len__()#

Get the number of tuples in the relation.

Returns:

The number of tuples in the relation

Return type:

int

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, FrozenRelation

Represents 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 FrozenRelation for 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: Relation

Selects some tuples from a relation using criteria.

Parameters:
  • relation (Relation) – A relation to select from

  • criteria (Callable[[tuple[Any, ...]], bool]) – The selection criteria as a mapping from index to value

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:

bool

__contains__(nuple)#

Check if a tuple is in the selection.

Parameters:

nuple (object) – A tuple to check for membership

Returns:

True if the tuple is in the selection, False otherwise

Return type:

bool

__iter__()#

Iterate over the tuples in the selection.

Returns:

An iterator over the tuples in the selection

Return type:

Iterator[tuple[Any, ...]]

__len__()#

Get the number of tuples in the selection.

Returns:

The number of tuples in the selection

Return type:

int

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: Relation

Projects a relation onto specified indexes.

Parameters:
  • relation (Relation) – A relation to project

  • indexes (tuple[int, ...]) – The indexes to project onto

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.

Parameters:

other (object) – Another projection to compare with

Returns:

True if the relations are equal, False otherwise

Return type:

bool

__bool__()#

Check if the projection is non-empty.

Returns:

True if the projection is non-empty, False otherwise

Return type:

bool

__contains__(nuple)#

Check if a tuple is in the projection.

Parameters:

nuple (object) – The tuple to check for membership

Returns:

True if the tuple is in the projection, False otherwise

Return type:

bool

__iter__()#

Iterate over the tuples in the projection.

Returns:

An iterator over the tuples in the projection

Return type:

Iterator[tuple[Any, ...]]

__len__()#

Get the number of tuples in the projection.

Returns:

The number of tuples in the projection

Return type:

int

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], RelationMixin

Represents a concrete immutable binary relation.

Parameters:
  • universes (tuple[Iterable[_E], Iterable[_F]]) – A pair of iterables representing the domain and co-domain.

  • *elements (Iterable[tuple[_E, _F]]) – The initial pairs to populate the relation

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:

int

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:

bool

__contains__(elem)#

Compute if a pair belongs to the relation.

Parameters:

elem (object) – The pair to check

Returns:

True if the pair belongs to the relation, False otherwise

Return type:

bool

__iter__()#

Iterate over the pairs of the relation.

Returns:

An iterator over the pairs of the relation

Return type:

Iterator[tuple[_E, _F]]

__len__()#

Compute the size of the relation.

Returns:

The size of the relation

Return type:

int

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], MutableRelationMixin

Represents 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:

MutableCollection[_E]

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:

MutableCollection[_F]

class FrozenEndoRelation[_E](universes, *elements)#

Bases: FrozenBinaryRelation[_E, _E]

Represents a concrete immutable endo relation.

Parameters:
  • universes (tuple[Iterable[_E]]) – An tuple of one iterable of initial values for the domain

  • *elements (Iterable[tuple[_E, _E]]) – The initial pairs to populate the relation

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)#

Bases: Relation, ABC

Mixin providing set-like operations for concrete relations.

Parameters:
Raises:

NotImplementedError –

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.

__copy__()#

Compute a copy of this relation.

Returns:

A copy of this relation

Return type:

Self

copy()#

Compute a copy of this relation.

Returns:

A copy of this relation

Return type:

Self

__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:

Self

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:

Self

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:

Self

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:

Self

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.

Parameters:

*others (Iterable[tuple[Any, ...]]) – A sequence of iterable containing ntuples

Returns:

The union of the relation and the others

Return type:

Self

intersection(*others)#

Compute the intersection between the relation and the others.

Parameters:

*others (Iterable[tuple[Any, ...]]) – A sequence of iterable containing ntuples

Returns:

The intersection between the relation and the others

Return type:

Self

difference(*others)#

Compute the difference between the relation and the others.

Parameters:

*others (Iterable[tuple[Any, ...]]) – A sequence of iterable containing ntuples

Returns:

The difference between the relation and the others

Return type:

Self

symmetric_difference(other)#

Compute the symmetric difference between the relation and the other.

Parameters:

other (Iterable[tuple[Any, ...]]) – An iterable of elements representing the other relation

Returns:

The symmetric difference between the relation and the other

Return type:

Self

class MutableRelationMixin(universes, *elements)#

Bases: RelationMixin, ABC

Mixin 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.

Parameters:

nuple (tuple[Any, ...]) – The tuple to add

Return type:

None

remove(nuple)#

Remove a tuple from the relation if it is present.

Parameters:

nuple (tuple[Any, ...]) – The tuple to remove

Return type:

None

discard(nuple)#

Discard a tuple from the relation.

Parameters:

nuple (tuple[Any, ...]) – The tuple to discard

Return type:

None

pop()#

Remove and return an arbitrary tuple from the relation.

Returns:

An arbitrary tuple from the relation

Return type:

tuple[Any, ...]

clear()#

Clear the relation.

Return type:

None

extend(iterable)#

Extend the relation.

Parameters:

iterable (Iterable[tuple[Any, ...]]) – An iterable of elements

Return type:

None

__ior__(other)#

Update a relation with the union of itself and the other.

Parameters:

other (object) – Another relation

Returns:

The updated relation

Return type:

Self

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.

Parameters:

other (object) – Another relation

Returns:

The updated relation

Return type:

Self

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.

Parameters:

other (object) – Another relation

Returns:

The updated relation

Return type:

Self

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.

Parameters:

other (object) – Another relation

Returns:

The updated relation

Return type:

Self

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.

Parameters:

*others (Iterable[tuple[Any, ...]]) – A sequence of iterable containing ntuples

Return type:

None

intersection_update(*others)#

Update a relation with the intersection of itself and others.

Parameters:

*others (Iterable[Any]) – A sequence of iterable containing ntuples

Return type:

None

difference_update(*others)#

Update a relation with the difference of itself and others.

Parameters:

*others (Iterable[Any]) – A sequence of iterable containing ntuples

Return type:

None

symmetric_difference_update(other)#

Update a relation with the symmetric difference of itself and the other.

Parameters:

other (Iterable[tuple[Any, ...]]) – An iterable of elements representing the other relation

Return type:

None