Automatic Differentiation
 
Loading...
Searching...
No Matches
exp.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_EXP_HPP
2#define STAN_MATH_PRIM_FUN_EXP_HPP
3
8#include <cmath>
9#include <complex>
10#include <limits>
11
12namespace stan {
13namespace math {
14
23template <typename T, require_arithmetic_t<T>* = nullptr>
24inline auto exp(T&& x) {
25 return std::exp(x);
26}
27
38template <typename T, require_complex_bt<std::is_arithmetic, T>* = nullptr>
39inline auto exp(T&& x) {
40 return std::exp(x);
41}
42
47struct exp_fun {
55 template <typename T>
56 static inline auto fun(T&& x) {
57 return exp(std::forward<T>(x));
58 }
59};
60
70template <typename Container, require_ad_container_t<Container>* = nullptr>
71inline auto exp(Container&& x) {
73 std::forward<Container>(x));
74}
75
84template <typename Container,
86inline auto exp(Container&& x) {
87 return apply_vector_unary<Container>::apply(
88 std::forward<Container>(x), [](auto&& v) { return v.array().exp(); });
89}
90
91namespace internal {
102template <typename V>
103inline std::complex<V> complex_exp(const std::complex<V>& z) {
104 if (is_inf(z.real()) && z.real() > 0) {
105 if (is_nan(z.imag()) || z.imag() == 0) {
106 // (+inf, nan), (+inf, 0)
107 return z;
108 } else if (is_inf(z.imag()) && z.imag() > 0) {
109 // (+inf, +inf)
110 return {z.real(), std::numeric_limits<double>::quiet_NaN()};
111 } else if (is_inf(z.imag()) && z.imag() < 0) {
112 // (+inf, -inf)
113 return {std::numeric_limits<double>::quiet_NaN(),
114 std::numeric_limits<double>::quiet_NaN()};
115 }
116 }
117 if (is_inf(z.real()) && z.real() < 0
118 && (is_nan(z.imag()) || is_inf(z.imag()))) {
119 // (-inf, nan), (-inf, -inf), (-inf, inf)
120 return {0, 0};
121 }
122 if (is_nan(z.real()) && z.imag() == -0.0) {
123 // (nan, -0)
124 return z;
125 }
126 V exp_re = exp(z.real());
127 return {exp_re * cos(z.imag()), exp_re * sin(z.imag())};
128}
129} // namespace internal
130} // namespace math
131} // namespace stan
132
133#endif
require_t< container_type_check_base< is_container, base_type_t, TypeCheck, Check... > > require_container_bt
Require type satisfies is_container.
std::complex< V > complex_exp(const std::complex< V > &z)
Return the natural (base e) complex exponentiation of the specified complex argument.
Definition exp.hpp:103
fvar< T > sin(const fvar< T > &x)
Definition sin.hpp:16
bool is_nan(T &&x)
Returns 1 if the input's value is NaN and 0 otherwise.
Definition is_nan.hpp:22
fvar< T > cos(const fvar< T > &x)
Definition cos.hpp:16
int is_inf(const fvar< T > &x)
Returns 1 if the input's value is infinite and 0 otherwise.
Definition is_inf.hpp:21
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Base template class for vectorization of unary scalar functions defined by a template class F to a sc...
static auto fun(T &&x)
Return the exponential of the specified scalar argument.
Definition exp.hpp:56
Structure to wrap exp() so that it can be vectorized.
Definition exp.hpp:47