From 5c1c4a4a3ecc6f48c44c1ce6692b84c6e49f4649 Mon Sep 17 00:00:00 2001 From: Ameya Chawla <88154798+ameyachawlaggsipu@users.noreply.github.com> Date: Fri, 22 Oct 2021 09:55:19 +0530 Subject: [PATCH] fix : fixed segmentation fault error --- numerical_methods/fast_fourier_transform.cpp | 107 ++++++++++--------- 1 file changed, 56 insertions(+), 51 deletions(-) diff --git a/numerical_methods/fast_fourier_transform.cpp b/numerical_methods/fast_fourier_transform.cpp index 612699525..84b548e93 100644 --- a/numerical_methods/fast_fourier_transform.cpp +++ b/numerical_methods/fast_fourier_transform.cpp @@ -37,49 +37,61 @@ namespace numerical_methods { * @returns p if n==1 * @returns y if n!=1 */ -std::complex *FastFourierTransform(std::complex *p, - uint64_t n) { - double pi = 2 * asin(1.0); /// Declaring value of pi - if (n == 1) { - return p; /// Base Case To return - } +std::complex* FastFourierTransform(std::complex*p,uint64_t n) +{ - auto om = std::complex( - cos(2 * pi / n), sin(2 * pi / n)); /// Calculating value of omega + if(n==1){ + + return p; ///Base Case To return + + } - /// auto is used inplace of std::complex to reduce complexity - auto *pe = new std::complex[n / 2]; /// Coefficents of even power + double pi = 2 * asin(1.0); /// Declaring value of pi + + auto om=std::complex(cos(2*pi/n),sin(2*pi/n)); ///Calculating value of omega - auto *po = new std::complex[n / 2]; /// Coefficents of odd power + auto *pe= new std::complex[n/2]; /// Coefficents of even power - uint64_t k1 = 0, k2 = 0; - for (uint64_t j = 0; j < n; j++) { - if (j % 2 == 0) { - pe[k1++] = p[j]; /// Assigning values of even coefficents + auto *po= new std::complex[n/2]; ///Coeeficents of odd power - } else - po[k2++] = p[j]; /// Assigning value of odd coefficents - } + int k1=0,k2=0; + for(int j=0;j[n]; /// Final value representation list + } - for (uint64_t i = 0; i < n / 2; i++) { - y[i] = ye[i] + pow(om, i) * yo[i]; /// Updating the first n/2 elements - y[i + n / 2] = - ye[i] - pow(om, i) * yo[i]; /// Updating the last n/2 elements - } - delete[] ye; /// Deleting dynamic array ye + auto *ye=FastFourierTransform(pe,n/2); ///Recursive Call + + auto *yo=FastFourierTransform(po,n/2); ///Recursive Call + + auto *y=new std::complex[n]; /// Final value representation list + + k1=0,k2=0; + + for(int i=0;i *FastFourierTransform(std::complex *p, * in predicted and true value is less than 0.000000000001. * @returns void */ -static void test() { + + static void test() { /* descriptions of the following test */ - auto *t1 = new std::complex[2]; /// Test case 1 - t1[0] = {1, 0}; - t1[1] = {2, 0}; - /// auto is used inplace of std::complex to reduce complexity - auto *t2 = new std::complex[4]; /// Test case 2 - t2[0] = {1, 0}; - t2[1] = {2, 0}; - t2[2] = {3, 0}; - t2[3] = {4, 0}; - - uint8_t n1 = sizeof(t1) / sizeof(std::complex); - uint8_t n2 = sizeof(t2) / sizeof(std::complex); + std::complex t1[2]={1,2}; /// Test case 1 + std::complex t2[4]={1,2,3,4}; /// Test case 2 + uint8_t n1 = 2; + uint8_t n2 = 4; std::vector> r1 = { {3, 0}, {-1, 0}}; /// True Answer for test case 1 @@ -128,10 +133,7 @@ static void test() { o2++; } - delete[] o1; /// Deleting dynamic array o1 - delete[] o2; /// Deleting dynamic array o2 - delete[] t1; /// Deleting dynamic array t1 - delete[] t2; /// Deleting dynamic array t2 + } /** @@ -141,7 +143,10 @@ static void test() { * calls automated test function to test the working of fast fourier transform. * @returns 0 on exit */ -int main(int argc, char const *argv[]) { - test(); // run self-test implementations - return 0; + +int main(int argc, char const *argv[]) +{ + + test();/// run self-test implementations + return 0; }