I use Rcpp to wrap a written algorithm (not by me) in C-like C ++ (without STL, without boost, I canβt do anything, as far as I can tell). You can see the implemented algorithm here (I wrap kmeans_w_03 ). Therefore, I pass the numeric vector from R, which then needs to be converted to a double array.
I am currently looping element by element and filling out one of the "tother", for example:
SEXP testfn(SEXP weightvec, SEXP cluster_num_k){ Rcpp::NumericVector weightR(weightvec) ; int point_num = weightR.size(); double weight[point_num] ; for(int i = 0; i < point_num; ++i) { weight[i] = weightR[i]; } }
But with singleton numerical vectors, I can take advantage of the superior customization functionality as Rcpp:
int cluster_num = Rcpp::as<int>(cluster_num_k);
Trying something similar for lengths> 1 of numerical vectors, however, results in a crash or error depending on the exact syntax variant:
double weight[point_num] = Rcpp::as<double>(weightvec);
I'm not necessarily against the loop, but I'm a complete neophyte and suspect the best way. I read the Rcpp introduction, the wiki tutorial on wiki and RcppExamples, and have not yet found anything to resolve this issue, but that does not mean that I did not just skip it. My reading of the Docsgen Rcpp docs is that as can use the STL vector but not an array (but it is very difficult for me to read these documents, so I suspect I'm wrong there). If so, I think I could pass the vector from there to the array too ....
So my question is: is there a better (less lines of code, more expressive code, and perhaps even able to avoid memory usage) way to convert NumericVector to double[] ?
c ++ r rcpp
Ari B. Friedman
source share