Stan Math Library  2.15.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 
4 #include <boost/math/tools/promotion.hpp>
8 #include <vector>
9 
10 namespace stan {
11  namespace math {
24  template <typename T, int R, int C>
25  inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
26  to_matrix(const Eigen::Matrix<T, R, C>& x) {
27  return x;
28  }
29 
38  template <typename T>
39  inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
40  to_matrix(const
41  std::vector<Eigen::Matrix<T, 1, Eigen::Dynamic> >& x) {
42  int rows = x.size();
43  if (rows == 0)
44  return Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> (0, 0);
45  int cols = x[0].size();
46  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
47  result(rows, cols);
48  for (int i = 0, ij = 0; i < cols; i++)
49  for (int j = 0; j < rows; j++, ij++)
50  result(ij) = x[j][i];
51  return result;
52  }
53 
62  template <typename T>
63  inline Eigen::Matrix<typename
64  boost::math::tools::promote_args<T, double>::type,
65  Eigen::Dynamic, Eigen::Dynamic>
66  to_matrix(const std::vector< std::vector<T> >& x) {
67  using boost::math::tools::promote_args;
68  size_t rows = x.size();
69  if (rows == 0)
70  return Eigen::Matrix<typename promote_args<T, double>::type,
71  Eigen::Dynamic, Eigen::Dynamic> (0, 0);
72  size_t cols = x[0].size();
73  Eigen::Matrix<typename promote_args<T, double>::type,
74  Eigen::Dynamic, Eigen::Dynamic> result(rows, cols);
75  for (size_t i = 0, ij = 0; i < cols; i++)
76  for (size_t j = 0; j < rows; j++, ij++)
77  result(ij) = x[j][i];
78  return result;
79  }
80 
93  template <typename T, int R, int C>
94  inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
95  to_matrix(const Eigen::Matrix<T, R, C>& x, int m, int n) {
96  static const char* fun = "to_matrix(matrix)";
97  check_size_match(fun, "rows * columns", m * n, "vector size",
98  x.size());
99  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> y = x;
100  y.resize(m, n);
101  return y;
102  }
103 
116  template <typename T>
117  inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
118  to_matrix(const std::vector<T>& x, int m, int n) {
119  static const char* fun = "to_matrix(array)";
120  check_size_match(fun, "rows * columns", m * n, "vector size",
121  x.size());
122  return Eigen::Map<const
123  Eigen::Matrix<T, Eigen::Dynamic,
124  Eigen::Dynamic> >(&x[0], m, n);
125  }
126 
138  inline Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>
139  to_matrix(const std::vector<int>& x, int m, int n) {
140  static const char* fun = "to_matrix(array)";
141  int size = x.size();
142  check_size_match(fun, "rows * columns", m * n,
143  "vector size", size);
144  Eigen::Matrix<double,
145  Eigen::Dynamic, Eigen::Dynamic> result(m, n);
146  for (int i = 0; i < size; i++)
147  result(i) = x[i];
148  return result;
149  }
150 
167  template <typename T, int R, int C>
168  inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
169  to_matrix(const Eigen::Matrix<T, R, C>& x, int m, int n,
170  bool col_major) {
171  if (col_major)
172  return to_matrix(x, m, n);
173  check_size_match("to_matrix", "rows * columns", m * n,
174  "matrix size", x.size());
175  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
176  result(m, n);
177  for (int i = 0, ij = 0; i < m; i++)
178  for (int j = 0; j < n; j++, ij++)
179  result(i, j) = x(ij);
180  return result;
181  }
182 
199  template <typename T>
200  inline
201  Eigen::Matrix<typename
202  boost::math::tools::promote_args<T, double>::type,
203  Eigen::Dynamic, Eigen::Dynamic>
204  to_matrix(const std::vector<T>& x, int m, int n, bool col_major) {
205  if (col_major)
206  return to_matrix(x, m, n);
207  check_size_match("to_matrix", "rows * columns", m * n,
208  "matrix size", x.size());
209  Eigen::Matrix<typename
210  boost::math::tools::promote_args<T, double>::type,
211  Eigen::Dynamic, Eigen::Dynamic>
212  result(m, n);
213  for (int i = 0, ij = 0; i < m; i++)
214  for (int j = 0; j < n; j++, ij++)
215  result(i, j) = x[ij];
216  return result;
217  }
218 
219  }
220 }
221 #endif
int rows(const Eigen::Matrix< T, R, C > &m)
Return the number of rows in the specified matrix, vector, or row vector.
Definition: rows.hpp:20
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
int cols(const Eigen::Matrix< T, R, C > &m)
Return the number of columns in the specified matrix, vector, or row vector.
Definition: cols.hpp:20
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > to_matrix(const Eigen::Matrix< T, R, C > &x)
Returns a matrix with dynamic dimensions constructed from an Eigen matrix which is either a row vecto...
Definition: to_matrix.hpp:26
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17

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