# In-place fft

**URL:** https://discourse.julialang.org/t/in-place-fft/23993
**Category:** General Usage
**Created:** [May 8, 2019, 11:38am UTC](https://discourse.julialang.org/t/in-place-fft/23993 "2019-05-08T11:38:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)
#### Post date: [May 8, 2019, 11:38am UTC](https://discourse.julialang.org/t/in-place-fft/23993/1 "2019-05-08T11:38:41Z")

</div>

Hi, I am using the FFTW.jl package. If I have a vector, say

> x = rand(100)

How can I compute its discrete Fourier transform values in-place ? The command

> fft(x)

returns a new array but leaves x intact.  
This brings me to my second question, or comment, that often, these excellent libraries in Julia do not come with a documentation that lists the various functions, like in Java or Matlab. Does FFTW.jl have one such list ?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 8, 2019, 11:59am UTC](https://discourse.julialang.org/t/in-place-fft/23993/2 "2019-05-08T11:59:35Z")

</div>

> [@iamsuddhasattwa](#):
>
> How can I compute its discrete Fourier transform values in-place ? The command `fft(x)` returns a new array but leaves `x` intact.

`x = rand(100)` is a real array, and `fft(x)` is complex, so you’re not going to do that operation in-place. If you have a complex array, you can do:

```julia
x = rand(ComplexF64, 100)
fft!(x)

```

or

```julia
p = plan_fft!(x)
... write some data into x ...
p * x
... write some other data into x ...
p * x

```

if you want to perform many in-place FFTs and want to perform the precomputations once.

There are specialized transforms in FFTW.jl for real data, but the `rfft` function isn’t currently available in-place (although [there is a pending PR](https://github.com/JuliaMath/FFTW.jl/pull/54)).

> [@iamsuddhasattwa](#):
>
> these excellent libraries in Julia do not come with a documentation that lists the various functions, like in Java or Matlab. Does FFTW.jl have one such list ?

As the [FFTW.jl manual](https://juliamath.github.io/FFTW.jl/stable/fft.html) says, most of its functions are implementing the AbstractFFTs.jl interface and are documented in the [AbstractFFTs.jl manual](https://juliamath.github.io/AbstractFFTs.jl/dev/api/).

---

<div class="post-metadata">

### Author: ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)
#### Post date: [May 8, 2019, 12:10pm UTC](https://discourse.julialang.org/t/in-place-fft/23993/3 "2019-05-08T12:10:17Z")

</div>

@stevengj Thank you very much for telling me about this function plan\_fft! (). However, in your commands, if I am to use plan\_fft!(x), then I think x is already changed, so I do not need to do the step  
p \* x.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 8, 2019, 12:17pm UTC](https://discourse.julialang.org/t/in-place-fft/23993/4 "2019-05-08T12:17:48Z")

</div>

> [@iamsuddhasattwa](#):
>
> if I am to use `plan_fft!(x)`, then I think x is already changed, so I do not need to do the step  
> p \* x.

The only reason for using `plan_fft!` is if you are going to perform _many_ FFTs of the same array `x` (overwritten in-place with different data). If you only need a single FFT, then use `fft!`. But if you only need a single FFT, why are you bothering with in-place transforms in the first place?

If you use `plan_fft` or `plan_fft!` with the default `FFTW.ESTIMATE` flag, then it does not modify `x`. If you use it with `plan_fft!(x, flags=FFTW.MEASURE)` or `plan_fft!(x, flags=FFTW.PATIENT)` then it will overwrite `x` with zeros (it runs some benchmarks to find the best algorithm). See also the [FFTW FAQ](http://www.fftw.org/faq/section3.html#allzero).

---

<div class="post-metadata">

### Author: ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)
#### Post date: [May 8, 2019, 9:59pm UTC](https://discourse.julialang.org/t/in-place-fft/23993/5 "2019-05-08T21:59:59Z")

</div>

Hi @stevengj,  
Thank you again for the great summary. Actually, I have a nXm matrix A of type Array{Float64,2}, and I want to replace it with columnwise fft.  
Reason for me wanting to do this is that I am trying to save allocation space. n is usually very large for me, and in the program that I intend to call this routine, there are other huge matrices stored in the memory.  
As you mentioned, it cannot be replaced in-place since it is a real valued matrix.
