Posets

The galactic.algebras.poset module defines types for representing posets.

their implementations:

and their elements which are partially comparable:

with their functions:

It also defines the HasseDiagram and HasseDiagramRenderer classes for drawing Hasse diagram of partially ordered sets into jupyter notebooks.

class Neighbourhood(element: _P, successors: AbstractSet[_P], predecessors: AbstractSet[_P])

The Neighbourhood class.

It associates an element with its direct successors and predecessors. It is a dataclass containing three fields:

  • element the element whose neighbourhood is considered;

  • successors the immediate successors of element;

  • predecessors the immediate predecessors of element;

class AbstractFiniteCoveringRelation

AbstractFiniteCoveringRelation represents the covering 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:

AbstractSet[_F]

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:

AbstractSet[_E]

isdisjoint(other)

Return True if two sets have a null intersection.

abstract neighbourhoods() Iterable[Neighbourhood[_P]]

Get an iterator over the neighbourhoods of an element.

Returns:

An iterable giving a triple (element, successors, predecessors) for each element in the covering relation.

Return type:

Iterable[Neighbourhood[_P]]

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:

AbstractSet[_E]

Raises:

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

abstract property sinks: AbstractSet[_P]

Get the elements with no successors.

Returns:

The elements with no successors.

Return type:

AbstractSet[_P]

abstract property sources: AbstractSet[_P]

Get the elements with no predecessors.

Returns:

The elements with no predecessors.

Return type:

AbstractSet[_P]

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:

AbstractSet[_F]

Raises:

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

abstract property universes: Tuple[AbstractSet[_P], AbstractSet[_P]]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[AbstractSet[_P], AbstractSet[_P]]

class AbstractFiniteReversibleCoveringRelation

The AbstractFiniteReversibleCoveringRelation class.

It represents a reversible version of the covering relation of a poset.

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:

AbstractSet[_F]

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:

AbstractSet[_E]

isdisjoint(other)

Return True if two sets have a null intersection.

abstract neighbourhoods() Reversible[Neighbourhood[_P]]

Get an iterator over the neighbourhoods of an element.

Returns:

An iterable giving a triple (element, successors, predecessors) for each element in the covering relation.

Return type:

Reversible[Neighbourhood[_P]]

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:

AbstractSet[_E]

Raises:

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

abstract property sinks: AbstractSet[_P]

Get the elements with no successors.

Returns:

The elements with no successors.

Return type:

AbstractSet[_P]

abstract property sources: AbstractSet[_P]

Get the elements with no predecessors.

Returns:

The elements with no predecessors.

Return type:

AbstractSet[_P]

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:

AbstractSet[_F]

Raises:

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

abstract property universes: Tuple[AbstractSet[_P], AbstractSet[_P]]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[AbstractSet[_P], AbstractSet[_P]]

class AbstractFinitePartiallyOrderedSet

The :class`AbstractFinitePartiallyOrderedSet` abstract class.

A partially ordered set is an endo-relation over a set of PartiallyComparable elements.

__contains__(item: object) bool

Get the membership of an element.

Parameters:

item (object) – The element whose membership is requested.

Returns:

True if the item belongs to the partially ordered set.

Return type:

bool

__iter__() Iterator[Tuple[_P, _P]]

Get an iterator over the elements.

Yields:

Tuple[_P, _P] – A couple of the relation.

abstract property bottom: AbstractSet[_P]

Get the bottom elements.

Returns:

The bottom elements.

Return type:

AbstractSet[_P]

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:

AbstractSet[_F]

abstract property cover: AbstractFiniteCoveringRelation[_P]

Get the covering relation.

Returns:

The covering relation.

Return type:

AbstractFiniteCoveringRelation[_P]

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:

AbstractSet[_E]

abstract filter(element: _P) AbstractFiniteLowerBoundedSet[_P]

Get a filter of a poset.

Parameters:

element (_P) – The lower limit

Returns:

The lower bounded poset.

Return type:

AbstractFiniteLowerBoundedSet[_P]

Raises:

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

abstract ideal(element: _P) AbstractFiniteUpperBoundedSet[_P]

Get an ideal of the poset.

Parameters:

element (_P) – The upper limit

Returns:

The upper bounded set.

Return type:

AbstractFiniteUpperBoundedSet[_P]

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:

