評估給定度數的多項式和已知係數(按順序)的最快已知算法是什麼? 我試着做以下方式:在特定值處評估多項式的最快方法
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