Automatic Differentiation
 
Loading...
Searching...
No Matches
accumulator.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_ACCUMULATOR_HPP
2#define STAN_MATH_FWD_FUN_ACCUMULATOR_HPP
3
9
10#include <vector>
11#include <type_traits>
12
13namespace stan {
14namespace math {
15template <typename T, typename>
16class accumulator;
27template <typename T>
29 private:
30 std::vector<T> buf_;
31
32 public:
43 template <typename S, typename = require_stan_scalar_t<S>>
44 inline void add(S x) {
45 buf_.push_back(x);
46 }
47
55 template <typename S, require_matrix_t<S>* = nullptr>
56 inline void add(const S& m) {
57 buf_.push_back(stan::math::sum(m));
58 }
59
69 template <typename S>
70 inline void add(const std::vector<S>& xs) {
71 for (size_t i = 0; i < xs.size(); ++i) {
72 this->add(xs[i]);
73 }
74 }
75
76#ifdef STAN_OPENCL
77
83 template <typename S,
85 inline void add(const S& xs) {
86 buf_.push_back(stan::math::sum(xs));
87 }
88
89#endif
90
96 inline T sum() const { return stan::math::sum(buf_); }
97};
98
99} // namespace math
100} // namespace stan
101
102#endif
void add(const S &m)
Add each entry in the specified matrix, vector, or row vector of values to the buffer.
void add(const S &xs)
Sum each entry and then push to the buffer.
T sum() const
Return the sum of the accumulated values.
void add(const std::vector< S > &xs)
Recursively add each entry in the specified standard vector to the buffer.
void add(S x)
Add the specified arithmetic type value to the buffer after static casting it to the class type T.
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_t< is_fvar< std::decay_t< T > > > require_fvar_t
Require type satisfies is_fvar.
Definition is_fvar.hpp:25
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...
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 ...