Simplify type information when reinterpreting/reshaping

I frequently have to do things like this:

``````julia> x = rand(3,10)
3×10 Matrix{Float64}:
0.233999  0.217953   0.391807  0.396972  0.612708  0.344216  0.513826  0.724808  0.222591  0.668894
0.152236  0.0596347  0.297019  0.320482  0.864853  0.658225  0.72082   0.966251  0.429694  0.980746
0.181907  0.561663   0.991661  0.968927  0.800681  0.838888  0.624427  0.874867  0.818654  0.536921

julia> y = reinterpret(reshape,SVector{3,Float64},x)
10-element reinterpret(reshape, SVector{3, Float64}, ::Matrix{Float64}) with eltype SVector{3, Float64}:
[0.23399915174244423, 0.1522357683743134, 0.1819071100157874]
[0.21795312154627955, 0.059634728256461544, 0.5616633601811634]
[0.39180686893905836, 0.29701928018071544, 0.9916612904284883]
...

``````

Is there some command that makes ` y` be just a ` Vector{SVector{3,Float64}}` , instead of carrying forever all its original type? (I mean particularly in this case, where the memory layouts are the same, as far as I understand).

In the StaticArrays.jl doc it is suggested to define some `svectors()` or `svectorscopy()` functions for this task.

If I copy the data the type signature changes, but I would like to not copy.

Probably missing the point , but what type signature changes in example below?

``````function svectorscopy(x::Matrix{T}, ::Val{N}) where {T,N}
size(x,1) == N || error("sizes mismatch")
isbitstype(T) || error("use for bitstypes only")
copy(reinterpret(SVector{N,T}, vec(x)))
end

x = rand(3,10)
y = svectorscopy(x, Val{3}())

10-element Vector{SVector{3, Float64}}:
[0.2934038605494145, 0.7926735779438824, 0.32679136208850756]
[0.9627113447751533, 0.07318706625463711, 0.8181536397448753]
[0.19577880969394235, 0.193712489726949, 0.009605787454978976]
[0.11050497078529231, 0.47596496598727633, 0.7392340772243953]
[0.8758005379065857, 0.5946355196925027, 0.32579452774945816]
[0.18191497127368395, 0.627220353635663, 0.5098188072009696]
[0.1845299828496222, 0.8192515115780793, 0.7930647894907568]
[0.895422764583234, 0.7529396456277849, 0.07670529122150205]
[0.5884055415673282, 0.8693150682627604, 0.7793829961607535]
[0.7968259720647202, 0.872461845502001, 0.9392783282178289]
``````

Or without copying:

``````function svectors(x::Matrix{T}, ::Val{N}) where {T,N}
size(x,1) == N || error("sizes mismatch")
isbitstype(T) || error("use for bitstypes only")
reinterpret(SVector{N,T}, vec(x))
end

x = rand(3,10)
y = svectors(x, Val{3}())

10-element reinterpret(SVector{3, Float64}, ::Vector{Float64}):
[0.43484279489143973, 0.3433845181618924, 0.8949897891014285]
[0.5632122167660084, 0.00808457834710985, 0.13876411174217862]
[0.4314491754059935, 0.24532306263713932, 0.9433935437475155]
[0.5817430805373387, 0.8486724899701844, 0.1666691669108591]
[0.8196370888035363, 0.8179178128661923, 0.7780387513807245]
[0.22964414518776088, 0.824704961854821, 0.543947079105416]
[0.6311081948549806, 0.14342382498659412, 0.5339623599311454]
[0.28299016894967266, 0.6468720901975916, 0.639679142608495]
[0.6212645796710144, 0.053254068944845745, 0.296617488302537]
[0.00187153138118068, 0.1692451204229175, 0.5988612165522477]
``````

What I want is to not copy and get the result as:

``````10-element Vector{SVector{3, Float64}}
``````

(But, FYI, this is only about ahestetics)

1 Like