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)
6 #ifndef BOOST_MATH_SPECIAL_CHEBYSHEV_TRANSFORM_HPP
7 #define BOOST_MATH_SPECIAL_CHEBYSHEV_TRANSFORM_HPP
11 #include <boost/math/constants/constants.hpp>
12 #include <boost/math/special_functions/chebyshev.hpp>
14 #ifdef BOOST_HAS_FLOAT128
18 namespace boost { namespace math {
23 struct fftw_cos_transform;
26 struct fftw_cos_transform<double>
28 fftw_cos_transform(int n, double* data1, double* data2)
30 plan = fftw_plan_r2r_1d(n, data1, data2, FFTW_REDFT10, FFTW_ESTIMATE);
34 fftw_destroy_plan(plan);
36 void execute(double* data1, double* data2)
38 fftw_execute_r2r(plan, data1, data2);
40 static double cos(double x) { return std::cos(x); }
41 static double fabs(double x) { return std::fabs(x); }
47 struct fftw_cos_transform<float>
49 fftw_cos_transform(int n, float* data1, float* data2)
51 plan = fftwf_plan_r2r_1d(n, data1, data2, FFTW_REDFT10, FFTW_ESTIMATE);
55 fftwf_destroy_plan(plan);
57 void execute(float* data1, float* data2)
59 fftwf_execute_r2r(plan, data1, data2);
61 static float cos(float x) { return std::cos(x); }
62 static float fabs(float x) { return std::fabs(x); }
68 struct fftw_cos_transform<long double>
70 fftw_cos_transform(int n, long double* data1, long double* data2)
72 plan = fftwl_plan_r2r_1d(n, data1, data2, FFTW_REDFT10, FFTW_ESTIMATE);
76 fftwl_destroy_plan(plan);
78 void execute(long double* data1, long double* data2)
80 fftwl_execute_r2r(plan, data1, data2);
82 static long double cos(long double x) { return std::cos(x); }
83 static long double fabs(long double x) { return std::fabs(x); }
87 #ifdef BOOST_HAS_FLOAT128
89 struct fftw_cos_transform<__float128>
91 fftw_cos_transform(int n, __float128* data1, __float128* data2)
93 plan = fftwq_plan_r2r_1d(n, data1, data2, FFTW_REDFT10, FFTW_ESTIMATE);
97 fftwq_destroy_plan(plan);
99 void execute(__float128* data1, __float128* data2)
101 fftwq_execute_r2r(plan, data1, data2);
103 static __float128 cos(__float128 x) { return cosq(x); }
104 static __float128 fabs(__float128 x) { return fabsq(x); }
113 class chebyshev_transform
117 chebyshev_transform(const F& f, Real a, Real b,
118 Real tol = 500 * std::numeric_limits<Real>::epsilon(),
119 size_t max_refinements = 15) : m_a(a), m_b(b)
123 throw std::domain_error("a < b is required.\n");
125 using boost::math::constants::half;
126 using boost::math::constants::pi;
129 Real bma = (b-a)*half<Real>();
130 Real bpa = (b+a)*half<Real>();
132 std::vector<Real> vf;
134 size_t refinements = 0;
135 while(refinements < max_refinements)
140 detail::fftw_cos_transform<Real> plan(static_cast<int>(n), vf.data(), m_coeffs.data());
141 Real inv_n = 1/static_cast<Real>(n);
142 for(size_t j = 0; j < n/2; ++j)
144 // Use symmetry cos((j+1/2)pi/n) = - cos((n-1-j+1/2)pi/n)
145 Real y = detail::fftw_cos_transform<Real>::cos(pi<Real>()*(j+half<Real>())*inv_n);
146 vf[j] = f(y*bma + bpa)*inv_n;
147 vf[n-1-j]= f(bpa-y*bma)*inv_n;
150 plan.execute(vf.data(), m_coeffs.data());
152 for (auto const & coeff : m_coeffs)
154 if (detail::fftw_cos_transform<Real>::fabs(coeff) > max_coeff)
156 max_coeff = detail::fftw_cos_transform<Real>::fabs(coeff);
159 size_t j = m_coeffs.size() - 1;
160 while (abs(m_coeffs[j])/max_coeff < tol)
164 // If ten coefficients are eliminated, the we say we've done all
168 m_coeffs.resize(j+1);
177 Real operator()(Real x) const
179 using boost::math::constants::half;
180 if (x > m_b || x < m_a)
182 throw std::domain_error("x not in [a, b]\n");
185 Real z = (2*x - m_a - m_b)/(m_b - m_a);
186 return chebyshev_clenshaw_recurrence(m_coeffs.data(), m_coeffs.size(), z);
189 // Integral over entire domain [a, b]
190 Real integrate() const
192 Real Q = m_coeffs[0]/2;
193 for(size_t j = 2; j < m_coeffs.size(); j += 2)
195 Q += -m_coeffs[j]/((j+1)*(j-1));
197 return (m_b - m_a)*Q;
200 const std::vector<Real>& coefficients() const
205 Real prime(Real x) const
207 Real z = (2*x - m_a - m_b)/(m_b - m_a);
208 Real dzdx = 2/(m_b - m_a);
209 if (m_coeffs.size() < 2)
215 Real b1 = m_coeffs[m_coeffs.size() -1];
217 for(size_t j = m_coeffs.size() - 2; j >= 1; --j)
219 Real tmp1 = 2*z*b1 - b2 + m_coeffs[j];
220 Real tmp2 = 2*z*d1 - d2 + 2*b1;
227 return dzdx*(z*d1 - d2 + b1);
230 void print_coefficients() const
233 for(auto const & coeff : m_coeffs) {
234 std::cout << coeff << ", ";
241 std::vector<Real> m_coeffs;