1 #ifndef STAN_MATH_PRIM_FUN_TO_ARRAY_1D_HPP
2 #define STAN_MATH_PRIM_FUN_TO_ARRAY_1D_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_1d(matrix)
12 // real[] to_array_1d(row_vector)
13 // real[] to_array_1d(vector)
14 template <typename EigMat, require_eigen_t<EigMat>* = nullptr>
to_array_1d(const EigMat & matrix)15 inline std::vector<value_type_t<EigMat>> to_array_1d(const EigMat& matrix) {
16   using T_val = value_type_t<EigMat>;
17   std::vector<T_val> result(matrix.size());
18   Eigen::Map<Eigen::Matrix<T_val, EigMat::RowsAtCompileTime,
19                            EigMat::ColsAtCompileTime>>(
20       result.data(), matrix.rows(), matrix.cols())
21       = matrix;
22   return result;
23 }
24 
25 // real[] to_array_1d(...)
26 template <typename T>
to_array_1d(const std::vector<T> & x)27 inline std::vector<T> to_array_1d(const std::vector<T>& x) {
28   return x;
29 }
30 
31 // real[] to_array_1d(...)
32 template <typename T>
to_array_1d(const std::vector<std::vector<T>> & x)33 inline std::vector<typename scalar_type<T>::type> to_array_1d(
34     const std::vector<std::vector<T>>& x) {
35   size_t size1 = x.size();
36   size_t size2 = 0;
37   if (size1 != 0) {
38     size2 = x[0].size();
39   }
40   std::vector<T> y(size1 * size2);
41   for (size_t i = 0, ij = 0; i < size1; i++) {
42     for (size_t j = 0; j < size2; j++, ij++) {
43       y[ij] = x[i][j];
44     }
45   }
46   return to_array_1d(y);
47 }
48 
49 }  // namespace math
50 }  // namespace stan
51 #endif
52