AbstractSet[_E]

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:

AbstractSet[_E]

abstract property sources: AbstractSet[_E]

Get the sources of this DAG.

Returns:

The sources of this DAG.

Return type:

AbstractSet[_E]

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:

AbstractSet[_F]

Raises:

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

abstract property top: AbstractSet[_P]

Get the top elements.

Returns:

The top elements.

Return type:

AbstractSet[_P]

abstract property universes: Tuple[AbstractSet[_P], AbstractSet[_P]]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[AbstractSet[_P], AbstractSet[_P]]

class AbstractFiniteUpperBoundedSet

AbstractFiniteUpperBoundedSet represents upper bounded posets.

__contains__(item: object) bool

Get the membership of an element.

Parameters:

item (object) – The element whose membership is requested.

Returns:

True if the item belongs to the partially ordered set.

Return type:

bool

__iter__() Iterator[Tuple[_P, _P]]

Get an iterator over the elements.

Yields:

Tuple[_P, _P] – A couple of the relation.

abstract property bottom: AbstractSet[_P]

Get the bottom elements.

Returns:

The bottom elements.

Return type:

AbstractSet[_P]

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:

AbstractSet[_F]

abstract property cover: AbstractFiniteCoveringRelation[_P]

Get the covering relation.

Returns:

The covering relation.

Return type:

AbstractFiniteCoveringRelation[_P]

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:

AbstractSet[_E]

abstract filter(element: _P) AbstractFiniteLowerBoundedSet[_P]

Get a filter of a poset.

Parameters:

element (_P) – The lower limit

Returns:

The lower bounded poset.

Return type:

AbstractFiniteLowerBoundedSet[_P]

Raises:

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

abstract ideal(element: _P) AbstractFiniteUpperBoundedSet[_P]

Get an ideal of the poset.

Parameters:

element (_P) – The upper limit

Returns:

The upper bounded set.

Return type:

AbstractFiniteUpperBoundedSet[_P]

isdisjoint(other)

Return True if two sets have a null intersection.

abstract property maximum: _P

Get the maximum of the poset.

Returns:

The maximum element.

Return type:

_P

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:

AbstractSet[_E]

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:

AbstractSet[_E]

abstract property sources: AbstractSet[_E]

Get the sources of this DAG.

Returns:

The sources of this DAG.

Return type:

AbstractSet[_E]

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:

AbstractSet[_F]

Raises:

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

abstract property top: AbstractSet[_P]

Get the top elements.

Returns:

The top elements.

Return type:

AbstractSet[_P]

abstract property universes: Tuple[AbstractSet[_P], AbstractSet[_P]]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[AbstractSet[_P], AbstractSet[_P]]

class AbstractFiniteLowerBoundedSet

AbstractFiniteLowerBoundedSet represents lower bounded posets.

__contains__(item: object) bool

Get the membership of an element.

Parameters:

item (object) – The element whose membership is requested.

Returns:

True if the item belongs to the partially ordered set.

Return type:

bool

__iter__() Iterator[Tuple[_P, _P]]

Get an iterator over the elements.

Yields:

Tuple[_P, _P] – A couple of the relation.

abstract property bottom: AbstractSet[_P]

Get the bottom elements.

Returns:

The bottom elements.

Return type:

AbstractSet[_P]

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:

AbstractSet[_F]

abstract property cover: AbstractFiniteCoveringRelation[_P]

Get the covering relation.

Returns:

The covering relation.

Return type:

AbstractFiniteCoveringRelation[_P]

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:

AbstractSet[_E]

abstract filter(element: _P) AbstractFiniteLowerBoundedSet[_P]

Get a filter of a poset.

Parameters:

element (_P) – The lower limit

Returns:

The lower bounded poset.

Return type:

AbstractFiniteLowerBoundedSet[_P]

Raises:

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

abstract ideal(element: _P) AbstractFiniteUpperBoundedSet[_P]

Get an ideal of the poset.

Parameters:

element (_P) – The upper limit

Returns:

The upper bounded set.

Return type:

AbstractFiniteUpperBoundedSet[_P]

isdisjoint(other)

Return True if two sets have a null intersection.

abstract property minimum: _P

Get the minimum of the poset.

Returns:

The minimum element.

Return type:

_P

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:

