SparseArrays.jl icon indicating copy to clipboard operation
SparseArrays.jl copied to clipboard

`nonzeros` for SubArray returns values of parent array

Open btmit opened this issue 2 years ago • 0 comments

nonzeros appears to be giving the wrong answer for this case. See example:

julia> a = sparse([1 0 2; 0 3 0]) 2×3 SparseMatrixCSC{Int64, Int64} with 3 stored entries: 1 ⋅ 2 ⋅ 3 ⋅

julia> b = view(a, :, 2:3) 2×2 view(::SparseMatrixCSC{Int64, Int64}, :, 2:3) with eltype Int64: 0 2 3 0

julia> nonzeros(b) 3-element Vector{Int64}: 1 3 2

It looks like there is a problem where the call is on the parent array, not the subarray. If I only look at a single column it seems to work

julia> c = view(a, :, 2) 2-element view(::SparseMatrixCSC{Int64, Int64}, :, 2) with eltype Int64: 0 3

julia> nonzeros(c) 1-element view(::Vector{Int64}, 2:2) with eltype Int64: 3

A few other observations:

  • rowvals seems to have the same problem as nonzeros
  • nnz and nzrange appear to work
  • tested in 1.8.5 and 1.9-rc1

btmit avatar Mar 28 '23 20:03 btmit