1#ifndef STAN_MATH_REV_FUNCTOR_integrate_1d_HPP
2#define STAN_MATH_REV_FUNCTOR_integrate_1d_HPP
39template <
typename F,
typename T_a,
typename T_b,
typename... Args,
42 const F &f,
const T_a &a,
const T_b &b,
double relative_tolerance,
43 std::ostream *msgs,
const Args &... args) {
44 static constexpr const char *function =
"integrate_1d";
57 auto args_val_tuple = std::make_tuple(
value_of(args)...);
60 [&](
const auto &x,
const auto &xc) {
62 [&](
auto &&... val_args) {
return f(x, xc, msgs, val_args...); },
65 a_val, b_val, relative_tolerance);
67 constexpr size_t num_vars_ab = is_var<T_a>::value + is_var<T_b>::value;
70 num_vars_ab + num_vars_args);
72 num_vars_ab + num_vars_args);
78 for (
size_t i = 0; i < num_vars_ab + num_vars_args; ++i) {
82 if (is_var<T_a>::value && !
is_inf(a)) {
84 [&f, a_val, msgs](
auto &&... val_args) {
85 return -f(a_val, 0.0, msgs, val_args...);
91 if (!
is_inf(b) && is_var<T_b>::value) {
93 [&f, b_val, msgs](
auto &&... val_args) {
94 return f(b_val, 0.0, msgs, val_args...);
101 nested_rev_autodiff argument_nest;
104 auto args_tuple_local_copy = std::make_tuple(
deep_copy_vars(args)...);
107 std::vector<vari *> local_varis(num_vars_args);
109 [&](
const auto &... args) {
112 args_tuple_local_copy);
114 for (
size_t n = 0; n < num_vars_args; ++n) {
118 [&](
const auto &x,
const auto &xc) {
119 argument_nest.set_zero_all_adjoints();
121 nested_rev_autodiff gradient_nest;
123 [&f, &x, &xc, msgs](
auto &&... local_args) {
124 return f(x, xc, msgs, local_args...);
126 args_tuple_local_copy);
129 double gradient = local_varis[n]->adj();
139 "is nan for parameter ",
"");
144 a_val, b_val, relative_tolerance);
151 [total_vars = num_vars_ab + num_vars_args, varis,
partials](
auto &vi) {
152 for (
size_t i = 0; i < total_vars; ++i) {
153 varis[i]->adj_ +=
partials[i] * vi.adj();
214template <
typename F,
typename T_a,
typename T_b,
typename T_theta,
215 typename = require_any_var_t<T_a, T_b, T_theta>>
217 const F &f,
const T_a &a,
const T_b &b,
const std::vector<T_theta> &theta,
218 const std::vector<double> &x_r,
const std::vector<int> &x_i,
219 std::ostream *msgs,
const double relative_tolerance = std::sqrt(
EPSILON)) {
221 msgs, theta, x_r, x_i);
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
require_any_t< is_var< scalar_type_t< std::decay_t< Types > > >... > require_any_st_var
Require any of the scalar types satisfy is_var.
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high, Idxs... idxs)
Throw an exception if y is not less than high.
bool is_nan(T &&x)
Returns 1 if the input's value is NaN and 0 otherwise.
static constexpr double EPSILON
Smallest positive value.
void gradient(const F &f, const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, T &fx, Eigen::Matrix< T, Eigen::Dynamic, 1 > &grad_fx)
Calculate the value and the gradient of the specified function at the specified argument.
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
vari_value< double > vari
void throw_domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
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.
Arith deep_copy_vars(Arith &&arg)
Forward arguments that do not contain vars.
size_t count_vars(Pargs &&... args)
Count the number of vars in the input argument list.
vari ** save_varis(vari **dest, const var &x, Pargs &&... args)
Save the vari pointer in x into the memory pointed to by dest, increment the dest storage pointer,...
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...
constexpr auto & partials(internal::partials_propagator< Types... > &x) noexcept
Access the partials for an edge of an partials_propagator
double integrate(const F &f, double a, double b, double relative_tolerance)
Integrate a single variable function f from a to b to within a specified relative tolerance.
int is_inf(const fvar< T > &x)
Returns 1 if the input's value is infinite and 0 otherwise.
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
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.
static thread_local AutodiffStackStorage * instance_