1 #ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_COLUMN_INDEX_HPP
2 #define STAN_MATH_PRIM_MAT_ERR_CHECK_COLUMN_INDEX_HPP
3
4 #include <stan/math/prim/scal/err/out_of_range.hpp>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <stan/math/prim/scal/meta/error_index.hpp>
7 #include <sstream>
8 #include <string>
9
10 namespace stan {
11 namespace math {
12
13 /**
14 * Check if the specified index is a valid
15 * column of the matrix.
16 *
17 * By default, this is a 1-indexed check (as opposed to
18 * 0-indexed). Behavior can be changed by setting
19 * <code>stan::error_index::value</code>. This function will
20 * throw an <code>std::out_of_range</code> exception if
21 * the index is out of bounds.
22 *
23 * @tparam T_y Type of scalar.
24 * @tparam R Number of rows of the matrix
25 * @tparam C Number of columns of the matrix
26 *
27 * @param function Function name (for error messages)
28 * @param name Variable name (for error messages)
29 * @param y Matrix
30 * @param i Index to check
31 *
32 * @throw std::out_of_range if index is an invalid column index
33 */
34 template <typename T_y, int R, int C>
check_column_index(const char * function,const char * name,const Eigen::Matrix<T_y,R,C> & y,size_t i)35 inline void check_column_index(const char* function, const char* name,
36 const Eigen::Matrix<T_y, R, C>& y, size_t i) {
37 if (i >= stan::error_index::value
38 && i < static_cast<size_t>(y.cols()) + stan::error_index::value)
39 return;
40
41 std::stringstream msg;
42 msg << " for columns of " << name;
43 std::string msg_str(msg.str());
44 out_of_range(function, y.cols(), i, msg_str.c_str());
45 }
46
47 } // namespace math
48 } // namespace stan
49 #endif
50