5位精度的复数DFT

时间:2018-08-26 11:36:54

标签: c++ precision complex-numbers polynomial-math polynomials

这是我要在C ++中解决的问题;

给出一个以度 d-1 指定的多项式 A(x),其系数为a0,a1,a2,a3 ... ad-1。假设 a 是这些系数的d维向量。给定一个整数n输出矢量DFT(a,n),其中DFT(a,n)定义为see here

INPUT and OUTPUT format

我的代码在这里

    #include<bits/stdc++.h>
    #include<complex>
    #include <iomanip>
    using namespace std;

    void DfT(vector<double> &a, vector<double> &b, int n, int d){
        vector<double> ans1, ans2;
        for(int i=0;i<n;i++){
            double k=(M_PI*2.00000)/n;
            k=round(k*1e5)/1e5;
            //cout<<k<<" ";
            double r=cos(i*k), im=sin(i*k);
            r=round(r*1e5)/1e5;
            im=round(im*1e5)/1e5;
            double wr=1,wi=0;
        for(int j=0;j<d;j++){
            double a1=wr*a[j]-wi*b[j];
            a1=round(a1*1e5)/1e5;
            double b1=wr*b[j]+wi*a[j];
            b1=round(b1*1e5)/1e5;
            ans1.push_back(a1);
            ans2.push_back(b1);
            double temp=wr;
            wr=wr*r-wi*im;
            wr=round(wr*1e5)/1e5;
            wi=temp*im+r*wi;
            wi=round(wi*1e5)/1e5;
        }

    }
        for(auto &it:ans1)cout<<fixed<<setprecision(5)<<it<<" "<<endl;
        for(auto &it:ans2)cout<<fixed<<setprecision(5)<<it<<" ";

    }
    int main(){
        int d,n;      //d -1 is the degree of the polynomial A(x)
        cin>>d;
        vector<double> a,b;
        for(int i=0;i<d;i++){
            double t;
            cin>>t;
            a.push_back(t);   // the real part of polynomial
        }
        for(int i=0;i<d;i++){
            double t;
            cin>>t;
            b.push_back(t);           // imaginary part of polynomial
        }
        cin>>n;               // n is surely power of 2 and we find 
        //nth root of infinity and put the value of roots in polynomial A(x) as x.
        FfT(a,b,n,d);    //we have to output the value of polynomial ie A(w1),A(w2).. with precision to 5 decimal places 
        return 0;
    }

逻辑似乎很好,但看起来答案的准确性也有所下降。仅少数测试用例通过了。 请帮助

0 个答案:

没有答案