Relation

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

and their implementations:

It defines two factories that have the ability to create sets for set operations:

It also defines classes for drawing diagrams into jupyter notebooks:

It also defines classes for drawing Sagittal diagrams into jupyter notebooks:

And classes for displaying binary relation into jupyter notebooks:

class AbstractFinitaryRelation(*args, **kwds)

The AbstractFinitaryRelation class represents finitary relation.

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

isdisjoint(other)

Return True if two sets have a null intersection.

abstract property universes: Tuple[AbstractSet[Any], ...]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[AbstractSet[Any], ...]

class FinitaryRelation(*args: Iterable[Tuple[Hashable, ...]], universes: Optional[Tuple[Iterable[Hashable], ...]] = None)

The FinitaryRelation class implements AbstractFinitaryRelation.

A finitary relation is a subset of the Cartesian product between domains.

Examples

>>> from galactic.algebras.relational import FinitaryRelation
>>> r = FinitaryRelation([(1, 2, 3), (2, 3, 4), (3, 4, 5)])
>>> list(r)
[(1, 2, 3), (2, 3, 4), (3, 4, 5)]
>>> r.discard((2, 3, 4))
>>> list(r)
[(1, 2, 3), (3, 4, 5)]
>>> list(r.universes[0])
[1, 2, 3]
>>> list(r.universes[1])
[2, 3, 4]
>>> list(r.universes[2])
[3, 4, 5]
>>> r.universes[0].clear()
>>> list(r)
[]
>>> list(r.universes[0])
[]
>>> list(r.universes[1])
[2, 3, 4]
>>> list(r.universes[2])
[3, 4, 5]
__init__(*args: Iterable[Tuple[Hashable, ...]], universes: Optional[Tuple[Iterable[Hashable], ...]] = None) None

Initialise a FinitaryRelation instance.

Parameters:
add(value: Tuple[Hashable, ...]) None

Add a tuple to the relation.

Parameters:

value (Tuple[Hashable, ...]) – The tuple to add.

clear() None

Clear the relation.

discard(value: Tuple[Hashable, ...]) None

Discard a tuple from the relation.

Parameters:

value (Tuple[Hashable, ...]) – The tuple to discard.

isdisjoint(other)

Return True if two sets have a null intersection.

pop()

Return the popped value. Raise KeyError if empty.

remove(value)

Remove an element. If not a member, raise a KeyError.

property universes: Tuple[AbstractSet[Hashable], ...]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[AbstractSet[Any], ...]

class FinitaryRelationFactory

The FinitaryRelationFactory provides a _from_iterable() method.

This method is used by builtin set classes when set operations are executed.

class AbstractBinaryRelation(*args, **kwds)

The AbstractBinaryRelation class represents binary relation.

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

property co_domain: AbstractSet[galactic.algebras.relational._binary._F]

Get the co-domain of this binary relation.

This is a proxy to self.universes[1].

Returns:

The co-domain of this binary relation.

Return type:

AbstractSet[_F]

property domain: AbstractSet[galactic.algebras.relational._binary._E]

Get the domain of this binary relation.

This is a proxy to self.universes[0].

Returns:

The domain of this binary relation.

Return type:

AbstractSet[_E]

isdisjoint(other)

Return True if two sets have a null intersection.

abstract predecessors(element: galactic.algebras.relational._binary._F) AbstractSet[galactic.algebras.relational._binary._E]

Get the predecessors of an element.

Parameters:

element (_F) – The element whose predecessors are requested

Returns:

The predecessors.

Return type:

AbstractSet[_E]

Raises:

ValueError – If the element does not belong to the relation.

abstract successors(element: galactic.algebras.relational._binary._E) AbstractSet[galactic.algebras.relational._binary._F]

Get the successors of an element.

Parameters:

element (_E) – The element whose successors are requested

Returns:

The successors.

Return type:

AbstractSet[_F]

Raises:

ValueError – If the element does not belong to the relation.

abstract property universes: Tuple[AbstractSet[galactic.algebras.relational._binary._E], AbstractSet[galactic.algebras.relational._binary._F]]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[AbstractSet[_E], AbstractSet[_F]]

class AbstractEndoRelation(*args, **kwds)

An AbstractEndoRelation is a binary relation from an universe to itself.

property co_domain: AbstractSet[galactic.algebras.relational._binary._F]

Get the co-domain of this binary relation.

This is a proxy to self.universes[1].

Returns:

The co-domain of this binary relation.

Return type:

AbstractSet[_F]

property domain: AbstractSet[galactic.algebras.relational._binary._E]

Get the domain of this binary relation.

This is a proxy to self.universes[0].

