Automatic Differentiation
 
Loading...
Searching...
No Matches
accumulator.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_ACCUMULATOR_HPP
2#define STAN_MATH_REV_FUN_ACCUMULATOR_HPP
3
8#include <vector>
9#include <type_traits>
10
11namespace stan {
12namespace math {
13
24template <typename T>
26 private:
27 static constexpr int max_size_ = 128;
28 std::vector<var, arena_allocator<var>> buf_;
29
33 void check_size() {
34 if (buf_.size() == max_size_) {
36 buf_.resize(1);
37 buf_[0] = tmp;
38 }
39 }
40
41 public:
45 accumulator() : buf_() { buf_.reserve(max_size_); }
46
57 template <typename S, typename = require_stan_scalar_t<S>>
58 inline void add(S x) {
59 check_size();
60 buf_.push_back(x);
61 }
62
70 template <typename S, require_matrix_t<S>* = nullptr>
71 inline void add(const S& m) {
72 check_size();
73 buf_.push_back(stan::math::sum(m));
74 }
75
82 template <typename S, require_std_vector_vt<is_stan_scalar, S>* = nullptr>
83 inline void add(const S& xs) {
84 check_size();
85 this->add(stan::math::sum(xs));
86 }
87
95 template <typename S,
97 inline void add(const S& xs) {
98 check_size();
99 for (const auto& i : xs) {
100 this->add(i);
101 }
102 }
103
104#ifdef STAN_OPENCL
110 template <typename S,
112 inline void add(const var_value<S>& xs) {
113 check_size();
114 buf_.push_back(stan::math::sum(xs));
115 }
116
122 template <typename S,
124 inline void add(const S& xs) {
125 check_size();
126 buf_.push_back(stan::math::sum(xs));
127 }
128
129#endif
135 inline var sum() const { return stan::math::sum(buf_); }
136};
137
138} // namespace math
139} // namespace stan
140
141#endif
void check_size()
Checks if the internal buffer is full and if so reduces it to 1 element.
void add(S x)
Add the specified arithmetic type value to the buffer after static casting it to the class type T.
std::vector< var, arena_allocator< var > > buf_
void add(const S &m)
Add each entry in the specified matrix, vector, or row vector of values to the buffer.
void add(const var_value< S > &xs)
Sum each entry and then push to the buffer.
void add(const S &xs)
Add each entry in the specified std vector of values to the buffer.
var sum() const
Return the sum of the accumulated values.
void add(S x)
Add the specified arithmetic type value to the buffer after static casting it to the class type T.
std::vector< T > buf_
Class to accumulate values and eventually return their sum.
require_all_t< is_kernel_expression_and_not_scalar< Types >... > require_all_kernel_expressions_and_none_scalar_t
Enables a template if all given types are non-scalar types that are a valid kernel generator expressi...
require_t< container_type_check_base< is_std_vector, value_type_t, TypeCheck, Check... > > require_std_vector_vt
Require type satisfies is_std_vector.
require_t< is_var< std::decay_t< T > > > require_var_t
Require type satisfies is_var.
Definition is_var.hpp:24
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...