1 #ifndef STAN_MATH_PRIM_FUN_ATANH_HPP
2 #define STAN_MATH_PRIM_FUN_ATANH_HPP
3
4 #include <stan/math/prim/core.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/meta.hpp>
7 #include <stan/math/prim/fun/copysign.hpp>
8 #include <stan/math/prim/fun/is_nan.hpp>
9 #include <stan/math/prim/fun/log.hpp>
10 #include <stan/math/prim/functor/apply_scalar_unary.hpp>
11 #include <cmath>
12 #include <complex>
13
14 namespace stan {
15 namespace math {
16
17 /**
18 * Return the inverse hyperbolic tangent of the specified value.
19 * An argument of -1 returns negative infinity and an argument of 1
20 * returns infinity.
21 * Returns nan for nan argument.
22 *
23 * @param[in] x Argument.
24 * @return Inverse hyperbolic tangent of the argument.
25 * @throw std::domain_error If argument is not in [-1, 1].
26 */
atanh(double x)27 inline double atanh(double x) {
28 if (is_nan(x)) {
29 return x;
30 } else {
31 check_bounded("atanh", "x", x, -1.0, 1.0);
32 return std::atanh(x);
33 }
34 }
35
36 /**
37 * Integer version of atanh.
38 *
39 * @param[in] x Argument.
40 * @return Inverse hyperbolic tangent of the argument.
41 * @throw std::domain_error If argument is less than 1.
42 */
atanh(int x)43 inline double atanh(int x) {
44 check_bounded("atanh", "x", x, -1, 1);
45 return std::atanh(x);
46 }
47
48 /**
49 * Structure to wrap atanh() so it can be vectorized.
50 */
51 struct atanh_fun {
52 /**
53 * Return the inverse hyperbolic tangent of the specified argument.
54 *
55 * @tparam T type of argument
56 * @param x argument
57 * @return Inverse hyperbolic tangent of the argument.
58 */
59 template <typename T>
funstan::math::atanh_fun60 static inline T fun(const T& x) {
61 return atanh(x);
62 }
63 };
64
65 /**
66 * Return the elementwise application of <code>atanh()</code> to
67 * specified argument container. The return type promotes the
68 * underlying scalar argument type to double if it is an integer,
69 * and otherwise is the argument type.
70 *
71 * @tparam T type of container
72 * @param x container
73 * @return Elementwise atanh of members of container.
74 */
75 template <
76 typename T, require_not_var_matrix_t<T>* = nullptr,
77 require_all_not_nonscalar_prim_or_rev_kernel_expression_t<T>* = nullptr>
atanh(const T & x)78 inline auto atanh(const T& x) {
79 return apply_scalar_unary<atanh_fun, T>::apply(x);
80 }
81
82 namespace internal {
83 /**
84 * Return the hyperbolic arc tangent of the complex argument.
85 *
86 * @tparam V value type of argument
87 * @param[in] z argument
88 * @return hyperbolic arc tangent of the argument
89 */
90 template <typename V>
complex_atanh(const std::complex<V> & z)91 inline std::complex<V> complex_atanh(const std::complex<V>& z) {
92 std::complex<double> y_d = atanh(value_of_rec(z));
93 V one(1);
94 auto y = 0.5 * (log(one + z) - log(one - z));
95 return copysign(y, y_d);
96 }
97 } // namespace internal
98
99 } // namespace math
100 } // namespace stan
101
102 #endif
103