如何获得输出中带有'i'(虚值)的复数的输出

how to get output of complex number with 'i' (imaginary value ) in the output

本文关键字:输出 何获得 虚值      更新时间:2023-10-16

这是我在c++中使用bluestein算法的代码4fft计算

#include <iostream>
#include <cmath>
#include <complex>
#define pi 3.14
using namespace std;

int main()
{
    int n, k, N;

    std::complex<double> y, z, sum = 0,  x[] = { 1, 2, 2, 1 };
    int i = sqrt(-1);
    N = 4;
    for (k = 0; k <= N - 1; k++)
    {
        y = exp(pi*k*N*k*i);
        for (n = 0; n <= N - 1; n++)
        {
            z = exp((pi*(k*k - 2 * k*n*i)) / N);
            sum += (x[n] *  z);
        }
        x[k] = (sum * y);
         cout << "The fft of x[n] is =  " << x[k] << endl;
    }
    return 0;
}

当v在visualstudio2013中运行时,我需要输出为{6,-1-i,0,-1+j}。如果可能的话,plz可以帮助我一般地声明输入数组。

<lt;运算符是这样重载的http://en.cppreference.com/w/cpp/numeric/complex/operator_ltltgtgt

然而,您可以使用它来获得所需的输出:

cout << "The fft of x[n] is =  " << x[k].real() << "+"<< x[k].imag()<< "i" << endl;