Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Serguei Sokol
Le 09/12/2018 à 09:35, Mark Leeds a écrit : Hi All: I wrote below and it works but I have a strong feeling there's a better way to do it. If performance is an issue, you can save few percents of cpu time by using std::copy() instead of explicit for loop. Yet, for this operation R's c() remains

Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Jan van der Laan
For performance memcpy is probably fastest. This gives the same performance a c(). // [[Rcpp::export]] NumericVector mybar3(NumericVector x, double firstelem) { NumericVector result(x.size() + 1); result[0] = firstelem; std::memcpy(result.begin()+1, x.begin(), x.size()*sizeof(double));

Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Jan van der Laan
Small addendum: A large part of the performance gain in my example comes from using NumericVector instead of std::vector. Which avoids a conversion. An example using std::copy with Numeric vector runs in the same time as the version using memcpy. Jan On 10-12-18 12:28, Jan van der Laan wrot

Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Dirk Eddelbuettel
On 10 December 2018 at 13:04, Jan van der Laan wrote: | Small addendum: A large part of the performance gain in my example comes | from using NumericVector instead of std::vector. Which avoids a | conversion. An example using std::copy with Numeric vector runs in the | same time as the version

Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Serguei Sokol
Le 10/12/2018 à 13:04, Jan van der Laan a écrit : Small addendum: A large part of the performance gain in my example comes from using NumericVector instead of std::vector. Which avoids a conversion. An example using std::copy with Numeric vector runs in the same time as the version using memcpy

Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Mark Leeds
Thanks to all for all the great repliies. I have to go through them but it sounds like memcopy is the best so I'll probably ending use that. Amazing to be part of this list and receive all of this wisdom. Oh, as I said, the documentation on Rcpp is incredible but is there anything discussing memo

Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Serguei Sokol
Le 10/12/2018 à 16:48, Mark Leeds a écrit : ... Oh, as I said, the documentation on Rcpp is incredible but is there anything discussing memory because I'm pretty lost on that. Thanks again. Are you talking about this list archives? http://lists.r-forge.r-project.org/pipermail/rcpp-devel/ Sergu

[Rcpp-devel] Create scalar values in Rcpp

2018-12-10 Thread Wolf Vollprecht
Hi, What's the canonical way of creating a scalar SEXP in Rcpp? E.g. I would like to store a single numeric / integer / complex ... value. I can create an array with 1 element, but I couldn't find a way to do it for a scalar (and I figured out later that a scalar is *not* the same as a 1element ar

Re: [Rcpp-devel] Create scalar values in Rcpp

2018-12-10 Thread Avraham Adler
There are no scalars in R; there are vectors of length 1. https://stat.ethz.ch/pipermail/r-help/2003-April/032010.html Avi On Mon, Dec 10, 2018 at 11:14 AM Wolf Vollprecht wrote: > Hi, > > What's the canonical way of creating a scalar SEXP in Rcpp? E.g. I would > like to store a single numeric

Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Jeff Newmiller
The archives probably weren't what Mark was hoping for, though they are good medicine. Reading the R documentation [1] is probably also good medicine that Mark is hoping (in vain) to avoid. And then there is the topic of conventional C++ memory handling, which is relevant but for which there are

Re: [Rcpp-devel] Create scalar values in Rcpp

2018-12-10 Thread Dirk Eddelbuettel
On 10 December 2018 at 11:24, Avraham Adler wrote: | There are no scalars in R; there are vectors of length 1. Yep. An R feature, and gotcha if you come from somwhere else. See R> a <- 1 R> is.vector(a) [1] TRUE R> And while we can do this R> Rcpp::cppFunction("double wolf(double x)

Re: [Rcpp-devel] Create scalar values in Rcpp

2018-12-10 Thread Jeff Newmiller
Your use of the term "array" in this context suggests you still don't have a firm grasp of R data types. R arrays are vectors with dim attributes. What you want is to create an R vector with 1 element, as shown in the quick reference. [1] [1] http://dirk.eddelbuettel.com/code/rcpp/Rcpp-quickref

Re: [Rcpp-devel] Create scalar values in Rcpp

2018-12-10 Thread Dirk Eddelbuettel
Jeff, On 10 December 2018 at 08:43, Jeff Newmiller wrote: | Your use of the term "array" in this context suggests you still don't have a firm grasp of R data types. R arrays are vectors with dim attributes. What you want is to create an R vector with 1 element, as shown in the quick reference.

Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Mark Leeds
Hi Serguei: Thanks for the link but I was talking about how one would understand that my original Rcpp code was about as memory inefficient as one could possibly get. I knew it was ugly but not inefficient. If there are specific threads that discuss the topic, those are fine also. Mark On Mon,

Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Mark Leeds
Thanks Jeff. I'll check those out. I recently found a gist of Gabor that explains the relation between Rcpp and the concept of pointers in C. https://gist.github.com/ggrothendieck On Mon, Dec 10, 2018 at 11:29 AM Jeff Newmiller wrote: > The archives probably weren't what Mark was hoping for, t

Re: [Rcpp-devel] Create scalar values in Rcpp

2018-12-10 Thread Jeff Newmiller
I did not intend to shoot anyone... but my own learning curve led me to skip over the clear descriptions in the "Introduction to R" document because I thought I already understood the terminology. True, it happened a long time ago, but it took me an embarrassingly long time to grok this because