| 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 | |