In-place fft

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


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 ?

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:

x = rand(ComplexF64, 100)


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).

As the FFTW.jl manual says, most of its functions are implementing the AbstractFFTs.jl interface and are documented in the AbstractFFTs.jl manual.

1 Like

@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.

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.

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.