gpt4 book ai didi

C++ - 改进复数数学的计算时间

转载 作者:太空宇宙 更新时间:2023-11-04 14:08:16 25 4
gpt4 key购买 nike

我正在尝试用 C++ 计算二维数组的复数。代码运行非常缓慢,我已经缩小了 exp 函数的主要原因(当我注释掉该行时程序运行很快,即使我有 4 个嵌套循环)。

int main() {

typedef vector< complex<double> > complexVect;
typedef vector<double> doubleVect;

const int SIZE = 256;
vector<doubleVect> phi_w(SIZE, doubleVect(SIZE));
vector<complexVect> phi_k(SIZE, complexVect(SIZE));
complex<double> i (0, 1), cmplx (0, 0);
complex<double> temp;
int x, y, t, k, w;
double dk = 2.0*M_PI / (SIZE-1);
double dt = M_PI / (SIZE-1);
int xPos, yPos;
double arg, arg2, arg4;
complex<double> arg3;
double angle;
vector<complexVect> newImg(SIZE, complexVect(SIZE));

for (x = 0; x < SIZE; ++x) {
xPos = -127 + x;
for (y = 0; y < SIZE; ++y) {
yPos = -127 + y;
for (t = 0; t < SIZE; ++t) {
temp = cmplx;
angle = dt * t;
arg = xPos * cos(angle) + yPos * sin(angle);
for (k = 0; k < SIZE; ++k) {
arg2 = -M_PI + dk*k;
arg3 = exp(-i * arg * arg2);
arg4 = abs(arg) * M_PI / (abs(arg) + M_PI);
temp = temp + arg4 * arg3 * phi_k[k][t];
}
}
newImg[y][x] = temp;
}
}
}

有什么方法可以缩短计算时间吗?我尝试使用以下辅助函数,但没有明显帮助。

complex<double> complexexp(double arg) {
complex<double> temp (sin(arg), cos(arg));
return temp;
}

我正在使用 clang++ 来编译我的代码

编辑:我认为问题在于我正在尝试计算复数。如果我只使用欧拉公式计算单独数组中的实部和虚部,而不必处理复杂的类,会不会更快?

最佳答案

关于C++ - 改进复数数学的计算时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16008463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com