/usr/share/doc/r-cran-matrixstats/tests/logSumExp.R is in r-cran-matrixstats 0.14.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | library("matrixStats")
library("stats")
logSumExp_R <- function(lx, na.rm=FALSE) {
log(sum(exp(lx), na.rm=na.rm))
}
## R-help thread \emph{'[R] Beyond double-precision?'} on May 9, 2009.
for (mode in c("integer", "double")) {
cat("mode: ", mode, "\n", sep="")
set.seed(1)
x <- runif(20, min=1.0, max=3.0)
storage.mode(x) <- mode
str(x)
## The logarithm of the harmonic mean
y0 <- log(1/mean(1/x))
print(y0) ## -1.600885
lx <- log(x)
y1 <- log(length(x)) - logSumExp(-lx)
print(y1) ## [1] -1.600885
# Sanity check
stopifnot(all.equal(y1, y0))
y2 <- log(length(x)) - logSumExp_R(-lx)
# Sanity check
stopifnot(all.equal(y2, y0))
} # for (mode ...)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Missing values
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
## NA values
lx <- c(1,2,3)
lx[2] <- NA_real_
y0 <- logSumExp_R(lx, na.rm=FALSE)
y <- logSumExp(lx, na.rm=FALSE)
print(y)
stopifnot(identical(y, NA_real_))
stopifnot(all.equal(y, y0))
y0 <- logSumExp_R(lx, na.rm=TRUE)
y <- logSumExp(lx, na.rm=TRUE)
print(y)
stopifnot(all.equal(y, y0))
## NaN values
lx <- c(1,2,3)
lx[2] <- NaN
y0 <- logSumExp_R(lx, na.rm=FALSE)
y <- logSumExp(lx, na.rm=FALSE)
print(y)
stopifnot(identical(y, NA_real_))
stopifnot(all.equal(y, y0))
y0 <- logSumExp_R(lx, na.rm=TRUE)
y <- logSumExp(lx, na.rm=TRUE)
print(y)
stopifnot(all.equal(y, y0))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Corner cases
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
## Zero-length vectors
lx <- numeric(0L)
y0 <- logSumExp_R(lx)
y <- logSumExp(lx)
print(y)
stopifnot(identical(y, -Inf))
stopifnot(all.equal(y, y0))
## Vectors of length one
lx <- 1.0
y0 <- logSumExp_R(lx)
y <- logSumExp(lx)
print(y)
stopifnot(identical(y, lx))
stopifnot(all.equal(y, y0))
lx <- NA_real_
y0 <- logSumExp_R(lx, na.rm=TRUE)
y <- logSumExp(lx, na.rm=TRUE)
print(y)
stopifnot(identical(y, -Inf))
stopifnot(all.equal(y, y0))
## All missing values
lx <- c(NA_real_, NA_real_)
y0 <- logSumExp_R(lx, na.rm=TRUE)
y <- logSumExp(lx, na.rm=TRUE)
print(y)
stopifnot(identical(y, -Inf))
stopifnot(all.equal(y, y0))
lx <- c(NA_real_, NA_real_)
y0 <- logSumExp_R(lx, na.rm=FALSE)
y <- logSumExp(lx, na.rm=FALSE)
print(y)
stopifnot(identical(y, NA_real_))
stopifnot(all.equal(y, y0))
## +Inf values
lx <- c(1, 2, +Inf)
y0 <- logSumExp_R(lx)
y <- logSumExp(lx)
print(y)
stopifnot(identical(y, +Inf))
stopifnot(all.equal(y, y0))
## First element is a missing value, cf. PR #33
lx <- c(NA_real_, 1)
y0 <- logSumExp_R(lx)
print(y0)
y <- logSumExp(lx, na.rm=FALSE)
print(y)
stopifnot(identical(y, NA_real_))
stopifnot(all.equal(y, y0))
y0 <- logSumExp_R(lx, na.rm=TRUE)
print(y0)
y <- logSumExp(lx, na.rm=TRUE)
print(y)
stopifnot(identical(y, 1))
stopifnot(all.equal(y, y0))
|