Automatic Differentiation
 
Loading...
Searching...
No Matches
cumulative_sum.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_CUMULATIVE_SUM_HPP
2#define STAN_MATH_REV_FUN_CUMULATIVE_SUM_HPP
3
7#include <vector>
8#include <numeric>
9#include <functional>
10
11namespace stan {
12namespace math {
13
29template <typename EigVec, require_rev_vector_t<EigVec>* = nullptr>
30inline auto cumulative_sum(const EigVec& x) {
31 arena_t<EigVec> x_arena(x);
32 using return_t = return_var_matrix_t<EigVec>;
33 arena_t<return_t> res = cumulative_sum(x_arena.val()).eval();
34 if (unlikely(x.size() == 0)) {
35 return return_t(res);
36 }
37 reverse_pass_callback([x_arena, res]() mutable {
38 for (Eigen::Index i = x_arena.size() - 1; i > 0; --i) {
39 x_arena.adj().coeffRef(i) += res.adj().coeffRef(i);
40 res.adj().coeffRef(i - 1) += res.adj().coeffRef(i);
41 }
42 x_arena.adj().coeffRef(0) += res.adj().coeffRef(0);
43 });
44 return return_t(res);
45}
46
47} // namespace math
48} // namespace stan
49
50#endif
#define unlikely(x)
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
auto cumulative_sum(T_vec &&v)
Return the cumulative sum of the specified vector.
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
std::conditional_t< is_any_var_matrix< ReturnType, Types... >::value, stan::math::var_value< stan::math::promote_scalar_t< double, plain_type_t< ReturnType > > >, stan::math::promote_scalar_t< stan::math::var_value< double >, plain_type_t< ReturnType > > > return_var_matrix_t
Given an Eigen type and several inputs, determine if a matrix should be var<Matrix> or Matrix<var>.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...