I am working on fast fourier transform where I have to use std::complex<> template class types. I have a situation where I want to calculate nth roots of unity as follows
w = pow(e, 2*pi*j/n)
Where e is eulers constant, and j=sqrt(-1.0); But this doesnt work for me as I both tried type "complex<double>" as well as "double" for "w". But I get "error:more than one instance of overloaded function pow matches the argument list". So the problem seems with both pow function and type of "w"..... Can anybody help me in this? Thanks in advance:)

7 Years
Discussion Span
Last Post by akhal

Well, I think you need to brush-up on some complex algebra stuff...

With Euler's identity, this e to the power of 2*Pi*j/n is simply a rotation by 2*Pi/n. You can easily construct this complex number by using the "polar" function, as so:

w = polar(1.0, 2*pi/n);

I couldnt make "n" complex, but even if I can, I dont want to, because my code is part of a parallel program and all my intentions are to time optimize my code, so making more complex constructors may slow down my program. Anyway, restating my problem; I have to code the equation: w=pow(e,2*pi*j/n) (where j=sqrt(-1) )
So since the solution below didn't work.

#define sqrt(-1.0) j
complex<double> w = pow(e,2*pi*j/n);

Thats why I tried this way

#define TWO_PI (6.2831853071795864769252867665590057683943L)
#define J sqrt(-1.0)
#define e 2.7182818284590452353602875

complex<double> w = pow(e, TWO_PI*J/n);

But this even didnt work with showing some errors. So I rather tried my last resort as follows

#define TWO_PI (6.2831853071795864769252867665590057683943L)
#define e 2.7182818284590452353602875

complex<double> TWO_PI_J(0,TWO_PI);
/* Overriding complex division so I can divide complex number by a real number too */
template<class T>
complex<T> complex<T>::_Div(T n) { this.real/=n; this.imag/=n }
void my_function(complex<double>* A, int b)
	int j=1, nth=1;
	complex<double> w; //double w;
	for(int s=1;s<=b;s++)
		w = pow(e, TWO_PI_J._Div(nth));
		nthroot(A+j, nth, w);
		nth = nth*2;
		j = j*2;

So now visual studio even before compilation pops us like "no instance of template function std::complex<T>::_Div matches the argument list". While I have already provided complex division on real number above.
I need help, and I have put the whole story here so if there is another totally different way to code "pow(e, 2*pi*j/n)", that is more efficient/simple, please let me know.


>>#define J sqrt(-1.0)

there is no such thing as sqrt(-1.0), the parameter to sqrt shouldn't be negative. Instead just imagine the second paramter of complex type is imaginary.

typedef std::complex<float,float> Complex;
typedef std::vector<float> TimeDomainData;
typedef std::vector<Complex> FrequencyDomainData;
int main(){
 const int SAMPLE_POINTS = 1024;
 const TimeDomainData data = generateTimeDomainData(SAMPLE_POINTS); //or something
 FrequencyDomainData freqData;
 for(int n = 0; n < SAMPLE_POINTS; ++n){
    const float theta = 2 * PI * n/SAMPLE_POINTS ; //straight from definition
    const float realValue = std::cos(theta);
    const float imaginaryValue = std::sin(theta)
    freqData.push_back( Complex(realValue,imaginaryValue) );

Edited by firstPerson: n/a


"pow(e,2*pi*j/n)" is a complex number with magnitude 1 and angle 2*pi/n. You just need to create that number, and thus, I reiterate my solution:

complex<double> w = polar(1.0, 2*pi/n);

Thank you so much "firstPerson" and "Mikael Persson" for your so kind information/support. I tried "polar" function but it stated wrong arguments when I tried the given ones, so I rather invented trying like:

theta = TWO_PI/nth;
		complex<double> w(cos(theta), sin(theta));

And I think it is working for me, at least compiler is not complaining. Though I havent confirmed results of my algorithm yet. Also I hope I can use suggested coding sample of mr "firstPerson" too later on, thank you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.