1 #ifndef STAN_MATH_REV_FUN_ABS_HPP
2 #define STAN_MATH_REV_FUN_ABS_HPP
3 
4 #include <stan/math/prim/fun/abs.hpp>
5 #include <stan/math/rev/meta.hpp>
6 #include <stan/math/rev/fun/fabs.hpp>
7 #include <stan/math/rev/fun/hypot.hpp>
8 #include <complex>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Return the absolute value of the variable (std).
15  *
16  * Delegates to <code>fabs()</code> (see for doc).
17  *
18    \f[
19    \mbox{abs}(x) =
20    \begin{cases}
21      |x| & \mbox{if } -\infty\leq x\leq \infty \\[6pt]
22      \textrm{NaN} & \mbox{if } x = \textrm{NaN}
23    \end{cases}
24    \f]
25 
26    \f[
27    \frac{\partial\, \mbox{abs}(x)}{\partial x} =
28    \begin{cases}
29      -1 & \mbox{if } x < 0 \\
30      0 & \mbox{if } x = 0 \\
31      1 & \mbox{if } x > 0 \\[6pt]
32      \textrm{NaN} & \mbox{if } x = \textrm{NaN}
33    \end{cases}
34    \f]
35  *
36  * @tparam T A floating point type or an Eigen type with floating point scalar.
37  * @param a Variable input.
38  * @return Absolute value of variable.
39  */
40 template <typename T>
abs(const var_value<T> & a)41 inline auto abs(const var_value<T>& a) {
42   return fabs(a);
43 }
44 
45 /**
46  * Return the absolute value of the complex argument.
47  *
48  * @param[in] z argument
49  * @return absolute value of the argument
50  */
abs(const std::complex<var> & z)51 inline var abs(const std::complex<var>& z) { return internal::complex_abs(z); }
52 
53 }  // namespace math
54 }  // namespace stan
55 #endif
56