Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
operator_division.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__OPERATORS__OPERATOR_DIVISION_HPP
2 #define STAN__AGRAD__REV__OPERATORS__OPERATOR_DIVISION_HPP
3 
4 #include <stan/agrad/rev/var.hpp>
8 #include <boost/math/special_functions/fpclassify.hpp>
9 
10 namespace stan {
11  namespace agrad {
12 
13  namespace {
14  // (a/b)' = a' * (1 / b) - b' * (a / [b * b])
15  class divide_vv_vari : public op_vv_vari {
16  public:
17  divide_vv_vari(vari* avi, vari* bvi) :
18  op_vv_vari(avi->val_ / bvi->val_, avi, bvi) {
19  }
20  void chain() {
21  if (unlikely(boost::math::isnan(avi_->val_)
22  || boost::math::isnan(bvi_->val_))) {
23  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
24  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
25  } else {
26  avi_->adj_ += adj_ / bvi_->val_;
27  bvi_->adj_ -= adj_ * avi_->val_ / (bvi_->val_ * bvi_->val_);
28  }
29  }
30  };
31 
32  class divide_vd_vari : public op_vd_vari {
33  public:
34  divide_vd_vari(vari* avi, double b) :
35  op_vd_vari(avi->val_ / b, avi, b) {
36  }
37  void chain() {
38  if (unlikely(boost::math::isnan(avi_->val_)
39  || boost::math::isnan(bd_)))
40  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
41  else
42  avi_->adj_ += adj_ / bd_;
43  }
44  };
45 
46  class divide_dv_vari : public op_dv_vari {
47  public:
48  divide_dv_vari(double a, vari* bvi) :
49  op_dv_vari(a / bvi->val_, a, bvi) {
50  }
51  void chain() {
52  bvi_->adj_ -= adj_ * ad_ / (bvi_->val_ * bvi_->val_);
53  }
54  };
55  }
56 
95  inline var operator/(const var& a, const var& b) {
96  return var(new divide_vv_vari(a.vi_,b.vi_));
97  }
98 
110  inline var operator/(const var& a, const double b) {
111  if (b == 1.0)
112  return a;
113  return var(new divide_vd_vari(a.vi_,b));
114  }
115 
127  inline var operator/(const double a, const var& b) {
128  return var(new divide_dv_vari(a,b.vi_));
129  }
130 
131  }
132 }
133 #endif
bool isnan(const stan::agrad::var &v)
Checks if the given number is NaN.
Eigen::Matrix< fvar< T >, R, C > operator/(const Eigen::Matrix< fvar< T >, R, C > &v, const fvar< T > &c)
Definition: divide.hpp:58
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:40
#define unlikely(x)
Definition: likely.hpp:9
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:27

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