评估给定度数的多项式和已知系数(按顺序)的最快已知算法是什么? 我试着做以下方式:在特定值处评估多项式的最快方法
long long int evaluatepoly(long long int* coeffa0,long long int degree,long long int x)
{
/*coeffa0 is the coeffecient array in order x^0,x^1,x^2....degree->degree of polynomial
and x is the value where the polynomial is to be evaluated*/
if(degree==1)
{
return (coeffa0[0] + (coeffa0[1])*x);
}
else if(degree==0)
return coeffa0[0];
else{
long long int odd,even,n=degree;
if(degree%2==0){
odd=(n/2);
even=(n/2)+1;
}
else{
odd=(n+1)/2;
even=(n+1)/2;
}
long long int oddcoeff[odd],evencoeff[even];
int i=0;
while(i<=degree)
{
if(i%2==0)
evencoeff[i/2]=coeffa0[i];
else
oddcoeff[i/2]=coeffa0[i];
i++;
}
int y=x*x;
return (evaluatepoly(evencoeff,(even-1),y) + x*(evaluatepoly(oddcoeff,(odd-1),y)));
}
}
我是初学者所以在改善上面的代码是建议也欢迎(在C/C++)。
快速还是精确?有时你不能同时获得 – user463035818
一个“常用”的方法是从最高度的系数开始,然后乘以“x”并添加下一个系数:“res = a [n]; res = x * res + a [n - 1]; res = x * res + a [n - 2]; ...; res = x * res + a [0];'。有了这个,你有n次乘法和n次加法。 – Holt
这是霍纳的方法吧?...... – yobro97