I have the following example:

```
julia> a=1+2im
1 + 2im
julia> A=a*ones(3)
3-element Vector{ComplexF64}:
1.0 + 2.0im
1.0 + 2.0im
1.0 + 2.0im
```

How can I can convert A into two vectors, a vector of the real values and a vector of the imaginary values?

Thanks a lot! Is that documented anywhere? I tried re(A) and im(A) and A.re and A.im which didn’t work…

```
julia> A[1]. <TAB><TAB>
im re
julia> getproperty.(A,:im)
3-element Vector{Float64}:
2.0
2.0
2.0
julia> getproperty.(A,:re)
3-element Vector{Float64}:
1.0
1.0
1.0
```

2 Likes

I’ll point out one other way via `reinterpret`

. This allows you to make real and imaginary views of the data without copying.

```
julia> B = reinterpret(Float64, A)
6-element reinterpret(Float64, ::Vector{ComplexF64}):
1.0
2.0
1.0
2.0
1.0
2.0
julia> A_real = @view B[1:2:end]
3-element view(reinterpret(Float64, ::Vector{ComplexF64}), 1:2:5) with eltype Float64:
1.0
1.0
1.0
julia> A_imag = @view B[2:2:end]
3-element view(reinterpret(Float64, ::Vector{ComplexF64}), 2:2:6) with eltype Float64:
2.0
2.0
2.0
julia> A[1] = 3 + 5im
3 + 5im
julia> A_real[1]
3.0
julia> A_imag[1]
5.0
```

5 Likes

Looks like Base already has a function for this: `reim`

I do not think that `reim`

produces views. We should make `@view reim(A)`

work though.

2 Likes

Ah right. I misread the non-copying mention in the docs there.

1 Like

DNF
July 2, 2023, 4:30pm
11
Messing about with internal fields, when there are canonical functions for this, is really not advisable.

1 Like

Yes sure, fully agree. I just wanted to point out that @ufechner7 came very close in using the ‘re’ and ‘im’ properties and the usefulness of experimenting with the double <TAB> and see what comes up

Also see StructArrays: `A = StructArray(...)`

, and now you have both an array of complex numbers `A`

and its components `A.re`

and `A.im`

. These two representations share the same memory and operations are often more efficient.