Automatic Differentiation
 
Loading...
Searching...
No Matches
to_matrix.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_TO_MATRIX_HPP
2#define STAN_MATH_PRIM_FUN_TO_MATRIX_HPP
3
7#include <vector>
8
9namespace stan {
10namespace math {
11
21template <typename EigMat, require_eigen_dense_dynamic_t<EigMat>* = nullptr>
22inline EigMat to_matrix(EigMat&& x) {
23 return std::forward<EigMat>(x);
24}
25
36template <typename EigVec, require_eigen_vector_t<EigVec>* = nullptr>
37inline auto to_matrix(EigVec&& matrix) {
38 return Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>(
39 std::forward<EigVec>(matrix));
40}
41
50template <typename T>
51inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> to_matrix(
52 const std::vector<Eigen::Matrix<T, 1, Eigen::Dynamic>>& x) {
53 int rows = x.size();
54 if (rows == 0) {
55 return {};
56 }
57 int cols = x[0].size();
58 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> result(rows, cols);
59 for (int i = 0, ij = 0; i < cols; i++) {
60 for (int j = 0; j < rows; j++, ij++) {
61 result.coeffRef(ij) = x[j][i];
62 }
63 }
64 return result;
65}
66
75template <typename T>
76inline Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
77to_matrix(const std::vector<std::vector<T>>& x) {
78 size_t rows = x.size();
79 if (rows == 0) {
80 return {};
81 }
82 size_t cols = x[0].size();
83 Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
84 result(rows, cols);
85 for (size_t i = 0, ij = 0; i < cols; i++) {
86 for (size_t j = 0; j < rows; j++, ij++) {
87 result.coeffRef(ij) = x[j][i];
88 }
89 }
90 return result;
91}
92
106template <typename EigMat, require_eigen_t<EigMat>* = nullptr>
107inline Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic>
108to_matrix(EigMat&& x, int m, int n) {
109 static constexpr const char* function = "to_matrix(matrix)";
110 check_size_match(function, "rows * columns", m * n, "vector size", x.size());
111 Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic> y
112 = std::forward<EigMat>(x);
113 y.resize(m, n);
114 return y;
115}
116
129template <typename T>
130inline auto to_matrix(const std::vector<T>& x, int m, int n) {
131 static constexpr const char* function = "to_matrix(array)";
132 check_size_match(function, "rows * columns", m * n, "vector size", x.size());
133 return Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>>(
134 &x[0], m, n);
135}
136
148inline Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> to_matrix(
149 const std::vector<int>& x, int m, int n) {
150 static constexpr const char* function = "to_matrix(array)";
151 int x_size = x.size();
152 check_size_match(function, "rows * columns", m * n, "vector size", x_size);
153 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> result(m, n);
154 for (int i = 0; i < x_size; i++) {
155 result.coeffRef(i) = x[i];
156 }
157 return result;
158}
159
177template <typename EigMat, require_eigen_t<EigMat>* = nullptr>
178inline Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic>
179to_matrix(EigMat&& x, int m, int n, bool col_major) {
180 if (col_major) {
181 return to_matrix(std::forward<EigMat>(x), m, n);
182 } else {
183 Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic> res
184 = to_matrix(std::forward<EigMat>(x), n, m);
185 res.transposeInPlace();
186 return res;
187 }
188}
189
206template <typename T>
207inline Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
208to_matrix(const std::vector<T>& x, int m, int n, bool col_major) {
209 if (col_major) {
210 return to_matrix(x, m, n);
211 }
212 check_size_match("to_matrix", "rows * columns", m * n, "matrix size",
213 x.size());
214 Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
215 result(m, n);
216 for (int i = 0, ij = 0; i < m; i++) {
217 for (int j = 0; j < n; j++, ij++) {
218 result.coeffRef(i, j) = x[ij];
219 }
220 }
221 return result;
222}
223
224} // namespace math
225} // namespace stan
226
227#endif
T_x to_matrix(T_x &&x)
Returns input matrix.
Definition to_matrix.hpp:21
int rows(const T_x &x)
Returns the number of rows in the specified kernel generator expression.
Definition rows.hpp:21
int cols(const T_x &x)
Returns the number of columns in the specified kernel generator expression.
Definition cols.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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9