AbstractSet[_E]

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:

AbstractSet[_E]

abstract property sources: AbstractSet[_E]

Get the sources of this DAG.

Returns:

The sources of this DAG.

Return type:

AbstractSet[_E]

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:

AbstractSet[_F]

Raises:

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

abstract property top: AbstractSet[_P]

Get the top elements.

Returns:

The top elements.

Return type:

AbstractSet[_P]

abstract property universes: Tuple[AbstractSet[_P], AbstractSet[_P]]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[AbstractSet[_P], AbstractSet[_P]]

class AbstractFiniteBoundedSet

AbstractFiniteBoundedSet represents bounded posets.

__contains__(item: object) bool

Get the membership of an element.

Parameters:

item (object) – The element whose membership is requested.

Returns:

True if the item belongs to the partially ordered set.

Return type:

bool

__iter__() Iterator[Tuple[_P, _P]]

Get an iterator over the elements.

Yields:

Tuple[_P, _P] – A couple of the relation.

abstract property bottom: AbstractSet[_P]

Get the bottom elements.

Returns:

The bottom elements.

Return type:

AbstractSet[_P]

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:

AbstractSet[_F]

abstract property cover: AbstractFiniteCoveringRelation[_P]

Get the covering relation.

Returns:

The covering relation.

Return type:

AbstractFiniteCoveringRelation[_P]

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:

AbstractSet[_E]

abstract filter(element: _P) AbstractFiniteLowerBoundedSet[_P]

Get a filter of a poset.

Parameters:

element (_P) – The lower limit

Returns:

The lower bounded poset.

Return type:

AbstractFiniteLowerBoundedSet[_P]

Raises:

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

abstract ideal(element: _P) AbstractFiniteUpperBoundedSet[_P]

Get an ideal of the poset.

Parameters:

element (_P) – The upper limit

Returns:

The upper bounded set.

Return type:

AbstractFiniteUpperBoundedSet[_P]

isdisjoint(other)

Return True if two sets have a null intersection.

abstract property maximum: _P

Get the maximum of the poset.

Returns:

The maximum element.

Return type:

_P

abstract property minimum: _P

Get the minimum of the poset.

Returns:

The minimum element.

Return type:

_P

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:

AbstractSet[_E]

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:

AbstractSet[_E]

abstract property sources: AbstractSet[_E]

Get the sources of this DAG.

Returns:

The sources of this DAG.

Return type:

AbstractSet[_E]

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:

AbstractSet[_F]

Raises:

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

abstract property top: AbstractSet[_P]

Get the top elements.

Returns:

The top elements.

Return type:

AbstractSet[_P]

abstract property universes: Tuple[AbstractSet[_P], AbstractSet[_P]]

Get the universes of this relation.

Returns:

The universes of this relation.

Return type:

Tuple[AbstractSet[_P], AbstractSet[_P]]

class PartiallyComparable

Partially comparable elements.

A partially ordered type sets for each pair of elements \(a\), \(b\) either:

  • \(a \leq b\)

  • \(b \leq a\)

  • a and b are incomparable (\(a \| b\))

The relation \(\leq\) must be:

  • reflexive (\(a \leq a\))

  • transitive (\(a \leq b\) and \(b \leq c\) implies \(a \leq c\))

  • anti-symmetric (\(a \leq b\) and \(b \leq a\) implies \(a = b\))

A class implementing the PartiallyComparable abstract class must be declared by inheriting from PartiallyComparable and must implement the methods:

Notes

The builtin classes

are considered as subclasses of PartiallyComparable.

Example

Let the integers ordered by the relation \(a \leq b\): is \(a\) a divisor of \(b\)?

implemented by the PrimeFactors class:

>>> from galactic.algebras.examples.arithmetic import PrimeFactors
>>> PrimeFactors(5) <= PrimeFactors(10)  # 5 is a divisor of 10
True
>>> PrimeFactors(5) <= PrimeFactors(11)  # 5 is not a divisor of 11
False
>>> PrimeFactors(5) >= PrimeFactors(11)  # 5 is not a multiple of 11
False
abstract __eq__(other: object) bool

Test if this element is equal to the other.

Parameters:

other (object) – the other element

Returns:

  • True – if this element is equal to the other.

  • NotImplemented – if the operation is not implemented between the two objects

