Stan Math Library  2.20.0
reverse mode automatic differentiation
Public Types | Public Member Functions | Public Attributes | List of all members
stan::math::adj_jac_vari< F, Targs > Struct Template Reference

adj_jac_vari interfaces a user supplied functor with the reverse mode autodiff. More...

#include <adj_jac_apply.hpp>

Inheritance diagram for stan::math::adj_jac_vari< F, Targs >:
stan::math::vari

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< varbuild_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...
 
- Public Member Functions inherited from stan::math::vari
 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_
 
std::array< int, sizeof...(Targs)> offsets_
 
vari ** x_vis_
 
std::array< int, internal::compute_dims< FReturnType >::value > M_
 
vari ** y_vi_
 
- Public Attributes inherited from stan::math::vari
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 Public Member Functions inherited from stan::math::vari
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...
 

Detailed Description

template<typename F, typename... Targs>
struct stan::math::adj_jac_vari< F, Targs >

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

Template Parameters
Fclass of functor
TargsTypes of arguments

Definition at line 147 of file adj_jac_apply.hpp.

Member Typedef Documentation

◆ FReturnType

template<typename F , typename... Targs>
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.

Constructor & Destructor Documentation

◆ adj_jac_vari()

template<typename F , typename... Targs>
stan::math::adj_jac_vari< F, Targs >::adj_jac_vari ( )
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.

Member Function Documentation

◆ accumulate_adjoints() [1/6]

template<typename F , typename... Targs>
template<int R, int C, typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::accumulate_adjoints ( const Eigen::Matrix< double, R, C > &  y_adj_jac,
const Pargs &...  args 
)
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.

Template Parameters
REigen Matrix row type
CEigen Matrix column type
PargsTypes of the rest of adjoints to accumulate
Parameters
y_adj_jacset of values to be accumulated in adjoints
argsthe rest of the arguments (that will be iterated through recursively)

Definition at line 430 of file adj_jac_apply.hpp.

◆ accumulate_adjoints() [2/6]

template<typename F , typename... Targs>
template<typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::accumulate_adjoints ( const std::vector< double > &  y_adj_jac,
const Pargs &...  args 
)
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.

Template Parameters
PargsTypes of the rest of adjoints to accumulate
Parameters
y_adj_jacset of values to be accumulated in adjoints
argsthe rest of the arguments (that will be iterated through recursively)

Definition at line 454 of file adj_jac_apply.hpp.

◆ accumulate_adjoints() [3/6]

template<typename F , typename... Targs>
template<typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::accumulate_adjoints ( const std::vector< int > &  y_adj_jac,
const Pargs &...  args 
)
inline

Recursively call accumulate_adjoints with args.

There are no adjoints to accumulate for std::vector<int> arguments.

Template Parameters
PargsTypes of the rest of adjoints to accumulate
Parameters
y_adj_jacignored
argsthe rest of the arguments (that will be iterated through recursively)

Definition at line 475 of file adj_jac_apply.hpp.

◆ accumulate_adjoints() [4/6]

template<typename F , typename... Targs>
template<typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::accumulate_adjoints ( const double &  y_adj_jac,
const Pargs &...  args 
)
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.

Template Parameters
PargsTypes of the rest of adjoints to accumulate
Parameters
y_adj_jacnext set of adjoints to be accumulated
argsthe rest of the arguments (that will be iterated through recursively)

Definition at line 492 of file adj_jac_apply.hpp.

◆ accumulate_adjoints() [5/6]

template<typename F , typename... Targs>
template<typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::accumulate_adjoints ( const int &  y_adj_jac,
const Pargs &...  args 
)
inline

Recursively call accumulate_adjoints with args.

There are no adjoints to accumulate for int arguments.

Template Parameters
PargsTypes of the rest of adjoints to accumulate
Parameters
y_adj_jacignored
argsthe rest of the arguments (that will be iterated through recursively)

Definition at line 511 of file adj_jac_apply.hpp.

◆ accumulate_adjoints() [6/6]

template<typename F , typename... Targs>
void stan::math::adj_jac_vari< F, Targs >::accumulate_adjoints ( )
inline

Definition at line 515 of file adj_jac_apply.hpp.

◆ build_return_varis_and_vars() [1/3]

template<typename F , typename... Targs>
var stan::math::adj_jac_vari< F, Targs >::build_return_varis_and_vars ( const double &  val_y)
inline

Return a var with a new vari holding the given value.

Parameters
val_youtput of F::operator()
Returns
var

