All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
precomputed_gradients.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_CORE_PRECOMPUTED_GRADIENTS_HPP
#define STAN_MATH_REV_CORE_PRECOMPUTED_GRADIENTS_HPP
#include <stan/math/rev/core/vari.hpp>
#include <stan/math/rev/core/var.hpp>
#include <algorithm>
#include <vector>
#include <stdexcept>
namespace stan {
namespace math {
class precomputed_gradients_vari : public vari {
protected:
const size_t size_;
vari** varis_;
double* gradients_;
public:
precomputed_gradients_vari(double val,
size_t size,
vari** varis,
double* gradients)
: vari(val),
size_(size),
varis_(varis),
gradients_(gradients) {
}
precomputed_gradients_vari(double val,
const std::vector<var>& vars,
const std::vector<double>& gradients)
: vari(val),
size_(vars.size()),
varis_(ChainableStack::memalloc_
.alloc_array<vari*>(vars.size())),
gradients_(ChainableStack::memalloc_
.alloc_array<double>(vars.size())) {
if (vars.size() != gradients.size())
throw std::invalid_argument("sizes of vars and gradients"
" do not match");
for (size_t i = 0; i < vars.size(); ++i)
varis_[i] = vars[i].vi_;
std::copy(gradients.begin(), gradients.end(), gradients_);
}
void chain() {
for (size_t i = 0; i < size_; ++i)
varis_[i]->adj_ += adj_ * gradients_[i];
}
};
var precomputed_gradients(const double value,
const std::vector<var>& operands,
const std::vector<double>& gradients) {
return var(new precomputed_gradients_vari(value, operands, gradients));
}
}
}
#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:98
vari.hpp
stan::math::precomputed_gradients_vari::gradients_
double * gradients_
Definition: precomputed_gradients.hpp:25
stan::math::precomputed_gradients_vari::size_
const size_t size_
Definition: precomputed_gradients.hpp:23
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:28
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
stan::math::precomputed_gradients_vari::varis_
vari ** varis_
Definition: precomputed_gradients.hpp:24
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:21
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:58
stan::math::invalid_argument
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw an invalid_argument exception with a consistently formatted message.
Definition: invalid_argument.hpp:31
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:79
stan::math::size
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
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:42
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:37

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