Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
infrastructure for lattice posets
Browse files Browse the repository at this point in the history
  • Loading branch information
mantepse committed Jan 27, 2023
1 parent ac97e27 commit c84d0a1
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/sage/combinat/posets/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@

from .lattices import LatticePoset, MeetSemilattice, JoinSemilattice

from .poset_examples import posets, Posets
from .poset_examples import posets, Posets, LatticePosets
111 changes: 110 additions & 1 deletion src/sage/combinat/posets/lattices.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@
# https://www.gnu.org/licenses/
# *****************************************************************************
from itertools import repeat
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
from sage.categories.finite_lattice_posets import FiniteLatticePosets
from sage.combinat.posets.posets import Poset, FinitePoset
from sage.structure.unique_representation import UniqueRepresentation
from sage.structure.parent import Parent
from sage.rings.integer import Integer
from sage.combinat.posets.posets import Poset, FinitePoset, FinitePosets_n
from sage.combinat.posets.elements import (LatticePosetElement,
MeetSemilatticeElement,
JoinSemilatticeElement)
Expand Down Expand Up @@ -4946,6 +4950,111 @@ def congruences_lattice(self, labels='congruence'):
for v in p] for p in C[e]]))


class FiniteLatticePosets_n(UniqueRepresentation, Parent):
r"""
The finite enumerated set of all lattice posets on `n` elements, up to an isomorphism.
EXAMPLES::
sage: P = LatticePosets(5)
sage: P.cardinality()
5
sage: for p in P: print(p.cover_relations())
[['bottom', 0], ['bottom', 1], ['bottom', 2], [0, 'top'], [1, 'top'], [2, 'top']]
[['bottom', 0], ['bottom', 1], [0, 'top'], [1, 2], [2, 'top']]
[['bottom', 0], [0, 1], [0, 2], [1, 'top'], [2, 'top']]
[['bottom', 0], [0, 1], [1, 2], [2, 'top']]
[['bottom', 0], ['bottom', 1], [0, 2], [1, 2], [2, 'top']]
"""

def __init__(self, n):
r"""
EXAMPLES::
sage: P = LatticePosets(5); P
Lattice posets containing 5 elements
sage: P.category()
Category of finite enumerated sets
sage: TestSuite(P).run()
"""
Parent.__init__(self, category=FiniteEnumeratedSets())
self._n = n

def _repr_(self):
r"""
EXAMPLES::
sage: P = LatticePosets(5)
sage: P._repr_()
'Lattice posets containing 5 elements'
"""
return "Lattice posets containing %s elements" % self._n

def __contains__(self, P):
"""
EXAMPLES::
sage: posets.PentagonPoset() in LatticePosets(5)
True
sage: posets.PentagonPoset() in LatticePosets(3)
False
sage: posets.AntichainPoset(3) in LatticePosets(3)
False
sage: 1 in LatticePosets(3)
False
"""
return P in FiniteLatticePosets() and P.cardinality() == self._n

def __iter__(self):
"""
Return an iterator of representatives of the isomorphism classes
of finite posets of a given size.
EXAMPLES::
sage: P = LatticePosets(2)
sage: list(P)
[Finite lattice containing 2 elements]
"""
if self._n <= 2:
for P in FinitePosets_n(self._n):
if P.is_lattice():
yield LatticePoset(P)
else:
for P in FinitePosets_n(self._n-2):
Q = P.with_bounds()
if Q.is_lattice():
yield LatticePoset(Q)

def cardinality(self, from_iterator=False):
r"""
Return the cardinality of this object.
.. note::
By default, this returns pre-computed values obtained from
the On-Line Encyclopedia of Integer Sequences (:oeis:`A006966`).
To override this, pass the argument ``from_iterator=True``.
EXAMPLES::
sage: P = LatticePosets(5)
sage: P.cardinality()
5
sage: P.cardinality(from_iterator=True)
5
"""
# Obtained from The On-Line Encyclopedia of Integer Sequences;
# this is sequence number A006966.
known_values = [1, 1, 1, 1, 2, 5, 15, 53, 222, 1078, 5994,
37622, 262776, 2018305, 16873364, 152233518, 1471613387,
15150569446, 165269824761, 1901910625578, 23003059864006]
if not from_iterator and self._n < len(known_values):
return Integer(known_values[self._n])
else:
return super(FiniteLatticePosets_n, self).cardinality()


def _log_2(n):
"""
Return the 2-based logarithm of `n` rounded up.
Expand Down
28 changes: 27 additions & 1 deletion src/sage/combinat/posets/poset_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
from sage.combinat.posets.d_complete import DCompletePoset
from sage.combinat.posets.mobile import MobilePoset as Mobile
from sage.combinat.posets.lattices import (LatticePoset, MeetSemilattice,
JoinSemilattice, FiniteLatticePoset)
JoinSemilattice, FiniteLatticePoset,
FiniteLatticePosets_n)
from sage.categories.finite_posets import FinitePosets
from sage.categories.finite_lattice_posets import FiniteLatticePosets
from sage.graphs.digraph import DiGraph
Expand Down Expand Up @@ -2184,3 +2185,28 @@ def _random_stone_lattice(n):


posets = Posets

class LatticePosets(metaclass=ClasscallMetaclass):
@staticmethod
def __classcall__(cls, n=None):
r"""
Return either the category of all posets, or the finite
enumerated set of all finite posets on ``n`` elements up to an
isomorphism.
EXAMPLES::
sage: Posets()
Category of posets
sage: Posets(4)
Posets containing 4 elements
"""
if n is None:
return sage.categories.lattice_posets.LatticePosets()
try:
n = Integer(n)
except TypeError:
raise TypeError("number of elements must be an integer, not {0}".format(n))
if n < 0:
raise ValueError("number of elements must be non-negative, not {0}".format(n))
return FiniteLatticePosets_n(n)

0 comments on commit c84d0a1

Please sign in to comment.