[Rcpp-devel] Eigen-like pointer mappings in Rcpp?
Yixuan Qiu
yixuan.qiu at cos.name
Tue Feb 17 21:57:13 CET 2015
Hello Rodney,
If all you need is to pass a pointer to the fit() function (assuming fit()
does not modify x) and get the result a, you do not need to make a copy.
Simply wrap _x by a NumericVector and call the data() method.
Rcpp::NumericVector x(_x);
double a = fit(x.data(), n);
You can even directly use the R API without creating an Rcpp object.
double a = fit(REAL(_x), n);
Best,
Yixuan
2015-02-17 14:53 GMT-05:00 Sparapani, Rodney <rsparapa at mcw.edu>:
> Hi Gang:
>
> I am working with some C++ code that uses pointers. I know I could
> change the code. But, I was wondering if there is an easy way to
> map the double pointer into the R data via Rcpp. I have been using
> RcppEigen to do that. For example...
>
> RcppExport SEXP myfunc(SEXP _n, SEXP _x) {
> const int n=Rcpp::as<int>(_n);
>
> double *x=(double *)malloc(n*sizeof(double));
>
> Eigen::Map<Eigen::VectorXd> map_x(x, n);
>
> map_x=Rcpp::as< Eigen::Map<Eigen::VectorXd> >(_x);
>
> double a=fit(x, n);
>
> ...
>
> It seems to me there should be an easier way like ...
>
> RcppExport SEXP myfunc(SEXP _n, SEXP _x) {
> const int n=Rcpp::as<int>(_n);
>
> double *x=Rcpp::as<double*>(_x); // or a const-correct version
>
> double a=fit(x, n);
>
> ...
>
> Thanks,
>
> Rodney
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
--
Yixuan Qiu <yixuan.qiu at cos.name>
Department of Statistics,
Purdue University
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150217/0833d908/attachment.html>
More information about the Rcpp-devel
mailing list