Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,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 @@ -170,18 +170,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 @@ -193,7 +191,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 @@ -202,11 +200,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 @@ -1416,7 +1414,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 @@ -2105,6 +2105,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