我想计算一个复数的指数,例如 2+3i
。
我已经知道这个公式了
exp( i z ) = cos(z) + i sin(z)
OpenCV 有内置函数吗?如果是,请举例说明一下好吗?
最佳答案
在 complex.h
中声明的 cexp( )
函数有什么问题?为什么要使用 OpenCV 而不是标准库?
#include <complex.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
double complex z = 2 + 3*I;
double complex w = cexp(z);
printf("%f + %fi\n", creal(w), cimag(w));
return 0;
}
如果您的目标平台不提供复杂的类型或操作,您可以使用以下方法作为快速而简单的解决方案:
struct mycomplex { double real; double imag; }
struct mycomplex my_exp(struct mycomplex z) {
struct mycomplex w;
w.real = exp(z.real)*cos(z.imag);
w.imag = exp(z.real)*sin(z.imag);
return w;
}
最后,由于您使用的是 MSVC,这是一个非常基本的 C++ 示例:
#include <complex>
#include <iostream>
int main(int argc, char *argv[]) {
auto z = std::complex<double>(2,3);
auto w = std::exp(z);
std::cout << w << std::endl;
return 0;
}
关于c++ - 求复数的指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25771761/