Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
precomputed_gradients.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__INTERNAL__PRECOMPUTED_GRADIENTS_HPP
2 #define STAN__AGRAD__REV__INTERNAL__PRECOMPUTED_GRADIENTS_HPP
3 
4 #include <iostream>
5 #include <vector>
6 #include <stdexcept>
8 #include <stan/agrad/rev/var.hpp>
9 
10 namespace stan {
11  namespace agrad {
12 
21  protected:
22  std::vector<vari *> varis_;
23  std::vector<double> gradients_;
24 
25  public:
41  precomputed_gradients_vari(const double val,
42  std::vector<vari *>& varis,
43  const std::vector<double>& gradients)
44  : vari(val),
45  varis_(varis),
46  gradients_(gradients) {
47  if (varis_.size() != gradients_.size())
48  throw std::invalid_argument("sizes of varis and gradients do not match");
49  }
50 
58  void chain() {
59  for (size_t n = 0; n < varis_.size(); n++) {
60  varis_[n]->adj_ += adj_ * gradients_[n];
61  }
62  }
63  };
64 
65 
86  var precomputed_gradients(const double value,
87  const std::vector<var>& vars,
88  const std::vector<double>& gradients) {
89  std::vector<vari *> varis;
90  varis.resize(vars.size());
91  for (size_t n = 0; n < vars.size(); n++) {
92  varis[n] = vars[n].vi_;
93  }
94  return var(new precomputed_gradients_vari(value, varis, gradients));
95  }
96  }
97 }
98 #endif
This is a var implementation class that takes precomputed gradient values.
var precomputed_gradients(const double value, const std::vector< var > &vars, const std::vector< double > &gradients)
This function is provided for Stan users that want to compute gradients without using Stan's auto-dif...
void chain()
Implements the chain rule for this variable.
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:40
The variable implementation base class.
Definition: vari.hpp:28
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:27
precomputed_gradients_vari(const double val, std::vector< vari * > &varis, const std::vector< double > &gradients)
Constructs a precomputed_gradients_vari.
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:43

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