Skip to content

Commit 5cddf02

Browse files
abraunstStefanKarpinski
authored andcommitted
sparse matrices: display with less whitespace (#31461)
1 parent 27da21a commit 5cddf02

File tree

3 files changed

+37
-42
lines changed

3 files changed

+37
-42
lines changed

docs/src/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ julia> I = [1, 4, 3, 5]; J = [4, 7, 18, 9]; V = [1, 2, -5, 3];
104104
105105
julia> S = sparse(I,J,V)
106106
5×18 SparseMatrixCSC{Int64,Int64} with 4 stored entries:
107-
[1 , 4] = 1
108-
[4 , 7] = 2
109-
[5 , 9] = 3
110-
[3 , 18] = -5
107+
[1, 4] = 1
108+
[4, 7] = 2
109+
[5, 9] = 3
110+
[3, 18] = -5
111111
112112
julia> R = sparsevec(I,V)
113113
5-element SparseVector{Int64,Int64} with 4 stored entries:

src/sparsematrix.jl

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -172,55 +172,50 @@ end
172172

173173
Base.show(io::IO, S::SparseMatrixCSC) = Base.show(convert(IOContext, io), S::SparseMatrixCSC)
174174
function Base.show(io::IOContext, S::SparseMatrixCSC)
175-
if nnz(S) == 0
176-
return show(io, MIME("text/plain"), S)
177-
end
178-
limit::Bool = get(io, :limit, false)
179-
rows = displaysize(io)[1] - 4 # -4 from [Prompt, header, newline after elements, new prompt]
180-
will_fit = !limit || rows >= nnz(S) # Will the whole matrix fit when printed?
181-
182-
if rows <= 2 && !will_fit
183-
print(io, "\n \u22ee")
184-
return
185-
end
175+
nnz(S) == 0 && return show(io, MIME("text/plain"), S)
186176

187177
ioc = IOContext(io, :compact => true)
188-
pad = ndigits(max(S.m, S.n))
189-
190-
function _format_line(r, col)
191-
print(ioc, "\n [", rpad(S.rowval[r], pad), ", ", lpad(col, pad), "] = ")
178+
function _format_line(r, col, padr, padc)
179+
print(ioc, "\n [", rpad(S.rowval[r], padr), ", ", lpad(col, padc), "] = ")
192180
if isassigned(S.nzval, Int(r))
193181
show(ioc, S.nzval[r])
194182
else
195183
print(ioc, Base.undef_ref_str)
196184
end
197185
end
198186

199-
if will_fit
200-
print_count = nnz(S)
201-
else
202-
print_count = div(rows-1, 2)
203-
end
204-
205-
count = 0
206-
for col = 1:S.n, r = nzrange(S, col)
207-
count += 1
208-
_format_line(r, col)
209-
count == print_count && break
187+
function _get_cols(from, to)
188+
idx = eltype(S.colptr)[]
189+
c = searchsortedlast(S.colptr, from)
190+
for i = from:to
191+
while i == S.colptr[c+1]
192+
c +=1
193+
end
194+
push!(idx, c)
195+
end
196+
idx
210197
end
211198

212-
if !will_fit
199+
rows = displaysize(io)[1] - 4 # -4 from [Prompt, header, newline after elements, new prompt]
200+
if !get(io, :limit, false) || rows >= nnz(S) # Will the whole matrix fit when printed?
201+
cols = _get_cols(1, nnz(S))
202+
padr, padc = ndigits.((maximum(S.rowval[1:nnz(S)]), cols[end]))
203+
_format_line.(1:nnz(S), cols, padr, padc)
204+
else
205+
if rows <= 2
206+
print(io, "\n \u22ee")
207+
return
208+
end
209+
s1, e1 = 1, div(rows - 1, 2) # -1 accounts for \vdots
210+
s2, e2 = nnz(S) - (rows - 1 - e1) + 1, nnz(S)
211+
cols1, cols2 = _get_cols(s1, e1), _get_cols(s2, e2)
212+
padr = ndigits(max(maximum(S.rowval[s1:e1]), maximum(S.rowval[s2:e2])))
213+
padc = ndigits(cols2[end])
214+
_format_line.(s1:e1, cols1, padr, padc)
213215
print(io, "\n \u22ee")
214-
# find the column to start printing in for the last print_count elements
215-
nextcol = searchsortedfirst(S.colptr, nnz(S) - print_count + 1)
216-
for r = (nnz(S) - print_count + 1) : (S.colptr[nextcol] - 1)
217-
_format_line(r, nextcol - 1)
218-
end
219-
# print all of the remaining columns
220-
for col = nextcol:S.n, r = nzrange(S, col)
221-
_format_line(r, col)
222-
end
216+
_format_line.(s2:e2, cols2, padr, padc)
223217
end
218+
return
224219
end
225220

226221
## Reshape

test/sparse.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,11 +2088,11 @@ end
20882088

20892089
show(ioc, MIME"text/plain"(), sparse(Int64[1,2,3,4,5], Int64[1,1,2,2,3], [1.0,2.0,3.0,4.0,5.0]))
20902090
@test String(take!(io)) == string("5×3 SparseArrays.SparseMatrixCSC{Float64,Int64} with 5 stored entries:\n [1, 1]",
2091-
" = 1.0\n\n [5, 3] = 5.0")
2091+
" = 1.0\n\n [4, 2] = 4.0\n [5, 3] = 5.0")
20922092

20932093
show(ioc, MIME"text/plain"(), sparse(fill(1.,5,3)))
20942094
@test String(take!(io)) == string("5×3 SparseArrays.SparseMatrixCSC{Float64,$Int} with 15 stored entries:\n [1, 1]",
2095-
" = 1.0\n\n [5, 3] = 1.0")
2095+
" = 1.0\n\n [4, 3] = 1.0\n [5, 3] = 1.0")
20962096

20972097
# odd number of rows
20982098
ioc = IOContext(io, :displaysize => (9, 80), :limit => true)

0 commit comments

Comments
 (0)