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

**URL:** <https://discourse.julialang.org/t/sparsematrixcsc-as-a-general-sparse-container-setindex-not-working-for-sparsematrixcsc-with-vector-elements-v0-7/14529>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 4, 2018, 10:28am UTC](https://discourse.julialang.org/t/sparsematrixcsc-as-a-general-sparse-container-setindex-not-working-for-sparsematrixcsc-with-vector-elements-v0-7/14529 "2018-09-04T10:28:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HTSykora](https://avatars.discourse-cdn.com/v4/letter/h/aca169/32.png) [@HTSykora](https://discourse.julialang.org/u/HTSykora)\
**Post date:** [September 4, 2018, 10:28am UTC](https://discourse.julialang.org/t/sparsematrixcsc-as-a-general-sparse-container-setindex-not-working-for-sparsematrixcsc-with-vector-elements-v0-7/14529/1 "2018-09-04T10:28:51Z")

</div>

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
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
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?

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [September 4, 2018, 10:44am UTC](https://discourse.julialang.org/t/sparsematrixcsc-as-a-general-sparse-container-setindex-not-working-for-sparsematrixcsc-with-vector-elements-v0-7/14529/2 "2018-09-04T10:44:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 4, 2018, 10:58am UTC](https://discourse.julialang.org/t/sparsematrixcsc-as-a-general-sparse-container-setindex-not-working-for-sparsematrixcsc-with-vector-elements-v0-7/14529/3 "2018-09-04T10:58:38Z")

</div>

I don’t think it’s intentional.

> <https://github.com/JuliaLang/julia/blob/c8450d862f0e2653011f68118daecfe12b398c90/stdlib/SparseArrays/src/sparsematrix.jl#L2538>

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](https://github.com/JuliaLang/julia/blob/3ab56f19a81a1d658a02750b48abbd63950ef248/base/array.jl#L38) `const AbstractVecOrMat{T} = Union{AbstractVector{T}, AbstractMatrix{T}}`. I think an issue is appropriate.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 4, 2018, 11:20am UTC](https://discourse.julialang.org/t/sparsematrixcsc-as-a-general-sparse-container-setindex-not-working-for-sparsematrixcsc-with-vector-elements-v0-7/14529/4 "2018-09-04T11:20:26Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![HTSykora](https://avatars.discourse-cdn.com/v4/letter/h/aca169/32.png) [@HTSykora](https://discourse.julialang.org/u/HTSykora)\
**Post date:** [September 4, 2018, 2:39pm UTC](https://discourse.julialang.org/t/sparsematrixcsc-as-a-general-sparse-container-setindex-not-working-for-sparsematrixcsc-with-vector-elements-v0-7/14529/5 "2018-09-04T14:39:56Z")

</div>

Of course, I’m on it

---

<div class="post-metadata">

**Author:** ![danspielman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danspielman/32/4708_2.png) [@danspielman](https://discourse.julialang.org/u/danspielman)\
**Post date:** [November 25, 2018, 10:01pm UTC](https://discourse.julialang.org/t/sparsematrixcsc-as-a-general-sparse-container-setindex-not-working-for-sparsematrixcsc-with-vector-elements-v0-7/14529/6 "2018-11-25T22:01:33Z")

</div>

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.