Returns:

The domain of this binary relation.

Return type:

AbstractSet[_E]

isdisjoint(other)

Return True if two sets have a null intersection.

abstract predecessors(element: galactic.algebras.relational._binary._F) AbstractSet[galactic.algebras.relational._binary._E]

Get the predecessors of an element.

Parameters:

element (_F) – The element whose predecessors are requested

Returns:

The predecessors.

Return type:

AbstractSet[_E]

Raises:

ValueError – If the element does not belong to the relation.

abstract successors(element: galactic.algebras.relational._binary._E) AbstractSet[galactic.algebras.relational._binary._F]

Get the successors of an element.

Parameters:

element (_E) – The element whose successors are requested

Returns:

The successors.

Return type:

AbstractSet[_F]

Raises:

ValueError – If the element does not belong to the relation.

abstract property universes: Tuple[AbstractSet[galactic.algebras.relational._binary._E], AbstractSet[galactic.algebras.relational._binary._F]]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[AbstractSet[_E], AbstractSet[_F]]

class AbstractDirectedAcyclicGraph(*args, **kwds)

The AbstractDirectedAcyclicGraph represents directed acyclic graph.

property co_domain: AbstractSet[galactic.algebras.relational._binary._F]

Get the co-domain of this binary relation.

This is a proxy to self.universes[1].

Returns:

The co-domain of this binary relation.

Return type:

AbstractSet[_F]

property domain: AbstractSet[galactic.algebras.relational._binary._E]

Get the domain of this binary relation.

This is a proxy to self.universes[0].

Returns:

The domain of this binary relation.

Return type:

AbstractSet[_E]

isdisjoint(other)

Return True if two sets have a null intersection.

abstract predecessors(element: galactic.algebras.relational._binary._F) AbstractSet[galactic.algebras.relational._binary._E]

Get the predecessors of an element.

Parameters:

element (_F) – The element whose predecessors are requested

Returns:

The predecessors.

Return type:

AbstractSet[_E]

Raises:

ValueError – If the element does not belong to the relation.

abstract property sinks: AbstractSet[galactic.algebras.relational._binary._E]

Get the sinks of this DAG.

Returns:

The sinks of this DAG.

Return type:

AbstractSet[_E]

abstract property sources: AbstractSet[galactic.algebras.relational._binary._E]

Get the sources of this DAG.

Returns:

The sources of this DAG.

Return type:

AbstractSet[_E]

abstract successors(element: galactic.algebras.relational._binary._E) AbstractSet[galactic.algebras.relational._binary._F]

Get the successors of an element.

Parameters:

element (_E) – The element whose successors are requested

Returns:

The successors.

Return type:

AbstractSet[_F]

Raises:

ValueError – If the element does not belong to the relation.

abstract property universes: Tuple[AbstractSet[galactic.algebras.relational._binary._E], AbstractSet[galactic.algebras.relational._binary._F]]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[AbstractSet[_E], AbstractSet[_F]]

class BinaryRelation(*args: Iterable[Tuple[galactic.algebras.relational._binary._G, galactic.algebras.relational._binary._H]], domain: Optional[Iterable[galactic.algebras.relational._binary._G]] = None, co_domain: Optional[Iterable[galactic.algebras.relational._binary._H]] = None)

The BinaryRelation class implements AbstractBinaryRelation.

Examples

