Stan Math Library  2.14.0
reverse mode automatic differentiation
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  // matrix to_matrix(matrix)
12  // matrix to_matrix(vector)
13  // matrix to_matrix(row_vector)
14  template <typename T, int R, int C>
15  inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
16  to_matrix(Eigen::Matrix<T, R, C> matrix) {
17  return matrix;
18  }
19 
20  // matrix to_matrix(real[, ])
21  template <typename T>
22  inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
23  to_matrix(const std::vector< std::vector<T> > & vec) {
24  size_t R = vec.size();
25  if (R != 0) {
26  size_t C = vec[0].size();
27  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> result(R, C);
28  T* datap = result.data();
29  for (size_t i=0, ij=0; i < C; i++)
30  for (size_t j=0; j < R; j++, ij++)
31  datap[ij] = vec[j][i];
32  return result;
33  } else {
34  return Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> (0, 0);
35  }
36  }
37 
38  // matrix to_matrix(int[, ])
39  inline Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>
40  to_matrix(const std::vector< std::vector<int> > & vec) {
41  size_t R = vec.size();
42  if (R != 0) {
43  size_t C = vec[0].size();
44  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> result(R, C);
45  double* datap = result.data();
46  for (size_t i=0, ij=0; i < C; i++)
47  for (size_t j=0; j < R; j++, ij++)
48  datap[ij] = vec[j][i];
49  return result;
50  } else {
51  return Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> (0, 0);
52  }
53  }
54 
55  }
56 }
57 #endif
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > to_matrix(Eigen::Matrix< T, R, C > matrix)
Definition: to_matrix.hpp:16

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