Missing method to slice sparse matrices?

linearalgebra

#1

In Julia 1.0.2,

julia> D = convert(SparseMatrixCSC{Float32,Int32}, spdiagm(0 => rand(5)))

julia> D[2:3,:]
ERROR: MethodError: no method matching getindex_traverse_col(::UnitRange{Int64}, ::Int32, ::Int64)
Closest candidates are:
  getindex_traverse_col(::AbstractUnitRange, ::Int64, ::Int64) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:1971
  getindex_traverse_col(::StepRange, ::Int64, ::Int64) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:1972
Stacktrace:
 [1] macro expansion at ./simdloop.jl:72 [inlined]
 [2] getindex(::SparseMatrixCSC{Float32,Int32}, ::UnitRange{Int64}, ::UnitRange{Int64}) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:1995
 [3] getindex(::SparseMatrixCSC{Float32,Int32}, ::UnitRange{Int64}, ::Colon) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/SparseArrays/src/sparsematrix.jl:1934
 [4] top-level scope at none:0

This seems to fix it:

julia> import SparseArrays.getindex_traverse_col

julia> getindex_traverse_col(::AbstractUnitRange, lo::Ti, hi::Tj) where {Ti<:Integer,Tj<:Integer} = lo:hi

Is that the way to go?


#2

This seems like a bug to me. I would suggest making the change you suggested to getindex_traverse_col and making a PR. Or filing an issue.


#3

Yup, that looks like the right fix. Want to submit a pull request with this change? note that you don’t need the type parameters — you can just do ::Integer here:

getindex_traverse_col(::AbstractUnitRange, lo::Integer, hi::Integer) = ...

It’d also be good to fix the ::StepRange method similarly.


#4

Thanks. Here we go: https://github.com/JuliaLang/julia/pull/30319