From 633bc1b990469bc7cec22826f2ba2d5127497b6d Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 11 Jul 2019 17:09:39 +0800 Subject: [PATCH 1/3] Added support for complex vectors to coalesce, part of #3690 --- NEWS.md | 2 +- inst/tests/tests.Rraw | 12 ++++++++++++ src/coalesce.c | 25 +++++++++++++++++++++++++ src/data.table.h | 6 ++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 691647cba1..0abc71ea6e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -112,7 +112,7 @@ # 0.357 0.763 0.292 # now ``` -18. New function `coalesce(...)` has been written in C, and is multithreaded for numeric and factor types. It replaces missing values according to a prioritized list of candidates (as per SQL COALESCE, `dplyr::coalesce`, and `hutils::coalesce`), [#3424](https://github.com/Rdatatable/data.table/issues/3424). It accepts any number of vectors in several forms. For example, given three vectors `x`, `y`, and `z`, where each `NA` in `x` is to be replaced by the corresponding value in `y` if that is non-NA, else the corresponding value in `z`, the following equivalent forms are all accepted: `coalesce(x,y,z)`, `coalesce(x,list(y,z))`, and `coalesce(list(x,y,z))`. +18. New function `coalesce(...)` has been written in C, and is multithreaded for numeric (including complex, part of [#3690](https://github.com/Rdatatable/data.table/issues/3690)) and factor types. It replaces missing values according to a prioritized list of candidates (as per SQL COALESCE, `dplyr::coalesce`, and `hutils::coalesce`), [#3424](https://github.com/Rdatatable/data.table/issues/3424). It accepts any number of vectors in several forms. For example, given three vectors `x`, `y`, and `z`, where each `NA` in `x` is to be replaced by the corresponding value in `y` if that is non-NA, else the corresponding value in `z`, the following equivalent forms are all accepted: `coalesce(x,y,z)`, `coalesce(x,list(y,z))`, and `coalesce(list(x,y,z))`. ```R # default 4 threads on a laptop with 16GB RAM and 8 logical CPU diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 0c498e9f76..f4bdbedf64 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -15196,6 +15196,18 @@ setcoalesce(xx, list()) test(2060.503, xx_addr, address(xx)) test(2060.504, xx, x) test(2060.505, address(setcoalesce(xx)), xx_addr) +# complex support for coalesce +z1 = c(1i, NA, 1-1i, NA, 0+3i, NA) +z2 = c(NA, 4-2i, 0+0i, NA, NA, NA) +z3 = c(2, NA, 3+6i, 5-1i, NA, NA) +na_idx = c(2L, 4L, 6L) +test(2060.600, coalesce(z1, 0+0i), `[<-`(z1, na_idx, 0+0i)) +test(2060.601, coalesce(z1, z2), `[<-`(z1, na_idx, c(4-2i, NA, NA))) +test(2060.602, coalesce(z1, z2, z3), `[<-`(z1, na_idx, c(4-2i, 5-1i, NA))) +z_addr = address(z1) +setcoalesce(z1, z2, z3) +test(2060.603, address(z1), z_addr) +test(2060.604, z1, `[<-`(z1, na_idx, c(4-2i, 5-1i, NA))) # #3650 -- ensure max nrow check on CJ is applied after unique l = replicate(ceiling(log10(.Machine$integer.max)), rep(1L, 10L), simplify = FALSE) diff --git a/src/coalesce.c b/src/coalesce.c index d1cb2bc0aa..249bc4f1f7 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -1,4 +1,5 @@ #include "data.table.h" +#include SEXP coalesce(SEXP x, SEXP inplaceArg) { if (TYPEOF(x)!=VECSXP) @@ -115,6 +116,30 @@ SEXP coalesce(SEXP x, SEXP inplaceArg) { } } } break; + case CPLXSXP: { + double complex *xP = (double complex *)COMPLEX(first); + double complex finalVal=NA_REAL + NA_REAL*I; + int k=0; + for (int j=0; j Date: Sun, 14 Jul 2019 04:28:29 +0800 Subject: [PATCH 2/3] move to Rcomplex API --- src/coalesce.c | 18 +++++++++--------- src/data.table.h | 6 ------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/coalesce.c b/src/coalesce.c index 249bc4f1f7..c5223d3384 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -117,27 +117,27 @@ SEXP coalesce(SEXP x, SEXP inplaceArg) { } } break; case CPLXSXP: { - double complex *xP = (double complex *)COMPLEX(first); - double complex finalVal=NA_REAL + NA_REAL*I; + Rcomplex *xP = COMPLEX(first); + Rcomplex finalVal={NA_REAL, NA_REAL}; int k=0; for (int j=0; j Date: Wed, 17 Jul 2019 14:17:22 -0700 Subject: [PATCH 3/3] ISNAN not ==NA_REAL --- NEWS.md | 6 +++--- src/coalesce.c | 15 ++++++--------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/NEWS.md b/NEWS.md index 7a999c8b1f..30cd4b84cf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -112,7 +112,7 @@ # 0.357 0.763 0.292 # now ``` -18. New function `coalesce(...)` has been written in C, and is multithreaded for numeric (including complex, part of [#3690](https://github.com/Rdatatable/data.table/issues/3690)) and factor types. It replaces missing values according to a prioritized list of candidates (as per SQL COALESCE, `dplyr::coalesce`, and `hutils::coalesce`), [#3424](https://github.com/Rdatatable/data.table/issues/3424). It accepts any number of vectors in several forms. For example, given three vectors `x`, `y`, and `z`, where each `NA` in `x` is to be replaced by the corresponding value in `y` if that is non-NA, else the corresponding value in `z`, the following equivalent forms are all accepted: `coalesce(x,y,z)`, `coalesce(x,list(y,z))`, and `coalesce(list(x,y,z))`. +18. New function `coalesce(...)` has been written in C, and is multithreaded for numeric, complex, and factor types. It replaces missing values according to a prioritized list of candidates (as per SQL COALESCE, `dplyr::coalesce`, and `hutils::coalesce`), [#3424](https://github.com/Rdatatable/data.table/issues/3424). It accepts any number of vectors in several forms. For example, given three vectors `x`, `y`, and `z`, where each `NA` in `x` is to be replaced by the corresponding value in `y` if that is non-NA, else the corresponding value in `z`, the following equivalent forms are all accepted: `coalesce(x,y,z)`, `coalesce(x,list(y,z))`, and `coalesce(list(x,y,z))`. ```R # default 4 threads on a laptop with 16GB RAM and 8 logical CPU @@ -128,8 +128,8 @@ identical(y1,y2) && identical(y1,y3) # TRUE ``` - -19. Extended support of `shift` to complex vectors, part of [#3690](https://github.com/Rdatatable/data.table/issues/3690). + +19. `shift` now supports complex vectors, part of [#3690](https://github.com/Rdatatable/data.table/issues/3690). #### BUG FIXES diff --git a/src/coalesce.c b/src/coalesce.c index 37255d9eef..81e8156341 100644 --- a/src/coalesce.c +++ b/src/coalesce.c @@ -1,5 +1,4 @@ #include "data.table.h" -#include SEXP coalesce(SEXP x, SEXP inplaceArg) { if (TYPEOF(x)!=VECSXP) @@ -120,27 +119,25 @@ SEXP coalesce(SEXP x, SEXP inplaceArg) { } } break; case CPLXSXP: { - Rcomplex *xP = COMPLEX(first); - Rcomplex finalVal={NA_REAL, NA_REAL}; + Rcomplex *xP = COMPLEX(first), finalVal=NA_CPLX; int k=0; for (int j=0; j