Automatic Differentiation
 
Loading...
Searching...
No Matches
integrate_1d.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUNCTOR_INTEGRATE_1D_HPP
2#define STAN_MATH_FWD_FUNCTOR_INTEGRATE_1D_HPP
3
10
11namespace stan {
12namespace math {
29template <typename F, typename T_a, typename T_b, typename... Args,
30 require_any_st_fvar<T_a, T_b, Args...> * = nullptr>
31inline return_type_t<T_a, T_b, Args...> integrate_1d_impl(
32 const F &f, const T_a &a, const T_b &b, double relative_tolerance,
33 std::ostream *msgs, const Args &... args) {
34 using FvarT = scalar_type_t<return_type_t<T_a, T_b, Args...>>;
35
36 // Wrap integrate_1d call in a functor where the input arguments are only
37 // for which tangents are needed
38 auto a_val = value_of(a);
39 auto b_val = value_of(b);
40 auto func
41 = [f, msgs, relative_tolerance, a_val, b_val](const auto &... args_var) {
42 return integrate_1d_impl(f, a_val, b_val, relative_tolerance, msgs,
43 args_var...);
44 };
45 FvarT ret = finite_diff(func, args...);
46
47 // Calculate tangents w.r.t. integration bounds if needed
49 auto val_args = std::make_tuple(value_of(args)...);
51 ret.d_ += math::forward_as<FvarT>(a).d_
53 [&](auto &&... tuple_args) {
54 return -f(a_val, 0.0, msgs, tuple_args...);
55 },
56 val_args);
57 }
59 ret.d_ += math::forward_as<FvarT>(b).d_
61 [&](auto &&... tuple_args) {
62 return f(b_val, 0.0, msgs, tuple_args...);
63 },
64 val_args);
65 }
66 }
67 return ret;
68}
69
89template <typename F, typename T_a, typename T_b, typename T_theta,
92 const F &f, const T_a &a, const T_b &b, const std::vector<T_theta> &theta,
93 const std::vector<double> &x_r, const std::vector<int> &x_i,
94 std::ostream *msgs, const double relative_tolerance) {
95 return integrate_1d_impl(integrate_1d_adapter<F>(f), a, b, relative_tolerance,
96 msgs, theta, x_r, x_i);
97}
98
99} // namespace math
100} // namespace stan
101#endif
require_any_t< is_fvar< std::decay_t< Types > >... > require_any_fvar_t
Require any of the types satisfy is_fvar.
Definition is_fvar.hpp:40
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:89
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
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_impl(const F &f, const T_a &a, const T_b &b, double relative_tolerance, std::ostream *msgs, const Args &... args)
Return the integral of f from a to b to the given relative tolerance.
return_type_t< T_a, T_b, T_theta > 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)
Compute the integral of the single variable function f from a to b to within a specified relative tol...
auto finite_diff(const F &func, const TArgs &... args)
Construct an fvar<T> where the tangent is calculated by finite-differencing.
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:52
typename scalar_type< T >::type scalar_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Adapt the non-variadic integrate_1d arguments to the variadic integrate_1d_impl interface.
Defines a static member function type which is defined to be false as the primitive scalar types cann...
Definition is_fvar.hpp:15