Automatic Differentiation
 
Loading...
Searching...
No Matches
check_consistent_sizes.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_CONSISTENT_SIZES_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_CONSISTENT_SIZES_HPP
3
9#include <algorithm>
10#include <sstream>
11#include <string>
12
13namespace stan {
14namespace math {
15
17inline void check_consistent_sizes(const char*) { return; }
18
23template <typename T1>
24inline void check_consistent_sizes(const char*, const char*, const T1&) {
25 return;
26}
27
47template <typename T1, typename T2, typename... Ts>
48inline void check_consistent_sizes(const char* function, const char* name1,
49 const T1& x1, const char* name2,
50 const T2& x2, const Ts&... names_and_xs) {
52 check_consistent_sizes(function, name2, x2, name1, x1, names_and_xs...);
53 } else if constexpr (!is_vector<T2>::value) {
54 check_consistent_sizes(function, name1, x1, names_and_xs...);
55 } else if (stan::math::size(x1) == stan::math::size(x2)) {
56 check_consistent_sizes(function, name1, x1, names_and_xs...);
57 } else {
58 [&]() STAN_COLD_PATH {
59 size_t size_x1 = stan::math::size(x1);
60 size_t size_x2 = stan::math::size(x2);
61 std::stringstream msg;
62 msg << ", but " << name2 << " has size " << size_x2
63 << "; and they must be the same size.";
64 std::string msg_str(msg.str());
65 invalid_argument(function, name1, size_x1,
66 "has size = ", msg_str.c_str());
67 }();
68 }
69}
70
82template <typename T, typename... Types,
83 require_all_container_t<T, Types...>* = nullptr>
84inline 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
99} // namespace math
100} // namespace stan
101#endif
#define STAN_COLD_PATH
require_all_t< is_container< std::decay_t< Types > >... > require_all_container_t
Require all of the types satisfy is_container.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
void check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Check if two structures at the same size.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw an invalid_argument exception with a consistently formatted message.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
If the input type T is either an eigen matrix with 1 column or 1 row at compile time or a standard ve...