chebyshev.qbk 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. [/
  2. Copyright 2017, Nick Thompson
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:chebyshev Chebyshev Polynomials]
  8. [h4 Synopsis]
  9. ``
  10. #include <boost/math/special_functions/chebyshev.hpp>
  11. ``
  12. namespace boost{ namespace math{
  13. template<class Real1, class Real2, class Real3>
  14. ``__sf_result`` chebyshev_next(Real1 const & x, Real2 const & Tn, Real3 const & Tn_1);
  15. template<class Real>
  16. ``__sf_result`` chebyshev_t(unsigned n, Real const & x);
  17. template<class Real, class ``__Policy``>
  18. ``__sf_result`` chebyshev_t(unsigned n, Real const & x, const ``__Policy``&);
  19. template<class Real>
  20. ``__sf_result`` chebyshev_u(unsigned n, Real const & x);
  21. template<class Real, class ``__Policy``>
  22. ``__sf_result`` chebyshev_u(unsigned n, Real const & x, const ``__Policy``&);
  23. template<class Real>
  24. ``__sf_result`` chebyshev_t_prime(unsigned n, Real const & x);
  25. template<class Real1, class Real2>
  26. ``__sf_result`` chebyshev_clenshaw_recurrence(const Real* const c, size_t length, Real2 x);
  27. }} // namespaces
  28. ['"Real analysts cannot do without Fourier, complex analysts cannot do without Laurent, and numerical analysts cannot do without Chebyshev"] --Lloyd N. Trefethen
  29. The Chebyshev polynomials of the first kind are defined by the recurrence /T/[sub n+1](/x/) := /2xT/[sub n](/x/) - /T/[sub n-1](/x/), /n > 0/,
  30. where /T/[sub 0](/x/) := 1 and /T/[sub 1](/x/) := /x/.
  31. These can be calculated in Boost using the following simple code
  32. double x = 0.5;
  33. double T12 = boost::math::chebyshev_t(12, x);
  34. Calculation of derivatives is also straightforward:
  35. double T12_prime = boost::math::chebyshev_t_prime(12, x);
  36. The complexity of evaluation of the /n/-th Chebyshev polynomial by these functions is linear.
  37. So they are unsuitable for use in calculation of (say) a Chebyshev series, as a sum of linear scaling functions scales quadratically.
  38. Though there are very sophisticated algorithms for the evaluation of Chebyshev series,
  39. a linear time algorithm is presented below:
  40. double x = 0.5;
  41. std::vector<double> c{14.2, -13.7, 82.3, 96};
  42. double T0 = 1;
  43. double T1 = x;
  44. double f = c[0]*T0/2;
  45. unsigned l = 1;
  46. while(l < c.size())
  47. {
  48. f += c[l]*T1;
  49. std::swap(T0, T1);
  50. T1 = boost::math::chebyshev_next(x, T0, T1);
  51. ++l;
  52. }
  53. This uses the `chebyshev_next` function to evaluate each term of the Chebyshev series in constant time.
  54. However, this naive algorithm has a catastrophic loss of precision as /x/ approaches 1.
  55. A method to mitigate this way given by [@http://www.ams.org/journals/mcom/1955-09-051/S0025-5718-1955-0071856-0/S0025-5718-1955-0071856-0.pdf Clenshaw],
  56. and is implemented in boost as
  57. double x = 0.5;
  58. std::vector<double> c{14.2, -13.7, 82.3, 96};
  59. double f = chebyshev_clenshaw_recurrence(c.data(), c.size(), Real x);
  60. N.B.: There is factor of /2/ difference in our definition of the first coefficient in the Chebyshev series from Clenshaw's original work.
  61. This is because two traditions exist in notation for the Chebyshev series expansion,
  62. [:/f/(/x/) \u2248 \u2211[sub n=0][super N-1] /a/[sub n]/T/[sub n](/x/)]
  63. and
  64. [:/f/(/x/) \u2248 /c/[sub 0]/2 + \u2211[sub n=1][super N-1] /c/[sub n]/T/[sub n](/x/)]
  65. ['*boost math always uses the second convention, with the factor of 1/2 on the first coefficient.*]
  66. Chebyshev polynomials of the second kind can be evaluated via `chebyshev_u`:
  67. double x = -0.23;
  68. double U1 = boost::math::chebyshev_u(1, x);
  69. The evaluation of Chebyshev polynomials by a three-term recurrence is known to be
  70. [@https://link.springer.com/article/10.1007/s11075-014-9925-x mixed forward-backward stable] for /x/ \u220A \[-1, 1\].
  71. However, the author does not know of a similar result for /x/ outside \[-1, 1\].
  72. For this reason, evaluation of Chebyshev polynomials outside of \[-1, 1\] is strongly discouraged.
  73. That said, small rounding errors in the course of a computation will often lead to this situation,
  74. and termination of the computation due to these small problems is very discouraging.
  75. For this reason, `chebyshev_t` and `chebyshev_u` have code paths for /x > 1/ and /x < -1/ which do not use three-term recurrences.
  76. These code paths are /much slower/, and should be avoided if at all possible.
  77. Evaluation of a Chebyshev series is relatively simple.
  78. The real challenge is /generation/ of the Chebyshev series.
  79. For this purpose, boost provides a /Chebyshev transform/, a projection operator which projects a function onto a finite-dimensional span of Chebyshev polynomials.
  80. But before we discuss the API, let's analyze why we might want to project a function onto a span of Chebyshev polynomials.
  81. * We want a numerically stable way to evaluate the function's derivative.
  82. * Our function is expensive to evaluate, and we wish to find a less expensive way to estimate its value.
  83. An example are the standard library transcendental functions:
  84. These functions are guaranteed to evaluate to within 1 ulp of the exact value, but often this accuracy is not needed.
  85. A projection onto the Chebyshev polynomials with a low accuracy requirement can vastly accelerate the computation of these functions.
  86. * We wish to numerically integrate the function.
  87. The API is given below.
  88. ``
  89. #include <boost/math/special_functions/chebyshev_transform.hpp>
  90. ``
  91. namespace boost{ namespace math{
  92. template<class Real>
  93. class chebyshev_transform
  94. {
  95. public:
  96. template<class F>
  97. chebyshev_transform(const F& f, Real a, Real b, Real tol=500*std::numeric_limits<Real>::epsilon());
  98. Real operator()(Real x) const
  99. Real integrate() const
  100. const std::vector<Real>& coefficients() const
  101. Real prime(Real x) const
  102. };
  103. }}// end namespaces
  104. The Chebyshev transform takes a function /f/ and returns a /near-minimax/ approximation to /f/ in terms of Chebyshev polynomials.
  105. By /near-minimax/, we mean that the resulting Chebyshev polynomial is "very close" the polynomial /p/[sub n] which minimizes the uniform norm of /f/ - /p/[sub n].
  106. The notion of "very close" can be made rigorous; see Trefethen's "Approximation Theory and Approximation Practice" for details.
  107. The Chebyshev transform works by creating a vector of values by evaluating the input function at the Chebyshev points, and then performing a discrete cosine transform on the resulting vector.
  108. In order to do this efficiently, we have used [@http://www.fftw.org/ FFTW3].
  109. So to compile, you must have `FFTW3` installed, and link with `-lfftw3` for double precision, `-lfftw3f` for float precision, `-lfftw3l` for long double precision, and -lfftwq for quad (`__float128`) precision.
  110. After the coefficients of the Chebyshev series are known, the routine goes back through them and filters out all the coefficients whose absolute ratio to the largest coefficient are less than the tolerance requested in the constructor.
  111. [endsect] [/section:chebyshev Chebyshev Polynomials]