Stan Math Library  2.20.0
reverse mode automatic differentiation
precomputed_gradients.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_CORE_PRECOMPUTED_GRADIENTS_HPP
2 #define STAN_MATH_REV_CORE_PRECOMPUTED_GRADIENTS_HPP
3 
7 #include <algorithm>
8 #include <vector>
9 
10 namespace stan {
11 namespace math {
12 
21  protected:
22  const size_t size_;
24  double* gradients_;
25 
26  public:
36  precomputed_gradients_vari(double val, size_t size, vari** varis,
37  double* gradients)
38  : vari(val), size_(size), varis_(varis), gradients_(gradients) {}
39 
51  precomputed_gradients_vari(double val, const std::vector<var>& vars,
52  const std::vector<double>& gradients)
53  : vari(val),
54  size_(vars.size()),
55  varis_(ChainableStack::instance_->memalloc_.alloc_array<vari*>(
56  vars.size())),
57  gradients_(ChainableStack::instance_->memalloc_.alloc_array<double>(
58  vars.size())) {
59  check_consistent_sizes("precomputed_gradients_vari", "vars", vars,
60  "gradients", gradients);
61  for (size_t i = 0; i < vars.size(); ++i)
62  varis_[i] = vars[i].vi_;
63  std::copy(gradients.begin(), gradients.end(), gradients_);
64  }
65 
70  void chain() {
71  for (size_t i = 0; i < size_; ++i)
72  varis_[i]->adj_ += adj_ * gradients_[i];
73  }
74 };
75 
88 inline var precomputed_gradients(double value, const std::vector<var>& operands,
89  const std::vector<double>& gradients) {
90  return var(new precomputed_gradients_vari(value, operands, gradients));
91 }
92 } // namespace math
93 } // namespace stan
94 #endif
The variable implementation base class.
Definition: vari.hpp:30
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
friend class var
Definition: vari.hpp:32
A variable implementation taking a sequence of operands and partial derivatives with respect to the o...
precomputed_gradients_vari(double val, const std::vector< var > &vars, const std::vector< double > &gradients)
Construct a precomputed vari with the specified value, operands, and gradients.
var precomputed_gradients(double value, const std::vector< var > &operands, const std::vector< double > &gradients)
This function returns a var for an expression that has the specified value, vector of operands...
void chain()
Implements the chain rule for this variable, using the prestored operands and gradient.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
This struct always provides access to the autodiff stack using the singleton pattern.
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
precomputed_gradients_vari(double val, size_t size, vari **varis, double *gradients)
Construct a precomputed vari with the specified value, operands, and gradients.
const kernel_cl< in_buffer, out_buffer, int, int > copy("copy", {indexing_helpers, copy_kernel_code})
See the docs for copy() .

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