logspace.add

0th

Percentile

Logspace Arithmetix -- Addition and Subtraction

Compute the log(arithm) of a sum (or difference) from the log of terms without causing overflows and without throwing away large handfuls of accuracy.

logspace.add(lx, ly):=

$$\log (\exp (lx) + \exp (ly))$$

logspace.sub(lx, ly):=

$$\log (\exp (lx) - \exp (ly))$$

Keywords
arith
Usage
logspace.add(lx, ly)
logspace.sub(lx, ly)
Arguments
lx, ly

numeric vectors, typically of the same length, but will be recycled to common length as with other R arithmetic.

Value

a numeric vector of the same length as x+y.

Note

This is really from R's C source code for pgamma(), i.e., <R>/src/nmath/pgamma.c

The function definitions are very simple, logspace.sub() using log1mexp().

See Also

lsum, lssum; then pgamma()

Aliases
  • logspace.add
  • logspace.sub
Examples
# NOT RUN {
set.seed(12)
ly <- rnorm(100, sd= 50)
lx <- ly + abs(rnorm(100, sd=100))  # lx - ly must be positive for *.sub()
stopifnot(exprs = {
   all.equal(logspace.add(lx,ly),
             log(exp(lx) + exp(ly)), tol=1e-14)
   all.equal(logspace.sub(lx,ly),
             log(exp(lx) - exp(ly)), tol=1e-14)
})
# }
Documentation reproduced from package DPQ, version 0.3-3, License: GPL (>= 2)

Community examples

Looks like there are no examples yet.