def BooleanLattice(n, facade=None): """ Return the Boolean lattice containing `2^n` elements. - ``n`` (an integer) -- number of elements will be `2^n` - ``facade`` (boolean) -- whether to make the returned poset a facade poset (see :mod:`sage.categories.facade_sets`); the default behaviour is the same as the default behaviour of the :func:`~sage.combinat.posets.posets.Poset` constructor EXAMPLES:: sage: Posets.BooleanLattice(5) Finite lattice containing 32 elements """ 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)) if n == 0: return LatticePoset(([0], [])) if n == 1: return LatticePoset(([0, 1], [[0, 1]])) return LatticePoset( [[Integer(x | (1 << y)) for y in range(0, n) if x & (1 << y) == 0] for x in range(0, 2**n)], facade=facade)
def PentagonPoset(facade = None): """ Returns the Pentagon poset. INPUT: - ``facade`` (boolean) -- whether to make the returned poset a facade poset (see :mod:`sage.categories.facade_sets`). The default behaviour is the same as the default behaviour of the :func:`~sage.combinat.posets.posets.Poset` constructor). EXAMPLES:: sage: P = Posets.PentagonPoset(); P Finite lattice containing 5 elements sage: P.cover_relations() [[0, 1], [0, 2], [1, 4], [2, 3], [3, 4]] This is smallest lattice that is not modular:: sage: P.is_modular() False This poset and the :meth:`DiamondPoset` are the two smallest lattices which are not distributive:: sage: P.is_distributive() False sage: Posets.DiamondPoset(5).is_distributive() False """ p = LatticePoset([[1,2],[4],[3],[4],[]], facade = facade) p.hasse_diagram()._pos = {0:[2,0],1:[0,2],2:[3,1],3:[3,3],4:[2,4]} return p
def order_ideals_lattice(self, as_ideals=True): r""" Returns the lattice of order ideals of a poset `P`, ordered by inclusion. The usual notation is `J(P)`. The underlying set is by default the set of order ideals of `P`. It can be alternatively chosen to be the set of antichains of `P`. INPUT: - ``as_ideals`` -- Boolean, if ``True`` (default) returns a poset on the set of order ideals, otherwise on the set of antichains EXAMPLES:: sage: P = Posets.PentagonPoset(facade = True) sage: P.cover_relations() [[0, 1], [0, 2], [1, 4], [2, 3], [3, 4]] sage: J = P.order_ideals_lattice(); J Finite lattice containing 8 elements sage: list(J) [{}, {0}, {0, 2}, {0, 1}, {0, 1, 2}, {0, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3, 4}] As a lattice on antichains:: sage: J2 = P.order_ideals_lattice(False); J2 Finite lattice containing 8 elements sage: list(J2) [(0,), (1, 2), (1, 3), (1,), (2,), (3,), (4,), ()] TESTS:: sage: J = Posets.DiamondPoset(4, facade = True).order_ideals_lattice(); J Finite lattice containing 6 elements sage: list(J) [{}, {0}, {0, 2}, {0, 1}, {0, 1, 2}, {0, 1, 2, 3}] sage: J.cover_relations() [[{}, {0}], [{0}, {0, 2}], [{0}, {0, 1}], [{0, 2}, {0, 1, 2}], [{0, 1}, {0, 1, 2}], [{0, 1, 2}, {0, 1, 2, 3}]] .. NOTE:: we use facade posets in the examples above just to ensure a nicer ordering in the output. """ from sage.combinat.posets.lattices import LatticePoset if as_ideals: from sage.misc.misc import attrcall from sage.sets.set import Set ideals = [Set( self.order_ideal(antichain) ) for antichain in self.antichains()] return LatticePoset((ideals,attrcall("issubset"))) else: from sage.misc.cachefunc import cached_function antichains = [tuple(a) for a in self.antichains()] @cached_function def is_above(a,xb): return any(self.is_lequal(xa,xb) for xa in a) def cmp(a,b): return all(is_above(a,xb) for xb in b) return LatticePoset((antichains,cmp))
def order_ideals_lattice(self): r""" Returns the lattice of order ideals of a poset `P`, ordered by inclusion. The usual notation is `J(P)`. EXAMPLES:: sage: P = Posets.PentagonPoset(facade = True) sage: P.cover_relations() [[0, 1], [0, 2], [1, 4], [2, 3], [3, 4]] sage: J = P.order_ideals_lattice(); J Finite lattice containing 8 elements sage: list(J) [{}, {0}, {0, 2}, {0, 1}, {0, 1, 2}, {0, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3, 4}] TESTS:: sage: J = Posets.DiamondPoset(4, facade = True).order_ideals_lattice(); J Finite lattice containing 6 elements sage: list(J) [{}, {0}, {0, 2}, {0, 1}, {0, 1, 2}, {0, 1, 2, 3}] sage: J.cover_relations() [[{}, {0}], [{0}, {0, 2}], [{0}, {0, 1}], [{0, 2}, {0, 1, 2}], [{0, 1}, {0, 1, 2}], [{0, 1, 2}, {0, 1, 2, 3}]] .. note:: we use facade posets in the examples above just to ensure a nicer ordering in the output. """ from sage.misc.misc import attrcall from sage.sets.set import Set from sage.combinat.posets.lattices import LatticePoset ideals = [ Set(self.order_ideal(antichain)) for antichain in self.antichains() ] return LatticePoset((ideals, attrcall("issubset")))
def PentagonPoset(facade = False): """ Returns the "pentagon poset". EXAMPLES:: sage: P = Posets.PentagonPoset(); P Finite lattice containing 5 elements sage: P.cover_relations() [[0, 1], [0, 2], [1, 4], [2, 3], [3, 4]] This lattice and the diamond poset on 5 elements are the two smallest lattices which are not distributive:: sage: P.is_distributive() False sage: Posets.DiamondPoset(5).is_distributive() False """ p = LatticePoset([[1,2],[4],[3],[4],[]], facade = facade) p.hasse_diagram()._pos = {0:[2,0],1:[0,2],2:[3,1],3:[3,3],4:[2,4]} return p
def ChainPoset(n, facade=None): """ Return a chain (a totally ordered poset) containing ``n`` elements. - ``n`` (an integer) -- number of elements. - ``facade`` (boolean) -- whether to make the returned poset a facade poset (see :mod:`sage.categories.facade_sets`); the default behaviour is the same as the default behaviour of the :func:`~sage.combinat.posets.posets.Poset` constructor EXAMPLES:: sage: C = Posets.ChainPoset(6); C Finite lattice containing 6 elements sage: C.linear_extension() [0, 1, 2, 3, 4, 5] TESTS:: sage: for i in range(5): ....: for j in range(5): ....: if C.covers(C(i),C(j)) and j != i+1: ....: print("TEST FAILED") Check that :trac:`8422` is solved:: sage: Posets.ChainPoset(0) Finite lattice containing 0 elements sage: C = Posets.ChainPoset(1); C Finite lattice containing 1 elements sage: C.cover_relations() [] sage: C = Posets.ChainPoset(2); C Finite lattice containing 2 elements sage: C.cover_relations() [[0, 1]] """ 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 LatticePoset((range(n), [[x, x + 1] for x in range(n - 1)]), facade=facade)
def YoungsLatticePrincipalOrderIdeal(lam): """ Return the principal order ideal of the partition `lam` in Young's Lattice. INPUT: - ``lam`` -- a partition EXAMPLES:: sage: P = Posets.YoungsLatticePrincipalOrderIdeal(Partition([2,2])) sage: P Finite lattice containing 6 elements sage: P.cover_relations() [[[], [1]], [[1], [1, 1]], [[1], [2]], [[1, 1], [2, 1]], [[2], [2, 1]], [[2, 1], [2, 2]]] """ from sage.misc.flatten import flatten from sage.combinat.partition import Partition def lower_covers(l): """ Nested function returning those partitions obtained from the partition `l` by removing a single cell. """ return [l.remove_cell(c[0], c[1]) for c in l.removable_cells()] def contained_partitions(l): """ Nested function returning those partitions contained in the partition `l` """ if l == Partition([]): return l return flatten( [l, [contained_partitions(m) for m in lower_covers(l)]]) ideal = list(set(contained_partitions(lam))) H = DiGraph(dict([[p, lower_covers(p)] for p in ideal])) return LatticePoset(H.reverse())
def ChainPoset(n): """ Returns a chain (a totally ordered poset) containing ``n`` elements. EXAMPLES:: sage: C = Posets.ChainPoset(6); C Finite lattice containing 6 elements sage: C.linear_extension() [0, 1, 2, 3, 4, 5] TESTS:: sage: for i in range(5): ....: for j in range(5): ....: if C.covers(C(i),C(j)) and j != i+1: ....: print "TEST FAILED" Check that :trac:`8422` is solved:: sage: Posets.ChainPoset(0) Finite lattice containing 0 elements sage: C = Posets.ChainPoset(1); C Finite lattice containing 1 elements sage: C.cover_relations() [] sage: C = Posets.ChainPoset(2); C Finite lattice containing 2 elements sage: C.cover_relations() [[0, 1]] """ 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 LatticePoset((range(n), [[x, x + 1] for x in range(n - 1)]))
def order_ideals_lattice(self, as_ideals=True): r""" Return the lattice of order ideals of a poset ``self``, ordered by inclusion. The lattice of order ideals of a poset `P` is usually denoted by `J(P)`. Its underlying set is the set of order ideals of `P`, and its partial order is given by inclusion. The order ideals of `P` are in a canonical bijection with the antichains of `P`. The bijection maps every order ideal to the antichain formed by its maximal elements. By setting the ``as_ideals`` keyword variable to ``False``, one can make this method apply this bijection before returning the lattice. INPUT: - ``as_ideals`` -- Boolean, if ``True`` (default) returns a poset on the set of order ideals, otherwise on the set of antichains EXAMPLES:: sage: P = Posets.PentagonPoset(facade = True) sage: P.cover_relations() [[0, 1], [0, 2], [1, 4], [2, 3], [3, 4]] sage: J = P.order_ideals_lattice(); J Finite lattice containing 8 elements sage: list(J) [{}, {0}, {0, 2}, {0, 1}, {0, 1, 2}, {0, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3, 4}] As a lattice on antichains:: sage: J2 = P.order_ideals_lattice(False); J2 Finite lattice containing 8 elements sage: list(J2) [(0,), (1, 2), (1, 3), (1,), (2,), (3,), (4,), ()] TESTS:: sage: J = Posets.DiamondPoset(4, facade = True).order_ideals_lattice(); J Finite lattice containing 6 elements sage: list(J) [{}, {0}, {0, 2}, {0, 1}, {0, 1, 2}, {0, 1, 2, 3}] sage: J.cover_relations() [[{}, {0}], [{0}, {0, 2}], [{0}, {0, 1}], [{0, 2}, {0, 1, 2}], [{0, 1}, {0, 1, 2}], [{0, 1, 2}, {0, 1, 2, 3}]] .. NOTE:: we use facade posets in the examples above just to ensure a nicer ordering in the output. """ from sage.combinat.posets.lattices import LatticePoset if as_ideals: from sage.misc.misc import attrcall from sage.sets.set import Set ideals = [ Set(self.order_ideal(antichain)) for antichain in self.antichains() ] return LatticePoset((ideals, attrcall("issubset"))) else: from sage.misc.cachefunc import cached_function antichains = [tuple(a) for a in self.antichains()] @cached_function def is_above(a, xb): return any(self.is_lequal(xa, xb) for xa in a) def cmp(a, b): return all(is_above(a, xb) for xb in b) return LatticePoset((antichains, cmp))