1 #ifndef STAN_MATH_PRIM_FUN_TRACE_HPP
2 #define STAN_MATH_PRIM_FUN_TRACE_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 
7 namespace stan {
8 namespace math {
9 
10 /**
11  * Returns the trace of the specified matrix.  The trace
12  * is defined as the sum of the elements on the diagonal.
13  * The matrix is not required to be square.  Returns 0 if
14  * matrix is empty.
15  *
16  * @tparam T type of the elements in the matrix
17  * @param[in] m Specified matrix.
18  * @return Trace of the matrix.
19  */
20 template <typename T, require_eigen_t<T>* = nullptr,
21           require_not_st_var<T>* = nullptr>
trace(const T & m)22 inline value_type_t<T> trace(const T& m) {
23   return m.trace();
24 }
25 
26 }  // namespace math
27 }  // namespace stan
28 
29 #endif
30