Automatic Differentiation
 
Loading...
Searching...
No Matches
to_vector.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_TO_VECTOR_HPP
2#define STAN_MATH_PRIM_FUN_TO_VECTOR_HPP
3
6#include <vector>
7
8namespace stan {
9namespace math {
10
11// vector to_vector(matrix)
12// vector to_vector(row_vector)
13// vector to_vector(vector)
14template <typename EigMat, require_eigen_t<EigMat>* = nullptr>
15inline 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[])
27template <typename T>
28inline 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[])
34inline 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
auto to_vector(T_x &&x)
Returns input matrix reshaped into a vector.
Definition to_vector.hpp:21
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...