Stan Math Library  2.20.0
reverse mode automatic differentiation
Public Member Functions | List of all members
stan::math::accumulator< T > Class Template Reference

Class to accumulate values and eventually return their sum. More...

#include <accumulator.hpp>

Public Member Functions

 accumulator ()
 Construct an accumulator. More...
 
 ~accumulator ()
 Destroy an accumulator. More...
 
template<typename S >
std::enable_if< std::is_arithmetic< S >::value, void >::type add (S x)
 Add the specified arithmetic type value to the buffer after static casting it to the class type T. More...
 
template<typename S >
std::enable_if< !std::is_arithmetic< S >::value, typename std::enable_if< std::is_same< S, T >::value, void >::type >::type add (const S &x)
 Add the specified non-arithmetic value to the buffer. More...
 
template<typename S , int R, int C>
void add (const Eigen::Matrix< S, R, C > &m)
 Add each entry in the specified matrix, vector, or row vector of values to the buffer. More...
 
template<typename S >
void add (const std::vector< S > &xs)
 Recursively add each entry in the specified standard vector to the buffer. More...
 
sum () const
 Return the sum of the accumulated values. More...
 

Detailed Description

template<typename T>
class stan::math::accumulator< T >

Class to accumulate values and eventually return their sum.

If no values are ever added, the return value is 0.

This class is useful for speeding up auto-diff of long sums because it uses the sum() operation (either from stan::math or one defined by argument-dependent lookup.

Template Parameters
TType of scalar added

Definition at line 23 of file accumulator.hpp.

Constructor & Destructor Documentation

◆ accumulator()

template<typename T>
stan::math::accumulator< T >::accumulator ( )
inline

Construct an accumulator.

Definition at line 31 of file accumulator.hpp.

◆ ~accumulator()

template<typename T>
stan::math::accumulator< T >::~accumulator ( )
inline

Destroy an accumulator.

Definition at line 36 of file accumulator.hpp.

Member Function Documentation

◆ add() [1/4]

template<typename T>
template<typename S >
std::enable_if<std::is_arithmetic<S>::value, void>::type stan::math::accumulator< T >::add ( x)
inline

Add the specified arithmetic type value to the buffer after static casting it to the class type T.

See the std library doc for std::is_arithmetic for information on what counts as an arithmetic type.

Template Parameters
SType of argument
Parameters
xValue to add

Definition at line 49 of file accumulator.hpp.

◆ add() [2/4]

template<typename T>
template<typename S >
std::enable_if< !std::is_arithmetic<S>::value, typename std::enable_if<std::is_same<S, T>::value, void>::type>::type stan::math::accumulator< T >::add ( const S &  x)
inline

Add the specified non-arithmetic value to the buffer.

This function is disabled if the type S is arithmetic or if it's not the same as T.

See the std library doc for std::is_arithmetic for information on what counts as an arithmetic type.

Template Parameters
SType of argument
Parameters
xValue to add

Definition at line 69 of file accumulator.hpp.

◆ add() [3/4]

template<typename T>
template<typename S , int R, int C>
void stan::math::accumulator< T >::add ( const Eigen::Matrix< S, R, C > &  m)
inline

Add each entry in the specified matrix, vector, or row vector of values to the buffer.

Template Parameters
Stype of values in matrix
Rnumber of rows in matrix
Cnumber of columns in matrix
Parameters
mMatrix of values to add

Definition at line 83 of file accumulator.hpp.

◆ add() [4/4]

template<typename T>
template<typename S >
void stan::math::accumulator< T >::add ( const std::vector< S > &  xs)
inline

Recursively add each entry in the specified standard vector to the buffer.

This will allow vectors of primitives, auto-diff variables to be added; if the vector entries are collections, their elements are recursively added.

Template Parameters
SType of value to recursively add.
Parameters
xsVector of entries to add

Definition at line 98 of file accumulator.hpp.

◆ sum()

template<typename T>
T stan::math::accumulator< T >::sum ( ) const
inline

Return the sum of the accumulated values.

Returns
Sum of accumulated values.

Definition at line 108 of file accumulator.hpp.


The documentation for this class was generated from the following file:

     [ Stan Home Page ] © 2011–2018, Stan Development Team.