Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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  using Eigen::Dynamic;
12  using Eigen::Matrix;
13  using std::vector;
14 
15  // real[] to_array_1d(matrix)
16  // real[] to_array_1d(row_vector)
17  // real[] to_array_1d(vector)
18  template <typename T, int R, int C>
19  inline vector<T> to_array_1d(const Matrix<T, R, C> & matrix) {
20  const T* datap = matrix.data();
21  int size = matrix.size();
22  vector<T> result(size);
23  for (int i=0; i < size; i++)
24  result[i] = datap[i];
25  return result;
26  }
27 
28  // real[] to_array_1d(...)
29  template <typename T>
30  inline vector<T>
31  to_array_1d(const vector<T> & x) {
32  return x;
33  }
34 
35  // real[] to_array_1d(...)
36  template <typename T>
37  inline vector<typename scalar_type<T>::type>
38  to_array_1d(const vector< vector<T> > & x) {
39  size_t size1 = x.size();
40  size_t size2 = 0;
41  if (size1 != 0)
42  size2 = x[0].size();
43  vector<T> y(size1*size2);
44  for (size_t i = 0, ij = 0; i < size1; i++)
45  for (size_t j = 0; j < size2; j++, ij++)
46  y[ij] = x[i][j];
47  return to_array_1d(y);
48  }
49 
50  }
51 }
52 #endif
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
vector< T > to_array_1d(const Matrix< T, R, C > &matrix)
Definition: to_array_1d.hpp:19

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