1 #ifndef STAN_MATH_PRIM_FUN_TO_ARRAY_2D_HPP
2 #define STAN_MATH_PRIM_FUN_TO_ARRAY_2D_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 #include <vector>
7 
8 namespace stan {
9 namespace math {
10 
11 // real[, ] to_array_2d(matrix)
12 template <typename EigMat, require_eigen_t<EigMat>* = nullptr>
to_array_2d(const EigMat & matrix)13 inline std::vector<std::vector<value_type_t<EigMat>>> to_array_2d(
14     const EigMat& matrix) {
15   using std::vector;
16   using T = value_type_t<EigMat>;
17   const Eigen::Ref<const Eigen::Matrix<T, EigMat::RowsAtCompileTime,
18                                        EigMat::ColsAtCompileTime>>& mat_ref
19       = matrix;
20   int C = matrix.cols();
21   int R = matrix.rows();
22   vector<vector<T>> result(R, vector<T>(C));
23   for (int i = 0, ij = 0; i < C; i++) {
24     for (int j = 0; j < R; j++, ij++) {
25       result[j][i] = mat_ref.data()[ij];
26     }
27   }
28   return result;
29 }
30 
31 }  // namespace math
32 }  // namespace stan
33 #endif
34