具有5位精度的复数DFT 这是我在C++中试图解决的问题;p>
给定一个多项式a(x),该多项式用次数d-1指定,其系数为a0、a1、a2、a3…ad-1。设a为这些系数的d维向量。给定一个整数n输出向量DFT(a,n),其中DFT(a,n)定义为 我的代码是具有5位精度的复数DFT 这是我在C++中试图解决的问题;p>,c++,precision,complex-numbers,polynomial-math,polynomials,C++,Precision,Complex Numbers,Polynomial Math,Polynomials,给定一个多项式a(x),该多项式用次数d-1指定,其系数为a0、a1、a2、a3…ad-1。设a为这些系数的d维向量。给定一个整数n输出向量DFT(a,n),其中DFT(a,n)定义为 我的代码是 #include<bits/stdc++.h> #include<complex> #include <iomanip> using namespace std; void DfT(vector<double>
#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;
}
#包括
#包括
#包括
使用名称空间std;
无效DfT(向量a、向量b、整数n、整数d){
向量ans1,ans2;
对于(int i=0;iIt看起来您应该使用全精度进行计算,但使用5位数字打印结果。我将1e5替换为1e7,但当我使用精度5打印时,仍然存在相同的错误。在某些测试案例中,甚至可以删除计算中的所有舍入,并让输出流的精度处理舍入。好的d我也这么认为…我想现在它与逻辑有关了…因为精度部分无法提高到您建议的水平,并且在某些情况下仍然存在TLEbits/stdc++
头不是标准的,不应该包括在内。您将需要vector
、cmath
、iostream
以及其他。