1#ifndef STAN_MATH_PRIM_FUN_ZIP_INDEX_HPP
2#define STAN_MATH_PRIM_FUN_ZIP_INDEX_HPP
24template <
typename EigMat,
typename IdxRows,
typename IdxCols,
25 require_eigen_matrix_dynamic_t<EigMat>* =
nullptr,
26 require_all_vector_t<IdxRows, IdxCols>* =
nullptr>
27inline auto zip_index(EigMat&& x, IdxRows&& idx_row, IdxCols&& idx_col) {
29 "size of idx_col", idx_col.size());
31 [](
auto&& x_,
auto&& idx_row_,
auto&& idx_col_) {
32 using map_t = Eigen::Map<const Eigen::Array<int, Eigen::Dynamic, 1>>;
33 const map_t
rows(idx_row_.data(), idx_row_.size());
34 const map_t
cols(idx_col_.data(), idx_col_.size());
36#ifndef STAN_NO_RANGE_CHECKS
37 check_range(
"zip_index",
"minimum row index", x_.rows(),
39 check_range(
"zip_index",
"maximum row index", x_.rows(),
41 check_range(
"zip_index",
"minimum column index", x_.cols(),
43 check_range(
"zip_index",
"maximum column index", x_.cols(),
46 const auto linear_idx = (
rows.cast<Eigen::Index>() - 1)
47 + (
cols.cast<Eigen::Index>() - 1) * x_.rows();
48 return x_.reshaped()(linear_idx);
50 std::forward<EigMat>(x), std::forward<IdxRows>(idx_row),
51 std::forward<IdxCols>(idx_col));
int64_t cols(const T_x &x)
Returns the number of columns in the specified kernel generator expression.
int64_t rows(const T_x &x)
Returns the number of rows in the specified kernel generator expression.
auto make_holder(F &&func, Args &&... args)
Calls given function with given arguments.
void check_range(const char *function, const char *name, int max, int index, int nested_level, const char *error_msg)
Check if specified index is within range.
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
auto zip_index(EigMat &&x, IdxRows &&idx_row, IdxCols &&idx_col)
Return the column vector of elements of x selected by paired (zipped) 1-based row and column indices.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...