Differentiation in N Dimensions with the Fast Fourier Transform (FFT)
Your Daily Dose of Scientific Python
This is a quick follow-up to my recent article on taking derivatives using the FFT Fourier transform. As promised, I give the generalization from 1 to 𝑁 dimensions. The approach is quite the same, just the numpy functions are different and we have to use meshed grids.
Suppose we have the function
and we want to calculate the partial derivative
The idea is just the same. We perform a Fourier transform, multiply by the corresponding wave number and transform back:
In numpy, we used the functions fft
and ifft
to do the one-dimensional FFT and its reverse. In 𝑁N dimensions, we use the functions fftn
and ifftn
, respectively. Moreover, in 1D, our grids are just one-dimensional arrays. In 𝑁 dimensions, we have to use meshed grids for the coordinate grids. The same applies to the reciprocal / wave number axes. For each dimension, we get a wave number axis and we then mesh them together. The code looks like…