Partially ordered set
Elements
Partially ordered elements implements the 6 classical comparison operation:
\(<\)
\(\leq\)
\(>\)
\(\geq\)
\(=\)
\(\neq\)
They can be filtered using some special functions.
PrimeFactors example
The PrimeFactors
class in the examples folder implements a partially ordered relation between positive integers using the divisor of notion.
To import the PrimeFactors
class:
[1]:
from galactic.algebras.examples.arithmetic import PrimeFactors
PrimeFactors(12)
[1]:
3 is lesser than 6:
[2]:
PrimeFactors(3) <= PrimeFactors(6)
[2]:
True
2 and 3 are incomparable:
[3]:
PrimeFactors(2) <= PrimeFactors(3)
[3]:
False
[4]:
PrimeFactors(3) <= PrimeFactors(2)
[4]:
False
1 is lesser than all the other numbers (1 is a divisor of all numbers) and 0 is greater than all other numbers (all numbers are divisors of 0):
[5]:
PrimeFactors(1) <= PrimeFactors(6)
[5]:
True
[6]:
PrimeFactors(6) <= PrimeFactors(0)
[6]:
True
[7]:
from galactic.algebras.poset import top
elements = [PrimeFactors(6), PrimeFactors(24), PrimeFactors(13)]
top(elements)
[7]:
<odict_iterator at 0x70bffc770fe0>
Note that the result is a python iterator. To get the list:
[8]:
display(*top(elements))
An analog operation is available to get the bottom elements:
[9]:
from galactic.algebras.poset import bottom
display(*bottom(elements))
It’s possible to get the elements greater or lower than a given limit:
[10]:
from galactic.algebras.poset import upper_limit
display(*upper_limit(iterable=elements, limit=PrimeFactors(6)))
[11]:
from galactic.algebras.poset import lower_limit
display(*lower_limit(iterable=elements, limit=PrimeFactors(12), strict=True))
Color example
The Color
class in the examples folder implements a partially ordered relation between colors.
To import the Color
class:
[12]:
from galactic.algebras.examples.color import Color
[13]:
Color(red=1.0, green=0.5)
[13]:
[14]:
Color(red=1.0, green=1.0)
[14]:
The orange color is less or equal than the yellow color:
[15]:
True
[16]:
Color(red=0.5, green=1.0)
[16]:
Collections
A poset is a set or partially ordered elements.
PrimeFactors example
[17]:
from galactic.algebras.examples.arithmetic.renderer import PrimeFactorsRenderer
from galactic.algebras.poset import MutableFinitePartiallyOrderedSet
from galactic.algebras.poset.renderer import HasseDiagram
poset = MutableFinitePartiallyOrderedSet[PrimeFactors](
elements=[
PrimeFactors(24),
PrimeFactors(18),
PrimeFactors(9),
PrimeFactors(6),
PrimeFactors(3),
PrimeFactors(2),
],
)
poset have all classical python operations on sets:
[18]:
HasseDiagram[PrimeFactors](poset, domain_renderer=PrimeFactorsRenderer())
[18]:
[19]:
len(poset)
[19]:
6
[20]:
display(*poset.cover)
(PrimeFactors(3), PrimeFactors(9))
(PrimeFactors(3), PrimeFactors(6))
(PrimeFactors(2), PrimeFactors(6))
(PrimeFactors(9), PrimeFactors(18))
(PrimeFactors(6), PrimeFactors(24))
(PrimeFactors(6), PrimeFactors(18))
[21]:
PrimeFactors(3) in poset.cover.domain
[21]:
True
[22]:
poset.extend([PrimeFactors(9), PrimeFactors(6), PrimeFactors(5)])
HasseDiagram[PrimeFactors](poset, domain_renderer=PrimeFactorsRenderer())
[22]:
[23]:
poset.extend([PrimeFactors(10)])
HasseDiagram[PrimeFactors](poset, domain_renderer=PrimeFactorsRenderer())
[23]:
[24]:
poset >= {PrimeFactors(9), PrimeFactors(6)}
[24]:
True
From a poset, several additional methods can be applied:
get the top or the bottom elements from the poset;
get the descendants or the ascendants of an element;
get the successors and predecessors of an element;
get the filter and an ideal of an element.
[25]:
display(*poset.top)
[26]:
display(*poset.bottom)
[27]:
display(*poset.cover.predecessors(PrimeFactors(18)))
[28]:
display(*poset.order.predecessors(PrimeFactors(18)))
[29]:
display(*poset.cover.successors(PrimeFactors(6)))
[30]:
display(*poset.order.successors(PrimeFactors(6)))
[31]:
HasseDiagram[PrimeFactors](poset, domain_renderer=PrimeFactorsRenderer())
[31]:
[32]:
HasseDiagram[PrimeFactors](
poset.ideal(PrimeFactors(18)),
domain_renderer=PrimeFactorsRenderer(),
)
[32]:
[33]:
HasseDiagram[PrimeFactors](
poset.filter(PrimeFactors(3)),
domain_renderer=PrimeFactorsRenderer(),
)
[33]:
[34]:
HasseDiagram[PrimeFactors](
poset.filter(PrimeFactors(3)).ideal(PrimeFactors(18)),
domain_renderer=PrimeFactorsRenderer(),
)
[34]:
[35]:
HasseDiagram[PrimeFactors](
poset.ideal(PrimeFactors(18)).filter(PrimeFactors(3)),
domain_renderer=PrimeFactorsRenderer(),
)
[35]:
Color example
Using colors, you can can test the poset collections:
[36]:
from galactic.algebras.examples.color.renderer import ColorRenderer
from galactic.algebras.poset import MutableFinitePartiallyOrderedSet
colors = MutableFinitePartiallyOrderedSet[Color](
elements=[
Color(red=1.0),
Color(red=1.0, green=1.0),
Color(red=0.5),
Color(red=0.5, green=0.5),
Color(green=1.0, blue=0.5),
Color(green=1.0, blue=1.0),
],
)
[37]:
HasseDiagram[Color](colors, domain_renderer=ColorRenderer())
[37]:
[38]:
len(colors)
[38]:
6
[39]:
display(*colors)
[40]:
display(*colors.cover)
(Color(red=0.5, green=0.0, blue=0.0), Color(red=1.0, green=0.0, blue=0.0))
(Color(red=0.5, green=0.0, blue=0.0), Color(red=0.5, green=0.5, blue=0.0))
(Color(red=0.0, green=1.0, blue=0.5), Color(red=0.0, green=1.0, blue=1.0))
(Color(red=1.0, green=0.0, blue=0.0), Color(red=1.0, green=1.0, blue=0.0))
(Color(red=0.5, green=0.5, blue=0.0), Color(red=1.0, green=1.0, blue=0.0))
[41]:
True
[42]:
display(*colors.top)
[43]:
display(*colors.bottom)
[44]:
Color(red=0.5)
[44]:
[45]:
display(*colors.cover.successors(Color(red=0.5)))
[46]:
display(*colors.order.successors(Color(red=0.5)))
[47]:
Color(red=1.0)
[47]:
[48]:
display(*colors.cover.predecessors(Color(red=1.0)))
[49]:
display(*colors.order.predecessors(Color(red=1.0)))