Stan Math Library  2.10.0
reverse mode automatic differentiation
gevv_vvv_vari.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_CORE_GEVV_VVV_VARI_HPP
2 #define STAN_MATH_REV_CORE_GEVV_VVV_VARI_HPP
3 
7 
8 namespace stan {
9  namespace math {
10 
12  protected:
16  double dotval_;
17  size_t length_;
18  inline static double eval_gevv(const stan::math::var* alpha,
19  const stan::math::var* v1, int stride1,
20  const stan::math::var* v2, int stride2,
21  size_t length, double *dotprod) {
22  double result = 0;
23  for (size_t i = 0; i < length; i++)
24  result += v1[i*stride1].vi_->val_ * v2[i*stride2].vi_->val_;
25  *dotprod = result;
26  return alpha->vi_->val_ * result;
27  }
28 
29  public:
31  const stan::math::var* v1, int stride1,
32  const stan::math::var* v2, int stride2, size_t length) :
33  vari(eval_gevv(alpha, v1, stride1, v2, stride2, length, &dotval_)),
34  length_(length) {
35  alpha_ = alpha->vi_;
36  // TODO(carpenter): replace this with array alloc fun call
37  v1_ = reinterpret_cast<stan::math::vari**>
39  .alloc(2 * length_ * sizeof(stan::math::vari*)));
40  v2_ = v1_ + length_;
41  for (size_t i = 0; i < length_; i++)
42  v1_[i] = v1[i*stride1].vi_;
43  for (size_t i = 0; i < length_; i++)
44  v2_[i] = v2[i*stride2].vi_;
45  }
46  virtual ~gevv_vvv_vari() {}
47  void chain() {
48  const double adj_alpha = adj_ * alpha_->val_;
49  for (size_t i = 0; i < length_; i++) {
50  v1_[i]->adj_ += adj_alpha * v2_[i]->val_;
51  v2_[i]->adj_ += adj_alpha * v1_[i]->val_;
52  }
53  alpha_->adj_ += adj_ * dotval_;
54  }
55  };
56 
57  }
58 }
59 
60 #endif
The variable implementation base class.
Definition: vari.hpp:30
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
static double eval_gevv(const stan::math::var *alpha, const stan::math::var *v1, int stride1, const stan::math::var *v2, int stride2, size_t length, double *dotprod)
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
const double val_
The value of this variable.
Definition: vari.hpp:38
stan::math::vari * alpha_
stan::math::vari ** v1_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:43
gevv_vvv_vari(const stan::math::var *alpha, const stan::math::var *v1, int stride1, const stan::math::var *v2, int stride2, size_t length)
stan::math::vari ** v2_
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...

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