Automatic Differentiation
 
Loading...
Searching...
No Matches
tanh.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_TANH_HPP
2#define STAN_MATH_FWD_FUN_TANH_HPP
3
12#include <complex>
13
14namespace stan {
15namespace math {
16
26template <typename T>
27inline fvar<T> tanh(const fvar<T>& x) {
28 const T tanh_x = tanh(x.val_);
29 const double x_val = value_of_rec(x.val_);
30 if (fabs(x_val) < 0.5) {
31 return fvar<T>(tanh_x, x.d_ * (1 - tanh_x * tanh_x));
32 }
33 // exp(-2 |x|), with the sign of x fixed in this branch
34 const T exp_neg_2abs_x = exp(x_val > 0 ? -2 * x.val_ : 2 * x.val_);
35 const T one_plus_exp = 1 + exp_neg_2abs_x;
36 return fvar<T>(tanh_x,
37 x.d_ * (4 * exp_neg_2abs_x / (one_plus_exp * one_plus_exp)));
38}
39
47template <typename T>
48inline std::complex<fvar<T>> tanh(const std::complex<fvar<T>>& z) {
50}
51
52} // namespace math
53} // namespace stan
54#endif
std::complex< V > complex_tanh(const std::complex< V > &z)
Return the hyperbolic tangent of the complex argument.
Definition tanh.hpp:94
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > tanh(const fvar< T > &x)
Return the hyperbolic tangent of the argument.
Definition tanh.hpp:27
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 ...
Scalar val_
The value of this variable.
Definition fvar.hpp:49
Scalar d_
The tangent (derivative) of this variable.
Definition fvar.hpp:61
This template class represents scalars used in forward-mode automatic differentiation,...
Definition fvar.hpp:40