Automatic Differentiation
 
Loading...
Searching...
No Matches
tanh.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_TANH_HPP
2#define STAN_MATH_PRIM_FUN_TANH_HPP
3
11#include <cmath>
12#include <complex>
13
14namespace stan {
15namespace math {
16
24template <typename T, require_arithmetic_t<T>* = nullptr>
25inline auto tanh(T&& x) {
26 return std::tanh(x);
27}
28
36template <typename T, require_complex_bt<std::is_arithmetic, T>* = nullptr>
37inline auto tanh(T&& x) {
38 return std::tanh(x);
39}
40
48struct tanh_fun {
49 template <typename T>
50 static inline auto fun(T&& x) {
51 return tanh(std::forward<T>(x));
52 }
53};
54
62template <typename Container, require_ad_container_t<Container>* = nullptr>
63inline auto tanh(Container&& x) {
65 std::forward<Container>(x));
66}
67
76template <typename Container,
78inline auto tanh(Container&& x) {
79 auto&& x_ref = to_ref(std::forward<Container>(x));
80 return apply_vector_unary<decltype(x_ref)>::apply(
81 std::forward<decltype(x_ref)>(x_ref),
82 [](auto&& v) { return v.array().tanh(); });
83}
84
85namespace internal {
93template <typename V>
94inline std::complex<V> complex_tanh(const std::complex<V>& z) {
95 auto exp_z = exp(z);
96 auto exp_neg_z = exp(-z);
97 return stan::math::internal::complex_divide(exp_z - exp_neg_z,
98 exp_z + exp_neg_z);
99}
100} // namespace internal
101
102} // namespace math
103} // namespace stan
104
105#endif
require_t< container_type_check_base< is_container, base_type_t, TypeCheck, Check... > > require_container_bt
Require type satisfies is_container.
complex_return_t< U, V > complex_divide(const U &lhs, const V &rhs)
Return the quotient of the specified arguments.
std::complex< V > complex_tanh(const std::complex< V > &z)
Return the hyperbolic tangent of the complex argument.
Definition tanh.hpp:94
fvar< T > tanh(const fvar< T > &x)
Definition tanh.hpp:15
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:51
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)
Definition tanh.hpp:50
Structure to wrap tanh() so that it can be vectorized.
Definition tanh.hpp:48