Skip to content

Can sparse cat apply only to numbers? #71

Open
@mcabbott

Description

@mcabbott

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions