polynomial.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // (C) Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_TOOLS_POLYNOMIAL_HPP
  6. #define BOOST_MATH_TOOLS_POLYNOMIAL_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/assert.hpp>
  11. #include <boost/math/tools/rational.hpp>
  12. #include <boost/math/tools/real_cast.hpp>
  13. #include <boost/math/special_functions/binomial.hpp>
  14. #include <vector>
  15. #include <ostream>
  16. #include <algorithm>
  17. namespace boost{ namespace math{ namespace tools{
  18. template <class T>
  19. T chebyshev_coefficient(unsigned n, unsigned m)
  20. {
  21. BOOST_MATH_STD_USING
  22. if(m > n)
  23. return 0;
  24. if((n & 1) != (m & 1))
  25. return 0;
  26. if(n == 0)
  27. return 1;
  28. T result = T(n) / 2;
  29. unsigned r = n - m;
  30. r /= 2;
  31. BOOST_ASSERT(n - 2 * r == m);
  32. if(r & 1)
  33. result = -result;
  34. result /= n - r;
  35. result *= boost::math::binomial_coefficient<T>(n - r, r);
  36. result *= ldexp(1.0f, m);
  37. return result;
  38. }
  39. template <class Seq>
  40. Seq polynomial_to_chebyshev(const Seq& s)
  41. {
  42. // Converts a Polynomial into Chebyshev form:
  43. typedef typename Seq::value_type value_type;
  44. typedef typename Seq::difference_type difference_type;
  45. Seq result(s);
  46. difference_type order = s.size() - 1;
  47. difference_type even_order = order & 1 ? order - 1 : order;
  48. difference_type odd_order = order & 1 ? order : order - 1;
  49. for(difference_type i = even_order; i >= 0; i -= 2)
  50. {
  51. value_type val = s[i];
  52. for(difference_type k = even_order; k > i; k -= 2)
  53. {
  54. val -= result[k] * chebyshev_coefficient<value_type>(static_cast<unsigned>(k), static_cast<unsigned>(i));
  55. }
  56. val /= chebyshev_coefficient<value_type>(static_cast<unsigned>(i), static_cast<unsigned>(i));
  57. result[i] = val;
  58. }
  59. result[0] *= 2;
  60. for(difference_type i = odd_order; i >= 0; i -= 2)
  61. {
  62. value_type val = s[i];
  63. for(difference_type k = odd_order; k > i; k -= 2)
  64. {
  65. val -= result[k] * chebyshev_coefficient<value_type>(static_cast<unsigned>(k), static_cast<unsigned>(i));
  66. }
  67. val /= chebyshev_coefficient<value_type>(static_cast<unsigned>(i), static_cast<unsigned>(i));
  68. result[i] = val;
  69. }
  70. return result;
  71. }
  72. template <class Seq, class T>
  73. T evaluate_chebyshev(const Seq& a, const T& x)
  74. {
  75. // Clenshaw's formula:
  76. typedef typename Seq::difference_type difference_type;
  77. T yk2 = 0;
  78. T yk1 = 0;
  79. T yk = 0;
  80. for(difference_type i = a.size() - 1; i >= 1; --i)
  81. {
  82. yk2 = yk1;
  83. yk1 = yk;
  84. yk = 2 * x * yk1 - yk2 + a[i];
  85. }
  86. return a[0] / 2 + yk * x - yk1;
  87. }
  88. template <class T>
  89. class polynomial
  90. {
  91. public:
  92. // typedefs:
  93. typedef typename std::vector<T>::value_type value_type;
  94. typedef typename std::vector<T>::size_type size_type;
  95. // construct:
  96. polynomial(){}
  97. template <class U>
  98. polynomial(const U* data, unsigned order)
  99. : m_data(data, data + order + 1)
  100. {
  101. }
  102. template <class U>
  103. polynomial(const U& point)
  104. {
  105. m_data.push_back(point);
  106. }
  107. // copy:
  108. polynomial(const polynomial& p)
  109. : m_data(p.m_data) { }
  110. template <class U>
  111. polynomial(const polynomial<U>& p)
  112. {
  113. for(unsigned i = 0; i < p.size(); ++i)
  114. {
  115. m_data.push_back(boost::math::tools::real_cast<T>(p[i]));
  116. }
  117. }
  118. // access:
  119. size_type size()const { return m_data.size(); }
  120. size_type degree()const { return m_data.size() - 1; }
  121. value_type& operator[](size_type i)
  122. {
  123. return m_data[i];
  124. }
  125. const value_type& operator[](size_type i)const
  126. {
  127. return m_data[i];
  128. }
  129. T evaluate(T z)const
  130. {
  131. return boost::math::tools::evaluate_polynomial(&m_data[0], z, m_data.size());;
  132. }
  133. std::vector<T> chebyshev()const
  134. {
  135. return polynomial_to_chebyshev(m_data);
  136. }
  137. // operators:
  138. template <class U>
  139. polynomial& operator +=(const U& value)
  140. {
  141. if(m_data.size() == 0)
  142. m_data.push_back(value);
  143. else
  144. {
  145. m_data[0] += value;
  146. }
  147. return *this;
  148. }
  149. template <class U>
  150. polynomial& operator -=(const U& value)
  151. {
  152. if(m_data.size() == 0)
  153. m_data.push_back(-value);
  154. else
  155. {
  156. m_data[0] -= value;
  157. }
  158. return *this;
  159. }
  160. template <class U>
  161. polynomial& operator *=(const U& value)
  162. {
  163. for(size_type i = 0; i < m_data.size(); ++i)
  164. m_data[i] *= value;
  165. return *this;
  166. }
  167. template <class U>
  168. polynomial& operator +=(const polynomial<U>& value)
  169. {
  170. size_type s1 = (std::min)(m_data.size(), value.size());
  171. for(size_type i = 0; i < s1; ++i)
  172. m_data[i] += value[i];
  173. for(size_type i = s1; i < value.size(); ++i)
  174. m_data.push_back(value[i]);
  175. return *this;
  176. }
  177. template <class U>
  178. polynomial& operator -=(const polynomial<U>& value)
  179. {
  180. size_type s1 = (std::min)(m_data.size(), value.size());
  181. for(size_type i = 0; i < s1; ++i)
  182. m_data[i] -= value[i];
  183. for(size_type i = s1; i < value.size(); ++i)
  184. m_data.push_back(-value[i]);
  185. return *this;
  186. }
  187. template <class U>
  188. polynomial& operator *=(const polynomial<U>& value)
  189. {
  190. // TODO: FIXME: use O(N log(N)) algorithm!!!
  191. BOOST_ASSERT(value.size());
  192. polynomial base(*this);
  193. *this *= value[0];
  194. for(size_type i = 1; i < value.size(); ++i)
  195. {
  196. polynomial t(base);
  197. t *= value[i];
  198. size_type s = size() - i;
  199. for(size_type j = 0; j < s; ++j)
  200. {
  201. m_data[i+j] += t[j];
  202. }
  203. for(size_type j = s; j < t.size(); ++j)
  204. m_data.push_back(t[j]);
  205. }
  206. return *this;
  207. }
  208. private:
  209. std::vector<T> m_data;
  210. };
  211. template <class T>
  212. inline polynomial<T> operator + (const polynomial<T>& a, const polynomial<T>& b)
  213. {
  214. polynomial<T> result(a);
  215. result += b;
  216. return result;
  217. }
  218. template <class T>
  219. inline polynomial<T> operator - (const polynomial<T>& a, const polynomial<T>& b)
  220. {
  221. polynomial<T> result(a);
  222. result -= b;
  223. return result;
  224. }
  225. template <class T>
  226. inline polynomial<T> operator * (const polynomial<T>& a, const polynomial<T>& b)
  227. {
  228. polynomial<T> result(a);
  229. result *= b;
  230. return result;
  231. }
  232. template <class T, class U>
  233. inline polynomial<T> operator + (const polynomial<T>& a, const U& b)
  234. {
  235. polynomial<T> result(a);
  236. result += b;
  237. return result;
  238. }
  239. template <class T, class U>
  240. inline polynomial<T> operator - (const polynomial<T>& a, const U& b)
  241. {
  242. polynomial<T> result(a);
  243. result -= b;
  244. return result;
  245. }
  246. template <class T, class U>
  247. inline polynomial<T> operator * (const polynomial<T>& a, const U& b)
  248. {
  249. polynomial<T> result(a);
  250. result *= b;
  251. return result;
  252. }
  253. template <class U, class T>
  254. inline polynomial<T> operator + (const U& a, const polynomial<T>& b)
  255. {
  256. polynomial<T> result(b);
  257. result += a;
  258. return result;
  259. }
  260. template <class U, class T>
  261. inline polynomial<T> operator - (const U& a, const polynomial<T>& b)
  262. {
  263. polynomial<T> result(a);
  264. result -= b;
  265. return result;
  266. }
  267. template <class U, class T>
  268. inline polynomial<T> operator * (const U& a, const polynomial<T>& b)
  269. {
  270. polynomial<T> result(b);
  271. result *= a;
  272. return result;
  273. }
  274. template <class charT, class traits, class T>
  275. inline std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& os, const polynomial<T>& poly)
  276. {
  277. os << "{ ";
  278. for(unsigned i = 0; i < poly.size(); ++i)
  279. {
  280. if(i) os << ", ";
  281. os << poly[i];
  282. }
  283. os << " }";
  284. return os;
  285. }
  286. } // namespace tools
  287. } // namespace math
  288. } // namespace boost
  289. #endif // BOOST_MATH_TOOLS_POLYNOMIAL_HPP