|
1 | 1 | #ifndef STAN_MATH_PRIM_ERR_CHECK_CONSISTENT_SIZES_HPP |
2 | 2 | #define STAN_MATH_PRIM_ERR_CHECK_CONSISTENT_SIZES_HPP |
3 | 3 |
|
| 4 | +#include <stan/math/prim/err/check_matching_sizes.hpp> |
4 | 5 | #include <stan/math/prim/err/invalid_argument.hpp> |
5 | 6 | #include <stan/math/prim/fun/size.hpp> |
| 7 | +#include <stan/math/prim/meta/is_container.hpp> |
6 | 8 | #include <stan/math/prim/meta/require_generics.hpp> |
7 | 9 | #include <algorithm> |
8 | 10 | #include <sstream> |
@@ -66,6 +68,34 @@ inline void check_consistent_sizes(const char* function, const char* name1, |
66 | 68 | } |
67 | 69 | } |
68 | 70 |
|
| 71 | +/** |
| 72 | + * Check that the unnamed container inputs have the same size. Inputs are |
| 73 | + * labeled `arg1`, `arg2`, and so on in error messages. |
| 74 | + * |
| 75 | + * @tparam T type of the first input |
| 76 | + * @tparam Types types of the remaining inputs |
| 77 | + * @param function function name (for error messages) |
| 78 | + * @param x first input |
| 79 | + * @param xs remaining inputs |
| 80 | + * @throw `invalid_argument` if sizes are inconsistent |
| 81 | + */ |
| 82 | +template <typename T, typename... Types, |
| 83 | + require_all_container_t<T, Types...>* = nullptr> |
| 84 | +inline void check_consistent_sizes(const char* function, T&& x, Types&&... xs) { |
| 85 | + std::size_t arg_idx = 2; |
| 86 | + ( |
| 87 | + [&](const auto& y) { |
| 88 | + if (x.size() != y.size()) { |
| 89 | + [&]() STAN_COLD_PATH { |
| 90 | + const std::string name = "arg" + std::to_string(arg_idx); |
| 91 | + check_matching_sizes(function, "arg1", x, name.c_str(), y); |
| 92 | + }(); |
| 93 | + } |
| 94 | + ++arg_idx; |
| 95 | + }(xs), |
| 96 | + ...); |
| 97 | +} |
| 98 | + |
69 | 99 | } // namespace math |
70 | 100 | } // namespace stan |
71 | 101 | #endif |
0 commit comments