SparseArrays.jl icon indicating copy to clipboard operation
SparseArrays.jl copied to clipboard

Can sparse `cat` apply only to numbers?

Open mcabbott opened this issue 4 years ago • 2 comments

Concatenation of a sparse and a dense array usually makes a sparse array. This fails completely when the resulting eltype cannot be used for a sparse array:

julia> hcat([1,2,3], sparse([1,0,0]))
3×2 SparseMatrixCSC{Int64, Int64} with 4 stored entries:
 1  1
 2  ⋅
 3  ⋅

julia> hcat(string.(1:3), sparse([1,0,0]))
ERROR: MethodError: no method matching zero(::String)

julia> invoke(hcat, Tuple{Vararg{AbstractVecOrMat}}, string.(1:3), sparse([1,0,0]))
3×2 Matrix{Any}:
 "1"  1
 "2"  0
 "3"  0

Should the sparse methods be restricted to T<:Number? Then I believe this would call the dense one. But perhaps Number is too narrow, e.g. using StaticArrays; hcat(sparse([SA[1,2], SA[0,0]]))?

Xref JuliaLang/julia#2326 for discussion of more elaborate promotion schemes.

mcabbott avatar Oct 14 '21 18:10 mcabbott

I believe this is done now

vtjnash avatar Dec 13 '23 15:12 vtjnash

Actually, no, I tested and it is fixed for _SparseConcatGroup but not _SparseVecConcatGroup

vtjnash avatar Dec 13 '23 15:12 vtjnash