rational.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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_RATIONAL_HPP
  6. #define BOOST_MATH_TOOLS_RATIONAL_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/array.hpp>
  11. #include <boost/math/tools/config.hpp>
  12. #if BOOST_MATH_POLY_METHOD == 1
  13. # define BOOST_HEADER() <BOOST_JOIN(boost/math/tools/detail/polynomial_horner1_, BOOST_MATH_MAX_POLY_ORDER).hpp>
  14. # include BOOST_HEADER()
  15. # undef BOOST_HEADER
  16. #elif BOOST_MATH_POLY_METHOD == 2
  17. # define BOOST_HEADER() <BOOST_JOIN(boost/math/tools/detail/polynomial_horner2_, BOOST_MATH_MAX_POLY_ORDER).hpp>
  18. # include BOOST_HEADER()
  19. # undef BOOST_HEADER
  20. #elif BOOST_MATH_POLY_METHOD == 3
  21. # define BOOST_HEADER() <BOOST_JOIN(boost/math/tools/detail/polynomial_horner3_, BOOST_MATH_MAX_POLY_ORDER).hpp>
  22. # include BOOST_HEADER()
  23. # undef BOOST_HEADER
  24. #endif
  25. #if BOOST_MATH_RATIONAL_METHOD == 1
  26. # define BOOST_HEADER() <BOOST_JOIN(boost/math/tools/detail/rational_horner1_, BOOST_MATH_MAX_POLY_ORDER).hpp>
  27. # include BOOST_HEADER()
  28. # undef BOOST_HEADER
  29. #elif BOOST_MATH_RATIONAL_METHOD == 2
  30. # define BOOST_HEADER() <BOOST_JOIN(boost/math/tools/detail/rational_horner2_, BOOST_MATH_MAX_POLY_ORDER).hpp>
  31. # include BOOST_HEADER()
  32. # undef BOOST_HEADER
  33. #elif BOOST_MATH_RATIONAL_METHOD == 3
  34. # define BOOST_HEADER() <BOOST_JOIN(boost/math/tools/detail/rational_horner3_, BOOST_MATH_MAX_POLY_ORDER).hpp>
  35. # include BOOST_HEADER()
  36. # undef BOOST_HEADER
  37. #endif
  38. #if 0
  39. //
  40. // This just allows dependency trackers to find the headers
  41. // used in the above PP-magic.
  42. //
  43. #include <boost/math/tools/detail/polynomial_horner1_2.hpp>
  44. #include <boost/math/tools/detail/polynomial_horner1_3.hpp>
  45. #include <boost/math/tools/detail/polynomial_horner1_4.hpp>
  46. #include <boost/math/tools/detail/polynomial_horner1_5.hpp>
  47. #include <boost/math/tools/detail/polynomial_horner1_6.hpp>
  48. #include <boost/math/tools/detail/polynomial_horner1_7.hpp>
  49. #include <boost/math/tools/detail/polynomial_horner1_8.hpp>
  50. #include <boost/math/tools/detail/polynomial_horner1_9.hpp>
  51. #include <boost/math/tools/detail/polynomial_horner1_10.hpp>
  52. #include <boost/math/tools/detail/polynomial_horner1_11.hpp>
  53. #include <boost/math/tools/detail/polynomial_horner1_12.hpp>
  54. #include <boost/math/tools/detail/polynomial_horner1_13.hpp>
  55. #include <boost/math/tools/detail/polynomial_horner1_14.hpp>
  56. #include <boost/math/tools/detail/polynomial_horner1_15.hpp>
  57. #include <boost/math/tools/detail/polynomial_horner1_16.hpp>
  58. #include <boost/math/tools/detail/polynomial_horner1_17.hpp>
  59. #include <boost/math/tools/detail/polynomial_horner1_18.hpp>
  60. #include <boost/math/tools/detail/polynomial_horner1_19.hpp>
  61. #include <boost/math/tools/detail/polynomial_horner1_20.hpp>
  62. #include <boost/math/tools/detail/polynomial_horner2_2.hpp>
  63. #include <boost/math/tools/detail/polynomial_horner2_3.hpp>
  64. #include <boost/math/tools/detail/polynomial_horner2_4.hpp>
  65. #include <boost/math/tools/detail/polynomial_horner2_5.hpp>
  66. #include <boost/math/tools/detail/polynomial_horner2_6.hpp>
  67. #include <boost/math/tools/detail/polynomial_horner2_7.hpp>
  68. #include <boost/math/tools/detail/polynomial_horner2_8.hpp>
  69. #include <boost/math/tools/detail/polynomial_horner2_9.hpp>
  70. #include <boost/math/tools/detail/polynomial_horner2_10.hpp>
  71. #include <boost/math/tools/detail/polynomial_horner2_11.hpp>
  72. #include <boost/math/tools/detail/polynomial_horner2_12.hpp>
  73. #include <boost/math/tools/detail/polynomial_horner2_13.hpp>
  74. #include <boost/math/tools/detail/polynomial_horner2_14.hpp>
  75. #include <boost/math/tools/detail/polynomial_horner2_15.hpp>
  76. #include <boost/math/tools/detail/polynomial_horner2_16.hpp>
  77. #include <boost/math/tools/detail/polynomial_horner2_17.hpp>
  78. #include <boost/math/tools/detail/polynomial_horner2_18.hpp>
  79. #include <boost/math/tools/detail/polynomial_horner2_19.hpp>
  80. #include <boost/math/tools/detail/polynomial_horner2_20.hpp>
  81. #include <boost/math/tools/detail/polynomial_horner3_2.hpp>
  82. #include <boost/math/tools/detail/polynomial_horner3_3.hpp>
  83. #include <boost/math/tools/detail/polynomial_horner3_4.hpp>
  84. #include <boost/math/tools/detail/polynomial_horner3_5.hpp>
  85. #include <boost/math/tools/detail/polynomial_horner3_6.hpp>
  86. #include <boost/math/tools/detail/polynomial_horner3_7.hpp>
  87. #include <boost/math/tools/detail/polynomial_horner3_8.hpp>
  88. #include <boost/math/tools/detail/polynomial_horner3_9.hpp>
  89. #include <boost/math/tools/detail/polynomial_horner3_10.hpp>
  90. #include <boost/math/tools/detail/polynomial_horner3_11.hpp>
  91. #include <boost/math/tools/detail/polynomial_horner3_12.hpp>
  92. #include <boost/math/tools/detail/polynomial_horner3_13.hpp>
  93. #include <boost/math/tools/detail/polynomial_horner3_14.hpp>
  94. #include <boost/math/tools/detail/polynomial_horner3_15.hpp>
  95. #include <boost/math/tools/detail/polynomial_horner3_16.hpp>
  96. #include <boost/math/tools/detail/polynomial_horner3_17.hpp>
  97. #include <boost/math/tools/detail/polynomial_horner3_18.hpp>
  98. #include <boost/math/tools/detail/polynomial_horner3_19.hpp>
  99. #include <boost/math/tools/detail/polynomial_horner3_20.hpp>
  100. #include <boost/math/tools/detail/rational_horner1_2.hpp>
  101. #include <boost/math/tools/detail/rational_horner1_3.hpp>
  102. #include <boost/math/tools/detail/rational_horner1_4.hpp>
  103. #include <boost/math/tools/detail/rational_horner1_5.hpp>
  104. #include <boost/math/tools/detail/rational_horner1_6.hpp>
  105. #include <boost/math/tools/detail/rational_horner1_7.hpp>
  106. #include <boost/math/tools/detail/rational_horner1_8.hpp>
  107. #include <boost/math/tools/detail/rational_horner1_9.hpp>
  108. #include <boost/math/tools/detail/rational_horner1_10.hpp>
  109. #include <boost/math/tools/detail/rational_horner1_11.hpp>
  110. #include <boost/math/tools/detail/rational_horner1_12.hpp>
  111. #include <boost/math/tools/detail/rational_horner1_13.hpp>
  112. #include <boost/math/tools/detail/rational_horner1_14.hpp>
  113. #include <boost/math/tools/detail/rational_horner1_15.hpp>
  114. #include <boost/math/tools/detail/rational_horner1_16.hpp>
  115. #include <boost/math/tools/detail/rational_horner1_17.hpp>
  116. #include <boost/math/tools/detail/rational_horner1_18.hpp>
  117. #include <boost/math/tools/detail/rational_horner1_19.hpp>
  118. #include <boost/math/tools/detail/rational_horner1_20.hpp>
  119. #include <boost/math/tools/detail/rational_horner2_2.hpp>
  120. #include <boost/math/tools/detail/rational_horner2_3.hpp>
  121. #include <boost/math/tools/detail/rational_horner2_4.hpp>
  122. #include <boost/math/tools/detail/rational_horner2_5.hpp>
  123. #include <boost/math/tools/detail/rational_horner2_6.hpp>
  124. #include <boost/math/tools/detail/rational_horner2_7.hpp>
  125. #include <boost/math/tools/detail/rational_horner2_8.hpp>
  126. #include <boost/math/tools/detail/rational_horner2_9.hpp>
  127. #include <boost/math/tools/detail/rational_horner2_10.hpp>
  128. #include <boost/math/tools/detail/rational_horner2_11.hpp>
  129. #include <boost/math/tools/detail/rational_horner2_12.hpp>
  130. #include <boost/math/tools/detail/rational_horner2_13.hpp>
  131. #include <boost/math/tools/detail/rational_horner2_14.hpp>
  132. #include <boost/math/tools/detail/rational_horner2_15.hpp>
  133. #include <boost/math/tools/detail/rational_horner2_16.hpp>
  134. #include <boost/math/tools/detail/rational_horner2_17.hpp>
  135. #include <boost/math/tools/detail/rational_horner2_18.hpp>
  136. #include <boost/math/tools/detail/rational_horner2_19.hpp>
  137. #include <boost/math/tools/detail/rational_horner2_20.hpp>
  138. #include <boost/math/tools/detail/rational_horner3_2.hpp>
  139. #include <boost/math/tools/detail/rational_horner3_3.hpp>
  140. #include <boost/math/tools/detail/rational_horner3_4.hpp>
  141. #include <boost/math/tools/detail/rational_horner3_5.hpp>
  142. #include <boost/math/tools/detail/rational_horner3_6.hpp>
  143. #include <boost/math/tools/detail/rational_horner3_7.hpp>
  144. #include <boost/math/tools/detail/rational_horner3_8.hpp>
  145. #include <boost/math/tools/detail/rational_horner3_9.hpp>
  146. #include <boost/math/tools/detail/rational_horner3_10.hpp>
  147. #include <boost/math/tools/detail/rational_horner3_11.hpp>
  148. #include <boost/math/tools/detail/rational_horner3_12.hpp>
  149. #include <boost/math/tools/detail/rational_horner3_13.hpp>
  150. #include <boost/math/tools/detail/rational_horner3_14.hpp>
  151. #include <boost/math/tools/detail/rational_horner3_15.hpp>
  152. #include <boost/math/tools/detail/rational_horner3_16.hpp>
  153. #include <boost/math/tools/detail/rational_horner3_17.hpp>
  154. #include <boost/math/tools/detail/rational_horner3_18.hpp>
  155. #include <boost/math/tools/detail/rational_horner3_19.hpp>
  156. #include <boost/math/tools/detail/rational_horner3_20.hpp>
  157. #endif
  158. namespace boost{ namespace math{ namespace tools{
  159. //
  160. // Forward declaration to keep two phase lookup happy:
  161. //
  162. template <class T, class U>
  163. U evaluate_polynomial(const T* poly, U const& z, std::size_t count) BOOST_MATH_NOEXCEPT(U);
  164. namespace detail{
  165. template <class T, class V, class Tag>
  166. inline V evaluate_polynomial_c_imp(const T* a, const V& val, const Tag*) BOOST_MATH_NOEXCEPT(V)
  167. {
  168. return evaluate_polynomial(a, val, Tag::value);
  169. }
  170. } // namespace detail
  171. //
  172. // Polynomial evaluation with runtime size.
  173. // This requires a for-loop which may be more expensive than
  174. // the loop expanded versions above:
  175. //
  176. template <class T, class U>
  177. inline U evaluate_polynomial(const T* poly, U const& z, std::size_t count) BOOST_MATH_NOEXCEPT(U)
  178. {
  179. BOOST_ASSERT(count > 0);
  180. U sum = static_cast<U>(poly[count - 1]);
  181. for(int i = static_cast<int>(count) - 2; i >= 0; --i)
  182. {
  183. sum *= z;
  184. sum += static_cast<U>(poly[i]);
  185. }
  186. return sum;
  187. }
  188. //
  189. // Compile time sized polynomials, just inline forwarders to the
  190. // implementations above:
  191. //
  192. template <std::size_t N, class T, class V>
  193. inline V evaluate_polynomial(const T(&a)[N], const V& val) BOOST_MATH_NOEXCEPT(V)
  194. {
  195. typedef std::integral_constant<int, N> tag_type;
  196. return detail::evaluate_polynomial_c_imp(static_cast<const T*>(a), val, static_cast<tag_type const*>(0));
  197. }
  198. template <std::size_t N, class T, class V>
  199. inline V evaluate_polynomial(const boost::array<T,N>& a, const V& val) BOOST_MATH_NOEXCEPT(V)
  200. {
  201. typedef std::integral_constant<int, N> tag_type;
  202. return detail::evaluate_polynomial_c_imp(static_cast<const T*>(a.data()), val, static_cast<tag_type const*>(0));
  203. }
  204. //
  205. // Even polynomials are trivial: just square the argument!
  206. //
  207. template <class T, class U>
  208. inline U evaluate_even_polynomial(const T* poly, U z, std::size_t count) BOOST_MATH_NOEXCEPT(U)
  209. {
  210. return evaluate_polynomial(poly, U(z*z), count);
  211. }
  212. template <std::size_t N, class T, class V>
  213. inline V evaluate_even_polynomial(const T(&a)[N], const V& z) BOOST_MATH_NOEXCEPT(V)
  214. {
  215. return evaluate_polynomial(a, V(z*z));
  216. }
  217. template <std::size_t N, class T, class V>
  218. inline V evaluate_even_polynomial(const boost::array<T,N>& a, const V& z) BOOST_MATH_NOEXCEPT(V)
  219. {
  220. return evaluate_polynomial(a, V(z*z));
  221. }
  222. //
  223. // Odd polynomials come next:
  224. //
  225. template <class T, class U>
  226. inline U evaluate_odd_polynomial(const T* poly, U z, std::size_t count) BOOST_MATH_NOEXCEPT(U)
  227. {
  228. return poly[0] + z * evaluate_polynomial(poly+1, U(z*z), count-1);
  229. }
  230. template <std::size_t N, class T, class V>
  231. inline V evaluate_odd_polynomial(const T(&a)[N], const V& z) BOOST_MATH_NOEXCEPT(V)
  232. {
  233. typedef std::integral_constant<int, N-1> tag_type;
  234. return a[0] + z * detail::evaluate_polynomial_c_imp(static_cast<const T*>(a) + 1, V(z*z), static_cast<tag_type const*>(0));
  235. }
  236. template <std::size_t N, class T, class V>
  237. inline V evaluate_odd_polynomial(const boost::array<T,N>& a, const V& z) BOOST_MATH_NOEXCEPT(V)
  238. {
  239. typedef std::integral_constant<int, N-1> tag_type;
  240. return a[0] + z * detail::evaluate_polynomial_c_imp(static_cast<const T*>(a.data()) + 1, V(z*z), static_cast<tag_type const*>(0));
  241. }
  242. template <class T, class U, class V>
  243. V evaluate_rational(const T* num, const U* denom, const V& z_, std::size_t count) BOOST_MATH_NOEXCEPT(V);
  244. namespace detail{
  245. template <class T, class U, class V, class Tag>
  246. inline V evaluate_rational_c_imp(const T* num, const U* denom, const V& z, const Tag*) BOOST_MATH_NOEXCEPT(V)
  247. {
  248. return boost::math::tools::evaluate_rational(num, denom, z, Tag::value);
  249. }
  250. }
  251. //
  252. // Rational functions: numerator and denominator must be
  253. // equal in size. These always have a for-loop and so may be less
  254. // efficient than evaluating a pair of polynomials. However, there
  255. // are some tricks we can use to prevent overflow that might otherwise
  256. // occur in polynomial evaluation, if z is large. This is important
  257. // in our Lanczos code for example.
  258. //
  259. template <class T, class U, class V>
  260. V evaluate_rational(const T* num, const U* denom, const V& z_, std::size_t count) BOOST_MATH_NOEXCEPT(V)
  261. {
  262. V z(z_);
  263. V s1, s2;
  264. if(z <= 1)
  265. {
  266. s1 = static_cast<V>(num[count-1]);
  267. s2 = static_cast<V>(denom[count-1]);
  268. for(int i = (int)count - 2; i >= 0; --i)
  269. {
  270. s1 *= z;
  271. s2 *= z;
  272. s1 += num[i];
  273. s2 += denom[i];
  274. }
  275. }
  276. else
  277. {
  278. z = 1 / z;
  279. s1 = static_cast<V>(num[0]);
  280. s2 = static_cast<V>(denom[0]);
  281. for(unsigned i = 1; i < count; ++i)
  282. {
  283. s1 *= z;
  284. s2 *= z;
  285. s1 += num[i];
  286. s2 += denom[i];
  287. }
  288. }
  289. return s1 / s2;
  290. }
  291. template <std::size_t N, class T, class U, class V>
  292. inline V evaluate_rational(const T(&a)[N], const U(&b)[N], const V& z) BOOST_MATH_NOEXCEPT(V)
  293. {
  294. return detail::evaluate_rational_c_imp(a, b, z, static_cast<const std::integral_constant<int, N>*>(0));
  295. }
  296. template <std::size_t N, class T, class U, class V>
  297. inline V evaluate_rational(const boost::array<T,N>& a, const boost::array<U,N>& b, const V& z) BOOST_MATH_NOEXCEPT(V)
  298. {
  299. return detail::evaluate_rational_c_imp(a.data(), b.data(), z, static_cast<std::integral_constant<int, N>*>(0));
  300. }
  301. } // namespace tools
  302. } // namespace math
  303. } // namespace boost
  304. #endif // BOOST_MATH_TOOLS_RATIONAL_HPP