>>> from galactic.algebras.relational import BinaryRelation
>>> b = BinaryRelation[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 |= [(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
__init__(*args: Iterable[Tuple[galactic.algebras.relational._binary._G, galactic.algebras.relational._binary._H]], domain: Optional[Iterable[galactic.algebras.relational._binary._G]] = None, co_domain: Optional[Iterable[galactic.algebras.relational._binary._H]] = None) None

Initialise a BinaryRelation instance.

Parameters:
  • args (Iterable[Tuple[_G, _H]]) – A single iterable of couple.

  • domain (Iterable[_G], optional) – An iterable of initial values for the domain.

  • co_domain (Iterable[_H], optional) – An iterable of initial values for the co-domain.

add(value: Tuple[galactic.algebras.relational._binary._G, galactic.algebras.relational._binary._H]) None

Add a couple to the relation.

Parameters:

value (Tuple[_G, _H]) – The couple to add.

clear() None

Clear the relation.

property co_domain: MutableSet[galactic.algebras.relational._binary._H]

Get the co-domain of this binary relation.

This is a proxy to self.universes[1].

Returns:

The domain of this binary relation.

Return type:

MutableSet[_H]

discard(value: Tuple[galactic.algebras.relational._binary._G, galactic.algebras.relational._binary._H]) None

Discard a tuple from the relation.

Parameters:

value (Tuple[_G, _H]) – The couple to discard.

property domain: MutableSet[galactic.algebras.relational._binary._G]

Get the domain of this binary relation.

This is a proxy to self.universes[0].

Returns:

The domain of this binary relation.

Return type:

MutableSet[_G]

isdisjoint(other)

Return True if two sets have a null intersection.

pop()

Return the popped value. Raise KeyError if empty.

predecessors(element: galactic.algebras.relational._binary._H) MutableSet[galactic.algebras.relational._binary._G]

Get the predecessors of an element.

Parameters:

element (_H) – The element whose predecessors are requested

Returns:

The predecessors.

Return type:

MutableSet[_G]

Raises:

ValueError – If the element does not belong to the relation.

remove(value)

Remove an element. If not a member, raise a KeyError.

successors(element: galactic.algebras.relational._binary._G) MutableSet[galactic.algebras.relational._binary._H]

Get the successors of an element.

Parameters:

element (_G) – The element whose successors are requested

Returns:

The successors.

Return type:

MutableSet[_H]

Raises:

ValueError – If the element does not belong to the relation.

property universes: Tuple[MutableSet[galactic.algebras.relational._binary._G], MutableSet[galactic.algebras.relational._binary._H]]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[MutableSet[_G], MutableSet[_H]]

class BinaryRelationFactory(*args, **kwds)

The BinaryRelationFactory provides a _from_iterable() method.

This method is used by builtin set classes when set operations are executed.

class Selection(relation: galactic.algebras.relational.AbstractFinitaryRelation, criteria: Mapping[int, Any])

Select some tuple from a relation using criteria.

Examples

>>> from galactic.algebras.relational import FinitaryRelation, Selection
>>> r = FinitaryRelation([(1, 2, 3), (2, 3, 3), (1, 4, 5)])
>>> s = Selection(r, {2: 3})
>>> list(s)
[(1, 2, 3), (2, 3, 3)]
__init__(relation: galactic.algebras.relational.AbstractFinitaryRelation, criteria: Mapping[int, Any]) None

Select some tuple from the relation using criteria.

Parameters:

criteria (Mapping[int, Any]) – A set of criterion

isdisjoint(other)

Return True if two sets have a null intersection.

property universes: Tuple[AbstractSet[Any], ...]

Get the universe of this finitary relation.

Returns:

The universes.

Return type:

Tuple[AbstractSet[Any], ...]

class Projection(relation: galactic.algebras.relational.AbstractFinitaryRelation, indexes: Iterable[int])

Project a relation.

Examples

>>> from galactic.algebras.relational import FinitaryRelation, Projection
>>> r = FinitaryRelation([(1, 2, 3), (2, 3, 3), (1, 4, 5)])
>>> p = Projection(r, [1, 2])
>>> list(p)
[(2, 3), (3, 3), (4, 5)]
__init__(relation: galactic.algebras.relational.AbstractFinitaryRelation, indexes: Iterable[int]) None

Project a relation.

Parameters:

indexes (Iterable[int]) – An iterable of indexes.

isdisjoint(other)

Return True if two sets have a null intersection.

property universes: Tuple[AbstractSet[Any], ...]

Get the universe of this finitary relation.

Returns:

The universes.

Return type:

Tuple[AbstractSet[Any], ...]

class NodeRenderer(*args, **kwds)

The NodeRenderer class renders graphviz nodes.

Notes

See http://www.graphviz.org/doc/info/attrs.html.

attributes(element: galactic.algebras.relational._renderer._E, index: Optional[int] = None, current: bool = False, successors: Optional[AbstractSet[galactic.algebras.relational._renderer._F]] = None, predecessors: Optional[AbstractSet[galactic.algebras.relational._renderer._F]] = None) Dict[str, str]

Return a dictionary of graphviz attributes for a node.

Parameters:

element (_E) – The element to render.

Keyword Arguments:
  • index (int, optional) – The element index.

  • current (bool) – is element the current element?

  • successors (AbstractSet[_F], optional) – The successors of the element.

  • predecessors (AbstractSet[_F], optional) – The predecessors of the element.

Returns:

A dictionary of graphviz attributes.

Return type:

Dict[str, str]

class EdgeRenderer(*args, **kwds)

The EdgeRenderer class renders edge attributes.

Notes

See http://www.graphviz.org/doc/info/attrs.html.

attributes(source: galactic.algebras.relational._renderer._E, destination: galactic.algebras.relational._renderer._F, successors: Optional[AbstractSet[galactic.algebras.relational._renderer._F]] = None, predecessors: Optional[AbstractSet[galactic.algebras.relational._renderer._E]] = None) Dict[str, str]

Return a dictionary of graphviz attributes for an edge.

Parameters:
  • source (_E) – The edge source

  • destination (_F) – The edge destination

Keyword Arguments:
  • successors (AbstractSet[_F], optional) – The successors of source (including current destination).

  • predecessors (AbstractSet[_E], optional) – The predecessors of destination (including current source).

Returns:

A dictionary of graphviz attributes

Return type:

Dict[str, str]

class GraphRenderer(*args, **kwds)

The GraphRenderer class renders graph attributes.

Notes

See http://www.graphviz.org/doc/info/attrs.html.

add_destination(element: galactic.algebras.relational._renderer._F, index: Optional[int] = None, current: bool = False, successors: Optional[AbstractSet[galactic.algebras.relational._renderer._E]] = None, predecessors: Optional[AbstractSet[galactic.algebras.relational._renderer._E]] = None) None

Add a destination to the graph.

Parameters:

element (_F) – The destination to add.

Keyword Arguments:
  • index (int, optional) – The destination index.

  • current (bool) – is element the current element?

  • successors (AbstractSet[_E], optional) – The successors of the destination.

  • predecessors (AbstractSet[_E], optional) – The predecessors of the destination.

add_edge(source: galactic.algebras.relational._renderer._E, destination: galactic.algebras.relational._renderer._F, successors: Optional[AbstractSet[galactic.algebras.relational._renderer._F]] = None, predecessors: Optional[AbstractSet[galactic.algebras.relational._renderer._E]] = None) None

Add an edge to the graph.

Parameters:
  • source (_E) – The edge source

  • destination (_F) – The edge destination

Keyword Arguments:
  • successors (AbstractSet[_F], optional) – The successors of source (including current destination).

  • predecessors (AbstractSet[_E], optional) – The predecessors of destination (including current source).

add_source(element: galactic.algebras.relational._renderer._E, index: Optional[int] = None, current: bool = False, successors: Optional[AbstractSet[galactic.algebras.relational._renderer._F]] = None, predecessors: Optional[AbstractSet[galactic.algebras.relational._renderer._F]] = None) None

Add a source to the graph.

Parameters:

element (_E) – The source to add.

Keyword Arguments:
  • index (int, optional) – The source index.

  • current (bool) – is element the current element?

  • successors (AbstractSet[_F], optional) – The successors of the source.

  • predecessors (AbstractSet[_F], optional) – The predecessors of the source.

attributes() Dict[str, str]

Return a dictionary of graphviz attributes for a graph.

Returns:

A dictionary of graphviz attributes

Return type:

Dict[str, str]

class BinaryTable(relation: galactic.algebras.relational.AbstractBinaryRelation[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F], domain_renderer: Optional[galactic.algebras.relational.CellRenderer[galactic.algebras.relational._renderer._E]] = None, co_domain_renderer: Optional[galactic.algebras.relational.CellRenderer[galactic.algebras.relational._renderer._F]] = None)

The BinaryTable class can render binary relation in jupyter notebook.

__init__(relation: galactic.algebras.relational.AbstractBinaryRelation[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F], domain_renderer: Optional[galactic.algebras.relational.CellRenderer[galactic.algebras.relational._renderer._E]] = None, co_domain_renderer: Optional[galactic.algebras.relational.CellRenderer[galactic.algebras.relational._renderer._F]] = None) None

Initialise a BinaryTable instance.

Parameters:

relation (AbstractBinaryRelation[_E, _F]) – The binary relation.

Keyword Arguments:
property co_domain_renderer: galactic.algebras.relational.CellRenderer[galactic.algebras.relational._renderer._F]

Get the co-domain renderer.

Returns:

The co-domain renderer.

Return type:

CellRenderer[_E]

property domain_renderer: galactic.algebras.relational.CellRenderer[galactic.algebras.relational._renderer._E]

Get the domain renderer.

Returns:

The domain renderer.

Return type:

CellRenderer[_E]

property relation: galactic.algebras.relational.AbstractBinaryRelation[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F]

Get the relation.

Returns:

The underlying relation.

Return type:

AbstractBinaryRelation[_E, _F]

class CellRenderer(*args, **kwds)

The CellRenderer class is used to render cell elements for a binary table.

render(element: galactic.algebras.relational._renderer._E, index: int) str

Render an element.

Keyword Arguments:
  • element (_E) – The element to render

  • index (int) – The element index.

Returns:

The string representation of the element into a table.

Return type:

str

class SagittalDiagramRenderer(*args, **kwds)

The SagittalDiagramRenderer class renders attributes.

it is used for a sagittal diagram.

Notes

See http://www.graphviz.org/doc/info/attrs.html.

add_destination(element: galactic.algebras.relational._renderer._F, index: Optional[int] = None, current: bool = False, successors: Optional[AbstractSet[galactic.algebras.relational._renderer._E]] = None, predecessors: Optional[AbstractSet[galactic.algebras.relational._renderer._E]] = None) None

Add a destination to the graph.

Parameters:

element (_F) – The destination to add.

Keyword Arguments:
  • index (int, optional) – The destination index.

  • current (bool) – is element the current element?

  • successors (AbstractSet[_E], optional) – The successors of the destination.

  • predecessors (AbstractSet[_E], optional) – The predecessors of the destination.

add_edge(source: galactic.algebras.relational._renderer._E, destination: galactic.algebras.relational._renderer._F, successors: Optional[AbstractSet[galactic.algebras.relational._renderer._F]] = None, predecessors: Optional[AbstractSet[galactic.algebras.relational._renderer._E]] = None) None

Add an edge to the graph.

Parameters:
  • source (_E) – The edge source

  • destination (_F) – The edge destination

Keyword Arguments:
  • successors (AbstractSet[_F], optional) – The successors of source (including current destination).

  • predecessors (AbstractSet[_E], optional) – The predecessors of destination (including current source).

add_source(element: galactic.algebras.relational._renderer._E, index: Optional[int] = None, current: bool = False, successors: Optional[AbstractSet[galactic.algebras.relational._renderer._F]] = None, predecessors: Optional[AbstractSet[galactic.algebras.relational._renderer._F]] = None) None

Add a source to the graph.

Parameters:

element (_E) – The source to add.

Keyword Arguments:
  • index (int, optional) – The source index.

  • current (bool) – is element the current element?

  • successors (AbstractSet[_F], optional) – The successors of the source.

  • predecessors (AbstractSet[_F], optional) – The predecessors of the source.

attributes() Dict[str, str]

Return a dictionary of graphviz attributes for the graph.

class SagittalDiagram(relation: galactic.algebras.relational.AbstractBinaryRelation[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F], graph_renderer: Optional[galactic.algebras.relational.SagittalDiagramRenderer[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F]] = None, domain_renderer: Optional[galactic.algebras.relational.NodeRenderer[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F]] = None, co_domain_renderer: Optional[galactic.algebras.relational.NodeRenderer[galactic.algebras.relational._renderer._F, galactic.algebras.relational._renderer._E]] = None, edge_renderer: Optional[galactic.algebras.relational.EdgeRenderer[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F]] = None)

The SagittalDiagram class is used for drawing Sagittal diagrams.

It is useful for visualizing binary relations in jupyter notebooks.

__init__(relation: galactic.algebras.relational.AbstractBinaryRelation[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F], graph_renderer: Optional[galactic.algebras.relational.SagittalDiagramRenderer[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F]] = None, domain_renderer: Optional[galactic.algebras.relational.NodeRenderer[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F]] = None, co_domain_renderer: Optional[galactic.algebras.relational.NodeRenderer[galactic.algebras.relational._renderer._F, galactic.algebras.relational._renderer._E]] = None, edge_renderer: Optional[galactic.algebras.relational.EdgeRenderer[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F]] = None) None

Initialise a SagittalDiagram instance.

Parameters:

relation (AbstractBinaryRelation[_E, _F]) – The binary relation.

Keyword Arguments:
property co_domain_renderer: galactic.algebras.relational.NodeRenderer[galactic.algebras.relational._renderer._F, galactic.algebras.relational._renderer._E]

Get the co-domain renderer.

Returns:

The co-domain renderer.

Return type:

NodeRenderer[_F, _E]

property domain_renderer: galactic.algebras.relational.NodeRenderer[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F]

Get the domain renderer.

Returns:

The domain renderer.

Return type:

NodeRenderer[_E, _F]

property edge_renderer: galactic.algebras.relational.EdgeRenderer[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F]

Get the edge renderer.

Returns:

The edge renderer.

Return type:

EdgeRenderer[_E, _F]

property graph_renderer: galactic.algebras.relational.GraphRenderer[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F]

Get the graph renderer.

Returns:

The graph renderer.

Return type:

GraphRenderer[_E, _F]

property relation: galactic.algebras.relational.AbstractBinaryRelation[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F]

Get the relation.

Returns:

The underlying relation.

Return type:

AbstractBinaryRelation[_E, _F]

property source: str

Get the graphviz source for this partially ordered set.

Returns:

The graphviz output for this partially ordered set.

Return type:

str