| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
pow.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_FUN_POW_HPP
2 #define STAN_MATH_REV_SCAL_FUN_POW_HPP
3
4 #include <stan/math/rev/core.hpp>
5 #include <stan/math/rev/scal/fun/sqrt.hpp>
6 #include <boost/math/special_functions/fpclassify.hpp>
7 #include <cmath>
8 #include <limits>
9
10 namespace stan {
11 namespace math {
12
13 namespace {
14 class pow_vv_vari : public op_vv_vari {
15 public:
16 pow_vv_vari(vari* avi, vari* bvi) :
17 op_vv_vari(std::pow(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 if (avi_->val_ == 0.0) return; // partials zero, avoids 0 & log(0)
26 avi_->adj_ += adj_ * bvi_->val_ * val_ / avi_->val_;
27 bvi_->adj_ += adj_ * std::log(avi_->val_) * val_;
28 }
29 }
30 };
31
32 class pow_vd_vari : public op_vd_vari {
33 public:
34 pow_vd_vari(vari* avi, double b) :
35 op_vd_vari(std::pow(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 if (avi_->val_ == 0.0) return; // partials zero, avoids 0 & log(0)
43 avi_->adj_ += adj_ * bd_ * val_ / avi_->val_;
44 }
45 }
46 };
47
48 class pow_dv_vari : public op_dv_vari {
49 public:
50 pow_dv_vari(double a, vari* bvi) :
51 op_dv_vari(std::pow(a, bvi->val_), a, bvi) {
52 }
53 void chain() {
54 if (unlikely(boost::math::isnan(bvi_->val_)
55 || boost::math::isnan(ad_))) {
56 bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
57 } else {
58 if (ad_ == 0.0) return; // partials zero, avoids 0 & log(0)
59 bvi_->adj_ += adj_ * std::log(ad_) * val_;
60 }
61 }
62 };
63 }
64
103 inline var pow(const var& base, const var& exponent) {
104 return var(new pow_vv_vari(base.vi_, exponent.vi_));
105 }
106
119 inline var pow(const var& base, const double exponent) {
120 if (exponent == 0.5)
121 return sqrt(base);
122 if (exponent == 1.0)
123 return base;
124 if (exponent == 2.0)
125 return base * base; // FIXME: use square()
126 return var(new pow_vd_vari(base.vi_, exponent));
127 }
128
141 inline var pow(const double base, const var& exponent) {
142 return var(new pow_dv_vari(base, exponent.vi_));
143 }
144
145 }
146 }
147 #endif
core.hpp
stan::math::sqrt
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:15
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
sqrt.hpp
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:44
stan::math::pow
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:18

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