Automatic Differentiation
 
Loading...
Searching...
No Matches

◆ gradient() [2/3]

template<typename F >
void stan::math::gradient ( const F &  f,
const Eigen::Matrix< double, Eigen::Dynamic, 1 > &  x,
double &  fx,
Eigen::Matrix< double, Eigen::Dynamic, 1 > &  grad_fx 
)

Calculate the value and the gradient of the specified function at the specified argument.

The functor must implement

var operator()(const Eigen::Matrix<var, Eigen::Dynamic, 1>&)

using only operations that are defined for var. This latter constraint usually requires the functions to be defined in terms of the libraries defined in Stan or in terms of functions with appropriately general namespace imports that eventually depend on functions defined in Stan.

The evaluated gradient is stored into a Eigen::VectorXd named grad_fx.

Time and memory usage is on the order of the size of the fully unfolded expression for the function applied to the argument, independently of dimension.

Template Parameters
FType of function
Parameters
[in]fFunction
[in]xArgument to function
[out]fxFunction applied to argument
[out]grad_fxGradient of function at argument

Definition at line 46 of file gradient.hpp.