1 #ifndef STAN_MATH_REV_ARR_FUNCTOR_integrate_1d_HPP 2 #define STAN_MATH_REV_ARR_FUNCTOR_integrate_1d_HPP 11 #include <type_traits> 30 inline double gradient_of_f(
const F &f,
const double &x,
const double &xc,
31 const std::vector<double> &theta_vals,
32 const std::vector<double> &x_r,
33 const std::vector<int> &x_i,
size_t n,
37 std::vector<var> theta_var(theta_vals.size());
39 for (
size_t i = 0; i < theta_vals.size(); i++)
40 theta_var[i] = theta_vals[i];
41 var fx = f(x, xc, theta_var, x_r, x_i, &msgs);
43 gradient = theta_var[n].adj();
49 "is nan for parameter ",
"");
52 }
catch (
const std::exception &
e) {
115 template <
typename F,
typename T_a,
typename T_b,
typename T_theta>
116 inline typename std::enable_if<std::is_same<T_a, var>::value
117 || std::is_same<T_b, var>::value
118 || std::is_same<T_theta, var>::value,
121 const std::vector<T_theta> &theta,
const std::vector<double> &x_r,
122 const std::vector<int> &x_i, std::ostream &msgs,
123 const double relative_tolerance
124 =
std::sqrt(std::numeric_limits<double>::epsilon())) {
125 static const char *
function =
"integrate_1d";
135 std::bind<double>(f, std::placeholders::_1, std::placeholders::_2,
140 std::vector<double> dintegral_dtheta(N_theta_vars);
141 std::vector<var> theta_concat(N_theta_vars);
143 if (N_theta_vars > 0) {
144 std::vector<double> theta_vals =
value_of(theta);
146 for (
size_t n = 0; n < N_theta_vars; ++n) {
148 std::bind<double>(gradient_of_f<F>, f, std::placeholders::_1,
149 std::placeholders::_2, theta_vals, x_r, x_i, n,
152 theta_concat[n] = theta[n];
157 theta_concat.push_back(a);
158 dintegral_dtheta.push_back(
163 theta_concat.push_back(b);
164 dintegral_dtheta.push_back(
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high)
Check if y is less or equal to high.
double gradient_of_f(const F &f, const double &x, const double &xc, const std::vector< double > &theta_vals, const std::vector< double > &x_r, const std::vector< int > &x_i, size_t n, std::ostream &msgs)
Calculate first derivative of f(x, param, std::ostream&) with respect to the nth parameter.
fvar< T > sqrt(const fvar< T > &x)
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Defines a public enum named value which is defined to be false as the primitive scalar types cannot b...
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...
Independent (input) and dependent (output) variables for gradients.
var precomputed_gradients(double value, const std::vector< var > &operands, const std::vector< double > &gradients)
This function returns a var for an expression that has the specified value, vector of operands...
void 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.
void grad(std::vector< var > &x, std::vector< double > &g)
Compute the gradient of this (dependent) variable with respect to the specified vector of (independen...
int is_inf(const fvar< T > &x)
Returns 1 if the input's value is infinite and 0 otherwise.
double e()
Return the base of the natural logarithm.
double integrate_1d(const F &f, const double a, const double b, const std::vector< double > &theta, const std::vector< double > &x_r, const std::vector< int > &x_i, std::ostream &msgs, const double relative_tolerance=std::sqrt(std::numeric_limits< double >::epsilon()))
Compute the integral of the single variable function f from a to b to within a specified relative tol...
static void recover_memory_nested()
Recover only the memory used for the top nested call.
int is_nan(const fvar< T > &x)
Returns 1 if the input's value is NaN and 0 otherwise.
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.
static void start_nested()
Record the current position so that recover_memory_nested() can find it.
double val() const
Return the value of this variable.