Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
atan2.hpp
Go to the documentation of this file.
1 #ifndef __STAN__AGRAD__REV__ATAN2_HPP__
2 #define __STAN__AGRAD__REV__ATAN2_HPP__
3 
4 #include <valarray>
5 #include <stan/agrad/rev/var.hpp>
9 
10 namespace stan {
11  namespace agrad {
12 
13  namespace {
14  class atan2_vv_vari : public op_vv_vari {
15  public:
16  atan2_vv_vari(vari* avi, vari* bvi) :
17  op_vv_vari(std::atan2(avi->val_,bvi->val_),avi,bvi) {
18  }
19  void chain() {
20  double a_sq_plus_b_sq = (avi_->val_ * avi_->val_) + (bvi_->val_ * bvi_->val_);
21  avi_->adj_ += adj_ * bvi_->val_ / a_sq_plus_b_sq;
22  bvi_->adj_ -= adj_ * avi_->val_ / a_sq_plus_b_sq;
23  }
24  };
25 
26  class atan2_vd_vari : public op_vd_vari {
27  public:
28  atan2_vd_vari(vari* avi, double b) :
29  op_vd_vari(std::atan2(avi->val_,b),avi,b) {
30  }
31  void chain() {
32  double a_sq_plus_b_sq = (avi_->val_ * avi_->val_) + (bd_ * bd_);
33  avi_->adj_ += adj_ * bd_ / a_sq_plus_b_sq;
34  }
35  };
36 
37  class atan2_dv_vari : public op_dv_vari {
38  public:
39  atan2_dv_vari(double a, vari* bvi) :
40  op_dv_vari(std::atan2(a,bvi->val_),a,bvi) {
41  }
42  void chain() {
43  double a_sq_plus_b_sq = (ad_ * ad_) + (bvi_->val_ * bvi_->val_);
44  bvi_->adj_ -= adj_ * ad_ / a_sq_plus_b_sq;
45  }
46  };
47  }
48 
63  inline var atan2(const var& a, const var& b) {
64  return var(new atan2_vv_vari(a.vi_,b.vi_));
65  }
66 
79  inline var atan2(const var& a, const double b) {
80  return var(new atan2_vd_vari(a.vi_,b));
81  }
82 
95  inline var atan2(const double a, const var& b) {
96  return var(new atan2_dv_vari(a,b.vi_));
97  }
98 
99  }
100 }
101 #endif

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