Automatic Differentiation
 
Loading...
Searching...
No Matches
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
8namespace stan {
9namespace math {
10
11class gevv_vvv_vari : public vari {
12 protected:
16 double dotval_;
17 size_t length_;
18 inline static double eval_gevv(const var* alpha, const var* v1, int stride1,
19 const var* v2, int stride2, size_t length,
20 double* dotprod) {
21 double result = 0;
22 for (size_t i = 0; i < length; i++) {
23 result += v1[i * stride1].vi_->val_ * v2[i * stride2].vi_->val_;
24 }
25 *dotprod = result;
26 return alpha->vi_->val_ * result;
27 }
28
29 public:
30 gevv_vvv_vari(const var* alpha, const var* v1, int stride1, const var* v2,
31 int stride2, size_t length)
32 : vari(eval_gevv(alpha, v1, stride1, v2, stride2, length, &dotval_)),
33 length_(length) {
34 alpha_ = alpha->vi_;
35 // TODO(carpenter): replace this with array alloc fun call
36 v1_ = reinterpret_cast<vari**>(ChainableStack::instance_->memalloc_.alloc(
37 2 * length_ * sizeof(vari*)));
38 v2_ = v1_ + length_;
39 for (size_t i = 0; i < length_; i++) {
40 v1_[i] = v1[i * stride1].vi_;
41 }
42 for (size_t i = 0; i < length_; i++) {
43 v2_[i] = v2[i * stride2].vi_;
44 }
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} // namespace math
58} // namespace stan
59#endif
gevv_vvv_vari(const var *alpha, const var *v1, int stride1, const var *v2, int stride2, size_t length)
static double eval_gevv(const var *alpha, const var *v1, int stride1, const var *v2, int stride2, size_t length, double *dotprod)
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
static thread_local AutodiffStackStorage * instance_