From 95ce20f8b875cbb2cc8848b09120544a895612b5 Mon Sep 17 00:00:00 2001 From: Martin Rubey Date: Wed, 11 Jul 2018 11:53:30 +0200 Subject: [PATCH] provide iterators which return lists of lists --- src/sage/combinat/set_partition.py | 37 ++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/sage/combinat/set_partition.py b/src/sage/combinat/set_partition.py index a552e5d965d..b9a5d04fbad 100644 --- a/src/sage/combinat/set_partition.py +++ b/src/sage/combinat/set_partition.py @@ -1970,6 +1970,24 @@ def __iter__(self): sage: SetPartitions(3).list() [{{1, 2, 3}}, {{1, 2}, {3}}, {{1, 3}, {2}}, {{1}, {2, 3}}, {{1}, {2}, {3}}] """ + for sp in self._fast_iterator(): + yield self.element_class(self, sp, check=False) + + def _fast_iterator(self): + """ + A fast iterator for the set partitions of the base set, which + returns lists of lists instead of set partitions types. + + EXAMPLES:: + + sage: list(SetPartitions([1,-1,x])._fast_iterator()) + [[[1, -1, x]], + [[1, -1], [x]], + [[1, x], [-1]], + [[1], [-1, x]], + [[1], [-1], [x]]] + + """ base_set = list(self.base_set()) def from_word(w): sp = [] @@ -1978,7 +1996,7 @@ def from_word(w): sp.append([i]) else: sp[b].append(i) - return self.element_class(self, sp, check=False) + return sp # Knuth, TAOCP 4A 7.2.1.5, Algorithm H N = len(base_set) @@ -2247,6 +2265,21 @@ def __iter__(self): {{1, 2}, {3, 4}}, {{1, 2, 3}, {4}}] """ + for sp in self._fast_iterator(): + yield self.element_class(self, sp, check=False) + + def _fast_iterator(self): + """ + A fast iterator for the set partitions of the base set into the + specified number of blocks, which returns lists of lists + instead of set partitions types. + + EXAMPLES:: + + sage: list(SetPartitions([1,-1,x], 2)._fast_iterator()) + [[[1, x], [-1]], [[1], [-1, x]], [[1, -1], [x]]] + + """ base_set = list(self.base_set()) def from_word(w): sp = [] @@ -2255,7 +2288,7 @@ def from_word(w): sp.append([i]) else: sp[b].append(i) - return self.element_class(self, sp, check=False) + return sp # Ruskey, Combinatorial Generation, Algorithm 4.23 n = len(base_set)