Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
operator_subtraction.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__OPERATORS__OPERATOR_SUBTRACTION_HPP
2 #define STAN__AGRAD__REV__OPERATORS__OPERATOR_SUBTRACTION_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  class subtract_vv_vari : public op_vv_vari {
15  public:
16  subtract_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_ += adj_;
26  bvi_->adj_ -= adj_;
27  }
28  }
29  };
30 
31  class subtract_vd_vari : public op_vd_vari {
32  public:
33  subtract_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_;
42  }
43  };
44 
45  class subtract_dv_vari : public op_dv_vari {
46  public:
47  subtract_dv_vari(double a, vari* bvi) :
48  op_dv_vari(a - bvi->val_, a, bvi) {
49  }
50  void chain() {
52  || boost::math::isnan(bvi_->val_)))
53  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
54  else
55  bvi_->adj_ -= adj_;
56  }
57  };
58  }
59 
98  inline var operator-(const var& a, const var& b) {
99  return var(new subtract_vv_vari(a.vi_,b.vi_));
100  }
101 
113  inline var operator-(const var& a, const double b) {
114  if (b == 0.0)
115  return a;
116  return var(new subtract_vd_vari(a.vi_,b));
117  }
118 
130  inline var operator-(const double a, const var& b) {
131  return var(new subtract_dv_vari(a,b.vi_));
132  }
133 
134  }
135 }
136 #endif
bool isnan(const stan::agrad::var &v)
Checks if the given number is NaN.
fvar< T > operator-(const fvar< T > &x1, const fvar< T > &x2)
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.