diff --git a/stan/math/prim/fun/rep_matrix.hpp b/stan/math/prim/fun/rep_matrix.hpp index 8a59ff41af3..efb20628523 100644 --- a/stan/math/prim/fun/rep_matrix.hpp +++ b/stan/math/prim/fun/rep_matrix.hpp @@ -47,11 +47,12 @@ inline auto rep_matrix(const T& x, int m, int n) { * vector. * @tparam Vec An Eigen vector. * @param x An Eigen vector. For Row vectors the values are replicated rowwise. - * and for column vectors the values are repliacated colwise. + * and for column vectors the values are replicated colwise. * @param n Number of rows or columns. */ template * = nullptr> -inline auto rep_matrix(const Vec& x, int n) { +inline Eigen::Matrix, -1, -1> rep_matrix(const Vec& x, + int n) { if constexpr (is_eigen_row_vector::value) { check_nonnegative("rep_matrix", "rows", n); return x.replicate(n, 1); diff --git a/test/unit/math/prim/fun/rep_matrix_test.cpp b/test/unit/math/prim/fun/rep_matrix_test.cpp index f92e4d389e3..3ee6ac9c1d9 100644 --- a/test/unit/math/prim/fun/rep_matrix_test.cpp +++ b/test/unit/math/prim/fun/rep_matrix_test.cpp @@ -52,3 +52,15 @@ TEST(MathMatrixPrimMat, rep_matrix_row_vec) { EXPECT_THROW(rep_matrix(rv, -1), std::domain_error); } + +TEST(MathMatrixPrimMat, rep_matrix_both_ways) { + using stan::math::rep_matrix; + + Eigen::Matrix rv(3); + rv << 1.0, 4.0, 9.0; + + Eigen::Matrix v(3); + v << 1.0, 4.0, 9.0; + Eigen::Matrix x(3, 3); + x = true ? rep_matrix(v, 3) : rep_matrix(rv, 3); +}