![]() |
Stan Math Library
2.20.0
reverse mode automatic differentiation
|
adj_jac_vari interfaces a user supplied functor with the reverse mode autodiff. More...
#include <adj_jac_apply.hpp>
Public Types | |
using | FReturnType = std::result_of_t< F(decltype(is_var_), decltype(value_of(Targs()))...)> |
Public Member Functions | |
template<int R, int C, typename... Pargs> | |
size_t | count_memory (size_t count, const Eigen::Matrix< var, R, C > &x, const Pargs &... args) |
count_memory returns count (the first argument) + the number of varis used in the second argument + the number of arguments used to encode the variadic tail args. More... | |
template<int R, int C, typename... Pargs> | |
size_t | count_memory (size_t count, const Eigen::Matrix< double, R, C > &x, const Pargs &... args) |
template<typename... Pargs> | |
size_t | count_memory (size_t count, const std::vector< var > &x, const Pargs &... args) |
template<typename... Pargs> | |
size_t | count_memory (size_t count, const std::vector< double > &x, const Pargs &... args) |
template<typename... Pargs> | |
size_t | count_memory (size_t count, const std::vector< int > &x, const Pargs &... args) |
template<typename... Pargs> | |
size_t | count_memory (size_t count, const var &x, const Pargs &... args) |
template<typename... Pargs> | |
size_t | count_memory (size_t count, const double &x, const Pargs &... args) |
template<typename... Pargs> | |
size_t | count_memory (size_t count, const int &x, const Pargs &... args) |
size_t | count_memory (size_t count) |
template<int R, int C, typename... Pargs> | |
void | prepare_x_vis (const Eigen::Matrix< var, R, C > &x, const Pargs &... args) |
prepare_x_vis populates x_vis_ with the varis from each of its input arguments. More... | |
template<int R, int C, typename... Pargs> | |
void | prepare_x_vis (const Eigen::Matrix< double, R, C > &x, const Pargs &... args) |
template<typename... Pargs> | |
void | prepare_x_vis (const std::vector< var > &x, const Pargs &... args) |
template<typename... Pargs> | |
void | prepare_x_vis (const std::vector< double > &x, const Pargs &... args) |
template<typename... Pargs> | |
void | prepare_x_vis (const std::vector< int > &x, const Pargs &... args) |
template<typename... Pargs> | |
void | prepare_x_vis (const var &x, const Pargs &... args) |
template<typename... Pargs> | |
void | prepare_x_vis (const double &x, const Pargs &... args) |
template<typename... Pargs> | |
void | prepare_x_vis (const int &x, const Pargs &... args) |
adj_jac_vari () | |
Initializes is_var_ with true if the scalar type in each argument is a var (and false if not) More... | |
var | build_return_varis_and_vars (const double &val_y) |
Return a var with a new vari holding the given value. More... | |
std::vector< var > | build_return_varis_and_vars (const std::vector< double > &val_y) |
Return a std::vector of vars created from newly allocated varis initialized with the values of val_y. More... | |
template<int R, int C> | |
Eigen::Matrix< var, R, C > | build_return_varis_and_vars (const Eigen::Matrix< double, R, C > &val_y) |
Return an Eigen::Matrix of vars created from newly allocated varis initialized with the values of val_y. More... | |
void | prepare_x_vis () |
auto | operator() (const Targs &... args) |
The adj_jac_vari functor. More... | |
template<int R, int C, typename... Pargs> | |
void | accumulate_adjoints (const Eigen::Matrix< double, R, C > &y_adj_jac, const Pargs &... args) |
Accumulate, if necessary, the values of y_adj_jac into the adjoints of the varis pointed to by the appropriate elements of x_vis_. More... | |
template<typename... Pargs> | |
void | accumulate_adjoints (const std::vector< double > &y_adj_jac, const Pargs &... args) |
Accumulate, if necessary, the values of y_adj_jac into the adjoints of the varis pointed to by the appropriate elements of x_vis_. More... | |
template<typename... Pargs> | |
void | accumulate_adjoints (const std::vector< int > &y_adj_jac, const Pargs &... args) |
Recursively call accumulate_adjoints with args. More... | |
template<typename... Pargs> | |
void | accumulate_adjoints (const double &y_adj_jac, const Pargs &... args) |
Accumulate, if necessary, the value of y_adj_jac into the adjoint of the vari pointed to by the appropriate element of x_vis_. More... | |
template<typename... Pargs> | |
void | accumulate_adjoints (const int &y_adj_jac, const Pargs &... args) |
Recursively call accumulate_adjoints with args. More... | |
void | accumulate_adjoints () |
void | chain () |
Propagate the adjoints at the output varis (y_vi_) back to the input varis (x_vis_) by: More... | |
![]() | |
vari (double x) | |
Construct a variable implementation from a value. More... | |
vari (double x, bool stacked) | |
virtual | ~vari () |
Throw an illegal argument exception. More... | |
void | init_dependent () |
Initialize the adjoint for this (dependent) variable to 1. More... | |
void | set_zero_adjoint () |
Set the adjoint value of this variable to 0. More... | |
Public Attributes | |
std::array< bool, sizeof...(Targs)> | is_var_ |
F | f_ |
std::array< int, sizeof...(Targs)> | offsets_ |
vari ** | x_vis_ |
std::array< int, internal::compute_dims< FReturnType >::value > | M_ |
vari ** | y_vi_ |
![]() | |
const double | val_ |
The value of this variable. More... | |
double | adj_ |
The adjoint of this variable, which is the partial derivative of this variable with respect to the root variable. More... | |
Additional Inherited Members | |
![]() | |
static void * | operator new (size_t nbytes) |
Allocate memory from the underlying memory pool. More... | |
static void | operator delete (void *) |
Delete a pointer from the underlying memory pool. More... | |
adj_jac_vari interfaces a user supplied functor with the reverse mode autodiff.
It allows someone to implement functions with custom reverse mode autodiff without having to deal with autodiff types.
The requirements on the functor F are described in the documentation for adj_jac_apply
Targs (the input argument types) can be any mix of double, var, or Eigen::Matrices with double or var scalar components
F | class of functor |
Targs | Types of arguments |
Definition at line 147 of file adj_jac_apply.hpp.
using stan::math::adj_jac_vari< F, Targs >::FReturnType = std::result_of_t<F(decltype(is_var_), decltype(value_of(Targs()))...)> |
Definition at line 150 of file adj_jac_apply.hpp.
|
inline |
Initializes is_var_ with true if the scalar type in each argument is a var (and false if not)
Definition at line 319 of file adj_jac_apply.hpp.
|
inline |
Accumulate, if necessary, the values of y_adj_jac into the adjoints of the varis pointed to by the appropriate elements of x_vis_.
Recursively calls accumulate_adjoints on the rest of the arguments.
R | Eigen Matrix row type |
C | Eigen Matrix column type |
Pargs | Types of the rest of adjoints to accumulate |
y_adj_jac | set of values to be accumulated in adjoints |
args | the rest of the arguments (that will be iterated through recursively) |
Definition at line 430 of file adj_jac_apply.hpp.
|
inline |
Accumulate, if necessary, the values of y_adj_jac into the adjoints of the varis pointed to by the appropriate elements of x_vis_.
Recursively calls accumulate_adjoints on the rest of the arguments.
Pargs | Types of the rest of adjoints to accumulate |
y_adj_jac | set of values to be accumulated in adjoints |
args | the rest of the arguments (that will be iterated through recursively) |
Definition at line 454 of file adj_jac_apply.hpp.
|
inline |
Recursively call accumulate_adjoints with args.
There are no adjoints to accumulate for std::vector<int> arguments.
Pargs | Types of the rest of adjoints to accumulate |
y_adj_jac | ignored |
args | the rest of the arguments (that will be iterated through recursively) |
Definition at line 475 of file adj_jac_apply.hpp.
|
inline |
Accumulate, if necessary, the value of y_adj_jac into the adjoint of the vari pointed to by the appropriate element of x_vis_.
Recursively calls accumulate_adjoints on the rest of the arguments.
Pargs | Types of the rest of adjoints to accumulate |
y_adj_jac | next set of adjoints to be accumulated |
args | the rest of the arguments (that will be iterated through recursively) |
Definition at line 492 of file adj_jac_apply.hpp.
|
inline |
Recursively call accumulate_adjoints with args.
There are no adjoints to accumulate for int arguments.
Pargs | Types of the rest of adjoints to accumulate |
y_adj_jac | ignored |
args | the rest of the arguments (that will be iterated through recursively) |
Definition at line 511 of file adj_jac_apply.hpp.
|
inline |
Definition at line 515 of file adj_jac_apply.hpp.
|
inline |
Return a var with a new vari holding the given value.
val_y | output of F::operator() |
Definition at line 332 of file adj_jac_apply.hpp.
|
inline |
Return a std::vector of vars created from newly allocated varis initialized with the values of val_y.
val_y | output of F::operator() |
Definition at line 346 of file adj_jac_apply.hpp.
|
inline |
Return an Eigen::Matrix of vars created from newly allocated varis initialized with the values of val_y.
The shape of the new matrix comes from M_
val_y | output of F::operator() |
Definition at line 372 of file adj_jac_apply.hpp.
|
inlinevirtual |
Propagate the adjoints at the output varis (y_vi_) back to the input varis (x_vis_) by:
This operation may be called multiple times during the life of the vari
Reimplemented from stan::math::vari.
Definition at line 528 of file adj_jac_apply.hpp.
|
inline |
count_memory returns count (the first argument) + the number of varis used in the second argument + the number of arguments used to encode the variadic tail args.
The adj_jac_vari constructor uses this to figure out how much space to allocate in x_vis_.
The array offsets_ is populated with values to indicate where in x_vis_ the vari pointers for each argument will be stored.
Each of the arguments can be an Eigen::Matrix with var or double scalar types, a std::vector with var, double, or int scalar types, or a var, a double, or an int.
R | Eigen Matrix row type |
C | Eigen Matrix column type |
Pargs | Types of rest of arguments |
count | rolling count of number of varis that must be allocated |
x | next argument to have its varis counted |
args | the rest of the arguments (that will be iterated through recursively) |
Definition at line 182 of file adj_jac_apply.hpp.
|
inline |
Definition at line 191 of file adj_jac_apply.hpp.
|
inline |
Definition at line 199 of file adj_jac_apply.hpp.
|
inline |
Definition at line 208 of file adj_jac_apply.hpp.
|
inline |
Definition at line 216 of file adj_jac_apply.hpp.
|
inline |
Definition at line 224 of file adj_jac_apply.hpp.
|
inline |
Definition at line 232 of file adj_jac_apply.hpp.
|
inline |
Definition at line 239 of file adj_jac_apply.hpp.
|
inline |
Definition at line 245 of file adj_jac_apply.hpp.
|
inline |
The adj_jac_vari functor.
Each of the arguments can be an Eigen::Matrix with var or double scalar types, a std::vector with var, double, or int scalar types, or a var, a double, or an int.
args | Input arguments |
Definition at line 407 of file adj_jac_apply.hpp.
|
inline |
prepare_x_vis populates x_vis_ with the varis from each of its input arguments.
The vari pointers for argument n are copied into x_vis_ at the index starting at offsets_[n]. For Eigen::Matrix types, this copying is done in with column major ordering.
Each of the arguments can be an Eigen::Matrix with var or double scalar types, a std::vector with var, double, or int scalar types, or a var, a double, or an int.
R | Eigen Matrix row type |
C | Eigen Matrix column type |
Pargs | Types of the rest of the arguments to be processed |
x | next argument to have its vari pointers copied if necessary |
args | the rest of the arguments (that will be iterated through recursively) |
Definition at line 265 of file adj_jac_apply.hpp.
|
inline |
Definition at line 274 of file adj_jac_apply.hpp.
|
inline |
Definition at line 280 of file adj_jac_apply.hpp.
|
inline |
Definition at line 289 of file adj_jac_apply.hpp.
|
inline |
Definition at line 294 of file adj_jac_apply.hpp.
|
inline |
Definition at line 299 of file adj_jac_apply.hpp.
|
inline |
Definition at line 306 of file adj_jac_apply.hpp.
|
inline |
Definition at line 311 of file adj_jac_apply.hpp.
|
inline |
Definition at line 388 of file adj_jac_apply.hpp.
F stan::math::adj_jac_vari< F, Targs >::f_ |
Definition at line 152 of file adj_jac_apply.hpp.
std::array<bool, sizeof...(Targs)> stan::math::adj_jac_vari< F, Targs >::is_var_ |
Definition at line 148 of file adj_jac_apply.hpp.
std::array<int, internal::compute_dims<FReturnType>::value> stan::math::adj_jac_vari< F, Targs >::M_ |
Definition at line 155 of file adj_jac_apply.hpp.
std::array<int, sizeof...(Targs)> stan::math::adj_jac_vari< F, Targs >::offsets_ |
Definition at line 153 of file adj_jac_apply.hpp.
vari** stan::math::adj_jac_vari< F, Targs >::x_vis_ |
Definition at line 154 of file adj_jac_apply.hpp.
vari** stan::math::adj_jac_vari< F, Targs >::y_vi_ |
Definition at line 156 of file adj_jac_apply.hpp.