Automatic Differentiation
 
Loading...
Searching...
No Matches
zip_index.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_ZIP_INDEX_HPP
2#define STAN_MATH_PRIM_FUN_ZIP_INDEX_HPP
3
7#include <vector>
8
9namespace stan {
10namespace math {
11
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) {
28 check_size_match("zip_index", "size of idx_row", idx_row.size(),
29 "size of idx_col", idx_col.size());
30 return make_holder(
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());
35 // If the user turns of range checks do not pay for min and max sweeps
36#ifndef STAN_NO_RANGE_CHECKS
37 check_range("zip_index", "minimum row index", x_.rows(),
38 rows.minCoeff());
39 check_range("zip_index", "maximum row index", x_.rows(),
40 rows.maxCoeff());
41 check_range("zip_index", "minimum column index", x_.cols(),
42 cols.minCoeff());
43 check_range("zip_index", "maximum column index", x_.cols(),
44 cols.maxCoeff());
45#endif
46 const auto linear_idx = (rows.cast<Eigen::Index>() - 1)
47 + (cols.cast<Eigen::Index>() - 1) * x_.rows();
48 return x_.reshaped()(linear_idx);
49 },
50 std::forward<EigMat>(x), std::forward<IdxRows>(idx_row),
51 std::forward<IdxCols>(idx_col));
52}
53
54} // namespace math
55} // namespace stan
56
57#endif
int64_t cols(const T_x &x)
Returns the number of columns in the specified kernel generator expression.
Definition cols.hpp:21
int64_t rows(const T_x &x)
Returns the number of rows in the specified kernel generator expression.
Definition rows.hpp:22
auto make_holder(F &&func, Args &&... args)
Calls given function with given arguments.
Definition holder.hpp:488
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.
Definition zip_index.hpp:27
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...