问题0:
cuComplex.h 是否支持 exp()?
问题 1:
如何写 A = B * exp(i * C),其中 A、B、C 是相同大小的实数数组?这样对吗?
主要内容:
cuComplex A;
float B;
cuComplex c;
内核:
c[idx] = ( 0, C[idx] );
A[idx] = B[idx] * exp( c[idx] );
问题 2:
cuComplex 包含 2 个 float ,这意味着我必须分配比原始矩阵多 2 倍的内存。有什么方法可以创建纯虚数吗?
最佳答案
cuComplex.h 只提供一些对 cuComplex 的基本操作(主要是在 CUBLAS 和 CUFFT 库中使用的那些),不支持指数函数。
您可以使用基于分量的算法自行实现指数。 cuComplex 将复数的实部存储在 x 分量中,将虚部存储在 y 分量中。给定一个复数 z = x + i*y,指数可以计算为:
exp(z) = exp(x) * (cos(y) + i * sin(y))
这导致以下 CUDA 代码(未经测试):
cuComplex my_complex_exp (cuComplex arg)
{
cuComplex res;
float s, c;
float e = expf(arg.x);
sincosf(arg.y, &s, &c);
res.x = c * e;
res.y = s * e;
return res;
}
关于cuComplex.h 和 exp(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9860711/