Sebastian Martin Krantz
2021-Jun-29 14:21 UTC
[Rd] ALTREP ALTINTEGER_SUM/MIN/MAX Return Value and Behavior
Hello together, I'm working on some custom (grouped, weighted) sum, min and max functions and I want them to support the special case of plain integer sequences using ALTREP. I thereby encountered some behavior I cannot explain to myself. The head of my fsum C function looks like this (g is optional grouping vector, w is optional weights vector): SEXP fsumC(SEXP x, SEXP Rng, SEXP g, SEXP w, SEXP Rnarm) { int l = length(x), tx = TYPEOF(x), ng = asInteger(Rng), narm = asLogical(Rnarm), nprotect = 1, nwl = isNull(w); if(ALTREP(x) && ng == 0 && nwl) { switch(tx) { case INTSXP: return ALTINTEGER_SUM(x, (Rboolean)narm); case LGLSXP: return ALTLOGICAL_SUM(x, (Rboolean)narm); case REALSXP: return ALTLOGICAL_SUM(x, (Rboolean)narm); default: error("ALTREP object must be integer or real typed"); } } // ... } when I let x <- 1:1e8, fsum(x) works fine and returns the correct value. If I now make this a matrix dim(x) <- c(1e2, 1e6) and subsequently turn this into a vector again, dim(x) <- NULL, fsum(x) gives NULL and a warning message 'converting NULL pointer to R NULL'. For functions fmin and fmax (similarly defined using ALTINTEGER_MIN/MAX), I get this error right away e.g. fmin(1:1e8) gives NULL and warning 'converting NULL pointer to R NULL'. So what is going on here? What do these functions return? And how do I make this a robust implementation? Best regards, Sebastian Krantz [[alternative HTML version deleted]]
iuke-tier@ey m@iii@g oii uiow@@edu
2021-Jun-29 15:08 UTC
[Rd] [External] ALTREP ALTINTEGER_SUM/MIN/MAX Return Value and Behavior
ALTINTEGER_SUM and friends are _not_ intended for use in package code. Once we get some time to clean up headers they will no longer be visible to packages. Best, luke On Tue, 29 Jun 2021, Sebastian Martin Krantz wrote:> Hello together, I'm working on some custom (grouped, weighted) sum, min and > max functions and I want them to support the special case of plain integer > sequences using ALTREP. I thereby encountered some behavior I cannot > explain to myself. The head of my fsum C function looks like this (g is > optional grouping vector, w is optional weights vector): > > SEXP fsumC(SEXP x, SEXP Rng, SEXP g, SEXP w, SEXP Rnarm) { > int l = length(x), tx = TYPEOF(x), ng = asInteger(Rng), > narm = asLogical(Rnarm), nprotect = 1, nwl = isNull(w); > if(ALTREP(x) && ng == 0 && nwl) { > switch(tx) { > case INTSXP: return ALTINTEGER_SUM(x, (Rboolean)narm); > case LGLSXP: return ALTLOGICAL_SUM(x, (Rboolean)narm); > case REALSXP: return ALTLOGICAL_SUM(x, (Rboolean)narm); > default: error("ALTREP object must be integer or real typed"); > } > } > // ... > } > > when I let x <- 1:1e8, fsum(x) works fine and returns the correct value. If > I now make this a matrix dim(x) <- c(1e2, 1e6) and subsequently turn this > into a vector again, dim(x) <- NULL, fsum(x) gives NULL and a warning > message 'converting NULL pointer to R NULL'. For functions fmin and fmax > (similarly defined using ALTINTEGER_MIN/MAX), I get this error right away > e.g. fmin(1:1e8) gives NULL and warning 'converting NULL pointer to R > NULL'. So what is going on here? What do these functions return? And how do > I make this a robust implementation? > > Best regards, > > Sebastian Krantz > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Luke Tierney Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke-tierney at uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
Bill Dunlap
2021-Jun-29 16:43 UTC
[Rd] ALTREP ALTINTEGER_SUM/MIN/MAX Return Value and Behavior
Adding the dimensions attribute takes away the altrep-ness. Removing dimensions does not make it altrep. E.g.,> a <- 1:10 > am <- a ; dim(am) <- c(2L,5L) > amn <- am ; dim(amn) <- NULL > .Call("is_altrep", a)[1] TRUE> .Call("is_altrep", am)[1] FALSE> .Call("is_altrep", amn)[1] FALSE where is_altrep() is defined by the following C code: #include <R.h> #include <Rinternals.h> SEXP is_altrep(SEXP x) { return Rf_ScalarLogical(ALTREP(x)); } -Bill On Tue, Jun 29, 2021 at 8:03 AM Sebastian Martin Krantz < sebastian.krantz at graduateinstitute.ch> wrote:> Hello together, I'm working on some custom (grouped, weighted) sum, min and > max functions and I want them to support the special case of plain integer > sequences using ALTREP. I thereby encountered some behavior I cannot > explain to myself. The head of my fsum C function looks like this (g is > optional grouping vector, w is optional weights vector): > > SEXP fsumC(SEXP x, SEXP Rng, SEXP g, SEXP w, SEXP Rnarm) { > int l = length(x), tx = TYPEOF(x), ng = asInteger(Rng), > narm = asLogical(Rnarm), nprotect = 1, nwl = isNull(w); > if(ALTREP(x) && ng == 0 && nwl) { > switch(tx) { > case INTSXP: return ALTINTEGER_SUM(x, (Rboolean)narm); > case LGLSXP: return ALTLOGICAL_SUM(x, (Rboolean)narm); > case REALSXP: return ALTLOGICAL_SUM(x, (Rboolean)narm); > default: error("ALTREP object must be integer or real typed"); > } > } > // ... > } > > when I let x <- 1:1e8, fsum(x) works fine and returns the correct value. If > I now make this a matrix dim(x) <- c(1e2, 1e6) and subsequently turn this > into a vector again, dim(x) <- NULL, fsum(x) gives NULL and a warning > message 'converting NULL pointer to R NULL'. For functions fmin and fmax > (similarly defined using ALTINTEGER_MIN/MAX), I get this error right away > e.g. fmin(1:1e8) gives NULL and warning 'converting NULL pointer to R > NULL'. So what is going on here? What do these functions return? And how do > I make this a robust implementation? > > Best regards, > > Sebastian Krantz > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]