Learn R Programming

ISOpureR (version 1.1.3)

ISOpure.util.logsum: Log-sum-exp

Description

Prevents underflow/overflow using the log-sum-exp trick

Usage

ISOpure.util.logsum(xx, dimen);

Arguments

xx

A matrix of numerical values

dimen

The dimension along which the long sum is taken (1 for row, 2 for column)

Value

Returns log(sum(exp(x),dimen)), the log sum of exps, summing over dimension dimen but in a way that tries to avoid underflow/overflow.

Examples

Run this code
# NOT RUN {
x <-  c(1, 1e20, 1e40, -1e40, -1e20, -1);
x <- as.matrix(x);

# compute log sum exp without the function
log(sum(exp(x)))
#[1] Inf

# compute log sum exp with the function
ISOpure.util.logsum(x, 1)
#[1] 1e+40 
# }

Run the code above in your browser using DataLab