| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/scal/meta/scalar_type.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 T, int R, int C>
15 inline std::vector<T> to_array_1d(
16 const Eigen::Matrix<T, R, C> & matrix
17 ) {
18 const T* datap = matrix.data();
19 int size = matrix.size();
20 std::vector<T> result(size);
21 for (int i=0; i < size; i++)
22 result[i] = datap[i];
23 return result;
24 }
25
26 // real[] to_array_1d(...)
27 template <typename T>
28 inline std::vector<T>
29 to_array_1d(const std::vector<T> & x) {
30 return x;
31 }
32
33 // real[] to_array_1d(...)
34 template <typename T>
35 inline std::vector<typename scalar_type<T>::type>
36 to_array_1d(const std::vector< std::vector<T> > & x) {
37 size_t size1 = x.size();
38 size_t size2 = 0;
39 if (size1 != 0)
40 size2 = x[0].size();
41 std::vector<T> y(size1*size2);
42 for (size_t i = 0, ij = 0; i < size1; i++)
43 for (size_t j = 0; j < size2; j++, ij++)
44 y[ij] = x[i][j];
45 return to_array_1d(y);
46 }
47
48 }
49 }
50 #endif
stan
Definition: log_sum_exp.hpp:8
Eigen.hpp
scalar_type.hpp
stan::math::size
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
stan::math::to_array_1d
std::vector< T > to_array_1d(const Eigen::Matrix< T, R, C > &matrix)
Definition: to_array_1d.hpp:15

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