## CSDN博客

### 多项式求值

// 版权所有(C) 梁意, 2004 // 最后修改: 2004.7.杭州
#ifndef Polynomial_HEADER
#define Polynomial_HEADER
#include<vector>
using namespace std;
template<typename T,typename Container=vector<T> >
class Polynomial: public Container
{
public:
   Polynomial() { }
   Polynomial(const T *pT,size_t N)
   {
      copy(pT,pT+N,insert_iterator<Polynomial<T,Container> >(*this,begin()));
   }
   Polynomial(const Polynomial<T,Container> &other)   {
      copy(other.begin(),other.end(),insert_iterator<Polynomial<T,Container> >(*this,begin()));
   }
   template<typename _In>
   inline _In QinJiuShao_evaluate(_In x,_In identity)//identity 为单位元,如: 整数1、浮点数1.0、单位矩阵
   {
      size_t N=size();
      if(N==0) return identity;
      iterator iter=begin();
      if(N==1) return *iter;
      erator iter2=end();
      _In Res=(*iter)*x;
      iter++; iter2--;
      for(;iter!=iter2;iter++)
      {
         Res=(Res+(*iter)*identity)*x;
      }
      return Res+(*iter2)*identity;
   }
   template<typename _In>
   inline  _In QinJiuShao_evaluate(_In x) //这个更快
   {
   size_t N=size();
   if(N==0) return 1.0;
   iterator iter=begin();
   if(N==1) return *iter;
   iterator iter2=end();
   _In Res=(*iter)*x;
   iter2--;
   iter++;
   for(;iter!=iter2;iter++)
   {
      Res=(Res+(*iter))*x;
   }
   return Res+(*iter2);
   }
};
#endif
0 0