[Rcpp-devel] 'Nested' Rcpp functions using inline

Rubem Kaipper Ceratti rubem_ceratti at yahoo.com.br
Wed Oct 10 17:35:15 CEST 2012


I see. It's just that, for the time being, inline is pretty handy for a novice like me translating R code to C++. 


________________________________
 De: Douglas Bates <bates at stat.wisc.edu>
Para: Jeffrey Pollock <jeffpollock9 at gmail.com> 
Cc: Rubem Kaipper Ceratti <rubem_ceratti at yahoo.com.br>; "rcpp-devel at lists.r-forge.r-project.org" <rcpp-devel at r-forge.wu-wien.ac.at> 
Enviadas: Quarta-feira, 10 de Outubro de 2012 12:10
Assunto: Re: [Rcpp-devel] 'Nested' Rcpp functions using inline
 
Remember that the inline package is convenient for interactive
exploration during code development but usually the ultimate goal when
working with Rcpp is to produce a package including both R and C++
code.  When you start doing complicated things in the C++ code it is
probably time to look at creating a package.

On Wed, Oct 10, 2012 at 9:59 AM, Jeffrey Pollock <jeffpollock9 at gmail.com> wrote:
> I think you need to use the `includes` argument of cxxfunction to include a
> pure c++ function ie;
>
> library(inline)
> library(Rcpp)
>
> inc <-'
>         int fun1(double x, double y) {
>             return (exp(x) - y) > 0 ? 1 : 0;
>         }
>         '
>
> body <-'
>
>         NumericVector u   = as<NumericVector>(u_r);
>         double        mu  = as<double>(mu_r),
>         v   = log(mu);
>         int           n   = u.size(),
>         res = 0;
>
>         for(int i=0; i<n; i++){
>             res += fun1(u(i), v);
>         }
>
>         return wrap(res);
>         '
>
> fun.test <- cxxfunction(signature(u_r = 'numeric', mu_r = 'numeric'), body,
> "Rcpp", inc)
>
>> identical(fun.test(1:4, 100), sum(exp(1:4) > log(100)))
> [1] TRUE
>
> On Wed, Oct 10, 2012 at 3:39 PM, Rubem Kaipper Ceratti
> <rubem_ceratti at yahoo.com.br> wrote:
>>
>> Hi,
>>
>> I'm currently using Rcpp and inline to speed up some R code I wrote, but
>> being a C++/Rcpp newbie I'm running into some difficulties. More
>> specifically, I'm having problems calling an Rcpp function inside another
>> one. As a toy example consider the code:
>> ##
>> library(Rcpp)
>> library(inline)
>>
>> code2.0 <-'
>>   NumericVector u   = as<NumericVector>(u_r);
>>   double        mu  = as<double>(mu_r),
>>                 v   = log(mu);
>>   int           n   = u.size(),
>>                 res = 0;
>>
>>   for(int i=0; i<n; i++){
>>     res += (exp(u(i))-v) > 0 ? 1 : 0;
>>   }
>>
>>   return wrap(res);
>> '
>> sig2 <- signature(u_r = 'numeric', mu_r = 'numeric')
>> fun.test <- cxxfunction(sig2, code2.0, plugin = "Rcpp")
>>
>> fun.test(1:4, 100)
>> sum(exp(1:4) > log(100))  # ok!
>> ##
>>
>> It works just fine. Now, I'd like to break it down into two functions like
>> so:
>> ##
>> code1 <-'
>>   double x = as<double>(x_r),
>>          y = as<double>(y_r),
>>          ans;
>>
>>   ans = (exp(x)-y) > 0 ? 1 : 0;
>>
>>   return wrap(ans);
>> '
>>
>> code2.1 <-'
>>   NumericVector u   = as<NumericVector>(u_r);
>>   double        mu  = as<double>(mu_r),
>>                 v   = log(mu);
>>   int           n   = u.size(),
>>                 res = 0;
>>
>>   for(int i=0; i<n; i++){
>>     res += as<int>(fun1(u(i),v));
>>   }
>>
>>   return wrap(res);
>> '
>>
>> sig1 <- signature(x_r = 'numeric', y_r = 'numeric')
>> sig2 <- signature(u_r = 'numeric', mu_r = 'numeric')
>> fun.test <- cxxfunction(list(fun1 = sig1, fun2 = sig2),
>>                         list(code1, code2.1), plugin = "Rcpp")
>> ##
>>
>> But I get the error message:
>> # file5686a0c71e8.cpp: In function 'SEXPREC* fun2(SEXP, SEXP)':
>> #   file5686a0c71e8.cpp:52:33: error: invalid cast from type 'double' to
>> type 'SEXP'
>> # file5686a0c71e8.cpp:55:20: error: invalid cast from type
>> 'Rcpp::traits::storage_type<14>::type {aka double}' to type 'SEXP'
>>
>> I then tried to change the types of 'u(i)' and 'v' to SEXP, but I get the
>> same error:
>> ##
>> code2.2 <-'
>>   NumericVector u   = as<NumericVector>(u_r);
>>   double        mu  = as<double>(mu_r),
>>                 v   = log(mu);
>>   int           n   = u.size(),
>>                 res = 0;
>>   SEXP          us, vs = (SEXP) v;
>>
>>   for(int i=0; i<n; i++){
>>     us = (SEXP) u(i);
>>     res += as<int>(fun1(us,vs));
>>   }
>>
>>   return wrap(res);
>> '
>>
>> fun.test <- cxxfunction(list(fun1 = sig1, fun2 = sig2),
>>                         list(code1, code2.2), plugin = "Rcpp")
>> ##
>> # file5686a0c71e8.cpp: In function 'SEXPREC* fun2(SEXP, SEXP)':
>> #   file5686a0c71e8.cpp:52:33: error: invalid cast from type 'double' to
>> type 'SEXP'
>> # file5686a0c71e8.cpp:55:20: error: invalid cast from type
>> 'Rcpp::traits::storage_type<14>::type {aka double}' to type 'SEXP'
>>
>> Is there any way to make it work?
>>
>> Thanks,
>> Rubem
>>
>>
>>
>> _______________________________________________
>> 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
>
>
>
> _______________________________________________
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121010/fab72bf9/attachment.html>


More information about the Rcpp-devel mailing list