Stan Math Library  2.20.0
reverse mode automatic differentiation
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 
4 #include <stan/math/rev/meta.hpp>
7 #include <stan/math/rev/core.hpp>
8 #include <vector>
9 
10 namespace stan {
11 namespace math {
12 
13 namespace internal {
14 class dot_self_vari : public vari {
15  protected:
16  vari** v_;
17  size_t size_;
18 
19  public:
20  dot_self_vari(vari** v, size_t size)
21  : vari(var_dot_self(v, size)), v_(v), size_(size) {}
22  template <typename Derived>
23  explicit dot_self_vari(const Eigen::DenseBase<Derived>& v)
24  : vari(var_dot_self(v)), size_(v.size()) {
25  v_ = reinterpret_cast<vari**>(
26  ChainableStack::instance_->memalloc_.alloc(size_ * sizeof(vari*)));
27  for (size_t i = 0; i < size_; i++)
28  v_[i] = v[i].vi_;
29  }
30  template <int R, int C>
31  explicit dot_self_vari(const Eigen::Matrix<var, R, C>& v)
32  : vari(var_dot_self(v)), size_(v.size()) {
33  v_ = reinterpret_cast<vari**>(
34  ChainableStack::instance_->memalloc_.alloc(size_ * sizeof(vari*)));
35  for (size_t i = 0; i < size_; ++i)
36  v_[i] = v(i).vi_;
37  }
38  inline static double square(double x) { return x * x; }
39  inline static double var_dot_self(vari** v, size_t size) {
40  double sum = 0.0;
41  for (size_t i = 0; i < size; ++i)
42  sum += square(v[i]->val_);
43  return sum;
44  }
45  template <typename Derived>
46  double var_dot_self(const Eigen::DenseBase<Derived>& v) {
47  double sum = 0.0;
48  for (int i = 0; i < v.size(); ++i)
49  sum += square(v(i).vi_->val_);
50  return sum;
51  }
52  template <int R, int C>
53  inline static double var_dot_self(const Eigen::Matrix<var, R, C>& v) {
54  double sum = 0.0;
55  for (int i = 0; i < v.size(); ++i)
56  sum += square(v(i).vi_->val_);
57  return sum;
58  }
59  virtual void chain() {
60  for (size_t i = 0; i < size_; ++i)
61  v_[i]->adj_ += adj_ * 2.0 * v_[i]->val_;
62  }
63 };
64 } // namespace internal
75 template <int R, int C>
76 inline var dot_self(const Eigen::Matrix<var, R, C>& v) {
77  check_vector("dot_self", "v", v);
78  return var(new internal::dot_self_vari(v));
79 }
80 
81 } // namespace math
82 } // namespace stan
83 #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
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: dot_self.hpp:59
dot_self_vari(const Eigen::Matrix< var, R, C > &v)
Definition: dot_self.hpp:31
dot_self_vari(vari **v, size_t size)
Definition: dot_self.hpp:20
The variable implementation base class.
Definition: vari.hpp:30
void check_vector(const char *function, const char *name, const Eigen::Matrix< T, R, C > &x)
Check if the matrix is either a row vector or column vector.
static double var_dot_self(vari **v, size_t size)
Definition: dot_self.hpp:39
static STAN_THREADS_DEF AutodiffStackStorage * instance_
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:13
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
static double var_dot_self(const Eigen::Matrix< var, R, C > &v)
Definition: dot_self.hpp:53
double var_dot_self(const Eigen::DenseBase< Derived > &v)
Definition: dot_self.hpp:46
dot_self_vari(const Eigen::DenseBase< Derived > &v)
Definition: dot_self.hpp:23
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
static double square(double x)
Definition: dot_self.hpp:38
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–2018, Stan Development Team.