__ge__(other: object) bool

Test if this element is greater than or equal to the other.

Parameters:

other (object) – the other element

Returns:

  • True – if this element is greater than or equal to the other.

  • NotImplemented – if the operation is not implemented between the two objects

abstract __gt__(other: object) bool

Test if this element is greater than the other.

Parameters:

other (object) – the other element

Returns:

  • True – if this element is greater than the other.

  • NotImplemented – if the operation is not implemented between the two objects

__le__(other: object) bool

Test if this element is lesser than or equal to the other.

Parameters:

other (object) – the other element

Returns:

  • True – if this element is lesser than or equal to the other.

  • NotImplemented – if the operation is not implemented between the two objects

abstract __lt__(other: object) bool

Test if this element is lesser than the other.

Parameters:

other (object) – the other element

Returns:

  • True – if this element is lesser than the other.

  • NotImplemented – if the operation is not implemented between the two objects

bottom(iterable: Iterable[_P]) Iterator[_P]

Compute an iterator over the bottom elements of the iterable.

This operation computes in \(O(n^2)\) where \(n\) is the number of elements in the iterable.

Parameters:

iterable (Iterable[_P]) – An iterable of partially ordered elements.

Returns:

An iterator over the bottom elements.

Return type:

Iterator[_P]

Example

>>> from galactic.algebras.poset import top
>>> values = [{1, 2, 3}, {1, 2, 3, 4}, {2, 3, 4, 5}]
>>> list(bottom(list(map(frozenset, values))))
[frozenset({1, 2, 3}), frozenset({2, 3, 4, 5})]
top(iterable: Iterable[_P]) Iterator[_P]

Compute an iterator over the top elements of the iterable.

This operation computes in \(O(n^2)\) where \(n\) is the number of elements in the iterable.

Parameters:

iterable (Iterable[_P]) – An iterable of partially ordered elements.

Returns:

An iterator over the top elements.

Return type:

Iterator[_P]

Example

>>> from galactic.algebras.poset import top
>>> values = [{1, 2, 3}, {1, 2, 3, 4}, {2, 3, 4, 5}]
>>> list(top(list(map(frozenset, values))))
[frozenset({1, 2, 3, 4}), frozenset({2, 3, 4, 5})]
lower_limit(iterable: Iterable[_P], limit: _P, strict: bool = False) Iterator[_P]

Compute an iterator over the elements greater than the limit.

This operation computes in \(O(n)\) where \(n\) is the number of elements in the iterable.

Parameters:
  • iterable (Iterable[_P]) – An iterable of partially ordered elements.

  • limit (_P) – The lower limit

  • strict (bool) – Is the comparison strict?

Yields:

_P – A selected element

Example

>>> from galactic.algebras.poset import lower_limit
>>> values = [{1, 2}, {1, 2, 3}, {2, 3}]
>>> list(lower_limit(list(map(frozenset, values)), frozenset({2, 3})))
[frozenset({1, 2, 3}), frozenset({2, 3})]

# noqa: DAR301

upper_limit(iterable: Iterable[_P], limit: _P, strict: bool = False) Iterator[_P]

Compute an iterator over the elements lesser than the limit.

This operation computes in \(O(n)\) where \(n\) is the number of elements in the iterable.

Parameters:
  • iterable (Iterable[_P]) – An iterable of partially ordered elements.

  • limit (_P) – The upper limit

  • strict (bool) – Is the comparison strict?

Yields:

_P – A selected element.

Example

>>> from galactic.algebras.poset import upper_limit
>>> values = [{1, 2}, {1, 2, 3}, {2, 3}]
>>> list(upper_limit(list(map(frozenset, values)), frozenset({2, 3})))
[frozenset({2, 3})]

# noqa: DAR301

class FiniteReversibleCoveringRelation(domain: Iterable[_P] | None = None)

FiniteReversibleCoveringRelation represents the covering relation.

__copy__() FiniteReversibleCoveringRelation[_P]

Get a copy of the covering relation.

Its complexity is in \(O(n)\).

Returns:

The copy of this covering relation.

Return type:

FiniteReversibleCoveringRelation[_P]

__init__(domain: Iterable[_P] | None = None) None

Initialise a FiniteReversibleCoveringRelation instance.

The initializer can take 0 or 1 argument that should be iterable.

