A related thing. When working to make my code type-generic, one thing is that the user can provide as input a vector of vectors (a Vector{SVector{3,Flot64}}
, for example - these are 3D particles). Or the user can provide a (3,N)
matrix.
To accept both types of inputs, internally I reinterpret the matrices:
julia> using StaticArrays
julia> x = rand(3,10);
julia> x_re = reinterpret(reshape, SVector{3,eltype(x)}, x)
10-element reinterpret(reshape, SVector{3, Float64}, ::Matrix{Float64}) with eltype SVector{3, Float64}:
[0.5742818048521419, 0.32234266246581833, 0.6093222017438875]
[0.866691366605002, 0.1318015340128984, 0.16367154831685848]
[0.6015181769893767, 0.5517370492686737, 0.678539023285754]
[0.7542841101508817, 0.3016408997455371, 0.6186488361258082]
[0.6023197313041428, 0.40427222266515384, 0.9545413271708354]
[0.5863109575920729, 0.09176860424885502, 0.12026428196223216]
[0.7147765247840423, 0.49433535648543603, 0.3328376337636578]
[0.7681148704397425, 0.32334972226471614, 0.22885420042279425]
[0.6759855817145615, 0.8790234427839108, 0.11503876383911571]
[0.9814601419098894, 0.47802148780118725, 0.4606405182254045]
This works for eltype(x) <: Real
thus it works for ForwardDiff. It also works as is for Unitful
quantities.
But it does not work for measurements
:
julia> using Measurements
julia> x = [ measurement(rand(),rand()) for i in 1:3, j in 1:10 ]
3Ć10 Matrix{Measurement{Float64}}:
0.7Ā±0.99 0.541Ā±0.054 0.9Ā±0.18 0.089Ā±0.6 ā¦ 0.83Ā±0.98 0.065Ā±0.2 0.15Ā±0.2 0.007Ā±0.74
0.623Ā±0.078 0.21Ā±0.44 0.55Ā±0.64 0.74Ā±0.46 0.76Ā±0.69 0.78Ā±0.87 0.96Ā±0.66 0.089Ā±0.094
0.9Ā±0.7 0.98Ā±0.89 0.92Ā±1.0 0.76Ā±0.71 0.13Ā±0.78 0.65Ā±0.82 0.023Ā±0.067 0.6Ā±0.29
julia> x_re = reinterpret(reshape, SVector{3,eltype(x)}, x)
ERROR: ArgumentError: cannot reinterpret `Measurement{Float64}` as `SVector{3, Measurement{Float64}}`, type `SVector{3, Measurement{Float64}}` is not a bits type
Is there any good way to support all these types? Or maybe I have to check if the type is bits, and if not just copy the data to a vector of non-static vectors, and allow the code to (slowly) operate on these?