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, Eigen::Dynamic, 1>>& x) {
53 int cols = x.size();
54 if (cols == 0) {
55 return {};
56 }
57 int rows = x[0].size();
58 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> result(rows, cols);
59 for (int j = 0; j < cols; ++j) {
60 result.col(j) = x[j];
61 }
62 return result;
63}
64
73template <typename T>
74inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> to_matrix(
75 const std::vector<Eigen::Matrix<T, 1, Eigen::Dynamic>>& x) {
76 int rows = x.size();
77 if (rows == 0) {
78 return {};
79 }
80 int cols = x[0].size();
81 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> result(rows, cols);
82 for (int i = 0, ij = 0; i < cols; i++) {
83 for (int j = 0; j < rows; j++, ij++) {
84 result.coeffRef(ij) = x[j][i];
85 }
86 }
87 return result;
88}
89
98template <typename T>
99inline Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
100to_matrix(const std::vector<std::vector<T>>& x) {
101 size_t rows = x.size();
102 if (rows == 0) {
103 return {};
104 }
105 size_t cols = x[0].size();
106 Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
107 result(rows, cols);
108 for (size_t i = 0, ij = 0; i < cols; i++) {
109 for (size_t j = 0; j < rows; j++, ij++) {
110 result.coeffRef(ij) = x[j][i];
111 }
112 }
113 return result;
114}
115
129template <typename EigMat, require_eigen_t<EigMat>* = nullptr>
130inline Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic>
131to_matrix(EigMat&& x, int m, int n) {
132 static constexpr const char* function = "to_matrix(matrix)";
133 check_size_match(function, "rows * columns", m * n, "vector size", x.size());
134 Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic> y
135 = std::forward<EigMat>(x);
136 y.resize(m, n);
137 return y;
138}
139
152template <typename T, require_std_vector_vt<is_stan_scalar, T>* = nullptr>
153inline auto to_matrix(T&& x, int m, int n) {
154 static constexpr const char* function = "to_matrix(array)";
155 check_size_match(function, "rows * columns", m * n, "vector size", x.size());
156 return make_holder(
157 [m, n](auto&& x_) {
158 if constexpr (std::is_integral_v<value_type_t<decltype(x_)>>) {
159 return Eigen::Map<const Eigen::Matrix<
160 value_type_t<decltype(x_)>, Eigen::Dynamic, Eigen::Dynamic>>(
161 &x_[0], m, n)
162 .template cast<double>();
163 } else {
164 return Eigen::Map<const Eigen::Matrix<
165 value_type_t<decltype(x_)>, Eigen::Dynamic, Eigen::Dynamic>>(
166 &x_[0], m, n);
167 }
168 },
169 std::forward<T>(x));
170}
171
189template <typename EigMat, require_eigen_t<EigMat>* = nullptr>
190inline Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic>
191to_matrix(EigMat&& x, int m, int n, bool col_major) {
192 if (col_major) {
193 return to_matrix(std::forward<EigMat>(x), m, n);
194 } else {
195 Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic> res
196 = to_matrix(std::forward<EigMat>(x), n, m);
197 res.transposeInPlace();
198 return res;
199 }
200}
201
218template <typename T>
219inline Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
220to_matrix(const std::vector<T>& x, int m, int n, bool col_major) {
221 if (col_major) {
222 return to_matrix(x, m, n);
223 }
224 check_size_match("to_matrix", "rows * columns", m * n, "matrix size",
225 x.size());
226 Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
227 result(m, n);
228 for (int i = 0, ij = 0; i < m; i++) {
229 for (int j = 0; j < n; j++, ij++) {
230 result.coeffRef(i, j) = x[ij];
231 }
232 }
233 return result;
234}
235
236} // namespace math
237} // namespace stan
238
239#endif
T_x to_matrix(T_x &&x)
Returns input matrix.
Definition to_matrix.hpp:21
int64_t cols(const T_x &x)
Returns the number of columns in the specified kernel generator expression.
Definition cols.hpp:21
int64_t rows(const T_x &x)
Returns the number of rows in the specified kernel generator expression.
Definition rows.hpp:22
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
auto make_holder(F &&func, Args &&... args)
Calls given function with given arguments.
Definition holder.hpp:488
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 ...