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)), v_(v), size_(size) {}
23  template <typename Derived>
24  explicit dot_self_vari(const Eigen::DenseBase<Derived>& v)
25  : vari(var_dot_self(v)), size_(v.size()) {
26  v_ = reinterpret_cast<vari**>(
27  ChainableStack::instance().memalloc_.alloc(size_ * sizeof(vari*)));
28  for (size_t i = 0; i < size_; i++)
29  v_[i] = v[i].vi_;
30  }
31  template <int R, int C>
32  explicit dot_self_vari(const Eigen::Matrix<var, R, C>& v)
33  : vari(var_dot_self(v)), size_(v.size()) {
34  v_ = reinterpret_cast<vari**>(
35  ChainableStack::instance().memalloc_.alloc(size_ * sizeof(vari*)));
36  for (size_t i = 0; i < size_; ++i)
37  v_[i] = v(i).vi_;
38  }
39  inline static double square(double x) { return x * x; }
40  inline static double var_dot_self(vari** v, size_t size) {
41  double sum = 0.0;
42  for (size_t i = 0; i < size; ++i)
43  sum += square(v[i]->val_);
44  return sum;
45  }
46  template <typename Derived>
47  double var_dot_self(const Eigen::DenseBase<Derived>& v) {
48  double sum = 0.0;
49  for (int i = 0; i < v.size(); ++i)
50  sum += square(v(i).vi_->val_);
51  return sum;
52  }
53  template <int R, int C>
54  inline static double var_dot_self(const Eigen::Matrix<var, R, C>& v) {
55  double sum = 0.0;
56  for (int i = 0; i < v.size(); ++i)
57  sum += square(v(i).vi_->val_);
58  return sum;
59  }
60  virtual void chain() {
61  for (size_t i = 0; i < size_; ++i)
62  v_[i]->adj_ += adj_ * 2.0 * v_[i]->val_;
63  }
64 };
65 } // namespace
66 /**
67  * Returns the dot product of a vector with itself.
68  *
69  * @param[in] v Vector.
70  * @return Dot product of the vector with itself.
71  * @tparam R number of rows or <code>Eigen::Dynamic</code> for
72  * dynamic; one of R or C must be 1
73  * @tparam C number of rows or <code>Eigen::Dyanmic</code> for
74  * dynamic; one of R or C must be 1
75  */
76 template <int R, int C>
77 inline var dot_self(const Eigen::Matrix<var, R, C>& v) {
78  check_vector("dot_self", "v", v);
79  return var(new dot_self_vari(v));
80 }
81 
82 } // namespace math
83 } // namespace stan
84 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Definition: sum.hpp:20
void check_vector(const char *function, const char *name, const Eigen::Matrix< T, R, C > &x)
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:15
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:12
size_t size_
Definition: dot_self.hpp:18
chain
Check that an output directory exists.
vari ** v_
Definition: dot_self.hpp:17
int size(const std::vector< T > &x)
Definition: size.hpp:17
static AutodiffStackStorage & instance()
void * alloc(size_t len)