Relation
The galactic.algebras.relational
module defines types for relations.
Link
;
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 Link(*args, **kwds)
The
Link
class is an relation instance between two elements.It is a named tuple containing two fields:
source
representing the source of the link;destination
representing the destination of the link.
New in version 0.5.0.
- count(value, /)
Return number of occurrences of value.
- destination: _F
Alias for field number 1
- index(value, start=0, stop=9223372036854775807, /)
Return first index of value.
Raises ValueError if the value is not present.
- source: _E
Alias for field number 0
- 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], ...]
, optional) – Some initial values for the universes.
- add(value: Tuple[Hashable, ...]) None
Add a tuple to the relation.
- Parameters:
value (
Tuple[Hashable, ...]
) – The tuple to add.
- 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:
- 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[_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[_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: _F) AbstractSet[_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: _E) AbstractSet[_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[_E], AbstractSet[_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[_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[_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: _F) AbstractSet[_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: _E) AbstractSet[_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[_E], AbstractSet[_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[_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[_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: _F) AbstractSet[_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[_E]
Get the sinks of this DAG.
- Returns:
The sinks of this DAG.
- Return type:
- abstract property sources: AbstractSet[_E]
Get the sources of this DAG.
- Returns:
The sources of this DAG.
- Return type:
- abstract successors(element: _E) AbstractSet[_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[_E], AbstractSet[_F]]
Get the universes of this relation.
- Returns:
The universes of this relation.
- Return type:
- class BinaryRelation(*args: Iterable[Tuple[_G, _H]], domain: Optional[Iterable[_G]] = None, co_domain: Optional[Iterable[_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) [Link(source=1, destination=4), Link(source=3, destination=4)] >>> list(b.successors(1)) [4] >>> list(b.predecessors(4)) [1, 3] >>> len(b.predecessors(4)) 2
- __init__(*args: Iterable[Tuple[_G, _H]], domain: Optional[Iterable[_G]] = None, co_domain: Optional[Iterable[_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[_G, _H]) None
Add a couple to the relation.
- Parameters:
value (
Tuple[_G, _H]
) – The couple to add.
- property co_domain: MutableSet[_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(value: Tuple[_G, _H]) None
Discard a tuple from the relation.
- Parameters:
value (
Tuple[_G, _H]
) – The couple to discard.
- property domain: MutableSet[_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: _H) MutableSet[_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: _G) MutableSet[_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[_G], MutableSet[_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.
- class Selection(relation: 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: 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: 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: 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 NodeRenderer(*args, **kwds)
The
NodeRenderer
class renders graphviz nodes.Notes
- attributes(element: _E, index: Optional[int] = None, current: bool = False, successors: Optional[AbstractSet[_F]] = None, predecessors: Optional[AbstractSet[_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:
- class EdgeRenderer(*args, **kwds)
The
EdgeRenderer
class renders edge attributes.Notes
- attributes(source: _E, destination: _F, successors: Optional[AbstractSet[_F]] = None, predecessors: Optional[AbstractSet[_E]] = None) Dict[str, str]
Return a dictionary of graphviz attributes for an edge.
- Parameters:
- 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:
- class GraphRenderer(*args, **kwds)
The
GraphRenderer
class renders graph attributes.Notes
- add_destination(element: _F, index: Optional[int] = None, current: bool = False, successors: Optional[AbstractSet[_E]] = None, predecessors: Optional[AbstractSet[_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: _E, destination: _F, successors: Optional[AbstractSet[_F]] = None, predecessors: Optional[AbstractSet[_E]] = None) None
Add an edge to the graph.
- Parameters:
- 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: _E, index: Optional[int] = None, current: bool = False, successors: Optional[AbstractSet[_F]] = None, predecessors: Optional[AbstractSet[_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.
- class BinaryTable(relation: AbstractBinaryRelation[_E, _F], domain_renderer: Optional[CellRenderer[_E]] = None, co_domain_renderer: Optional[CellRenderer[_F]] = None)
The
BinaryTable
class can render binary relation in jupyter notebook.- __init__(relation: AbstractBinaryRelation[_E, _F], domain_renderer: Optional[CellRenderer[_E]] = None, co_domain_renderer: Optional[CellRenderer[_F]] = None) None
Initialise a
BinaryTable
instance.- Parameters:
relation (
AbstractBinaryRelation[_E, _F]
) – The binary relation.- Keyword Arguments:
domain_renderer (
CellRenderer[_E]
) – The domain renderer.co_domain_renderer (
CellRenderer[_E]
) – The co-domain renderer.
- property co_domain_renderer: CellRenderer[_F]
Get the co-domain renderer.
- Returns:
The co-domain renderer.
- Return type:
- property domain_renderer: CellRenderer[_E]
Get the domain renderer.
- Returns:
The domain renderer.
- Return type:
- property relation: AbstractBinaryRelation[_E, _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.
- class SagittalDiagramRenderer(*args, **kwds)
The
SagittalDiagramRenderer
class renders attributes.it is used for a sagittal diagram.
Notes
- add_destination(element: _F, index: Optional[int] = None, current: bool = False, successors: Optional[AbstractSet[_E]] = None, predecessors: Optional[AbstractSet[_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: _E, destination: _F, successors: Optional[AbstractSet[_F]] = None, predecessors: Optional[AbstractSet[_E]] = None) None
Add an edge to the graph.
- Parameters:
- 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: _E, index: Optional[int] = None, current: bool = False, successors: Optional[AbstractSet[_F]] = None, predecessors: Optional[AbstractSet[_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.
- class SagittalDiagram(relation: AbstractBinaryRelation[_E, _F], graph_renderer: Optional[SagittalDiagramRenderer[_E, _F]] = None, domain_renderer: Optional[NodeRenderer[_E, _F]] = None, co_domain_renderer: Optional[NodeRenderer[_F, _E]] = None, edge_renderer: Optional[EdgeRenderer[_E, _F]] = None)
The
SagittalDiagram
class is used for drawing Sagittal diagrams.It is useful for visualizing binary relations in jupyter notebooks.
- __init__(relation: AbstractBinaryRelation[_E, _F], graph_renderer: Optional[SagittalDiagramRenderer[_E, _F]] = None, domain_renderer: Optional[NodeRenderer[_E, _F]] = None, co_domain_renderer: Optional[NodeRenderer[_F, _E]] = None, edge_renderer: Optional[EdgeRenderer[_E, _F]] = None) None
Initialise a
SagittalDiagram
instance.- Parameters:
relation (
AbstractBinaryRelation[_E, _F]
) – The binary relation.- Keyword Arguments:
graph_renderer (
SagittalDiagramRenderer[_E, _F]
, optional) – The graph renderer.domain_renderer (
NodeRenderer[_E, _F]
, optional) – The domain renderer.co_domain_renderer (
NodeRenderer[_F, _E]
, optional) – The co-domain renderer.edge_renderer (
EdgeRenderer[_E, _F]
, optional) – The co-domain renderer.
- property co_domain_renderer: NodeRenderer[_F, _E]
Get the co-domain renderer.
- Returns:
The co-domain renderer.
- Return type:
- property domain_renderer: NodeRenderer[_E, _F]
Get the domain renderer.
- Returns:
The domain renderer.
- Return type:
- property edge_renderer: EdgeRenderer[_E, _F]
Get the edge renderer.
- Returns:
The edge renderer.
- Return type:
- property graph_renderer: GraphRenderer[_E, _F]
Get the graph renderer.
- Returns:
The graph renderer.
- Return type:
- property relation: AbstractBinaryRelation[_E, _F]
Get the relation.
- Returns:
The underlying relation.
- Return type: