From 93eb201c1d02d7656e3cd74344ee026cf33f3ba3 Mon Sep 17 00:00:00 2001 From: Henrik Bengtsson Date: Mon, 28 Mar 2022 12:42:15 -0700 Subject: [PATCH] colCummins(), colCummaxs(), rowCummins(), and rowCummaxs() now support also logical input [#215] --- DESCRIPTION | 2 +- NEWS | 7 ++++++- src/rowCummaxs.c | 4 ++-- src/rowCummins.c | 4 ++-- tests/rowCumMinMaxs.R | 15 +++++++-------- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 51722fe4..e9f7f171 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: matrixStats -Version: 0.61.0-9002 +Version: 0.61.0-9003 Depends: R (>= 2.12.0) Suggests: diff --git a/NEWS b/NEWS index 3a248a90..5ebf0cbb 100644 --- a/NEWS +++ b/NEWS @@ -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: diff --git a/src/rowCummaxs.c b/src/rowCummaxs.c index dbb72f49..72d151fe 100644 --- a/src/rowCummaxs.c +++ b/src/rowCummaxs.c @@ -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); @@ -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){ diff --git a/src/rowCummins.c b/src/rowCummins.c index 8a4b8ae9..25fd4453 100644 --- a/src/rowCummins.c +++ b/src/rowCummins.c @@ -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); @@ -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){ diff --git a/tests/rowCumMinMaxs.R b/tests/rowCumMinMaxs.R index bcbccc9c..c9f67c03 100644 --- a/tests/rowCumMinMaxs.R +++ b/tests/rowCumMinMaxs.R @@ -26,7 +26,6 @@ rowCummaxs_R <- function(x, ..., useNames = NA) { dimnames <- dimnames(x) if (isTRUE(useNames) && !is.null(dimnames)) dimnames(y) <- dimnames - storage.mode(y) <- mode y } @@ -34,7 +33,7 @@ rowCummaxs_R <- function(x, ..., useNames = NA) { # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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 = "") @@ -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 @@ -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") @@ -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)) @@ -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))