gradient.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_FWD_MAT_FUNCTOR_GRADIENT_HPP
#define STAN_MATH_FWD_MAT_FUNCTOR_GRADIENT_HPP
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/mat/fun/Eigen.hpp>
namespace stan {
namespace math {
template <typename T, typename F>
void
gradient(const F& f,
const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
T& fx,
Eigen::Matrix<T, Eigen::Dynamic, 1>& grad_fx) {
Eigen::Matrix<fvar<T>, Eigen::Dynamic, 1> x_fvar(x.size());
grad_fx.resize(x.size());
for (int i = 0; i < x.size(); ++i) {
for (int k = 0; k < x.size(); ++k)
x_fvar(k) = fvar<T>(x(k), k == i);
fvar<T> fx_fvar = f(x_fvar);
if (i == 0) fx = fx_fvar.val_;
grad_fx(i) = fx_fvar.d_;
}
}
}
}
#endif
core.hpp
stan::math::fvar::d_
T d_
Definition: fvar.hpp:16
stan
Definition: log_sum_exp.hpp:8
stan::math::fvar::val_
T val_
Definition: fvar.hpp:15
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:14

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