### Output inside function mismatches outside function

Why does my function returns zeros when called in a script but then if I try to print out the output "inside" the function I get the correct results? I am confused!

For example, here's an FFT function I wrote, where the print statements return correct results:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````void r2cfft3d(Eigen::Tensor& rArr, Eigen::Tensor, 3> cArr){ fftw_complex *input_array; fftw_complex *output_array; input_array = (fftw_complex*) fftw_malloc(nx*ny*nz * sizeof(fftw_complex)); output_array = (fftw_complex*) fftw_malloc(nx*ny*nz * sizeof(fftw_complex)); for (int i = 0; i < nx; ++i) { for (int j = 0; j < ny; ++j) { for (int k = 0; k < nz; ++k) { { input_array[k + nz * (j + ny * i)][REAL] = rArr(k,i,j); input_array[k + nz * (j + ny * i)][IMAG] = 0; } } } } fftw_plan forward = fftw_plan_dft_3d(nx, ny, nz, input_array, output_array, FFTW_FORWARD, FFTW_ESTIMATE); fftw_execute(forward); fftw_destroy_plan(forward); fftw_cleanup(); for(int i=0; i < nx; ++i){ for(int j=0; j < ny; ++j) { for(int k=0; k < nz; ++k) { cArr(k,i,j).real(output_array[k + nz * (j + ny * i)][REAL]); cArr(k,i,j).imag(output_array[k + nz * (j + ny * i)][IMAG]); } } } std::cout<

Then when I call this function in a separate script:
 ``12345678910111213141516`` `````` Eigen::Tensor uFun(nx,ny,nz); uFun.setZero(); for(int i = 0; i< nx; i++){ for(int j = 0; j< ny; j++){ for(int k = 0; k< nz; k++){ uFun(k,i,j) = sin(3. * A * eZZ(k,i,j)) * sin(A * eXX(k,i,j)) * cos(A1 * eYY(k,i,j)); } } } Eigen::Tensor, 3> Test(nx,ny,nz); Test.setZero(); r2cfft3d(ne,Test); std::cout<
Last edited on
Pass it by reference (to non-cost).
@JLBorges
oh I see. Thanks!
Topic archived. No new replies allowed.