[Rcpp-devel] What's the most efficient way to convert a NumericVector to an array of doubles
Hello, I need to interface some R code with a numerical library containing functions taking as input double* What is the most efficient way to create an array of doubles from a NumericVector. I was thinking of something like: // input is of type Rcpp::NumericVector and passed from R std::vector vec; std::copy(input.begin(), input.end(), std::back_inserter(vec)); double* values = &vec[0]; // now call function taking a double* Regards, Anwar___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] What's the most efficient way to convert a NumericVector to an array of doubles
Hello, just use: double* values = input.begin() ; A NumericVector uses R allocated memory, and R allocates an array of doubles to store a numeric vector. You then need to remember you dońt own the pointer. So make sure your code does not eg delete[] it. The code you've used was very inefficient and will make many unecessary copies of the data, each time the std::vctor wants to grow. If you want a std:vector you can do something like this: std::vector vec( input.begin(), input.end() ) ; Or: std::vector vec = as< std::vector >(input) ; Romain Le 29 juin 2013 à 12:08, Anwar Ludin a écrit : > Hello, > > I need to interface some R code with a numerical library containing functions > taking as input double* > > What is the most efficient way to create an array of doubles from a > NumericVector. > > I was thinking of something like: > > > > > > > // input is of type Rcpp::NumericVector and passed from R > std::vector vec; > std::copy(input.begin(), input.end(), std::back_inserter(vec)); > > double* values = &vec[0]; > > // now call function taking a double* > > > Regards, > > Anwar > ___ > Rcpp-devel mailing list > Rcpp-devel@lists.r-forge.r-project.org > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] What's the most efficient way to convert a NumericVector to an array of doubles
Romain, Thanks a lot for the clarifications! Anwar On Jun 29, 2013, at 12:35 PM, Romain Francois wrote: > Hello, > > just use: > > double* values = input.begin() ; > > A NumericVector uses R allocated memory, and R allocates an array of doubles > to store a numeric vector. > > You then need to remember you dońt own the pointer. So make sure your code > does not eg delete[] it. > > The code you've used was very inefficient and will make many unecessary > copies of the data, each time the std::vctor wants to grow. > > If you want a std:vector you can do something like this: > > std::vector vec( input.begin(), input.end() ) ; > > Or: > > std::vector vec = as< std::vector >(input) ; > > Romain > > Le 29 juin 2013 à 12:08, Anwar Ludin a écrit : > >> Hello, >> >> I need to interface some R code with a numerical library containing >> functions taking as input double* >> >> What is the most efficient way to create an array of doubles from a >> NumericVector. >> >> I was thinking of something like: >> >> >> >> >> >> >> // input is of type Rcpp::NumericVector and passed from R >> std::vector vec; >> std::copy(input.begin(), input.end(), std::back_inserter(vec)); >> >> double* values = &vec[0]; >> >> // now call function taking a double* >> >> >> Regards, >> >> Anwar >> ___ >> Rcpp-devel mailing list >> Rcpp-devel@lists.r-forge.r-project.org >> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel