Ticket #11293: trac_11293-all-relations-poset.patch

File trac_11293-all-relations-poset.patch, 2.5 KB (added by rbeezer, 10 years ago)
  • sage/combinat/posets/posets.py

    # HG changeset patch
    # User Rob Beezer <beezer@ups.edu>
    # Date 1304534650 25200
    # Node ID 86367af255a984dfccacce996946644b972cb65e
    # Parent  52179a365cc3f43af41bd9764c8adee89f9c19dd
    11293: all relations of a poset
    
    diff -r 52179a365cc3 -r 86367af255a9 sage/combinat/posets/posets.py
    a b  
    695695        for u,v,l in self._hasse_diagram.edge_iterator():
    696696            yield map(self._vertex_to_element,(u,v))
    697697
     698    def relations(self):
     699        r"""
     700        Returns a list of all relations of the poset.
     701
     702        OUTPUT:
     703
     704        A list of pairs (each pair is a list), where the first element
     705        of the pair is less than the second element.
     706
     707        Pairs are produced in a rough sort of lexicographic order,
     708        where earlier elements are from lower levels of the poset.
     709
     710        EXAMPLES::
     711
     712            sage: Q = Poset({0:[2], 1:[2], 2:[3], 3:[4], 4:[]})
     713            sage: Q.relations()
     714            [[1, 2], [1, 3], [1, 4], [0, 2], [0, 3], [0, 4], [2, 3], [2, 4], [3, 4]]
     715
     716        AUTHOR:
     717
     718        - Rob Beezer (2011-05-04)
     719        """
     720        return [r for r in self.relations_iterator()]
     721
     722    def relations_iterator(self):
     723        r"""
     724        Returns an iterator for all the relations of the poset.
     725
     726        OUTPUT:
     727
     728        A generator that produces pairs (each pair is a list), where
     729        the first element of the pair is less than the second element.
     730
     731        Pairs are produced in a rough sort of lexicographic order,
     732        where earlier elements are from lower levels of the poset.
     733
     734        EXAMPLES::
     735
     736            sage: Q = Poset({0:[2], 1:[2], 2:[3], 3:[4], 4:[]})
     737            sage: type(Q.relations_iterator())
     738            <type 'generator'>
     739            sage: [z for z in Q.relations_iterator()]
     740            [[1, 2], [1, 3], [1, 4], [0, 2], [0, 3], [0, 4], [2, 3], [2, 4], [3, 4]]
     741
     742        AUTHOR:
     743
     744        - Rob Beezer (2011-05-04)
     745        """
     746        # Relies on vertices 0 to n-1 in the Hasse diagram being a
     747        # linear extension of the elements of the poset
     748        n = self._hasse_diagram.order()
     749        for i in range(n):
     750            for j in range(i+1, n):
     751                if self._hasse_diagram.is_less_than(i, j):
     752                    yield [self._vertex_to_element(i), self._vertex_to_element(j)]
     753
    698754    def is_lequal(self, x, y):
    699755        """
    700756        Returns True if x is less than or equal to y in the poset, and