1 #ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_MATCHING_DIMS_HPP
2 #define STAN_MATH_PRIM_MAT_ERR_CHECK_MATCHING_DIMS_HPP
3
4 #include <stan/math/prim/scal/err/domain_error.hpp>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <stan/math/prim/scal/err/check_size_match.hpp>
7 #include <stan/math/prim/scal/err/invalid_argument.hpp>
8 #include <sstream>
9 #include <string>
10
11 namespace stan {
12 namespace math {
13
14 /**
15 * Check if the two matrices are of the same size.
16 *
17 * This function checks the runtime sizes only.
18 *
19 * @tparam T1 Scalar type of the first matrix
20 * @tparam T2 Scalar type of the second matrix
21 * @tparam R1 Rows specified at compile time of the first matrix
22 * @tparam C1 Columns specified at compile time of the first matrix
23 * @tparam R2 Rows specified at compile time of the second matrix
24 * @tparam C2 Columns specified at compile time of the second matrix
25 *
26 * @param function Function name (for error messages)
27 * @param name1 Variable name for the first matrix (for error messages)
28 * @param y1 First matrix
29 * @param name2 Variable name for the second matrix (for error messages)
30 * @param y2 Second matrix
31 *
32 * @throw <code>std::invalid_argument</code>
33 * if the dimensions of the matrices do not match
34 */
35 template <typename T1, typename T2, int R1, int C1, int R2, int C2>
check_matching_dims(const char * function,const char * name1,const Eigen::Matrix<T1,R1,C1> & y1,const char * name2,const Eigen::Matrix<T2,R2,C2> & y2)36 inline void check_matching_dims(const char* function, const char* name1,
37 const Eigen::Matrix<T1, R1, C1>& y1,
38 const char* name2,
39 const Eigen::Matrix<T2, R2, C2>& y2) {
40 check_size_match(function, "Rows of ", name1, y1.rows(), "rows of ", name2,
41 y2.rows());
42 check_size_match(function, "Columns of ", name1, y1.cols(), "columns of ",
43 name2, y2.cols());
44 }
45
46 /**
47 * Check if the two matrices are of the same size.
48 *
49 * This function checks the runtime sizes and can also check the static
50 * sizes as well. For example, a 4x1 matrix is not the same as a vector
51 * with 4 elements.
52 *
53 * @tparam check_compile Whether to check the static sizes
54 * @tparam T1 Scalar type of the first matrix
55 * @tparam T2 Scalar type of the second matrix
56 * @tparam R1 Rows specified at compile time of the first matrix
57 * @tparam C1 Columns specified at compile time of the first matrix
58 * @tparam R2 Rows specified at compile time of the second matrix
59 * @tparam C2 Columns specified at compile time of the second matrix
60 *
61 * @param function Function name (for error messages)
62 * @param name1 Variable name for the first matrix (for error messages)
63 * @param y1 First matrix
64 * @param name2 Variable name for the second matrix (for error messages)
65 * @param y2 Second matrix
66 *
67 * @throw <code>std::invalid_argument</code> if the
68 * dimensions of the matrices do not match
69 */
70 template <bool check_compile, typename T1, typename T2, int R1, int C1, int R2,
71 int C2>
check_matching_dims(const char * function,const char * name1,const Eigen::Matrix<T1,R1,C1> & y1,const char * name2,const Eigen::Matrix<T2,R2,C2> & y2)72 inline void check_matching_dims(const char* function, const char* name1,
73 const Eigen::Matrix<T1, R1, C1>& y1,
74 const char* name2,
75 const Eigen::Matrix<T2, R2, C2>& y2) {
76 if (check_compile && (R1 != R2 || C1 != C2)) {
77 std::ostringstream msg;
78 msg << "Static rows and cols of " << name1 << " and " << name2
79 << " must match in size.";
80 std::string msg_str(msg.str());
81 invalid_argument(function, msg_str.c_str(), "", "");
82 }
83 check_matching_dims(function, name1, y1, name2, y2);
84 }
85 } // namespace math
86 } // namespace stan
87 #endif
88