1#ifndef STAN_MATH_PRIM_FUNCTOR_FINITE_DIFF_GRADIENT_AUTO_HPP
2#define STAN_MATH_PRIM_FUNCTOR_FINITE_DIFF_GRADIENT_AUTO_HPP
49template <
typename F,
typename VectorT,
typename GradVectorT,
50 typename ScalarT = return_type_t<VectorT>>
52 GradVectorT& grad_fx) {
53 using EigT = Eigen::Matrix<ScalarT, -1, 1>;
54 static constexpr int h_scale[6] = {3, 2, 1, -3, -2, -1};
55 static constexpr int mults[6] = {1, -9, 45, -1, 9, -45};
58 grad_fx.resize(x.size());
59 Eigen::Map<EigT> grad_map(grad_fx.data(), grad_fx.size());
61 grad_map = EigT::NullaryExpr(x.size(), [&f, &x](Eigen::Index i) {
62 double h = finite_diff_stepsize(value_of_rec(x[i]));
64 for (int j = 0; j < 6; ++j) {
66 = EigT::NullaryExpr(x.size(), [&x, &i, &h, &j](Eigen::Index k) {
67 return k == i ? x[i] + h * h_scale[j] : x[k];
69 delta_f += f(std::move(x_temp)) * mults[j];
71 return delta_f / (60 * h);
void finite_diff_gradient_auto(const F &f, VectorT &&x, ScalarT &fx, GradVectorT &grad_fx)
Calculate the value and the gradient of the specified function at the specified argument using finite...
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...