Stan Math Library  2.14.0
reverse mode automatic differentiation
hypot.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_FUN_HYPOT_HPP
2 #define STAN_MATH_REV_SCAL_FUN_HYPOT_HPP
3 
4 #include <stan/math/rev/core.hpp>
6 
7 namespace stan {
8  namespace math {
9 
10  namespace {
11  class hypot_vv_vari : public op_vv_vari {
12  public:
13  hypot_vv_vari(vari* avi, vari* bvi) :
14  op_vv_vari(hypot(avi->val_, bvi->val_), avi, bvi) {
15  }
16  void chain() {
17  avi_->adj_ += adj_ * avi_->val_ / val_;
18  bvi_->adj_ += adj_ * bvi_->val_ / val_;
19  }
20  };
21 
22  class hypot_vd_vari : public op_v_vari {
23  public:
24  hypot_vd_vari(vari* avi, double b) :
25  op_v_vari(hypot(avi->val_, b), avi) {
26  }
27  void chain() {
28  avi_->adj_ += adj_ * avi_->val_ / val_;
29  }
30  };
31  }
32 
47  inline var hypot(const var& a, const var& b) {
48  return var(new hypot_vv_vari(a.vi_, b.vi_));
49  }
50 
63  inline var hypot(const var& a, double b) {
64  return var(new hypot_vd_vari(a.vi_, b));
65  }
66 
106  inline var hypot(double a, const var& b) {
107  return var(new hypot_vd_vari(b.vi_, a));
108  }
109 
110  }
111 }
112 #endif
fvar< T > hypot(const fvar< T > &x1, const fvar< T > &x2)
Return the length of the hypoteneuse of a right triangle with opposite and adjacent side lengths give...
Definition: hypot.hpp:25
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42

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