EmrysFilter 0.1.0
State estimation utilities in C++23 using Eigen
Loading...
Searching...
No Matches
Emrys::LogSumExp< Scalar > Class Template Reference

Streaming log-sum-exp: accumulates \( \log \sum_i e^{x_i} \) without overflow. More...

#include <EmrysFilter/Core/Weights.h>

Public Member Functions

void add (Scalar x)
 Adds x to the sum, rescaling the running sum if x is the new maximum.
template<typename Derived>
void add (Eigen::ArrayBase< Derived > const &xs)
 Adds every element of xs, as if they had been added one by one, up to rounding.
void add (LogSumExp const &other)
 Adds everything added to other, as if its inputs had been added one by one, up to rounding.
Scalar value () const

Detailed Description

template<typename Scalar>
class Emrys::LogSumExp< Scalar >

Streaming log-sum-exp: accumulates \( \log \sum_i e^{x_i} \) without overflow.

Inputs of -inf contribute nothing; an empty sum is -inf.

The running maximum \( m \) and sum \( s = \sum_i e^{x_i - m} \) are rescaled when a larger input arrives, the one-pass form of the log-sum-exp trick.

Adding one input is not associative, but adding a whole sum is: the pairs combine as \( (m_1, s_1) \oplus (m_2, s_2) = (m, \; s_1 e^{m_1 - m} + s_2 e^{m_2 - m}) \) with \( m = \max(m_1, m_2) \), whose identity is the empty sum, \( (-\infty, 0) \). So a reduction streams the inputs of each chunk and combines the chunks' sums (logSumExp()).

Member Function Documentation

◆ add() [1/2]

template<typename Scalar>
template<typename Derived>
void Emrys::LogSumExp< Scalar >::add ( Eigen::ArrayBase< Derived > const & xs)
inline

Adds every element of xs, as if they had been added one by one, up to rounding.

For float and double the array's sum is taken whole, as its maximum and then the sum of its exponentials relative to that, two whole-array expressions that Eigen vectorizes, and joined to this sum as add(LogSumExp) joins two; other scalars add the elements one by one. A NaN gives NaN either way.

◆ add() [2/2]

template<typename Scalar>
void Emrys::LogSumExp< Scalar >::add ( LogSumExp< Scalar > const & other)
inline

Adds everything added to other, as if its inputs had been added one by one, up to rounding.

Associative and commutative, with the empty sum as identity.

◆ value()

template<typename Scalar>
Scalar Emrys::LogSumExp< Scalar >::value ( ) const
inlinenodiscard
Returns
\( \log \sum_i e^{x_i} \) over everything added so far, or -inf for nothing.

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