1 #ifndef STAN_MATH_PRIM_FUN_EIGENVALUES_SYM_HPP
2 #define STAN_MATH_PRIM_FUN_EIGENVALUES_SYM_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/Eigen.hpp>
7 
8 namespace stan {
9 namespace math {
10 
11 /**
12  * Return the eigenvalues of the specified symmetric matrix
13  * in descending order of magnitude.  This function is more
14  * efficient than the general eigenvalues function for symmetric
15  * matrices.
16  * <p>See <code>eigen_decompose()</code> for more information.
17  *
18  * @tparam EigMat type of the matrix
19  * @param m Specified matrix.
20  * @return Eigenvalues of matrix.
21  */
22 template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr,
23           require_not_st_var<EigMat>* = nullptr>
eigenvalues_sym(const EigMat & m)24 Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, 1> eigenvalues_sym(
25     const EigMat& m) {
26   using PlainMat = plain_type_t<EigMat>;
27   const PlainMat& m_eval = m;
28   check_nonzero_size("eigenvalues_sym", "m", m_eval);
29   check_symmetric("eigenvalues_sym", "m", m_eval);
30 
31   Eigen::SelfAdjointEigenSolver<PlainMat> solver(m_eval,
32                                                  Eigen::EigenvaluesOnly);
33   return solver.eigenvalues();
34 }
35 
36 }  // namespace math
37 }  // namespace stan
38 
39 #endif
40