@@ -59,7 +59,8 @@ column going across the screen.
59
59
"""
60
60
function alignment (io:: IO , @nospecialize (X:: AbstractVecOrMat ),
61
61
rows:: AbstractVector{T} , cols:: AbstractVector{V} ,
62
- cols_if_complete:: Integer , cols_otherwise:: Integer , sep:: Integer ) where {T,V}
62
+ cols_if_complete:: Integer , cols_otherwise:: Integer , sep:: Integer ,
63
+ #= `size(X) may not infer, set this in caller =# ncols:: Integer = size (X, 2 )) where {T,V}
63
64
a = Tuple{T, V}[]
64
65
for j in cols # need to go down each column one at a time
65
66
l = r = 0
@@ -78,7 +79,7 @@ function alignment(io::IO, @nospecialize(X::AbstractVecOrMat),
78
79
break
79
80
end
80
81
end
81
- if 1 < length (a) < length ( axes (X, 2 ))
82
+ if 1 < length (a) < ncols
82
83
while sum (map (sum,a)) + sep* length (a) >= cols_otherwise
83
84
pop! (a)
84
85
end
@@ -95,7 +96,8 @@ is specified as string sep.
95
96
"""
96
97
function print_matrix_row (io:: IO ,
97
98
@nospecialize (X:: AbstractVecOrMat ), A:: Vector ,
98
- i:: Integer , cols:: AbstractVector , sep:: AbstractString )
99
+ i:: Integer , cols:: AbstractVector , sep:: AbstractString ,
100
+ #= `axes(X)` may not infer, set this in caller =# idxlast:: Integer = last (axes (X, 2 )))
99
101
for (k, j) = enumerate (cols)
100
102
k > length (A) && break
101
103
if isassigned (X,Int (i),Int (j)) # isassigned accepts only `Int` indices
@@ -114,7 +116,7 @@ function print_matrix_row(io::IO,
114
116
sx = undef_ref_str
115
117
end
116
118
l = repeat (" " , A[k][1 ]- a[1 ]) # pad on left and right as needed
117
- r = j == axes (X, 2 )[ end ] ? " " : repeat (" " , A[k][2 ]- a[2 ])
119
+ r = j == idxlast ? " " : repeat (" " , A[k][2 ]- a[2 ])
118
120
prettysx = replace_in_print_matrix (X,i,j,sx)
119
121
print (io, l, prettysx, r)
120
122
if k < length (A); print (io, sep); end
171
173
172
174
function _print_matrix (io, @nospecialize (X:: AbstractVecOrMat ), pre, sep, post, hdots, vdots, ddots, hmod, vmod, rowsA, colsA)
173
175
hmod, vmod = Int (hmod):: Int , Int (vmod):: Int
176
+ ncols, idxlast = length (colsA), last (colsA)
174
177
if ! (get (io, :limit , false ):: Bool )
175
178
screenheight = screenwidth = typemax (Int)
176
179
else
@@ -201,26 +204,26 @@ function _print_matrix(io, @nospecialize(X::AbstractVecOrMat), pre, sep, post, h
201
204
else
202
205
colsA = [colsA;]
203
206
end
204
- A = alignment (io, X, rowsA, colsA, screenwidth, screenwidth, sepsize)
207
+ A = alignment (io, X, rowsA, colsA, screenwidth, screenwidth, sepsize, ncols )
205
208
# Nine-slicing is accomplished using print_matrix_row repeatedly
206
209
if m <= screenheight # rows fit vertically on screen
207
210
if n <= length (A) # rows and cols fit so just print whole matrix in one piece
208
211
for i in rowsA
209
212
print (io, i == first (rowsA) ? pre : presp)
210
- print_matrix_row (io, X,A,i,colsA,sep)
213
+ print_matrix_row (io, X,A,i,colsA,sep,idxlast )
211
214
print (io, i == last (rowsA) ? post : postsp)
212
215
if i != last (rowsA); println (io); end
213
216
end
214
217
else # rows fit down screen but cols don't, so need horizontal ellipsis
215
218
c = div (screenwidth- length (hdots):: Int + 1 ,2 )+ 1 # what goes to right of ellipsis
216
- Ralign = reverse (alignment (io, X, rowsA, reverse (colsA), c, c, sepsize)) # alignments for right
219
+ Ralign = reverse (alignment (io, X, rowsA, reverse (colsA), c, c, sepsize, ncols )) # alignments for right
217
220
c = screenwidth - sum (map (sum,Ralign)) - (length (Ralign)- 1 )* sepsize - length (hdots):: Int
218
- Lalign = alignment (io, X, rowsA, colsA, c, c, sepsize) # alignments for left of ellipsis
221
+ Lalign = alignment (io, X, rowsA, colsA, c, c, sepsize, ncols ) # alignments for left of ellipsis
219
222
for i in rowsA
220
223
print (io, i == first (rowsA) ? pre : presp)
221
- print_matrix_row (io, X,Lalign,i,colsA[1 : length (Lalign)],sep)
224
+ print_matrix_row (io, X,Lalign,i,colsA[1 : length (Lalign)],sep,idxlast )
222
225
print (io, (i - first (rowsA)) % hmod == 0 ? hdots : repeat (" " , length (hdots):: Int ))
223
- print_matrix_row (io, X, Ralign, i, (n - length (Ralign)) .+ colsA, sep)
226
+ print_matrix_row (io, X, Ralign, i, (n - length (Ralign)) .+ colsA, sep, idxlast )
224
227
print (io, i == last (rowsA) ? post : postsp)
225
228
if i != last (rowsA); println (io); end
226
229
end
@@ -229,7 +232,7 @@ function _print_matrix(io, @nospecialize(X::AbstractVecOrMat), pre, sep, post, h
229
232
if n <= length (A) # rows don't fit, cols do, so only vertical ellipsis
230
233
for i in rowsA
231
234
print (io, i == first (rowsA) ? pre : presp)
232
- print_matrix_row (io, X,A,i,colsA,sep)
235
+ print_matrix_row (io, X,A,i,colsA,sep,idxlast )
233
236
print (io, i == last (rowsA) ? post : postsp)
234
237
if i != rowsA[end ] || i == rowsA[halfheight]; println (io); end
235
238
if i == rowsA[halfheight]
@@ -240,15 +243,15 @@ function _print_matrix(io, @nospecialize(X::AbstractVecOrMat), pre, sep, post, h
240
243
end
241
244
else # neither rows nor cols fit, so use all 3 kinds of dots
242
245
c = div (screenwidth- length (hdots):: Int + 1 ,2 )+ 1
243
- Ralign = reverse (alignment (io, X, rowsA, reverse (colsA), c, c, sepsize))
246
+ Ralign = reverse (alignment (io, X, rowsA, reverse (colsA), c, c, sepsize, ncols ))
244
247
c = screenwidth - sum (map (sum,Ralign)) - (length (Ralign)- 1 )* sepsize - length (hdots):: Int
245
- Lalign = alignment (io, X, rowsA, colsA, c, c, sepsize)
248
+ Lalign = alignment (io, X, rowsA, colsA, c, c, sepsize, ncols )
246
249
r = mod ((length (Ralign)- n+ 1 ),vmod) # where to put dots on right half
247
250
for i in rowsA
248
251
print (io, i == first (rowsA) ? pre : presp)
249
- print_matrix_row (io, X,Lalign,i,colsA[1 : length (Lalign)],sep)
252
+ print_matrix_row (io, X,Lalign,i,colsA[1 : length (Lalign)],sep,idxlast )
250
253
print (io, (i - first (rowsA)) % hmod == 0 ? hdots : repeat (" " , length (hdots):: Int ))
251
- print_matrix_row (io, X,Ralign,i,(n- length (Ralign)). + colsA,sep)
254
+ print_matrix_row (io, X,Ralign,i,(n- length (Ralign)). + colsA,sep,idxlast )
252
255
print (io, i == last (rowsA) ? post : postsp)
253
256
if i != rowsA[end ] || i == rowsA[halfheight]; println (io); end
254
257
if i == rowsA[halfheight]
0 commit comments