chebyshev.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // (C) Copyright Nick Thompson 2017.
  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_SPECIAL_CHEBYSHEV_HPP
  6. #define BOOST_MATH_SPECIAL_CHEBYSHEV_HPP
  7. #include <cmath>
  8. #include <boost/math/special_functions/math_fwd.hpp>
  9. #include <boost/math/policies/error_handling.hpp>
  10. #include <boost/math/constants/constants.hpp>
  11. #include <boost/math/tools/promotion.hpp>
  12. #if (__cplusplus > 201103) || (defined(_CPPLIB_VER) && (_CPPLIB_VER >= 610))
  13. # define BOOST_MATH_CHEB_USE_STD_ACOSH
  14. #endif
  15. #ifndef BOOST_MATH_CHEB_USE_STD_ACOSH
  16. # include <boost/math/special_functions/acosh.hpp>
  17. #endif
  18. namespace boost { namespace math {
  19. template<class T1, class T2, class T3>
  20. inline typename tools::promote_args<T1, T2, T3>::type chebyshev_next(T1 const & x, T2 const & Tn, T3 const & Tn_1)
  21. {
  22. return 2*x*Tn - Tn_1;
  23. }
  24. namespace detail {
  25. template<class Real, bool second, class Policy>
  26. inline Real chebyshev_imp(unsigned n, Real const & x, const Policy&)
  27. {
  28. #ifdef BOOST_MATH_CHEB_USE_STD_ACOSH
  29. using std::acosh;
  30. #define BOOST_MATH_ACOSH_POLICY
  31. #else
  32. using boost::math::acosh;
  33. #define BOOST_MATH_ACOSH_POLICY , Policy()
  34. #endif
  35. using std::cosh;
  36. using std::pow;
  37. using std::sqrt;
  38. Real T0 = 1;
  39. Real T1;
  40. if (second)
  41. {
  42. if (x > 1 || x < -1)
  43. {
  44. Real t = sqrt(x*x -1);
  45. return static_cast<Real>((pow(x+t, (int)(n+1)) - pow(x-t, (int)(n+1)))/(2*t));
  46. }
  47. T1 = 2*x;
  48. }
  49. else
  50. {
  51. if (x > 1)
  52. {
  53. return cosh(n*acosh(x BOOST_MATH_ACOSH_POLICY));
  54. }
  55. if (x < -1)
  56. {
  57. if (n & 1)
  58. {
  59. return -cosh(n*acosh(-x BOOST_MATH_ACOSH_POLICY));
  60. }
  61. else
  62. {
  63. return cosh(n*acosh(-x BOOST_MATH_ACOSH_POLICY));
  64. }
  65. }
  66. T1 = x;
  67. }
  68. if (n == 0)
  69. {
  70. return T0;
  71. }
  72. unsigned l = 1;
  73. while(l < n)
  74. {
  75. std::swap(T0, T1);
  76. T1 = boost::math::chebyshev_next(x, T0, T1);
  77. ++l;
  78. }
  79. return T1;
  80. }
  81. } // namespace detail
  82. template <class Real, class Policy>
  83. inline typename tools::promote_args<Real>::type
  84. chebyshev_t(unsigned n, Real const & x, const Policy&)
  85. {
  86. typedef typename tools::promote_args<Real>::type result_type;
  87. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  88. typedef typename policies::normalise<
  89. Policy,
  90. policies::promote_float<false>,
  91. policies::promote_double<false>,
  92. policies::discrete_quantile<>,
  93. policies::assert_undefined<> >::type forwarding_policy;
  94. return policies::checked_narrowing_cast<result_type, Policy>(detail::chebyshev_imp<value_type, false>(n, static_cast<value_type>(x), forwarding_policy()), "boost::math::chebyshev_t<%1%>(unsigned, %1%)");
  95. }
  96. template<class Real>
  97. inline typename tools::promote_args<Real>::type chebyshev_t(unsigned n, Real const & x)
  98. {
  99. return chebyshev_t(n, x, policies::policy<>());
  100. }
  101. template <class Real, class Policy>
  102. inline typename tools::promote_args<Real>::type
  103. chebyshev_u(unsigned n, Real const & x, const Policy&)
  104. {
  105. typedef typename tools::promote_args<Real>::type result_type;
  106. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  107. typedef typename policies::normalise<
  108. Policy,
  109. policies::promote_float<false>,
  110. policies::promote_double<false>,
  111. policies::discrete_quantile<>,
  112. policies::assert_undefined<> >::type forwarding_policy;
  113. return policies::checked_narrowing_cast<result_type, Policy>(detail::chebyshev_imp<value_type, true>(n, static_cast<value_type>(x), forwarding_policy()), "boost::math::chebyshev_u<%1%>(unsigned, %1%)");
  114. }
  115. template<class Real>
  116. inline typename tools::promote_args<Real>::type chebyshev_u(unsigned n, Real const & x)
  117. {
  118. return chebyshev_u(n, x, policies::policy<>());
  119. }
  120. template <class Real, class Policy>
  121. inline typename tools::promote_args<Real>::type
  122. chebyshev_t_prime(unsigned n, Real const & x, const Policy&)
  123. {
  124. typedef typename tools::promote_args<Real>::type result_type;
  125. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  126. typedef typename policies::normalise<
  127. Policy,
  128. policies::promote_float<false>,
  129. policies::promote_double<false>,
  130. policies::discrete_quantile<>,
  131. policies::assert_undefined<> >::type forwarding_policy;
  132. if (n == 0)
  133. {
  134. return result_type(0);
  135. }
  136. return policies::checked_narrowing_cast<result_type, Policy>(n * detail::chebyshev_imp<value_type, true>(n - 1, static_cast<value_type>(x), forwarding_policy()), "boost::math::chebyshev_t_prime<%1%>(unsigned, %1%)");
  137. }
  138. template<class Real>
  139. inline typename tools::promote_args<Real>::type chebyshev_t_prime(unsigned n, Real const & x)
  140. {
  141. return chebyshev_t_prime(n, x, policies::policy<>());
  142. }
  143. /*
  144. * This is Algorithm 3.1 of
  145. * Gil, Amparo, Javier Segura, and Nico M. Temme.
  146. * Numerical methods for special functions.
  147. * Society for Industrial and Applied Mathematics, 2007.
  148. * https://www.siam.org/books/ot99/OT99SampleChapter.pdf
  149. * However, our definition of c0 differs by a factor of 1/2, as stated in the docs. . .
  150. */
  151. template<class Real, class T2>
  152. inline Real chebyshev_clenshaw_recurrence(const Real* const c, size_t length, const T2& x)
  153. {
  154. using boost::math::constants::half;
  155. if (length < 2)
  156. {
  157. if (length == 0)
  158. {
  159. return 0;
  160. }
  161. return c[0]/2;
  162. }
  163. Real b2 = 0;
  164. Real b1 = c[length -1];
  165. for(size_t j = length - 2; j >= 1; --j)
  166. {
  167. Real tmp = 2*x*b1 - b2 + c[j];
  168. b2 = b1;
  169. b1 = tmp;
  170. }
  171. return x*b1 - b2 + half<Real>()*c[0];
  172. }
  173. namespace detail {
  174. template<class Real>
  175. inline Real unchecked_chebyshev_clenshaw_recurrence(const Real* const c, size_t length, const Real & a, const Real & b, const Real& x)
  176. {
  177. Real t;
  178. Real u;
  179. // This cutoff is not super well defined, but it's a good estimate.
  180. // See "An Error Analysis of the Modified Clenshaw Method for Evaluating Chebyshev and Fourier Series"
  181. // J. OLIVER, IMA Journal of Applied Mathematics, Volume 20, Issue 3, November 1977, Pages 379–391
  182. // https://doi.org/10.1093/imamat/20.3.379
  183. const Real cutoff = 0.6;
  184. if (x - a < b - x)
  185. {
  186. u = 2*(x-a)/(b-a);
  187. t = u - 1;
  188. if (t > -cutoff)
  189. {
  190. Real b2 = 0;
  191. Real b1 = c[length -1];
  192. for(size_t j = length - 2; j >= 1; --j)
  193. {
  194. Real tmp = 2*t*b1 - b2 + c[j];
  195. b2 = b1;
  196. b1 = tmp;
  197. }
  198. return t*b1 - b2 + c[0]/2;
  199. }
  200. else
  201. {
  202. Real b = c[length -1];
  203. Real d = b;
  204. Real b2 = 0;
  205. for (size_t r = length - 2; r >= 1; --r)
  206. {
  207. d = 2*u*b - d + c[r];
  208. b2 = b;
  209. b = d - b;
  210. }
  211. return t*b - b2 + c[0]/2;
  212. }
  213. }
  214. else
  215. {
  216. u = -2*(b-x)/(b-a);
  217. t = u + 1;
  218. if (t < cutoff)
  219. {
  220. Real b2 = 0;
  221. Real b1 = c[length -1];
  222. for(size_t j = length - 2; j >= 1; --j)
  223. {
  224. Real tmp = 2*t*b1 - b2 + c[j];
  225. b2 = b1;
  226. b1 = tmp;
  227. }
  228. return t*b1 - b2 + c[0]/2;
  229. }
  230. else
  231. {
  232. Real b = c[length -1];
  233. Real d = b;
  234. Real b2 = 0;
  235. for (size_t r = length - 2; r >= 1; --r)
  236. {
  237. d = 2*u*b + d + c[r];
  238. b2 = b;
  239. b = d + b;
  240. }
  241. return t*b - b2 + c[0]/2;
  242. }
  243. }
  244. }
  245. } // namespace detail
  246. template<class Real>
  247. inline Real chebyshev_clenshaw_recurrence(const Real* const c, size_t length, const Real & a, const Real & b, const Real& x)
  248. {
  249. if (x < a || x > b)
  250. {
  251. throw std::domain_error("x in [a, b] is required.");
  252. }
  253. if (length < 2)
  254. {
  255. if (length == 0)
  256. {
  257. return 0;
  258. }
  259. return c[0]/2;
  260. }
  261. return detail::unchecked_chebyshev_clenshaw_recurrence(c, length, a, b, x);
  262. }
  263. }}
  264. #endif