Automatic Differentiation
 
Loading...
Searching...
No Matches
check_matching_dims.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_MATCHING_DIMS_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_MATCHING_DIMS_HPP
3
8#include <sstream>
9#include <string>
10
11namespace stan {
12namespace math {
13
26template <typename T1, typename T2, require_all_not_matrix_t<T1, T2>* = nullptr,
27 require_all_not_nonscalar_prim_or_rev_kernel_expression_t<
28 T1, T2>* = nullptr>
29inline void check_matching_dims(const char* function, const char* name1,
30 const T1& y1, const char* name2, const T2& y2) {
31 std::vector<int> y1_d = dims(y1);
32 std::vector<int> y2_d = dims(y2);
33 auto error_throw = [&]() STAN_COLD_PATH {
34 std::ostringstream y1s;
35 if (y1_d.size() > 0) {
36 y1s << y1_d[0];
37 for (int i = 1; i < y1_d.size(); i++) {
38 y1s << ", " << y1_d[i];
39 }
40 }
41 std::ostringstream msg;
42 msg << ") and " << name2 << " (";
43 if (y2_d.size() > 0) {
44 msg << y2_d[0];
45 for (int i = 1; i < y2_d.size(); i++) {
46 msg << ", " << y2_d[i];
47 }
48 }
49 msg << ") must match in size";
50 std::string msg_str(msg.str());
51 invalid_argument(function, name1, y1s.str(), "(", msg_str.c_str());
52 };
53 if (y1_d.size() != y2_d.size()) {
54 error_throw();
55 } else {
56 for (int i = 0; i < y1_d.size(); i++) {
57 if (y1_d[i] != y2_d[i]) {
58 error_throw();
59 }
60 }
61 }
62}
63
76template <
77 typename T1, typename T2,
79 conjunction<is_prim_or_rev_kernel_expression<T1>,
82inline void check_matching_dims(const char* function, const char* name1,
83 const T1& y1, const char* name2, const T2& y2) {
84 if (y1.rows() != y2.rows() || y1.cols() != y2.cols()) {
85 [&]() STAN_COLD_PATH {
86 std::ostringstream y1_err;
87 std::ostringstream msg_str;
88 y1_err << "(" << y1.rows() << ", " << y1.cols() << ") and ";
89 msg_str << " (" << y2.rows() << ", " << y2.cols()
90 << ") must match in size";
91 invalid_argument(function, name1, name2,
92 std::string(y1_err.str()).c_str(),
93 std::string(msg_str.str()).c_str());
94 }();
95 }
96}
97
112template <typename T1, typename T2, require_any_matrix_t<T1, T2>* = nullptr,
113 require_any_stan_scalar_t<T1, T2>* = nullptr>
114inline void check_matching_dims(const char* function, const char* name1,
115 const T1& y1, const char* name2, const T2& y2) {
116 std::string y1_err("");
117 std::string msg_str("Tried Checking the dimensions of a matrix vs a scalar");
118 invalid_argument(function, name1, y1_err, "", msg_str.c_str());
119}
120
137template <bool check_compile, typename Mat1, typename Mat2,
138 typename = require_all_eigen_t<Mat1, Mat2>>
139inline void check_matching_dims(const char* function, const char* name1,
140 const Mat1& y1, const char* name2,
141 const Mat2& y2) {
142 if (check_compile
143 && (static_cast<int>(Mat1::RowsAtCompileTime)
144 != static_cast<int>(Mat2::RowsAtCompileTime)
145 || static_cast<int>(Mat1::ColsAtCompileTime)
146 != static_cast<int>(Mat2::ColsAtCompileTime))) {
147 [&]() STAN_COLD_PATH {
148 std::ostringstream msg;
149 msg << "Static rows and cols of " << name1 << " and " << name2
150 << " must match in size.";
151 std::string msg_str(msg.str());
152 invalid_argument(function, msg_str.c_str(), "", "");
153 }();
154 }
155 check_matching_dims(function, name1, y1, name2, y2);
156}
157
158} // namespace math
159} // namespace stan
160#endif
#define STAN_COLD_PATH
void dims(const T_x &x, std::vector< int > &result)
matrix_cl overload of the dims helper function in prim/fun/dims.hpp.
Definition dims.hpp:21
require_any_not_t< is_stan_scalar< std::decay_t< Types > >... > require_any_not_stan_scalar_t
Require at least one of the types do not satisfy is_stan_scalar.
void check_matching_dims(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the two containers have the same dimensions.
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.
std::enable_if_t< math::disjunction< Checks... >::value > require_any_t
If any condition is true, template is enabled.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Check if a type is derived from Eigen::EigenBase or is a var_value whose value_type is derived from E...
Definition is_matrix.hpp:18
Determines whether a type is either a kernel generator expression or a var containing a kernel genera...