1 #ifndef STAN_MATH_PRIM_FUN_IDENTITY_MATRIX_HPP
2 #define STAN_MATH_PRIM_FUN_IDENTITY_MATRIX_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 a square identity matrix
13  *
14  * @param K size of the matrix
15  * @return An identity matrix of size K.
16  * @throw std::domain_error if K is negative.
17  */
18 template <typename T = Eigen::MatrixXd, require_eigen_t<T>* = nullptr>
identity_matrix(int K)19 inline auto identity_matrix(int K) {
20   check_nonnegative("identity_matrix", "size", K);
21   return T::Identity(K, K);
22 }
23 
24 }  // namespace math
25 }  // namespace stan
26 
27 #endif
28