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

Fix indexing with `Vector{Block{1}}`

Open dlfivefifty opened this issue 4 years ago • 1 comments

Below gives an error. I think Vector{<:Block{1}} indexing should work like BlockRange, but this needs some thought.

julia> a = BlockArray(1:6, [1,2,2,1])
4-blocked 6-element BlockVector{Int64, Vector{UnitRange{Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}}}:
 1
 ─
 2
 3
 ─
 4
 5
 ─
 6

julia> a[Block.(2:3)]
2-blocked 4-element BlockArray{Int64,1}:
 2
 3
 ─
 4
 5

julia> a[collect(Block.(2:3))]
ERROR: MethodError: Cannot `convert` an object of type UnitRange{Int64} to an object of type Int64
Closest candidates are:
  convert(::Type{T}, ::T) where T<:Number at number.jl:6
  convert(::Type{T}, ::Number) where T<:Number at number.jl:7
  convert(::Type{T}, ::Base.TwicePrecision) where T<:Number at twiceprecision.jl:250
  ...
Stacktrace:
  [1] setindex!(A::Vector{Int64}, x::UnitRange{Int64}, i1::Int64)
    @ Base ./array.jl:839
  [2] copyto_unaliased!(deststyle::IndexLinear, dest::Vector{Int64}, srcstyle::IndexCartesian, src::SubArray{Int64, 1, BlockVector{Int64, Vector{UnitRange{Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}}}, Tuple{Vector{Block{1, Int64}}}, false})
    @ Base ./abstractarray.jl:976
  [3] copyto!
    @ ./abstractarray.jl:950 [inlined]
  [4] _copyto!
    @ ~/.julia/packages/ArrayLayouts/3lnt1/src/ArrayLayouts.jl:205 [inlined]
  [5] _copyto!
    @ ~/.julia/packages/ArrayLayouts/3lnt1/src/ArrayLayouts.jl:209 [inlined]
  [6] copyto!
    @ ~/.julia/packages/ArrayLayouts/3lnt1/src/ArrayLayouts.jl:218 [inlined]
  [7] copyto_axcheck!(dest::Vector{Int64}, src::SubArray{Int64, 1, BlockVector{Int64, Vector{UnitRange{Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}}}, Tuple{Vector{Block{1, Int64}}}, false})
    @ Base ./abstractarray.jl:1056
  [8] Array
    @ ./array.jl:540 [inlined]
  [9] Array
    @ ./boot.jl:472 [inlined]
 [10] sub_materialize
    @ ~/.julia/packages/ArrayLayouts/3lnt1/src/ArrayLayouts.jl:101 [inlined]
 [11] sub_materialize
    @ ~/.julia/packages/ArrayLayouts/3lnt1/src/ArrayLayouts.jl:102 [inlined]
 [12] sub_materialize
    @ ~/.julia/packages/ArrayLayouts/3lnt1/src/ArrayLayouts.jl:103 [inlined]
 [13] layout_getindex
    @ ~/.julia/packages/ArrayLayouts/3lnt1/src/ArrayLayouts.jl:109 [inlined]
 [14] getindex(A::BlockVector{Int64, Vector{UnitRange{Int64}}, Tuple{BlockedUnitRange{Vector{Int64}}}}, kr::Vector{Block{1, Int64}})
    @ ArrayLayouts ~/.julia/packages/ArrayLayouts/3lnt1/src/ArrayLayouts.jl:160
 [15] top-level scope
    @ REPL[5]:1

dlfivefifty avatar Aug 06 '21 23:08 dlfivefifty

This would be great to have! I have a use case for that (in my application I am permuting blocks of a block sparse array in order to merge them into bigger blocks).

mtfishman avatar Nov 08 '23 18:11 mtfishman