我需要计算 C 中的虚指数。

据我所知,C中没有复数库。有可能得到 e^xexp(x)math.h, ,但是我如何计算的值 e^(-i), , 在哪里 i = sqrt(-1)?

有帮助吗?

解决方案

请注意,复数的指数等于:

e^(ix) = cos(x)+i*sin(x)

然后:

e^(-i) = cos(-1)+i*sin(-1)

其他提示

在C99中,有一个 complex 类型。包括 complex.h;您可能需要链接到 -lm 关于海湾合作委员会。请注意,Microsoft Visual C 不支持 complex;如果你需要使用这个编译器,也许你可以加入一些 C++ 并使用 complex 模板。

I 被定义为虚数单位,并且 cexp 做幂运算。完整代码示例:

#include <complex.h>
#include <stdio.h>

int main() {
    complex x = cexp(-I);
    printf("%lf + %lfi\n", creal(x), cimag(x));
    return 0;
}

man 7 complex 了解更多信息。

使用 欧拉公式 你有那个 e^-i == cos(1) - i*sin(1)

e^-j 只是 cos(1) - j*sin(1), ,因此您可以使用实函数生成实部和虚部。

只需使用笛卡尔形式

如果 z = m*e^j*(arg);

re(z) = m * cos(arg);
im(z) = m * sin(arg);

调用 C++ 函数是您的解决方案吗?C++ STL 有一个很好的复杂类,boost 也必须提供一些不错的选项。用 C++ 编写一个函数并将其声明为“extern C”

extern "C" void myexp(float*, float*);

#include <complex>

using std::complex;

void myexp (float *real, float *img )
{
  complex<float> param(*real, *img);
  complex<float> result = exp (param);
  *real = result.real();
  *img = result.imag();
}

然后您可以从您依赖的任何 C 代码(Ansi-C、C99,...)调用该函数。

#include <stdio.h>

void myexp(float*, float*);

int main(){
    float real = 0.0;
    float img = -1.0;
    myexp(&real, &img);
    printf ("e^-i = %f + i* %f\n", real, img);
    return 0;
}

在C++中可以直接完成:std::exp(std::complex(0, -1));

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top