Stan Math Library  2.20.0
reverse mode automatic differentiation
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 
8 #include <limits>
9 
10 namespace stan {
11 namespace math {
12 
13 namespace internal {
14 class multiply_vv_vari : public op_vv_vari {
15  public:
17  : op_vv_vari(avi->val_ * bvi->val_, avi, bvi) {}
18  void chain() {
19  if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
20  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
21  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
22  } else {
23  avi_->adj_ += bvi_->val_ * adj_;
24  bvi_->adj_ += avi_->val_ * adj_;
25  }
26  }
27 };
28 
29 class multiply_vd_vari : public op_vd_vari {
30  public:
31  multiply_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ * b, avi, b) {}
32  void chain() {
33  if (unlikely(is_any_nan(avi_->val_, bd_)))
34  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
35  else
36  avi_->adj_ += adj_ * bd_;
37  }
38 };
39 } // namespace internal
40 
78 inline var operator*(const var& a, const var& b) {
79  return var(new internal::multiply_vv_vari(a.vi_, b.vi_));
80 }
81 
93 inline var operator*(const var& a, double b) {
94  if (b == 1.0)
95  return a;
96  return var(new internal::multiply_vd_vari(a.vi_, b));
97 }
98 
110 inline var operator*(double a, const var& b) {
111  if (a == 1.0)
112  return b;
113  return var(new internal::multiply_vd_vari(b.vi_, a)); // by symmetry
114 }
115 
116 } // namespace math
117 } // namespace stan
118 #endif
fvar< T > operator*(const fvar< T > &x, const fvar< T > &y)
Return the product of the two arguments.
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
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
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
#define unlikely(x)
Definition: likely.hpp:9
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44

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