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

◆ gradient() [3/3]

template<typename F , typename EigVec , typename InputIt , require_eigen_vector_vt< std::is_arithmetic, EigVec > * = nullptr>
void stan::math::gradient ( const F &  f,
const EigVec &  x,
double &  fx,
InputIt  first_grad_fx,
InputIt  last_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 the object whose data begins at *first_grad_fx and ends at *last_grad_fx. The caller is responsible for ensuring the size of the object pointed to by first_grad_fx matches the size of the argument x.

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
EigVecType of Eigen vector
InputItmust meet the requirements of LegacyInputIterator.
Parameters
[in]fFunction
[in]xArgument to function
[out]fxFunction applied to argument
[out]first_grad_fxFirst element of gradient of function at argument
[out]last_grad_fxLast element of gradient of function at argument
Exceptions
std::invalid_argumentif the iterator isn't the right size to hold the gradients

Definition at line 102 of file gradient.hpp.