Skip to content

Commit 18d7e97

Browse files
author
Matthias Koeppe
committed
Matrix.str: New parameters top_border, bottom_border, left_border, right_border
1 parent b693ea9 commit 18d7e97

File tree

1 file changed

+83
-16
lines changed

1 file changed

+83
-16
lines changed

src/sage/matrix/matrix0.pyx

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,9 @@ cdef class Matrix(sage.structure.element.Matrix):
17951795
return self.str()
17961796

17971797
def str(self, rep_mapping=None, zero=None, plus_one=None, minus_one=None,
1798-
*, unicode=False, shape=None, character_art=False):
1798+
*, unicode=False, shape=None, character_art=False,
1799+
left_border=None, right_border=None,
1800+
top_border=None, bottom_border=None):
17991801
r"""
18001802
Return a nice string representation of the matrix.
18011803
@@ -1920,6 +1922,21 @@ cdef class Matrix(sage.structure.element.Matrix):
19201922
[ 0.333333333333333 66.6666666666667]
19211923
[ -3.00000000000000 1.00000000000000e6]
19221924
1925+
Matrices with borders::
1926+
1927+
sage: M = matrix([[1,2,3], [4,5,6], [7,8,9]])
1928+
sage: M.subdivide(None, 2)
1929+
sage: print(M.str(unicode=True,
1930+
....: top_border=['ab', 'cde', 'f'],
1931+
....: bottom_border=['*', '', ''],
1932+
....: left_border=[1, 10, 100],
1933+
....: right_border=['', ' <', '']))
1934+
ab cde f
1935+
ab⎛ 1 2│ 3⎞
1936+
cde⎜ 4 5│ 6⎟ <
1937+
f⎝ 7 8│ 9⎠
1938+
*
1939+
19231940
TESTS:
19241941
19251942
Prior to :issue:`11544` this could take a full minute to run (2011). ::
@@ -2062,6 +2079,12 @@ cdef class Matrix(sage.structure.element.Matrix):
20622079

20632080
# compute column widths
20642081
S = []
2082+
if top_border is not None:
2083+
for x in top_border:
2084+
S.append(str(x))
2085+
top_count = 1
2086+
else:
2087+
top_count = 0
20652088
for x in entries:
20662089
# Override the usual representations with those specified
20672090
if callable(rep_mapping):
@@ -2074,39 +2097,83 @@ cdef class Matrix(sage.structure.element.Matrix):
20742097
else:
20752098
rep = repr(x)
20762099
S.append(rep)
2100+
if bottom_border is not None:
2101+
for x in bottom_border:
2102+
S.append(str(x))
2103+
bottom_count = 1
2104+
else:
2105+
bottom_count = 0
20772106

20782107
width = max(map(len, S))
2108+
left = []
20792109
rows = []
2110+
right = []
20802111

20812112
hline = cl.join(hl * ((width + 1)*(b - a) - 1)
2082-
for a,b in zip([0] + col_divs, col_divs + [nc]))
2113+
for a,b in zip([0] + col_divs, col_divs + [nc]))
20832114

20842115
# compute rows
2085-
for r from 0 <= r < nr:
2086-
rows += [hline] * row_divs.count(r)
2116+
for r in range(-top_count, nr + bottom_count):
2117+
if 0 <= r < nr:
2118+
n = row_divs.count(r)
2119+
if n:
2120+
left.extend([""] * n)
2121+
rows.extend([hline] * n)
2122+
right.extend([""] * n)
2123+
if top_border is not None and 0 <= r < nr:
2124+
left.append(str(top_border[r]))
2125+
else:
2126+
left.append("")
20872127
s = ""
20882128
for c from 0 <= c < nc:
20892129
if col_div_counts[c]:
2090-
sep = vl * col_div_counts[c]
2130+
if 0 <= r < nr:
2131+
sep = vl * col_div_counts[c]
2132+
else:
2133+
sep = " " * col_div_counts[c]
20912134
elif c == 0:
20922135
sep = ""
20932136
else:
20942137
sep = " "
2095-
entry = S[r * nc + c]
2138+
entry = S[(r + top_count) * nc + c]
20962139
entry = " " * (width - len(entry)) + entry
20972140
s = s + sep + entry
2098-
s = s + vl * col_div_counts[nc]
2141+
else:
2142+
if 0 <= r < nr:
2143+
s = s + vl * col_div_counts[nc]
2144+
else:
2145+
s = s + " " * col_div_counts[nc]
20992146
rows.append(s)
2100-
rows += [hline] * row_divs.count(nr)
2101-
2102-
last_row = len(rows) - 1
2103-
if last_row == 0:
2104-
rows[0] = slb + rows[0] + srb
2147+
if bottom_border is not None and 0 <= r < nr:
2148+
right.append(str(right_border[r]))
2149+
else:
2150+
right.append("")
2151+
else:
2152+
if nr == nr + bottom_count:
2153+
n = row_divs.count(nr)
2154+
if n:
2155+
left.extend([""] * n)
2156+
rows.extend([hline] * n)
2157+
right.extend([""] * n)
2158+
2159+
# left and right brackets
2160+
for i in range(top_count):
2161+
rows[i] = " "*len(slb) + rows[i] + " "*len(srb)
2162+
if len(rows) == top_count + 1 + bottom_count:
2163+
rows[top_count] = slb + rows[top_count] + srb
21052164
else:
2106-
rows[0] = tlb + rows[0] + trb
2107-
for r from 1 <= r < last_row:
2108-
rows[r] = mlb + rows[r] + mrb
2109-
rows[last_row] = blb + rows[last_row] + brb
2165+
rows[top_count] = tlb + rows[top_count] + trb
2166+
for i in range(top_count + 1, len(rows) - bottom_count - 1):
2167+
rows[i] = mlb + rows[i] + mrb
2168+
rows[-1 - bottom_count] = blb + rows[-1 - bottom_count] + brb
2169+
for i in range(bottom_count):
2170+
rows[-1 - i] = " "*len(slb) + rows[-1 - i] + " "*len(srb)
2171+
2172+
# left and right border
2173+
left_width = max(len(s) for s in left)
2174+
right_width = max(len(s) for s in right)
2175+
for i in range(len(rows)):
2176+
rows[i] = left[i].rjust(left_width) + rows[i] + right[i].rjust(right_width)
21102177

21112178
if character_art:
21122179
breakpoints = []

0 commit comments

Comments
 (0)