1 #ifndef STAN_MATH_PRIM_FUN_SD_HPP
2 #define STAN_MATH_PRIM_FUN_SD_HPP
3 
4 #include <stan/math/prim/err.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 #include <stan/math/prim/fun/variance.hpp>
7 #include <stan/math/prim/fun/sqrt.hpp>
8 #include <vector>
9 #include <cmath>
10 
11 namespace stan {
12 namespace math {
13 
14 /**
15  * Returns the unbiased sample standard deviation of the
16  * coefficients in the specified std vector, column vector, row vector, or
17  * matrix.
18  *
19  * @tparam T type of the container
20  *
21  * @param m Specified container.
22  * @return Sample variance.
23  */
24 template <typename T, require_container_t<T>* = nullptr,
25           require_not_st_var<T>* = nullptr>
sd(const T & m)26 inline auto sd(const T& m) {
27   using std::sqrt;
28 
29   return apply_vector_unary<T>::reduce(m, [](const auto& x) {
30     check_nonzero_size("sd", "x", x);
31 
32     if (x.size() == 1) {
33       return scalar_type_t<T>(0.0);
34     }
35 
36     return sqrt(variance(x));
37   });
38 }
39 
40 }  // namespace math
41 }  // namespace stan
42 
43 #endif
44