1 #ifndef STAN_MATH_FWD_MAT_FUNCTOR_GRADIENT_HPP 2 #define STAN_MATH_FWD_MAT_FUNCTOR_GRADIENT_HPP 38 template <
typename T,
typename F>
39 void gradient(
const F& f,
const Eigen::Matrix<T, Eigen::Dynamic, 1>& x, T& fx,
40 Eigen::Matrix<T, Eigen::Dynamic, 1>& grad_fx) {
41 Eigen::Matrix<fvar<T>, Eigen::Dynamic, 1> x_fvar(x.size());
42 grad_fx.resize(x.size());
43 for (
int i = 0; i < x.size(); ++i) {
44 for (
int k = 0; k < x.size(); ++k)
45 x_fvar(k) =
fvar<T>(x(k), k == i);
49 grad_fx(i) = fx_fvar.
d_;
T d_
The tangent (derivative) of this variable.
T val_
The value of this variable.
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.
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.