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
14#include <vector>
15
16namespace stan {
17namespace math {
18
25template <typename Alloc>
26inline var sum(const std::vector<var, Alloc>& m) {
27 if (unlikely(m.empty())) {
28 return 0.0;
29 } else {
30 auto arena_m = to_arena(as_array_or_scalar(m));
31 return make_callback_var(arena_m.val().sum(), [arena_m](auto& vi) mutable {
32 arena_m.adj() += vi.adj();
33 });
34 }
35}
36
46template <typename T, require_rev_matrix_t<T>* = nullptr>
47inline var sum(T&& x) {
48 arena_t<T> x_arena(std::forward<T>(x));
49 return make_callback_var(sum(x_arena.val()), [x_arena](auto& vi) mutable {
50 x_arena.adj().array() += vi.adj();
51 });
52}
53
54} // namespace math
55} // namespace stan
56#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 ...