| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 <stan/math/prim/scal/fun/is_nan.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(is_nan(avi_->val_)
21 || is_nan(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(is_nan(avi_->val_)
39 || is_nan(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(is_nan(bvi_->val_)
55 || is_nan(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:14
is_nan.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
std
Definition: std_numeric_limits.hpp:7
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
sqrt.hpp
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
stan::math::pow
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:17
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.