Learn R Programming

mBPCR (version 1.26.0)

logAdd: Overflow-safe computation of the logarithm of a sum

Description

Function to compute the logarithm of a sum of small numbers, avoiding overflow.

Usage

logAdd(x)

Arguments

x
array or matrix containing the logarithm of the terms of the sum. If x is a matrix, the function return the results by column.

Value

If x is an array, the function returns $log(sum_i(e^x[i]))$, otherwise it returns an array containing the results by column.

Examples

Run this code
x <- log(c(0.0001, 0.0003, 0.000006))
y <- logAdd(x) 
##verification that the computation is correct
z <- sum(c(0.0001, 0.0003, 0.000006))
z
exp(y)

Run the code above in your browser using DataLab