1 #ifndef STAN_MATH_PRIM_FUN_HYPOT_HPP
2 #define STAN_MATH_PRIM_FUN_HYPOT_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/functor/apply_scalar_binary.hpp>
6 #include <cmath>
7 
8 namespace stan {
9 namespace math {
10 
11 /**
12  * Return the length of the hypotenuse of a right triangle with
13  * opposite and adjacent side lengths given by the specified
14  * arguments (C++11).  In symbols, if the arguments are
15  * <code>x</code> and <code>y</code>, the result is <code>sqrt(x *
16  * x + y * y)</code>.
17  *
18  * @param x First argument.
19  * @param y Second argument.
20  * @return Length of hypotenuse of right triangle with opposite
21  * and adjacent side lengths x and y.
22  */
23 template <typename T1, typename T2, require_all_arithmetic_t<T1, T2>* = nullptr>
hypot(T1 x,T2 y)24 inline double hypot(T1 x, T2 y) {
25   using std::hypot;
26   return hypot(x, y);
27 }
28 
29 /**
30  * Enables the vectorised application of the hypot function,
31  * when the first and/or second arguments are containers.
32  *
33  * @tparam T1 type of first input
34  * @tparam T2 type of second input
35  * @param a First input
36  * @param b Second input
37  * @return hypot function applied to the two inputs.
38  */
39 template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr,
40           require_all_not_nonscalar_prim_or_rev_kernel_expression_t<
41               T1, T2>* = nullptr>
hypot(const T1 & a,const T2 & b)42 inline auto hypot(const T1& a, const T2& b) {
43   return apply_scalar_binary(
44       a, b, [&](const auto& c, const auto& d) { return hypot(c, d); });
45 }
46 
47 }  // namespace math
48 }  // namespace stan
49 #endif
50