Automatic Differentiation
 
Loading...
Searching...
No Matches
integrate_1d_gauss_kronrod.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUNCTOR_INTEGRATE_1D_GAUSS_KRONROD_HPP
2#define STAN_MATH_FWD_FUNCTOR_INTEGRATE_1D_GAUSS_KRONROD_HPP
3
10
11namespace stan {
12namespace math {
13
35template <typename F, typename T_a, typename T_b, typename... Args,
36 require_any_st_fvar<T_a, T_b, Args...> * = nullptr>
38 const F &f, const T_a &a, const T_b &b, double relative_tolerance,
39 double absolute_tolerance, int max_depth, std::ostream *msgs,
40 const Args &... args) {
41 using FvarT = scalar_type_t<return_type_t<T_a, T_b, Args...>>;
42
43 // Wrap integrate_1d_gauss_kronrod call in a functor where the input
44 // arguments are only those for which tangents are needed.
45 auto a_val = value_of(a);
46 auto b_val = value_of(b);
47 auto func = [f, msgs, relative_tolerance, absolute_tolerance, max_depth,
48 a_val, b_val](const auto &... args_var) {
49 return integrate_1d_gauss_kronrod_tol(f, a_val, b_val, relative_tolerance,
50 absolute_tolerance, max_depth, msgs,
51 args_var...);
52 };
53 FvarT ret = finite_diff(func, args...);
54 // Calculate tangents w.r.t. integration bounds if needed
56 if constexpr (is_fvar<T_a>::value) {
57 ret.d_ += a.d_ * -f(a_val, 0.0, msgs, value_of(args)...);
58 }
59 if constexpr (is_fvar<T_b>::value) {
60 ret.d_ += b.d_ * f(b_val, 0.0, msgs, value_of(args)...);
61 }
62 }
63 return ret;
64}
65
83template <typename F, typename T_a, typename T_b, typename... Args,
84 require_any_st_fvar<T_a, T_b, Args...> * = nullptr>
85inline return_type_t<T_a, T_b, Args...> integrate_1d_gauss_kronrod(
86 const F &f, const T_a &a, const T_b &b, std::ostream *msgs,
87 const Args &... args) {
88 return integrate_1d_gauss_kronrod_tol(f, a, b, std::sqrt(EPSILON), 0.0,
90 msgs, args...);
91}
92
93} // namespace math
94} // namespace stan
95#endif
require_any_t< is_fvar< scalar_type_t< std::decay_t< Types > > >... > require_any_st_fvar
Require any of the scalar types satisfy is_fvar.
Definition is_fvar.hpp:101
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
static constexpr double EPSILON
Smallest positive value.
Definition constants.hpp:41
return_type_t< T_a, T_b, Args... > integrate_1d_gauss_kronrod_tol(const F &f, const T_a &a, const T_b &b, double relative_tolerance, double absolute_tolerance, int max_depth, std::ostream *msgs, const Args &... args)
Return the integral of f from a to b using adaptive Gauss-Kronrod (G21,K21) quadrature,...
constexpr int INTEGRATE_1D_GAUSS_KRONROD_MAX_DEPTH
Default recursive bisection depth used by integrate_1d_gauss_kronrod when the user does not pass one ...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
return_type_t< T_a, T_b, Args... > integrate_1d_gauss_kronrod(const F &f, const T_a &a, const T_b &b, std::ostream *msgs, const Args &... args)
Return the integral of f from a to b using adaptive Gauss-Kronrod (G21,K21) quadrature,...
auto finite_diff(const F &func, const TArgs &... args)
Construct an fvar<T> where the tangent is calculated by finite-differencing.
typename scalar_type< T >::type scalar_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Defines a static member function type which is defined to be false as the primitive scalar types cann...
Definition is_fvar.hpp:15