Last data update: 2014.03.03

R: Overflow-safe computation of the logarithm of a sum
logAddR Documentation

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

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)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(mBPCR)
Loading required package: oligoClasses
Welcome to oligoClasses version 1.34.0
Loading required package: SNPchip
Welcome to SNPchip version 2.18.0
> png(filename="/home/ddbj/snapshot/RGM3/R_BC/result/mBPCR/logAdd.Rd_%03d_medium.png", width=480, height=480)
> ### Name: logAdd
> ### Title: Overflow-safe computation of the logarithm of a sum
> ### Aliases: logAdd
> ### Keywords: math
> 
> ### ** Examples
> 
> 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
[1] 0.000406
> exp(y)
[1] 0.000406
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>