Skip to content

Commit

Permalink
Trac #34345: Method to get cells in horizontal/vertical border strip
Browse files Browse the repository at this point in the history
The methods `Partition.add_*_border_strip` where `*` is `horizontal` or
`vertical` return new partitions. This ticket proposes a method that
returns the indices of the cells of these new partitions which are not
in the original partition.

URL: https://trac.sagemath.org/34345
Reported by: tkarn
Ticket author(s): Trevor K. Karn
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Sep 20, 2022
2 parents 1ec500e + 218b548 commit 4ef170a
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions src/sage/combinat/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4749,6 +4749,117 @@ def add_horizontal_border_strip(self, k):
res.append(_Partitions(tmp))
return res

def vertical_border_strip_cells(self, k):
"""
Return a list of all the vertical border strips of length ``k``
which can be added to ``self``, where each horizontal border strip is
a ``generator`` of cells.
EXAMPLES::
sage: list(Partition([]).vertical_border_strip_cells(0))
[]
sage: list(Partition([3,2,1]).vertical_border_strip_cells(0))
[]
sage: list(Partition([]).vertical_border_strip_cells(2))
[[(0, 0), (1, 0)]]
sage: list(Partition([2,2]).vertical_border_strip_cells(2))
[[(0, 2), (1, 2)],
[(0, 2), (2, 0)],
[(2, 0), (3, 0)]]
sage: list(Partition([3,2,2]).vertical_border_strip_cells(2))
[[(0, 3), (1, 2)],
[(0, 3), (3, 0)],
[(1, 2), (2, 2)],
[(1, 2), (3, 0)],
[(3, 0), (4, 0)]]
"""
if k == 0:
return []

shelf = []
res = []
i = 0
ell = len(self._list)
while i < ell:
tmp = 1
while i+1 < ell and self._list[i] == self._list[i+1]:
tmp += 1
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
shelf.append(k)
# list all of the positions for cells
tmp = self._list + [0]*k
for iv in IntegerListsBackend_invlex(k, length=len(shelf),
ceiling=shelf,
check=False)._iter():
j = 0
current_strip = []
for t in range(len(iv)):
for _ in range(iv[t]):
current_strip.append((j, tmp[j]))
j += 1
j = sum(shelf[:t+1])
yield current_strip

def horizontal_border_strip_cells(self, k):
"""
Return a list of all the horizontal border strips of length ``k``
which can be added to ``self``, where each horizontal border strip is
a ``generator`` of cells.
EXAMPLES::
sage: list(Partition([]).horizontal_border_strip_cells(0))
[]
sage: list(Partition([3,2,1]).horizontal_border_strip_cells(0))
[]
sage: list(Partition([]).horizontal_border_strip_cells(2))
[[(0, 0), (0, 1)]]
sage: list(Partition([2,2]).horizontal_border_strip_cells(2))
[[(0, 2), (0, 3)], [(0, 2), (2, 0)], [(2, 0), (2, 1)]]
sage: list(Partition([3,2,2]).horizontal_border_strip_cells(2))
[[(0, 3), (0, 4)],
[(0, 3), (1, 2)],
[(0, 3), (3, 0)],
[(1, 2), (3, 0)],
[(3, 0), (3, 1)]]
"""
if k == 0:
return list()

L = self._list
res = []
shelf = [k] # the number of boxes which will fit in a row
mapping = [0] # a record of the rows
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])

L.append(0) # add room on the bottom
# 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 = []
# mapping[i] is the row index, val is the number of cells added to the row.
for i, val in enumerate(iv):
tmp.extend((mapping[i], L[mapping[i]] + j) for j in range(val))
yield tmp

def remove_horizontal_border_strip(self, k):
"""
Return the partitions obtained from ``self`` by removing an
Expand Down

0 comments on commit 4ef170a

Please sign in to comment.