Stan Math Library  2.14.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  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(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, 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
fvar< T > inv_sqrt(const fvar< T > &x)
Definition: inv_sqrt.hpp:14
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:14
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:14
fvar< T > inv_square(const fvar< T > &x)
Definition: inv_square.hpp:14
var pow(double base, const var &exponent)
Return the base scalar raised to the power of the exponent variable (cmath).
Definition: pow.hpp:141
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:17
fvar< T > inv(const fvar< T > &x)
Definition: inv.hpp:14

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