Sparse Array Sum along axis

Does sum along a dimensional axis not work with sparse arrays?

a = sparse([1,2,3], [1,2,3], [ 1, 1, 2])
sum(a, 1)
ERROR: MethodError: objects of type SparseMatrixCSC{Int64,Int64} are not callable
Use square brackets [] for indexing an Array.
Stacktrace:
 [1] mapreduce_first(::SparseMatrixCSC{Int64,Int64}, ::Function, ::Int64) at ./reduce.jl:297
 [2] mapreduce(::SparseMatrixCSC{Int64,Int64}, ::Function, ::Int64) at ./reduce.jl:324
 [3] sum(::SparseMatrixCSC{Int64,Int64}, ::Int64) at ./reduce.jl:399
 [4] top-level scope at none:0

What do you expect the output to be? I find:

julia> sum(a[1,:])
1

Apparently, you need the dims keyword in the argument. E.g.

sum(a, dims = 1)
1×3 Array{Int64,2}:
 1  1  2
1 Like

FWIW

julia> sum(a[:])
4

I wasn’t sure which diagonal you were interested in.