1 #ifndef STAN_MATH_PRIM_FUN_TO_VECTOR_HPP
2 #define STAN_MATH_PRIM_FUN_TO_VECTOR_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 // vector to_vector(matrix)
12 // vector to_vector(row_vector)
13 // vector to_vector(vector)
14 template <typename EigMat, require_eigen_t<EigMat>* = nullptr>
to_vector(const EigMat & matrix)15 inline Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, 1> to_vector(
16     const EigMat& matrix) {
17   using T = value_type_t<EigMat>;
18   Eigen::Matrix<T, Eigen::Dynamic, 1> res(matrix.size());
19   Eigen::Map<
20       Eigen::Matrix<T, EigMat::RowsAtCompileTime, EigMat::ColsAtCompileTime>>
21       res_map(res.data(), matrix.rows(), matrix.cols());
22   res_map = matrix;
23   return res;
24 }
25 
26 // vector to_vector(real[])
27 template <typename T>
to_vector(const std::vector<T> & vec)28 inline Eigen::Matrix<T, Eigen::Dynamic, 1> to_vector(
29     const std::vector<T>& vec) {
30   return Eigen::Matrix<T, Eigen::Dynamic, 1>::Map(vec.data(), vec.size());
31 }
32 
33 // vector to_vector(int[])
to_vector(const std::vector<int> & vec)34 inline Eigen::Matrix<double, Eigen::Dynamic, 1> to_vector(
35     const std::vector<int>& vec) {
36   int R = vec.size();
37   Eigen::Matrix<double, Eigen::Dynamic, 1> result(R);
38   for (int i = 0; i < R; i++) {
39     result(i) = vec[i];
40   }
41   return result;
42 }
43 
44 }  // namespace math
45 }  // namespace stan
46 #endif
47