Definition at line 332 of file adj_jac_apply.hpp.

◆ build_return_varis_and_vars() [2/3]

template<typename F , typename... Targs>
std::vector<var> stan::math::adj_jac_vari< F, Targs >::build_return_varis_and_vars ( const std::vector< double > &  val_y)
inline

Return a std::vector of vars created from newly allocated varis initialized with the values of val_y.

Parameters
val_youtput of F::operator()
Returns
std::vector of vars

Definition at line 346 of file adj_jac_apply.hpp.

◆ build_return_varis_and_vars() [3/3]

template<typename F , typename... Targs>
template<int R, int C>
Eigen::Matrix<var, R, C> stan::math::adj_jac_vari< F, Targs >::build_return_varis_and_vars ( const Eigen::Matrix< double, R, C > &  val_y)
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_

Template Parameters
REigen row type
CEigen column type
Parameters
val_youtput of F::operator()
Returns
Eigen::Matrix of vars

Definition at line 372 of file adj_jac_apply.hpp.

◆ chain()

template<typename F , typename... Targs>
void stan::math::adj_jac_vari< F, Targs >::chain ( )
inlinevirtual

Propagate the adjoints at the output varis (y_vi_) back to the input varis (x_vis_) by:

  1. packing the adjoints in an appropriate container using build_y_adj
  2. using the multiply_adjoint_jacobian function of the user defined functor to compute what the adjoints on x_vis_ should be
  3. accumulating the adjoints into the varis pointed to by elements of x_vis_ using accumulate_adjoints

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.

◆ count_memory() [1/9]

template<typename F , typename... Targs>
template<int R, int C, typename... Pargs>
size_t stan::math::adj_jac_vari< F, Targs >::count_memory ( size_t  count,
const Eigen::Matrix< var, R, C > &  x,
const Pargs &...  args 
)
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.

Template Parameters
REigen Matrix row type
CEigen Matrix column type
PargsTypes of rest of arguments
Parameters
countrolling count of number of varis that must be allocated
xnext argument to have its varis counted
argsthe rest of the arguments (that will be iterated through recursively)

Definition at line 182 of file adj_jac_apply.hpp.

◆ count_memory() [2/9]

template<typename F , typename... Targs>
template<int R, int C, typename... Pargs>
size_t stan::math::adj_jac_vari< F, Targs >::count_memory ( size_t  count,
const Eigen::Matrix< double, R, C > &  x,
const Pargs &...  args 
)
inline

Definition at line 191 of file adj_jac_apply.hpp.

◆ count_memory() [3/9]

template<typename F , typename... Targs>
template<typename... Pargs>
size_t stan::math::adj_jac_vari< F, Targs >::count_memory ( size_t  count,
const std::vector< var > &  x,
const Pargs &...  args 
)
inline

Definition at line 199 of file adj_jac_apply.hpp.

◆ count_memory() [4/9]

template<typename F , typename... Targs>
template<typename... Pargs>
size_t stan::math::adj_jac_vari< F, Targs >::count_memory ( size_t  count,
const std::vector< double > &  x,
const Pargs &...  args 
)
inline

Definition at line 208 of file adj_jac_apply.hpp.

◆ count_memory() [5/9]

template<typename F , typename... Targs>
template<typename... Pargs>
size_t stan::math::adj_jac_vari< F, Targs >::count_memory ( size_t  count,
const std::vector< int > &  x,
const Pargs &...  args 
)
inline

Definition at line 216 of file adj_jac_apply.hpp.

◆ count_memory() [6/9]

template<typename F , typename... Targs>
template<typename... Pargs>
size_t stan::math::adj_jac_vari< F, Targs >::count_memory ( size_t  count,
const var x,
const Pargs &...  args 
)
inline

Definition at line 224 of file adj_jac_apply.hpp.

◆ count_memory() [7/9]

template<typename F , typename... Targs>
template<typename... Pargs>
size_t stan::math::adj_jac_vari< F, Targs >::count_memory ( size_t  count,
const double &  x,
const Pargs &...  args 
)
inline

Definition at line 232 of file adj_jac_apply.hpp.

◆ count_memory() [8/9]

template<typename F , typename... Targs>
template<typename... Pargs>
size_t stan::math::adj_jac_vari< F, Targs >::count_memory ( size_t  count,
const int &  x,
const Pargs &...  args 
)
inline

Definition at line 239 of file adj_jac_apply.hpp.

◆ count_memory() [9/9]

