feat():initial version
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,651 @@
|
||||
// Copyright Nick Thompson, 2019
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
#ifndef BOOST_MATH_QUADRATURE_DETAIL_OOURA_FOURIER_INTEGRALS_DETAIL_HPP
|
||||
#define BOOST_MATH_QUADRATURE_DETAIL_OOURA_FOURIER_INTEGRALS_DETAIL_HPP
|
||||
#include <utility> // for std::pair.
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <boost/math/special_functions/expm1.hpp>
|
||||
#include <boost/math/special_functions/sin_pi.hpp>
|
||||
#include <boost/math/special_functions/cos_pi.hpp>
|
||||
#include <boost/math/constants/constants.hpp>
|
||||
|
||||
namespace boost { namespace math { namespace quadrature { namespace detail {
|
||||
|
||||
// Ooura and Mori, A robust double exponential formula for Fourier-type integrals,
|
||||
// eta is the argument to the exponential in equation 3.3:
|
||||
template<class Real>
|
||||
std::pair<Real, Real> ooura_eta(Real x, Real alpha) {
|
||||
using std::expm1;
|
||||
using std::exp;
|
||||
using std::abs;
|
||||
Real expx = exp(x);
|
||||
Real eta_prime = 2 + alpha/expx + expx/4;
|
||||
Real eta;
|
||||
// This is the fast branch:
|
||||
if (abs(x) > 0.125) {
|
||||
eta = 2*x - alpha*(1/expx - 1) + (expx - 1)/4;
|
||||
}
|
||||
else {// this is the slow branch using expm1 for small x:
|
||||
eta = 2*x - alpha*expm1(-x) + expm1(x)/4;
|
||||
}
|
||||
return {eta, eta_prime};
|
||||
}
|
||||
|
||||
// Ooura and Mori, A robust double exponential formula for Fourier-type integrals,
|
||||
// equation 3.6:
|
||||
template<class Real>
|
||||
Real calculate_ooura_alpha(Real h)
|
||||
{
|
||||
using boost::math::constants::pi;
|
||||
using std::log1p;
|
||||
using std::sqrt;
|
||||
Real x = sqrt(16 + 4*log1p(pi<Real>()/h)/h);
|
||||
return 1/x;
|
||||
}
|
||||
|
||||
template<class Real>
|
||||
std::pair<Real, Real> ooura_sin_node_and_weight(long n, Real h, Real alpha)
|
||||
{
|
||||
using std::expm1;
|
||||
using std::exp;
|
||||
using std::abs;
|
||||
using boost::math::constants::pi;
|
||||
using std::isnan;
|
||||
|
||||
if (n == 0) {
|
||||
// Equation 44 of https://arxiv.org/pdf/0911.4796.pdf
|
||||
// Fourier Transform of the Stretched Exponential Function: Analytic Error Bounds,
|
||||
// Double Exponential Transform, and Open-Source Implementation,
|
||||
// Joachim Wuttke,
|
||||
// The C library libkww provides functions to compute the Kohlrausch-Williams-Watts function,
|
||||
// the Laplace-Fourier transform of the stretched (or compressed) exponential function exp(-t^beta)
|
||||
// for exponent beta between 0.1 and 1.9 with sixteen decimal digits accuracy.
|
||||
|
||||
Real eta_prime_0 = Real(2) + alpha + Real(1)/Real(4);
|
||||
Real node = pi<Real>()/(eta_prime_0*h);
|
||||
Real weight = pi<Real>()*boost::math::sin_pi(1/(eta_prime_0*h));
|
||||
Real eta_dbl_prime = -alpha + Real(1)/Real(4);
|
||||
Real phi_prime_0 = (1 - eta_dbl_prime/(eta_prime_0*eta_prime_0))/2;
|
||||
weight *= phi_prime_0;
|
||||
return {node, weight};
|
||||
}
|
||||
Real x = n*h;
|
||||
auto p = ooura_eta(x, alpha);
|
||||
auto eta = p.first;
|
||||
auto eta_prime = p.second;
|
||||
|
||||
Real expm1_meta = expm1(-eta);
|
||||
Real exp_meta = exp(-eta);
|
||||
Real node = -n*pi<Real>()/expm1_meta;
|
||||
|
||||
|
||||
// I have verified that this is not a significant source of inaccuracy in the weight computation:
|
||||
Real phi_prime = -(expm1_meta + x*exp_meta*eta_prime)/(expm1_meta*expm1_meta);
|
||||
|
||||
// The main source of inaccuracy is in computation of sin_pi.
|
||||
// But I've agonized over this, and I think it's as good as it can get:
|
||||
Real s = pi<Real>();
|
||||
Real arg;
|
||||
if(eta > 1) {
|
||||
arg = n/( 1/exp_meta - 1 );
|
||||
s *= boost::math::sin_pi(arg);
|
||||
if (n&1) {
|
||||
s *= -1;
|
||||
}
|
||||
}
|
||||
else if (eta < -1) {
|
||||
arg = n/(1-exp_meta);
|
||||
s *= boost::math::sin_pi(arg);
|
||||
}
|
||||
else {
|
||||
arg = -n*exp_meta/expm1_meta;
|
||||
s *= boost::math::sin_pi(arg);
|
||||
if (n&1) {
|
||||
s *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
Real weight = s*phi_prime;
|
||||
return {node, weight};
|
||||
}
|
||||
|
||||
#ifdef BOOST_MATH_INSTRUMENT_OOURA
|
||||
template<class Real>
|
||||
void print_ooura_estimate(size_t i, Real I0, Real I1, Real omega) {
|
||||
using std::abs;
|
||||
std::cout << std::defaultfloat
|
||||
<< std::setprecision(std::numeric_limits<Real>::digits10)
|
||||
<< std::fixed;
|
||||
std::cout << "h = " << Real(1)/Real(1<<i) << ", I_h = " << I0/omega
|
||||
<< " = " << std::hexfloat << I0/omega << ", absolute error estimate = "
|
||||
<< std::defaultfloat << std::scientific << abs(I0-I1) << std::endl;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
template<class Real>
|
||||
std::pair<Real, Real> ooura_cos_node_and_weight(long n, Real h, Real alpha)
|
||||
{
|
||||
using std::expm1;
|
||||
using std::exp;
|
||||
using std::abs;
|
||||
using boost::math::constants::pi;
|
||||
|
||||
Real x = h*(n-Real(1)/Real(2));
|
||||
auto p = ooura_eta(x, alpha);
|
||||
auto eta = p.first;
|
||||
auto eta_prime = p.second;
|
||||
Real expm1_meta = expm1(-eta);
|
||||
Real exp_meta = exp(-eta);
|
||||
Real node = pi<Real>()*(Real(1)/Real(2)-n)/expm1_meta;
|
||||
|
||||
Real phi_prime = -(expm1_meta + x*exp_meta*eta_prime)/(expm1_meta*expm1_meta);
|
||||
|
||||
// Takuya Ooura and Masatake Mori,
|
||||
// Journal of Computational and Applied Mathematics, 112 (1999) 229-241.
|
||||
// A robust double exponential formula for Fourier-type integrals.
|
||||
// Equation 4.6
|
||||
Real s = pi<Real>();
|
||||
Real arg;
|
||||
if (eta < -1) {
|
||||
arg = -(n-Real(1)/Real(2))/expm1_meta;
|
||||
s *= boost::math::cos_pi(arg);
|
||||
}
|
||||
else {
|
||||
arg = -(n-Real(1)/Real(2))*exp_meta/expm1_meta;
|
||||
s *= boost::math::sin_pi(arg);
|
||||
if (n&1) {
|
||||
s *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
Real weight = s*phi_prime;
|
||||
return {node, weight};
|
||||
}
|
||||
|
||||
|
||||
template<class Real>
|
||||
class ooura_fourier_sin_detail {
|
||||
public:
|
||||
ooura_fourier_sin_detail(const Real relative_error_goal, size_t levels) {
|
||||
#ifdef BOOST_MATH_INSTRUMENT_OOURA
|
||||
std::cout << "ooura_fourier_sin with relative error goal " << relative_error_goal
|
||||
<< " & " << levels << " levels." << std::endl;
|
||||
#endif // BOOST_MATH_INSTRUMENT_OOURA
|
||||
if (relative_error_goal < std::numeric_limits<Real>::epsilon() * 2) {
|
||||
throw std::domain_error("The relative error goal cannot be smaller than the unit roundoff.");
|
||||
}
|
||||
using std::abs;
|
||||
requested_levels_ = levels;
|
||||
starting_level_ = 0;
|
||||
rel_err_goal_ = relative_error_goal;
|
||||
big_nodes_.reserve(levels);
|
||||
bweights_.reserve(levels);
|
||||
little_nodes_.reserve(levels);
|
||||
lweights_.reserve(levels);
|
||||
|
||||
for (size_t i = 0; i < levels; ++i) {
|
||||
if (std::is_same<Real, float>::value) {
|
||||
add_level<double>(i);
|
||||
}
|
||||
else if (std::is_same<Real, double>::value) {
|
||||
add_level<long double>(i);
|
||||
}
|
||||
else {
|
||||
add_level<Real>(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<Real>> const & big_nodes() const {
|
||||
return big_nodes_;
|
||||
}
|
||||
|
||||
std::vector<std::vector<Real>> const & weights_for_big_nodes() const {
|
||||
return bweights_;
|
||||
}
|
||||
|
||||
std::vector<std::vector<Real>> const & little_nodes() const {
|
||||
return little_nodes_;
|
||||
}
|
||||
|
||||
std::vector<std::vector<Real>> const & weights_for_little_nodes() const {
|
||||
return lweights_;
|
||||
}
|
||||
|
||||
template<class F>
|
||||
std::pair<Real,Real> integrate(F const & f, Real omega) {
|
||||
using std::abs;
|
||||
using std::max;
|
||||
using boost::math::constants::pi;
|
||||
|
||||
if (omega == 0) {
|
||||
return {Real(0), Real(0)};
|
||||
}
|
||||
if (omega < 0) {
|
||||
auto p = this->integrate(f, -omega);
|
||||
return {-p.first, p.second};
|
||||
}
|
||||
|
||||
Real I1 = std::numeric_limits<Real>::quiet_NaN();
|
||||
Real relative_error_estimate = std::numeric_limits<Real>::quiet_NaN();
|
||||
// As we compute integrals, we learn about their structure.
|
||||
// Assuming we compute f(t)sin(wt) for many different omega, this gives some
|
||||
// a posteriori ability to choose a refinement level that is roughly appropriate.
|
||||
size_t i = starting_level_;
|
||||
do {
|
||||
Real I0 = estimate_integral(f, omega, i);
|
||||
#ifdef BOOST_MATH_INSTRUMENT_OOURA
|
||||
print_ooura_estimate(i, I0, I1, omega);
|
||||
#endif
|
||||
Real absolute_error_estimate = abs(I0-I1);
|
||||
Real scale = (max)(abs(I0), abs(I1));
|
||||
if (!isnan(I1) && absolute_error_estimate <= rel_err_goal_*scale) {
|
||||
starting_level_ = (max)(long(i) - 1, long(0));
|
||||
return {I0/omega, absolute_error_estimate/scale};
|
||||
}
|
||||
I1 = I0;
|
||||
} while(++i < big_nodes_.size());
|
||||
|
||||
// We've used up all our precomputed levels.
|
||||
// Now we need to add more.
|
||||
// It might seems reasonable to just keep adding levels indefinitely, if that's what the user wants.
|
||||
// But in fact the nodes and weights just merge into each other and the error gets worse after a certain number.
|
||||
// This value for max_additional_levels was chosen by observation of a slowly converging oscillatory integral:
|
||||
// f(x) := cos(7cos(x))sin(x)/x
|
||||
size_t max_additional_levels = 4;
|
||||
while (big_nodes_.size() < requested_levels_ + max_additional_levels) {
|
||||
size_t ii = big_nodes_.size();
|
||||
if (std::is_same<Real, float>::value) {
|
||||
add_level<double>(ii);
|
||||
}
|
||||
else if (std::is_same<Real, double>::value) {
|
||||
add_level<long double>(ii);
|
||||
}
|
||||
else {
|
||||
add_level<Real>(ii);
|
||||
}
|
||||
Real I0 = estimate_integral(f, omega, ii);
|
||||
Real absolute_error_estimate = abs(I0-I1);
|
||||
Real scale = (max)(abs(I0), abs(I1));
|
||||
#ifdef BOOST_MATH_INSTRUMENT_OOURA
|
||||
print_ooura_estimate(ii, I0, I1, omega);
|
||||
#endif
|
||||
if (absolute_error_estimate <= rel_err_goal_*scale) {
|
||||
starting_level_ = (max)(long(ii) - 1, long(0));
|
||||
return {I0/omega, absolute_error_estimate/scale};
|
||||
}
|
||||
I1 = I0;
|
||||
++ii;
|
||||
}
|
||||
|
||||
starting_level_ = static_cast<long>(big_nodes_.size() - 2);
|
||||
return {I1/omega, relative_error_estimate};
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
template<class PreciseReal>
|
||||
void add_level(size_t i) {
|
||||
using std::abs;
|
||||
size_t current_num_levels = big_nodes_.size();
|
||||
Real unit_roundoff = std::numeric_limits<Real>::epsilon()/2;
|
||||
// h0 = 1. Then all further levels have h_i = 1/2^i.
|
||||
// Since the nodes don't nest, we could conceivably divide h by (say) 1.5, or 3.
|
||||
// It's not clear how much benefit (or loss) would be obtained from this.
|
||||
PreciseReal h = PreciseReal(1)/PreciseReal(1<<i);
|
||||
|
||||
std::vector<Real> bnode_row;
|
||||
std::vector<Real> bweight_row;
|
||||
|
||||
// This is a pretty good estimate for how many elements will be placed in the vector:
|
||||
bnode_row.reserve((static_cast<size_t>(1)<<i)*sizeof(Real));
|
||||
bweight_row.reserve((static_cast<size_t>(1)<<i)*sizeof(Real));
|
||||
|
||||
std::vector<Real> lnode_row;
|
||||
std::vector<Real> lweight_row;
|
||||
|
||||
lnode_row.reserve((static_cast<size_t>(1)<<i)*sizeof(Real));
|
||||
lweight_row.reserve((static_cast<size_t>(1)<<i)*sizeof(Real));
|
||||
|
||||
Real max_weight = 1;
|
||||
auto alpha = calculate_ooura_alpha(h);
|
||||
long n = 0;
|
||||
Real w;
|
||||
do {
|
||||
auto precise_nw = ooura_sin_node_and_weight(n, h, alpha);
|
||||
Real node = static_cast<Real>(precise_nw.first);
|
||||
Real weight = static_cast<Real>(precise_nw.second);
|
||||
w = weight;
|
||||
if (bnode_row.size() == bnode_row.capacity()) {
|
||||
bnode_row.reserve(2*bnode_row.size());
|
||||
bweight_row.reserve(2*bnode_row.size());
|
||||
}
|
||||
|
||||
bnode_row.push_back(node);
|
||||
bweight_row.push_back(weight);
|
||||
if (abs(weight) > max_weight) {
|
||||
max_weight = abs(weight);
|
||||
}
|
||||
++n;
|
||||
// f(t)->0 as t->infty, which is why the weights are computed up to the unit roundoff.
|
||||
} while(abs(w) > unit_roundoff*max_weight);
|
||||
|
||||
// This class tends to consume a lot of memory; shrink the vectors back down to size:
|
||||
bnode_row.shrink_to_fit();
|
||||
bweight_row.shrink_to_fit();
|
||||
// Why we are splitting the nodes into regimes where t_n >> 1 and t_n << 1?
|
||||
// It will create the opportunity to sensibly truncate the quadrature sum to significant terms.
|
||||
n = -1;
|
||||
do {
|
||||
auto precise_nw = ooura_sin_node_and_weight(n, h, alpha);
|
||||
Real node = static_cast<Real>(precise_nw.first);
|
||||
if (node <= 0) {
|
||||
break;
|
||||
}
|
||||
Real weight = static_cast<Real>(precise_nw.second);
|
||||
w = weight;
|
||||
using std::isnan;
|
||||
if (isnan(node)) {
|
||||
// This occurs at n = -11 in quad precision:
|
||||
break;
|
||||
}
|
||||
if (lnode_row.size() > 0) {
|
||||
if (lnode_row[lnode_row.size()-1] == node) {
|
||||
// The nodes have fused into each other:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (lnode_row.size() == lnode_row.capacity()) {
|
||||
lnode_row.reserve(2*lnode_row.size());
|
||||
lweight_row.reserve(2*lnode_row.size());
|
||||
}
|
||||
lnode_row.push_back(node);
|
||||
lweight_row.push_back(weight);
|
||||
if (abs(weight) > max_weight) {
|
||||
max_weight = abs(weight);
|
||||
}
|
||||
--n;
|
||||
// f(t)->infty is possible as t->0, hence compute up to the min.
|
||||
} while(abs(w) > (std::numeric_limits<Real>::min)()*max_weight);
|
||||
|
||||
lnode_row.shrink_to_fit();
|
||||
lweight_row.shrink_to_fit();
|
||||
|
||||
// std::scoped_lock once C++17 is more common?
|
||||
std::lock_guard<std::mutex> lock(node_weight_mutex_);
|
||||
// Another thread might have already finished this calculation and appended it to the nodes/weights:
|
||||
if (current_num_levels == big_nodes_.size()) {
|
||||
big_nodes_.push_back(bnode_row);
|
||||
bweights_.push_back(bweight_row);
|
||||
|
||||
little_nodes_.push_back(lnode_row);
|
||||
lweights_.push_back(lweight_row);
|
||||
}
|
||||
}
|
||||
|
||||
template<class F>
|
||||
Real estimate_integral(F const & f, Real omega, size_t i) {
|
||||
// Because so few function evaluations are required to get high accuracy on the integrals in the tests,
|
||||
// Kahan summation doesn't really help.
|
||||
//auto cond = boost::math::tools::summation_condition_number<Real, true>(0);
|
||||
Real I0 = 0;
|
||||
auto const & b_nodes = big_nodes_[i];
|
||||
auto const & b_weights = bweights_[i];
|
||||
// Will benchmark if this is helpful:
|
||||
Real inv_omega = 1/omega;
|
||||
for(size_t j = 0 ; j < b_nodes.size(); ++j) {
|
||||
I0 += f(b_nodes[j]*inv_omega)*b_weights[j];
|
||||
}
|
||||
|
||||
auto const & l_nodes = little_nodes_[i];
|
||||
auto const & l_weights = lweights_[i];
|
||||
// If f decays rapidly as |t|->infty, not all of these calls are necessary.
|
||||
for (size_t j = 0; j < l_nodes.size(); ++j) {
|
||||
I0 += f(l_nodes[j]*inv_omega)*l_weights[j];
|
||||
}
|
||||
return I0;
|
||||
}
|
||||
|
||||
std::mutex node_weight_mutex_;
|
||||
// Nodes for n >= 0, giving t_n = pi*phi(nh)/h. Generally t_n >> 1.
|
||||
std::vector<std::vector<Real>> big_nodes_;
|
||||
// The term bweights_ will indicate that these are weights corresponding
|
||||
// to the big nodes:
|
||||
std::vector<std::vector<Real>> bweights_;
|
||||
|
||||
// Nodes for n < 0: Generally t_n << 1, and an invariant is that t_n > 0.
|
||||
std::vector<std::vector<Real>> little_nodes_;
|
||||
std::vector<std::vector<Real>> lweights_;
|
||||
Real rel_err_goal_;
|
||||
std::atomic<long> starting_level_;
|
||||
size_t requested_levels_;
|
||||
};
|
||||
|
||||
template<class Real>
|
||||
class ooura_fourier_cos_detail {
|
||||
public:
|
||||
ooura_fourier_cos_detail(const Real relative_error_goal, size_t levels) {
|
||||
#ifdef BOOST_MATH_INSTRUMENT_OOURA
|
||||
std::cout << "ooura_fourier_cos with relative error goal " << relative_error_goal
|
||||
<< " & " << levels << " levels." << std::endl;
|
||||
std::cout << "epsilon for type = " << std::numeric_limits<Real>::epsilon() << std::endl;
|
||||
#endif // BOOST_MATH_INSTRUMENT_OOURA
|
||||
if (relative_error_goal < std::numeric_limits<Real>::epsilon() * 2) {
|
||||
throw std::domain_error("The relative error goal cannot be smaller than the unit roundoff!");
|
||||
}
|
||||
|
||||
using std::abs;
|
||||
requested_levels_ = levels;
|
||||
starting_level_ = 0;
|
||||
rel_err_goal_ = relative_error_goal;
|
||||
big_nodes_.reserve(levels);
|
||||
bweights_.reserve(levels);
|
||||
little_nodes_.reserve(levels);
|
||||
lweights_.reserve(levels);
|
||||
|
||||
for (size_t i = 0; i < levels; ++i) {
|
||||
if (std::is_same<Real, float>::value) {
|
||||
add_level<double>(i);
|
||||
}
|
||||
else if (std::is_same<Real, double>::value) {
|
||||
add_level<long double>(i);
|
||||
}
|
||||
else {
|
||||
add_level<Real>(i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<class F>
|
||||
std::pair<Real,Real> integrate(F const & f, Real omega) {
|
||||
using std::abs;
|
||||
using std::max;
|
||||
using boost::math::constants::pi;
|
||||
|
||||
if (omega == 0) {
|
||||
throw std::domain_error("At omega = 0, the integral is not oscillatory. The user must choose an appropriate method for this case.\n");
|
||||
}
|
||||
|
||||
if (omega < 0) {
|
||||
return this->integrate(f, -omega);
|
||||
}
|
||||
|
||||
Real I1 = std::numeric_limits<Real>::quiet_NaN();
|
||||
Real absolute_error_estimate = std::numeric_limits<Real>::quiet_NaN();
|
||||
Real scale = std::numeric_limits<Real>::quiet_NaN();
|
||||
size_t i = starting_level_;
|
||||
do {
|
||||
Real I0 = estimate_integral(f, omega, i);
|
||||
#ifdef BOOST_MATH_INSTRUMENT_OOURA
|
||||
print_ooura_estimate(i, I0, I1, omega);
|
||||
#endif
|
||||
absolute_error_estimate = abs(I0-I1);
|
||||
scale = (max)(abs(I0), abs(I1));
|
||||
if (!isnan(I1) && absolute_error_estimate <= rel_err_goal_*scale) {
|
||||
starting_level_ = (max)(long(i) - 1, long(0));
|
||||
return {I0/omega, absolute_error_estimate/scale};
|
||||
}
|
||||
I1 = I0;
|
||||
} while(++i < big_nodes_.size());
|
||||
|
||||
size_t max_additional_levels = 4;
|
||||
while (big_nodes_.size() < requested_levels_ + max_additional_levels) {
|
||||
size_t ii = big_nodes_.size();
|
||||
if (std::is_same<Real, float>::value) {
|
||||
add_level<double>(ii);
|
||||
}
|
||||
else if (std::is_same<Real, double>::value) {
|
||||
add_level<long double>(ii);
|
||||
}
|
||||
else {
|
||||
add_level<Real>(ii);
|
||||
}
|
||||
Real I0 = estimate_integral(f, omega, ii);
|
||||
#ifdef BOOST_MATH_INSTRUMENT_OOURA
|
||||
print_ooura_estimate(ii, I0, I1, omega);
|
||||
#endif
|
||||
absolute_error_estimate = abs(I0-I1);
|
||||
scale = (max)(abs(I0), abs(I1));
|
||||
if (absolute_error_estimate <= rel_err_goal_*scale) {
|
||||
starting_level_ = (max)(long(ii) - 1, long(0));
|
||||
return {I0/omega, absolute_error_estimate/scale};
|
||||
}
|
||||
I1 = I0;
|
||||
++ii;
|
||||
}
|
||||
|
||||
starting_level_ = static_cast<long>(big_nodes_.size() - 2);
|
||||
return {I1/omega, absolute_error_estimate/scale};
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
template<class PreciseReal>
|
||||
void add_level(size_t i) {
|
||||
using std::abs;
|
||||
size_t current_num_levels = big_nodes_.size();
|
||||
Real unit_roundoff = std::numeric_limits<Real>::epsilon()/2;
|
||||
PreciseReal h = PreciseReal(1)/PreciseReal(1<<i);
|
||||
|
||||
std::vector<Real> bnode_row;
|
||||
std::vector<Real> bweight_row;
|
||||
bnode_row.reserve((static_cast<size_t>(1)<<i)*sizeof(Real));
|
||||
bweight_row.reserve((static_cast<size_t>(1)<<i)*sizeof(Real));
|
||||
|
||||
std::vector<Real> lnode_row;
|
||||
std::vector<Real> lweight_row;
|
||||
|
||||
lnode_row.reserve((static_cast<size_t>(1)<<i)*sizeof(Real));
|
||||
lweight_row.reserve((static_cast<size_t>(1)<<i)*sizeof(Real));
|
||||
|
||||
Real max_weight = 1;
|
||||
auto alpha = calculate_ooura_alpha(h);
|
||||
long n = 0;
|
||||
Real w;
|
||||
do {
|
||||
auto precise_nw = ooura_cos_node_and_weight(n, h, alpha);
|
||||
Real node = static_cast<Real>(precise_nw.first);
|
||||
Real weight = static_cast<Real>(precise_nw.second);
|
||||
w = weight;
|
||||
if (bnode_row.size() == bnode_row.capacity()) {
|
||||
bnode_row.reserve(2*bnode_row.size());
|
||||
bweight_row.reserve(2*bnode_row.size());
|
||||
}
|
||||
|
||||
bnode_row.push_back(node);
|
||||
bweight_row.push_back(weight);
|
||||
if (abs(weight) > max_weight) {
|
||||
max_weight = abs(weight);
|
||||
}
|
||||
++n;
|
||||
// f(t)->0 as t->infty, which is why the weights are computed up to the unit roundoff.
|
||||
} while(abs(w) > unit_roundoff*max_weight);
|
||||
|
||||
bnode_row.shrink_to_fit();
|
||||
bweight_row.shrink_to_fit();
|
||||
n = -1;
|
||||
do {
|
||||
auto precise_nw = ooura_cos_node_and_weight(n, h, alpha);
|
||||
Real node = static_cast<Real>(precise_nw.first);
|
||||
// The function cannot be singular at zero,
|
||||
// so zero is not a unreasonable node,
|
||||
// unlike in the case of the Fourier Sine.
|
||||
// Hence only break if the node is negative.
|
||||
if (node < 0) {
|
||||
break;
|
||||
}
|
||||
Real weight = static_cast<Real>(precise_nw.second);
|
||||
w = weight;
|
||||
if (lnode_row.size() > 0) {
|
||||
if (lnode_row.back() == node) {
|
||||
// The nodes have fused into each other:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (lnode_row.size() == lnode_row.capacity()) {
|
||||
lnode_row.reserve(2*lnode_row.size());
|
||||
lweight_row.reserve(2*lnode_row.size());
|
||||
}
|
||||
|
||||
lnode_row.push_back(node);
|
||||
lweight_row.push_back(weight);
|
||||
if (abs(weight) > max_weight) {
|
||||
max_weight = abs(weight);
|
||||
}
|
||||
--n;
|
||||
} while(abs(w) > (std::numeric_limits<Real>::min)()*max_weight);
|
||||
|
||||
lnode_row.shrink_to_fit();
|
||||
lweight_row.shrink_to_fit();
|
||||
|
||||
std::lock_guard<std::mutex> lock(node_weight_mutex_);
|
||||
// Another thread might have already finished this calculation and appended it to the nodes/weights:
|
||||
if (current_num_levels == big_nodes_.size()) {
|
||||
big_nodes_.push_back(bnode_row);
|
||||
bweights_.push_back(bweight_row);
|
||||
|
||||
little_nodes_.push_back(lnode_row);
|
||||
lweights_.push_back(lweight_row);
|
||||
}
|
||||
}
|
||||
|
||||
template<class F>
|
||||
Real estimate_integral(F const & f, Real omega, size_t i) {
|
||||
Real I0 = 0;
|
||||
auto const & b_nodes = big_nodes_[i];
|
||||
auto const & b_weights = bweights_[i];
|
||||
Real inv_omega = 1/omega;
|
||||
for(size_t j = 0 ; j < b_nodes.size(); ++j) {
|
||||
I0 += f(b_nodes[j]*inv_omega)*b_weights[j];
|
||||
}
|
||||
|
||||
auto const & l_nodes = little_nodes_[i];
|
||||
auto const & l_weights = lweights_[i];
|
||||
for (size_t j = 0; j < l_nodes.size(); ++j) {
|
||||
I0 += f(l_nodes[j]*inv_omega)*l_weights[j];
|
||||
}
|
||||
return I0;
|
||||
}
|
||||
|
||||
std::mutex node_weight_mutex_;
|
||||
std::vector<std::vector<Real>> big_nodes_;
|
||||
std::vector<std::vector<Real>> bweights_;
|
||||
|
||||
std::vector<std::vector<Real>> little_nodes_;
|
||||
std::vector<std::vector<Real>> lweights_;
|
||||
Real rel_err_goal_;
|
||||
std::atomic<long> starting_level_;
|
||||
size_t requested_levels_;
|
||||
};
|
||||
|
||||
|
||||
}}}}
|
||||
#endif
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,228 @@
|
||||
// Copyright Nick Thompson, 2017
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_MATH_QUADRATURE_DETAIL_TANH_SINH_CONSTANTS_HPP
|
||||
#define BOOST_MATH_QUADRATURE_DETAIL_TANH_SINH_CONSTANTS_HPP
|
||||
|
||||
#include <boost/type_traits/is_constructible.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace math {
|
||||
namespace quadrature {
|
||||
namespace detail {
|
||||
|
||||
template <class Real>
|
||||
inline Real abscissa_at_t(const Real& t)
|
||||
{
|
||||
using std::tanh;
|
||||
using std::sinh;
|
||||
return tanh(constants::half_pi<Real>()*sinh(t));
|
||||
}
|
||||
template <class Real>
|
||||
inline Real weight_at_t(const Real& t)
|
||||
{
|
||||
using std::cosh;
|
||||
using std::sinh;
|
||||
Real cs = cosh(constants::half_pi<Real>() * sinh(t));
|
||||
return constants::half_pi<Real>() * cosh(t) / (cs * cs);
|
||||
}
|
||||
template <class Real>
|
||||
inline Real abscissa_complement_at_t(const Real& t)
|
||||
{
|
||||
using std::cosh;
|
||||
using std::exp;
|
||||
using std::sinh;
|
||||
Real u2 = constants::half_pi<Real>() * sinh(t);
|
||||
return 1 / (exp(u2) *cosh(u2));
|
||||
}
|
||||
template <class Real>
|
||||
inline Real t_from_abscissa_complement(const Real& x)
|
||||
{
|
||||
using std::log;
|
||||
using std::sqrt;
|
||||
Real l = log(sqrt((2 - x) / x));
|
||||
return log((sqrt(4 * l * l + constants::pi<Real>() * constants::pi<Real>()) + 2 * l) / constants::pi<Real>());
|
||||
};
|
||||
|
||||
template<class Real, int precision>
|
||||
class tanh_sinh_detail_constants
|
||||
{
|
||||
static const int initializer_selector =
|
||||
!std::numeric_limits<Real>::is_specialized || (std::numeric_limits<Real>::radix != 2) ?
|
||||
0 :
|
||||
(std::numeric_limits<Real>::digits < 30) && (std::numeric_limits<Real>::max_exponent <= 128) ?
|
||||
1 : 0;
|
||||
public:
|
||||
tanh_sinh_detail_constants(std::size_t max_refinements, std::size_t initial_commit = 4) : m_max_refinements(max_refinements), m_committed_refinements(initial_commit)
|
||||
{
|
||||
typedef boost::integral_constant<int, initializer_selector> tag_type;
|
||||
init(tag_type());
|
||||
}
|
||||
|
||||
void init(const boost::integral_constant<int, 0>&)
|
||||
{
|
||||
using std::tanh;
|
||||
using std::sinh;
|
||||
using std::asinh;
|
||||
using std::atanh;
|
||||
using boost::math::constants::half_pi;
|
||||
using boost::math::constants::pi;
|
||||
using boost::math::constants::two_div_pi;
|
||||
|
||||
m_inital_row_length = 7;
|
||||
std::size_t first_complement = 0;
|
||||
m_t_max = m_inital_row_length;
|
||||
m_t_crossover = t_from_abscissa_complement(Real(0.5f));
|
||||
|
||||
m_abscissas.assign(m_max_refinements + 1, std::vector<Real>());
|
||||
m_weights.assign(m_max_refinements + 1, std::vector<Real>());
|
||||
m_first_complements.assign(m_max_refinements + 1, 0);
|
||||
//
|
||||
// First row is special:
|
||||
//
|
||||
Real h = m_t_max / m_inital_row_length;
|
||||
|
||||
std::vector<Real> temp(m_inital_row_length + 1, Real(0));
|
||||
for (std::size_t i = 0; i < m_inital_row_length; ++i)
|
||||
{
|
||||
Real t = h * i;
|
||||
if ((first_complement < 0) && (t < m_t_crossover))
|
||||
first_complement = i;
|
||||
temp[i] = t < m_t_crossover ? abscissa_at_t(t) : -abscissa_complement_at_t(t);
|
||||
}
|
||||
temp[m_inital_row_length] = abscissa_complement_at_t(m_t_max);
|
||||
m_abscissas[0].swap(temp);
|
||||
m_first_complements[0] = first_complement;
|
||||
temp.assign(m_inital_row_length + 1, Real(0));
|
||||
for (std::size_t i = 0; i < m_inital_row_length; ++i)
|
||||
temp[i] = weight_at_t(Real(h * i));
|
||||
temp[m_inital_row_length] = weight_at_t(m_t_max);
|
||||
m_weights[0].swap(temp);
|
||||
|
||||
for (std::size_t row = 1; row <= m_committed_refinements; ++row)
|
||||
{
|
||||
h /= 2;
|
||||
first_complement = 0;
|
||||
|
||||
for (Real pos = h; pos < m_t_max; pos += 2 * h)
|
||||
{
|
||||
if (pos < m_t_crossover)
|
||||
++first_complement;
|
||||
temp.push_back(pos < m_t_crossover ? abscissa_at_t(pos) : -abscissa_complement_at_t(pos));
|
||||
}
|
||||
m_abscissas[row].swap(temp);
|
||||
m_first_complements[row] = first_complement;
|
||||
for (Real pos = h; pos < m_t_max; pos += 2 * h)
|
||||
temp.push_back(weight_at_t(pos));
|
||||
m_weights[row].swap(temp);
|
||||
}
|
||||
}
|
||||
void init(const boost::integral_constant<int, 1>&)
|
||||
{
|
||||
m_inital_row_length = 4;
|
||||
m_abscissas.reserve(m_max_refinements + 1);
|
||||
m_weights.reserve(m_max_refinements + 1);
|
||||
m_first_complements.reserve(m_max_refinements + 1);
|
||||
m_abscissas = {
|
||||
{ 0.0f, -0.04863203593f, -2.252280754e-05f, -4.294161056e-14f, -1.167648898e-37f, },
|
||||
{ -0.3257285078f, -0.002485143543f, -1.112433512e-08f, -5.378491591e-23f, },
|
||||
{ 0.3772097382f, -0.1404309413f, -0.01295943949f, -0.0003117359716f, -7.952628853e-07f, -4.714355182e-11f, -5.415222824e-18f, -2.040300394e-29f, },
|
||||
{ 0.1943570033f, -0.4608532946f, -0.219392561f, -0.08512073674f, -0.0260331318f, -0.005944493369f, -0.0009348035442f, -9.061530486e-05f, -4.683958779e-06f, -1.072183876e-07f, -8.572949078e-10f, -1.767834693e-12f, -6.374878495e-16f, -2.442937279e-20f, -5.251546473e-26f, -2.789467162e-33f, },
|
||||
{ 0.09792388529f, 0.2878799327f, 0.4612535439f, -0.3897263425f, -0.2689819652f, -0.1766829945f, -0.1101085972f, -0.06483914248f, -0.03588783578f, -0.01854517332f, -0.008873007558f, -0.003891334562f, -0.001545791232f, -0.0005485655647f, -0.0001711779271f, -4.612899437e-05f, -1.051798518e-05f, -1.982859405e-06f, -3.011058474e-07f, -3.576091908e-08f, -3.212800902e-09f, -2.102671378e-10f, -9.606066479e-12f, -2.919026641e-13f, -5.586116639e-15f, -6.328207135e-17f, -3.956461339e-19f, -1.260975845e-21f, -1.872492175e-24f, -1.170030294e-27f, -2.740986178e-31f, -2.112282721e-35f, },
|
||||
{ 0.04905596731f, 0.1464179843f, 0.2415663195f, 0.3331422646f, 0.4199521113f, -0.4989866106f, -0.4244155094f, -0.356823241f, -0.2964499949f, -0.2433060914f, -0.1972012587f, -0.1577807536f, -0.1245646024f, -0.09698671849f, -0.07443136593f, -0.05626521395f, -0.04186397729f, -0.0306332671f, -0.02202376481f, -0.01554116883f, -0.01075156891f, -0.007283002803f, -0.004823973845f, -0.003119681872f, -0.001966663685f, -0.001206465701f, -0.0007188880782f, -0.0004152496485f, -0.0002320284004f, -0.0001251349512f, -6.498007492e-05f, -3.240693206e-05f, -1.548009773e-05f, -7.062123337e-06f, -3.06755081e-06f, -1.264528134e-06f, -4.929942806e-07f, -1.811062872e-07f, -6.244592163e-08f, -2.01254968e-08f, -6.035865798e-09f, -1.676638052e-09f, -4.292122274e-10f, -1.007222767e-10f, -2.154466259e-11f, -4.175393124e-12f, -7.284737264e-13f, -1.136386985e-13f, -1.57355351e-14f, -1.91921891e-15f, -2.044959541e-16f, -1.886958307e-17f, -1.493871649e-18f, -1.00469381e-19f, -5.679929178e-21f, -2.669103953e-22f, -1.030178138e-23f, -3.224484876e-25f, -8.0747829e-27f, -1.594654602e-28f, -2.445723975e-30f, -2.865919772e-32f, -2.521682525e-34f, -1.63551444e-36f, },
|
||||
{ 0.02453976357f, 0.07352512299f, 0.1222291222f, 0.1704679724f, 0.2180634735f, 0.2648450766f, 0.3106517806f, 0.3553338252f, 0.3987541505f, 0.440789599f, 0.4813318461f, -0.4797119493f, -0.4424187717f, -0.4068496464f, -0.3730497919f, -0.3410490083f, -0.3108622749f, -0.2824905325f, -0.2559216165f, -0.2311313132f, -0.2080845076f, -0.1867363915f, -0.1670337061f, -0.148915992f, -0.1323168242f, -0.1171650118f, -0.1033857457f, -0.09090168184f, -0.07963394697f, -0.069503062f, -0.06042977607f, -0.05233580938f, -0.04514450419f, -0.03878138485f, -0.03317462969f, -0.02825545843f, -0.02395843974f, -0.0202217242f, -0.01698720852f, -0.01420063697f, -0.0118116462f, -0.009773759532f, -0.008044336997f, -0.006584486831f, -0.005358944287f, -0.004335923183f, -0.00348694536f, -0.002786652957f, -0.002212608041f, -0.001745083828f, -0.001366851359f, -0.001062965166f, -0.0008205510651f, -0.0006285988591f, -0.0004777623488f, -0.0003601686544f, -0.0002692384802f, -0.0001995185689f, -0.0001465272269f, -0.0001066134524f, -7.682987071e-05f, -5.481938554e-05f, -3.871519214e-05f, -2.705357477e-05f, -1.869872988e-05f, -1.2778718e-05f, -8.631551655e-06f, -5.760372383e-06f, -3.796652834e-06f, -2.470376195e-06f, -1.586189035e-06f, -1.00458931e-06f, -6.272926646e-07f, -3.860114498e-07f, -2.339766676e-07f, -1.396287854e-07f, -8.199520529e-08f, -4.735733554e-08f, -2.688676406e-08f, -1.499692369e-08f, -8.213543901e-09f, -4.414366384e-09f, -2.326763262e-09f, -1.2020165e-09f, -6.082231242e-10f, -3.012456307e-10f, -1.459438845e-10f, -6.911160499e-11f, -3.196678326e-11f, -1.443120992e-11f, -6.353676128e-12f, -2.725950519e-12f, -1.138734572e-12f, -4.627734622e-13f, -1.827990225e-13f, -7.012046738e-14f, -2.609605366e-14f, -9.413361335e-15f, -3.287925695e-15f, -1.11086294e-15f, -3.626602264e-16f, -1.142787653e-16f, -3.471902269e-17f, -1.015781907e-17f, -2.858532825e-18f, -7.727834787e-19f, -2.004423992e-19f, -4.981568376e-20f, -1.184668492e-20f, -2.691984904e-21f, -5.836667442e-22f, -1.205661589e-22f, -2.369109351e-23f, -4.421339462e-24f, -7.823835004e-25f, -1.310533144e-25f, -2.074345013e-26f, -3.096968326e-27f, -4.353200528e-28f, -5.749955668e-29f, -7.122715927e-30f, -8.25783542e-31f, -8.941541007e-32f, -9.02279665e-33f, -8.46603012e-34f, -7.369272807e-35f, -5.936632356e-36f, -4.415277839e-37f, },
|
||||
{ 0.01227135512f, 0.03680228095f, 0.06129788941f, 0.08573475488f, 0.1100896299f, 0.1343395153f, 0.1584617283f, 0.1824339697f, 0.2062343883f, 0.2298416433f, 0.2532349634f, 0.2763942036f, 0.2992998981f, 0.3219333097f, 0.3442764756f, 0.3663122492f, 0.3880243378f, 0.4093973357f, 0.4304167537f, 0.4510690435f, 0.4713416183f, 0.4912228687f, -0.489297826f, -0.4702300899f, -0.4515825479f, -0.4333628262f, -0.4155775577f, -0.39823239f, -0.3813319982f, -0.3648801001f, -0.3488794756f, -0.3333319877f, -0.318238608f, -0.3035994429f, -0.2894137636f, -0.275680037f, -0.2623959593f, -0.2495584902f, -0.2371638893f, -0.2252077531f, -0.2136850525f, -0.2025901721f, -0.1919169483f, -0.1816587092f, -0.1718083133f, -0.1623581887f, -0.1533003716f, -0.1446265448f, -0.1363280753f, -0.1283960505f, -0.120821315f, -0.113594505f, -0.1067060822f, -0.1001463668f, -0.09390556883f, -0.08797381831f, -0.08234119416f, -0.07699775172f, -0.07193354881f, -0.06713867034f, -0.0626032516f, -0.0583175f, -0.0542717154f, -0.050456309f, -0.0468618209f, -0.0434789361f, -0.0402984993f, -0.03731152826f, -0.0345092259f, -0.03188299107f, -0.02942442821f, -0.02712535566f, -0.02497781299f, -0.02297406711f, -0.02110661737f, -0.01936819969f, -0.01775178968f, -0.01625060483f, -0.01485810598f, -0.01356799776f, -0.01237422849f, -0.01127098916f, -0.01025271192f, -0.009314067826f, -0.008449964034f, -0.007655540506f, -0.006926166179f, -0.006257434709f, -0.005645159804f, -0.005085370197f, -0.004574304294f, -0.004108404533f, -0.003684311494f, -0.003298857801f, -0.002949061834f, -0.002632121306f, -0.002345406714f, -0.002086454715f, -0.001852961444f, -0.001642775797f, -0.001453892723f, -0.001284446528f, -0.001132704236f, -0.0009970590063f, -0.0008760236476f, -0.0007682242321f, -0.0006723938372f, -0.000587366425f, -0.0005120708762f, -0.0004455251889f, -0.0003868308567f, -0.0003351674323f, -0.0002897872882f, -0.0002500105784f, -0.0002152204083f, -0.0001848582177f, -0.0001584193765f, -0.0001354489984f, -0.0001155379702f, -9.8319198e-05f, -8.346406605e-05f, -7.067910812e-05f, -5.970288552e-05f, -5.030306831e-05f, -4.227371392e-05f, -3.543273737e-05f, -2.961956641e-05f, -2.469297451e-05f, -2.052908425e-05f, -1.701953324e-05f, -1.406979453e-05f, -1.159764317e-05f, -9.531760786e-06f, -7.810469566e-06f, -6.380587461e-06f, -5.196396351e-06f, -4.218715072e-06f, -3.414069429e-06f, -2.753951574e-06f, -2.214161365e-06f, -1.774222689e-06f, -1.416868016e-06f, -1.127584838e-06f, -8.942180042e-07f, -7.066223315e-07f, -5.563602711e-07f, -4.364397681e-07f, -3.410878407e-07f, -2.65555767e-07f, -2.059521239e-07f, -1.591002641e-07f, -1.224171449e-07f, -9.381073151e-08f, -7.159348586e-08f, -5.440972705e-08f, -4.117489851e-08f, -3.102500976e-08f, -2.327473287e-08f, -1.738282654e-08f, -1.292373523e-08f, -9.564367214e-09f, -7.045195508e-09f, -5.164949276e-09f, -3.768272997e-09f, -2.735826254e-09f, -1.976380568e-09f, -1.420541964e-09f, -1.015790099e-09f, -7.225780068e-10f, -5.112816947e-10f, -3.598270551e-10f, -2.518536224e-10f, -1.753014775e-10f, -1.213298105e-10f, -8.349395075e-11f, -5.712266027e-11f, -3.8849686e-11f, -2.626342738e-11f, -1.764649978e-11f, -1.178329832e-11f, -7.818680628e-12f, -5.154836404e-12f, -3.376501461e-12f, -2.19707447e-12f, -1.420047367e-12f, -9.115800793e-13f, -5.811306251e-13f, -3.67867911e-13f, -2.312068904e-13f, -1.442617249e-13f, -8.934966665e-14f, -5.492567772e-14f, -3.35078831e-14f, -2.02840764e-14f, -1.218279513e-14f, -7.258853736e-15f, -4.290062787e-15f, -2.514652539e-15f, -1.461687113e-15f, -8.424330958e-16f, -4.81351759e-16f, -2.726317943e-16f, -1.530442297e-16f, -8.513785725e-17f, -4.692795039e-17f, -2.562597595e-17f, -1.386133467e-17f, -7.425750192e-18f, -3.939309402e-18f, -2.069079146e-18f, -1.075828622e-18f, -5.536671017e-19f, -2.819834004e-19f, -1.421006375e-19f, -7.084243878e-20f, -3.493350557e-20f, -1.703597751e-20f, -8.214706044e-21f, -3.915973438e-21f, -1.845149816e-21f, -8.591874581e-22f, -3.953006958e-22f, -1.7966698e-22f, -8.065408821e-23f, -3.575337212e-23f, -1.564782132e-23f, -6.760041764e-24f, -2.882136323e-24f, -1.212436233e-24f, -5.031421831e-25f, -2.059286312e-25f, -8.310787798e-26f, -3.306518068e-26f, -1.296597346e-26f, -5.010091032e-27f, -1.907179385e-27f, -7.150567334e-28f, -2.639906037e-28f, -9.594664542e-29f, -3.432078099e-29f, -1.207985066e-29f, -4.182464721e-30f, -1.424153707e-30f, -4.767833382e-31f, -1.568949178e-31f, -5.073438855e-32f, -1.611691916e-32f, -5.028356694e-33f, -1.540320437e-33f, -4.631392443e-34f, -1.366470225e-34f, -3.955008527e-35f, -1.122591825e-35f, -3.123848743e-36f, -8.519540247e-37f, -2.276473481e-37f, },
|
||||
};
|
||||
m_weights = {
|
||||
{ 1.570796327f, 0.2300223945f, 0.0002662005138f, 1.358178427e-12f, 1.001741678e-35f, },
|
||||
{ 0.9659765794f, 0.01834316699f, 2.143120456e-07f, 2.800315102e-21f, },
|
||||
{ 1.389614759f, 0.5310782754f, 0.07638574357f, 0.002902517748f, 1.198370136e-05f, 1.163116581e-09f, 2.197079236e-16f, 1.363510331e-27f, },
|
||||
{ 1.523283719f, 1.193463026f, 0.7374378484f, 0.3604614185f, 0.1374221077f, 0.03917500549f, 0.007742601026f, 0.0009499468043f, 6.248255924e-05f, 1.826332059e-06f, 1.868728227e-08f, 4.937853878e-11f, 2.28349267e-14f, 1.122753143e-18f, 3.09765397e-24f, 2.112123344e-31f, },
|
||||
{ 1.558773356f, 1.466014427f, 1.29747575f, 1.081634985f, 0.8501728565f, 0.6304051352f, 0.4408332363f, 0.2902406793f, 0.1793244121f, 0.1034321542f, 0.05528968374f, 0.02713351001f, 0.0120835436f, 0.004816298144f, 0.001690873998f, 0.0005133938241f, 0.0001320523413f, 2.811016433e-05f, 4.823718203e-06f, 6.477756604e-07f, 6.583518513e-08f, 4.876006097e-09f, 2.521634792e-10f, 8.675931415e-12f, 1.880207173e-13f, 2.412423038e-15f, 1.708453277e-17f, 6.168256849e-20f, 1.037679724e-22f, 7.345984103e-26f, 1.949783362e-29f, 1.702438776e-33f, },
|
||||
{ 1.567781431f, 1.543881116f, 1.497226223f, 1.430008355f, 1.345278885f, 1.246701207f, 1.138272243f, 1.024044933f, 0.9078793792f, 0.7932427008f, 0.6830685163f, 0.5796781031f, 0.4847580912f, 0.3993847415f, 0.3240825396f, 0.2589046395f, 0.2035239989f, 0.1573262035f, 0.1194974113f, 0.08910313924f, 0.06515553343f, 0.04666820805f, 0.03269873273f, 0.02237947106f, 0.0149378351f, 0.009707223739f, 0.006130037632f, 0.003754250977f, 0.002225082706f, 0.001273327945f, 0.0007018595157f, 0.0003716669362f, 0.0001885644298f, 9.139081749e-05f, 4.218318384e-05f, 1.84818136e-05f, 7.659575853e-06f, 2.991661588e-06f, 1.096883513e-06f, 3.759541186e-07f, 1.199244278e-07f, 3.543477717e-08f, 9.649888896e-09f, 2.409177326e-09f, 5.48283578e-10f, 1.130605535e-10f, 2.09893354e-11f, 3.484193767e-12f, 5.134127525e-13f, 6.663992283e-14f, 7.556721776e-15f, 7.420993231e-16f, 6.252804845e-17f, 4.475759507e-18f, 2.693120661e-19f, 1.346994157e-20f, 5.533583499e-22f, 1.843546975e-23f, 4.913936871e-25f, 1.032939131e-26f, 1.686277004e-28f, 2.103305749e-30f, 1.96992098e-32f, 1.359989462e-34f, },
|
||||
{ 1.570042029f, 1.564021404f, 1.55205317f, 1.534281738f, 1.510919723f, 1.482243298f, 1.448586255f, 1.410332971f, 1.367910512f, 1.321780117f, 1.272428346f, 1.22035811f, 1.16607987f, 1.110103194f, 1.05292888f, 0.995041804f, 0.9369046127f, 0.8789523456f, 0.8215880353f, 0.7651792989f, 0.7100559012f, 0.6565082461f, 0.6047867306f, 0.555101878f, 0.5076251588f, 0.4624903981f, 0.4197956684f, 0.3796055694f, 0.3419537959f, 0.3068459094f, 0.2742622297f, 0.2441607779f, 0.2164802091f, 0.1911426841f, 0.1680566379f, 0.1471194133f, 0.1282197336f, 0.111239999f, 0.09605839187f, 0.08255078811f, 0.07059246991f, 0.06005964236f, 0.05083075757f, 0.04278765216f, 0.0358165056f, 0.02980862812f, 0.02466108731f, 0.02027718382f, 0.01656678625f, 0.01344653661f, 0.01083993717f, 0.00867733075f, 0.006895785969f, 0.005438899798f, 0.004256529599f, 0.003304466994f, 0.002544065768f, 0.001941835776f, 0.00146901436f, 0.001101126113f, 0.0008175410133f, 0.0006010398799f, 0.0004373949562f, 0.0003149720919f, 0.0002243596521f, 0.000158027884f, 0.0001100211285f, 7.568399659e-05f, 5.142149745e-05f, 3.449212476e-05f, 2.283211811e-05f, 1.490851403e-05f, 9.598194128e-06f, 6.089910032e-06f, 3.806198326e-06f, 2.342166721e-06f, 1.418306716e-06f, 8.447375638e-07f, 4.94582887e-07f, 2.844992366e-07f, 1.606939458e-07f, 8.907139514e-08f, 4.84209502e-08f, 2.579956823e-08f, 1.346464552e-08f, 6.878461096e-09f, 3.437185674e-09f, 1.678889768e-09f, 8.009978448e-10f, 3.729950184e-10f, 1.693945779e-10f, 7.496739757e-11f, 3.230446433e-11f, 1.354251291e-11f, 5.518236947e-12f, 2.18359221e-12f, 8.383128961e-13f, 3.119497729e-13f, 1.124020896e-13f, 3.917679451e-14f, 1.319434223e-14f, 4.289196222e-15f, 1.344322288e-15f, 4.057557702e-16f, 1.177981213e-16f, 3.285386163e-17f, 8.791316559e-18f, 2.25407483e-18f, 5.530176913e-19f, 1.296452714e-19f, 2.899964556e-20f, 6.180143249e-21f, 1.252867643e-21f, 2.412250547e-22f, 4.4039067e-23f, 7.610577808e-24f, 1.242805165e-24f, 1.91431069e-25f, 2.776125103e-26f, 3.783124073e-27f, 4.834910155e-28f, 5.783178697e-29f, 6.460575703e-30f, 6.72603739e-31f, 6.511153451e-32f, 5.847409075e-33f, 4.860046055e-34f, 3.72923953e-35f, },
|
||||
{ 1.570607717f, 1.569099695f, 1.566088239f, 1.561582493f, 1.555596115f, 1.548147191f, 1.539258145f, 1.528955608f, 1.517270275f, 1.504236738f, 1.489893298f, 1.474281762f, 1.457447221f, 1.439437815f, 1.420304486f, 1.400100716f, 1.378882264f, 1.35670689f, 1.333634075f, 1.309724744f, 1.285040985f, 1.259645765f, 1.233602657f, 1.206975567f, 1.179828472f, 1.152225159f, 1.124228984f, 1.09590263f, 1.067307886f, 1.038505436f, 1.00955466f, 0.9805134517f, 0.951438051f, 0.9223828892f, 0.8934004523f, 0.8645411596f, 0.8358532563f, 0.807382723f, 0.7791731997f, 0.7512659245f, 0.723699687f, 0.6965107951f, 0.6697330554f, 0.6433977657f, 0.6175337199f, 0.5921672237f, 0.5673221206f, 0.5430198278f, 0.5192793805f, 0.4961174844f, 0.4735485755f, 0.4515848861f, 0.4302365164f, 0.4095115109f, 0.3894159397f, 0.3699539819f, 0.3511280132f, 0.3329386948f, 0.3153850641f, 0.2984646265f, 0.2821734476f, 0.2665062456f, 0.2514564831f, 0.2370164583f, 0.2231773949f, 0.2099295305f, 0.1972622032f, 0.1851639366f, 0.1736225217f, 0.1626250975f, 0.1521582278f, 0.1422079761f, 0.1327599774f, 0.1237995069f, 0.1153115463f, 0.1072808458f, 0.09969198461f, 0.09252942711f, 0.08577757654f, 0.0794208254f, 0.07344360286f, 0.06783041903f, 0.06256590638f, 0.05763485811f, 0.05302226366f, 0.04871334138f, 0.04469356846f, 0.04094870813f, 0.0374648342f, 0.03422835312f, 0.03122602351f, 0.02844497325f, 0.02587271434f, 0.02349715546f, 0.02130661237f, 0.01928981624f, 0.01743592007f, 0.01573450311f, 0.01417557353f, 0.01274956936f, 0.01144735783f, 0.01026023317f, 0.009179912924f, 0.008198533005f, 0.007308641451f, 0.006503191044f, 0.005775530877f, 0.005119396961f, 0.004528901979f, 0.003998524263f, 0.00352309611f, 0.003097791523f, 0.002718113458f, 0.002379880688f, 0.002079214354f, 0.001812524299f, 0.001576495262f, 0.00136807301f, 0.001184450486f, 0.001023054043f, 0.0008815298242f, 0.0007577303578f, 0.0006497014187f, 0.0005556692074f, 0.000474027894f, 0.0004033275645f, 0.0003422626065f, 0.0002896605611f, 0.0002444714673f, 0.0002057577147f, 0.0001726844199f, 0.0001445103343f, 0.0001205792873f, 0.0001003121646f, 8.319941724e-05f, 6.879409311e-05f, 5.670537985e-05f, 4.659264463e-05f, 3.815995412e-05f, 3.115105568e-05f, 2.534479897e-05f, 2.055097594e-05f, 1.660655598e-05f, 1.337229228e-05f, 1.072967541e-05f, 8.578209354e-06f, 6.832986277e-06f, 5.422535892e-06f, 4.286926494e-06f, 3.376095235e-06f, 2.648386225e-06f, 2.069276126e-06f, 1.610268009e-06f, 1.247935512e-06f, 9.631005212e-07f, 7.401289349e-07f, 5.66330284e-07f, 4.314482559e-07f, 3.272303733e-07f, 2.470662451e-07f, 1.856849137e-07f, 1.3890287e-07f, 1.034152804e-07f, 7.662387397e-08f, 5.649576387e-08f, 4.144823356e-08f, 3.025519646e-08f, 2.197164892e-08f, 1.587297809e-08f, 1.140646555e-08f, 8.152746483e-09f, 5.795349573e-09f, 4.096757914e-09f, 2.879701346e-09f, 2.012621022e-09f, 1.398441431e-09f, 9.659485186e-10f, 6.632086347e-10f, 4.52575761e-10f, 3.069270208e-10f, 2.068420354e-10f, 1.385028753e-10f, 9.214056423e-11f, 6.089338706e-11f, 3.997338952e-11f, 2.60619605e-11f, 1.687451934e-11f, 1.084916183e-11f, 6.925528015e-12f, 4.38886519e-12f, 2.760858767e-12f, 1.723764404e-12f, 1.068075044e-12f, 6.56694435e-13f, 4.00598538e-13f, 2.424296605e-13f, 1.455249916e-13f, 8.663812725e-14f, 5.114974901e-14f, 2.99421776e-14f, 1.737681695e-14f, 9.99642401e-15f, 5.699626666e-15f, 3.220432513e-15f, 1.802958964e-15f, 9.999957344e-16f, 5.493978397e-16f, 2.989420886e-16f, 1.610765424e-16f, 8.593209748e-17f, 4.538246827e-17f, 2.372253167e-17f, 1.227167167e-17f, 6.281229049e-18f, 3.180614714e-18f, 1.593049257e-18f, 7.890855159e-19f, 3.864733103e-19f, 1.87127733e-19f, 8.955739455e-20f, 4.235742852e-20f, 1.979436202e-20f, 9.138078558e-21f, 4.166641158e-21f, 1.876075055e-21f, 8.339901949e-22f, 3.659575236e-22f, 1.584785218e-22f, 6.771575694e-23f, 2.854281708e-23f, 1.186583858e-23f, 4.864069936e-24f, 1.965643419e-24f, 7.829165625e-25f, 3.072789229e-25f, 1.188107615e-25f, 4.524619749e-26f, 1.696710187e-26f, 6.263641003e-27f, 2.275790793e-27f, 8.136077716e-28f, 2.861306549e-28f, 9.896184197e-29f, 3.365200893e-29f, 1.124807055e-29f, 3.694460433e-30f, 1.192093301e-30f, 3.777757876e-31f, 1.175436379e-31f, 3.589879078e-32f, 1.075842686e-32f, 3.162835126e-33f, 9.118674189e-34f, 2.577393168e-34f, 7.139829504e-35f, 1.937828921e-35f, },
|
||||
};
|
||||
m_first_complements = {
|
||||
1, 0, 1, 1, 3, 5, 11, 22,
|
||||
};
|
||||
|
||||
while (m_abscissas.size() <= m_committed_refinements)
|
||||
extend_refinements();
|
||||
m_committed_refinements = m_abscissas.size() - 1;
|
||||
|
||||
if (m_max_refinements >= m_abscissas.size())
|
||||
{
|
||||
m_abscissas.resize(m_max_refinements + 1);
|
||||
m_weights.resize(m_max_refinements + 1);
|
||||
m_first_complements.resize(m_max_refinements + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_max_refinements = m_abscissas.size() - 1;
|
||||
}
|
||||
m_t_max = m_inital_row_length;
|
||||
m_t_crossover = t_from_abscissa_complement(Real(0.5f));
|
||||
}
|
||||
|
||||
void extend_refinements()const
|
||||
{
|
||||
++m_committed_refinements;
|
||||
std::size_t row = m_committed_refinements;
|
||||
Real h = ldexp(Real(1), -static_cast<int>(row));
|
||||
std::size_t first_complement = 0;
|
||||
for (Real pos = h; pos < m_t_max; pos += 2 * h)
|
||||
{
|
||||
if (pos < m_t_crossover)
|
||||
++first_complement;
|
||||
m_abscissas[row].push_back(pos < m_t_crossover ? abscissa_at_t(pos) : -abscissa_complement_at_t(pos));
|
||||
}
|
||||
m_first_complements[row] = first_complement;
|
||||
for (Real pos = h; pos < m_t_max; pos += 2 * h)
|
||||
m_weights[row].push_back(weight_at_t(pos));
|
||||
}
|
||||
|
||||
const std::vector<Real>& get_abscissa_row(std::size_t n)const
|
||||
{
|
||||
if (m_committed_refinements < n)
|
||||
extend_refinements();
|
||||
BOOST_ASSERT(m_committed_refinements >= n);
|
||||
return m_abscissas[n];
|
||||
}
|
||||
const std::vector<Real>& get_weight_row(std::size_t n)const
|
||||
{
|
||||
if (m_committed_refinements < n)
|
||||
extend_refinements();
|
||||
BOOST_ASSERT(m_committed_refinements >= n);
|
||||
return m_weights[n];
|
||||
}
|
||||
std::size_t get_first_complement_index(std::size_t n)const
|
||||
{
|
||||
if (m_committed_refinements < n)
|
||||
extend_refinements();
|
||||
BOOST_ASSERT(m_committed_refinements >= n);
|
||||
return m_first_complements[n];
|
||||
}
|
||||
|
||||
protected:
|
||||
mutable std::vector<std::vector<Real>> m_abscissas;
|
||||
mutable std::vector<std::vector<Real>> m_weights;
|
||||
mutable std::vector<std::size_t> m_first_complements;
|
||||
std::size_t m_max_refinements, m_inital_row_length;
|
||||
mutable std::size_t m_committed_refinements;
|
||||
Real m_t_max, m_t_crossover;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,94 @@
|
||||
// Copyright Nick Thompson, 2017
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
/*
|
||||
* This class performs exp-sinh quadrature on half infinite intervals.
|
||||
*
|
||||
* References:
|
||||
*
|
||||
* 1) Tanaka, Ken'ichiro, et al. "Function classes for double exponential integration formulas." Numerische Mathematik 111.4 (2009): 631-655.
|
||||
*/
|
||||
|
||||
#ifndef BOOST_MATH_QUADRATURE_EXP_SINH_HPP
|
||||
#define BOOST_MATH_QUADRATURE_EXP_SINH_HPP
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <boost/math/quadrature/detail/exp_sinh_detail.hpp>
|
||||
|
||||
namespace boost{ namespace math{ namespace quadrature {
|
||||
|
||||
template<class Real, class Policy = policies::policy<> >
|
||||
class exp_sinh
|
||||
{
|
||||
public:
|
||||
exp_sinh(size_t max_refinements = 9)
|
||||
: m_imp(std::make_shared<detail::exp_sinh_detail<Real, Policy>>(max_refinements)) {}
|
||||
|
||||
template<class F>
|
||||
auto integrate(const F& f, Real a, Real b, Real tol = boost::math::tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr)->decltype(std::declval<F>()(std::declval<Real>())) const;
|
||||
template<class F>
|
||||
auto integrate(const F& f, Real tol = boost::math::tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr)->decltype(std::declval<F>()(std::declval<Real>())) const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<detail::exp_sinh_detail<Real, Policy>> m_imp;
|
||||
};
|
||||
|
||||
template<class Real, class Policy>
|
||||
template<class F>
|
||||
auto exp_sinh<Real, Policy>::integrate(const F& f, Real a, Real b, Real tolerance, Real* error, Real* L1, std::size_t* levels)->decltype(std::declval<F>()(std::declval<Real>())) const
|
||||
{
|
||||
typedef decltype(f(a)) K;
|
||||
using std::abs;
|
||||
using boost::math::constants::half;
|
||||
using boost::math::quadrature::detail::exp_sinh_detail;
|
||||
|
||||
static const char* function = "boost::math::quadrature::exp_sinh<%1%>::integrate";
|
||||
|
||||
// Neither limit may be a NaN:
|
||||
if((boost::math::isnan)(a) || (boost::math::isnan)(b))
|
||||
{
|
||||
return static_cast<K>(policies::raise_domain_error(function, "NaN supplied as one limit of integration - sorry I don't know what to do", a, Policy()));
|
||||
}
|
||||
// Right limit is infinite:
|
||||
if ((boost::math::isfinite)(a) && (b >= boost::math::tools::max_value<Real>()))
|
||||
{
|
||||
// If a = 0, don't use an additional level of indirection:
|
||||
if (a == (Real) 0)
|
||||
{
|
||||
return m_imp->integrate(f, error, L1, function, tolerance, levels);
|
||||
}
|
||||
const auto u = [&](Real t)->K { return f(t + a); };
|
||||
return m_imp->integrate(u, error, L1, function, tolerance, levels);
|
||||
}
|
||||
|
||||
if ((boost::math::isfinite)(b) && a <= -boost::math::tools::max_value<Real>())
|
||||
{
|
||||
const auto u = [&](Real t)->K { return f(b-t);};
|
||||
return m_imp->integrate(u, error, L1, function, tolerance, levels);
|
||||
}
|
||||
|
||||
// Infinite limits:
|
||||
if ((a <= -boost::math::tools::max_value<Real>()) && (b >= boost::math::tools::max_value<Real>()))
|
||||
{
|
||||
return static_cast<K>(policies::raise_domain_error(function, "Use sinh_sinh quadrature for integration over the whole real line; exp_sinh is for half infinite integrals.", a, Policy()));
|
||||
}
|
||||
// If we get to here then both ends must necessarily be finite:
|
||||
return static_cast<K>(policies::raise_domain_error(function, "Use tanh_sinh quadrature for integration over finite domains; exp_sinh is for half infinite integrals.", a, Policy()));
|
||||
}
|
||||
|
||||
template<class Real, class Policy>
|
||||
template<class F>
|
||||
auto exp_sinh<Real, Policy>::integrate(const F& f, Real tolerance, Real* error, Real* L1, std::size_t* levels)->decltype(std::declval<F>()(std::declval<Real>())) const
|
||||
{
|
||||
static const char* function = "boost::math::quadrature::exp_sinh<%1%>::integrate";
|
||||
return m_imp->integrate(f, error, L1, function, tolerance, levels);
|
||||
}
|
||||
|
||||
|
||||
}}}
|
||||
#endif
|
||||
1297
install/boost_1_75_0/include/boost/math/quadrature/gauss.hpp
Normal file
1297
install/boost_1_75_0/include/boost/math/quadrature/gauss.hpp
Normal file
File diff suppressed because it is too large
Load Diff
1954
install/boost_1_75_0/include/boost/math/quadrature/gauss_kronrod.hpp
Normal file
1954
install/boost_1_75_0/include/boost/math/quadrature/gauss_kronrod.hpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,456 @@
|
||||
/*
|
||||
* Copyright Nick Thompson, 2018
|
||||
* Use, modification and distribution are subject to the
|
||||
* Boost Software License, Version 1.0. (See accompanying file
|
||||
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_MATH_QUADRATURE_NAIVE_MONTE_CARLO_HPP
|
||||
#define BOOST_MATH_QUADRATURE_NAIVE_MONTE_CARLO_HPP
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <boost/atomic.hpp>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <thread>
|
||||
#include <initializer_list>
|
||||
#include <utility>
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
#include <boost/math/policies/error_handling.hpp>
|
||||
|
||||
namespace boost { namespace math { namespace quadrature {
|
||||
|
||||
namespace detail {
|
||||
enum class limit_classification {FINITE,
|
||||
LOWER_BOUND_INFINITE,
|
||||
UPPER_BOUND_INFINITE,
|
||||
DOUBLE_INFINITE};
|
||||
}
|
||||
|
||||
template<class Real, class F, class RandomNumberGenerator = std::mt19937_64, class Policy = boost::math::policies::policy<>>
|
||||
class naive_monte_carlo
|
||||
{
|
||||
public:
|
||||
naive_monte_carlo(const F& integrand,
|
||||
std::vector<std::pair<Real, Real>> const & bounds,
|
||||
Real error_goal,
|
||||
bool singular = true,
|
||||
uint64_t threads = std::thread::hardware_concurrency(),
|
||||
uint64_t seed = 0): m_num_threads{threads}, m_seed{seed}
|
||||
{
|
||||
using std::numeric_limits;
|
||||
using std::sqrt;
|
||||
uint64_t n = bounds.size();
|
||||
m_lbs.resize(n);
|
||||
m_dxs.resize(n);
|
||||
m_limit_types.resize(n);
|
||||
m_volume = 1;
|
||||
static const char* function = "boost::math::quadrature::naive_monte_carlo<%1%>";
|
||||
for (uint64_t i = 0; i < n; ++i)
|
||||
{
|
||||
if (bounds[i].second <= bounds[i].first)
|
||||
{
|
||||
boost::math::policies::raise_domain_error(function, "The upper bound is <= the lower bound.\n", bounds[i].second, Policy());
|
||||
return;
|
||||
}
|
||||
if (bounds[i].first == -numeric_limits<Real>::infinity())
|
||||
{
|
||||
if (bounds[i].second == numeric_limits<Real>::infinity())
|
||||
{
|
||||
m_limit_types[i] = detail::limit_classification::DOUBLE_INFINITE;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_limit_types[i] = detail::limit_classification::LOWER_BOUND_INFINITE;
|
||||
// Ok ok this is bad to use the second bound as the lower limit and then reflect.
|
||||
m_lbs[i] = bounds[i].second;
|
||||
m_dxs[i] = numeric_limits<Real>::quiet_NaN();
|
||||
}
|
||||
}
|
||||
else if (bounds[i].second == numeric_limits<Real>::infinity())
|
||||
{
|
||||
m_limit_types[i] = detail::limit_classification::UPPER_BOUND_INFINITE;
|
||||
if (singular)
|
||||
{
|
||||
// I've found that it's easier to sample on a closed set and perturb the boundary
|
||||
// than to try to sample very close to the boundary.
|
||||
m_lbs[i] = std::nextafter(bounds[i].first, (std::numeric_limits<Real>::max)());
|
||||
}
|
||||
else
|
||||
{
|
||||
m_lbs[i] = bounds[i].first;
|
||||
}
|
||||
m_dxs[i] = numeric_limits<Real>::quiet_NaN();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_limit_types[i] = detail::limit_classification::FINITE;
|
||||
if (singular)
|
||||
{
|
||||
if (bounds[i].first == 0)
|
||||
{
|
||||
m_lbs[i] = std::numeric_limits<Real>::epsilon();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_lbs[i] = std::nextafter(bounds[i].first, (std::numeric_limits<Real>::max)());
|
||||
}
|
||||
|
||||
m_dxs[i] = std::nextafter(bounds[i].second, std::numeric_limits<Real>::lowest()) - m_lbs[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
m_lbs[i] = bounds[i].first;
|
||||
m_dxs[i] = bounds[i].second - bounds[i].first;
|
||||
}
|
||||
m_volume *= m_dxs[i];
|
||||
}
|
||||
}
|
||||
|
||||
m_integrand = [this, &integrand](std::vector<Real> & x)->Real
|
||||
{
|
||||
Real coeff = m_volume;
|
||||
for (uint64_t i = 0; i < x.size(); ++i)
|
||||
{
|
||||
// Variable transformation are listed at:
|
||||
// https://en.wikipedia.org/wiki/Numerical_integration
|
||||
// However, we've made some changes to these so that we can evaluate on a compact domain.
|
||||
if (m_limit_types[i] == detail::limit_classification::FINITE)
|
||||
{
|
||||
x[i] = m_lbs[i] + x[i]*m_dxs[i];
|
||||
}
|
||||
else if (m_limit_types[i] == detail::limit_classification::UPPER_BOUND_INFINITE)
|
||||
{
|
||||
Real t = x[i];
|
||||
Real z = 1/(1 + numeric_limits<Real>::epsilon() - t);
|
||||
coeff *= (z*z)*(1 + numeric_limits<Real>::epsilon());
|
||||
x[i] = m_lbs[i] + t*z;
|
||||
}
|
||||
else if (m_limit_types[i] == detail::limit_classification::LOWER_BOUND_INFINITE)
|
||||
{
|
||||
Real t = x[i];
|
||||
Real z = 1/(t+sqrt((numeric_limits<Real>::min)()));
|
||||
coeff *= (z*z);
|
||||
x[i] = m_lbs[i] + (t-1)*z;
|
||||
}
|
||||
else
|
||||
{
|
||||
Real t1 = 1/(1+numeric_limits<Real>::epsilon() - x[i]);
|
||||
Real t2 = 1/(x[i]+numeric_limits<Real>::epsilon());
|
||||
x[i] = (2*x[i]-1)*t1*t2/4;
|
||||
coeff *= (t1*t1+t2*t2)/4;
|
||||
}
|
||||
}
|
||||
return coeff*integrand(x);
|
||||
};
|
||||
|
||||
// If we don't do a single function call in the constructor,
|
||||
// we can't do a restart.
|
||||
std::vector<Real> x(m_lbs.size());
|
||||
|
||||
// If the seed is zero, that tells us to choose a random seed for the user:
|
||||
if (seed == 0)
|
||||
{
|
||||
std::random_device rd;
|
||||
seed = rd();
|
||||
}
|
||||
|
||||
RandomNumberGenerator gen(seed);
|
||||
Real inv_denom = 1/static_cast<Real>(((gen.max)()-(gen.min)()));
|
||||
|
||||
m_num_threads = (std::max)(m_num_threads, (uint64_t) 1);
|
||||
m_thread_calls.reset(new boost::atomic<uint64_t>[threads]);
|
||||
m_thread_Ss.reset(new boost::atomic<Real>[threads]);
|
||||
m_thread_averages.reset(new boost::atomic<Real>[threads]);
|
||||
|
||||
Real avg = 0;
|
||||
for (uint64_t i = 0; i < m_num_threads; ++i)
|
||||
{
|
||||
for (uint64_t j = 0; j < m_lbs.size(); ++j)
|
||||
{
|
||||
x[j] = (gen()-(gen.min)())*inv_denom;
|
||||
}
|
||||
Real y = m_integrand(x);
|
||||
m_thread_averages[i] = y; // relaxed store
|
||||
m_thread_calls[i] = 1;
|
||||
m_thread_Ss[i] = 0;
|
||||
avg += y;
|
||||
}
|
||||
avg /= m_num_threads;
|
||||
m_avg = avg; // relaxed store
|
||||
|
||||
m_error_goal = error_goal; // relaxed store
|
||||
m_start = std::chrono::system_clock::now();
|
||||
m_done = false; // relaxed store
|
||||
m_total_calls = m_num_threads; // relaxed store
|
||||
m_variance = (numeric_limits<Real>::max)();
|
||||
}
|
||||
|
||||
std::future<Real> integrate()
|
||||
{
|
||||
// Set done to false in case we wish to restart:
|
||||
m_done.store(false); // relaxed store, no worker threads yet
|
||||
m_start = std::chrono::system_clock::now();
|
||||
return std::async(std::launch::async,
|
||||
&naive_monte_carlo::m_integrate, this);
|
||||
}
|
||||
|
||||
void cancel()
|
||||
{
|
||||
// If seed = 0 (meaning have the routine pick the seed), this leaves the seed the same.
|
||||
// If seed != 0, then the seed is changed, so a restart doesn't do the exact same thing.
|
||||
m_seed = m_seed*m_seed;
|
||||
m_done = true; // relaxed store, worker threads will get the message eventually
|
||||
// Make sure the error goal is infinite, because otherwise we'll loop when we do the final error goal check:
|
||||
m_error_goal = (std::numeric_limits<Real>::max)();
|
||||
}
|
||||
|
||||
Real variance() const
|
||||
{
|
||||
return m_variance.load();
|
||||
}
|
||||
|
||||
Real current_error_estimate() const
|
||||
{
|
||||
using std::sqrt;
|
||||
//
|
||||
// There is a bug here: m_variance and m_total_calls get updated asynchronously
|
||||
// and may be out of synch when we compute the error estimate, not sure if it matters though...
|
||||
//
|
||||
return sqrt(m_variance.load()/m_total_calls.load());
|
||||
}
|
||||
|
||||
std::chrono::duration<Real> estimated_time_to_completion() const
|
||||
{
|
||||
auto now = std::chrono::system_clock::now();
|
||||
std::chrono::duration<Real> elapsed_seconds = now - m_start;
|
||||
Real r = this->current_error_estimate()/m_error_goal.load(); // relaxed load
|
||||
if (r*r <= 1) {
|
||||
return 0*elapsed_seconds;
|
||||
}
|
||||
return (r*r - 1)*elapsed_seconds;
|
||||
}
|
||||
|
||||
void update_target_error(Real new_target_error)
|
||||
{
|
||||
m_error_goal = new_target_error; // relaxed store
|
||||
}
|
||||
|
||||
Real progress() const
|
||||
{
|
||||
Real r = m_error_goal.load()/this->current_error_estimate(); // relaxed load
|
||||
if (r*r >= 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return r*r;
|
||||
}
|
||||
|
||||
Real current_estimate() const
|
||||
{
|
||||
return m_avg.load();
|
||||
}
|
||||
|
||||
uint64_t calls() const
|
||||
{
|
||||
return m_total_calls.load(); // relaxed load
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Real m_integrate()
|
||||
{
|
||||
uint64_t seed;
|
||||
// If the user tells us to pick a seed, pick a seed:
|
||||
if (m_seed == 0)
|
||||
{
|
||||
std::random_device rd;
|
||||
seed = rd();
|
||||
}
|
||||
else // use the seed we are given:
|
||||
{
|
||||
seed = m_seed;
|
||||
}
|
||||
RandomNumberGenerator gen(seed);
|
||||
int max_repeat_tries = 5;
|
||||
do{
|
||||
|
||||
if (max_repeat_tries < 5)
|
||||
{
|
||||
m_done = false;
|
||||
|
||||
#ifdef BOOST_NAIVE_MONTE_CARLO_DEBUG_FAILURES
|
||||
std::cout << "Failed to achieve required tolerance first time through..\n";
|
||||
std::cout << " variance = " << m_variance << std::endl;
|
||||
std::cout << " average = " << m_avg << std::endl;
|
||||
std::cout << " total calls = " << m_total_calls << std::endl;
|
||||
|
||||
for (std::size_t i = 0; i < m_num_threads; ++i)
|
||||
std::cout << " thread_calls[" << i << "] = " << m_thread_calls[i] << std::endl;
|
||||
for (std::size_t i = 0; i < m_num_threads; ++i)
|
||||
std::cout << " thread_averages[" << i << "] = " << m_thread_averages[i] << std::endl;
|
||||
for (std::size_t i = 0; i < m_num_threads; ++i)
|
||||
std::cout << " thread_Ss[" << i << "] = " << m_thread_Ss[i] << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::vector<std::thread> threads(m_num_threads);
|
||||
for (uint64_t i = 0; i < threads.size(); ++i)
|
||||
{
|
||||
threads[i] = std::thread(&naive_monte_carlo::m_thread_monte, this, i, gen());
|
||||
}
|
||||
do {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
uint64_t total_calls = 0;
|
||||
for (uint64_t i = 0; i < m_num_threads; ++i)
|
||||
{
|
||||
uint64_t t_calls = m_thread_calls[i].load(boost::memory_order::consume);
|
||||
total_calls += t_calls;
|
||||
}
|
||||
Real variance = 0;
|
||||
Real avg = 0;
|
||||
for (uint64_t i = 0; i < m_num_threads; ++i)
|
||||
{
|
||||
uint64_t t_calls = m_thread_calls[i].load(boost::memory_order::consume);
|
||||
// Will this overflow? Not hard to remove . . .
|
||||
avg += m_thread_averages[i].load(boost::memory_order::relaxed)*((Real)t_calls / (Real)total_calls);
|
||||
variance += m_thread_Ss[i].load(boost::memory_order::relaxed);
|
||||
}
|
||||
m_avg.store(avg, boost::memory_order::release);
|
||||
m_variance.store(variance / (total_calls - 1), boost::memory_order::release);
|
||||
m_total_calls = total_calls; // relaxed store, it's just for user feedback
|
||||
// Allow cancellation:
|
||||
if (m_done) // relaxed load
|
||||
{
|
||||
break;
|
||||
}
|
||||
} while (m_total_calls < 2048 || this->current_error_estimate() > m_error_goal.load(boost::memory_order::consume));
|
||||
// Error bound met; signal the threads:
|
||||
m_done = true; // relaxed store, threads will get the message in the end
|
||||
std::for_each(threads.begin(), threads.end(),
|
||||
std::mem_fn(&std::thread::join));
|
||||
if (m_exception)
|
||||
{
|
||||
std::rethrow_exception(m_exception);
|
||||
}
|
||||
// Incorporate their work into the final estimate:
|
||||
uint64_t total_calls = 0;
|
||||
for (uint64_t i = 0; i < m_num_threads; ++i)
|
||||
{
|
||||
uint64_t t_calls = m_thread_calls[i].load(boost::memory_order::consume);
|
||||
total_calls += t_calls;
|
||||
}
|
||||
Real variance = 0;
|
||||
Real avg = 0;
|
||||
|
||||
for (uint64_t i = 0; i < m_num_threads; ++i)
|
||||
{
|
||||
uint64_t t_calls = m_thread_calls[i].load(boost::memory_order::consume);
|
||||
// Averages weighted by the number of calls the thread made:
|
||||
avg += m_thread_averages[i].load(boost::memory_order::relaxed)*((Real)t_calls / (Real)total_calls);
|
||||
variance += m_thread_Ss[i].load(boost::memory_order::relaxed);
|
||||
}
|
||||
m_avg.store(avg, boost::memory_order::release);
|
||||
m_variance.store(variance / (total_calls - 1), boost::memory_order::release);
|
||||
m_total_calls = total_calls; // relaxed store, this is just user feedback
|
||||
|
||||
// Sometimes, the master will observe the variance at a very "good" (or bad?) moment,
|
||||
// Then the threads proceed to find the variance is much greater by the time they hear the message to stop.
|
||||
// This *WOULD* make sure that the final error estimate is within the error bounds.
|
||||
}
|
||||
while ((--max_repeat_tries >= 0) && (this->current_error_estimate() > m_error_goal));
|
||||
|
||||
return m_avg.load(boost::memory_order::consume);
|
||||
}
|
||||
|
||||
void m_thread_monte(uint64_t thread_index, uint64_t seed)
|
||||
{
|
||||
using std::numeric_limits;
|
||||
try
|
||||
{
|
||||
std::vector<Real> x(m_lbs.size());
|
||||
RandomNumberGenerator gen(seed);
|
||||
Real inv_denom = (Real) 1/(Real)( (gen.max)() - (gen.min)() );
|
||||
Real M1 = m_thread_averages[thread_index].load(boost::memory_order::consume);
|
||||
Real S = m_thread_Ss[thread_index].load(boost::memory_order::consume);
|
||||
// Kahan summation is required or the value of the integrand will go on a random walk during long computations.
|
||||
// See the implementation discussion.
|
||||
// The idea is that the unstabilized additions have error sigma(f)/sqrt(N) + epsilon*N, which diverges faster than it converges!
|
||||
// Kahan summation turns this to sigma(f)/sqrt(N) + epsilon^2*N, and the random walk occurs on a timescale of 10^14 years (on current hardware)
|
||||
Real compensator = 0;
|
||||
uint64_t k = m_thread_calls[thread_index].load(boost::memory_order::consume);
|
||||
while (!m_done) // relaxed load
|
||||
{
|
||||
int j = 0;
|
||||
// If we don't have a certain number of calls before an update, we can easily terminate prematurely
|
||||
// because the variance estimate is way too low. This magic number is a reasonable compromise, as 1/sqrt(2048) = 0.02,
|
||||
// so it should recover 2 digits if the integrand isn't poorly behaved, and if it is, it should discover that before premature termination.
|
||||
// Of course if the user has 64 threads, then this number is probably excessive.
|
||||
int magic_calls_before_update = 2048;
|
||||
while (j++ < magic_calls_before_update)
|
||||
{
|
||||
for (uint64_t i = 0; i < m_lbs.size(); ++i)
|
||||
{
|
||||
x[i] = (gen() - (gen.min)())*inv_denom;
|
||||
}
|
||||
Real f = m_integrand(x);
|
||||
using std::isfinite;
|
||||
if (!isfinite(f))
|
||||
{
|
||||
// The call to m_integrand transform x, so this error message states the correct node.
|
||||
std::stringstream os;
|
||||
os << "Your integrand was evaluated at {";
|
||||
for (uint64_t i = 0; i < x.size() -1; ++i)
|
||||
{
|
||||
os << x[i] << ", ";
|
||||
}
|
||||
os << x[x.size() -1] << "}, and returned " << f << std::endl;
|
||||
static const char* function = "boost::math::quadrature::naive_monte_carlo<%1%>";
|
||||
boost::math::policies::raise_domain_error(function, os.str().c_str(), /*this is a dummy arg to make it compile*/ 7.2, Policy());
|
||||
}
|
||||
++k;
|
||||
Real term = (f - M1)/k;
|
||||
Real y1 = term - compensator;
|
||||
Real M2 = M1 + y1;
|
||||
compensator = (M2 - M1) - y1;
|
||||
S += (f - M1)*(f - M2);
|
||||
M1 = M2;
|
||||
}
|
||||
m_thread_averages[thread_index].store(M1, boost::memory_order::release);
|
||||
m_thread_Ss[thread_index].store(S, boost::memory_order::release);
|
||||
m_thread_calls[thread_index].store(k, boost::memory_order::release);
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// Signal the other threads that the computation is ruined:
|
||||
m_done = true; // relaxed store
|
||||
m_exception = std::current_exception();
|
||||
}
|
||||
}
|
||||
|
||||
std::function<Real(std::vector<Real> &)> m_integrand;
|
||||
uint64_t m_num_threads;
|
||||
uint64_t m_seed;
|
||||
boost::atomic<Real> m_error_goal;
|
||||
boost::atomic<bool> m_done;
|
||||
std::vector<Real> m_lbs;
|
||||
std::vector<Real> m_dxs;
|
||||
std::vector<detail::limit_classification> m_limit_types;
|
||||
Real m_volume;
|
||||
boost::atomic<uint64_t> m_total_calls;
|
||||
// I wanted these to be vectors rather than maps,
|
||||
// but you can't resize a vector of atomics.
|
||||
std::unique_ptr<boost::atomic<uint64_t>[]> m_thread_calls;
|
||||
boost::atomic<Real> m_variance;
|
||||
std::unique_ptr<boost::atomic<Real>[]> m_thread_Ss;
|
||||
boost::atomic<Real> m_avg;
|
||||
std::unique_ptr<boost::atomic<Real>[]> m_thread_averages;
|
||||
std::chrono::time_point<std::chrono::system_clock> m_start;
|
||||
std::exception_ptr m_exception;
|
||||
};
|
||||
|
||||
}}}
|
||||
#endif
|
||||
@@ -0,0 +1,68 @@
|
||||
// Copyright Nick Thompson, 2019
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
/*
|
||||
* References:
|
||||
* Ooura, Takuya, and Masatake Mori. "A robust double exponential formula for Fourier-type integrals." Journal of computational and applied mathematics 112.1-2 (1999): 229-241.
|
||||
* http://www.kurims.kyoto-u.ac.jp/~ooura/intde.html
|
||||
*/
|
||||
#ifndef BOOST_MATH_QUADRATURE_OOURA_FOURIER_INTEGRALS_HPP
|
||||
#define BOOST_MATH_QUADRATURE_OOURA_FOURIER_INTEGRALS_HPP
|
||||
#include <memory>
|
||||
#include <boost/math/quadrature/detail/ooura_fourier_integrals_detail.hpp>
|
||||
|
||||
namespace boost { namespace math { namespace quadrature {
|
||||
|
||||
template<class Real>
|
||||
class ooura_fourier_sin {
|
||||
public:
|
||||
ooura_fourier_sin(const Real relative_error_tolerance = tools::root_epsilon<Real>(), size_t levels = sizeof(Real)) : impl_(std::make_shared<detail::ooura_fourier_sin_detail<Real>>(relative_error_tolerance, levels))
|
||||
{}
|
||||
|
||||
template<class F>
|
||||
std::pair<Real, Real> integrate(F const & f, Real omega) {
|
||||
return impl_->integrate(f, omega);
|
||||
}
|
||||
|
||||
// These are just for debugging/unit tests:
|
||||
std::vector<std::vector<Real>> const & big_nodes() const {
|
||||
return impl_->big_nodes();
|
||||
}
|
||||
|
||||
std::vector<std::vector<Real>> const & weights_for_big_nodes() const {
|
||||
return impl_->weights_for_big_nodes();
|
||||
}
|
||||
|
||||
std::vector<std::vector<Real>> const & little_nodes() const {
|
||||
return impl_->little_nodes();
|
||||
}
|
||||
|
||||
std::vector<std::vector<Real>> const & weights_for_little_nodes() const {
|
||||
return impl_->weights_for_little_nodes();
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<detail::ooura_fourier_sin_detail<Real>> impl_;
|
||||
};
|
||||
|
||||
|
||||
template<class Real>
|
||||
class ooura_fourier_cos {
|
||||
public:
|
||||
ooura_fourier_cos(const Real relative_error_tolerance = tools::root_epsilon<Real>(), size_t levels = sizeof(Real)) : impl_(std::make_shared<detail::ooura_fourier_cos_detail<Real>>(relative_error_tolerance, levels))
|
||||
{}
|
||||
|
||||
template<class F>
|
||||
std::pair<Real, Real> integrate(F const & f, Real omega) {
|
||||
return impl_->integrate(f, omega);
|
||||
}
|
||||
private:
|
||||
std::shared_ptr<detail::ooura_fourier_cos_detail<Real>> impl_;
|
||||
};
|
||||
|
||||
|
||||
}}}
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
// Copyright Nick Thompson, 2017
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
/*
|
||||
* This class performs sinh-sinh quadrature over the entire real line.
|
||||
*
|
||||
* References:
|
||||
*
|
||||
* 1) Tanaka, Ken'ichiro, et al. "Function classes for double exponential integration formulas." Numerische Mathematik 111.4 (2009): 631-655.
|
||||
*/
|
||||
|
||||
#ifndef BOOST_MATH_QUADRATURE_SINH_SINH_HPP
|
||||
#define BOOST_MATH_QUADRATURE_SINH_SINH_HPP
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <boost/math/quadrature/detail/sinh_sinh_detail.hpp>
|
||||
|
||||
namespace boost{ namespace math{ namespace quadrature {
|
||||
|
||||
template<class Real, class Policy = boost::math::policies::policy<> >
|
||||
class sinh_sinh
|
||||
{
|
||||
public:
|
||||
sinh_sinh(size_t max_refinements = 9)
|
||||
: m_imp(std::make_shared<detail::sinh_sinh_detail<Real, Policy> >(max_refinements)) {}
|
||||
|
||||
template<class F>
|
||||
auto integrate(const F f, Real tol = boost::math::tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr)->decltype(std::declval<F>()(std::declval<Real>())) const
|
||||
{
|
||||
return m_imp->integrate(f, tol, error, L1, levels);
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<detail::sinh_sinh_detail<Real, Policy>> m_imp;
|
||||
};
|
||||
|
||||
}}}
|
||||
#endif
|
||||
288
install/boost_1_75_0/include/boost/math/quadrature/tanh_sinh.hpp
Normal file
288
install/boost_1_75_0/include/boost/math/quadrature/tanh_sinh.hpp
Normal file
@@ -0,0 +1,288 @@
|
||||
// Copyright Nick Thompson, 2017
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
/*
|
||||
* This class performs tanh-sinh quadrature on the real line.
|
||||
* Tanh-sinh quadrature is exponentially convergent for integrands in Hardy spaces,
|
||||
* (see https://en.wikipedia.org/wiki/Hardy_space for a formal definition), and is optimal for a random function from that class.
|
||||
*
|
||||
* The tanh-sinh quadrature is one of a class of so called "double exponential quadratures"-there is a large family of them,
|
||||
* but this one seems to be the most commonly used.
|
||||
*
|
||||
* As always, there are caveats: For instance, if the function you want to integrate is not holomorphic on the unit disk,
|
||||
* then the rapid convergence will be spoiled. In this case, a more appropriate quadrature is (say) Romberg, which does not
|
||||
* require the function to be holomorphic, only differentiable up to some order.
|
||||
*
|
||||
* In addition, if you are integrating a periodic function over a period, the trapezoidal rule is better.
|
||||
*
|
||||
* References:
|
||||
*
|
||||
* 1) Mori, Masatake. "Quadrature formulas obtained by variable transformation and the DE-rule." Journal of Computational and Applied Mathematics 12 (1985): 119-130.
|
||||
* 2) Bailey, David H., Karthik Jeyabalan, and Xiaoye S. Li. "A comparison of three high-precision quadrature schemes." Experimental Mathematics 14.3 (2005): 317-329.
|
||||
* 3) Press, William H., et al. "Numerical recipes third edition: the art of scientific computing." Cambridge University Press 32 (2007): 10013-2473.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BOOST_MATH_QUADRATURE_TANH_SINH_HPP
|
||||
#define BOOST_MATH_QUADRATURE_TANH_SINH_HPP
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <boost/math/quadrature/detail/tanh_sinh_detail.hpp>
|
||||
|
||||
namespace boost{ namespace math{ namespace quadrature {
|
||||
|
||||
template<class Real, class Policy = policies::policy<> >
|
||||
class tanh_sinh
|
||||
{
|
||||
public:
|
||||
tanh_sinh(size_t max_refinements = 15, const Real& min_complement = tools::min_value<Real>() * 4)
|
||||
: m_imp(std::make_shared<detail::tanh_sinh_detail<Real, Policy>>(max_refinements, min_complement)) {}
|
||||
|
||||
template<class F>
|
||||
auto integrate(const F f, Real a, Real b, Real tolerance = tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr) ->decltype(std::declval<F>()(std::declval<Real>())) const;
|
||||
template<class F>
|
||||
auto integrate(const F f, Real a, Real b, Real tolerance = tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr) ->decltype(std::declval<F>()(std::declval<Real>(), std::declval<Real>())) const;
|
||||
|
||||
template<class F>
|
||||
auto integrate(const F f, Real tolerance = tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr) ->decltype(std::declval<F>()(std::declval<Real>())) const;
|
||||
template<class F>
|
||||
auto integrate(const F f, Real tolerance = tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr) ->decltype(std::declval<F>()(std::declval<Real>(), std::declval<Real>())) const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<detail::tanh_sinh_detail<Real, Policy>> m_imp;
|
||||
};
|
||||
|
||||
template<class Real, class Policy>
|
||||
template<class F>
|
||||
auto tanh_sinh<Real, Policy>::integrate(const F f, Real a, Real b, Real tolerance, Real* error, Real* L1, std::size_t* levels) ->decltype(std::declval<F>()(std::declval<Real>())) const
|
||||
{
|
||||
BOOST_MATH_STD_USING
|
||||
using boost::math::constants::half;
|
||||
using boost::math::quadrature::detail::tanh_sinh_detail;
|
||||
|
||||
static const char* function = "tanh_sinh<%1%>::integrate";
|
||||
|
||||
typedef decltype(std::declval<F>()(std::declval<Real>())) result_type;
|
||||
|
||||
if (!(boost::math::isnan)(a) && !(boost::math::isnan)(b))
|
||||
{
|
||||
|
||||
// Infinite limits:
|
||||
if ((a <= -tools::max_value<Real>()) && (b >= tools::max_value<Real>()))
|
||||
{
|
||||
auto u = [&](const Real& t, const Real& tc)->result_type
|
||||
{
|
||||
Real t_sq = t*t;
|
||||
Real inv;
|
||||
if (t > 0.5f)
|
||||
inv = 1 / ((2 - tc) * tc);
|
||||
else if(t < -0.5)
|
||||
inv = 1 / ((2 + tc) * -tc);
|
||||
else
|
||||
inv = 1 / (1 - t_sq);
|
||||
return f(t*inv)*(1 + t_sq)*inv*inv;
|
||||
};
|
||||
Real limit = sqrt(tools::min_value<Real>()) * 4;
|
||||
return m_imp->integrate(u, error, L1, function, limit, limit, tolerance, levels);
|
||||
}
|
||||
|
||||
// Right limit is infinite:
|
||||
if ((boost::math::isfinite)(a) && (b >= tools::max_value<Real>()))
|
||||
{
|
||||
auto u = [&](const Real& t, const Real& tc)->result_type
|
||||
{
|
||||
Real z, arg;
|
||||
if (t > -0.5f)
|
||||
z = 1 / (t + 1);
|
||||
else
|
||||
z = -1 / tc;
|
||||
if (t < 0.5)
|
||||
arg = 2 * z + a - 1;
|
||||
else
|
||||
arg = a + tc / (2 - tc);
|
||||
return f(arg)*z*z;
|
||||
};
|
||||
Real left_limit = sqrt(tools::min_value<Real>()) * 4;
|
||||
result_type Q = Real(2) * m_imp->integrate(u, error, L1, function, left_limit, tools::min_value<Real>(), tolerance, levels);
|
||||
if (L1)
|
||||
{
|
||||
*L1 *= 2;
|
||||
}
|
||||
if (error)
|
||||
{
|
||||
*error *= 2;
|
||||
}
|
||||
|
||||
return Q;
|
||||
}
|
||||
|
||||
if ((boost::math::isfinite)(b) && (a <= -tools::max_value<Real>()))
|
||||
{
|
||||
auto v = [&](const Real& t, const Real& tc)->result_type
|
||||
{
|
||||
Real z;
|
||||
if (t > -0.5)
|
||||
z = 1 / (t + 1);
|
||||
else
|
||||
z = -1 / tc;
|
||||
Real arg;
|
||||
if (t < 0.5)
|
||||
arg = 2 * z - 1;
|
||||
else
|
||||
arg = tc / (2 - tc);
|
||||
return f(b - arg) * z * z;
|
||||
};
|
||||
|
||||
Real left_limit = sqrt(tools::min_value<Real>()) * 4;
|
||||
result_type Q = Real(2) * m_imp->integrate(v, error, L1, function, left_limit, tools::min_value<Real>(), tolerance, levels);
|
||||
if (L1)
|
||||
{
|
||||
*L1 *= 2;
|
||||
}
|
||||
if (error)
|
||||
{
|
||||
*error *= 2;
|
||||
}
|
||||
return Q;
|
||||
}
|
||||
|
||||
if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return result_type(0);
|
||||
}
|
||||
if (b < a)
|
||||
{
|
||||
return -this->integrate(f, b, a, tolerance, error, L1, levels);
|
||||
}
|
||||
Real avg = (a + b)*half<Real>();
|
||||
Real diff = (b - a)*half<Real>();
|
||||
Real avg_over_diff_m1 = a / diff;
|
||||
Real avg_over_diff_p1 = b / diff;
|
||||
bool have_small_left = fabs(a) < 0.5f;
|
||||
bool have_small_right = fabs(b) < 0.5f;
|
||||
Real left_min_complement = float_next(avg_over_diff_m1) - avg_over_diff_m1;
|
||||
Real min_complement_limit = (std::max)(tools::min_value<Real>(), Real(tools::min_value<Real>() / diff));
|
||||
if (left_min_complement < min_complement_limit)
|
||||
left_min_complement = min_complement_limit;
|
||||
Real right_min_complement = avg_over_diff_p1 - float_prior(avg_over_diff_p1);
|
||||
if (right_min_complement < min_complement_limit)
|
||||
right_min_complement = min_complement_limit;
|
||||
//
|
||||
// These asserts will fail only if rounding errors on
|
||||
// type Real have accumulated so much error that it's
|
||||
// broken our internal logic. Should that prove to be
|
||||
// a persistent issue, we might need to add a bit of fudge
|
||||
// factor to move left_min_complement and right_min_complement
|
||||
// further from the end points of the range.
|
||||
//
|
||||
BOOST_ASSERT((left_min_complement * diff + a) > a);
|
||||
BOOST_ASSERT((b - right_min_complement * diff) < b);
|
||||
auto u = [&](Real z, Real zc)->result_type
|
||||
{
|
||||
Real position;
|
||||
if (z < -0.5)
|
||||
{
|
||||
if(have_small_left)
|
||||
return f(diff * (avg_over_diff_m1 - zc));
|
||||
position = a - diff * zc;
|
||||
}
|
||||
else if (z > 0.5)
|
||||
{
|
||||
if(have_small_right)
|
||||
return f(diff * (avg_over_diff_p1 - zc));
|
||||
position = b - diff * zc;
|
||||
}
|
||||
else
|
||||
position = avg + diff*z;
|
||||
BOOST_ASSERT(position != a);
|
||||
BOOST_ASSERT(position != b);
|
||||
return f(position);
|
||||
};
|
||||
result_type Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
|
||||
|
||||
if (L1)
|
||||
{
|
||||
*L1 *= diff;
|
||||
}
|
||||
if (error)
|
||||
{
|
||||
*error *= diff;
|
||||
}
|
||||
return Q;
|
||||
}
|
||||
}
|
||||
return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
|
||||
}
|
||||
|
||||
template<class Real, class Policy>
|
||||
template<class F>
|
||||
auto tanh_sinh<Real, Policy>::integrate(const F f, Real a, Real b, Real tolerance, Real* error, Real* L1, std::size_t* levels) ->decltype(std::declval<F>()(std::declval<Real>(), std::declval<Real>())) const
|
||||
{
|
||||
BOOST_MATH_STD_USING
|
||||
using boost::math::constants::half;
|
||||
using boost::math::quadrature::detail::tanh_sinh_detail;
|
||||
|
||||
static const char* function = "tanh_sinh<%1%>::integrate";
|
||||
|
||||
if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
|
||||
{
|
||||
if (b <= a)
|
||||
{
|
||||
return policies::raise_domain_error(function, "Arguments to integrate are in wrong order; integration over [a,b] must have b > a.", a, Policy());
|
||||
}
|
||||
auto u = [&](Real z, Real zc)->Real
|
||||
{
|
||||
if (z < 0)
|
||||
return f((a - b) * zc / 2 + a, (b - a) * zc / 2);
|
||||
else
|
||||
return f((a - b) * zc / 2 + b, (b - a) * zc / 2);
|
||||
};
|
||||
Real diff = (b - a)*half<Real>();
|
||||
Real left_min_complement = tools::min_value<Real>() * 4;
|
||||
Real right_min_complement = tools::min_value<Real>() * 4;
|
||||
Real Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
|
||||
|
||||
if (L1)
|
||||
{
|
||||
*L1 *= diff;
|
||||
}
|
||||
if (error)
|
||||
{
|
||||
*error *= diff;
|
||||
}
|
||||
return Q;
|
||||
}
|
||||
return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
|
||||
}
|
||||
|
||||
template<class Real, class Policy>
|
||||
template<class F>
|
||||
auto tanh_sinh<Real, Policy>::integrate(const F f, Real tolerance, Real* error, Real* L1, std::size_t* levels) ->decltype(std::declval<F>()(std::declval<Real>())) const
|
||||
{
|
||||
using boost::math::quadrature::detail::tanh_sinh_detail;
|
||||
static const char* function = "tanh_sinh<%1%>::integrate";
|
||||
Real min_complement = tools::epsilon<Real>();
|
||||
return m_imp->integrate([&](const Real& arg, const Real&) { return f(arg); }, error, L1, function, min_complement, min_complement, tolerance, levels);
|
||||
}
|
||||
|
||||
template<class Real, class Policy>
|
||||
template<class F>
|
||||
auto tanh_sinh<Real, Policy>::integrate(const F f, Real tolerance, Real* error, Real* L1, std::size_t* levels) ->decltype(std::declval<F>()(std::declval<Real>(), std::declval<Real>())) const
|
||||
{
|
||||
using boost::math::quadrature::detail::tanh_sinh_detail;
|
||||
static const char* function = "tanh_sinh<%1%>::integrate";
|
||||
Real min_complement = tools::min_value<Real>() * 4;
|
||||
return m_imp->integrate(f, error, L1, function, min_complement, min_complement, tolerance, levels);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright Nick Thompson, 2017
|
||||
* Use, modification and distribution are subject to the
|
||||
* Boost Software License, Version 1.0. (See accompanying file
|
||||
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*
|
||||
* Use the adaptive trapezoidal rule to estimate the integral of periodic functions over a period,
|
||||
* or to integrate a function whose derivative vanishes at the endpoints.
|
||||
*
|
||||
* If your function does not satisfy these conditions, and instead is simply continuous and bounded
|
||||
* over the whole interval, then this routine will still converge, albeit slowly. However, there
|
||||
* are much more efficient methods in this case, including Romberg, Simpson, and double exponential quadrature.
|
||||
*/
|
||||
|
||||
#ifndef BOOST_MATH_QUADRATURE_TRAPEZOIDAL_HPP
|
||||
#define BOOST_MATH_QUADRATURE_TRAPEZOIDAL_HPP
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <boost/math/constants/constants.hpp>
|
||||
#include <boost/math/special_functions/fpclassify.hpp>
|
||||
#include <boost/math/policies/error_handling.hpp>
|
||||
#include <boost/math/tools/cxx03_warn.hpp>
|
||||
|
||||
namespace boost{ namespace math{ namespace quadrature {
|
||||
|
||||
template<class F, class Real, class Policy>
|
||||
auto trapezoidal(F f, Real a, Real b, Real tol, std::size_t max_refinements, Real* error_estimate, Real* L1, const Policy& pol)->decltype(std::declval<F>()(std::declval<Real>()))
|
||||
{
|
||||
static const char* function = "boost::math::quadrature::trapezoidal<%1%>(F, %1%, %1%, %1%)";
|
||||
using std::abs;
|
||||
using boost::math::constants::half;
|
||||
// In many math texts, K represents the field of real or complex numbers.
|
||||
// Too bad we can't put blackboard bold into C++ source!
|
||||
typedef decltype(f(a)) K;
|
||||
if (!(boost::math::isfinite)(a))
|
||||
{
|
||||
return static_cast<K>(boost::math::policies::raise_domain_error(function, "Left endpoint of integration must be finite for adaptive trapezoidal integration but got a = %1%.\n", a, pol));
|
||||
}
|
||||
if (!(boost::math::isfinite)(b))
|
||||
{
|
||||
return static_cast<K>(boost::math::policies::raise_domain_error(function, "Right endpoint of integration must be finite for adaptive trapezoidal integration but got b = %1%.\n", b, pol));
|
||||
}
|
||||
|
||||
if (a == b)
|
||||
{
|
||||
return static_cast<K>(0);
|
||||
}
|
||||
if(a > b)
|
||||
{
|
||||
return -trapezoidal(f, b, a, tol, max_refinements, error_estimate, L1, pol);
|
||||
}
|
||||
|
||||
|
||||
K ya = f(a);
|
||||
K yb = f(b);
|
||||
Real h = (b - a)*half<Real>();
|
||||
K I0 = (ya + yb)*h;
|
||||
Real IL0 = (abs(ya) + abs(yb))*h;
|
||||
|
||||
K yh = f(a + h);
|
||||
K I1;
|
||||
I1 = I0*half<Real>() + yh*h;
|
||||
Real IL1 = IL0*half<Real>() + abs(yh)*h;
|
||||
|
||||
// The recursion is:
|
||||
// I_k = 1/2 I_{k-1} + 1/2^k \sum_{j=1; j odd, j < 2^k} f(a + j(b-a)/2^k)
|
||||
std::size_t k = 2;
|
||||
// We want to go through at least 5 levels so we have sampled the function at least 20 times.
|
||||
// Otherwise, we could terminate prematurely and miss essential features.
|
||||
// This is of course possible anyway, but 20 samples seems to be a reasonable compromise.
|
||||
Real error = abs(I0 - I1);
|
||||
// I take k < 5, rather than k < 4, or some other smaller minimum number,
|
||||
// because I hit a truly exceptional bug where the k = 2 and k =3 refinement were bitwise equal,
|
||||
// but the quadrature had not yet converged.
|
||||
while (k < 5 || (k < max_refinements && error > tol*IL1) )
|
||||
{
|
||||
I0 = I1;
|
||||
IL0 = IL1;
|
||||
|
||||
I1 = I0*half<Real>();
|
||||
IL1 = IL0*half<Real>();
|
||||
std::size_t p = static_cast<std::size_t>(1u) << k;
|
||||
h *= half<Real>();
|
||||
K sum = 0;
|
||||
Real absum = 0;
|
||||
|
||||
for(std::size_t j = 1; j < p; j += 2)
|
||||
{
|
||||
K y = f(a + j*h);
|
||||
sum += y;
|
||||
absum += abs(y);
|
||||
}
|
||||
|
||||
I1 += sum*h;
|
||||
IL1 += absum*h;
|
||||
++k;
|
||||
error = abs(I0 - I1);
|
||||
}
|
||||
|
||||
if (error_estimate)
|
||||
{
|
||||
*error_estimate = error;
|
||||
}
|
||||
|
||||
if (L1)
|
||||
{
|
||||
*L1 = IL1;
|
||||
}
|
||||
|
||||
return static_cast<K>(I1);
|
||||
}
|
||||
#if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
|
||||
// Template argument deduction failure otherwise:
|
||||
template<class F, class Real>
|
||||
auto trapezoidal(F f, Real a, Real b, Real tol = 0, std::size_t max_refinements = 12, Real* error_estimate = 0, Real* L1 = 0)->decltype(std::declval<F>()(std::declval<Real>()))
|
||||
#elif !defined(BOOST_NO_CXX11_NULLPTR)
|
||||
template<class F, class Real>
|
||||
auto trapezoidal(F f, Real a, Real b, Real tol = boost::math::tools::root_epsilon<Real>(), std::size_t max_refinements = 12, Real* error_estimate = nullptr, Real* L1 = nullptr)->decltype(std::declval<F>()(std::declval<Real>()))
|
||||
#else
|
||||
template<class F, class Real>
|
||||
auto trapezoidal(F f, Real a, Real b, Real tol = boost::math::tools::root_epsilon<Real>(), std::size_t max_refinements = 12, Real* error_estimate = 0, Real* L1 = 0)->decltype(std::declval<F>()(std::declval<Real>()))
|
||||
#endif
|
||||
{
|
||||
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1600)
|
||||
if (tol == 0)
|
||||
tol = boost::math::tools::root_epsilon<Real>();
|
||||
#endif
|
||||
return trapezoidal(f, a, b, tol, max_refinements, error_estimate, L1, boost::math::policies::policy<>());
|
||||
}
|
||||
|
||||
}}}
|
||||
#endif
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright Nick Thompson, 2020
|
||||
* Use, modification and distribution are subject to the
|
||||
* Boost Software License, Version 1.0. (See accompanying file
|
||||
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_MATH_QUADRATURE_WAVELET_TRANSFORMS_HPP
|
||||
#define BOOST_MATH_QUADRATURE_WAVELET_TRANSFORMS_HPP
|
||||
#include <boost/math/special_functions/daubechies_wavelet.hpp>
|
||||
#include <boost/math/quadrature/trapezoidal.hpp>
|
||||
|
||||
namespace boost::math::quadrature {
|
||||
|
||||
template<class F, typename Real, int p>
|
||||
class daubechies_wavelet_transform
|
||||
{
|
||||
public:
|
||||
daubechies_wavelet_transform(F f, int grid_refinements = -1, Real tol = 100*std::numeric_limits<Real>::epsilon(),
|
||||
int max_refinements = 12) : f_{f}, psi_(grid_refinements), tol_{tol}, max_refinements_{max_refinements}
|
||||
{}
|
||||
|
||||
daubechies_wavelet_transform(F f, boost::math::daubechies_wavelet<Real, p> wavelet, Real tol = 100*std::numeric_limits<Real>::epsilon(),
|
||||
int max_refinements = 12) : f_{f}, psi_{wavelet}, tol_{tol}, max_refinements_{max_refinements}
|
||||
{}
|
||||
|
||||
auto operator()(Real s, Real t)->decltype(std::declval<F>()(std::declval<Real>())) const
|
||||
{
|
||||
using std::sqrt;
|
||||
using std::abs;
|
||||
using boost::math::quadrature::trapezoidal;
|
||||
auto g = [&] (Real u) {
|
||||
return f_(s*u+t)*psi_(u);
|
||||
};
|
||||
auto [a,b] = psi_.support();
|
||||
return sqrt(abs(s))*trapezoidal(g, a, b, tol_, max_refinements_);
|
||||
}
|
||||
|
||||
private:
|
||||
F f_;
|
||||
boost::math::daubechies_wavelet<Real, p> psi_;
|
||||
Real tol_;
|
||||
int max_refinements_;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user