# Convenience type for assembling sparse matrices

**URL:** <https://discourse.julialang.org/t/convenience-type-for-assembling-sparse-matrices/15262>\
**Category:** Internals & Design\
**Created:** [September 21, 2018, 2:58am UTC](https://discourse.julialang.org/t/convenience-type-for-assembling-sparse-matrices/15262 "2018-09-21T02:58:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [September 21, 2018, 2:58am UTC](https://discourse.julialang.org/t/convenience-type-for-assembling-sparse-matrices/15262/1 "2018-09-21T02:58:08Z")

</div>

I find the `IJV`-approach to assembling sparse matrices quite cumbersome as it forces me to write three lines to set a single entry `A[i,j] = v`:

```julia
push!(I,i)
push!(J,j)
push!(V,v)

```

The situation becomes even worse if instead of a single entry I want to add a whole block of entries `A[i1:i2, j1:j2] = v` since then the entries of `i1:i2` and `j1:j2` have to be repeated.

I think it would be nice to have a convenience `SparseMatrixAssembler` type which wraps the `IJV` vectors and allows to use `setindex!` to achieve the above. Once you are done assembling, you simply call `SparseMatrixCSC(assembler[,m,n])` to convert to a proper sparse matrix. For example:

```julia
julia> A = SparseMatrixAssembler();

julia> A[2,2] = π;

julia> SparseMatrixCSC(A,3,3)
3×3 SparseMatrixCSC{Float64,Int64} with 1 stored entry:
  [2, 2] = 3.14159

```

A rough prototype implementation can be found at [GitHub - ettersi/SparseMatrixAssemblers.jl: Assemble sparse matrices using block assignment](https://github.com/ettersi/SparseMatrixAssemblers.jl). Any chance a more carefully designed version of this could make it into the standard `SparseArrays` package?

---

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [February 14, 2023, 1:45pm UTC](https://discourse.julialang.org/t/convenience-type-for-assembling-sparse-matrices/15262/2 "2023-02-14T13:45:50Z")

</div>

I’m replying just to say that I have the same issue in 2023. Scipy has the `coo` format which serves the same purpose; Julia is lagging behind here.

Another advantage of this solution is that it would make easier to switch between assembling a dense and a sparse matrix, since the same syntax would work in both cases.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 18, 2023, 10:14am UTC](https://discourse.julialang.org/t/convenience-type-for-assembling-sparse-matrices/15262/3 "2023-02-18T10:14:59Z")

</div>

a) I think `SparseArrayInitializer` or something of that nature would be more consistent with existing names  
b) Is there any particular reason to restrict this to matrices? If you built it to be generalizable to other-dimensional-arrays, how would you do it differently?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 20, 2023, 11:41pm UTC](https://discourse.julialang.org/t/convenience-type-for-assembling-sparse-matrices/15262/4 "2023-02-20T23:41:10Z")

</div>

Unlike SciPy, there’s no reason for this to be built in, and adding more baggage to the SparseArray stdlib doesn’t make sense, so if you’re waiting for this to be solved in base Julia, it won’t. If you need such a type why not use the implementation above or something based on it?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 21, 2023, 1:33am UTC](https://discourse.julialang.org/t/convenience-type-for-assembling-sparse-matrices/15262/5 "2023-02-21T01:33:46Z")

</div>

An oldie: [FinEtools.jl/AssemblyModule.jl at main · PetrKryslUCSD/FinEtools.jl · GitHub](https://github.com/PetrKryslUCSD/FinEtools.jl/blob/main/src/AssemblyModule.jl)

And a “more Julian” solution [Elfel.jl/Assemblers.jl at master · PetrKryslUCSD/Elfel.jl · GitHub](https://github.com/PetrKryslUCSD/Elfel.jl/blob/master/src/Assemblers.jl)

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [February 21, 2023, 11:36pm UTC](https://discourse.julialang.org/t/convenience-type-for-assembling-sparse-matrices/15262/6 "2023-02-21T23:36:00Z")

</div>

I’ll confess that I find the `sparse(i,j,v,m,n)` constructor rather non-ergonomic. It’s adopted verbatim from MATLAB (where almost anything other than `Array{Float64,N}` is a pain to use) and I consider it quite clumsy in Julia.

I would have found a constructor based on an iterable (e.g., `Vector`) of `Pair{CartesianIndex{N},T}` to be much easier to use, for example. Then you could add entries by `push!`ing onto a single vector instead of 3.

Sometimes I grit my teeth and build the `i`/`j`/`v` vectors separately. Other times I end up building my single vector (with eltype `Tuple{Int,Int,T}` or the above `Pair`) and then decompose it into the 3 to pass to `sparse`. As suggested above, this isn’t a very hard transformation to do. It’s also something easily done by a package without the need to modify `SparseArrays`, so perhaps we accept this ship as “sailed”.
