Automatic Differentiation
 
Loading...
Searching...
No Matches
log_cosh.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_LOG_COSH_HPP
2#define STAN_MATH_REV_FUN_LOG_COSH_HPP
3
9#include <utility>
10
11namespace stan {
12namespace math {
13
21template <typename T, require_arithmetic_t<T>* = nullptr>
22inline var log_cosh(const var_value<T>& x) {
23 const double tanh_x = tanh(x.val());
24 return make_callback_var(log_cosh(x.val()), [x, tanh_x](auto& vi) mutable {
25 x.adj() += vi.adj() * tanh_x;
26 });
27}
28
37template <typename T, require_var_matrix_t<T>* = nullptr>
38inline auto log_cosh(T&& x) {
39 auto arena_x = to_arena(std::forward<T>(x));
40 arena_t<plain_type_t<value_type_t<T>>> tanh_x = tanh(arena_x.val());
41 return make_callback_var(
42 log_cosh(arena_x.val()), [arena_x, tanh_x](auto& vi) mutable {
43 arena_x.adj().array() += vi.adj().array() * tanh_x.array();
44 });
45}
46
47} // namespace math
48} // namespace stan
49#endif
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
Definition to_arena.hpp:25
fvar< T > tanh(const fvar< T > &x)
Return the hyperbolic tangent of the argument.
Definition tanh.hpp:27
fvar< T > log_cosh(const fvar< T > &x)
Return the natural logarithm of the hyperbolic cosine of the argument.
Definition log_cosh.hpp:21
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...