Parameters:

domain (Iterable[_P], optional) – The domain of the covering relation.

property co_domain: MutableSet[_P]

Get the co-domain of this covering relation.

Returns:

The co-domain of this covering relation.

Return type:

MutableSet[_P]

Notes

The domain and the co-domain of a covering relation are identical.

property domain: MutableSet[_P]

Get the domain of this covering relation.

Returns:

The domain of this covering relation.

Return type:

MutableSet[_P]

extend(iterable: Iterable[_P]) None

Extend this covering relation.

Parameters:

iterable (Iterable[_P]) – An iterable of values.

isdisjoint(other)

Return True if two sets have a null intersection.

neighbourhoods() Reversible[Neighbourhood[_P]]

Get an iterator over the neighbourhoods of an element.

Returns:

An iterable giving a triple (element, successors, predecessors) for each element in the covering relation.

Return type:

Reversible[Neighbourhood[_P]]

predecessors(element: _P) AbstractSet[_P]

Get the predecessors of an element.

Parameters:

element (_P) – The element whose predecessors are requested

Returns:

The predecessors.

Return type:

AbstractSet[_P]

Raises:

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

property sinks: AbstractSet[_P]

Get the elements with no successors.

Returns:

The elements with no successors.

Return type:

AbstractSet[_P]

property sources: AbstractSet[_P]

Get the elements with no predecessors.

Returns:

The elements with no predecessors.

Return type:

AbstractSet[_P]

successors(element: _P) AbstractSet[_P]

Get the successors of an element.

Parameters:

element (_P) – The element whose successors are requested

Returns:

The successors.

Return type:

AbstractSet[_P]

Raises:

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

property universes: Tuple[MutableSet[_P], MutableSet[_P]]

Get the universes of this covering relation.

Returns:

The universes of this covering relation.

Return type:

Tuple[MutableSet[_P], MutableSet[_P]]

class FinitePartiallyOrderedSet(domain: Iterable[_P] | None = None)

FinitePartiallyOrderedSet implements posets.

A FinitePartiallyOrderedSet instance stores its values in two dictionaries for the immediate successors (\(\succ\)) and the immediate predecessors (\(\prec\)).

Its memory complexity is in \(O(n)\).

Operation complexities:

Example

>>> from galactic.algebras.poset import FinitePartiallyOrderedSet
>>> from galactic.algebras.examples.arithmetic import PrimeFactors
>>> poset = FinitePartiallyOrderedSet[PrimeFactors](
...     domain=[
...         PrimeFactors(2*3*5*7),
...         PrimeFactors(3*5*7*11),
...         PrimeFactors(3*5*7),
...         PrimeFactors(3*5),
...         PrimeFactors(5),
...         PrimeFactors(7),
...     ]
... )
>>>

It’s possible to iterate over the poset elements. The elements are iterated level by level starting from the top ones.

Example

>>> sorted(list(map(int, poset.domain)))
[5, 7, 15, 105, 210, 1155]

It’s possible to know the poset length.

Example

>>> len(poset.domain)
6

It’s possible to know if an element is in the poset.

Example

>>> PrimeFactors(210) in poset.domain
True
>>> PrimeFactors(211) in poset.domain
False

It’s possible to iterate over the top and bottom elements.

Example

>>> sorted(list(map(int, poset.top)))
[210, 1155]
>>> sorted(list(map(int, poset.bottom)))
[5, 7]

It’s possible to iterate over the descendants and the ascendants of an element.

Example

>>> sorted(list(map(int, poset.filter(PrimeFactors(105)).domain)))
[105, 210, 1155]
>>> sorted(list(map(int, poset.ideal(PrimeFactors(105)).domain)))
[5, 7, 15, 105]

It’s possible to iterate over the (immediate) successors and the (immediate) predecessors of an element.

Example

>>> sorted(list(map(int, poset.cover.successors(PrimeFactors(105)))))
[210, 1155]
>>> sorted(list(map(int, poset.successors(PrimeFactors(105)))))
[105, 210, 1155]
>>> sorted(list(map(int, poset.cover.predecessors(PrimeFactors(105)))))
[7, 15]
>>> sorted(list(map(int, poset.predecessors(PrimeFactors(105)))))
[5, 7, 15, 105]

