Stan Math Library  2.20.0
reverse mode automatic differentiation
to_array_1d.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_TO_ARRAY_1D_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_TO_ARRAY_1D_HPP
3 
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 T, int R, int C>
15 inline std::vector<T> to_array_1d(const Eigen::Matrix<T, R, C>& matrix) {
16  const T* datap = matrix.data();
17  int size = matrix.size();
18  std::vector<T> result(size);
19  for (int i = 0; i < size; i++)
20  result[i] = datap[i];
21  return result;
22 }
23 
24 // real[] to_array_1d(...)
25 template <typename T>
26 inline std::vector<T> to_array_1d(const std::vector<T>& x) {
27  return x;
28 }
29 
30 // real[] to_array_1d(...)
31 template <typename T>
32 inline std::vector<typename scalar_type<T>::type> to_array_1d(
33  const std::vector<std::vector<T> >& x) {
34  size_t size1 = x.size();
35  size_t size2 = 0;
36  if (size1 != 0)
37  size2 = x[0].size();
38  std::vector<T> y(size1 * size2);
39  for (size_t i = 0, ij = 0; i < size1; i++)
40  for (size_t j = 0; j < size2; j++, ij++)
41  y[ij] = x[i][j];
42  return to_array_1d(y);
43 }
44 
45 } // namespace math
46 } // namespace stan
47 #endif
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
std::vector< T > to_array_1d(const Eigen::Matrix< T, R, C > &matrix)
Definition: to_array_1d.hpp:15

     [ Stan Home Page ] © 2011–2018, Stan Development Team.