Automatic Differentiation
 
Loading...
Searching...
No Matches
log_cosh.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_LOG_COSH_HPP
2#define STAN_MATH_PRIM_FUN_LOG_COSH_HPP
3
10#include <utility>
11
12namespace stan {
13namespace math {
14
25template <typename T, require_arithmetic_t<T>* = nullptr>
26inline double log_cosh(T x) {
27 const double abs_x = fabs(static_cast<double>(x));
28 if (abs_x < 1.0) {
29 const double expm1_abs_x = expm1(abs_x);
30 return log1p(expm1_abs_x * expm1_abs_x / (2.0 * (1.0 + expm1_abs_x)));
31 }
32 const double exp_neg_abs_x = exp(-abs_x);
33 return abs_x + log1p(0.5 * (exp_neg_abs_x * exp_neg_abs_x - 1.0));
34}
35
47 template <typename T>
48 static inline auto fun(const T& x) {
49 return log_cosh(x);
50 }
51};
52
60template <typename T, require_container_t<T>* = nullptr,
61 require_not_var_matrix_t<T>* = nullptr,
62 require_not_nonscalar_prim_or_rev_kernel_expression_t<T>* = nullptr>
63inline auto log_cosh(T&& x) {
64 return apply_scalar_unary<log_cosh_fun, T>::apply(std::forward<T>(x));
65}
66
67} // namespace math
68} // namespace stan
69
70#endif
fvar< T > expm1(const fvar< T > &x)
Definition expm1.hpp:14
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
fvar< T > log_cosh(const fvar< T > &x)
Return the natural logarithm of the hyperbolic cosine of the argument.
Definition log_cosh.hpp:21
fvar< T > fabs(const fvar< T > &x)
Definition fabs.hpp:16
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(const T &x)
Return the natural logarithm of the hyperbolic cosine of the argument.
Definition log_cosh.hpp:48
Structure to wrap log_cosh() so it can be vectorized.
Definition log_cosh.hpp:39