| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
5 #include <stan/math/rev/core/vari.hpp>
6 #include <stan/math/rev/core/var.hpp>
7 #include <algorithm>
8 #include <vector>
9
10 namespace stan {
11 namespace math {
12
20 class precomputed_gradients_vari : public vari {
21 protected:
22 const size_t size_;
23 vari** varis_;
24 double* gradients_;
25
26 public:
36 precomputed_gradients_vari(double val,
37 size_t size,
38 vari** varis,
39 double* gradients)
40 : vari(val),
41 size_(size),
42 varis_(varis),
43 gradients_(gradients) {
44 }
45
57 precomputed_gradients_vari(double val,
58 const std::vector<var>& vars,
59 const std::vector<double>& gradients)
60 : vari(val),
61 size_(vars.size()),
62 varis_(ChainableStack::memalloc_
63 .alloc_array<vari*>(vars.size())),
64 gradients_(ChainableStack::memalloc_
65 .alloc_array<double>(vars.size())) {
66 check_consistent_sizes("precomputed_gradients_vari",
67 "vars", vars, "gradients", gradients);
68 for (size_t i = 0; i < vars.size(); ++i)
69 varis_[i] = vars[i].vi_;
70 std::copy(gradients.begin(), gradients.end(), gradients_);
71 }
72
77 void chain() {
78 for (size_t i = 0; i < size_; ++i)
79 varis_[i]->adj_ += adj_ * gradients_[i];
80 }
81 };
82
95 inline var precomputed_gradients(const double value,
96 const std::vector<var>& operands,
97 const std::vector<double>& gradients) {
98 return var(new precomputed_gradients_vari(value, operands, gradients));
99 }
100 }
101 }
102 #endif
stan::math::precomputed_gradients
var precomputed_gradients(const 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...
Definition: precomputed_gradients.hpp:95
vari.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::precomputed_gradients_vari::gradients_
double * gradients_
Definition: precomputed_gradients.hpp:24
stan::math::precomputed_gradients_vari::size_
const size_t size_
Definition: precomputed_gradients.hpp:22
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:30
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
stan::math::precomputed_gradients_vari::varis_
vari ** varis_
Definition: precomputed_gradients.hpp:23
stan::math::precomputed_gradients_vari
A variable implementation taking a sequence of operands and partial derivatives with respect to the o...
Definition: precomputed_gradients.hpp:20
stan::math::precomputed_gradients_vari::precomputed_gradients_vari
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.
Definition: precomputed_gradients.hpp:57
var.hpp
stan::math::precomputed_gradients_vari::chain
void chain()
Implements the chain rule for this variable, using the prestored operands and gradient.
Definition: precomputed_gradients.hpp:77
stan::math::size
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
stan::math::check_consistent_sizes
bool check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Return true if the dimension of x1 is consistent with x2.
Definition: check_consistent_sizes.hpp:31
stan::math::AutodiffStackStorage
Definition: autodiffstackstorage.hpp:12
stan::math::vari::adj_
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
stan::math::precomputed_gradients_vari::precomputed_gradients_vari
precomputed_gradients_vari(double val, size_t size, vari **varis, double *gradients)
Construct a precomputed vari with the specified value, operands, and gradients.
Definition: precomputed_gradients.hpp:36
check_consistent_sizes.hpp

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