Skip to content

Commit

Permalink
fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveBronder committed Jan 8, 2024
1 parent e46a24b commit fc3d4ff
Showing 1 changed file with 72 additions and 55 deletions.
127 changes: 72 additions & 55 deletions src/stan/model/indexing/rvalue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,30 @@ inline auto rvalue(Vec&& v, const char* name, index_uni idx) {
* Types: vector[multi] = vector
*
* @tparam EigVec Eigen type with either dynamic rows or columns, but not both.
* @tparam MultiIndex A multi index.
* @param[in] v Eigen vector type.
* @param[in] name Name of variable
* @param[in] idx Sequence of integers.
* @throw std::out_of_range If any of the indices are out of bounds.
* @throw std::invalid_argument If the value size isn't the same as
* the indexed size.
*/
template <typename EigVec, typename MultiIndex, require_eigen_vector_t<EigVec>* = nullptr,
require_same_t<MultiIndex, index_multi>* = nullptr>
template <typename EigVec, typename MultiIndex,
require_eigen_vector_t<EigVec>* = nullptr,
require_same_t<MultiIndex, index_multi>* = nullptr>
inline auto rvalue(EigVec&& v, const char* name, MultiIndex&& idx) {
using fwd_t = decltype(stan::math::to_ref(std::forward<EigVec>(v)));
for (auto idx_i : idx.ns_) {
math::check_range("vector[multi] indexing", name, v.size(), idx_i);
}
return stan::math::make_holder(
[name](auto&& v_ref, auto&& idx_inner) {
Eigen::Map<Eigen::Array<int, -1, 1>> idx2(idx_inner.ns_.data(), idx_inner.ns_.size());
return std::forward<decltype(v_ref)>(v_ref)(idx2 - 1);
},
std::forward<fwd_t>(stan::math::to_ref(std::forward<EigVec>(v))),
std::forward<MultiIndex>(idx));
[name](auto&& v_ref, auto&& idx_inner) {
Eigen::Map<Eigen::Array<int, -1, 1>> idx2(idx_inner.ns_.data(),
idx_inner.ns_.size());
return std::forward<decltype(v_ref)>(v_ref)(idx2 - 1);
},
std::forward<fwd_t>(stan::math::to_ref(std::forward<EigVec>(v))),
std::forward<MultiIndex>(idx));
}

