JuliaArrays / JuliaArrays/StaticArrays.jl

Indexing a StructVector with an MVector fails

Open
#1,109 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
844
Forks
159
Avg merge
3d 21h
Merged PRs (30d)
3

Description

Why does indexing a StructVector with an MVector fail? E.g., I have

meas_idx

 7-element MVector{7, Int64} with indices SOneTo(7):
  809
  267
 1721
 1165
  643
  716
 1808

and


pc[meas_idx]
ArgumentError: invalid index: StaticArrays.StaticIndexing{MVector{7, Int64}}([809, 267, 1721, 1165, 643, 716, 1808]) of type StaticArrays.StaticIndexing{MVector{7, Int64}}

Stacktrace:
  [1] to_index(i::StaticArrays.StaticIndexing{MVector{7, Int64}})
    @ Base ./indices.jl:300
  [2] to_index(A::Vector{SVector{3, Float64}}, i::StaticArrays.StaticIndexing{MVector{7, Int64}})
    @ Base ./indices.jl:277
  [3] to_indices
    @ ./indices.jl:333 [inlined]
  [4] to_indices
    @ ./indices.jl:325 [inlined]
  [5] getindex
    @ ./abstractarray.jl:1241 [inlined]
  [6] (::StructArrays.var"#41#42"{Tuple{StaticArrays.StaticIndexing{MVector{7, Int64}}}})(v::Vector{SVector{3, Float64}})
    @ StructArrays ~/.julia/packages/StructArrays/wkp4X/src/structarray.jl:358
  [7] map
    @ ./tuple.jl:222 [inlined]
  [8] map(::Function, ::NamedTuple{(:first, :second), Tuple{Vector{SVector{3, Float64}}, Vector{SVector{3, Float64}}}})
    @ Base ./namedtuple.jl:219
  [9] _getindex
    @ ~/.julia/packages/StructArrays/wkp4X/src/structarray.jl:358 [inlined]
 [10] getindex(x::StructArrays.StructVector{NamedTuple{(:first, :second), Tuple{SVector{3, Float64}, SVector{3, Float64}}}, NamedTuple{(:first, :second), Tuple{Vector{SVector{3, Float64}}, Vector{SVector{3, Float64}}}}, Int64}, I::MVector{7, Int64})
    @ StructArrays ~/.julia/packages/StructArrays/wkp4X/src/structarray.jl:348
 [11] top-level scope
    @ In[60]:1
 [12] eval
    @ ./boot.jl:368 [inlined]
 [13] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1428

but if I do

 idx = zeros(Int64,7)

 7-element Vector{Int64}:
 0
 0
 0
 0
 0
 0
 0

 idx.=meas_idx

 7-element Vector{Int64}:
  809
  267
 1721
 1165
  643
  716
 1808

pc[idx]

 7-element StructArray(::Vector{SVector{3, Float64}}, ::Vector{SVector{3, Float64}}) with eltype NamedTuple{(:first, :second), Tuple{SVector{3, Float64}, SVector{3, Float64}}}:
 (first = [258.845273716783, 487.8977904159955, 1.0], second = [157.75229949765156, 595.5183920659432, 1.0])
 (first = [289.51900905492465, 393.4757347730604, 1.0], second = [308.550992447045, 617.3501755260837, 1.0])
 (first = [362.3296991292668, 332.0351721512725, 1.0], second = [579.3702766788141, 107.44831106083338, 1.0])
 (first = [212.17086720049917, 384.978545513196, 1.0], second = [72.76975409415482, 325.9100048752102, 1.0])
 (first = [434.6340517380251, 302.23356061671365, 1.0], second = [817.7310106439842, -96.58932611310641, 1.0])
 (first = [129.05937348135052, 179.94913799787562, 1.0], second = [32.05033362166862, 171.35102226173754, 1.0])
 (first = [217.74765338653387, 483.1258600918965, 1.0], second = [58.753844157796244, 847.0375594100266, 1.0])

it works, but why can't MVector be used for indexing?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the shown MVector and StructVector indexing examples. Read StaticArrays.StaticIndexing and the calls at StructArrays src/structarray.jl:348 and :358 alongside Julia Base indices.jl:277-333. Done means explaining or correcting the incompatibility so equivalent MVector and Vector indexing behave consistently, with a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.