| 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_FWD_SCAL_FUN_POW_HPP
2 #define STAN_MATH_FWD_SCAL_FUN_POW_HPP
3
4 #include <stan/math/fwd/core.hpp>
5
6 #include <stan/math/fwd/scal/fun/sqrt.hpp>
7 #include <stan/math/fwd/scal/fun/inv.hpp>
8 #include <stan/math/fwd/scal/fun/inv_sqrt.hpp>
9 #include <stan/math/fwd/scal/fun/inv_square.hpp>
10
11 namespace stan {
12 namespace math {
13
14 template <typename T>
15 inline
16 fvar<T>
17 pow(const fvar<T>& x1, const fvar<T>& x2) {
18 using std::pow;
19 using std::log;
20 T pow_x1_x2(pow(x1.val_, x2.val_));
21 return fvar<T>(pow_x1_x2,
22 (x2.d_ * log(x1.val_)
23 + x2.val_ * x1.d_ / x1.val_) * pow_x1_x2);
24 }
25
26 template <typename T>
27 inline
28 fvar<T>
29 pow(const double x1, const fvar<T>& x2) {
30 using std::pow;
31 using std::log;
32 T u = pow(x1, x2.val_);
33 return fvar<T>(u, x2.d_ * log(x1) * u);
34 }
35
36 template <typename T>
37 inline
38 fvar<T>
39 pow(const fvar<T>& x1, const double x2) {
40 using std::pow;
41 using std::sqrt;
42
43 if (x2 == -2)
44 return inv_square(x1);
45 if (x2 == -1)
46 return inv(x1);
47 if (x2 == -0.5)
48 return inv_sqrt(x1);
49 if (x2 == 0.5)
50 return sqrt(x1);
51 if (x2 == 1.0)
52 return x1;
53 if (x2 == 2.0)
54 return square(x1);
55
56 return fvar<T>(pow(x1.val_, x2),
57 x1.d_ * x2 * pow(x1.val_, x2 - 1));
58 }
59 }
60 }
61 #endif
core.hpp
stan::math::inv_sqrt
fvar< T > inv_sqrt(const fvar< T > &x)
Definition: inv_sqrt.hpp:14
stan::math::fvar::d_
T d_
Definition: fvar.hpp:14
stan::math::sqrt
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:14
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
inv_square.hpp
stan::math::pow
var pow(const double base, const var &exponent)
Return the base scalar raised to the power of the exponent variable (cmath).
Definition: pow.hpp:141
stan::math::square
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:14
inv.hpp
stan::math::fvar::val_
T val_
Definition: fvar.hpp:13
sqrt.hpp
stan::math::inv_square
fvar< T > inv_square(const fvar< T > &x)
Definition: inv_square.hpp:14
inv_sqrt.hpp
stan::math::pow
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:17
stan::math::fvar
Definition: fvar.hpp:12
stan::math::inv
fvar< T > inv(const fvar< T > &x)
Definition: inv.hpp:14

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