It’s possible to enlarge a partially ordered set.

Example

>>> poset.extend([PrimeFactors(13)])
>>> sorted(list(map(int, poset.domain)))
[5, 7, 13, 15, 105, 210, 1155]
__contains__(item: object) bool

Get the membership of an element.

Parameters:

item (object) – The element whose membership is requested.

Returns:

True if the item belongs to the partially ordered set.

Return type:

bool

__copy__() FinitePartiallyOrderedSet[_P]

Get a copy of the partially ordered set.

Its complexity is in \(O(n)\).

Returns:

The copy of this poset.

Return type:

PartiallyOrderedSet[_P]

__init__(domain: Iterable[_P] | None = None) None

Initialise a FinitePartiallyOrderedSet instance.

The initializer can take 0 or 1 argument that should be iterable.

Parameters:

domain (Iterable[_P], optional) – An iterable of values.

__iter__() Iterator[Tuple[_P, _P]]

Get an iterator over the elements.

Yields:

Tuple[_P, _P] – A couple of the relation.

property bottom: AbstractSet[_P]

Get the bottom elements.

Returns:

The bottom elements.

Return type:

AbstractSet[_P]

property co_domain: MutableSet[_P]

Get the co-domain of this poset.

Returns:

The co-domain of this poset.

Return type:

MutableSet[_P]

Notes

The domain and the co-domain of a poset are identical.

property cover: FiniteReversibleCoveringRelation[_P]

Get the covering relation of this poset.

Returns:

The covering relation.

Return type:

FiniteReversibleCoveringRelation[_P]

property domain: MutableSet[_P]

Get the domain of this poset.

Returns:

The domain of this poset.

Return type:

MutableSet[_P]

extend(iterable: Iterable[_P]) None

Extend this poset.

Parameters:

iterable (Iterable[_P]) – An iterable of values.

filter(element: _P) AbstractFiniteLowerBoundedSet[_P]

Get a filter of a poset.

Parameters:

element (_P) – The lower limit

Returns:

The lower bounded poset.

Return type:

AbstractFiniteLowerBoundedSet[_P]

Raises:

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

ideal(element: _P) AbstractFiniteUpperBoundedSet[_P]

Get an ideal of the poset.

Parameters:

element (_P) – The upper limit

Returns:

The upper bounded set.

Return type:

AbstractFiniteUpperBoundedSet[_P]

Raises:

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

isdisjoint(other)

Return True if two sets have a null intersection.

predecessors(element: _P) AbstractSet[_P]

Get the predecessors of an element.

Parameters:

element (_P) – The element whose predecessors are requested

Returns:

The predecessors.

Return type:

AbstractSet[_P]

Raises:

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

property sinks: AbstractSet[_P]

Get the sink elements.

Returns:

The sink elements.

Return type:

AbstractSet[_P]

property sources: AbstractSet[_P]

Get the source elements.

Returns:

The source elements.

Return type:

AbstractSet[_P]

successors(element: _P) AbstractSet[_P]

Get the successors of an element.

Parameters:

element (_P) – The element whose successors are requested

Returns:

The successors.

Return type:

AbstractSet[_P]

Raises:

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

property top: AbstractSet[_P]

Get the top elements.

Returns:

The top elements.

Return type:

AbstractSet[_P]

property universes: Tuple[MutableSet[_P], MutableSet[_P]]

Get the universes of this poset.

Returns:

The universes of this poset.

Return type:

Tuple[MutableSet[_P], MutableSet[_P]]

class HasseDiagramRenderer

HasseDiagramRenderer renders graph attributes.

add_destination(element: _F, index: int | None = None, current: bool = False, successors: AbstractSet[_E] | None = None, predecessors: AbstractSet[_E] | None = None) None

Add a destination to the graph.

Parameters:
  • element (_F) – The destination to add.

  • 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: AbstractSet[_F] | None = None, predecessors: AbstractSet[_E] | None = None) None

Add an edge to the graph.

Parameters:
  • source (_E) – The edge source

  • destination (_F) – The edge destination

  • 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: int | None = None, current: bool = False, successors: AbstractSet[_F] | None = None, predecessors: AbstractSet[_F] | None = None) None

Add a source to the graph.

Parameters:
  • element (_E) – The source to add.

  • 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 Hasse diagram.

