Stan Math Library  2.20.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> to_matrix(
26  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> to_matrix(
40  const std::vector<Eigen::Matrix<T, 1, Eigen::Dynamic> >& x) {
41  int rows = x.size();
42  if (rows == 0)
43  return Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>(0, 0);
44  int cols = x[0].size();
45  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> result(rows, cols);
46  for (int i = 0, ij = 0; i < cols; i++)
47  for (int j = 0; j < rows; j++, ij++)
48  result(ij) = x[j][i];
49  return result;
50 }
51 
60 template <typename T>
61 inline Eigen::Matrix<typename boost::math::tools::promote_args<T, double>::type,
62  Eigen::Dynamic, Eigen::Dynamic>
63 to_matrix(const std::vector<std::vector<T> >& x) {
64  using boost::math::tools::promote_args;
65  size_t rows = x.size();
66  if (rows == 0)
67  return Eigen::Matrix<typename promote_args<T, double>::type, Eigen::Dynamic,
68  Eigen::Dynamic>(0, 0);
69  size_t cols = x[0].size();
70  Eigen::Matrix<typename promote_args<T, double>::type, Eigen::Dynamic,
71  Eigen::Dynamic>
72  result(rows, cols);
73  for (size_t i = 0, ij = 0; i < cols; i++)
74  for (size_t j = 0; j < rows; j++, ij++)
75  result(ij) = x[j][i];
76  return result;
77 }
78 
91 template <typename T, int R, int C>
92 inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> to_matrix(
93  const Eigen::Matrix<T, R, C>& x, int m, int n) {
94  static const char* function = "to_matrix(matrix)";
95  check_size_match(function, "rows * columns", m * n, "vector size", x.size());
96  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> y = x;
97  y.resize(m, n);
98  return y;
99 }
100 
113 template <typename T>
114 inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> to_matrix(
115  const std::vector<T>& x, int m, int n) {
116  static const char* function = "to_matrix(array)";
117  check_size_match(function, "rows * columns", m * n, "vector size", x.size());
118  return Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >(
119  &x[0], m, n);
120 }
121 
133 inline Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> to_matrix(
134  const std::vector<int>& x, int m, int n) {
135  static const char* function = "to_matrix(array)";
136  int size = x.size();
137  check_size_match(function, "rows * columns", m * n, "vector size", size);
138  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> result(m, n);
139  for (int i = 0; i < size; i++)
140  result(i) = x[i];
141  return result;
142 }
143 
160 template <typename T, int R, int C>
161 inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> to_matrix(
162  const Eigen::Matrix<T, R, C>& x, int m, int n, bool col_major) {
163  if (col_major)
164  return to_matrix(x, m, n);
165  check_size_match("to_matrix", "rows * columns", m * n, "matrix size",
166  x.size());
167  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> result(m, n);
168  for (int i = 0, ij = 0; i < m; i++)
169  for (int j = 0; j < n; j++, ij++)
170  result(i, j) = x(ij);
171  return result;
172 }
173 
190 template <typename T>
191 inline Eigen::Matrix<typename boost::math::tools::promote_args<T, double>::type,
192  Eigen::Dynamic, Eigen::Dynamic>
193 to_matrix(const std::vector<T>& x, int m, int n, bool col_major) {
194  if (col_major)
195  return to_matrix(x, m, n);
196  check_size_match("to_matrix", "rows * columns", m * n, "matrix size",
197  x.size());
198  Eigen::Matrix<typename boost::math::tools::promote_args<T, double>::type,
199  Eigen::Dynamic, Eigen::Dynamic>
200  result(m, n);
201  for (int i = 0, ij = 0; i < m; i++)
202  for (int j = 0; j < n; j++, ij++)
203  result(i, j) = x[ij];
204  return result;
205 }
206 
207 } // namespace math
208 } // namespace stan
209 #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:25
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17

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