From 6ccd9e4203f2f6364f279e6d180a32da2c8a0012 Mon Sep 17 00:00:00 2001 From: Travis Scrimshaw Date: Thu, 11 Aug 2022 11:22:40 +0900 Subject: [PATCH] Optimization of add_*_border_strip() and doctest update. --- src/sage/combinat/partition.py | 118 ++++++++++++++++++---------- src/sage/combinat/superpartition.py | 54 ++++++------- src/sage/combinat/tableau.py | 20 ++--- 3 files changed, 113 insertions(+), 79 deletions(-) diff --git a/src/sage/combinat/partition.py b/src/sage/combinat/partition.py index 811679ede16..6b8b2f669e4 100644 --- a/src/sage/combinat/partition.py +++ b/src/sage/combinat/partition.py @@ -318,8 +318,8 @@ from . import permutation from . import composition from sage.combinat.partitions import ZS1_iterator, ZS1_iterator_nk -from sage.combinat.integer_vector import IntegerVectors from sage.combinat.integer_lists import IntegerListsLex +from sage.combinat.integer_lists.invlex import IntegerListsBackend_invlex from sage.combinat.integer_vector_weighted import iterator_fast as weighted_iterator_fast from sage.combinat.combinat_cython import conjugate from sage.combinat.root_system.weyl_group import WeylGroup @@ -4655,6 +4655,8 @@ def add_vertical_border_strip(self, k): sage: Partition([]).add_vertical_border_strip(0) [[]] + sage: Partition([3,2,1]).add_vertical_border_strip(0) + [[3, 2, 1]] sage: Partition([]).add_vertical_border_strip(2) [[1, 1]] sage: Partition([2,2]).add_vertical_border_strip(2) @@ -4662,60 +4664,92 @@ def add_vertical_border_strip(self, k): sage: Partition([3,2,2]).add_vertical_border_strip(2) [[4, 3, 2], [4, 2, 2, 1], [3, 3, 3], [3, 3, 2, 1], [3, 2, 2, 1, 1]] """ - return [p.conjugate() for p in self.conjugate().add_horizontal_border_strip(k)] + if k == 0: + return [self] - def add_horizontal_border_strip(self, k): - """ - Return a list of all the partitions that can be obtained by adding - a horizontal border strip of length ``k`` to ``self``. - - EXAMPLES:: - - sage: Partition([]).add_horizontal_border_strip(0) - [[]] - sage: Partition([]).add_horizontal_border_strip(2) - [[2]] - sage: Partition([2,2]).add_horizontal_border_strip(2) - [[2, 2, 2], [3, 2, 1], [4, 2]] - sage: Partition([3,2,2]).add_horizontal_border_strip(2) - [[3, 2, 2, 2], [3, 3, 2, 1], [4, 2, 2, 1], [4, 3, 2], [5, 2, 2]] - - .. TODO:: - - Reimplement like ``remove_horizontal_border_strip`` using - :class:`IntegerListsLex` - """ - conj = self.conjugate().to_list() shelf = [] res = [] i = 0 - while i < len(conj): + ell = len(self._list) + while i < ell: tmp = 1 - while i+1 < len(conj) and conj[i] == conj[i+1]: + while i+1 < ell and self._list[i] == self._list[i+1]: tmp += 1 i += 1 - if i == len(conj)-1 and i > 0 and conj[i] != conj[i-1]: + if i == ell-1 and i > 0 and self._list[i] != self._list[i-1]: tmp = 1 shelf.append(tmp) i += 1 - #added the last shelf on the right side of - #the first line + # added the last shelf on the right side of + # the first line shelf.append(k) - #list all of the positions for cells - #filling each self from the left to the right - for iv in IntegerVectors(k, len(shelf), outer=shelf): - iv = list(iv) # Make a mutable list - tmp = conj + [0]*k + # list all of the positions for cells + # filling each self from the left to the right + for iv in IntegerListsBackend_invlex(k, length=len(shelf), ceiling=shelf, check=False)._iter(): + tmp = self._list + [0]*k j = 0 for t in range(len(iv)): - while iv[t] > 0: + for _ in range(iv[t]): tmp[j] += 1 - iv[t] -= 1 j += 1 j = sum(shelf[:t+1]) - res.append(Partition([u for u in tmp if u != 0]).conjugate()) + # This should never return the empty partition. + # So tmp should never be [0, ..., 0]. + while not tmp[-1]: + tmp.pop() + res.append(_Partitions(tmp)) + return res + + def add_horizontal_border_strip(self, k): + """ + Return a list of all the partitions that can be obtained by adding + a horizontal border strip of length ``k`` to ``self``. + + EXAMPLES:: + + sage: Partition([]).add_horizontal_border_strip(0) + [[]] + sage: Partition([3,2,1]).add_horizontal_border_strip(0) + [[3, 2, 1]] + sage: Partition([]).add_horizontal_border_strip(2) + [[2]] + sage: Partition([2,2]).add_horizontal_border_strip(2) + [[4, 2], [3, 2, 1], [2, 2, 2]] + sage: Partition([3,2,2]).add_horizontal_border_strip(2) + [[5, 2, 2], [4, 3, 2], [4, 2, 2, 1], [3, 3, 2, 1], [3, 2, 2, 2]] + """ + if k == 0: + return [self] + + L = self._list + res = [] + mapping = [0] + shelf = [k] + for i in range(len(L)-1): + val = L[i] - L[i+1] + if not val: + continue + mapping.append(i+1) + shelf.append(val) + + # add the last shelf + if L: + mapping.append(len(L)) + shelf.append(L[-1]) + + # list all of the positions for cells + # filling each self from the top to bottom + for iv in IntegerListsBackend_invlex(k, length=len(shelf), ceiling=shelf, check=False)._iter(): + tmp = self._list + [0] + for i, val in enumerate(iv): + if val: + tmp[mapping[i]] += val + # Only the last row is possibly empty + if not tmp[-1]: + tmp.pop() + res.append(_Partitions(tmp)) return res def remove_horizontal_border_strip(self, k): @@ -4839,7 +4873,7 @@ def atom(self): return res def k_atom(self, k): - """ + r""" Return a list of the standard tableaux of size ``self.size()`` whose ``k``-atom is equal to ``self``. @@ -4849,12 +4883,12 @@ def k_atom(self, k): sage: p.k_atom(1) [] sage: p.k_atom(3) - [[[1, 1, 1], [2, 2], [3]], - [[1, 1, 1, 2], [2], [3]], + [[[1, 1, 1, 2, 3], [2]], [[1, 1, 1, 3], [2, 2]], - [[1, 1, 1, 2, 3], [2]]] + [[1, 1, 1, 2], [2], [3]], + [[1, 1, 1], [2, 2], [3]]] sage: Partition([3,2,1]).k_atom(4) - [[[1, 1, 1], [2, 2], [3]], [[1, 1, 1, 3], [2, 2]]] + [[[1, 1, 1, 3], [2, 2]], [[1, 1, 1], [2, 2], [3]]] TESTS:: diff --git a/src/sage/combinat/superpartition.py b/src/sage/combinat/superpartition.py index ced73e8dadd..1981b6bae0e 100644 --- a/src/sage/combinat/superpartition.py +++ b/src/sage/combinat/superpartition.py @@ -678,19 +678,19 @@ def add_horizontal_border_strip_star(self, h) -> list: EXAMPLES:: sage: SuperPartition([[4,1],[3]]).add_horizontal_border_strip_star(3) - [[4, 1; 3, 3], - [4, 1; 4, 2], - [3, 1; 5, 2], - [4, 1; 5, 1], + [[3, 1; 7], + [4, 1; 6], + [3, 0; 6, 2], [3, 1; 6, 1], - [4, 0; 4, 3], - [3, 0; 5, 3], [4, 0; 5, 2], - [3, 0; 6, 2], - [4, 1; 6], - [3, 1; 7]] + [4, 1; 5, 1], + [3, 0; 5, 3], + [3, 1; 5, 2], + [4, 0; 4, 3], + [4, 1; 4, 2], + [4, 1; 3, 3]] sage: SuperPartition([[2,1],[3]]).add_horizontal_border_strip_star(2) - [[2, 1; 3, 2], [2, 1; 4, 1], [2, 0; 3, 3], [2, 0; 4, 2], [2, 1; 5]] + [[2, 1; 5], [2, 0; 4, 2], [2, 1; 4, 1], [2, 0; 3, 3], [2, 1; 3, 2]] """ sp1, circ_list = self.to_circled_diagram() nsp = [list(la) + [0] for la in sp1.add_horizontal_border_strip(h)] @@ -726,29 +726,29 @@ def add_horizontal_border_strip_star_bar(self, h) -> list: EXAMPLES:: sage: SuperPartition([[4,1],[5,4]]).add_horizontal_border_strip_star_bar(3) - [[4, 3; 5, 4, 1], - [4, 1; 5, 4, 3], - [4, 2; 5, 5, 1], - [4, 1; 5, 5, 2], + [[4, 1; 8, 4], + [4, 1; 7, 5], + [4, 2; 7, 4], + [4, 1; 7, 4, 1], + [4, 2; 6, 5], + [4, 1; 6, 5, 1], + [4, 3; 6, 4], [4, 2; 6, 4, 1], [4, 1; 6, 4, 2], - [4, 1; 6, 5, 1], - [4, 1; 7, 4, 1], [4, 3; 5, 5], - [4, 3; 6, 4], - [4, 2; 6, 5], - [4, 2; 7, 4], - [4, 1; 7, 5], - [4, 1; 8, 4]] + [4, 2; 5, 5, 1], + [4, 1; 5, 5, 2], + [4, 3; 5, 4, 1], + [4, 1; 5, 4, 3]] sage: SuperPartition([[3,1],[5]]).add_horizontal_border_strip_star_bar(2) - [[3, 2; 5, 1], - [3, 1; 5, 2], - [4, 1; 5, 1], + [[3, 1; 7], + [4, 1; 6], + [3, 2; 6], [3, 1; 6, 1], [4, 2; 5], - [3, 2; 6], - [4, 1; 6], - [3, 1; 7]] + [4, 1; 5, 1], + [3, 2; 5, 1], + [3, 1; 5, 2]] """ sp1, circ_list = self.to_circled_diagram() nsp = [list(la) + [0] for la in sp1.add_horizontal_border_strip(h)] diff --git a/src/sage/combinat/tableau.py b/src/sage/combinat/tableau.py index c45fb0f2e87..f5652f86733 100644 --- a/src/sage/combinat/tableau.py +++ b/src/sage/combinat/tableau.py @@ -3239,26 +3239,26 @@ def promotion_operator(self, i): sage: t = Tableau([[1,2],[3]]) sage: t.promotion_operator(1) - [[[1, 2], [3], [4]], [[1, 2], [3, 4]], [[1, 2, 4], [3]]] + [[[1, 2, 4], [3]], [[1, 2], [3, 4]], [[1, 2], [3], [4]]] sage: t.promotion_operator(2) - [[[1, 1], [2, 3], [4]], - [[1, 1, 2], [3], [4]], + [[[1, 1, 2, 4], [3]], [[1, 1, 4], [2, 3]], - [[1, 1, 2, 4], [3]]] + [[1, 1, 2], [3], [4]], + [[1, 1], [2, 3], [4]]] sage: Tableau([[1]]).promotion_operator(2) - [[[1, 1], [2]], [[1, 1, 2]]] + [[[1, 1, 2]], [[1, 1], [2]]] sage: Tableau([[1,1],[2]]).promotion_operator(3) - [[[1, 1, 1], [2, 2], [3]], - [[1, 1, 1, 2], [2], [3]], + [[[1, 1, 1, 2, 3], [2]], [[1, 1, 1, 3], [2, 2]], - [[1, 1, 1, 2, 3], [2]]] + [[1, 1, 1, 2], [2], [3]], + [[1, 1, 1], [2, 2], [3]]] The example from [LLM2003]_ p. 12:: sage: Tableau([[1,1],[2,2]]).promotion_operator(3) - [[[1, 1, 1], [2, 2], [3, 3]], + [[[1, 1, 1, 3, 3], [2, 2]], [[1, 1, 1, 3], [2, 2], [3]], - [[1, 1, 1, 3, 3], [2, 2]]] + [[1, 1, 1], [2, 2], [3, 3]]] TESTS::