| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

gradient.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUNCTOR_GRADIENT_HPP
2 #define STAN_MATH_FWD_MAT_FUNCTOR_GRADIENT_HPP
3
4 #include <stan/math/fwd/core.hpp>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6
7 namespace stan {
8 namespace math {
9
38 template <typename T, typename F>
39 void
40 gradient(const F& f,
41 const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
42 T& fx,
43 Eigen::Matrix<T, Eigen::Dynamic, 1>& grad_fx) {
44 Eigen::Matrix<fvar<T>, Eigen::Dynamic, 1> x_fvar(x.size());
45 grad_fx.resize(x.size());
46 for (int i = 0; i < x.size(); ++i) {
47 for (int k = 0; k < x.size(); ++k)
48 x_fvar(k) = fvar<T>(x(k), k == i);
49 fvar<T> fx_fvar = f(x_fvar);
50 if (i == 0) fx = fx_fvar.val_;
51 grad_fx(i) = fx_fvar.d_;
52 }
53 }
54
55 }
56 }
57 #endif
core.hpp
stan::math::fvar::d_
T d_
Definition: fvar.hpp:14
stan
Definition: log_sum_exp.hpp:8
stan::math::fvar::val_
T val_
Definition: fvar.hpp:13
Eigen.hpp
stan::math::gradient
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
stan::math::fvar
Definition: fvar.hpp:12

[ Stan Home Page ] © 2011–2016, Stan Development Team.