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

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

    # HG changeset patch
    # User Rob Beezer <beezer@ups.edu>
    # Date 1304540094 25200
    # Node ID 4701e0cfd91480a8b311cc548b81e655c7e27752
    # Parent  52179a365cc3f43af41bd9764c8adee89f9c19dd
    11293: all relations of a poset
    * * *
    trac_11293: reviewer patch; efficiency improvements
    * * *
    11293: poset relation, diagonal fix
    
    diff -r 52179a365cc3 -r 4701e0cfd914 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 or equal to 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, 1], [1, 2], [1, 3], [1, 4], [0, 0], [0, 2], [0, 3], [0, 4], [2, 2], [2, 3], [2, 4], [3, 3], [3, 4], [4, 4]]
     715
     716        AUTHOR:
     717
     718        - Rob Beezer (2011-05-04)
     719        """
     720        return list(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 the
     729        first element of the pair is less than or equal to 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, 1], [1, 2], [1, 3], [1, 4], [0, 0], [0, 2], [0, 3], [0, 4], [2, 2], [2, 3], [2, 4], [3, 3], [3, 4], [4, 4]]
     741
     742        AUTHOR:
     743
     744        - Rob Beezer (2011-05-04)
     745        """
     746        # Relies on vertices the fact that _elements correspond to the rows and
     747        # columns of the lequal matrix
     748        leq_mat = self.lequal_matrix()
     749        n = leq_mat.nrows()
     750        elements = self._elements
     751        for i in range(n):
     752            for j in range(i, n):
     753                if leq_mat[i,j]:
     754                    yield [elements[i], elements[j]]
     755
    698756    def is_lequal(self, x, y):
    699757        """
    700758        Returns True if x is less than or equal to y in the poset, and