I’m sorry, I copied your first code directly and didn’t notice that the index was not written correctly, I corrected it now.
Perform fft on rank-1 arrays of the same length, fft internal working array wsave has the same initialization, if you want to avoid it and improve the running efficiency, please use zffti, zfftf, not fft.
program rand2D_test
use fftpack
use fftpack_kind, only: rk
implicit none
integer(kind=4), parameter :: Nx = 2, Ny = 2
complex(kind=rk), dimension(Nx, Ny) :: r
real(kind=rk) :: wsave(4*Nx + 15)
integer(kind=4) :: i, j
call random_number(r%re)
r%im = 0.0_rk
print *, ''
print *, 'Random Numbers are'
print *, ''
do i = 1, Nx
print *, (r(i, j), j=1, Ny)
end do
do i = 1, Nx
r(:, i) = fft(r(:, i))
end do
do j = 1, Ny
r(j, :) = fft(r(j, :)) ! i -> j
end do
print *, ''
print *, 'FFT is'
print *, ''
do i = 1, Nx
print *, (r(i, j), j=1, Ny)
end do
do i = 1, Nx
r(:, i) = ifft(r(:, i))
end do
do j = 1, Ny
r(j, :) = ifft(r(j, :)) ! i -> j
end do
print *, ''
print *, 'Inverse FFT is'
print *, ''
r = r/Nx/Ny ! normalize
do i = 1, Nx
print *, (r(i, j), j=1, Ny)
end do
call zffti(Nx, wsave)
do i = 1, Nx
call zfftf(Ny, r(i, :), wsave)
end do
do i = 1, Ny
call zfftf(Nx, r(:, i), wsave)
end do
print *, ''
print *, 'zfftf is'
print *, ''
do i = 1, Nx
print *, (r(i, j), j=1, Ny)
end do
end program rand2D_test
Random Numbers are
(0.96918105010483302,0.0000000000000000) (0.61750876263731336,0.0000000000000000)
(0.45185016840516656,0.0000000000000000) (0.84288263491537585,0.0000000000000000)
FFT is
(2.8814226160626886,0.0000000000000000) (-3.93601790426896248E-002,0.0000000000000000)
(0.29195700942160396,0.0000000000000000) (0.74270475397772895,0.0000000000000000)
Inverse FFT is
(0.96918105010483302,0.0000000000000000) (0.61750876263731325,0.0000000000000000)
(0.45185016840516651,0.0000000000000000) (0.84288263491537574,0.0000000000000000)
zfftf is
(2.8814226160626886,0.0000000000000000) (-3.93601790426894582E-002,0.0000000000000000)
(0.29195700942160396,0.0000000000000000) (0.74270475397772895,0.0000000000000000)