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 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:
- class FinitaryRelation(*args: Iterable[Tuple[Hashable, ...]], universes: Optional[Tuple[Iterable[Hashable], ...]] = None)
The
FinitaryRelation
class implementsAbstractFinitaryRelation
.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:
args (
Iterable[Tuple[Hashable, ...]]
) – A single iterable of tuple.universes (
Tuple[Iterable[Hashable], ...]
) – Some initial values for the universes.
- add(element: Tuple[Hashable, ...]) None
Add a tuple to the relation.
- Parameters:
element (
Tuple[Hashable, ...]
) – The tuple to add.
- discard(element: Tuple[Hashable, ...]) None
Discard a tuple from the relation.
- Parameters:
element (
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:
- class FinitaryRelationFactory
The
FinitaryRelationFactory
provides a_from_iterable()
method.This method is used by builtin set classes when set operations are executed.
Notes
See https://docs.python.org/fr/3/library/collections.abc.html.
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- 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:
- abstract property sources: AbstractSet[galactic.algebras.relational._binary._E]
Get the sources of this DAG.
- Returns:
The sources of this DAG.
- Return type:
- 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:
- 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:
- 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 implementsAbstractBinaryRelation
.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]
) – An iterable of initial values for the domain.co_domain (
Iterable[_H]
) – An iterable of initial values for the co-domain.
- add(element: Tuple[galactic.algebras.relational._binary._G, galactic.algebras.relational._binary._H]) None
Add a couple to the relation.
- Parameters:
element (
Tuple[_G, _H]
) – The couple to add.
- 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:
- discard(element: Tuple[galactic.algebras.relational._binary._G, galactic.algebras.relational._binary._H]) None
Discard a tuple from the relation.
- Parameters:
element (
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:
- 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:
- 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:
- 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:
- class BinaryRelationFactory(*args, **kwds)
The
BinaryRelationFactory
provides a_from_iterable()
method.This method is used by builtin set classes when set operations are executed.
Notes
See https://docs.python.org/fr/3/library/collections.abc.html.
- 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:
- 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:
- class SagittalDiagram(relation: galactic.algebras.relational.AbstractBinaryRelation[galactic.algebras.relational._renderer._E, galactic.algebras.relational._renderer._F], 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], 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.domain_renderer (
NodeRenderer[_E, _F]
) – The domain renderer.co_domain_renderer (
NodeRenderer[_F, _E]
) – The co-domain renderer.edge_renderer (
EdgeRenderer[_E, _F]
) – The co-domain renderer.
- 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:
- 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:
- 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:
- 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:
- 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.index (
Optional[int]
) – The element index.current (bool) – is
element
the current element?successors (
Optional[AbstractSet[_F]]
) – The successors of the element.predecessors (
Optional[AbstractSet[_F]]
) – The predecessors of the element.
- Returns:
A dictionary of graphviz attributes.
- Return type:
- 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.
- Keyword Arguments:
source (
_E
) – The edge sourcedestination (
_F
) – The edge destinationsuccessors (
Optional[AbstractSet[_F]]
) – The successors of source (including current destination).predecessors (
Optional[AbstractSet[_E]]
) – The predecessors of destination (including current source).
- Returns:
A dictionary of graphviz attributes
- Return type:
- 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.domain_renderer (
CellRenderer[_E]
) – The domain renderer.co_domain_renderer (
CellRenderer[_E]
) – The co-domain renderer.
- 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:
- property domain_renderer: galactic.algebras.relational.CellRenderer[galactic.algebras.relational._renderer._E]
Get the domain renderer.
- Returns:
The domain renderer.
- Return type:
- 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:
- class CellRenderer(*args, **kwds)
The
CellRenderer
class is used to render cell elements for a binary table.