Stan Math Library: stan/math/fwd/scal/fun/pow.hpp Source File

| Stan Math Library 2.10.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
13 namespace math {
14
15 template <typename T>
16 inline
17 fvar<T>
18 pow(const fvar<T>& x1, const fvar<T>& x2) {
19 using std::pow;
20 using std::log;
21 T pow_x1_x2(pow(x1.val_, x2.val_));
22 return fvar<T>(pow_x1_x2,
23 (x2.d_ * log(x1.val_)
24 + x2.val_ * x1.d_ / x1.val_) * pow_x1_x2);
25 }
26
27 template <typename T>
28 inline
29 fvar<T>
30 pow(const double x1, const fvar<T>& x2) {
31 using std::pow;
32 using std::log;
33 T u = pow(x1, x2.val_);
34 return fvar<T>(u, x2.d_ * log(x1) * u);
35 }
36
37 template <typename T>
38 inline
39 fvar<T>
40 pow(const fvar<T>& x1, const double x2) {
41 using std::pow;
42 using stan::math::sqrt;
43 using stan::math::inv;
44 using stan::math::inv_sqrt;
45 using stan::math::inv_square;
46 using std::sqrt;
47 using stan::math::square;
48
49 if (x2 == -2)
50 return inv_square(x1);
51 if (x2 == -1)
52 return inv(x1);
53 if (x2 == -0.5)
54 return inv_sqrt(x1);
55 if (x2 == 0.5)
56 return sqrt(x1);
57 if (x2 == 1.0)
58 return x1;
59 if (x2 == 2.0)
60 return square(x1);
61
62 return fvar<T>(pow(x1.val_, x2),
63 x1.d_ * x2 * pow(x1.val_, x2 - 1));
64 }
65 }
66 }
67 #endif
core.hpp
stan::math::inv_sqrt
fvar< T > inv_sqrt(const fvar< T > &x)
Definition: inv_sqrt.hpp:15
stan::math::fvar::d_
T d_
Definition: fvar.hpp:15
stan::math::sqrt
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:15
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
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:15
inv.hpp
stan::math::fvar::val_
T val_
Definition: fvar.hpp:14
sqrt.hpp
stan::math::inv_square
fvar< T > inv_square(const fvar< T > &x)
Definition: inv_square.hpp:15
inv_sqrt.hpp
stan::math::pow
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:18
stan::math::fvar
Definition: fvar.hpp:13
stan::math::inv
fvar< T > inv(const fvar< T > &x)
Definition: inv.hpp:15

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