【问题标题】:Computing e^(-j) in C在 C 中计算 e^(-j)
【发布时间】:2010-05-14 14:25:09
【问题描述】:

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

据我所知,C 中没有复数库。exp(x)math.h 可以得到e^x,但是我如何计算e^(-i) 的值,其中@987654325 @?

【问题讨论】:

    标签: c complex-numbers exponential


    【解决方案1】:

    在 C99 中,有一个 complex 类型。包括complex.h;您可能需要在 gcc 上与 -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

    【讨论】:

    • 我需要手动执行此操作。感谢您将来的参考,我会记住这一点。
    【解决方案2】:

    注意复数的指数等于:

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

    然后:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • e^(-k) = cos(k) - i*sin(k),你的意思是。
      • 是的,但在他的情况下 k = 1。在任何情况下,一般形式是 e^(-ki) = cos(k)-i*sin(k),而不是 e^(-k )
      【解决方案4】:

      e^-j 就是cos(1) - j*sin(1),所以你可以使用实函数生成实部和虚部。

      【讨论】:

        【解决方案5】:

        只需使用笛卡尔形式

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

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

        【讨论】:

          【解决方案6】:

          调用 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++ 对我没有好处
          【解决方案7】:

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

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

          【讨论】:

          • 这个问题用 C 标记,而不是 C++
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-27
          • 1970-01-01
          • 2017-04-04
          相关资源
          最近更新 更多