# HG changeset patch
# User Rob Beezer
# 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/sage/combinat/posets/posets.py Fri Apr 22 16:27:04 2011 +0000
+++ b/sage/combinat/posets/posets.py Wed May 04 13:14:54 2011 -0700
@@ -695,6 +695,64 @@
for u,v,l in self._hasse_diagram.edge_iterator():
yield map(self._vertex_to_element,(u,v))
+ def relations(self):
+ r"""
+ Returns a list of all relations of the poset.
+
+ OUTPUT:
+
+ A list of pairs (each pair is a list), where the first element
+ of the pair is less than or equal to the second element.
+
+ Pairs are produced in a rough sort of lexicographic order,
+ where earlier elements are from lower levels of the poset.
+
+ EXAMPLES::
+
+ sage: Q = Poset({0:[2], 1:[2], 2:[3], 3:[4], 4:[]})
+ sage: Q.relations()
+ [[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]]
+
+ AUTHOR:
+
+ - Rob Beezer (2011-05-04)
+ """
+ return list(self.relations_iterator())
+
+ def relations_iterator(self):
+ r"""
+ Returns an iterator for all the relations of the poset.
+
+ OUTPUT:
+
+ A generator that produces pairs (each pair is a list), where the
+ first element of the pair is less than or equal to the second element.
+
+ Pairs are produced in a rough sort of lexicographic order,
+ where earlier elements are from lower levels of the poset.
+
+ EXAMPLES::
+
+ sage: Q = Poset({0:[2], 1:[2], 2:[3], 3:[4], 4:[]})
+ sage: type(Q.relations_iterator())
+
+ sage: [z for z in Q.relations_iterator()]
+ [[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]]
+
+ AUTHOR:
+
+ - Rob Beezer (2011-05-04)
+ """
+ # Relies on vertices the fact that _elements correspond to the rows and
+ # columns of the lequal matrix
+ leq_mat = self.lequal_matrix()
+ n = leq_mat.nrows()
+ elements = self._elements
+ for i in range(n):
+ for j in range(i, n):
+ if leq_mat[i,j]:
+ yield [elements[i], elements[j]]
+
def is_lequal(self, x, y):
"""
Returns True if x is less than or equal to y in the poset, and