1 #ifndef STAN_MATH_PRIM_FUN_BLOCK_HPP
2 #define STAN_MATH_PRIM_FUN_BLOCK_HPP
3 
4 #include <stan/math/prim/err.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 
7 namespace stan {
8 namespace math {
9 
10 /**
11  * Return a nrows x ncols submatrix starting at (i-1, j-1).
12  *
13  * @tparam T type of elements in the matrix
14  * @param m Matrix.
15  * @param i Starting row.
16  * @param j Starting column.
17  * @param nrows Number of rows in block.
18  * @param ncols Number of columns in block.
19  * @throw std::out_of_range if either index is out of range.
20  */
21 template <typename T, require_matrix_t<T>* = nullptr>
block(const T & m,size_t i,size_t j,size_t nrows,size_t ncols)22 inline auto block(const T& m, size_t i, size_t j, size_t nrows, size_t ncols) {
23   check_row_index("block", "i", m, i);
24   check_row_index("block", "i+nrows-1", m, i + nrows - 1);
25   check_column_index("block", "j", m, j);
26   check_column_index("block", "j+ncols-1", m, j + ncols - 1);
27   return m.block(i - 1, j - 1, nrows, ncols);
28 }
29 
30 }  // namespace math
31 }  // namespace stan
32 
33 #endif
34