Relation

The galactic.algebras.relational module defines types for relations.

and their implementations:

Mixins are made public for sub-classes:

class AbstractRelation(*args, **kwargs)

Bases: Container[tuple[Any, …]], Protocol

It represents finitary relation.

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

abstract property universes: tuple[Container[Any], ...]

Get the universes of this relation.

Return type:

The universes of this 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:

NotImplementedError

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:

NotImplementedError

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:

NotImplementedError

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:

NotImplementedError

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:

NotImplementedError

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:

NotImplementedError

abstract property universes: tuple[Container[_E], Container[_F]]

Get the universes of this relation.

Return type:

The universes of this relation.

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:

NotImplementedError

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:

NotImplementedError

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:

NotImplementedError

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:

NotImplementedError

abstract property universes: tuple[Container[_E], Container[_F]]

Get the universes of this relation.

Return type:

The universes of this relation.

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:

NotImplementedError

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:

NotImplementedError

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.

Parameters:

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

Returns:

The difference between 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.

Returns:

The intersection between the relation and the others

Return type:

Self

isdisjoint(other)

Test if the relation is disjoint from the other.

Parameters:

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

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

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

Returns:

True if the relation is a superset of the other.

Return type:

bool

symmetric_difference(other)

Compute the symmetric difference between the relation and the other.

Parameters:

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

Returns:

The symmetric difference between the relation and the other.

Return type:

Self

union(*others)

Compute the union of the relation and the others.

Parameters:

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

Returns:

The union of the relation and the others.

Return type:

Self

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.

Parameters:

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

Return type:

None

clear()

Clear the relation.

Return type:

None

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.

Parameters:

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

Returns:

The difference between the relation and the others

Return type:

Self

difference_update(*others)

Update a relation with the difference of itself and others.

Parameters:

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

Return type:

None

discard(element)

Discard a tuple from the relation.

Parameters:

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

Return type:

None

extend(iterable)

Extend the relation.

Parameters:

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

Return type:

None

intersection(*others)

Compute the intersection between the relation and the others.

Parameters:

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

Returns:

The intersection between the relation and the others

Return type:

Self

intersection_update(*others)

Update a relation with the intersection of itself and others.

Parameters:

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

Return type:

None

isdisjoint(other)

Test if the relation is disjoint from the other.

Parameters:

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

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

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

Returns:

True if the relation is a superset of the other.

Return type:

bool

pop()

Remove and return an arbitrary element from the relation.

Return type:

tuple[Any, ...]

Returns:

An arbitrary element from the relation.

remove(element)

Remove element from the relation if it is present.

Parameters:

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

Return type:

None

symmetric_difference(other)

Compute the symmetric difference between the relation and the other.

Parameters:

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

Returns:

The symmetric difference between the relation and the other.

Return type:

Self

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.

Return type:

None

union(*others)

Compute the union of the relation and the others.

Parameters:

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

Returns:

The union of the relation and the others.

Return type:

Self

update(*others)

Update a relation with the union of itself and others.

Parameters:

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

Return type:

None

property universes: tuple[MutableCollection[Any], ...]

Get the universes of this relation.

Return type:

The universes of this relation.

class FiniteSelection(relation, criteria)

Bases: FiniteRelationMixin

Select some tuple from a relation using criteria.

Parameters:

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.

Parameters:

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

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

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

Returns:

True if the relation is a superset of the other.

Return type:

bool

class FiniteProjection(relation, indexes)

Bases: FiniteRelationMixin

Project a relation.

Parameters:

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.

Parameters:

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

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

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

Returns:

True if the relation is a superset of the other.

Return type:

bool

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.

Parameters:

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

Returns:

The difference between 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.

Returns:

The intersection between the relation and the others

Return type:

Self

isdisjoint(other)

Test if the relation is disjoint from the other.

Parameters:

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

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

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

Returns:

True if the relation is a superset of the other.

Return type:

bool

symmetric_difference(other)

Compute the symmetric difference between the relation and the other.

Parameters:

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

Returns:

The symmetric difference between the relation and the other.

Return type:

Self

union(*others)

Compute the union of the relation and the others.

Parameters:

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

Returns:

The union of the relation and the others.

Return type:

Self

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.

Parameters:

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

Return type:

None

clear()

Clear the relation.

Return type:

None

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.

Parameters:

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

Returns:

The difference between the relation and the others

Return type:

Self

difference_update(*others)

Update a relation with the difference of itself and others.

Parameters:

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

Return type:

None

discard(element)

Discard a tuple from the relation.

Parameters:

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

Return type:

None

extend(iterable)

Extend the relation.

Parameters:

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

Return type:

None

intersection(*others)

Compute the intersection between the relation and the others.

Parameters:

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

Returns:

The intersection between the relation and the others

