| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

operator_multiplication.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_CORE_OPERATOR_MULTIPLICATION_HPP
2 #define STAN_MATH_REV_CORE_OPERATOR_MULTIPLICATION_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 <boost/math/special_functions/fpclassify.hpp>
8 #include <limits>
9
10 namespace stan {
11 namespace math {
12
13 namespace {
14 class multiply_vv_vari : public op_vv_vari {
15 public:
16 multiply_vv_vari(vari* avi, vari* bvi) :
17 op_vv_vari(avi->val_ * bvi->val_, avi, bvi) {
18 }
19 void chain() {
20 if (unlikely(boost::math::isnan(avi_->val_)
21 || boost::math::isnan(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_ += bvi_->val_ * adj_;
26 bvi_->adj_ += avi_->val_ * adj_;
27 }
28 }
29 };
30
31 class multiply_vd_vari : public op_vd_vari {
32 public:
33 multiply_vd_vari(vari* avi, double b) :
34 op_vd_vari(avi->val_ * b, avi, b) {
35 }
36 void chain() {
37 if (unlikely(boost::math::isnan(avi_->val_)
38 || boost::math::isnan(bd_)))
39 avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
40 else
41 avi_->adj_ += adj_ * bd_;
42 }
43 };
44 }
45
83 inline var operator*(const var& a, const var& b) {
84 return var(new multiply_vv_vari(a.vi_, b.vi_));
85 }
86
98 inline var operator*(const var& a, const double b) {
99 if (b == 1.0)
100 return a;
101 return var(new multiply_vd_vari(a.vi_, b));
102 }
103
115 inline var operator*(const double a, const var& b) {
116 if (a == 1.0)
117 return b;
118 return var(new multiply_vd_vari(b.vi_, a)); // by symmetry
119 }
120
121 }
122 }
123 #endif
stan
Definition: log_sum_exp.hpp:8
vd_vari.hpp
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
boost::math::isnan
bool isnan(const stan::math::var &v)
Checks if the given number is NaN.
Definition: boost_isnan.hpp:22
unlikely
#define unlikely(x)
Definition: likely.hpp:9
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:43
var.hpp
vv_vari.hpp
stan::math::operator*
fvar< T > operator*(const fvar< T > &x1, const fvar< T > &x2)
Definition: operator_multiplication.hpp:14

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