Automatic Differentiation
 
Loading...
Searching...
No Matches
zip_index.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_ZIP_INDEX_HPP
2#define STAN_MATH_REV_FUN_ZIP_INDEX_HPP
3
9#include <vector>
10
11namespace stan::math {
12
29template <typename EigMat, typename IdxRows, typename IdxCols,
30 require_eigen_matrix_dynamic_t<EigMat>* = nullptr,
31 require_all_std_vector_t<IdxRows, IdxCols>* = nullptr>
33 IdxRows&& idx_row,
34 IdxCols&& idx_col) {
35 check_size_match("zip_index", "size of idx_row", idx_row.size(),
36 "size of idx_col", idx_col.size());
37 if (idx_row.empty()) {
38 return var_value<Eigen::VectorXd>(Eigen::VectorXd(0));
39 }
40 auto rows = to_arena(std::forward<IdxRows>(idx_row));
41 auto cols = to_arena(std::forward<IdxCols>(idx_col));
42 return make_callback_var(zip_index(x.val(), rows, cols),
43 [x, rows, cols](auto& vi) mutable {
44 zip_index(x.adj(), rows, cols) += vi.adj();
45 });
46}
47
48} // namespace stan::math
49
50#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
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
Definition to_arena.hpp:25
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
Matrices and templated mathematical functions.