Return type:

Self

intersection_update(*others)

Update a relation with the intersection of itself and others.

Parameters:

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

Return type:

None

isdisjoint(other)

Test if the relation is disjoint from the other.

Parameters:

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

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

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

Returns:

True if the relation is a superset of the other.

Return type:

bool

pop()

Remove and return an arbitrary element from the relation.

Return type:

tuple[Any, ...]

Returns:

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.

Parameters:

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

Return type:

None

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.

Parameters:

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

Returns:

The symmetric difference between the relation and the other.

Return type:

Self

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.

Return type:

None

union(*others)

Compute the union of the relation and the others.

Parameters:

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

Returns:

The union of the relation and the others.

Return type:

Self

update(*others)

Update a relation with the union of itself and others.

Parameters:

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

Return type:

None

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.

Parameters:

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

Returns:

The difference between the relation and the others

Return type:

Self

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.

Parameters:

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

Returns:

The intersection between the relation and the others

Return type:

Self

isdisjoint(other)

Test if the relation is disjoint from the other.

Parameters:

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

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

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

Returns:

True if the relation is a superset of the other.

Return type:

bool

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.

Parameters:

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

Returns:

The symmetric difference between the relation and the other.

Return type:

Self

union(*others)

Compute the union of the relation and the others.

Parameters:

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

Returns:

The union of the relation and the others.

Return type:

Self

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.

Parameters:

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

Return type:

None

clear()

Clear the relation.

Return type:

None

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.

Parameters:

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

Returns:

The difference between the relation and the others

Return type:

Self

difference_update(*others)

Update a relation with the difference of itself and others.

Parameters:

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

Return type:

None

discard(element)

Discard a tuple from the relation.

Parameters:

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

Return type:

None

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.

Parameters:

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

Return type:

None

intersection(*others)

Compute the intersection between the relation and the others.

Parameters:

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

Returns:

The intersection between the relation and the others

Return type:

Self

intersection_update(*others)

Update a relation with the intersection of itself and others.

Parameters:

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

Return type:

None

isdisjoint(other)

Test if the relation is disjoint from the other.

Parameters:

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

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

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

Returns:

True if the relation is a superset of the other.

Return type:

bool

pop()

Remove and return an arbitrary element from the relation.

Return type:

tuple[Any, ...]

Returns:

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.

Parameters:

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

Return type:

None

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.

Parameters:

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

Returns:

The symmetric difference between the relation and the other.

Return type:

Self

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.

Return type:

None

union(*others)

Compute the union of the relation and the others.

Parameters:

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

Returns:

The union of the relation and the others.

Return type:

Self

property universes: tuple[MutableCollection[_E], MutableCollection[_F]]

Get the universes of this relation.

Return type:

The universes of this relation.

update(*others)

Update a relation with the union of itself and others.

Parameters:

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

Return type:

None

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.

Parameters:

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

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

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

Returns:

True if the relation is a superset of the other.

Return type:

bool

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:

NotImplementedError

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.

Parameters:

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

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.

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.

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.

Returns:

The symmetric difference between the relation and the other.

Return type:

Self

isdisjoint(other)

Test if the relation is disjoint from the other.

Parameters:

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

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

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

Returns:

True if the relation is a superset of the other.

Return type:

bool

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.

Parameters:

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

Return type:

None

remove(element)

Remove element from the relation if it is present.

Parameters:

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

Return type:

None

discard(element)

Discard a tuple from the relation.

Parameters:

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

Return type:

None

pop()

Remove and return an arbitrary element from the relation.

Return type:

tuple[Any, ...]

Returns:

An arbitrary element from the relation.

clear()

Clear the relation.

Return type:

None

extend(iterable)

Extend the relation.

Parameters:

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

Return type:

None

update(*others)

Update a relation with the union of itself and others.

Parameters:

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

Return type:

None

intersection_update(*others)

Update a relation with the intersection of itself and others.

Parameters:

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

Return type:

None

difference_update(*others)

Update a relation with the difference of itself and others.

Parameters:

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

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.

Return type:

None

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.

Parameters:

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

Returns:

The difference between 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.

Returns:

The intersection between the relation and the others

Return type:

Self

isdisjoint(other)

Test if the relation is disjoint from the other.

Parameters:

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

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

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

Returns:

True if the relation is a superset of the other.

Return type:

bool

symmetric_difference(other)

Compute the symmetric difference between the relation and the other.

Parameters:

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

Returns:

The symmetric difference between the relation and the other.

Return type:

Self

union(*others)

Compute the union of the relation and the others.

Parameters:

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

Returns:

The union of the relation and the others.

Return type:

Self

abstract property universes: tuple[Collection[Any], ...]

Get the universes of this relation.

Return type:

The universes of this relation.