Hornerの方法:問題2.34
問題2.33のlengthにとても良く似ている問題と気づけば簡単。
----
typedef
double CoefficientRing;
//
Horner's method
const
CoefficientRing evaluateHorner
(const
CoefficientRing& x, const List& polynomial)
{
function<CoefficientRing(CoefficientRing,CoefficientRing)>
riseHorner=[x]
(const CoefficientRing& a_i,const
CoefficientRing& higherTerms)
{return(a_i+x*higherTerms);};
const CoefficientRing zero(0);
return(accumulate(riseHorner,zero,polynomial));
}
int
main(int argc, char** argv)
{
typedef double Coefficient;
const auto polynomialCoefficients
(makeList(1.0,3.0,0.0,5.0,0.0,1.0));
cout<<"1+3x+5x^3+x^5 (x=2) =
"
<<evaluateHorner(2.0,polynomialCoefficients)<<endl;
return(0);
}
----
出力
----
1+3x+5x^3+x^5
(x=2) = 79
0 件のコメント :
コメントを投稿