Hi, everyone. I want to ask you if you know a better way of doing the following that does not imply accessing internal fields.
Imagine I have a vector of complex numbers:
julia> v1 = [1.0 + 0.5im, 4.8 - 9.0im]
2-element Vector{ComplexF64}:
1.0 + 0.5im
4.8 - 9.0im
I can convert it to a vector of floats with:
julia> v2 = reinterpret(Float64, v1)
4-element reinterpret(Float64, ::Vector{ComplexF64}):
1.0
0.5
4.8
-9.0
And go back likewise:
julia> v3 = reinterpret(ComplexF64, v2)
2-element Vector{ComplexF64}:
1.0 + 0.5im
4.8 - 9.0im
Everything works fine as I like it. Now I would like to do the same with SVectors:
julia> s1 = SVector(1.0 + 0.5im, 4.8 - 9.0im)
2-element SVector{2, ComplexF64} with indices SOneTo(2):
1.0 + 0.5im
4.8 - 9.0im
The naĂŻve implementation fails:
julia> s2 = reinterpret(SVector{4,Float64}, s1)
Error showing value of type Base.ReinterpretArray{SVector{4, Float64}, 1, ComplexF64, SVector{2, ComplexF64}, false}:
SYSTEM (REPL): showing an error caused an error
ERROR: 1-element ExceptionStack:
DimensionMismatch: 1:1 is inconsistent with SOneTo{2}
Stacktrace:
...
However, if access the internal field data of s1, then it works.
julia> s2 = reinterpret(SVector{4,Float64}, s1.data)
4-element SVector{4, Float64} with indices SOneTo(4):
1.0
0.5
4.8
-9.0
If I wanted to go back, I could do so accessing again .data:
julia> s3 = reinterpret(SVector{2,ComplexF64}, s2.data)
2-element SVector{2, ComplexF64} with indices SOneTo(2):
1.0 + 0.5im
4.8 - 9.0im
Is there a way of doing this without accessing .data? What I show here is MWE of what I need. I cannot afford to allocate intermediate arrays in the process.
Likewise, in another part of the program, I need to convert SMatrix and SHermitianCompact arrays back to the vector that built them, and the only way to do so is accessing those internal fields. I know it is a bad practice in case the api changes in the future, but I havenât found a better way for now.