Returns:

A dictionary of graphviz attributes

Return type:

Dict[str, str]

class DynamicDiagram(graph_renderer: HasseDiagramRenderer[_P] | None = None, domain_renderer: NodeRenderer[_P, _P] | None = None, edge_renderer: EdgeRenderer[_P, _P] | None = None)

DynamicDiagram is designed to allow dynamic graph rendering.

__init__(graph_renderer: HasseDiagramRenderer[_P] | None = None, domain_renderer: NodeRenderer[_P, _P] | None = None, edge_renderer: EdgeRenderer[_P, _P] | None = None) None

Initialise a DynamicDiagram instance.

Parameters:
  • graph_renderer (HasseDiagramRendererRenderer[_P]) – A Hasse diagram renderer.

  • domain_renderer (NodeRenderer[_P, _P]) – A node renderer.

  • edge_renderer (EdgeRenderer[_P, _P]) – An edge renderer.

property body: Sequence[str]

Get the current graphviz body.

Returns:

The current body.

Return type:

Sequence[str]

finish() None

Finish the iteration.

pipe(output: str = 'svg') bytes

Pipe the diagram.

Parameters:

output (str) – The format used

Returns:

The image of the diagram.

Return type:

bytes

render(filename: str) None

Render the diagram in a file.

Parameters:

filename (str) – A file path.

property source: str

Get the graphviz source for this diagram.

Returns:

The graphviz output for this diagram.

Return type:

str

start() None

Start a new iteration.

step(element: _P, successors: AbstractSet[_P], predecessors: AbstractSet[_P]) None

Insert a new element in the diagram.

Parameters:
  • element (_P) – The new element

  • successors (AbstractSet[_P]) – The immediate successors of the element.

  • predecessors (AbstractSet[_P]) – The immediate predecessors of the element.

class HasseDiagram(poset: AbstractFinitePartiallyOrderedSet[_P], graph_renderer: HasseDiagramRenderer[_P] | None = None, domain_renderer: NodeRenderer[_P, _P] | None = None, edge_renderer: EdgeRenderer[_P, _P] | None = None)

HasseDiagram is used for drawing Hasse diagrams.

__init__(poset: AbstractFinitePartiallyOrderedSet[_P], graph_renderer: HasseDiagramRenderer[_P] | None = None, domain_renderer: NodeRenderer[_P, _P] | None = None, edge_renderer: EdgeRenderer[_P, _P] | None = None) None

Initialise a Hasse diagram instance.

Parameters:
property domain_renderer: NodeRenderer[_P, _P]

Get the domain renderer.

Returns:

The domain renderer.

Return type:

NodeRenderer[_E, _E]

property edge_renderer: EdgeRenderer[_P, _P]

Get the edge renderer.

Returns:

The edge renderer.

Return type:

EdgeRenderer[_P, _P]

property graph_renderer: HasseDiagramRenderer[_P]

Get the Hasse diagram renderer.

Returns:

The Hasse diagram renderer.

Return type:

HasseDiagramRenderer[_P]

pipe(output: str = 'svg') bytes

Pipe the diagram.

Parameters:

output (str) – The format used

Returns:

The image of the diagram.

Return type:

bytes

property poset: AbstractFinitePartiallyOrderedSet[_P]

Get the poset.

Returns:

The underlying poset.

Return type:

AbstractFinitePartiallyOrderedSet[_P]

render(filename: str) None

Render the diagram in a file.

Parameters:

filename (str) – A file path.

property source: str

Get the graphviz source for this partially ordered set.

Returns:

The graphviz output for this partially ordered set.

Return type:

str

class IterativeDiagram(poset: AbstractFinitePartiallyOrderedSet[_P], graph_renderer: HasseDiagramRenderer[_P] | None = None, domain_renderer: NodeRenderer[_P, _P] | None = None, edge_renderer: EdgeRenderer[_P, _P] | None = None)

IterativeDiagram allows iterative rendering.

__init__(poset: AbstractFinitePartiallyOrderedSet[_P], graph_renderer: HasseDiagramRenderer[_P] | None = None, domain_renderer: NodeRenderer[_P, _P] | None = None, edge_renderer: EdgeRenderer[_P, _P] | None = None) None

Initialise a IterativeDiagram instance.

Parameters: