jinml
April 3, 2022, 11:46pm
1
I know I can reinterpret an array of SVector
as an ND array as follows:
julia> SA=[@SVector rand(2) for _=1:100];
julia> A=reinterpret(reshape,Float64,SA);
julia> @show SA[1],A[:,1];
(SA[1], A[:, 1]) = ([0.01565087031449286, 0.4977468415798615], [0.01565087031449286, 0.4977468415798615])
I can even modify the SVector
through the reinterpretation:
julia> A[:,1].=0.0;
julia> @show SA[1],A[:,1];
(SA[1], A[:, 1]) = ([0.0, 0.0], [0.0, 0.0])
My question is, given a 2D array like A
above, is there a way to convert to a 1D array of SVector
s in-place, like SA
above, i.e., sharing the same memory ?
jinml:
My question is, given a 2D array like A
above, is there a way to convert to a 1D array of SVector
s in-place, like SA
above, i.e., sharing the same memory
Just use reinterpret
:
julia> A = rand(3,6)
3×6 Matrix{Float64}:
0.781215 0.864153 0.190954 0.513353 0.684802 0.304443
0.717076 0.248486 0.394957 0.231284 0.572842 0.174061
0.605211 0.814295 0.173915 0.0166475 0.597283 0.482926
julia> B = reinterpret(SVector{3,Float64}, vec(A))
6-element reinterpret(SVector{3, Float64}, ::Vector{Float64}):
[0.7812150216015193, 0.7170764937769242, 0.6052113147390563]
[0.8641531429329602, 0.24848604689820653, 0.8142954282406346]
[0.1909536286428969, 0.3949565597637975, 0.17391456890697454]
[0.5133528560285338, 0.23128368035325675, 0.016647532659139097]
[0.6848016310407239, 0.5728415428873161, 0.5972827074627644]
[0.3044430234630139, 0.17406109329315256, 0.4829260227587332]
julia> A[1,1] = 3.14159
3.14159
julia> B[1]
3-element SVector{3, Float64} with indices SOneTo(3):
3.14159
0.7170764937769242
0.6052113147390563
1 Like