ISOpure.util.logsum: Log-sum-exp

Description Usage Arguments Value Author(s) Examples

View source: R/ISOpure.util.logsum.R

Description

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

Usage

1
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.

Author(s)

Gerald Quon and Catalina Anghel

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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 

ISOpureR documentation built on May 2, 2018, 1:04 a.m.