与えられた次数の多項式と既知の係数を(順番に)評価する最も速いアルゴリズムは何ですか? (C++/Cで)上記のコードの改善の推奨事項も大歓迎ですので、私は初心者です特定の値で多項式を評価する最速の方法
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)));
}
}
: は、私はそれを次の方法を実行しようとしました。多項式を評価する
高速または正確ですか?時には両方を得ることができない場合があります – 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