Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
to_matrix.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_TO_MATRIX_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_TO_MATRIX_HPP
3 
5  // stan::scalar_type
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  // matrix to_matrix(matrix)
16  // matrix to_matrix(vector)
17  // matrix to_matrix(row_vector)
18  template <typename T, int R, int C>
19  inline Matrix<T, Dynamic, Dynamic>
20  to_matrix(Matrix<T, R, C> matrix) {
21  return matrix;
22  }
23 
24  // matrix to_matrix(real[, ])
25  template <typename T>
26  inline Matrix<T, Dynamic, Dynamic>
27  to_matrix(const vector< vector<T> > & vec) {
28  size_t R = vec.size();
29  if (R != 0) {
30  size_t C = vec[0].size();
31  Matrix<T, Dynamic, Dynamic> result(R, C);
32  T* datap = result.data();
33  for (size_t i=0, ij=0; i < C; i++)
34  for (size_t j=0; j < R; j++, ij++)
35  datap[ij] = vec[j][i];
36  return result;
37  } else {
38  return Matrix<T, Dynamic, Dynamic> (0, 0);
39  }
40  }
41 
42  // matrix to_matrix(int[, ])
43  inline Matrix<double, Dynamic, Dynamic>
44  to_matrix(const vector< vector<int> > & vec) {
45  size_t R = vec.size();
46  if (R != 0) {
47  size_t C = vec[0].size();
48  Matrix<double, Dynamic, Dynamic> result(R, C);
49  double* datap = result.data();
50  for (size_t i=0, ij=0; i < C; i++)
51  for (size_t j=0; j < R; j++, ij++)
52  datap[ij] = vec[j][i];
53  return result;
54  } else {
55  return Matrix<double, Dynamic, Dynamic> (0, 0);
56  }
57  }
58 
59  }
60 }
61 #endif
Matrix< T, Dynamic, Dynamic > to_matrix(Matrix< T, R, C > matrix)
Definition: to_matrix.hpp:20

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