1 #ifndef STAN_MATH_PRIM_FUN_ATAN_HPP
2 #define STAN_MATH_PRIM_FUN_ATAN_HPP
3
4 #include <stan/math/prim/core.hpp>
5 #include <stan/math/prim/meta.hpp>
6 #include <stan/math/prim/fun/atanh.hpp>
7 #include <stan/math/prim/fun/Eigen.hpp>
8 #include <stan/math/prim/fun/i_times.hpp>
9 #include <stan/math/prim/functor/apply_scalar_unary.hpp>
10 #include <stan/math/prim/functor/apply_vector_unary.hpp>
11 #include <cmath>
12 #include <complex>
13
14 namespace stan {
15 namespace math {
16
17 /**
18 * Structure to wrap \c atan() so it can be vectorized.
19 *
20 * @tparam T type of variable
21 * @param x variable
22 * @return Arctan of x in radians.
23 */
24 struct atan_fun {
25 template <typename T>
funstan::math::atan_fun26 static inline T fun(const T& x) {
27 using std::atan;
28 return atan(x);
29 }
30 };
31
32 /**
33 * Returns the elementwise \c atan() of the input,
34 * which may be a scalar or any Stan container of numeric scalars.
35 *
36 * @tparam Container type of container
37 * @param x container
38 * @return Arctan of each value in x, in radians.
39 */
40 template <typename Container,
41 require_not_container_st<std::is_arithmetic, Container>* = nullptr,
42 require_not_var_matrix_t<Container>* = nullptr,
43 require_all_not_nonscalar_prim_or_rev_kernel_expression_t<
44 Container>* = nullptr>
atan(const Container & x)45 inline auto atan(const Container& x) {
46 return apply_scalar_unary<atan_fun, Container>::apply(x);
47 }
48
49 /**
50 * Version of atan() that accepts std::vectors, Eigen Matrix/Array objects,
51 * or expressions, and containers of these.
52 *
53 * @tparam Container Type of x
54 * @param x Container
55 * @return Elementwise atan of members of container.
56 */
57 template <typename Container,
58 require_container_st<std::is_arithmetic, Container>* = nullptr>
atan(const Container & x)59 inline auto atan(const Container& x) {
60 return apply_vector_unary<Container>::apply(
61 x, [](const auto& v) { return v.array().atan(); });
62 }
63
64 namespace internal {
65 /**
66 * Return the arc tangent of the complex argument.
67 *
68 * @tparam V value type of argument
69 * @param[in] z argument
70 * @return arc tangent of the argument
71 */
72 template <typename V>
complex_atan(const std::complex<V> & z)73 inline std::complex<V> complex_atan(const std::complex<V>& z) {
74 return neg_i_times(atanh(i_times(z)));
75 }
76 } // namespace internal
77
78 } // namespace math
79 } // namespace stan
80
81 #endif
82