# How to apply fft to an array of static arrays?

**URL:** <https://discourse.julialang.org/t/how-to-apply-fft-to-an-array-of-static-arrays/124127>\
**Category:** General Usage\
**Tags:** staticarrays, fft\
**Created:** [December 23, 2024, 3:11pm UTC](https://discourse.julialang.org/t/how-to-apply-fft-to-an-array-of-static-arrays/124127 "2024-12-23T15:11:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcsgil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcsgil/32/33908_2.png) [@marcsgil](https://discourse.julialang.org/u/marcsgil)\
**Post date:** [December 23, 2024, 3:11pm UTC](https://discourse.julialang.org/t/how-to-apply-fft-to-an-array-of-static-arrays/124127/1 "2024-12-23T15:11:13Z")

</div>

I’d like to be able to do something like

```julia
using StaticArrays, FFTW
A = [@SVector rand(ComplexF64, 3) for m ∈ 1:512]
fft(A)

```

but FFTW.jl throws an error when I try it.

Due to performance and ergonomic reasons, I really want to use an array of static vectors, instead of an array with one extra dimension. But just to be clear, I’m trying to perform an operation analogous to

```julia
A = rand(ComplexF64, 3, 512)
fft(A, dims=2)

```

Is there a way to accomplish this with an array of `SArrays`? I’d like to also do this on the GPU.

Thanks in advance!

---

<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:** [December 24, 2024, 1:27am UTC](https://discourse.julialang.org/t/how-to-apply-fft-to-an-array-of-static-arrays/124127/2 "2024-12-24T01:27:28Z")

</div>

There is a `@SMatrix` macro for 2-dimensional static arrays.

```julia-repl
julia> using StaticArrays, FFTW

julia> A = @SMatrix rand(ComplexF64, 3, 512)
3×512 SMatrix{3, 512, ComplexF64, 1536} with indices SOneTo(3)×SOneTo(512):
 0.700458+0.261335im 0.380832+0.435067im … 0.94796+0.0524721im
 0.789127+0.51047im 0.790958+0.100901im 0.486345+0.982996im
 0.813353+0.462645im 0.0897808+0.122944im 0.550222+0.546134im

julia> fft(A, 2)
3×512 Matrix{ComplexF64}:
   261.6+254.863im 0.532757+2.99622im … -4.6475+5.76742im
 247.911+260.101im -2.50283+2.4603im -3.08027+6.46999im
 251.814+255.649im -8.23491+4.1779im -2.99529-13.4493im

julia> fft(A, 2)[:,1:1] ≈ sum(A, dims=2)
true

```

Someone else will have to answer for GPUs.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [December 24, 2024, 1:31am UTC](https://discourse.julialang.org/t/how-to-apply-fft-to-an-array-of-static-arrays/124127/3 "2024-12-24T01:31:31Z")

</div>

Maybe you want `fft(reinterpret(reshape, ComplexF64, A), 2)`? That ought to be a little quicker than `fft(stack(A), 2)`. You can reinterpret back afterwards (or call `eachcol`).

---

<div class="post-metadata">

**Author:** ![marcsgil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcsgil/32/33908_2.png) [@marcsgil](https://discourse.julialang.org/u/marcsgil)\
**Post date:** [December 24, 2024, 6:47am UTC](https://discourse.julialang.org/t/how-to-apply-fft-to-an-array-of-static-arrays/124127/4 "2024-12-24T06:47:30Z")

</div>

Thanks for the answer! Nonetheless, I really want to stick with arrays of static arrays. I’ve edited the post to make this point clearer

---

<div class="post-metadata">

**Author:** ![marcsgil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcsgil/32/33908_2.png) [@marcsgil](https://discourse.julialang.org/u/marcsgil)\
**Post date:** [December 24, 2024, 6:58am UTC](https://discourse.julialang.org/t/how-to-apply-fft-to-an-array-of-static-arrays/124127/5 "2024-12-24T06:58:54Z")

</div>

Thanks, I guess this is it! I didn’t know about this three argument `reinterpret`. Is this expected to work on GPU as well? I currently do not have access to one to try things out.
