From fd8c8d71563c5fe6a29bb5dacbf3926113840c92 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Thu, 11 Jul 2019 02:59:33 +0800 Subject: [PATCH 1/3] adds support for complex vectors to shift, part of #3690 --- src/shift.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/shift.c b/src/shift.c index 10e7cbeb3c..46f2a4f728 100644 --- a/src/shift.c +++ b/src/shift.c @@ -1,5 +1,6 @@ #include "data.table.h" #include +#include SEXP shift(SEXP obj, SEXP k, SEXP fill, SEXP type) { @@ -85,6 +86,25 @@ SEXP shift(SEXP obj, SEXP k, SEXP fill, SEXP type) { copyMostAttrib(elem, tmp); } break; + + case CPLXSXP : + thisfill = PROTECT(coerceVector(fill, CPLXSXP)); protecti++; + double complex cfill = COMPLEX(thisfill)[0].r + COMPLEX(thisfill)[0].i; + for (int j=0; j= 0) || (stype == LEAD && kd[j] < 0)) { + if (tailk > 0) memmove(ctmp+thisk, COMPLEX(elem), tailk*size); + for (int m=0; m 0) memmove(ctmp, COMPLEX(elem)+thisk, tailk*size); + for (int m=tailk; m Date: Thu, 11 Jul 2019 09:02:48 +0800 Subject: [PATCH 2/3] fix small bug and add tests, NEWS item --- NEWS.md | 2 ++ inst/tests/tests.Rraw | 9 ++++++++- src/shift.c | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 10e3d5d5ea..3cf4ab416f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -128,6 +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). #### BUG FIXES diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 1c720d4de9..8316855038 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -13325,7 +13325,7 @@ test(1963.07, shift(DT, -1:1), c(NA, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L))) ## some coverage tests for good measure test(1963.08, shift(DT$x, type = 'some_other_type'), error='should be one of.*lag.*lead') -test(1963.09, shift(c(1+3i, 2-1i)), error = 'Unsupported type') +test(1963.09, shift(as.raw(0:1)), error = 'Unsupported type') test(1963.10, shift(DT, -1:1, type="shift", give.names = TRUE), # new type="shift" #3223 ans <- list(`x_shift_-1` = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, NA), x_shift_0 = 1:10, @@ -15200,6 +15200,13 @@ d2 = data.table(r = 1:5, s = seq(0L, 20L, 5L)) test(2062.1, d1[d2, on = .(a <= s, b >= s), j = .SD], ans<-data.table(a=INT(0,5,10,10,15,20), b=INT(0,5,10,10,15,20))) test(2062.2, d1[d2, on = .(a <= s, b >= s)][, .(a, b)], ans) +# Shift complex values, part of #3690 +z = c(1:3) + c(3:1)*1i +test(2063.1, shift(z), c(NA, z[1:2])) +test(2063.2, shift(z, type = 'lead'), c(z[2:3], NA)) +test(2063.3, shift(z, fill = 1i), c(1i, z[1:2])) +test(2063.4, shift(list(z, 1:3)), list(c(NA, z[1:2]), c(NA, 1:2))) + ################################### # Add new tests above this line # diff --git a/src/shift.c b/src/shift.c index 46f2a4f728..9044114f2a 100644 --- a/src/shift.c +++ b/src/shift.c @@ -89,7 +89,7 @@ SEXP shift(SEXP obj, SEXP k, SEXP fill, SEXP type) { case CPLXSXP : thisfill = PROTECT(coerceVector(fill, CPLXSXP)); protecti++; - double complex cfill = COMPLEX(thisfill)[0].r + COMPLEX(thisfill)[0].i; + double complex cfill = COMPLEX(thisfill)[0].r + COMPLEX(thisfill)[0].i*I; for (int j=0; j Date: Sun, 14 Jul 2019 04:24:00 +0800 Subject: [PATCH 3/3] move to Rcomplex API --- src/shift.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/shift.c b/src/shift.c index 9044114f2a..75eff7b2a2 100644 --- a/src/shift.c +++ b/src/shift.c @@ -1,6 +1,5 @@ #include "data.table.h" #include -#include SEXP shift(SEXP obj, SEXP k, SEXP fill, SEXP type) { @@ -86,14 +85,14 @@ SEXP shift(SEXP obj, SEXP k, SEXP fill, SEXP type) { copyMostAttrib(elem, tmp); } break; - + case CPLXSXP : thisfill = PROTECT(coerceVector(fill, CPLXSXP)); protecti++; - double complex cfill = COMPLEX(thisfill)[0].r + COMPLEX(thisfill)[0].i*I; + Rcomplex cfill = COMPLEX(thisfill)[0]; for (int j=0; j= 0) || (stype == LEAD && kd[j] < 0)) { if (tailk > 0) memmove(ctmp+thisk, COMPLEX(elem), tailk*size);