Stan Math Library  2.10.0
reverse mode automatic differentiation
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 
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;
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
fvar< T > inv_sqrt(const fvar< T > &x)
Definition: inv_sqrt.hpp:15
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:15
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
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
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:15
fvar< T > inv_square(const fvar< T > &x)
Definition: inv_square.hpp:15
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:18
fvar< T > inv(const fvar< T > &x)
Definition: inv.hpp:15

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