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, require_std_vector_vt<is_stan_scalar, T>* = nullptr>
130inline auto to_matrix(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 make_holder(
134 [m, n](auto&& x_) {
135 if constexpr (std::is_integral_v<value_type_t<decltype(x_)>>) {
136 return Eigen::Map<const Eigen::Matrix<
137 value_type_t<decltype(x_)>, Eigen::Dynamic, Eigen::Dynamic>>(
138 &x_[0], m, n)
139 .template cast<double>();
140 } else {
141 return Eigen::Map<const Eigen::Matrix<
142 value_type_t<decltype(x_)>, Eigen::Dynamic, Eigen::Dynamic>>(
143 &x_[0], m, n);
144 }
145 },
146 std::forward<T>(x));
147}
148
166template <typename EigMat, require_eigen_t<EigMat>* = nullptr>
167inline Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic>
168to_matrix(EigMat&& x, int m, int n, bool col_major) {
169 if (col_major) {
170 return to_matrix(std::forward<EigMat>(x), m, n);
171 } else {
172 Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic> res
173 = to_matrix(std::forward<EigMat>(x), n, m);
174 res.transposeInPlace();
175 return res;
176 }
177}
178
195template <typename T>
196inline Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
197to_matrix(const std::vector<T>& x, int m, int n, bool col_major) {
198 if (col_major) {
199 return to_matrix(x, m, n);
200 }
201 check_size_match("to_matrix", "rows * columns", m * n, "matrix size",
202 x.size());
203 Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
204 result(m, n);
205 for (int i = 0, ij = 0; i < m; i++) {
206 for (int j = 0; j < n; j++, ij++) {
207 result.coeffRef(i, j) = x[ij];
208 }
209 }
210 return result;
211}
212
213} // namespace math
214} // namespace stan
215
216#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:437
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 ...