Automatic Differentiation
 
Loading...
Searching...
No Matches

◆ integrate_1d() [1/2]

template<typename F , typename T_a , typename T_b , typename T_theta , require_any_fvar_t< T_a, T_b, T_theta > * = nullptr>
return_type_t< T_a, T_b, T_theta > stan::math::integrate_1d ( const F &  f,
const T_a &  a,
const T_b &  b,
const std::vector< T_theta > &  theta,
const std::vector< double > &  x_r,
const std::vector< int > &  x_i,
std::ostream *  msgs,
const double  relative_tolerance = std::sqrt(EPSILON) 
)
inline

Compute the integral of the single variable function f from a to b to within a specified relative tolerance.

a and b can be finite or infinite.

Template Parameters
T_atype of first limit
T_btype of second limit
T_thetatype of parameters
TType of f
Parameters
fthe functor to integrate
alower limit of integration
bupper limit of integration
thetaadditional parameters to be passed to f
x_radditional data to be passed to f
x_iadditional integer data to be passed to f
[in,out]msgsthe print stream for warning messages
relative_tolerancerelative tolerance passed to Boost quadrature
Returns
numeric integral of function f

a and b can be finite or infinite.

f should be compatible with reverse mode autodiff and have the signature: var f(double x, double xc, const std::vector<var>& theta, const std::vector<double>& x_r, const std::vector<int> &x_i, std::ostream* msgs)

It should return the value of the function evaluated at x. Any errors should be printed to the msgs stream.

Integrals that cross zero are broken into two, and the separate integrals are each integrated to the given relative tolerance.

For integrals with finite limits, the xc argument is the distance to the nearest boundary. So for a > 0, b > 0, it will be a - x for x closer to a, and b - x for x closer to b. xc is computed in a way that avoids the precision loss of computing a - x or b - x manually. For integrals that cross zero, xc can take values a - x, -x, or b - x depending on which integration limit it is nearest.

If either limit is infinite, xc is set to NaN

The integration algorithm terminates when

\[ \frac{{|I_{n + 1} - I_n|}}{{|I|_{n + 1}}} < \text{relative tolerance} \]

where \(I_{n}\) is the nth estimate of the integral and \(|I|_{n}\) is the nth estimate of the norm of the integral.

Integrals that cross zero are split into two. In this case, each integral is separately integrated to the given relative_tolerance.

Gradients of f that evaluate to NaN when the function evaluates to zero are set to zero themselves. This is due to the autodiff easily overflowing to NaN when evaluating gradients near the maximum and minimum floating point values (where the function should be zero anyway for the integral to exist)

Template Parameters
T_atype of first limit
T_btype of second limit
T_thetatype of parameters
TType of f
Parameters
fthe functor to integrate
alower limit of integration
bupper limit of integration
thetaadditional parameters to be passed to f
x_radditional data to be passed to f
x_iadditional integer data to be passed to f
[in,out]msgsthe print stream for warning messages
relative_tolerancerelative tolerance passed to Boost quadrature
Returns
numeric integral of function f

Definition at line 91 of file integrate_1d.hpp.