/**
Expand Down Expand Up @@ -259,22 +262,24 @@ inline auto rvalue(Mat&& x, const char* name, index_uni idx) {
* Types: matrix[multi] = matrix
*
* @tparam EigMat Eigen type with dynamic rows and columns.
* @tparam MultiIndex A multi index.
* @param[in] x Eigen type
* @param[in] name Name of variable
* @param[in] idx A multi index for selecting a set of rows.
* @throw std::out_of_range If any of the indices are out of bounds.
*/
template <typename EigMat, typename MultiIndex, require_eigen_dense_dynamic_t<EigMat>* = nullptr,
require_same_t<MultiIndex, index_multi>* = nullptr>
inline auto rvalue(EigMat&& x, const char* name,
MultiIndex&& idx) {
template <typename EigMat, typename MultiIndex,
require_eigen_dense_dynamic_t<EigMat>* = nullptr,
require_same_t<MultiIndex, index_multi>* = nullptr>
inline auto rvalue(EigMat&& x, const char* name, MultiIndex&& idx) {
for (int i = 0; i < idx.ns_.size(); ++i) {
math::check_range("matrix[multi] row indexing", name, x.rows(), idx.ns_[i]);
}
return stan::math::make_holder(
[&idx](auto&& x_ref, auto&& idx_inner) {
using vec_map = Eigen::Map<Eigen::Array<int, -1, 1>>;
return x_ref((vec_map(idx_inner.ns_.data(), idx_inner.ns_.size()) - 1), Eigen::all);
return x_ref((vec_map(idx_inner.ns_.data(), idx_inner.ns_.size()) - 1),
Eigen::all);
},
stan::math::to_ref(x), std::forward<MultiIndex>(idx));
}
Expand Down Expand Up @@ -419,29 +424,33 @@ inline auto rvalue(Mat&& x, const char* name, index_uni row_idx,
* Types: matrix[uni, multi] = row vector
*
* @tparam EigMat Eigen type with dynamic rows and columns.
* @tparam MultiIndex A multi index.
* @param[in] x Matrix to index.
* @param[in] name Name of variable
* @param[in] row_idx uni index for selecting rows.
* @param[in] col_idx multi index for selecting cols.
* @throw std::out_of_range If any of the indices are out of bounds.
*/
template <typename EigMat, typename MultiIndex, require_eigen_dense_dynamic_t<EigMat>* = nullptr,
require_same_t<MultiIndex, index_multi>* = nullptr>
template <typename EigMat, typename MultiIndex,
require_eigen_dense_dynamic_t<EigMat>* = nullptr,
require_same_t<MultiIndex, index_multi>* = nullptr>
inline auto rvalue(EigMat&& x, const char* name, index_uni row_idx,
MultiIndex&& col_idx) {
MultiIndex&& col_idx) {
math::check_range("matrix[uni, multi] row indexing", name, x.rows(),
row_idx.n_);
for (auto idx_i : col_idx.ns_) {
math::check_range("matrix[uni, multi] column indexing", name, x.cols(),
idx_i);
math::check_range("matrix[uni, multi] column indexing", name, x.cols(),
idx_i);
}
return stan::math::make_holder(
[name, row_idx](auto&& x_ref, auto&& col_idx_inner) {
using vec_map = Eigen::Map<Eigen::Array<int, -1, 1>>;
return x_ref(row_idx.n_ - 1, (vec_map(col_idx_inner.ns_.data(), col_idx_inner.ns_.size()) - 1));
return x_ref(
row_idx.n_ - 1,
(vec_map(col_idx_inner.ns_.data(), col_idx_inner.ns_.size()) - 1));
},
stan::math::to_ref(std::forward<EigMat>(x)),
std::forward<MultiIndex>(col_idx));
std::forward<MultiIndex>(col_idx));
}

/**
Expand All @@ -451,26 +460,29 @@ inline auto rvalue(EigMat&& x, const char* name, index_uni row_idx,
* Types: matrix[multi, uni] = vector
*
* @tparam EigMat Eigen type with dynamic rows and columns.
* @tparam MultiIndex A multi index.
* @param[in] x Matrix to index.
* @param[in] name Name of variable
* @param[in] row_idx multi index for selecting rows.
* @param[in] col_idx uni index for selecting cols.
* @throw std::out_of_range If any of the indices are out of bounds.
*/
template <typename EigMat, typename MultiIndex, require_eigen_dense_dynamic_t<EigMat>* = nullptr,
require_same_t<MultiIndex, index_multi>* = nullptr>
template <typename EigMat, typename MultiIndex,
require_eigen_dense_dynamic_t<EigMat>* = nullptr,
require_same_t<MultiIndex, index_multi>* = nullptr>
inline auto rvalue(EigMat&& x, const char* name, MultiIndex&& row_idx,
index_uni col_idx) {
index_uni col_idx) {
math::check_range("matrix[multi, uni] column indexing", name, x.cols(),
col_idx.n_);
for (auto idx_i : row_idx.ns_) {
math::check_range("matrix[uni, multi] row indexing", name, x.rows(),
idx_i);
math::check_range("matrix[uni, multi] row indexing", name, x.rows(), idx_i);
}
return stan::math::make_holder(
[name, col_idx](auto&& x_ref, auto&& row_idx_inner) {
using vec_map = Eigen::Map<Eigen::Array<int, -1, 1>>;
return x_ref((vec_map(row_idx_inner.ns_.data(), row_idx_inner.ns_.size()) - 1), col_idx.n_ - 1);
return x_ref(
(vec_map(row_idx_inner.ns_.data(), row_idx_inner.ns_.size()) - 1),
col_idx.n_ - 1);
},
stan::math::to_ref(x), std::forward<MultiIndex>(row_idx));
}
Expand All @@ -482,36 +494,38 @@ inline auto rvalue(EigMat&& x, const char* name, MultiIndex&& row_idx,
* Types: matrix[multi, multi] = matrix
*
* @tparam EigMat An eigen matrix
* @tparam RowMultiIndex A multi index.
* @tparam ColMultiIndex A multi index.
* @param[in] x Matrix to index.
* @param[in] name String form of expression being evaluated.
* @param[in] row_idx multi index for selecting rows.
* @param[in] col_idx multi index for selecting cols.
* @return Result of indexing matrix.
*/
template <typename EigMat, typename RowIndexMulti, typename ColIndexMulti,
require_eigen_dense_dynamic_t<EigMat>* = nullptr,
require_all_same_t<RowIndexMulti, ColIndexMulti, index_multi> * = nullptr>
inline auto rvalue(EigMat&& x, const char* name, RowIndexMulti&& row_idx,
ColIndexMulti&& col_idx) {
template <
typename EigMat, typename RowIndexMulti, typename ColIndexMulti,
require_eigen_dense_dynamic_t<EigMat>* = nullptr,
require_all_same_t<RowMultiIndex, ColMultiIndex, index_multi>* = nullptr>
inline auto rvalue(EigMat&& x, const char* name, RowMultiMulti&& row_idx,
ColMultiIndex&& col_idx) {
const Eigen::Index rows = row_idx.ns_.size();
const Eigen::Index cols = col_idx.ns_.size();
for (auto idx_i : row_idx.ns_) {
math::check_range("matrix[uni, multi] row indexing", name, x.rows(),
idx_i);
math::check_range("matrix[uni, multi] row indexing", name, x.rows(), idx_i);
}
for (auto idx_j : col_idx.ns_) {
math::check_range("matrix[uni, multi] col indexing", name, x.cols(),
idx_j);
math::check_range("matrix[uni, multi] col indexing", name, x.cols(), idx_j);
}
return stan::math::make_holder(
[name](auto&& x_ref, auto&& row_idx_inner, auto&& col_idx_inner) {
using vec_map = Eigen::Map<Eigen::Array<int, -1, 1>>;
return x_ref((vec_map(row_idx_inner.ns_.data(), row_idx_inner.ns_.size()) - 1),
(vec_map(col_idx_inner.ns_.data(), col_idx_inner.ns_.size()) - 1));
return x_ref(
(vec_map(row_idx_inner.ns_.data(), row_idx_inner.ns_.size()) - 1),
(vec_map(col_idx_inner.ns_.data(), col_idx_inner.ns_.size()) - 1));
},
stan::math::to_ref(std::forward<EigMat>(x)),
std::forward<RowIndexMulti>(row_idx),
std::forward<ColIndexMulti>(col_idx));
std::forward<RowMultiIndex>(row_idx),
std::forward<ColMultiIndex>(col_idx));
}

/**
Expand Down Expand Up @@ -542,7 +556,9 @@ inline auto rvalue(Mat&& x, const char* name, const Idx& row_idx,
* Types: matrix[Idx, multi] = matrix
*
* @tparam EigMat An eigen matrix
* @param[in] x Eigen matrix.
* @tparam Idx An index type
* @tparam MultiIndex A multi index
* @param[in] x Eigen matrix
* @param[in] name String form of expression being evaluated.
* @param[in] row_idx index for selecting rows.
* @param[in] col_idx multi index for selecting cols.
Expand All @@ -552,23 +568,23 @@ template <typename EigMat, typename Idx, typename MultiIndex,
require_eigen_dense_dynamic_t<EigMat>* = nullptr,
require_not_same_t<std::decay_t<Idx>, index_uni>* = nullptr,
require_same_t<MultiIndex, index_multi>* = nullptr>
inline auto rvalue(EigMat&& x, const char* name,
Idx&& row_idx,
MultiIndex&& col_idx) {
inline auto rvalue(EigMat&& x, const char* name, Idx&& row_idx,
MultiIndex&& col_idx) {
for (auto idx_j : col_idx.ns_) {
math::check_range("matrix[..., multi] column indexing", name, x.cols(),
idx_j);
math::check_range("matrix[..., multi] column indexing", name, x.cols(),
idx_j);
}
return stan::math::make_holder(
[name](auto&& x_ref, auto&& row_idx_inner, auto&& col_idx_inner) {
using vec_map = Eigen::Map<Eigen::Array<int, -1, 1>>;
return rvalue(x_ref(Eigen::all,
(vec_map(col_idx_inner.ns_.data(), col_idx_inner.ns_.size()) - 1)),
name, std::forward<decltype(row_idx_inner)>(row_idx_inner));
return rvalue(
x_ref(Eigen::all,
(vec_map(col_idx_inner.ns_.data(), col_idx_inner.ns_.size())
- 1)),
name, std::forward<decltype(row_idx_inner)>(row_idx_inner));
},
stan::math::to_ref(std::forward<EigMat>(x)),
std::forward<Idx>(row_idx),
std::forward<MultiIndex>(col_idx));
stan::math::to_ref(std::forward<EigMat>(x)), std::forward<Idx>(row_idx),
std::forward<MultiIndex>(col_idx));
}

/**
Expand All @@ -578,6 +594,7 @@ inline auto rvalue(EigMat&& x, const char* name,
*
* @tparam Mat An eigen matrix or `var_value<T>` whose inner type is an Eigen
* matrix.
* @tparam Idx An index type
* @param[in] x type
* @param[in] name Name of variable
* @param[in] row_idx index for selecting rows.
Expand Down Expand Up @@ -680,7 +697,7 @@ inline auto rvalue(Mat&& x, const char* name, Idx&& row_idx,
* Types: std::vector<T>[uni | Idx] : T[Idx]
*
* @tparam T Type of list elements.
* @tparam Idx Index list type for indexes after first index.
* @tparam Idxs Index list type for indexes after first index.
* @param[in] v Container of list elements.
* @param[in] name String form of expression being evaluated.
* @param[in] idx1 first index.
Expand Down Expand Up @@ -744,7 +761,7 @@ inline auto rvalue(StdVec&& v, const char* name, index_uni idx) {
*
* @tparam T Type of list elements.
* @tparam Idx1 Index list type for first index.
* @tparam Idx2 Index list type for second index index.
* @tparam Idxs Index list type for second index index.
* @param[in] v Container of list elements.
* @param[in] name String form of expression being evaluated.
* @param[in] idx1 first index
Expand All @@ -756,8 +773,8 @@ template <typename StdVec, typename Idx1, typename... Idxs,
require_not_same_t<Idx1, index_uni>* = nullptr>
inline auto rvalue(StdVec&& v, const char* name, const Idx1& idx1,
Idxs&&... idxs) {
using inner_type = plain_type_t<decltype(
rvalue(v[rvalue_at(0, idx1) - 1], name, idxs...))>;
using inner_type = plain_type_t<decltype(rvalue(v[rvalue_at(0, idx1) - 1],
name, idxs...))>;
const auto index_size = rvalue_index_size(idx1, v.size());
stan::math::check_greater_or_equal("array[..., ...] indexing", "size",
index_size, 0);
Expand Down

0 comments on commit fc3d4ff

Please sign in to comment.