Automatic Differentiation
 
Loading...
Searching...
No Matches
pow.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_POW_HPP
2#define STAN_MATH_FWD_FUN_POW_HPP
3
11#include <cmath>
12#include <complex>
13#include <type_traits>
14
15namespace stan {
16namespace math {
17
18template <typename T>
19inline fvar<T> pow(const fvar<T>& x1, const fvar<T>& x2) {
20 using std::log;
21 using std::pow;
22 T pow_x1_x2(pow(x1.val_, x2.val_));
23 return fvar<T>(pow_x1_x2, (x2.d_ * log(x1.val_) + x2.val_ * x1.d_ / x1.val_)
24 * pow_x1_x2);
25}
26
27template <typename T, typename U, typename = require_arithmetic_t<U>>
28inline fvar<T> pow(U x1, const fvar<T>& x2) {
29 using std::log;
30 using std::pow;
31 T u = pow(x1, x2.val_);
32 return fvar<T>(u, x2.d_ * log(x1) * u);
33}
34
35template <typename T, typename U, typename = require_arithmetic_t<U>>
36inline fvar<T> pow(const fvar<T>& x1, U x2) {
37 using std::pow;
38 using std::sqrt;
39 if (x2 == -2) {
40 return inv_square(x1);
41 }
42 if (x2 == -1) {
43 return inv(x1);
44 }
45 if (x2 == -0.5) {
46 return inv_sqrt(x1);
47 }
48 if (x2 == 0.5) {
49 return sqrt(x1);
50 }
51 if (x2 == 1.0) {
52 return x1;
53 }
54 if (x2 == 2.0) {
55 return square(x1);
56 }
57 return fvar<T>(pow(x1.val_, x2), x1.d_ * x2 * pow(x1.val_, x2 - 1));
58}
59
60// must uniquely match all pairs of:
61// { complex<fvar<V>>, complex<T>, fvar<V>, T }
62// with at least one fvar<V> and at least one complex, where T is arithmetic:
63// 1) complex<fvar<V>>, complex<fvar<V>>
64// 2) complex<fvar<V>>, complex<T>
65// 3) complex<fvar<V>>, fvar<V>
66// 4) complex<fvar<V>>, T
67// 5) complex<T>, complex<fvar<V>>
68// 6) complex<T>, fvar<V>
69// 7) fvar<V>, complex<fvar<V>>
70// 8) fvar<V>, complex<T>
71// 9) T, complex<fvar<V>>
72
80template <typename V>
81inline std::complex<fvar<V>> pow(const std::complex<fvar<V>>& x,
82 const std::complex<fvar<V>>& y) {
83 return internal::complex_pow(x, y);
84}
85
95template <typename V, typename T, typename = require_arithmetic_t<T>>
96inline std::complex<fvar<V>> pow(const std::complex<fvar<V>>& x,
97 const std::complex<T>& y) {
98 return internal::complex_pow(x, y);
99}
100
109template <typename V>
110inline std::complex<fvar<V>> pow(const std::complex<fvar<V>>& x,
111 const fvar<V>& y) {
112 return internal::complex_pow(x, y);
113}
114
124template <typename V, typename T, typename = require_arithmetic_t<T>>
125inline std::complex<fvar<V>> pow(const std::complex<fvar<V>>& x, const T& y) {
126 return internal::complex_pow(x, y);
127}
128
138template <typename V, typename T, typename = require_arithmetic_t<T>>
139inline std::complex<fvar<V>> pow(const std::complex<T>& x,
140 const std::complex<fvar<V>>& y) {
141 return internal::complex_pow(x, y);
142}
143
153template <typename V, typename T, typename = require_arithmetic_t<T>>
154inline std::complex<fvar<V>> pow(const std::complex<T>& x, const fvar<V>& y) {
155 return internal::complex_pow(x, y);
156}
157
166template <typename V>
167inline std::complex<fvar<V>> pow(const fvar<V>& x,
168 const std::complex<fvar<V>>& y) {
169 return internal::complex_pow(x, y);
170}
171
181template <typename V, typename T, typename = require_arithmetic_t<T>>
182inline std::complex<fvar<V>> pow(const fvar<V>& x, const std::complex<T>& y) {
183 return internal::complex_pow(x, y);
184}
185
195template <typename T, typename V, typename = require_arithmetic_t<T>>
196inline std::complex<fvar<V>> pow(T x, const std::complex<fvar<V>>& y) {
197 return internal::complex_pow(x, y);
198}
199
212template <typename T>
213inline std::complex<fvar<T>> pow(const std::complex<fvar<T>>& x, int y) {
214 return internal::complex_pow(x, y);
215}
216
217} // namespace math
218} // namespace stan
219#endif
complex_return_t< U, V > complex_pow(const U &x, const V &y)
Return the first argument raised to the power of the second argument.
Definition pow.hpp:26
fvar< T > inv_square(const fvar< T > &x)
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:17
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition pow.hpp:19
fvar< T > inv_sqrt(const fvar< T > &x)
Definition inv_sqrt.hpp:12
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:12
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
Scalar val_
The value of this variable.
Definition fvar.hpp:49
Scalar d_
The tangent (derivative) of this variable.
Definition fvar.hpp:61
This template class represents scalars used in forward-mode automatic differentiation,...
Definition fvar.hpp:40