Search code examples
c++eigeneigen3

Error: how to divide an Eigen tensor by a scalar or int?


I am trying to divide just an int by an Eigen tensor and the only way is to introduce a dummy variable that still returns zeros. Somehow I am overwriting my output with zeros:

Code:

void c2rfft3d(Eigen::Tensor<std::complex<double>, 3>& cArr, Eigen::Tensor<double, 3>& rArr){    
    
        fftw_complex *input_array;
        input_array = (fftw_complex*) fftw_malloc(nx*ny*nz * sizeof(fftw_complex));
        memcpy(input_array, cArr.data(), nx*ny*nz * sizeof(fftw_complex));
        
        fftw_complex *output_array;
        output_array = (fftw_complex*) fftw_malloc(nx*ny*nz * sizeof(fftw_complex));
        
        fftw_plan backward = fftw_plan_dft_3d(nx, ny, nz, input_array, output_array, FFTW_BACKWARD, FFTW_ESTIMATE);
        
        fftw_execute(backward);
        fftw_destroy_plan(backward);
        fftw_cleanup();

        memcpy(rArr.data(),output_array, nx*ny*nz * sizeof(double));
//normalize by size:
Eigen::Tensor<double, 3> dummy (nx,ny,nz); 
dummy = 1/(nx*ny*nz) * rArr.data();
rArr = dummy;
}

This clearly does not work and trying something like

dummy = 1/(nx*ny*nz) * rArr;

None of the attempts above work really, I am just trying to divide by (nx*ny*nz) or rArr/(nx*ny*nz) is there an efficient way to do this? Thanks!


Solution

  • Not an expert in Eigen, but it sounds like rArr.data() returns a pointer that might be converted somehow to a non-zero value.

    Additionally, 1/(nx*ny*nz) seems to be "integer division", returning exactly zero always.

    Perhaps this has a chance to work:

    Eigen::Tensor<double, 3> dummy = 1.0/(nx*ny*nz) * rArr;