/usr/share/doc/r-cran-matrixstats/tests/rowCumprods.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 133 134 135 | library("matrixStats")
rowCumprods_R <- function(x) {
suppressWarnings({
t(apply(x, MARGIN=1L, FUN=cumprod))
})
}
colCumprods_R <- function(x) {
suppressWarnings({
apply(x, MARGIN=2L, FUN=cumprod)
})
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# With and without some NAs
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for (mode in c("integer", "double")) {
for (addNA in c(FALSE, TRUE)) {
cat("addNA=", addNA, "\n", sep="")
x <- matrix(1:100, nrow=20, ncol=5)
if (addNA) {
x[13:17,c(2,4)] <- NA_real_
}
cat("mode: ", mode, "\n", sep="")
storage.mode(x) <- mode
str(x)
# Row/column ranges
r0 <- rowCumprods_R(x)
r1 <- rowCumprods(x)
r2 <- t(colCumprods(t(x)))
stopifnot(all.equal(r1, r2))
stopifnot(all.equal(r1, r0))
stopifnot(all.equal(r2, r0))
} # for (addNA ...)
} # for (mode ...)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# All NAs
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for (mode in c("integer", "double")) {
x <- matrix(NA_real_, nrow=20, ncol=5)
cat("mode: ", mode, "\n", sep="")
storage.mode(x) <- mode
str(x)
r0 <- rowCumprods_R(x)
r1 <- rowCumprods(x)
r2 <- t(colCumprods(t(x)))
stopifnot(all.equal(r1, r2))
stopifnot(all.equal(r1, r0))
stopifnot(all.equal(r2, r0))
} # for (mode ...)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# A 1x1 matrix
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for (mode in c("integer", "double")) {
x <- matrix(0, nrow=1, ncol=1)
cat("mode: ", mode, "\n", sep="")
storage.mode(x) <- mode
str(x)
r0 <- rowCumprods_R(x)
r1 <- rowCumprods(x)
r2 <- t(colCumprods(t(x)))
stopifnot(all.equal(r1, r2))
stopifnot(all.equal(r1, r0))
stopifnot(all.equal(r2, r0))
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# BUG FIX TEST: Assert zeros don't trump NAs in integer matrices
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for (mode in c("integer", "double")) {
x <- matrix(NA_real_, nrow=3, ncol=2)
x[1,2] <- 0
x[2,2] <- 1
x[3,1] <- 0
storage.mode(x) <- mode
cat("mode: ", mode, "\n", sep="")
str(x)
r0 <- rowCumprods_R(x)
r1 <- rowCumprods(x)
r2 <- t(colCumprods(t(x)))
stopifnot(all.equal(r1, r2))
stopifnot(all.equal(r1, r0))
stopifnot(all.equal(r2, r0))
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Corner cases
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for (mode in c("integer", "double")) {
cat("mode: ", mode, "\n", sep="")
value <- 0
storage.mode(value) <- mode
# A 0x0 matrix
x <- matrix(value, nrow=0L, ncol=0L)
str(x)
r0 <- matrix(value, nrow=nrow(x), ncol=ncol(x))
r1 <- rowCumprods(x)
r2 <- t(colCumprods(t(x)))
stopifnot(all.equal(r1, r2))
stopifnot(all.equal(r1, r0))
stopifnot(all.equal(r2, r0))
# A 0xK matrix
x <- matrix(value, nrow=0L, ncol=5L)
str(x)
r0 <- matrix(value, nrow=nrow(x), ncol=ncol(x))
r1 <- rowCumprods(x)
r2 <- t(colCumprods(t(x)))
stopifnot(all.equal(r1, r2))
stopifnot(all.equal(r1, r0))
stopifnot(all.equal(r2, r0))
# A Nx0 matrix
x <- matrix(value, nrow=5L, ncol=0L)
str(x)
r0 <- matrix(value, nrow=nrow(x), ncol=ncol(x))
r1 <- rowCumprods(x)
r2 <- t(colCumprods(t(x)))
stopifnot(all.equal(r1, r2))
stopifnot(all.equal(r1, r0))
stopifnot(all.equal(r2, r0))
} # for (mode ...)
|