| 1 |
// (C) Copyright Nick Thompson 2019. |
| 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 |
|
| 6 |
#ifndef BOOST_MATH_INTERPOLATORS_DETAIL_CARDINAL_TRIGONOMETRIC_HPP |
| 7 |
#define BOOST_MATH_INTERPOLATORS_DETAIL_CARDINAL_TRIGONOMETRIC_HPP |
| 8 |
#include <cstddef> |
| 9 |
#include <cmath> |
| 10 |
#include <stdexcept> |
| 11 |
#include <boost/math/constants/constants.hpp> |
| 12 |
|
| 13 |
#ifdef BOOST_HAS_FLOAT128 |
| 14 |
#include <quadmath.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
#ifdef __has_include |
| 18 |
# if __has_include(<fftw3.h>) |
| 19 |
# include <fftw3.h> |
| 20 |
# else |
| 21 |
# error "This feature is unavailable without fftw3 installed" |
| 22 |
#endif |
| 23 |
#endif |
| 24 |
|
| 25 |
namespace boost { namespace math { namespace interpolators { namespace detail { |
| 26 |
|
| 27 |
template<typename Real> |
| 28 |
class cardinal_trigonometric_detail { |
| 29 |
public: |
| 30 |
cardinal_trigonometric_detail(const Real* data, size_t length, Real t0, Real h) |
| 31 |
{ |
| 32 |
m_data = data; |
| 33 |
m_length = length; |
| 34 |
m_t0 = t0; |
| 35 |
m_h = h; |
| 36 |
throw std::domain_error("Not implemented."); |
| 37 |
} |
| 38 |
private: |
| 39 |
size_t m_length; |
| 40 |
Real m_t0; |
| 41 |
Real m_h; |
| 42 |
Real* m_data; |
| 43 |
}; |
| 44 |
|
| 45 |
template<> |
| 46 |
class cardinal_trigonometric_detail<float> { |
| 47 |
public: |
| 48 |
cardinal_trigonometric_detail(const float* data, size_t length, float t0, float h) : m_t0{t0}, m_h{h} |
| 49 |
{ |
| 50 |
if (length == 0) |
| 51 |
{ |
| 52 |
throw std::logic_error("At least one sample is required."); |
| 53 |
} |
| 54 |
if (h <= 0) |
| 55 |
{ |
| 56 |
throw std::logic_error("The step size must be > 0"); |
| 57 |
} |
| 58 |
// The period sadly must be stored, since the complex vector has length that cannot be used to recover the period: |
| 59 |
m_T = m_h*length; |
| 60 |
m_complex_vector_size = length/2 + 1; |
| 61 |
m_gamma = fftwf_alloc_complex(m_complex_vector_size); |
| 62 |
// The const_cast is legitimate: FFTW does not change the data as long as FFTW_ESTIMATE is provided. |
| 63 |
fftwf_plan plan = fftwf_plan_dft_r2c_1d(length, const_cast<float*>(data), m_gamma, FFTW_ESTIMATE); |
| 64 |
// FFTW says a null plan is impossible with the basic interface we are using, and I have no reason to doubt them. |
| 65 |
// But it just feels weird not to check this: |
| 66 |
if (!plan) |
| 67 |
{ |
| 68 |
throw std::logic_error("A null fftw plan was created."); |
| 69 |
} |
| 70 |
|
| 71 |
fftwf_execute(plan); |
| 72 |
fftwf_destroy_plan(plan); |
| 73 |
|
| 74 |
float denom = length; |
| 75 |
for (size_t k = 0; k < m_complex_vector_size; ++k) |
| 76 |
{ |
| 77 |
m_gamma[k][0] /= denom; |
| 78 |
m_gamma[k][1] /= denom; |
| 79 |
} |
| 80 |
|
| 81 |
if (length % 2 == 0) |
| 82 |
{ |
| 83 |
m_gamma[m_complex_vector_size -1][0] /= 2; |
| 84 |
// numerically, m_gamma[m_complex_vector_size -1][1] should be zero . . . |
| 85 |
// I believe, but need to check, that FFTW guarantees that it is identically zero. |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
cardinal_trigonometric_detail(const cardinal_trigonometric_detail& old) = delete; |
| 90 |
|
| 91 |
cardinal_trigonometric_detail& operator=(const cardinal_trigonometric_detail&) = delete; |
| 92 |
|
| 93 |
cardinal_trigonometric_detail(cardinal_trigonometric_detail &&) = delete; |
| 94 |
|
| 95 |
float operator()(float t) const |
| 96 |
{ |
| 97 |
using std::sin; |
| 98 |
using std::cos; |
| 99 |
using boost::math::constants::two_pi; |
| 100 |
using std::exp; |
| 101 |
float s = m_gamma[0][0]; |
| 102 |
float x = two_pi<float>()*(t - m_t0)/m_T; |
| 103 |
fftwf_complex z; |
| 104 |
// boost::math::cos_pi with a redefinition of x? Not now . . . |
| 105 |
z[0] = cos(x); |
| 106 |
z[1] = sin(x); |
| 107 |
fftwf_complex b{0, 0}; |
| 108 |
// u = b*z |
| 109 |
fftwf_complex u; |
| 110 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) { |
| 111 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 112 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 113 |
b[0] = m_gamma[k][0] + u[0]; |
| 114 |
b[1] = m_gamma[k][1] + u[1]; |
| 115 |
} |
| 116 |
|
| 117 |
s += 2*(b[0]*z[0] - b[1]*z[1]); |
| 118 |
return s; |
| 119 |
} |
| 120 |
|
| 121 |
float prime(float t) const |
| 122 |
{ |
| 123 |
using std::sin; |
| 124 |
using std::cos; |
| 125 |
using boost::math::constants::two_pi; |
| 126 |
using std::exp; |
| 127 |
float x = two_pi<float>()*(t - m_t0)/m_T; |
| 128 |
fftwf_complex z; |
| 129 |
z[0] = cos(x); |
| 130 |
z[1] = sin(x); |
| 131 |
fftwf_complex b{0, 0}; |
| 132 |
// u = b*z |
| 133 |
fftwf_complex u; |
| 134 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) |
| 135 |
{ |
| 136 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 137 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 138 |
b[0] = k*m_gamma[k][0] + u[0]; |
| 139 |
b[1] = k*m_gamma[k][1] + u[1]; |
| 140 |
} |
| 141 |
// b*z = (b[0]*z[0] - b[1]*z[1]) + i(b[1]*z[0] + b[0]*z[1]) |
| 142 |
return -2*two_pi<float>()*(b[1]*z[0] + b[0]*z[1])/m_T; |
| 143 |
} |
| 144 |
|
| 145 |
float double_prime(float t) const |
| 146 |
{ |
| 147 |
using std::sin; |
| 148 |
using std::cos; |
| 149 |
using boost::math::constants::two_pi; |
| 150 |
using std::exp; |
| 151 |
float x = two_pi<float>()*(t - m_t0)/m_T; |
| 152 |
fftwf_complex z; |
| 153 |
z[0] = cos(x); |
| 154 |
z[1] = sin(x); |
| 155 |
fftwf_complex b{0, 0}; |
| 156 |
// u = b*z |
| 157 |
fftwf_complex u; |
| 158 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) |
| 159 |
{ |
| 160 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 161 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 162 |
b[0] = k*k*m_gamma[k][0] + u[0]; |
| 163 |
b[1] = k*k*m_gamma[k][1] + u[1]; |
| 164 |
} |
| 165 |
// b*z = (b[0]*z[0] - b[1]*z[1]) + i(b[1]*z[0] + b[0]*z[1]) |
| 166 |
return -2*two_pi<float>()*two_pi<float>()*(b[0]*z[0] - b[1]*z[1])/(m_T*m_T); |
| 167 |
} |
| 168 |
|
| 169 |
float period() const |
| 170 |
{ |
| 171 |
return m_T; |
| 172 |
} |
| 173 |
|
| 174 |
float integrate() const |
| 175 |
{ |
| 176 |
return m_T*m_gamma[0][0]; |
| 177 |
} |
| 178 |
|
| 179 |
float squared_l2() const |
| 180 |
{ |
| 181 |
float s = 0; |
| 182 |
// Always add smallest to largest for accuracy. |
| 183 |
for (size_t i = m_complex_vector_size - 1; i >= 1; --i) |
| 184 |
{ |
| 185 |
s += (m_gamma[i][0]*m_gamma[i][0] + m_gamma[i][1]*m_gamma[i][1]); |
| 186 |
} |
| 187 |
s *= 2; |
| 188 |
s += m_gamma[0][0]*m_gamma[0][0]; |
| 189 |
return s*m_T; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
~cardinal_trigonometric_detail() |
| 194 |
{ |
| 195 |
if (m_gamma) |
| 196 |
{ |
| 197 |
fftwf_free(m_gamma); |
| 198 |
m_gamma = nullptr; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
private: |
| 204 |
float m_t0; |
| 205 |
float m_h; |
| 206 |
float m_T; |
| 207 |
fftwf_complex* m_gamma; |
| 208 |
size_t m_complex_vector_size; |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
template<> |
| 213 |
class cardinal_trigonometric_detail<double> { |
| 214 |
public: |
| 215 |
cardinal_trigonometric_detail(const double* data, size_t length, double t0, double h) : m_t0{t0}, m_h{h} |
| 216 |
{ |
| 217 |
if (length == 0) |
| 218 |
{ |
| 219 |
throw std::logic_error("At least one sample is required."); |
| 220 |
} |
| 221 |
if (h <= 0) |
| 222 |
{ |
| 223 |
throw std::logic_error("The step size must be > 0"); |
| 224 |
} |
| 225 |
m_T = m_h*length; |
| 226 |
m_complex_vector_size = length/2 + 1; |
| 227 |
m_gamma = fftw_alloc_complex(m_complex_vector_size); |
| 228 |
fftw_plan plan = fftw_plan_dft_r2c_1d(length, const_cast<double*>(data), m_gamma, FFTW_ESTIMATE); |
| 229 |
if (!plan) |
| 230 |
{ |
| 231 |
throw std::logic_error("A null fftw plan was created."); |
| 232 |
} |
| 233 |
|
| 234 |
fftw_execute(plan); |
| 235 |
fftw_destroy_plan(plan); |
| 236 |
|
| 237 |
double denom = length; |
| 238 |
for (size_t k = 0; k < m_complex_vector_size; ++k) |
| 239 |
{ |
| 240 |
m_gamma[k][0] /= denom; |
| 241 |
m_gamma[k][1] /= denom; |
| 242 |
} |
| 243 |
|
| 244 |
if (length % 2 == 0) |
| 245 |
{ |
| 246 |
m_gamma[m_complex_vector_size -1][0] /= 2; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
cardinal_trigonometric_detail(const cardinal_trigonometric_detail& old) = delete; |
| 251 |
|
| 252 |
cardinal_trigonometric_detail& operator=(const cardinal_trigonometric_detail&) = delete; |
| 253 |
|
| 254 |
cardinal_trigonometric_detail(cardinal_trigonometric_detail &&) = delete; |
| 255 |
|
| 256 |
double operator()(double t) const |
| 257 |
{ |
| 258 |
using std::sin; |
| 259 |
using std::cos; |
| 260 |
using boost::math::constants::two_pi; |
| 261 |
using std::exp; |
| 262 |
double s = m_gamma[0][0]; |
| 263 |
double x = two_pi<double>()*(t - m_t0)/m_T; |
| 264 |
fftw_complex z; |
| 265 |
z[0] = cos(x); |
| 266 |
z[1] = sin(x); |
| 267 |
fftw_complex b{0, 0}; |
| 268 |
// u = b*z |
| 269 |
fftw_complex u; |
| 270 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) |
| 271 |
{ |
| 272 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 273 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 274 |
b[0] = m_gamma[k][0] + u[0]; |
| 275 |
b[1] = m_gamma[k][1] + u[1]; |
| 276 |
} |
| 277 |
|
| 278 |
s += 2*(b[0]*z[0] - b[1]*z[1]); |
| 279 |
return s; |
| 280 |
} |
| 281 |
|
| 282 |
double prime(double t) const |
| 283 |
{ |
| 284 |
using std::sin; |
| 285 |
using std::cos; |
| 286 |
using boost::math::constants::two_pi; |
| 287 |
using std::exp; |
| 288 |
double x = two_pi<double>()*(t - m_t0)/m_T; |
| 289 |
fftw_complex z; |
| 290 |
z[0] = cos(x); |
| 291 |
z[1] = sin(x); |
| 292 |
fftw_complex b{0, 0}; |
| 293 |
// u = b*z |
| 294 |
fftw_complex u; |
| 295 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) |
| 296 |
{ |
| 297 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 298 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 299 |
b[0] = k*m_gamma[k][0] + u[0]; |
| 300 |
b[1] = k*m_gamma[k][1] + u[1]; |
| 301 |
} |
| 302 |
// b*z = (b[0]*z[0] - b[1]*z[1]) + i(b[1]*z[0] + b[0]*z[1]) |
| 303 |
return -2*two_pi<double>()*(b[1]*z[0] + b[0]*z[1])/m_T; |
| 304 |
} |
| 305 |
|
| 306 |
double double_prime(double t) const |
| 307 |
{ |
| 308 |
using std::sin; |
| 309 |
using std::cos; |
| 310 |
using boost::math::constants::two_pi; |
| 311 |
using std::exp; |
| 312 |
double x = two_pi<double>()*(t - m_t0)/m_T; |
| 313 |
fftw_complex z; |
| 314 |
z[0] = cos(x); |
| 315 |
z[1] = sin(x); |
| 316 |
fftw_complex b{0, 0}; |
| 317 |
// u = b*z |
| 318 |
fftw_complex u; |
| 319 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) |
| 320 |
{ |
| 321 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 322 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 323 |
b[0] = k*k*m_gamma[k][0] + u[0]; |
| 324 |
b[1] = k*k*m_gamma[k][1] + u[1]; |
| 325 |
} |
| 326 |
// b*z = (b[0]*z[0] - b[1]*z[1]) + i(b[1]*z[0] + b[0]*z[1]) |
| 327 |
return -2*two_pi<double>()*two_pi<double>()*(b[0]*z[0] - b[1]*z[1])/(m_T*m_T); |
| 328 |
} |
| 329 |
|
| 330 |
double period() const |
| 331 |
{ |
| 332 |
return m_T; |
| 333 |
} |
| 334 |
|
| 335 |
double integrate() const |
| 336 |
{ |
| 337 |
return m_T*m_gamma[0][0]; |
| 338 |
} |
| 339 |
|
| 340 |
double squared_l2() const |
| 341 |
{ |
| 342 |
double s = 0; |
| 343 |
for (size_t i = m_complex_vector_size - 1; i >= 1; --i) |
| 344 |
{ |
| 345 |
s += (m_gamma[i][0]*m_gamma[i][0] + m_gamma[i][1]*m_gamma[i][1]); |
| 346 |
} |
| 347 |
s *= 2; |
| 348 |
s += m_gamma[0][0]*m_gamma[0][0]; |
| 349 |
return s*m_T; |
| 350 |
} |
| 351 |
|
| 352 |
~cardinal_trigonometric_detail() |
| 353 |
{ |
| 354 |
if (m_gamma) |
| 355 |
{ |
| 356 |
fftw_free(m_gamma); |
| 357 |
m_gamma = nullptr; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
private: |
| 362 |
double m_t0; |
| 363 |
double m_h; |
| 364 |
double m_T; |
| 365 |
fftw_complex* m_gamma; |
| 366 |
size_t m_complex_vector_size; |
| 367 |
}; |
| 368 |
|
| 369 |
|
| 370 |
template<> |
| 371 |
class cardinal_trigonometric_detail<long double> { |
| 372 |
public: |
| 373 |
cardinal_trigonometric_detail(const long double* data, size_t length, long double t0, long double h) : m_t0{t0}, m_h{h} |
| 374 |
{ |
| 375 |
if (length == 0) |
| 376 |
{ |
| 377 |
throw std::logic_error("At least one sample is required."); |
| 378 |
} |
| 379 |
if (h <= 0) |
| 380 |
{ |
| 381 |
throw std::logic_error("The step size must be > 0"); |
| 382 |
} |
| 383 |
m_T = m_h*length; |
| 384 |
m_complex_vector_size = length/2 + 1; |
| 385 |
m_gamma = fftwl_alloc_complex(m_complex_vector_size); |
| 386 |
fftwl_plan plan = fftwl_plan_dft_r2c_1d(length, const_cast<long double*>(data), m_gamma, FFTW_ESTIMATE); |
| 387 |
if (!plan) |
| 388 |
{ |
| 389 |
throw std::logic_error("A null fftw plan was created."); |
| 390 |
} |
| 391 |
|
| 392 |
fftwl_execute(plan); |
| 393 |
fftwl_destroy_plan(plan); |
| 394 |
|
| 395 |
long double denom = length; |
| 396 |
for (size_t k = 0; k < m_complex_vector_size; ++k) |
| 397 |
{ |
| 398 |
m_gamma[k][0] /= denom; |
| 399 |
m_gamma[k][1] /= denom; |
| 400 |
} |
| 401 |
|
| 402 |
if (length % 2 == 0) { |
| 403 |
m_gamma[m_complex_vector_size -1][0] /= 2; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
cardinal_trigonometric_detail(const cardinal_trigonometric_detail& old) = delete; |
| 408 |
|
| 409 |
cardinal_trigonometric_detail& operator=(const cardinal_trigonometric_detail&) = delete; |
| 410 |
|
| 411 |
cardinal_trigonometric_detail(cardinal_trigonometric_detail &&) = delete; |
| 412 |
|
| 413 |
long double operator()(long double t) const |
| 414 |
{ |
| 415 |
using std::sin; |
| 416 |
using std::cos; |
| 417 |
using boost::math::constants::two_pi; |
| 418 |
using std::exp; |
| 419 |
long double s = m_gamma[0][0]; |
| 420 |
long double x = two_pi<long double>()*(t - m_t0)/m_T; |
| 421 |
fftwl_complex z; |
| 422 |
z[0] = cos(x); |
| 423 |
z[1] = sin(x); |
| 424 |
fftwl_complex b{0, 0}; |
| 425 |
fftwl_complex u; |
| 426 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) |
| 427 |
{ |
| 428 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 429 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 430 |
b[0] = m_gamma[k][0] + u[0]; |
| 431 |
b[1] = m_gamma[k][1] + u[1]; |
| 432 |
} |
| 433 |
|
| 434 |
s += 2*(b[0]*z[0] - b[1]*z[1]); |
| 435 |
return s; |
| 436 |
} |
| 437 |
|
| 438 |
long double prime(long double t) const |
| 439 |
{ |
| 440 |
using std::sin; |
| 441 |
using std::cos; |
| 442 |
using boost::math::constants::two_pi; |
| 443 |
using std::exp; |
| 444 |
long double x = two_pi<long double>()*(t - m_t0)/m_T; |
| 445 |
fftwl_complex z; |
| 446 |
z[0] = cos(x); |
| 447 |
z[1] = sin(x); |
| 448 |
fftwl_complex b{0, 0}; |
| 449 |
// u = b*z |
| 450 |
fftwl_complex u; |
| 451 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) |
| 452 |
{ |
| 453 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 454 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 455 |
b[0] = k*m_gamma[k][0] + u[0]; |
| 456 |
b[1] = k*m_gamma[k][1] + u[1]; |
| 457 |
} |
| 458 |
// b*z = (b[0]*z[0] - b[1]*z[1]) + i(b[1]*z[0] + b[0]*z[1]) |
| 459 |
return -2*two_pi<long double>()*(b[1]*z[0] + b[0]*z[1])/m_T; |
| 460 |
} |
| 461 |
|
| 462 |
long double double_prime(long double t) const |
| 463 |
{ |
| 464 |
using std::sin; |
| 465 |
using std::cos; |
| 466 |
using boost::math::constants::two_pi; |
| 467 |
using std::exp; |
| 468 |
long double x = two_pi<long double>()*(t - m_t0)/m_T; |
| 469 |
fftwl_complex z; |
| 470 |
z[0] = cos(x); |
| 471 |
z[1] = sin(x); |
| 472 |
fftwl_complex b{0, 0}; |
| 473 |
// u = b*z |
| 474 |
fftwl_complex u; |
| 475 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) |
| 476 |
{ |
| 477 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 478 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 479 |
b[0] = k*k*m_gamma[k][0] + u[0]; |
| 480 |
b[1] = k*k*m_gamma[k][1] + u[1]; |
| 481 |
} |
| 482 |
// b*z = (b[0]*z[0] - b[1]*z[1]) + i(b[1]*z[0] + b[0]*z[1]) |
| 483 |
return -2*two_pi<long double>()*two_pi<long double>()*(b[0]*z[0] - b[1]*z[1])/(m_T*m_T); |
| 484 |
} |
| 485 |
|
| 486 |
long double period() const |
| 487 |
{ |
| 488 |
return m_T; |
| 489 |
} |
| 490 |
|
| 491 |
long double integrate() const |
| 492 |
{ |
| 493 |
return m_T*m_gamma[0][0]; |
| 494 |
} |
| 495 |
|
| 496 |
long double squared_l2() const |
| 497 |
{ |
| 498 |
long double s = 0; |
| 499 |
for (size_t i = m_complex_vector_size - 1; i >= 1; --i) |
| 500 |
{ |
| 501 |
s += (m_gamma[i][0]*m_gamma[i][0] + m_gamma[i][1]*m_gamma[i][1]); |
| 502 |
} |
| 503 |
s *= 2; |
| 504 |
s += m_gamma[0][0]*m_gamma[0][0]; |
| 505 |
return s*m_T; |
| 506 |
} |
| 507 |
|
| 508 |
~cardinal_trigonometric_detail() |
| 509 |
{ |
| 510 |
if (m_gamma) |
| 511 |
{ |
| 512 |
fftwl_free(m_gamma); |
| 513 |
m_gamma = nullptr; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
private: |
| 518 |
long double m_t0; |
| 519 |
long double m_h; |
| 520 |
long double m_T; |
| 521 |
fftwl_complex* m_gamma; |
| 522 |
size_t m_complex_vector_size; |
| 523 |
}; |
| 524 |
|
| 525 |
#ifdef BOOST_HAS_FLOAT128 |
| 526 |
template<> |
| 527 |
class cardinal_trigonometric_detail<__float128> { |
| 528 |
public: |
| 529 |
cardinal_trigonometric_detail(const __float128* data, size_t length, __float128 t0, __float128 h) : m_t0{t0}, m_h{h} |
| 530 |
{ |
| 531 |
if (length == 0) |
| 532 |
{ |
| 533 |
throw std::logic_error("At least one sample is required."); |
| 534 |
} |
| 535 |
if (h <= 0) |
| 536 |
{ |
| 537 |
throw std::logic_error("The step size must be > 0"); |
| 538 |
} |
| 539 |
m_T = m_h*length; |
| 540 |
m_complex_vector_size = length/2 + 1; |
| 541 |
m_gamma = fftwq_alloc_complex(m_complex_vector_size); |
| 542 |
fftwq_plan plan = fftwq_plan_dft_r2c_1d(length, reinterpret_cast<__float128*>(const_cast<__float128*>(data)), m_gamma, FFTW_ESTIMATE); |
| 543 |
if (!plan) |
| 544 |
{ |
| 545 |
throw std::logic_error("A null fftw plan was created."); |
| 546 |
} |
| 547 |
|
| 548 |
fftwq_execute(plan); |
| 549 |
fftwq_destroy_plan(plan); |
| 550 |
|
| 551 |
__float128 denom = length; |
| 552 |
for (size_t k = 0; k < m_complex_vector_size; ++k) |
| 553 |
{ |
| 554 |
m_gamma[k][0] /= denom; |
| 555 |
m_gamma[k][1] /= denom; |
| 556 |
} |
| 557 |
if (length % 2 == 0) |
| 558 |
{ |
| 559 |
m_gamma[m_complex_vector_size -1][0] /= 2; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
cardinal_trigonometric_detail(const cardinal_trigonometric_detail& old) = delete; |
| 564 |
|
| 565 |
cardinal_trigonometric_detail& operator=(const cardinal_trigonometric_detail&) = delete; |
| 566 |
|
| 567 |
cardinal_trigonometric_detail(cardinal_trigonometric_detail &&) = delete; |
| 568 |
|
| 569 |
__float128 operator()(__float128 t) const |
| 570 |
{ |
| 571 |
using std::sin; |
| 572 |
using std::cos; |
| 573 |
using boost::math::constants::two_pi; |
| 574 |
using std::exp; |
| 575 |
__float128 s = m_gamma[0][0]; |
| 576 |
__float128 x = two_pi<__float128>()*(t - m_t0)/m_T; |
| 577 |
fftwq_complex z; |
| 578 |
z[0] = cosq(x); |
| 579 |
z[1] = sinq(x); |
| 580 |
fftwq_complex b{0, 0}; |
| 581 |
fftwq_complex u; |
| 582 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) |
| 583 |
{ |
| 584 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 585 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 586 |
b[0] = m_gamma[k][0] + u[0]; |
| 587 |
b[1] = m_gamma[k][1] + u[1]; |
| 588 |
} |
| 589 |
|
| 590 |
s += 2*(b[0]*z[0] - b[1]*z[1]); |
| 591 |
return s; |
| 592 |
} |
| 593 |
|
| 594 |
__float128 prime(__float128 t) const |
| 595 |
{ |
| 596 |
using std::sin; |
| 597 |
using std::cos; |
| 598 |
using boost::math::constants::two_pi; |
| 599 |
using std::exp; |
| 600 |
__float128 x = two_pi<__float128>()*(t - m_t0)/m_T; |
| 601 |
fftwq_complex z; |
| 602 |
z[0] = cosq(x); |
| 603 |
z[1] = sinq(x); |
| 604 |
fftwq_complex b{0, 0}; |
| 605 |
// u = b*z |
| 606 |
fftwq_complex u; |
| 607 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) |
| 608 |
{ |
| 609 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 610 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 611 |
b[0] = k*m_gamma[k][0] + u[0]; |
| 612 |
b[1] = k*m_gamma[k][1] + u[1]; |
| 613 |
} |
| 614 |
// b*z = (b[0]*z[0] - b[1]*z[1]) + i(b[1]*z[0] + b[0]*z[1]) |
| 615 |
return -2*two_pi<__float128>()*(b[1]*z[0] + b[0]*z[1])/m_T; |
| 616 |
} |
| 617 |
|
| 618 |
__float128 double_prime(__float128 t) const |
| 619 |
{ |
| 620 |
using std::sin; |
| 621 |
using std::cos; |
| 622 |
using boost::math::constants::two_pi; |
| 623 |
using std::exp; |
| 624 |
__float128 x = two_pi<__float128>()*(t - m_t0)/m_T; |
| 625 |
fftwq_complex z; |
| 626 |
z[0] = cosq(x); |
| 627 |
z[1] = sinq(x); |
| 628 |
fftwq_complex b{0, 0}; |
| 629 |
// u = b*z |
| 630 |
fftwq_complex u; |
| 631 |
for (size_t k = m_complex_vector_size - 1; k >= 1; --k) |
| 632 |
{ |
| 633 |
u[0] = b[0]*z[0] - b[1]*z[1]; |
| 634 |
u[1] = b[0]*z[1] + b[1]*z[0]; |
| 635 |
b[0] = k*k*m_gamma[k][0] + u[0]; |
| 636 |
b[1] = k*k*m_gamma[k][1] + u[1]; |
| 637 |
} |
| 638 |
// b*z = (b[0]*z[0] - b[1]*z[1]) + i(b[1]*z[0] + b[0]*z[1]) |
| 639 |
return -2*two_pi<__float128>()*two_pi<__float128>()*(b[0]*z[0] - b[1]*z[1])/(m_T*m_T); |
| 640 |
} |
| 641 |
|
| 642 |
__float128 period() const |
| 643 |
{ |
| 644 |
return m_T; |
| 645 |
} |
| 646 |
|
| 647 |
__float128 integrate() const |
| 648 |
{ |
| 649 |
return m_T*m_gamma[0][0]; |
| 650 |
} |
| 651 |
|
| 652 |
__float128 squared_l2() const |
| 653 |
{ |
| 654 |
__float128 s = 0; |
| 655 |
for (size_t i = m_complex_vector_size - 1; i >= 1; --i) |
| 656 |
{ |
| 657 |
s += (m_gamma[i][0]*m_gamma[i][0] + m_gamma[i][1]*m_gamma[i][1]); |
| 658 |
} |
| 659 |
s *= 2; |
| 660 |
s += m_gamma[0][0]*m_gamma[0][0]; |
| 661 |
return s*m_T; |
| 662 |
} |
| 663 |
|
| 664 |
~cardinal_trigonometric_detail() |
| 665 |
{ |
| 666 |
if (m_gamma) |
| 667 |
{ |
| 668 |
fftwq_free(m_gamma); |
| 669 |
m_gamma = nullptr; |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
private: |
| 675 |
__float128 m_t0; |
| 676 |
__float128 m_h; |
| 677 |
__float128 m_T; |
| 678 |
fftwq_complex* m_gamma; |
| 679 |
size_t m_complex_vector_size; |
| 680 |
}; |
| 681 |
#endif |
| 682 |
|
| 683 |
}}}} |
| 684 |
#endif |