Stan Math Library  2.6.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
dot_self.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_DOT_SELF_HPP
2 #define STAN_MATH_REV_MAT_FUN_DOT_SELF_HPP
3 
7 #include <stan/math/rev/core.hpp>
9 #include <vector>
10 
11 namespace stan {
12  namespace math {
13 
14  namespace {
15  class dot_self_vari : public vari {
16  protected:
17  vari** v_;
18  size_t size_;
19 
20  public:
21  dot_self_vari(vari** v, size_t size)
22  : vari(var_dot_self(v, size)),
23  v_(v),
24  size_(size) {
25  }
26  template<typename Derived>
27  explicit dot_self_vari(const Eigen::DenseBase<Derived> &v) :
28  vari(var_dot_self(v)), size_(v.size()) {
29  v_ = reinterpret_cast<vari**>(ChainableStack::memalloc_
30  .alloc(size_*sizeof(vari*)));
31  for (size_t i = 0; i < size_; i++)
32  v_[i] = v[i].vi_;
33  }
34  template <int R, int C>
35  explicit dot_self_vari(const Eigen::Matrix<var, R, C>& v) :
36  vari(var_dot_self(v)), size_(v.size()) {
37  v_ = reinterpret_cast<vari**>
38  (ChainableStack::memalloc_.alloc(size_ * sizeof(vari*)));
39  for (size_t i = 0; i < size_; ++i)
40  v_[i] = v(i).vi_;
41  }
42  inline static double square(double x) { return x * x; }
43  inline static double var_dot_self(vari** v, size_t size) {
44  double sum = 0.0;
45  for (size_t i = 0; i < size; ++i)
46  sum += square(v[i]->val_);
47  return sum;
48  }
49  template<typename Derived>
50  double var_dot_self(const Eigen::DenseBase<Derived> &v) {
51  double sum = 0.0;
52  for (int i = 0; i < v.size(); ++i)
53  sum += square(v(i).vi_->val_);
54  return sum;
55  }
56  template <int R, int C>
57  inline static double var_dot_self(const Eigen::Matrix<var, R, C> &v) {
58  double sum = 0.0;
59  for (int i = 0; i < v.size(); ++i)
60  sum += square(v(i).vi_->val_);
61  return sum;
62  }
63  virtual void chain() {
64  for (size_t i = 0; i < size_; ++i)
65  v_[i]->adj_ += adj_ * 2.0 * v_[i]->val_;
66  }
67  };
68  }
79  template<int R, int C>
80  inline var dot_self(const Eigen::Matrix<var, R, C>& v) {
81  stan::math::check_vector("dot_self", "v", v);
82  return var(new dot_self_vari(v));
83  }
84 
85  }
86 }
87 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
bool check_vector(const char *function, const char *name, const Eigen::Matrix< T, R, C > &x)
Return true if the matrix is either a row vector or column vector.
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:16
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:15
size_t size_
Definition: dot_self.hpp:18
vari ** v_
Definition: dot_self.hpp:17
int size(const std::vector< T > &x)
Definition: size.hpp:11
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...

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