Stan Math Library  2.20.0
reverse mode automatic differentiation
operator_division.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_CORE_OPERATOR_DIVISION_HPP
2 #define STAN_MATH_REV_CORE_OPERATOR_DIVISION_HPP
3 
9 #include <limits>
10 
11 namespace stan {
12 namespace math {
13 
14 namespace internal {
15 // (a/b)' = a' * (1 / b) - b' * (a / [b * b])
16 class divide_vv_vari : public op_vv_vari {
17  public:
18  divide_vv_vari(vari* avi, vari* bvi)
19  : op_vv_vari(avi->val_ / bvi->val_, avi, bvi) {}
20  void chain() {
21  if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
22  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
23  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
24  } else {
25  avi_->adj_ += adj_ / bvi_->val_;
26  bvi_->adj_ -= adj_ * avi_->val_ / (bvi_->val_ * bvi_->val_);
27  }
28  }
29 };
30 
31 class divide_vd_vari : public op_vd_vari {
32  public:
33  divide_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ / b, avi, b) {}
34  void chain() {
35  if (unlikely(is_any_nan(avi_->val_, bd_)))
36  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
37  else
38  avi_->adj_ += adj_ / bd_;
39  }
40 };
41 
42 class divide_dv_vari : public op_dv_vari {
43  public:
44  divide_dv_vari(double a, vari* bvi) : op_dv_vari(a / bvi->val_, a, bvi) {}
45  void chain() { bvi_->adj_ -= adj_ * ad_ / (bvi_->val_ * bvi_->val_); }
46 };
47 } // namespace internal
48 
87 inline var operator/(const var& a, const var& b) {
88  return var(new internal::divide_vv_vari(a.vi_, b.vi_));
89 }
90 
102 inline var operator/(const var& a, double b) {
103  if (b == 1.0)
104  return a;
105  return var(new internal::divide_vd_vari(a.vi_, b));
106 }
107 
119 inline var operator/(double a, const var& b) {
120  return var(new internal::divide_dv_vari(a, b.vi_));
121 }
122 
123 } // namespace math
124 } // namespace stan
125 #endif
fvar< T > operator/(const fvar< T > &x1, const fvar< T > &x2)
Return the result of dividing the first argument by the second.
The variable implementation base class.
Definition: vari.hpp:30
bool is_any_nan(const T &x)
Returns true if the input is NaN and false otherwise.
Definition: is_any_nan.hpp:21
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. ...
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
#define unlikely(x)
Definition: likely.hpp:9
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. ...
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. ...

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