Contains useful functions for performing arithmetic in log space, such as computing the maximum of an array, computing sum-exp, log-sum-exp, and normalization. This file also contains log_cache, which caches the logarithms of positive integers. All functions in this file avoid loss of precision by performing operations in log space when feasible.
In the following example, the array { -100.0, -100.0, -100.0 + log(2.0), -100.0 + log(4.0) }
of log probabilities is normalized and printed. The expected output is [0.125000, 0.125000, 0.250000, 0.500000]
.
#include <math/log.h> #include <core/io.h> using namespace core; int main() { double a[] = { -100.0, -100.0, -100.0 + log(2.0), -100.0 + log(4.0) }; normalize_exp(a, 4); print(a, 4, stdout); }
Classes, functions, and variables in this file | |
---|---|
#define | LOG_ZERO |
float | log_probability (float src) |
double | log_probability (double src) |
void | max (V & max, const T * src, unsigned int length, unsigned int skip) |
void | max (V & max, const T * src, const P * prior, unsigned int length, unsigned int skip) |
V | max (const T * src, unsigned int length, unsigned int skip) |
V | max (const T * src, const P * prior, unsigned int length, unsigned int skip) |
V | max (const Collection & src) |
V | max (const T * src, unsigned int length) |
V | sumexp (const T * src, unsigned int length, unsigned int skip, const V & shift) |
V | sumexp (const T * src, const P * prior, unsigned int length, unsigned int skip, const V & shift) |
V | sumexp (const Collection & src, const V & shift) |
void | normalize (const T * src, V * dst, unsigned int length, unsigned int skip, const V & normalization) |
void | normalize_exp (const T * src, V * dst, unsigned int length, unsigned int skip, const V & normalization) |
void | normalize_exp (const T * src, const P * prior, V * dst, unsigned int length, unsigned int skip, const V & normalization) |
V | logsumexp (const T * src, unsigned int length, unsigned int skip = 1) |
V | logsumexp (const T * src, const P * prior, unsigned int length, unsigned int skip = 1) |
V | logsumexp (const Collection & src) |
void | normalize (const T * src, V * dst, unsigned int length, unsigned int skip = 1) |
void | normalize_exp (const T * src, V * dst, unsigned int length, unsigned int skip = 1) |
void | normalize_exp (const T * src, const P * prior, V * dst, unsigned int length, unsigned int skip = 1) |
void | normalize_exp (T * x, unsigned int length) |
V | logsumexp (const V & first, const V & second) |
V | logdiffexp (const V & first, const V & second) |
struct | log_cache |
This value is used for log(0)
in some operations to avoid floating-point infinities and improve performance.
float | src | ) |
Returns src
. This function is defined so that the function in this file max
, sum_exp
, and normalize
behave correctly when T
is float
.
double | src | ) |
Returns src
. This function is defined so that the function in this file max
, sum_exp
, and normalize
behave correctly when T
is double
.
V & | max, | |
const T * | src, | |
unsigned int | length, | |
unsigned int | skip | ) |
src
with the given length
, only inspecting the elements at every skip
locations, until length
elements have been considered. For example, if skip = 2
, this function will find the maximum of the elements at every even index. The computed maximum is stored in max
.T | the type of every element in |
V | the type of the log probabilities. |
V & | max, | |
const T * | src, | |
const P * | prior, | |
unsigned int | length, | |
unsigned int | skip | ) |
Computes the maximum sum of the log probability of the elements in src
and the elements in prior
with the given length
, only inspecting the elements in src
at every skip
locations, until length
elements have been considered. For example, if skip = 2
, this function will find the maximum of the elements at every even index. Note that skip
does not affect the inspection of elements in prior
. The computed maximum is stored in max
.
So more precisely, this function computes:
V log_probability(const T&)
and $ g(\cdot) $ is the function V log_probability(const P&)
.T | the type of every element in |
P | the type of every element in |
V | the type of the log probabilities. |
const T * | src, | |
unsigned int | length, | |
unsigned int | skip | ) |
src
with the given length
, only inspecting the elements at every skip
locations, until length
elements have been considered. For example, if skip = 2
, this function will find the maximum of the elements at every even index.T | the type of every element in |
V | the type of the log probabilities. By default, if |
const T * | src, | |
const P * | prior, | |
unsigned int | length, | |
unsigned int | skip | ) |
Returns the maximum sum of the log probability of the elements in src
and the elements in prior
with the given length
, only inspecting the elements in src
at every skip
locations, until length
elements have been considered. For example, if skip = 2
, this function will find the maximum of the elements at every even index. Note that skip
does not affect the inspection of elements in prior
. The computed maximum is stored in max
.
So more precisely, this function computes:
V log_probability(const T&)
and $ g(\cdot) $ is the function V log_probability(const P&)
.T | the type of every element in |
P | the type of every element in |
V | the type of the log probabilities. By default, if |
src
.Collection | the type of a collection of items, each of type |
V | the type of the log probabilities. |
src
.T | the type of every element in |
const T * | src, | |
unsigned int | length, | |
unsigned int | skip, | |
const V & | shift | ) |
V log_probability(const T&)
.T | the type of every element in |
V | the type of the log probabilities. |
const T * | src, | |
const P * | prior, | |
unsigned int | length, | |
unsigned int | skip, | |
const V & | shift | ) |
V log_probability(const T&)
and $ g(\cdot) $ is the function V log_probability(const P&)
.T | the type of every element in |
P | the type of every element in |
V | the type of the log probabilities. |
const Collection & | src, | |
const V & | shift | ) |
V log_probability(const T&)
.Collection | the type of a collection of items, each of type |
V | the type of the log probabilities. |
const T * | src, | |
V * | dst, | |
unsigned int | length, | |
unsigned int | skip, | |
const V & | normalization | ) |
src
into dst
, subtracting normalization
from each element. This function assumes dst
is initialized and has sufficient capacity. This function only considers elements at every skip
positions, until length
elements have been considered. For example, if skip = 2
, only the elements at even indices are copied. Note that src
and dst
can be the same.T | the type of every element in |
V | the type of the log probabilities. |
const T * | src, | |
V * | dst, | |
unsigned int | length, | |
unsigned int | skip, | |
const V & | normalization | ) |
src
into dst
, subtracting normalization
from each element, and taking the natural exponent of the result. This function assumes dst
is initialized and has sufficient capacity. This function only considers elements at every skip
positions, until length
elements have been considered. For example, if skip = 2
, only the elements at even indices are copied. Note that src
and dst
can be the same.T | the type of every element in |
V | the type of the log probabilities. |
const T * | src, | |
const P * | prior, | |
V * | dst, | |
unsigned int | length, | |
unsigned int | skip, | |
const V & | normalization | ) |
dst[i]
, where i
ranges from 0
to length - 1
, and $ f(\cdot) $ is the function V log_probability(const T&)
and $ g(\cdot) $ is the function V log_probability(const P&)
. Note that src
and dst
can be the same.T | the type of every element in |
P | the type of every element in |
V | the type of the log probabilities. |
const T * | src, | |
unsigned int | length, | |
unsigned int | skip = 1 | ) |
src
, skipping every skip
elements, until length
elements have been considered.T | the type of every element in |
V | the type of the log probabilities. |
const T * | src, | |
const P * | prior, | |
unsigned int | length, | |
unsigned int | skip = 1 | ) |
src
, with the given prior
, skipping every skip
elements, until length
elements have been considered. Note that skip
does not affect the inspection of the elements in prior
.T | the type of every element in |
P | the type of every element in |
V | the type of the log probabilities. |
const Collection & | src | ) |
src
.Collection | the type of a collection of items, each of type |
V | the type of the log probabilities. |
const T * | src, | |
V * | dst, | |
unsigned int | length, | |
unsigned int | skip = 1 | ) |
src
and stores them in dst
, skipping every skip
elements, until length
elements have been considered. Note that src
and dst
can be the same.T | the type of every element in |
V | the type of the log probabilities. |
const T * | src, | |
V * | dst, | |
unsigned int | length, | |
unsigned int | skip = 1 | ) |
src
and stores them in dst
, skipping every skip
elements, until length
elements have been considered. Note that src
and dst
can be the same.T | the type of every element in |
V | the type of the log probabilities. |
const T * | src, | |
const P * | prior, | |
V * | dst, | |
unsigned int | length, | |
unsigned int | skip = 1 | ) |
src
and stores them in dst
, skipping every skip
elements, until length
elements have been considered. Note that skip
does not affect the inspection of the elements in prior
. Also note that src
and dst
can be the same.T | the type of every element in |
P | the type of every element in |
V | the type of the log probabilities. |
Normalizes the elements in src
. Like all functions in this file, loss of precision is avoided by performing operations in log space when feasible.
first
and second
.V | satisfies is_arithmetic. |
exp(first) - exp(second)
.V | satisfies is_arithmetic. |
This structure caches natural logarithms of positive integers.
The following example displays a typical use-case of this cache. The expected output is log(2) = 0.693147, log(183) = 5.209486, sum from i=1 to 999 of log(i) = 5905.220423
.
#include <math/log.h> using namespace core; int main() { log_cache<double> cache(1000); printf("log(2) = %lf, ", cache.get(2)); printf("log(183) = %lf, ", cache.get(183)); double sum = 0.0; for (unsigned int i = 1; i < 1000; i++) sum += cache.get(i); printf("sum from i=1 to 999 of log(i) = %lf\n", sum); }
V | satisfies is_arithmetic. |
Public members | |
---|---|
log_cache (unsigned int initial_size) | |
bool | ensure_size (unsigned int requested_size) |
V | get (unsigned int x) const |
static log_cache< V > & | instance () |
unsigned int | initial_size | ) |
Constructs the cache with the given initial_size
. The natural logarithms up to and includig log(initial_size - 1)
are precomputed and stored.
unsigned int | requested_size | ) |
Checks that the natural logarithms up to and including log(requested_size - 1)
are computed and stored.
unsigned int | x | ) const |
Returns the natural logarithm of x
. This function assumes that log(x)
has already been computed, either by the constructor or by ensure_size, and no bounds checking is performed.