/usr/share/doc/r-cran-matrixstats/tests/rowProds.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 | library("matrixStats")
rowProds_R <- function(x, na.rm=FALSE) {
apply(x, MARGIN=1L, FUN=prod, na.rm=na.rm)
}
colProds_R <- function(x, na.rm=FALSE) {
apply(x, MARGIN=2L, FUN=prod, na.rm=na.rm)
}
all.equal.na <- function(target, current, ...) {
# Computations involving NaN may return NaN or NA, cf. ?is.nan
current[is.nan(current)] <- NA_real_
target[is.nan(target)] <- NA_real_
all.equal(target, current, ...)
}
for (mode in c("integer", "double")) {
# Missing values
x <- matrix(c(1,NA,NaN,1, 1,0,1,0), nrow=4, ncol=2)
cat("mode: ", mode, "\n", sep="")
storage.mode(x) <- mode
str(x)
y0 <- rowProds_R(x, na.rm=TRUE)
print(y0)
y1 <- rowProds(x, na.rm=TRUE)
print(y1)
y2 <- colProds(t(x), na.rm=TRUE)
print(y2)
stopifnot(all.equal(y1, y0))
stopifnot(all.equal(y1, x[,2]))
stopifnot(all.equal(y2, y1))
# Missing values
y0 <- rowProds_R(x, na.rm=FALSE)
print(y0)
y1 <- rowProds(x, na.rm=FALSE)
print(y1)
y2 <- colProds(t(x), na.rm=FALSE)
print(y2)
stopifnot(all.equal.na(y1, y0))
stopifnot(all.equal(y2, y1))
y3 <- x[,1]*x[,2]
print(y3)
stopifnot(all.equal.na(y1, y3))
# "Empty" rows
y0 <- rowProds_R(x[integer(0),,drop=FALSE], na.rm=FALSE)
print(y0)
y1 <- rowProds(x[integer(0),,drop=FALSE], na.rm=FALSE)
print(y1)
y2 <- colProds(t(x[integer(0),,drop=FALSE]), na.rm=FALSE)
print(y2)
stopifnot(all.equal.na(y1, y0))
stopifnot(all.equal(y2, y1))
stopifnot(length(y1) == 0L)
# Using product()
y1 <- rowProds(x, method="expSumLog", na.rm=FALSE)
print(y1)
y2 <- colProds(t(x), method="expSumLog", na.rm=FALSE)
print(y2)
stopifnot(all.equal(y2, y1))
} # for (mode ...)
# Bug report 2012-06-25
x <- matrix(c(1,1,1,1, 1,0,1,0), nrow=4, ncol=2)
y0 <- rowProds_R(x)
print(y0)
y1 <- rowProds(x)
print(y1)
y2 <- colProds(t(x))
print(y2)
stopifnot(all.equal.na(y1, y0))
stopifnot(all.equal.na(y1, x[,1]*x[,2]))
stopifnot(all.equal.na(y2, y1))
# Bug report 2014-03-25 ("all rows contains a zero")
x <- matrix(c(0,1,1,0), nrow=2, ncol=2)
y0 <- rowProds_R(x)
print(y0)
y1 <- rowProds(x)
print(y1)
y2 <- colProds(t(x))
print(y2)
stopifnot(all.equal.na(y1, y0))
stopifnot(all.equal.na(y1, c(0,0)))
stopifnot(all.equal.na(y2, y1))
|