### 2D FFT of Eigen matrices using FFTW library

I'm trying to use the FFTW library with Eigen. Since Eigen's unsupported module is slower. So, I'm struggling on implementing a very basic example. Here is what I have so far:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````#define _USE_MATH_DEFINES #include #include #include #include "fftw3.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace Eigen; int main(){ static const int nx = 10; static const int ny = 10; static const int nyk = ny/2 + 1; static const int ncomp = 2; double Lx = 2*EIGEN_PI; double A = (2 * EIGEN_PI)/Lx; Matrix eYY; eYY.setZero(); Matrix eXX; eXX.setZero(); Eigen::Matrix< double, (ny+1), (ny)> u; u.setZero(); for(int i = 0; i< nx; i++){ for(int j = 0; j< ny+1; j++){ eXX(j + (ny+1)*i) = (i)*2*EIGEN_PI/nx; eYY(j + (ny+1)*i) = -1. * cos(((j) * EIGEN_PI )/ny); u(j + (ny+1)*i) = pow((eYY(j + (ny+1)*i)-0.5),2) * sin(A * eXX(j + (ny+1)*i)); //test function:u } } fftw_complex *uhk; uhk = (fftw_complex*) fftw_malloc(((ny*(ny+1)))* sizeof(fftw_complex)); memset(uhk, 42, ((ny*(ny+1)))* sizeof(fftw_complex)); Eigen::MatrixXcd uh; uh.setZero((ny+1), (ny)); fftw_complex *uhkOut; uhkOut = (fftw_complex*) fftw_malloc(((ny*(ny+1)))* sizeof(fftw_complex)); memset(uhkOut, 42, ((ny*(ny+1)))* sizeof(fftw_complex)); fftw_plan r2c; r2c = fftw_plan_dft_r2c_2d(ny, (ny+1), &uhk[0], &uhkOut[0], FFTW_ESTIMATE); fftw_execute(r2c); fftw_destroy_plan(r2c); fftw_cleanup(); }``````

Basically, what I am trying to do is define an Eigen matrix u and try to take the 2D fft of it using my fftw library, where I had to copy `memcpy ` the content over. But, I am getting the error:
 ``123456789`` ``````Test.cpp: In function ‘int main()’: Test.cpp:455:41: error: cannot convert ‘double (*)[2]’ to ‘double*’ 455 | r2c = fftw_plan_dft_r2c_2d(ny, (ny+1), &uhk[0], &uhkOut[0], FFTW_ESTIMATE); | ^~~~~~~ | | | double (*)[2] In file included from Test.cpp:8: /usr/include/fftw3.h:457:1: note: initializing argument 3 of ‘fftw_plan_s* fftw_plan_dft_r2c_2d(int, int, double*, double (*)[2], unsigned int)’ 457 | FFTW_DEFINE_API(FFTW_MANGLE_DOUBLE, double, fftw_complex)``````

Last edited on
It's because you are trying to pass a `fftw_complex*` pointer (which is a typedef for `double[2]`) as "in" parameter, but in fact a `double*` pointer is expected here! Only the "out" array is of type `fftw_complex*`:
https://www.fftw.org/fftw3_doc/Multi_002dDimensional-DFTs-of-Real-Data.html

In other words, the "input" data has to be real numbers, but the "output" data will be complex numbers.

That's why it is called "r2c", by the way ;-)
Last edited on
@kigar64551 hey you're so right, I guess writing code at 1 am is not a good idea. Thanks though for pointing it out. I see that my size if off now since I am getting the error:
 ``12`` ``````corrupted size vs. prev_size Aborted``````

But, I will take a second closer look.