在 C 中计算 e^(-j)

发布于 2024-09-02 05:35:25 字数 160 浏览 2 评论 0原文

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

据我所知,C 中没有复数库。可以使用 exp(x)e^x math.h 的 >,但是如何计算 e^(-i) 的值,其中 i = sqrt(-1)

I need to compute imaginary exponential in C.

As far as I know, there is no complex number library in C. It is possible to get e^x with exp(x) of math.h, but how can I compute the value of e^(-i), where i = sqrt(-1)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

妥活 2024-09-09 05:35:25

在 C99 中,有一个complex 类型。包含complex.h;您可能需要在 gcc 上链接 -lm 。请注意,Microsoft Visual C 不支持复杂;如果您需要使用此编译器,也许您可​​以加入一些 C++ 并使用 <代码>复杂模板。

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 了解更多信息。

In C99, there is a complex type. Include complex.h; you may need to link with -lm on gcc. Note that Microsoft Visual C does not support complex; if you need to use this compiler, maybe you can sprinkle in some C++ and use the complex template.

I is defined as the imaginary unit, and cexp does exponentiation. Full code example:

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

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

See man 7 complex for more information.

剑心龙吟 2024-09-09 05:35:25

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

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

那么:

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

Note that exponent of complex number equals:

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

Then:

e^(-i) = cos(-1)+i*sin(-1)
我偏爱纯白色 2024-09-09 05:35:25

使用欧拉公式,您可以得到e^-i == cos(1) - i*sin(1)

Using the Euler's Formula you have that e^-i == cos(1) - i*sin(1)

橘虞初梦 2024-09-09 05:35:25

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

e^-j is just cos(1) - j*sin(1), so you can just generate the real and imaginary parts using real functions.

后知后觉 2024-09-09 05:35:25

只需使用笛卡尔形式

if z = m*e^j*(arg);

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

Just use the cartesian form

if z = m*e^j*(arg);

re(z) = m * cos(arg);
im(z) = m * sin(arg);
比忠 2024-09-09 05:35:25

调用 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;
}

Is calling a c++ function a solution for you? The C++ STL has a nice complex-class and boost also has to offer some nice options. Write a function in C++ and declare it "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();
}

Then you can call the function from whatever C-code you rely on ( 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;
}
雅心素梦 2024-09-09 05:35:25

在C++中可以直接完成:

std::exp(std::complex<double>(0, -1));

In C++ it can be done directly:

std::exp(std::complex<double>(0, -1));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文