Stan Math Library  2.20.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/meta.hpp>
5 #include <stan/math/fwd/core.hpp>
6 
11 
12 namespace stan {
13 namespace math {
14 
15 template <typename T>
16 inline fvar<T> pow(const fvar<T>& x1, const fvar<T>& x2) {
17  using std::log;
18  using std::pow;
19  T pow_x1_x2(pow(x1.val_, x2.val_));
20  return fvar<T>(pow_x1_x2, (x2.d_ * log(x1.val_) + x2.val_ * x1.d_ / x1.val_)
21  * pow_x1_x2);
22 }
23 
24 template <typename T>
25 inline fvar<T> pow(double x1, const fvar<T>& x2) {
26  using std::log;
27  using std::pow;
28  T u = pow(x1, x2.val_);
29  return fvar<T>(u, x2.d_ * log(x1) * u);
30 }
31 
32 template <typename T>
33 inline fvar<T> pow(const fvar<T>& x1, double x2) {
34  using std::pow;
35  using std::sqrt;
36 
37  if (x2 == -2)
38  return inv_square(x1);
39  if (x2 == -1)
40  return inv(x1);
41  if (x2 == -0.5)
42  return inv_sqrt(x1);
43  if (x2 == 0.5)
44  return sqrt(x1);
45  if (x2 == 1.0)
46  return x1;
47  if (x2 == 2.0)
48  return square(x1);
49 
50  return fvar<T>(pow(x1.val_, x2), x1.d_ * x2 * pow(x1.val_, x2 - 1));
51 }
52 } // namespace math
53 } // namespace stan
54 #endif
fvar< T > inv_sqrt(const fvar< T > &x)
Definition: inv_sqrt.hpp:11
T d_
The tangent (derivative) of this variable.
Definition: fvar.hpp:50
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:13
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:12
T val_
The value of this variable.
Definition: fvar.hpp:45
fvar< T > inv_square(const fvar< T > &x)
Definition: inv_square.hpp:12
var pow(double base, const var &exponent)
Return the base scalar raised to the power of the exponent variable (cmath).
Definition: pow.hpp:149
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:16
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41
fvar< T > inv(const fvar< T > &x)
Definition: inv.hpp:12

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