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, Eigen::Dynamic, 1>>& x) {
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) {
74inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
to_matrix(
75 const std::vector<Eigen::Matrix<T, 1, Eigen::Dynamic>>& x) {
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];
99inline Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
101 size_t rows = x.size();
105 size_t cols = x[0].size();
106 Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
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];
129template <
typename EigMat, require_eigen_t<EigMat>* =
nullptr>
130inline Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic>
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);
152template <
typename T, require_std_vector_vt<is_stan_scalar, T>* =
nullptr>
154 static constexpr const char* function =
"to_matrix(array)";
155 check_size_match(function,
"rows * columns", m * n,
"vector size", x.size());
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>>(
162 .template cast<double>();
164 return Eigen::Map<
const Eigen::Matrix<
165 value_type_t<
decltype(x_)>, Eigen::Dynamic, Eigen::Dynamic>>(
189template <
typename EigMat, require_eigen_t<EigMat>* =
nullptr>
190inline Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic>
193 return to_matrix(std::forward<EigMat>(x), m, n);
195 Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic> res
196 =
to_matrix(std::forward<EigMat>(x), n, m);
197 res.transposeInPlace();
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) {
226 Eigen::Matrix<return_type_t<T, double>, Eigen::Dynamic, Eigen::Dynamic>
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];
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 ...