@@ -1795,7 +1795,9 @@ cdef class Matrix(sage.structure.element.Matrix):
1795
1795
return self .str()
1796
1796
1797
1797
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 ):
1799
1801
r """
1800
1802
Return a nice string representation of the matrix.
1801
1803
@@ -1920,6 +1922,21 @@ cdef class Matrix(sage.structure.element.Matrix):
1920
1922
[ 0.333333333333333 66.6666666666667 ]
1921
1923
[ -3.00000000000000 1.00000000000000e6 ]
1922
1924
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
+
1923
1940
TESTS:
1924
1941
1925
1942
Prior to :issue:`11544` this could take a full minute to run ( 2011) . ::
@@ -2062,6 +2079,12 @@ cdef class Matrix(sage.structure.element.Matrix):
2062
2079
2063
2080
# compute column widths
2064
2081
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
2065
2088
for x in entries:
2066
2089
# Override the usual representations with those specified
2067
2090
if callable (rep_mapping):
@@ -2074,39 +2097,83 @@ cdef class Matrix(sage.structure.element.Matrix):
2074
2097
else :
2075
2098
rep = repr (x)
2076
2099
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
2077
2106
2078
2107
width = max (map (len , S))
2108
+ left = []
2079
2109
rows = []
2110
+ right = []
2080
2111
2081
2112
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]))
2083
2114
2084
2115
# 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(" " )
2087
2127
s = " "
2088
2128
for c from 0 <= c < nc:
2089
2129
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]
2091
2134
elif c == 0 :
2092
2135
sep = " "
2093
2136
else :
2094
2137
sep = " "
2095
- entry = S[r * nc + c]
2138
+ entry = S[(r + top_count) * nc + c]
2096
2139
entry = " " * (width - len (entry)) + entry
2097
2140
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]
2099
2146
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
2105
2164
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)
2110
2177
2111
2178
if character_art:
2112
2179
breakpoints = []
0 commit comments