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
36 changes: 21 additions & 15 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,26 @@ function Base.print_array(io::IO, S::AbstractSparseMatrixCSCInclAdjointAndTransp
end
end

"""
ColumnIndices(S::AbstractSparseMatrixCSC)

Return the column indices of the stored values in `S`.
This is an internal type that is used in displaying sparse matrices,
and is not a part of the public interface.
"""
struct ColumnIndices{Ti,S<:AbstractSparseMatrixCSC{<:Any,Ti}} <: AbstractVector{Ti}
arr :: S
end

size(C::ColumnIndices) = (nnz(C.arr),)
# returns the column index of the n-th non-zero value from the column pointer
@inline function getindex(C::ColumnIndices, i::Int)
@boundscheck checkbounds(C, i)
colptr = getcolptr(C.arr)
ind = searchsortedlast(colptr, i)
eltype(C)(ind)
end

# always show matrices as `sparse(I, J, K)`
function Base.show(io::IO, _S::AbstractSparseMatrixCSCInclAdjointAndTranspose)
_checkbuffers(_S)
Expand All @@ -358,21 +378,7 @@ function Base.show(io::IO, _S::AbstractSparseMatrixCSCInclAdjointAndTranspose)
print(io, "transpose(")
end
print(io, "sparse(", I, ", ")
if length(I) == 0
print(io, eltype(getcolptr(S)), "[]")
else
print(io, "[")
il = nnz(S) - 1
for col in 1:size(S, 2),
k in getcolptr(S)[col] : (getcolptr(S)[col+1]-1)
print(io, col)
if il > 0
print(io, ", ")
il -= 1
end
end
print(io, "]")
end
show(io, ColumnIndices(S))
print(io, ", ", K, ", ", m, ", ", n, ")")
if _S isa Adjoint || _S isa Transpose
print(io, ")")
Expand Down
9 changes: 9 additions & 0 deletions test/sparsematrix_constructors_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,15 @@ end
_show_with_braille_patterns(ioc, _filled_sparse(8, 16))
@test String(take!(io)) == "⎡⣿⣿⎤\n" *
"⎣⣿⣿⎦"

# respect IOContext while displaying J
I, J, V = shuffle(1:50), shuffle(1:50), [1:50;]
S = sparse(I, J, V)
I, J, V = I[sortperm(J)], sort(J), V[sortperm(J)]
@test repr(S) == "sparse($I, $J, $V, $(size(S,1)), $(size(S,2)))"
limctxt(x) = repr(x, context=:limit=>true)
expstr = "sparse($(limctxt(I)), $(limctxt(J)), $(limctxt(V)), $(size(S,1)), $(size(S,2)))"
@test limctxt(S) == expstr
end

@testset "issparse for specialized matrix types" begin
Expand Down