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
30 changes: 30 additions & 0 deletions stan/math/prim/err/check_consistent_sizes.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef STAN_MATH_PRIM_ERR_CHECK_CONSISTENT_SIZES_HPP
#define STAN_MATH_PRIM_ERR_CHECK_CONSISTENT_SIZES_HPP

#include <stan/math/prim/err/check_matching_sizes.hpp>
#include <stan/math/prim/err/invalid_argument.hpp>
#include <stan/math/prim/fun/size.hpp>
#include <stan/math/prim/meta/is_container.hpp>
#include <stan/math/prim/meta/require_generics.hpp>
#include <algorithm>
#include <sstream>
Expand Down Expand Up @@ -66,6 +68,34 @@ inline void check_consistent_sizes(const char* function, const char* name1,
}
}

/**
* Check that the unnamed container inputs have the same size. Inputs are
* labeled `arg1`, `arg2`, and so on in error messages.
*
* @tparam T type of the first input
* @tparam Types types of the remaining inputs
* @param function function name (for error messages)
* @param x first input
* @param xs remaining inputs
* @throw `invalid_argument` if sizes are inconsistent
*/
template <typename T, typename... Types,
require_all_container_t<T, Types...>* = nullptr>
inline void check_consistent_sizes(const char* function, T&& x, Types&&... xs) {
std::size_t arg_idx = 2;
(
[&](const auto& y) {
if (x.size() != y.size()) {
[&]() STAN_COLD_PATH {
const std::string name = "arg" + std::to_string(arg_idx);
check_matching_sizes(function, "arg1", x, name.c_str(), y);
}();
}
++arg_idx;
}(xs),
...);
}

} // namespace math
} // namespace stan
#endif
29 changes: 29 additions & 0 deletions stan/math/prim/err/check_matching_dims.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,35 @@ inline void check_matching_dims(const char* function, const char* name1,
check_matching_dims(function, name1, y1, name2, y2);
}

/**
* Check that the unnamed Eigen inputs have the same dimensions. Inputs are
* labeled `arg1`, `arg2`, and so on in error messages.
*
* @tparam T type of the first input
* @tparam Types types of the remaining inputs
* @param function function name (for error messages)
* @param x first input
* @param xs remaining inputs
* @throw `invalid_argument` if dimensions do not match
*/
template <typename T, typename... Types,
require_all_eigen_t<T, Types...>* = nullptr>
inline void check_matching_dims(const char* function, const T& x,
const Types&... xs) {
std::size_t arg_idx = 2;
(
[&](const auto& y) {
if (x.rows() != y.rows() || x.cols() != y.cols()) {
[&]() STAN_COLD_PATH {
const std::string name = "arg" + std::to_string(arg_idx);
check_matching_dims(function, "arg1", x, name.c_str(), y);
}();
}
++arg_idx;
}(xs),
...);
}

} // namespace math
} // namespace stan
#endif
1 change: 1 addition & 0 deletions stan/math/prim/functor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <stan/math/prim/functor/ode_ckrk.hpp>
#include <stan/math/prim/functor/ode_rk45.hpp>
#include <stan/math/prim/functor/ode_store_sensitivities.hpp>
#include <stan/math/prim/functor/map.hpp>
#include <stan/math/prim/functor/map_if.hpp>
#include <stan/math/prim/functor/map_rect.hpp>
#include <stan/math/prim/functor/map_rect_combine.hpp>
Expand Down
Loading