// Hello. This is pratiikgoogler here. This code is written in order to find the fast fourier transform of a polynomial.
// Special thanks to Cormen and Emaxx for elaborating the code and making life easy for me. hope this helps everyone.
#include<iostream>
#include<complex>
#include<vector>
#include<cmath>
#define PI acos(-1)
usingnamespace std;
void fft(vector<complex<double>>a,bool show ){// we take the complex vector although the coefficient are passed as integers but we cannot be sure that the recursive calls would take just integers. Just to be on the saf side, we have taken complex data type.
int n = a.size();
if(n==1)
return;
vector<complex<double>>a1(n/2),a2(n/2);
for(int i=0;i*2<n;i++){
a1[i]= a[2*i];// As said in the blog, we take coefficients of even powers in one array and those of odd powers in another.
a2[i]= a[2*i+1];// Done as said earlier.
}
fft(a1,0);// We compute the FFT of a1 recursively. Divide and Conquer.
fft(a2,0);// We compute the FFT of a2 recursively. DAC .
double angle =2*PI/n;// initialise varaible to 2pi/n, to help in computing nth roots of unity.
complex<double> w(1),wn(cos(angle),sin(angle));
for(int i=0;i*2<n;i++){
a[i]= a1[i]+ w*a2[i];// one value of a[i]
a[i+n/2]= a1[i]- w*a2[i];// rotate a[i] by 180 degrees and we get a[i+n/2]
}
w = w*wn;// we multiply with powers of omega hece this step.
if(show)
for(int i=0;i<n;i++)
cout<<a[i]<<" ";// Display the values of the corresponding values of the fourier transform i.re, the points which satisfies the polynomial.