1 #ifndef STAN_MATH_PRIM_FUN_FABS_HPP
2 #define STAN_MATH_PRIM_FUN_FABS_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 #include <stan/math/prim/functor/apply_scalar_unary.hpp>
7 #include <stan/math/prim/functor/apply_vector_unary.hpp>
8 #include <cmath>
9 
10 namespace stan {
11 namespace math {
12 
13 template <typename T, require_arithmetic_t<T>* = nullptr>
fabs(T x)14 auto fabs(T x) {
15   return std::abs(x);
16 }
17 
18 template <typename T, require_complex_t<T>* = nullptr>
fabs(T x)19 auto fabs(T x) {
20   return hypot(x.real(), x.imag());
21 }
22 
23 /**
24  * Structure to wrap `fabs()` so that it can be vectorized.
25  *
26  * @tparam T type of variable
27  * @param x variable
28  * @return Absolute value of x.
29  */
30 struct fabs_fun {
31   template <typename T>
funstan::math::fabs_fun32   static inline T fun(const T& x) {
33     return fabs(x);
34   }
35 };
36 
37 /**
38  * Returns the elementwise `fabs()` of the input,
39  * which may be a scalar or any Stan container of numeric scalars.
40  *
41  * @tparam Container type of container
42  * @param x container
43  * @return Absolute value of each value in x.
44  */
45 template <typename Container,
46           require_not_container_st<std::is_arithmetic, Container>* = nullptr,
47           require_not_var_matrix_t<Container>* = nullptr,
48           require_all_not_nonscalar_prim_or_rev_kernel_expression_t<
49               Container>* = nullptr,
50           require_not_stan_scalar_t<Container>* = nullptr>
fabs(const Container & x)51 inline auto fabs(const Container& x) {
52   return apply_scalar_unary<fabs_fun, Container>::apply(x);
53 }
54 
55 /**
56  * Version of `fabs()` that accepts std::vectors, Eigen Matrix/Array objects
57  *  or expressions, and containers of these.
58  *
59  * @tparam Container Type of x
60  * @param x Container
61  * @return Absolute value of each value in x.
62  */
63 template <typename Container,
64           require_container_st<std::is_arithmetic, Container>* = nullptr>
fabs(const Container & x)65 inline auto fabs(const Container& x) {
66   return apply_vector_unary<Container>::apply(
67       x, [](const auto& v) { return v.array().abs(); });
68 }
69 
70 }  // namespace math
71 }  // namespace stan
72 
73 #endif
74