template<typename F , typename... Targs>
size_t stan::math::adj_jac_vari< F, Targs >::count_memory ( size_t  count)
inline

Definition at line 245 of file adj_jac_apply.hpp.

◆ operator()()

template<typename F , typename... Targs>
auto stan::math::adj_jac_vari< F, Targs >::operator() ( const Targs &...  args)
inline

The adj_jac_vari functor.

  1. Initializes an instance of the user defined functor F
  2. Calls operator() on the F instance with the double values from the input args
  3. Saves copies of the varis pointed to by the input vars for subsequent calls to chain
  4. Calls build_return_varis_and_vars to construct the appropriate output data structure of vars

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.

Parameters
argsInput arguments
Returns
Output of f_ as vars

Definition at line 407 of file adj_jac_apply.hpp.

◆ prepare_x_vis() [1/9]

template<typename F , typename... Targs>
template<int R, int C, typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::prepare_x_vis ( const Eigen::Matrix< var, R, C > &  x,
const Pargs &...  args 
)
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.

Template Parameters
REigen Matrix row type
CEigen Matrix column type
PargsTypes of the rest of the arguments to be processed
Parameters
xnext argument to have its vari pointers copied if necessary
argsthe rest of the arguments (that will be iterated through recursively)

Definition at line 265 of file adj_jac_apply.hpp.

◆ prepare_x_vis() [2/9]

template<typename F , typename... Targs>
template<int R, int C, typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::prepare_x_vis ( const Eigen::Matrix< double, R, C > &  x,
const Pargs &...  args 
)
inline

Definition at line 274 of file adj_jac_apply.hpp.

◆ prepare_x_vis() [3/9]

template<typename F , typename... Targs>
template<typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::prepare_x_vis ( const std::vector< var > &  x,
const Pargs &...  args 
)
inline

Definition at line 280 of file adj_jac_apply.hpp.

◆ prepare_x_vis() [4/9]

template<typename F , typename... Targs>
template<typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::prepare_x_vis ( const std::vector< double > &  x,
const Pargs &...  args 
)
inline

Definition at line 289 of file adj_jac_apply.hpp.

◆ prepare_x_vis() [5/9]

template<typename F , typename... Targs>
template<typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::prepare_x_vis ( const std::vector< int > &  x,
const Pargs &...  args 
)
inline

Definition at line 294 of file adj_jac_apply.hpp.

◆ prepare_x_vis() [6/9]

template<typename F , typename... Targs>
template<typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::prepare_x_vis ( const var x,
const Pargs &...  args 
)
inline

Definition at line 299 of file adj_jac_apply.hpp.

◆ prepare_x_vis() [7/9]

template<typename F , typename... Targs>
template<typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::prepare_x_vis ( const double &  x,
const Pargs &...  args 
)
inline

Definition at line 306 of file adj_jac_apply.hpp.

◆ prepare_x_vis() [8/9]

template<typename F , typename... Targs>
template<typename... Pargs>
void stan::math::adj_jac_vari< F, Targs >::prepare_x_vis ( const int &  x,
const Pargs &...  args 
)
inline

Definition at line 311 of file adj_jac_apply.hpp.

◆ prepare_x_vis() [9/9]

template<typename F , typename... Targs>
void stan::math::adj_jac_vari< F, Targs >::prepare_x_vis ( )
inline

Definition at line 388 of file adj_jac_apply.hpp.

Member Data Documentation

◆ f_

template<typename F , typename... Targs>
F stan::math::adj_jac_vari< F, Targs >::f_

Definition at line 152 of file adj_jac_apply.hpp.

◆ is_var_

template<typename F , typename... Targs>
std::array<bool, sizeof...(Targs)> stan::math::adj_jac_vari< F, Targs >::is_var_

Definition at line 148 of file adj_jac_apply.hpp.

◆ M_

template<typename F , typename... Targs>
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.

◆ offsets_

template<typename F , typename... Targs>
std::array<int, sizeof...(Targs)> stan::math::adj_jac_vari< F, Targs >::offsets_

Definition at line 153 of file adj_jac_apply.hpp.

◆ x_vis_

template<typename F , typename... Targs>
vari** stan::math::adj_jac_vari< F, Targs >::x_vis_

Definition at line 154 of file adj_jac_apply.hpp.

◆ y_vi_

template<typename F , typename... Targs>
vari** stan::math::adj_jac_vari< F, Targs >::y_vi_

Definition at line 156 of file adj_jac_apply.hpp.


The documentation for this struct was generated from the following file:

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