Skip to content

Commit

Permalink
colCummins(), colCummaxs(), rowCummins(), and rowCummaxs() now suppor…
Browse files Browse the repository at this point in the history
…t also logical input [#215]
  • Loading branch information
HenrikBengtsson committed Mar 28, 2022
1 parent 9077b54 commit 93eb201
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: matrixStats
Version: 0.61.0-9002
Version: 0.61.0-9003
Depends:
R (>= 2.12.0)
Suggests:
Expand Down
7 changes: 6 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Package: matrixStats
====================

Version: 0.61.0-9002 [2022-03-28]
Version: 0.61.0-9003 [2022-03-28]

NEW FEATURES:

* colCummins(), colCummaxs(), rowCummins(), and rowCummaxs() now support
also logical input.

MISCELLANEOUS:

Expand Down
4 changes: 2 additions & 2 deletions src/rowCummaxs.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SEXP rowCummaxs(SEXP x, SEXP dim, SEXP rows, SEXP cols, SEXP byRow, SEXP useName
PROTECT(dim = coerceVector(dim, INTSXP));

/* Argument 'x' and 'dim': */
assertArgMatrix(x, dim, (R_TYPE_INT | R_TYPE_REAL), "x");
assertArgMatrix(x, dim, (R_TYPE_LGL | R_TYPE_INT | R_TYPE_REAL), "x");
nrow = asR_xlen_t(dim, 0);
ncol = asR_xlen_t(dim, 1);

Expand All @@ -47,7 +47,7 @@ SEXP rowCummaxs(SEXP x, SEXP dim, SEXP rows, SEXP cols, SEXP byRow, SEXP useName
}
}
UNPROTECT(1);
} else if (isInteger(x)) {
} else if (isInteger(x) | isLogical(x)) {
PROTECT(ans = allocMatrix(INTSXP, nrows, ncols));
rowCummaxs_int(INTEGER(x), nrow, ncol, crows, nrows, ccols, ncols, byrow, INTEGER(ans));
if (usenames != NA_LOGICAL && usenames){
Expand Down
4 changes: 2 additions & 2 deletions src/rowCummins.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SEXP rowCummins(SEXP x, SEXP dim, SEXP rows, SEXP cols, SEXP byRow, SEXP useName
PROTECT(dim = coerceVector(dim, INTSXP));

/* Argument 'x' and 'dim': */
assertArgMatrix(x, dim, (R_TYPE_INT | R_TYPE_REAL), "x");
assertArgMatrix(x, dim, (R_TYPE_LGL | R_TYPE_INT | R_TYPE_REAL), "x");
nrow = asR_xlen_t(dim, 0);
ncol = asR_xlen_t(dim, 1);

Expand All @@ -47,7 +47,7 @@ SEXP rowCummins(SEXP x, SEXP dim, SEXP rows, SEXP cols, SEXP byRow, SEXP useName
}
}
UNPROTECT(1);
} else if (isInteger(x)) {
} else if (isInteger(x) | isLogical(x)) {
PROTECT(ans = allocMatrix(INTSXP, nrows, ncols));
rowCummins_int(INTEGER(x), nrow, ncol, crows, nrows, ccols, ncols, byrow, INTEGER(ans));
if (usenames != NA_LOGICAL && usenames){
Expand Down
15 changes: 7 additions & 8 deletions tests/rowCumMinMaxs.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ rowCummaxs_R <- function(x, ..., useNames = NA) {
dimnames <- dimnames(x)
if (isTRUE(useNames) && !is.null(dimnames)) dimnames(y) <- dimnames

storage.mode(y) <- mode
y
}


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# With and without some NAs
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for (mode in c("integer", "double")) {
for (mode in c("logical", "integer", "double")) {
for (add_na in c(FALSE, TRUE)) {
cat("add_na = ", add_na, "\n", sep = "")

Expand Down Expand Up @@ -78,7 +77,7 @@ for (mode in c("integer", "double")) {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# All NAs
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for (mode in c("integer", "double")) {
for (mode in c("logical", "integer", "double")) {
x <- matrix(NA_real_, nrow = 10L, ncol = 5L)
cat("mode: ", mode, "\n", sep = "")
storage.mode(x) <- mode
Expand Down Expand Up @@ -111,12 +110,12 @@ for (mode in c("integer", "double")) {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# A 1x1 matrix
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for (mode in c("integer", "double")) {
for (mode in c("logical", "integer", "double")) {
x <- matrix(0, nrow = 1L, ncol = 1L)
cat("mode: ", mode, "\n", sep = "")
storage.mode(x) <- mode
str(x)

# To check dimnames attribute
dimnames <- list("a", "A")

Expand Down Expand Up @@ -147,15 +146,16 @@ for (mode in c("integer", "double")) {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Corner cases
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for (mode in c("integer", "double")) {
for (mode in c("logical", "integer", "double")) {
cat("mode: ", mode, "\n", sep = "")
value <- 0
storage.mode(value) <- mode
value0 <- if (mode == "logical") 0L else value

# A 0x0 matrix
x <- matrix(value, nrow = 0L, ncol = 0L)
str(x)
r0 <- matrix(value, nrow = nrow(x), ncol = ncol(x))
r0 <- matrix(value0, nrow = nrow(x), ncol = ncol(x))
r1 <- rowCummins(x)
r2 <- t(colCummins(t(x)))
stopifnot(all.equal(r1, r2))
Expand All @@ -178,7 +178,6 @@ for (mode in c("integer", "double")) {
stopifnot(all.equal(r1, r2))
stopifnot(all.equal(r1, r0))
stopifnot(all.equal(r2, r0))

r0 <- rowCummaxs_R(x, useNames = useNames)
r1 <- rowCummaxs(x, useNames = useNames)
r2 <- t(colCummaxs(t(x), useNames = useNames))
Expand Down

0 comments on commit 93eb201

Please sign in to comment.