英文:
Error: how to divide an Eigen tensor by a scalar or int?
问题
以下是翻译好的部分:
我正在尝试将一个整数除以一个Eigen张量,唯一的方法是引入一个虚拟变量,它仍然返回零。不知何故,我用零覆盖了我的输出:
代码:
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));
//按大小标准化:
Eigen::Tensor<double, 3> dummy (nx,ny,nz);
dummy = 1/(nx*ny*nz) * rArr.data();
rArr = dummy;
}
这明显不起作用,尝试像这样做一些事情也不起作用:
dummy = 1/(nx*ny*nz) * rArr;
上面的尝试都不起作用,我只是想除以 (nx*ny*nz)
或 rArr/(nx*ny*nz)
,是否有一种高效的方法可以实现这一点?谢谢!
英文:
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!
答案1
得分: 1
Eigen
不是我的专业领域,但似乎rArr.data()
返回一个指针,可能可以以某种方式转换为非零值。
此外,1/(nx*ny*nz)
似乎是“整数除法”,总是返回零。
也许这有机会运行:
Eigen::Tensor<double, 3> dummy = 1.0 / (nx * ny * nz) * rArr;
英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论