Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions stan/math/prim/fun/rep_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename Vec, require_eigen_vector_t<Vec>* = nullptr>
inline auto rep_matrix(const Vec& x, int n) {
inline Eigen::Matrix<scalar_type_t<Vec>, -1, -1> rep_matrix(const Vec& x,
int n) {
if constexpr (is_eigen_row_vector<Vec>::value) {
check_nonnegative("rep_matrix", "rows", n);
return x.replicate(n, 1);
Expand Down
12 changes: 12 additions & 0 deletions test/unit/math/prim/fun/rep_matrix_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double, 1, Eigen::Dynamic> rv(3);
rv << 1.0, 4.0, 9.0;

Eigen::Matrix<double, Eigen::Dynamic, 1> v(3);
v << 1.0, 4.0, 9.0;
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> x(3, 3);
x = true ? rep_matrix(v, 3) : rep_matrix(rv, 3);
}