I’m having trouble discerning how to convert between different representations of array structures using reinterpret
. I’ve figured some things out, which I have posted below in case anyone else will find the examples useful. I end with a case that I am currently unable to solve and would appreciate any suggestions.
Suppose I have a type representing the Homogeneous coordinates of a planar point,
struct HomogeneousPoint{T <: AbstractFloat,N}
coords::NTuple{N, T}
end
and I instantiate it with 8 concrete points as follows:
pts2D_hom = map(HomogeneousPoint,
[(4.244432486132958, 4.244432486132958, 1.0),
(4.244432486132958, 16.755567513867042, 1.0),
(8.244432486132958, 8.244432486132958, 1.0),
(8.244432486132958, 12.755567513867042, 1.0),
(12.755567513867042, 8.244432486132958, 1.0),
(12.755567513867042, 12.755567513867042, 1.0),
(16.755567513867042, 4.244432486132958, 1.0),
(16.755567513867042, 16.755567513867042, 1.0)]).
This yields an Array of HomogeneousPoint
8-element Array{HomogeneousPoint{Float64,3},1}:
HomogeneousPoint{Float64,3}((4.24443, 4.24443, 1.0))
HomogeneousPoint{Float64,3}((4.24443, 16.7556, 1.0))
HomogeneousPoint{Float64,3}((8.24443, 8.24443, 1.0))
HomogeneousPoint{Float64,3}((8.24443, 12.7556, 1.0))
HomogeneousPoint{Float64,3}((12.7556, 8.24443, 1.0))
HomogeneousPoint{Float64,3}((12.7556, 12.7556, 1.0))
HomogeneousPoint{Float64,3}((16.7556, 4.24443, 1.0))
HomogeneousPoint{Float64,3}((16.7556, 16.7556, 1.0))
We can reinterpret this array as an 8 x 3 two-dimensional array with the command
mat = reinterpret(Float64,pts2D_hom ,(8,3))
resulting in
8×3 Array{Float64,2}:
4.24443 1.0 12.7556
4.24443 8.24443 1.0
1.0 12.7556 16.7556
4.24443 1.0 4.24443
16.7556 12.7556 1.0
1.0 8.24443 16.7556
8.24443 1.0 16.7556
8.24443 12.7556 1.0
The rational for the construction, as oatlzzvztd points out in Reinterpret SVector, is that
the first argument to reinterpret should be the type of the element of the resulting array (i.e., the result of eltype(y).
Now suppose that I wanted to reinterpret pts2D_hom
as an SMatrix
instead. The closest I came up with is
smat = reinterpret(SMatrix{8,3,Float64,24},pts2D_hom,(1,))
which creates an array with a single entry of an SMatrix as follows
1-element Array{StaticArrays.SArray{Tuple{8,3},Float64,2,24},1}:
[4.24443 1.0 12.7556; 4.24443 8.24443 1.0; … ; 8.24443 1.0 16.7556; 8.24443 12.7556 1.0]
However, what I am I to do if I don’t want an array containing an SMatrix, but I want the SMatrix on its own?