# How to split a vector of complex into a real and an imag vector

**URL:** https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053
**Category:** General Usage
**Tags:** question, complex-numbers
**Created:** [July 1, 2023, 4:05pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053 "2023-07-01T16:05:44Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [July 1, 2023, 4:05pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/1 "2023-07-01T16:05:44Z")

</div>

I have the following example:

```julia
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?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [July 1, 2023, 4:12pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/2 "2023-07-01T16:12:47Z")

</div>

`real(A)` and `imag(A)`

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [July 1, 2023, 4:14pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/3 "2023-07-01T16:14:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [July 1, 2023, 4:15pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/4 "2023-07-01T16:15:12Z")

</div>

[Mathematics · The Julia Language](https://docs.julialang.org/en/v1/base/math/#Base.real) and [Mathematics · The Julia Language](https://docs.julialang.org/en/v1/base/math/#Base.imag)

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [July 1, 2023, 4:22pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/5 "2023-07-01T16:22:15Z")

</div>

Thanks again!

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [July 1, 2023, 4:30pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/6 "2023-07-01T16:30:08Z")

</div>

```julia

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

```

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [July 2, 2023, 6:03am UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/7 "2023-07-02T06:03:53Z")

</div>

I’ll point out one other way via `reinterpret`. This allows you to make real and imaginary views of the data without copying.

```julia
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

```

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [July 2, 2023, 1:00pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/8 "2023-07-02T13:00:29Z")

</div>

> [@mkitti](#):
>
> This allows you to make real and imaginary views of the data without copying.

Looks like Base already has a function for this: [`reim`](https://docs.julialang.org/en/v1/base/math/#Base.reim)

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [July 2, 2023, 1:14pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/9 "2023-07-02T13:14:57Z")

</div>

I do not think that `reim` produces views. We should make `@view reim(A)` work though.

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [July 2, 2023, 1:16pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/10 "2023-07-02T13:16:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 2, 2023, 4:30pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/11 "2023-07-02T16:30:37Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `julia> getproperty.(A,:im)`

Messing about with internal fields, when there are canonical functions for this, is really not advisable.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [July 2, 2023, 9:01pm UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/12 "2023-07-02T21:01:03Z")

</div>

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

> [@ufechner7](#):
>
> I tried re(A) and im(A) and A.re and A.im which didn’t work…

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [July 3, 2023, 10:55am UTC](https://discourse.julialang.org/t/how-to-split-a-vector-of-complex-into-a-real-and-an-imag-vector/101053/13 "2023-07-03T10:55:42Z")

</div>

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.
