Automatic Differentiation
 
Loading...
Searching...
No Matches
gradient.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUNCTOR_GRADIENT_HPP
2#define STAN_MATH_FWD_FUNCTOR_GRADIENT_HPP
3
6
7namespace stan {
8namespace math {
9
39template <typename T, typename F>
40void gradient(const F& f, const Eigen::Matrix<T, Eigen::Dynamic, 1>& x, T& fx,
41 Eigen::Matrix<T, Eigen::Dynamic, 1>& grad_fx) {
42 Eigen::Matrix<fvar<T>, Eigen::Dynamic, 1> x_fvar(x.size());
43 grad_fx.resize(x.size());
44 for (int i = 0; i < x.size(); ++i) {
45 for (int k = 0; k < x.size(); ++k) {
46 x_fvar(k) = fvar<T>(x(k), k == i);
47 }
48 fvar<T> fx_fvar = f(x_fvar);
49 if (i == 0) {
50 fx = fx_fvar.val_;
51 }
52 grad_fx(i) = fx_fvar.d_;
53 }
54}
55
56} // namespace math
57} // namespace stan
58#endif
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.
Definition gradient.hpp:40
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Scalar val_
The value of this variable.
Definition fvar.hpp:49
Scalar d_
The tangent (derivative) of this variable.
Definition fvar.hpp:61
This template class represents scalars used in forward-mode automatic differentiation,...
Definition fvar.hpp:40