# Apply array function such as fft to nested array

**URL:** https://discourse.julialang.org/t/apply-array-function-such-as-fft-to-nested-array/66935
**Category:** New to Julia
**Tags:** fftw, arrays
**Created:** [August 24, 2021, 7:19pm UTC](https://discourse.julialang.org/t/apply-array-function-such-as-fft-to-nested-array/66935 "2021-08-24T19:19:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)
#### Post date: [August 24, 2021, 7:19pm UTC](https://discourse.julialang.org/t/apply-array-function-such-as-fft-to-nested-array/66935/1 "2021-08-24T19:19:30Z")

</div>

I have an array A of 2-`SVectors`. I’d like to apply `fft` component-wise, namely, something like `fft(A[:,:][1])` and` fft(A[:,:][2])` and put them together again as nested array of vectors. It is best if this can be done in-place, namely, through something like `plan_fft!`. Is there a natural way of doing this? I can use `getindex.(A,1)` but this cannot generate a “view” to allow in-place operations.

---

<div class="post-metadata">

### Author: ![jinml](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jinml](https://discourse.julialang.org/u/jinml)
#### Post date: [August 24, 2021, 7:56pm UTC](https://discourse.julialang.org/t/apply-array-function-such-as-fft-to-nested-array/66935/2 "2021-08-24T19:56:35Z")

</div>

I found a solution

```julia
A_r = reinterpret(reshape,eltype(A[1]),A)
fft!(A_r,(2,3))

```

Of course `eltype(A)` should be of some complex float type to allow in-place fft.

Update: here is an example: BTW,` reinterpret(reshape, ... )` only works for Julia 1.6 and above

```julia
julia> using LinearAlgebra

julia> using StaticArrays

julia> using FFTW

julia> N=10; M=20;

julia> vf=[@SVector rand(ComplexF64,2) for n in 1:N, m=1:M];        

julia> vf_copy=deepcopy(vf);

julia> vfr=reinterpret(reshape,eltype(eltype(vf)),vf);

julia> fft!(vfr,(2,3));

julia> fft_vf1=fft(getindex.(vf_copy,1));

julia> norm(fft_vf1-getindex.(vf,1))
0.0

julia> fft_vf2=fft(getindex.(vf_copy,2));

julia> norm(fft_vf2-getindex.(vf,2))
0.0

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 25, 2021, 8:02am UTC](https://discourse.julialang.org/t/apply-array-function-such-as-fft-to-nested-array/66935/3 "2021-08-25T08:02:21Z")

</div>

@jinml, could you complete your example with some fake input data, to make it useful to others?  
Thanks.
