Automatic Differentiation
 
Loading...
Searching...
No Matches
sum.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_SUM_HPP
2#define STAN_MATH_REV_FUN_SUM_HPP
3
15#include <vector>
16
17namespace stan {
18namespace math {
19
26template <typename Alloc>
27inline var sum(const std::vector<var, Alloc>& m) {
28 if (unlikely(m.empty())) {
29 return 0.0;
30 } else {
31 auto arena_m = to_arena(as_array_or_scalar(m));
32 return make_callback_var(arena_m.val().sum(), [arena_m](auto& vi) mutable {
33 arena_m.adj() += vi.adj();
34 });
35 }
36}
37
47template <typename T, require_rev_matrix_t<T>* = nullptr>
48inline var sum(T&& x) {
49 arena_t<T> x_arena(std::forward<T>(x));
50 return make_callback_var(sum(x_arena.val()), [x_arena](auto& vi) mutable {
51 x_arena.adj().array() += vi.adj();
52 });
53}
54
55} // namespace math
56} // namespace stan
57#endif
#define unlikely(x)
T as_array_or_scalar(T &&v)
Returns specified input value.
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
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...