Stan Math Library
4.9.0
Automatic Differentiation
|
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.
F | Type of function |
EigVec | Type of Eigen vector |
InputIt | must meet the requirements of LegacyInputIterator. |
[in] | f | Function |
[in] | x | Argument to function |
[out] | fx | Function applied to argument |
[out] | first_grad_fx | First element of gradient of function at argument |
[out] | last_grad_fx | Last element of gradient of function at argument |
std::invalid_argument | if the iterator isn't the right size to hold the gradients |
Definition at line 102 of file gradient.hpp.