如何将 Eigen 张量除以标量或整数?

huangapple go评论72阅读模式
英文:

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&lt;std::complex&lt;double&gt;, 3&gt;&amp; cArr, Eigen::Tensor&lt;double, 3&gt;&amp; 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&lt;double, 3&gt; 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&lt;double, 3&gt; dummy = 1.0/(nx*ny*nz) * rArr;

huangapple
  • 本文由 发表于 2023年4月11日 14:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定