1#ifndef STAN_MATH_PRIM_FUN_TO_MATRIX_HPP
2#define STAN_MATH_PRIM_FUN_TO_MATRIX_HPP
21template <
typename EigMat, require_eigen_dense_dynamic_t<EigMat>* =
nullptr>
23 return std::forward<EigMat>(x);
36template <
typename EigVec, require_eigen_vector_t<EigVec>* =
nullptr>
38 return Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>(
39 std::forward<EigVec>(matrix));
51inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
to_matrix(
52 const std::vector<Eigen::Matrix<T, 1, Eigen::Dynamic>>& x) {
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];
76inline Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
78 size_t rows = x.size();
82 size_t cols = x[0].size();
83 Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
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];
106template <
typename EigMat, require_eigen_t<EigMat>* =
nullptr>
107inline Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic>
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);
129template <
typename T, require_std_vector_vt<is_stan_scalar, T>* =
nullptr>
131 static constexpr const char* function =
"to_matrix(array)";
132 check_size_match(function,
"rows * columns", m * n,
"vector size", x.size());
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>>(
139 .template cast<double>();
141 return Eigen::Map<
const Eigen::Matrix<
142 value_type_t<
decltype(x_)>, Eigen::Dynamic, Eigen::Dynamic>>(
166template <
typename EigMat, require_eigen_t<EigMat>* =
nullptr>
167inline Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic>
170 return to_matrix(std::forward<EigMat>(x), m, n);
172 Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic> res
173 =
to_matrix(std::forward<EigMat>(x), n, m);
174 res.transposeInPlace();
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) {
203 Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
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];
T_x to_matrix(T_x &&x)
Returns input matrix.
int64_t cols(const T_x &x)
Returns the number of columns in the specified kernel generator expression.
int64_t rows(const T_x &x)
Returns the number of rows in the specified kernel generator expression.
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.
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 ...