Stan Math Library  2.20.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/meta.hpp>
5 #include <stan/math/rev/core.hpp>
7 
8 namespace stan {
9 namespace math {
10 
11 namespace internal {
12 class hypot_vv_vari : public op_vv_vari {
13  public:
14  hypot_vv_vari(vari* avi, vari* bvi)
15  : op_vv_vari(hypot(avi->val_, bvi->val_), avi, bvi) {}
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) : op_v_vari(hypot(avi->val_, b), avi) {}
25  void chain() { avi_->adj_ += adj_ * avi_->val_ / val_; }
26 };
27 } // namespace internal
28 
45 inline var hypot(const var& a, const var& b) {
46  return var(new internal::hypot_vv_vari(a.vi_, b.vi_));
47 }
48 
61 inline var hypot(const var& a, double b) {
62  return var(new internal::hypot_vd_vari(a.vi_, b));
63 }
64 
104 inline var hypot(double a, const var& b) {
105  return var(new internal::hypot_vd_vari(b.vi_, a));
106 }
107 
108 } // namespace math
109 } // namespace stan
110 #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:26
The variable implementation base class.
Definition: vari.hpp:30
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: hypot.hpp:16
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: hypot.hpp:25
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
hypot_vd_vari(vari *avi, double b)
Definition: hypot.hpp:24
hypot_vv_vari(vari *avi, vari *bvi)
Definition: hypot.hpp:14

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