Skip to content

Commit f280766

Browse files
authored
make show(::SparseMatrixCSC) more consistent (#41150)
This now always uses `sparse(I, J, K, m, n)` for showing sparse matrices. The three-arg `show` used for REPL printing should still behave the same but has been refactored to share more code with the existing methods in Base. closes #41135
1 parent 3f27f88 commit f280766

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

stdlib/SparseArrays/src/sparsematrix.jl

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,37 @@ end
227227
Base.replace_in_print_matrix(A::AbstractSparseMatrix, i::Integer, j::Integer, s::AbstractString) =
228228
Base.isstored(A, i, j) ? s : Base.replace_with_centered_mark(s)
229229

230-
function Base.show(io::IO, ::MIME"text/plain", S::AbstractSparseMatrixCSC)
230+
function Base.array_summary(io::IO, S::AbstractSparseMatrixCSC, dims::Tuple{Vararg{Base.OneTo}})
231231
_checkbuffers(S)
232232
xnnz = nnz(S)
233233
m, n = size(S)
234234
print(io, m, "×", n, " ", typeof(S), " with ", xnnz, " stored ",
235235
xnnz == 1 ? "entry" : "entries")
236-
if !(m == 0 || n == 0)
237-
print(io, ":")
238-
show(IOContext(io, :typeinfo => eltype(S)), S)
236+
nothing
237+
end
238+
239+
# called by `show(io, MIME("text/plain"), ::AbstractSparseMatrixCSC)`
240+
function Base.print_array(io::IO, S::AbstractSparseMatrixCSC)
241+
if max(size(S)...) < 16
242+
Base.print_matrix(io, S)
243+
else
244+
_show_with_braille_patterns(io, S)
239245
end
240246
end
241247

242-
Base.show(io::IO, S::AbstractSparseMatrixCSC) = Base.show(convert(IOContext, io), S::AbstractSparseMatrixCSC)
248+
# always show matrices as `sparse(I, J, K)`
249+
function Base.show(io::IO, S::AbstractSparseMatrixCSC)
250+
_checkbuffers(S)
251+
# can't use `findnz`, because that expects all values not to be #undef
252+
I = rowvals(S)
253+
J = [col for col = 1 : size(S, 2) for k = getcolptr(S)[col] : (getcolptr(S)[col+1]-1)]
254+
K = nonzeros(S)
255+
m, n = size(S)
256+
print(io, "sparse(", I, ", ", J, ", ", K, ", ", m, ", ", n, ")")
257+
end
243258

244259
const brailleBlocks = UInt16['', '', '', '', '', '', '', '']
245-
function _show_with_braille_patterns(io::IOContext, S::AbstractSparseMatrixCSC)
260+
function _show_with_braille_patterns(io::IO, S::AbstractSparseMatrixCSC)
246261
m, n = size(S)
247262
(m == 0 || n == 0) && return show(io, MIME("text/plain"), S)
248263

@@ -303,18 +318,6 @@ function _show_with_braille_patterns(io::IOContext, S::AbstractSparseMatrixCSC)
303318
foreach(c -> print(io, Char(c)), @view brailleGrid[1:end-1])
304319
end
305320

306-
function Base.show(io::IOContext, S::AbstractSparseMatrixCSC)
307-
_checkbuffers(S)
308-
if max(size(S)...) < 16 && !(get(io, :compact, false)::Bool)
309-
ioc = IOContext(io, :compact => true)
310-
println(ioc)
311-
Base.print_matrix(ioc, S)
312-
return
313-
end
314-
println(io)
315-
_show_with_braille_patterns(io, S)
316-
end
317-
318321
## Reshape
319322

320323
function sparse_compute_reshaped_colptr_and_rowval!(colptrS::Vector{Ti}, rowvalS::Vector{Ti},

stdlib/SparseArrays/test/sparse.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,4 +3172,21 @@ end
31723172
end
31733173
end
31743174

3175+
@testset "issue #41135" begin
3176+
@test repr(SparseMatrixCSC([7;;])) == "sparse([1], [1], [7], 1, 1)"
3177+
3178+
m = SparseMatrixCSC([0 3; 4 0])
3179+
@test repr(m) == "sparse([2, 1], [1, 2], [4, 3], 2, 2)"
3180+
@test eval(Meta.parse(repr(m))) == m
3181+
@test summary(m) == "2×2 $SparseMatrixCSC{$Int, $Int} with 2 stored entries"
3182+
3183+
m = sprand(100, 100, .1)
3184+
@test occursin(r"^sparse\(\[.+\], \[.+\], \[.+\], \d+, \d+\)$", repr(m))
3185+
@test eval(Meta.parse(repr(m))) == m
3186+
3187+
m = sparse([85, 5, 38, 37, 59], [19, 72, 76, 98, 162], [0.8, 0.3, 0.2, 0.1, 0.5], 100, 200)
3188+
@test repr(m) == "sparse([85, 5, 38, 37, 59], [19, 72, 76, 98, 162], [0.8, 0.3, 0.2, 0.1, 0.5], 100, 200)"
3189+
@test eval(Meta.parse(repr(m))) == m
3190+
end
3191+
31753192
end # module

test/show.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,18 @@ Base.zero(x::T12960) = T12960()
728728
let
729729
A = sparse(1.0I, 3, 3)
730730
B = similar(A, T12960)
731-
@test sprint(show, B) == "\n #undef ⋅ ⋅ \n ⋅ #undef ⋅ \n ⋅ ⋅ #undef"
732-
@test sprint(print, B) == "\n #undef ⋅ ⋅ \n ⋅ #undef ⋅ \n ⋅ ⋅ #undef"
731+
@test repr(B) == "sparse([1, 2, 3], [1, 2, 3], $T12960[#undef, #undef, #undef], 3, 3)"
732+
@test occursin(
733+
"\n #undef ⋅ ⋅ \n ⋅ #undef ⋅ \n ⋅ ⋅ #undef",
734+
repr(MIME("text/plain"), B),
735+
)
736+
733737
B[1,2] = T12960()
734-
@test sprint(show, B) == "\n #undef T12960() ⋅ \n ⋅ #undef ⋅ \n ⋅ ⋅ #undef"
735-
@test sprint(print, B) == "\n #undef T12960() ⋅ \n ⋅ #undef ⋅ \n ⋅ ⋅ #undef"
738+
@test repr(B) == "sparse([1, 1, 2, 3], [1, 2, 2, 3], $T12960[#undef, $T12960(), #undef, #undef], 3, 3)"
739+
@test occursin(
740+
"\n #undef T12960() ⋅ \n ⋅ #undef ⋅ \n ⋅ ⋅ #undef",
741+
repr(MIME("text/plain"), B),
742+
)
736743
end
737744

738745
# issue #13127

0 commit comments

Comments
 (0)