I am currently writing a small tool which should help me check whether my manually calculated fourier vectors are correct. Now i need the n-th Root of Unity specified by omega = exp(2*pi*i / n)
. Can somebody explain me how to represent this omega
as a complex
in C++?
Use Euler's formula :
exp(2πi/n) = cos(2π/n) + i sin(2π/n)
Then it's easy:
complex<double> rootOfUnity(cos(TWOPI/n), sin(TWOPI/n));
(replace TWOPI with either a macro available on your system or just the value of 2π however you see fit).
Well, the real and imaginary parts of the twiddle factor omega is just:
double angle = 2*pi/n;
double real = cos(angle);
double imaj = sin(angle);
complex<double> omega(real, imaj);
There is a function that returns a complex number using polar coordinates:
#include<complex>
complex polar(const T& rho)
complex polar(const T& rho, const T& theta)
where rho
is the magnitude, and theta
is the angle in radians.
In this case, rho
is always 1.0.
const double pi = 3.141592653589793238462643383279;
double omega = polar(1.0, 2*pi*i/n);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.