24#include "builtin/fftpack4/fftpack4.h"
25#include "builtin/fftpack4/fftpack4_precision.h"
28#include "marley/ChebyshevInterpolatingFunction.hh"
31 constexpr double MY_EPSILON = std::numeric_limits<double>::epsilon();
35 const std::function<
double(
double)>& func,
double x_min,
double x_max,
37 : marley::InterpolatingFunction( x_min, x_max )
62 for (
size_t j = 0; j <=
N_; ++j ) {
70 wsave_ = std::vector<double>(3*
Fs_.size() + 15, 0.);
71 ifac_ = std::vector<int>(
Fs_.size() / 2, 0);
73 int my_size =
Fs_.size();
74 costi( &my_size, wsave_.data(), ifac_.data() );
79 { return std::abs(left) < std::abs(right); });
81 double upper_limit = 2. * std::abs( biggest_coeff ) * MY_EPSILON;
84 double next_to_last_coeff_mag = std::abs(
86 if ( last_coeff_mag < upper_limit && next_to_last_coeff_mag < upper_limit )
104void marley::ChebyshevInterpolatingFunction::compute_integral() {
108 for (
size_t k = 0; k <= N_; k += 2) {
110 double term = chebyshev_coeffs_.at( k ) * 2.0 / ( 1. - std::pow(k, 2) );
111 if ( k == 0 || k == N_ ) term /= 2.;
117 integral_ *= (x_max_ - x_min_) / 2.;
121marley::ChebyshevInterpolatingFunction
122 marley::ChebyshevInterpolatingFunction::cdf()
const
124 marley::ChebyshevInterpolatingFunction result;
125 result.
N_ = this->N_ + 1;
126 result.
x_min_ = this->x_min_;
127 result.
x_max_ = this->x_max_;
129 double beta_0 = this->chebyshev_coeffs_.at(0);
130 beta_0 += -0.5 * this->chebyshev_coeffs_.at(1);
131 for (
size_t j = 2; j <= this->N_; ++j ) {
132 beta_0 += 2.*this->chebyshev_coeffs_.at(j)
133 * std::pow(-1.0, j + 1) / (j*j - 1.0);
137 for (
size_t k = 1; k < this->N_; ++k ) {
138 double ak_minus_one = this->chebyshev_coeffs_.at( k - 1 );
139 double ak_plus_one = this->chebyshev_coeffs_.at( k + 1 );
141 (ak_minus_one - ak_plus_one) / (2 * k) );
150 result.compute_integral();
152 for (
size_t j = 0; j <= result.
N_; ++j ) {
154 result.
Xs_.push_back( x );
157 result.wsave_ = std::vector<double>(
163 costi( &my_size, result.wsave_.data(), result.ifac_.data() );
164 cost( &my_size, result.
Fs_.data(), result.wsave_.data(), result.ifac_.data() );
165 for (
double& d : result.
Fs_) d *= 0.5;
static constexpr size_t N_MAX_
Maximum allowed value of the grid size parameter.
size_t N_
Grid size parameter (N + 1 total points)
ChebyshevInterpolatingFunction()
Default constructor used by cdf()
double chebyshev_point(size_t j) const
For a given N, returns the x position of the jth Chebyshev point (of the second kind)
std::vector< double > Xs_
Chebyshev points at which the function was evaluated.
std::vector< double > chebyshev_coeffs_
Coefficients of the Chebyshev expansion of this function.
std::vector< double > Fs_
Function values at the grid points.
double x_max_
Upper bound of the interpolation range.
double x_min_
Lower bound of the interpolation range.