Stan Math Library  2.20.0
reverse mode automatic differentiation
atan2.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_FUN_ATAN2_HPP
2 #define STAN_MATH_REV_SCAL_FUN_ATAN2_HPP
3 
4 #include <stan/math/rev/meta.hpp>
5 #include <stan/math/rev/core.hpp>
6 #include <cmath>
7 
8 namespace stan {
9 namespace math {
10 
11 namespace internal {
12 class atan2_vv_vari : public op_vv_vari {
13  public:
14  atan2_vv_vari(vari* avi, vari* bvi)
15  : op_vv_vari(std::atan2(avi->val_, bvi->val_), avi, bvi) {}
16  void chain() {
17  double a_sq_plus_b_sq
18  = (avi_->val_ * avi_->val_) + (bvi_->val_ * bvi_->val_);
19  avi_->adj_ += adj_ * bvi_->val_ / a_sq_plus_b_sq;
20  bvi_->adj_ -= adj_ * avi_->val_ / a_sq_plus_b_sq;
21  }
22 };
23 
24 class atan2_vd_vari : public op_vd_vari {
25  public:
26  atan2_vd_vari(vari* avi, double b)
27  : op_vd_vari(std::atan2(avi->val_, b), avi, b) {}
28  void chain() {
29  double a_sq_plus_b_sq = (avi_->val_ * avi_->val_) + (bd_ * bd_);
30  avi_->adj_ += adj_ * bd_ / a_sq_plus_b_sq;
31  }
32 };
33 
34 class atan2_dv_vari : public op_dv_vari {
35  public:
36  atan2_dv_vari(double a, vari* bvi)
37  : op_dv_vari(std::atan2(a, bvi->val_), a, bvi) {}
38  void chain() {
39  double a_sq_plus_b_sq = (ad_ * ad_) + (bvi_->val_ * bvi_->val_);
40  bvi_->adj_ -= adj_ * ad_ / a_sq_plus_b_sq;
41  }
42 };
43 } // namespace internal
44 
61 inline var atan2(const var& a, const var& b) {
62  return var(new internal::atan2_vv_vari(a.vi_, b.vi_));
63 }
64 
77 inline var atan2(const var& a, double b) {
78  return var(new internal::atan2_vd_vari(a.vi_, b));
79 }
80 
117 inline var atan2(double a, const var& b) {
118  return var(new internal::atan2_dv_vari(a, b.vi_));
119 }
120 
121 } // namespace math
122 } // namespace stan
123 #endif
atan2_vd_vari(vari *avi, double b)
Definition: atan2.hpp:26
fvar< T > atan2(const fvar< T > &x1, const fvar< T > &x2)
Definition: atan2.hpp:13
The variable implementation base class.
Definition: vari.hpp:30
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: atan2.hpp:38
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: atan2.hpp:16
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
atan2_dv_vari(double a, vari *bvi)
Definition: atan2.hpp:36
atan2_vv_vari(vari *avi, vari *bvi)
Definition: atan2.hpp:14
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: atan2.hpp:28

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