SparseMatrixCSC as a general sparse container: Setindex!(...) not working for SparseMatrixCSC with vector elements (v0.7)

In my application I have a sparse matrix, but every element of this matrix is a discretised function. If I try to use SparseMatrixCSC{Array{Float64,1},Int64} as the container, but setindex!(...) in 0.7 throws a stackoverflow:

julia> sa=sparse([1],[1],[Vector{Float64}(undef,3)],3,3)
3×3 SparseMatrixCSC{Array{Float64,1},Int64} with 1 stored entry:
  [1, 1]  =  [6.93279e-310, 6.93279e-310, 6.93279e-310]

julia> sa[1,1] = [1.0,2.0,3.0]
StackOverflowError:
Stacktrace:
 [1] setindex!(::SparseMatrixCSC{Array{Int64,1},Int64}, ::Array{Int64,1}, ::Int64, ::Int64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v0.7/SparseArrays/src/sparsematrix.jl:2540 (repeats 80000 times)

This can be solved by using broadcasted setindex!(...), but it does not solve the problem adding new elements to the array:

julia> sa[1,1] .= [1.0,2.0,3.0]
3-element Array{Float64,1}:
 1.0
 2.0
 3.0

julia> sa[2,2] = [1.0,2.0,3.0]
StackOverflowError:
Stacktrace:
 [1] setindex!(::SparseMatrixCSC{Array{Float64,1},Int64}, ::Array{Float64,1}, ::Int64, ::Int64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v0.7/SparseArrays/src/sparsematrix.jl:2540 (repeats 80000 times)

I can work around it using custom structures, but this has worked in 0.6.x. Is it intentional, or a bug?

Wow, good catch. I’m pretty sure this is not intentional. I would wait a bit for some pro feedback, and if it’s not clarified I’d definitely file an issue

I don’t think it’s intentional.

That line is probably calling itself again even after a conversion if B is already a Tv, since what you put in is in my opinion always going to be <:AbstractVecOrMat, which in itself is just const AbstractVecOrMat{T} = Union{AbstractVector{T}, AbstractMatrix{T}}. I think an issue is appropriate.

2 Likes

@HTSykora please also reference this discourse thread in the issue - I don’t have access to my github account right now, so I can’t add that reference. In particular the snippet from the source I linked is probably going to be useful.

Of course, I’m on it

Is there any word on when a fix for this will appear in Julia?
I am facing the same problem, but with a sparse array of StaticArrays, so the broadcast solution will not work for me.