Skip to content

Commit

Permalink
fix typeinfo for sparse-matrix/vector display (#30589)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj authored and fredrikekre committed Jan 15, 2019
1 parent 384eb30 commit 7004841
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
18 changes: 8 additions & 10 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function Base.show(io::IO, ::MIME"text/plain", S::SparseMatrixCSC)
xnnz == 1 ? "entry" : "entries")
if xnnz != 0
print(io, ":")
show(io, S)
show(IOContext(io, :typeinfo => eltype(S)), S)
end
end

Expand All @@ -172,18 +172,16 @@ function Base.show(io::IOContext, S::SparseMatrixCSC)
return
end

iob = IOBuffer()
ioc = IOContext(iob, :compact => true)
ioc = IOContext(io, :compact => true)
pad = ndigits(max(S.m, S.n))

function _format_line(r, col)
pad = ndigits(max(S.m, S.n))
print(ioc, " [", rpad(S.rowval[r], pad), ", ", lpad(col, pad), "] = ")
print(ioc, "\n [", rpad(S.rowval[r], pad), ", ", lpad(col, pad), "] = ")
if isassigned(S.nzval, Int(r))
show(ioc, S.nzval[r])
else
print(ioc, Base.undef_ref_str)
end
return String(take!(iob))
end

if will_fit
Expand All @@ -195,7 +193,7 @@ function Base.show(io::IOContext, S::SparseMatrixCSC)
count = 0
for col = 1:S.n, r = nzrange(S, col)
count += 1
print(io, "\n", _format_line(r, col))
_format_line(r, col)
count == print_count && break
end

Expand All @@ -204,11 +202,11 @@ function Base.show(io::IOContext, S::SparseMatrixCSC)
# find the column to start printing in for the last print_count elements
nextcol = searchsortedfirst(S.colptr, nnz(S) - print_count + 1)
for r = (nnz(S) - print_count + 1) : (S.colptr[nextcol] - 1)
print(io, "\n", _format_line(r, nextcol - 1))
_format_line(r, nextcol - 1)
end
# print all of the remaining columns
for col = nextcol:S.n, r = nzrange(S, col)
print(io, "\n", _format_line(r, col))
_format_line(r, col)
end
end
end
Expand Down Expand Up @@ -1418,7 +1416,7 @@ argument specifies a random number generator, see [Random Numbers](@ref).
```jldoctest; setup = :(using Random; Random.seed!(1234))
julia> sprand(Bool, 2, 2, 0.5)
2×2 SparseMatrixCSC{Bool,Int64} with 1 stored entry:
[2, 2] = true
[2, 2] = 1
julia> sprand(Float64, 3, 0.75)
3-element SparseVector{Float64,Int64} with 1 stored entry:
Expand Down
8 changes: 4 additions & 4 deletions stdlib/SparseArrays/src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ julia> sparsevec(II, V, 8, -)
julia> sparsevec([1, 3, 1, 2, 2], [true, true, false, false, false])
3-element SparseVector{Bool,Int64} with 3 stored entries:
[1] = true
[2] = false
[3] = true
[1] = 1
[2] = 0
[3] = 1
```
"""
function sparsevec(I::AbstractVector{<:Integer}, V::AbstractVector, combine::Function)
Expand Down Expand Up @@ -834,7 +834,7 @@ function show(io::IO, ::MIME"text/plain", x::AbstractSparseVector)
" stored ", xnnz == 1 ? "entry" : "entries")
if xnnz != 0
println(io, ":")
show(io, x)
show(IOContext(io, :typeinfo => eltype(x)), x)
end
end

Expand Down
3 changes: 3 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,9 @@ end
show(ioc, MIME"text/plain"(), sparse(Int64[1,2,3,4,5,6], Int64[1,1,2,2,3,3], [1.0,2.0,3.0,4.0,5.0,6.0]))
@test String(take!(io)) == string("6×3 SparseArrays.SparseMatrixCSC{Float64,Int64} with 6 stored entries:\n [1, 1] = 1.0\n",
" [2, 1] = 2.0\n [3, 2] = 3.0\n [4, 2] = 4.0\n [5, 3] = 5.0\n [6, 3] = 6.0")

# issue #30589
@test repr("text/plain", sparse([true true])) == "1×2 SparseArrays.SparseMatrixCSC{Bool,$Int} with 2 stored entries:\n [1, 1] = 1\n [1, 2] = 1"
end

@testset "check buffers" for n in 1:3
Expand Down
3 changes: 3 additions & 0 deletions stdlib/SparseArrays/test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ end
@test occursin("1.25", string(spv_x1))
@test occursin("-0.75", string(spv_x1))
@test occursin("3.5", string(spv_x1))

# issue #30589
@test repr("text/plain", sparse([true])) == "1-element SparseArrays.SparseVector{Bool,$Int} with 1 stored entry:\n [1] = 1"
end

### Comparison helper to ensure exact equality with internal structure
Expand Down

2 comments on commit 7004841

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.