From 97ca5fba3a69d301ccb7355fd39fb1c90e222a1b Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Mon, 14 Aug 2023 17:15:40 +0400 Subject: [PATCH 1/2] Respect IOContext in printing column indices --- src/sparsematrix.jl | 30 +++++++++++----------- test/sparsematrix_constructors_indexing.jl | 8 ++++++ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/sparsematrix.jl b/src/sparsematrix.jl index 10bd525a..44ec871f 100644 --- a/src/sparsematrix.jl +++ b/src/sparsematrix.jl @@ -344,6 +344,20 @@ function Base.print_array(io::IO, S::AbstractSparseMatrixCSCInclAdjointAndTransp end end +# struct to generate the column indices of the values +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) @@ -358,21 +372,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, ")") diff --git a/test/sparsematrix_constructors_indexing.jl b/test/sparsematrix_constructors_indexing.jl index 1ef19a1d..9f8c6888 100644 --- a/test/sparsematrix_constructors_indexing.jl +++ b/test/sparsematrix_constructors_indexing.jl @@ -1574,6 +1574,14 @@ end _show_with_braille_patterns(ioc, _filled_sparse(8, 16)) @test String(take!(io)) == "⎡⣿⣿⎤\n" * "⎣⣿⣿⎦" + + I, J, V = shuffle(1:100), shuffle(1:100), [1:100;] + 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 From 33a1a55cc015d5a403fecb34c260b8497fe87b6d Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 24 Aug 2023 12:01:21 +0400 Subject: [PATCH 2/2] Add docstring to ColumnIndices --- src/sparsematrix.jl | 8 +++++++- test/sparsematrix_constructors_indexing.jl | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/sparsematrix.jl b/src/sparsematrix.jl index 44ec871f..1703c870 100644 --- a/src/sparsematrix.jl +++ b/src/sparsematrix.jl @@ -344,7 +344,13 @@ function Base.print_array(io::IO, S::AbstractSparseMatrixCSCInclAdjointAndTransp end end -# struct to generate the column indices of the values +""" + 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 diff --git a/test/sparsematrix_constructors_indexing.jl b/test/sparsematrix_constructors_indexing.jl index 9f8c6888..5aad5628 100644 --- a/test/sparsematrix_constructors_indexing.jl +++ b/test/sparsematrix_constructors_indexing.jl @@ -1575,7 +1575,8 @@ end @test String(take!(io)) == "⎡⣿⣿⎤\n" * "⎣⣿⣿⎦" - I, J, V = shuffle(1:100), shuffle(1:100), [1:100;] + # 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)))"