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

Rubem Kaipper Ceratti rubem_ceratti at yahoo.com.br
Wed Oct 10 16:39:53 CEST 2012


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121010/2a9891ea/attachment.html>


More information about the Rcpp-devel mailing list