| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/rev/core/var.hpp>
5 #include <stan/math/rev/core/vv_vari.hpp>
6 #include <stan/math/rev/core/vd_vari.hpp>
7 #include <stan/math/rev/core/dv_vari.hpp>
8 #include <stan/math/prim/scal/fun/is_nan.hpp>
9 #include <limits>
10
11 namespace stan {
12 namespace math {
13
14 namespace {
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 }
21 void chain() {
22 if (unlikely(is_nan(avi_->val_)
23 || is_nan(bvi_->val_))) {
24 avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
25 bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
26 } else {
27 avi_->adj_ += adj_ / bvi_->val_;
28 bvi_->adj_ -= adj_ * avi_->val_ / (bvi_->val_ * bvi_->val_);
29 }
30 }
31 };
32
33 class divide_vd_vari : public op_vd_vari {
34 public:
35 divide_vd_vari(vari* avi, double b) :
36 op_vd_vari(avi->val_ / b, avi, b) {
37 }
38 void chain() {
39 if (unlikely(is_nan(avi_->val_)
40 || is_nan(bd_)))
41 avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
42 else
43 avi_->adj_ += adj_ / bd_;
44 }
45 };
46
47 class divide_dv_vari : public op_dv_vari {
48 public:
49 divide_dv_vari(double a, vari* bvi) :
50 op_dv_vari(a / bvi->val_, a, bvi) {
51 }
52 void chain() {
53 bvi_->adj_ -= adj_ * ad_ / (bvi_->val_ * bvi_->val_);
54 }
55 };
56 }
57
96 inline var operator/(const var& a, const var& b) {
97 return var(new divide_vv_vari(a.vi_, b.vi_));
98 }
99
111 inline var operator/(const var& a, const double b) {
112 if (b == 1.0)
113 return a;
114 return var(new divide_vd_vari(a.vi_, b));
115 }
116
128 inline var operator/(const double a, const var& b) {
129 return var(new divide_dv_vari(a, b.vi_));
130 }
131
132 }
133 }
134 #endif
dv_vari.hpp
stan::math::operator/
fvar< T > operator/(const fvar< T > &x1, const fvar< T > &x2)
Definition: operator_division.hpp:12
is_nan.hpp
stan
Definition: log_sum_exp.hpp:8
vd_vari.hpp
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
unlikely
#define unlikely(x)
Definition: likely.hpp:9
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42
var.hpp
vv_vari.hpp
stan::math::is_nan
int is_nan(const fvar< T > &x)
Returns 1 if the input's value is NaN and 0 otherwise.
Definition: is_nan.hpp:21

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