# Specialized reduction of sparse arrays

**URL:** <https://discourse.julialang.org/t/specialized-reduction-of-sparse-arrays/17807>\
**Category:** Internals & Design\
**Created:** [November 21, 2018, 9:21am UTC](https://discourse.julialang.org/t/specialized-reduction-of-sparse-arrays/17807 "2018-11-21T09:21:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [November 21, 2018, 9:21am UTC](https://discourse.julialang.org/t/specialized-reduction-of-sparse-arrays/17807/1 "2018-11-21T09:21:03Z")

</div>

Hi All,

I have created a specialized version of reduction for sparse arrays, as the default falls back to generic arrays, which is too slow.

The code is pretty simple as

```julia
function Base.reduce(::typeof(hcat), A::AbstractVector{T}) where {T<: SparseMatrixCSC{Tv, Ti} where {Tv, Ti}}
  length(A) <= 1 && return(A)
  Tv, Ti = eltype(A[1].nzval), eltype(A[1].colptr)
  coloffset = 0
  Is, Js, Vs = Vector{Vector{Ti}}(undef, length(A)), Vector{Vector{Ti}}(undef, length(A)), Vector{Vector{Tv}}(undef, length(A))
  for (i, a) in enumerate(A)
    (I, J, V) = findnz(a, coloffset)
    Is[i] = I
    Js[i] = J
    Vs[i] = V
    coloffset += a.n
  end
  sparse(reduce(vcat, Is), reduce(vcat, Js), reduce(vcat, Vs))
end

```

where I have slightly modified the default `findnz` function

```julia
function SparseArrays.findnz(S::SparseMatrixCSC{Tv,Ti}, coloffset) where {Tv,Ti}
    numnz = nnz(S)
    I = Vector{Ti}(undef, numnz)
    J = Vector{Ti}(undef, numnz)
    V = Vector{Tv}(undef, numnz)

    count = 1
    @inbounds for col = 1 : S.n, k = S.colptr[col] : (S.colptr[col+1]-1)
        I[count] = S.rowval[k]
        J[count] = col + coloffset
        V[count] = S.nzval[k]
        count += 1
    end

    return (I, J, V)
end

```

My question is, if it is of sufficient interest to be pushed to julia’s Sparse Arrays package. Also, if it possible to controbute only to SparseArrays and not to the whole Julia base.

Thanks for answer.
