Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pow.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__FWD__FUNCTIONS__POW_HPP
2 #define STAN__AGRAD__FWD__FUNCTIONS__POW_HPP
3 
5 #include <stan/meta/traits.hpp>
9 
10 namespace stan {
11 
12  namespace agrad {
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 stan::agrad::inv;
44  using std::sqrt;
45  using stan::math::square;
46 
47  if (x2 == -2)
48  return inv_square(x1);
49  if (x2 == -1)
50  return inv(x1);
51  if (x2 == -0.5)
52  return inv_sqrt(x1);
53  if (x2 == 0.5)
54  return sqrt(x1);
55  if (x2 == 1.0)
56  return x1;
57  if (x2 == 2.0)
58  return square(x1);
59 
60  return fvar<T>(pow(x1.val_, x2),
61  x1.d_ * x2 * pow(x1.val_, x2 - 1));
62  }
63  }
64 }
65 #endif
T square(const T x)
Return the square of the specified argument.
Definition: square.hpp:22
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:17
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:15
fvar< T > inv(const fvar< T > &x)
Definition: inv.hpp:15
fvar< T > inv_square(const fvar< T > &x)
Definition: inv_square.hpp:15
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.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:144
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
fvar< T > inv_sqrt(const fvar< T > &x)
Definition: inv_sqrt.hpp:15

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