1

I'm doing N-body calculations using pyopencl and I need to have my data to be 1D array as input and as output of some kernel. So if I have 2D problem, I do something like this: 1D_arr=numpy.reshape(3D_arr,(LxLy)), where LxLy number of all particles. How to perform a fast fourier transform(fft) of 1D array(If it is possible!), which corresponds to fft of 3D array (and ifft after)? These 2 arrays are connected by reshape transformation in real space.

import numpy as np
from scipy.fftpack import fftn, fft


Lx = 10 
Ly = 9
Lz = 1
L = Lx * Ly * Lz
Lspec = (2 * Lx - 1) * (2 * Ly - 1) * (2 * Lz - 1)

M1 = np.zeros(L).astype(np.float32)
for i in range(L):
    M1[i] = np.random.random()
M3 = np.zeros((Lx, Ly, Lz))
for z in range(Lz):
    for y in range(Ly):
        for x in range(Lx):
            i = x + Lx * y
            M3[x, y, z] = i
            M1[i] = i
            print(M3[x,y,z], M1[i])


print("________________________")


FM3 = fftn(M3)
FM1 = fftn(M1)

for z in range(Lz):
    for y in range(Ly):
        for x in range(Lx):
            i = x + Lx * y
            i2 = y + Ly * x
            print(FM3[x,y,z], FM1[i2])

I expect to see all ellements of FM1 and FM3 equal, but I don't know how to manage it.

GeorgePhysics
  • 71
  • 1
  • 8
  • 1
    Reshape to a 3D array, do the 3D FFT, then reshape back to vector. You need to know the sizes of the 3D array of course. There's no way around that. – Cris Luengo Sep 12 '19 at 16:32
  • 1
    Indeed, 3D DFT could not be equivalent to a single 1D DFT, as the dimensions of the 3D array need to be known. A 3D DFT is equivalent to performing 1D DFT over each dimension, for all the subarrays. The advanced interface of FFTW feature arguments such as strides and dist to handle non-contigous array and do multiple 1D DFT at once, but it does not seem to be available in pyfftw. However, if `numpy.reshape()` creates unnecessary copies, you may try a view. `a = np.zeros((10, 2))`, `b = a.view()`, `b.shape = (20)`. https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html – francis Sep 13 '19 at 17:55

0 Answers0