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
13#include <cmath>
14#include <complex>
15#include <type_traits>
16
17namespace stan {
18namespace math {
19/*
20 *
21 * @tparam T1 Either an `fvar`, `arithmetic`, or `complex` type with an inner
22 * `fvar` or `arithmetic` type.
23 * @tparam T2 Either a `fvar`, `arithmetic`, or `complex` type with an inner
24 * `fvar` or `arithmetic` type.
25 * @param x1 Base variable.
26 * @param x2 Exponent variable.
27 * @return Base raised to the exponent.
28 */
29template <typename T1, typename T2,
30 require_any_fvar_t<base_type_t<T1>, base_type_t<T2>>* = nullptr,
31 require_all_stan_scalar_t<T1, T2>* = nullptr>
32inline auto pow(const T1& x1, const T2& x2) {
33 using std::log;
34 using std::pow;
36 return internal::complex_pow(x1, x2);
37 } else if constexpr (is_fvar<T1>::value && is_fvar<T2>::value) {
38 auto pow_x1_x2(stan::math::pow(x1.val_, x2.val_));
39 return T1(pow_x1_x2,
40 (x2.d_ * stan::math::log(x1.val_) + x2.val_ * x1.d_ / x1.val_)
41 * pow_x1_x2);
42 } else if constexpr (is_fvar<T2>::value) {
43 auto u = stan::math::pow(x1, x2.val_);
44 return T2(u, x2.d_ * stan::math::log(x1) * u);
45 } else {
46 using std::sqrt;
47 if (x2 == -2) {
48 return stan::math::inv_square(x1);
49 }
50 if (x2 == -1) {
51 return stan::math::inv(x1);
52 }
53 if (x2 == -0.5) {
54 return stan::math::inv_sqrt(x1);
55 }
56 if (x2 == 0.5) {
57 return stan::math::sqrt(x1);
58 }
59 if (x2 == 1.0) {
60 return x1;
61 }
62 if (x2 == 2.0) {
63 return stan::math::square(x1);
64 }
65 return T1(stan::math::pow(x1.val_, x2),
66 x1.d_ * x2 * stan::math::pow(x1.val_, x2 - 1));
67 }
68}
69
81template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr,
82 require_all_not_matrix_st<is_var, T1, T2>* = nullptr,
83 require_any_fvar_t<base_type_t<T1>, base_type_t<T2>>* = nullptr>
84inline auto pow(const T1& a, const T2& b) {
86 a, b, [](const auto& c, const auto& d) { return stan::math::pow(c, d); });
87}
88
89} // namespace math
90} // namespace stan
91#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:27
fvar< T > inv_square(const fvar< T > &x)
auto pow(const T1 &x1, const T2 &x2)
Definition pow.hpp:32
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:17
auto apply_scalar_binary(const T1 &x, const T2 &y, const F &f)
Base template function for vectorization of binary scalar functions defined by applying a functor to ...
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 ...
If T is an arithmetic type (that is, an instance of std::complex) or a cv-qualified version thereof,...
Defines a static member function type which is defined to be false as the primitive scalar types cann...
Definition is_fvar.hpp:15