/** * @fast_fourier_transform.cpp * @brief A fast Fourier transform (FFT) is an algorithm that computes the * discrete Fourier transform (DFT) of a sequence, or its inverse (IDFT). * @details * https://medium.com/@aiswaryamathur/understanding-fast-fourier-transform-from-scratch-to -solve-polynomial-multiplication-8018d511162f * @author [Ameya Chawla](https://github.com/ameyachawlaggsipu) */ #include//Standard Library for input and output #include//For sine,cosine functions #include//For storing points and coefficents #include //For Assertions # define pi 3.14159265358979323846 using namespace std; /** * @brief FastFourierTransform is a recursive function which returns list of complex numbers * @param p List of Coefficents in form of complex numbers * @param n Count of elements in list p * @returns p if n==1 * @returns y if n!=1 */ complex* FastFourierTransform(complex*p,int n) { if(n==1) return p; ///Base Case To return complex om=complex(cos(2*pi/n),sin(2*pi/n)); ///Calculating value of omega complex *pe= new complex[n/2]; /// Coefficents of even power complex *po= new complex[n/2]; ///Coefficents of odd power int k1=0,k2=0; for(int j=0;j*ye=FastFourierTransform(pe,n/2);///Recursive Call complex*yo=FastFourierTransform(po,n/2);///Recursive Call complex*y=new complex[n];///Final value representation list for(int i=0;i t1[2]={1,2};///Test case 1 complex t2[4]={1,2,3,4};///Test case 2 int n1=sizeof(t1)/sizeof(complex); int n2=sizeof(t2)/sizeof(complex); complex r1[2]={{3,0},{-1,0} };///True Answer for test case 1 complex r2[4]={{10,0},{-2,-2},{-2,0},{-2,2} };///True Answer for test case 2 complex *o1=FastFourierTransform(t1,n1); complex *o2=FastFourierTransform(t2,n2); for(int i=0;ireal()<0.000000000001 and r1[i].imag()-o1->imag()<0.000000000001 ); o1++; } for(int i=0;ireal()<0.000000000001 and r2[i].imag()-o2->imag()<0.000000000001 ); o2++; } } /** * @brief Main function * @param argc commandline argument count (ignored) * @param argv commandline array of arguments (ignored) * @returns 0 on exit */ int main(int argc, char const *argv[]) { test(); return 0; }