Skip to content
Open
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
6 changes: 5 additions & 1 deletion stan/math/prim/fun/dims.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ template <typename T, typename Alloc>
inline void dims(const std::vector<T, Alloc>& x, std::vector<int>& result) {
result.push_back(x.size());
if (x.size() > 0) {
dims(x[0], result);
if constexpr (is_eigen_vector<T>::value) {
result.push_back(x[0].size());
} else {
dims(x[0], result);
}
}
}

Expand Down
21 changes: 17 additions & 4 deletions stan/math/prim/fun/resize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,38 @@
namespace stan {
namespace math {
namespace internal {

template <typename T, int R, int C>
inline void resize(Eigen::Matrix<T, R, C>& x, const std::vector<int>& dims,
int pos) {
if constexpr (R == 1 || C == 1) {
if (pos + 1 == static_cast<int>(dims.size())) {
if constexpr (R == 1 && C != 1) {
x.resize(1, dims[pos]);
} else {
x.resize(dims[pos], 1);
}
return;
}
}
x.resize(dims[pos], dims[pos + 1]);
}

template <typename T>
inline void resize(T /*x*/, const std::vector<int>& /*dims*/, int /*pos*/) {
// no-op
inline void resize(T /*x*/, const std::vector<int>& /*dims*/,
int /*pos*/ = 0) { // no-op
}

template <typename T>
inline void resize(std::vector<T>& x, const std::vector<int>& dims, int pos) {
inline void resize(std::vector<T>& x, const std::vector<int>& dims,
int pos = 0) {
x.resize(dims[pos]);
++pos;

if (pos >= static_cast<int>(dims.size())) {
return; // skips lowest loop to scalar
}

for (size_t i = 0; i < x.size(); ++i) {
resize(x[i], dims, pos);
}
Expand All @@ -47,5 +61,4 @@ inline void resize(T& x, std::vector<int> dims) {

} // namespace math
} // namespace stan

#endif
32 changes: 30 additions & 2 deletions test/unit/math/prim/fun/dims_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TEST(MathMatrixPrimMat, dims) {
EXPECT_EQ(1U, dims3.size());
EXPECT_EQ(2, dims3[0]);

vector<vector<double> > x4;
vector<vector<double>> x4;
x4.push_back(x3);
x4.push_back(x3);
x4.push_back(x3);
Expand All @@ -50,7 +50,7 @@ TEST(MathMatrixPrimMat, dims) {
EXPECT_EQ(1, dims7[0]);
EXPECT_EQ(17, dims7[1]);

vector<Matrix<double, Dynamic, Dynamic> > x8;
vector<Matrix<double, Dynamic, Dynamic>> x8;
x8.push_back(x5);
x8.push_back(x5);
vector<int> dims8 = dims(x8);
Expand All @@ -59,3 +59,31 @@ TEST(MathMatrixPrimMat, dims) {
EXPECT_EQ(7, dims8[1]);
EXPECT_EQ(8, dims8[2]);
}
TEST(MathMatrixPrimMat, dims_nested_vector_arrays) {
using Eigen::Dynamic;
using Eigen::Matrix;
using stan::math::dims;
using std::vector;

Matrix<double, Dynamic, 1> v1(1);
vector<Matrix<double, Dynamic, 1>> inner1{v1};
vector<vector<Matrix<double, Dynamic, 1>>> x1{inner1};

const auto result1 = dims(x1);

ASSERT_EQ(3U, result1.size());
EXPECT_EQ(1, result1[0]);
EXPECT_EQ(1, result1[1]);
EXPECT_EQ(1, result1[2]);

Matrix<double, Dynamic, 1> v2(5);
vector<Matrix<double, Dynamic, 1>> inner2(3, v2);
vector<vector<Matrix<double, Dynamic, 1>>> x2(2, inner2);

const auto result2 = dims(x2);

ASSERT_EQ(3U, result2.size());
EXPECT_EQ(2, result2[0]);
EXPECT_EQ(3, result2[1]);
EXPECT_EQ(5, result2[2]);
}
22 changes: 18 additions & 4 deletions test/unit/math/prim/fun/resize_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ TEST(MathMatrixPrimMat, resize_svec_svec_double) {
using stan::math::matrix_d;
using stan::math::row_vector_d;
using stan::math::vector_d;
std::vector<std::vector<double> > xx;
std::vector<std::vector<double>> xx;
EXPECT_EQ(0U, xx.size());
std::vector<int> dims;
dims.push_back(4U);
Expand All @@ -120,7 +120,7 @@ TEST(MathMatrixPrimMat, resize_svec_v_double) {
using stan::math::matrix_d;
using stan::math::row_vector_d;
using stan::math::vector_d;
std::vector<Matrix<double, Dynamic, 1> > xx;
std::vector<Matrix<double, Dynamic, 1>> xx;
EXPECT_EQ(0U, xx.size());
std::vector<int> dims;
dims.push_back(4U);
Expand All @@ -143,7 +143,7 @@ TEST(MathMatrixPrimMat, resize_svec_rv_double) {
using stan::math::matrix_d;
using stan::math::row_vector_d;
using stan::math::vector_d;
std::vector<Matrix<double, 1, Dynamic> > xx;
std::vector<Matrix<double, 1, Dynamic>> xx;
EXPECT_EQ(0U, xx.size());
std::vector<int> dims;
dims.push_back(4U);
Expand All @@ -166,7 +166,7 @@ TEST(MathMatrixPrimMat, resize_svec_svec_matrix_double) {
using stan::math::matrix_d;
using stan::math::row_vector_d;
using stan::math::vector_d;
std::vector<std::vector<Matrix<double, Dynamic, Dynamic> > > mm;
std::vector<std::vector<Matrix<double, Dynamic, Dynamic>>> mm;
std::vector<int> dims;
dims.push_back(4U);
dims.push_back(5U);
Expand All @@ -178,3 +178,17 @@ TEST(MathMatrixPrimMat, resize_svec_svec_matrix_double) {
EXPECT_EQ(6, mm[1][2].rows());
EXPECT_EQ(3, mm[3][4].cols());
}
TEST(MathMatrixPrimMat, resize_nested_vectors_of_eigen_vectors) {
std::vector<std::vector<Eigen::VectorXd>> x;

stan::math::resize(x, {2, 3, 4});

ASSERT_EQ(x.size(), 2);
for (const auto& row : x) {
ASSERT_EQ(row.size(), 3);
for (const auto& v : row) {
EXPECT_EQ(v.rows(), 4);
EXPECT_EQ(v.cols(), 1);
}
}
}