1 #ifndef STAN_MATH_PRIM_FUN_REP_MATRIX_HPP
2 #define STAN_MATH_PRIM_FUN_REP_MATRIX_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/Eigen.hpp>
7 
8 namespace stan {
9 namespace math {
10 
11 /**
12  * Implementation of rep_matrix returning an Eigen matrix with scalar
13  * type equal to the input scalar type.
14  * @tparam Ret An Eigen type.
15  * @tparam T A Scalar type.
16  * @param x A Scalar whose values are propogated to all values in the return
17  * matrix.
18  * @param m Number or rows.
19  * @param n Number of columns.
20  */
21 template <typename Ret, typename T,
22           require_eigen_matrix_dynamic_vt<is_stan_scalar, Ret>* = nullptr,
23           require_stan_scalar_t<T>* = nullptr>
rep_matrix(const T & x,int m,int n)24 inline auto rep_matrix(const T& x, int m, int n) {
25   check_nonnegative("rep_matrix", "rows", m);
26   check_nonnegative("rep_matrix", "cols", n);
27   return Ret::Constant(m, n, x);
28 }
29 
30 /**
31  * Default Implementation of rep_matrix returning an Eigen matrix with scalar
32  * type equal to the input scalar type.
33  * @tparam T A Scalar type.
34  * @param x A Scalar whose values are propogated to all values in the return
35  * matrix.
36  * @param m Number or rows.
37  * @param n Number of columns.
38  */
39 template <typename T, require_stan_scalar_t<T>* = nullptr>
rep_matrix(const T & x,int m,int n)40 inline auto rep_matrix(const T& x, int m, int n) {
41   return rep_matrix<
42       Eigen::Matrix<return_type_t<T>, Eigen::Dynamic, Eigen::Dynamic>>(x, m, n);
43 }
44 
45 /**
46  * Implementation of rep_matrix returning an Eigen matrix from an Eigen
47  * vector.
48  * @tparam Vec An Eigen vector.
49  * @param x An Eigen vector. For Row vectors the values are replacated rowwise.
50  * and for column vectors the values are repliacated colwise.
51  * @param n Number of rows or columns.
52  */
53 template <typename Vec, require_eigen_vector_t<Vec>* = nullptr>
rep_matrix(const Vec & x,int n)54 inline auto rep_matrix(const Vec& x, int n) {
55   if (is_eigen_row_vector<Vec>::value) {
56     check_nonnegative("rep_matrix", "rows", n);
57     return x.replicate(n, 1);
58   } else {
59     check_nonnegative("rep_matrix", "cols", n);
60     return x.replicate(1, n);
61   }
62 }
63 
64 }  // namespace math
65 }  // namespace stan
66 
67 #endif
68