Reinterpret 3D array into array of SMatrices

I don’t understand why I can’t reinterpret a 3D array of float as an array of static matrices. i’m trying to do this:

A = rand(2,3,5)
# can do
julia> reinterpret(SVector{3,Float64},A[1,:,:],(5,))
5-element Array{SVector{3,Float64},1}:
 [0.23546, 0.907289, 0.740175]   
 [0.61008, 0.734986, 0.629369]   
 [0.996311, 0.0236219, 0.452736] 
 [0.926702, 0.754213, 0.0497403] 
 [0.00632655, 0.469851, 0.832494]

# but not
julia> reinterpret(SMatrix{2,3,Float64},A,(5,))
ERROR: ArgumentError: cannot reinterpret Array{Float64} to ::Type{Array{SMatrix{2,3,Float64}}}, type SMatrix{2,3,Float64} is not a bits type

I don’t understand. I want a 5-element Array{SMatrix{2,3,Float64},1}, analog to the first example. I’m coming from FixedSizeArrays (julia v0.5) where this work like

# julia v0.5
julia> using FixedSizeArrays
julia> reinterpret(Mat{2,3,Float64},A,(5,))
5-element Array{FixedSizeArrays.Mat{2,3,Float64},1}:
 FixedSizeArrays.Mat{2,3,Float64}(
    0.5574181712074888 0.9644487115785165 0.6704697319255919
    0.9975096208909706 0.2222225851632973 0.14946990105711122
)
   
 FixedSizeArrays.Mat{2,3,Float64}(
    0.28001812808230175 0.44931736683789114 0.322378156341002
    0.46665857950152345 0.14451919484905318 0.22147629055395557
)

 FixedSizeArrays.Mat{2,3,Float64}(
    0.0002579687549821852 0.8900462296197154 0.5851686490075749
    0.6029813419932457 0.7732024531312345 0.12115694131802379
)

 FixedSizeArrays.Mat{2,3,Float64}(
    0.41651532906948185 0.44625111837501996 0.09088448384005732
    0.8730213366071933 0.7085121876088183 0.2729717970821717
)
 
 FixedSizeArrays.Mat{2,3,Float64}(
    0.3853576197872626 0.13637018253558097 0.04404747821364219
    0.9183888520473955 0.6311436084278026 0.9758791739746058
)

You need to specify the last type parameter in the SMatrix which I believe is M*N (easily checked with typeof).

dang, that was stupid. thanks!

I’m reactivating this thread since this doesn’t seem to work anymore in v0.7, unless I’m misunderstanding the explained solution (very possible). Here’s what I’m trying:

julia> using StaticArrays

julia> A = rand(2,4,5);

julia> reinterpret(SMatrix{2,4,Float64,8},A)
ERROR: ArgumentError:     cannot reinterpret an `Float64` array to `SArray{Tuple{2,4},Float64,2,8}` whose first dimension has size `2`.
    The resulting array would have non-integral first dimension.

Stacktrace:
 [1] (::getfield(Base, Symbol("#thrownonint#190")){Float64,SArray{Tuple{2,4},Float64,2,8}})(::Type{Float64}, ::Type{SArray{Tuple{2,4},Float64,2,8}}, ::Int64) at ./reinterpretarray.jl:24
 [2] reinterpret(::Type{SArray{Tuple{2,4},Float64,2,8}}, ::Array{Float64,3}) at ./reinterpretarray.jl:39
 [3] top-level scope at none:0
julia> using StaticArrays

julia> A = rand(2,4,5);

julia> reinterpret(SMatrix{2,4,Float64,8},reshape(A, length(A)))
5-element reinterpret(SArray{Tuple{2,4},Float64,2,8}, ::Array{Float64,1}):
 [0.17061 0.569503 0.986942 0.623712; 0.381364 0.963277 0.252662 0.504635] 
 [0.595363 0.364473 0.386016 0.122044; 0.901013 0.740808 0.823493 0.933547]
 [0.0286543 0.606252 0.116737 0.921229; 0.27438 0.426807 0.570889 0.186989]
 [0.467349 0.567191 0.288745 0.724425; 0.776126 0.623564 0.490382 0.979833]
 [0.848398 0.485889 0.232903 0.03917; 0.801746 0.0139404 0.422298 0.834867]

Warning: The result may be slow on 1.0. Something that works fast now but is expected to stop working cause very weird bugs in the future is

julia> typ = SMatrix{2,4,Float64,8}
julia> ccall(:jl_reshape_array, Vector{typ}, (Any,Any,Any), Vector{typ}, A, (size(A,3),))
5-element Array{SArray{Tuple{2,4},Float64,2,8},1}:
 [0.17061 0.569503 0.986942 0.623712; 0.381364 0.963277 0.252662 0.504635] 
 [0.595363 0.364473 0.386016 0.122044; 0.901013 0.740808 0.823493 0.933547]
 [0.0286543 0.606252 0.116737 0.921229; 0.27438 0.426807 0.570889 0.186989]
 [0.467349 0.567191 0.288745 0.724425; 0.776126 0.623564 0.490382 0.979833]
 [0.848398 0.485889 0.232903 0.03917; 0.801746 0.0139404 0.422298 0.834867]