I think you need to use the `includes` argument of cxxfunction to include a pure c++ function ie;<br><br>library(inline)<br>library(Rcpp)<br><br>inc <-'<br> int fun1(double x, double y) {<br> return (exp(x) - y) > 0 ? 1 : 0;<br>
}<br> '<br><br>body <-'<br> NumericVector u = as<NumericVector>(u_r);<br> double mu = as<double>(mu_r), <br> v = log(mu);<br> int n = u.size(),<br>
res = 0;<br> <br> for(int i=0; i<n; i++){<br> res += fun1(u(i), v);<br> }<br> <br> return wrap(res);<br> '<br><br>fun.test <- cxxfunction(signature(u_r = 'numeric', mu_r = 'numeric'), body, "Rcpp", inc)<br>
<br>> identical(fun.test(1:4, 100), sum(exp(1:4) > log(100)))<br>[1] TRUE<br><br><div class="gmail_quote">On Wed, Oct 10, 2012 at 3:39 PM, Rubem Kaipper Ceratti <span dir="ltr"><<a href="mailto:rubem_ceratti@yahoo.com.br" target="_blank">rubem_ceratti@yahoo.com.br</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div style="font-size:10pt;font-family:arial,helvetica,sans-serif"><div style="font-family:arial,helvetica,sans-serif;font-size:10pt">
Hi,</div><div style="font-family:arial,helvetica,sans-serif;font-size:10pt"><br></div><div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif">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:</div>
<div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif">## </div><div style="background-color:transparent"><div style="background-color:transparent"><font face="arial, helvetica,
sans-serif">library(Rcpp)</font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif">library(inline)</font></div><div style="background-color:transparent"><br></div><div style="background-color:transparent">
<font face="arial, helvetica, sans-serif">code2.0 <-'</font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif"> NumericVector u = as<NumericVector>(u_r);</font></div>
<div style="background-color:transparent"><font face="arial, helvetica, sans-serif"> double mu = as<double>(mu_r), </font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif"> v = log(mu);</font></div>
<div style="background-color:transparent"><font face="arial, helvetica,
sans-serif"> int n = u.size(),</font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif"> res = 0;</font></div><div style="background-color:transparent">
<font face="arial, helvetica, sans-serif"><br></font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif"> for(int i=0; i<n; i++){</font></div><div style="background-color:transparent">
<font face="arial, helvetica, sans-serif"> res += (exp(u(i))-v) > 0 ? 1 : 0;</font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif"> }</font></div><div style="background-color:transparent">
<font face="arial, helvetica, sans-serif"><br></font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif"> return wrap(res);</font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif">'</font></div>
<div style="background-color:transparent"><font face="arial, helvetica, sans-serif">sig2 <- signature(u_r = 'numeric', mu_r = 'numeric')</font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif">fun.test <- cxxfunction(sig2, code2.0, plugin = "Rcpp")</font></div>
<div style="background-color:transparent"><font face="arial, helvetica, sans-serif"><br></font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif">fun.test(1:4, 100) </font></div><div style="background-color:transparent">
<font face="arial, helvetica, sans-serif">sum(exp(1:4) > log(100)) # ok!</font></div><div style="font-style:normal;background-color:transparent;font-family:arial,helvetica,sans-serif"><font>##</font></div><div style="font-size:13px;font-style:normal;background-color:transparent;font-family:arial,helvetica,sans-serif">
<font><br></font></div><div style="font-size:13px;font-style:normal;background-color:transparent;font-family:arial,helvetica,sans-serif"><font>It works just fine. Now, I'd like to break it down into two functions like so:</font></div>
<div style="font-size:13px;font-style:normal;background-color:transparent;font-family:arial,helvetica,sans-serif"><font>##</font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif"><div style="background-color:transparent">
code1 <-'</div><div style="background-color:transparent"> double x =
as<double>(x_r),</div><div style="background-color:transparent"> y = as<double>(y_r),</div><div style="background-color:transparent"> ans;</div><div style="background-color:transparent"><br></div>
<div style="background-color:transparent"> ans = (exp(x)-y) > 0 ? 1 : 0;</div><div style="background-color:transparent"><br></div><div style="background-color:transparent"> return wrap(ans);</div><div style="background-color:transparent">
'</div><div style="background-color:transparent"><br></div><div style="background-color:transparent">code2.1 <-'</div><div style="background-color:transparent"> NumericVector u = as<NumericVector>(u_r);</div>
<div style="background-color:transparent"> double mu = as<double>(mu_r), </div><div style="background-color:transparent"> v = log(mu);</div><div style="background-color:transparent"> int n = u.size(),</div>
<div style="background-color:transparent"> res = 0;</div><div style="background-color:transparent"><br></div><div style="background-color:transparent"> for(int i=0; i<n; i++){</div><div style="background-color:transparent">
res += as<int>(fun1(u(i),v));</div><div style="background-color:transparent"> }</div><div style="background-color:transparent"><br></div><div style="background-color:transparent"> return wrap(res);</div><div style="background-color:transparent">
'</div><div style="background-color:transparent"><br></div><div style="background-color:transparent">sig1 <- signature(x_r = 'numeric', y_r = 'numeric')</div><div style="background-color:transparent">
sig2 <- signature(u_r = 'numeric', mu_r = 'numeric')</div><div style="background-color:transparent">fun.test <- cxxfunction(list(fun1 = sig1, fun2 = sig2), </div><div style="background-color:transparent">
list(code1, code2.1), plugin = "Rcpp")</div></font></div><div style="font-size:13px;font-style:normal;background-color:transparent;font-family:arial,helvetica,sans-serif"><font>##</font></div>
<div style="font-size:13px;font-style:normal;background-color:transparent;font-family:arial,helvetica,sans-serif"><font><br></font></div><div style="font-size:13px;font-style:normal;background-color:transparent;font-family:arial,helvetica,sans-serif">
<font>But I get the error message:</font></div><div style="background-color:transparent"><div style="background-color:transparent"><font face="arial, helvetica, sans-serif"># file5686a0c71e8.cpp: In function 'SEXPREC* fun2(SEXP, SEXP)':</font></div>
<div style="background-color:transparent"><font face="arial, helvetica, sans-serif"># file5686a0c71e8.cpp:52:33: error: invalid cast from type 'double' to type 'SEXP'</font></div><div style="background-color:transparent">
<font face="arial, helvetica, sans-serif"># file5686a0c71e8.cpp:55:20: error: invalid cast from type 'Rcpp::traits::storage_type<14>::type {aka double}' to type 'SEXP'</font></div><div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif">
<font face="arial, helvetica, sans-serif"><br></font></div><div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif"><font face="arial, helvetica, sans-serif">I then tried to change the types of 'u(i)' and 'v' to SEXP, but I get the same error:</font></div>
<div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif"><font face="arial, helvetica, sans-serif">##</font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif"><div style="background-color:transparent">
code2.2 <-'</div><div style="background-color:transparent"> NumericVector u = as<NumericVector>(u_r);</div><div style="background-color:transparent"> double mu = as<double>(mu_r), </div><div style="background-color:transparent">
v = log(mu);</div><div style="background-color:transparent"> int n = u.size(),</div><div style="background-color:transparent"> res = 0;</div><div style="background-color:transparent">
SEXP us, vs = (SEXP) v;</div><div style="background-color:transparent"><br></div><div style="background-color:transparent"> for(int i=0; i<n; i++){</div><div style="background-color:transparent"> us = (SEXP) u(i);</div>
<div style="background-color:transparent"> res += as<int>(fun1(us,vs));</div><div style="background-color:transparent"> }</div><div style="background-color:transparent"><br></div><div style="background-color:transparent">
return wrap(res);</div><div style="background-color:transparent">'</div><div style="background-color:transparent"><br></div><div style="background-color:transparent">fun.test <- cxxfunction(list(fun1 = sig1, fun2 = sig2), </div>
<div style="background-color:transparent"> list(code1, code2.2), plugin = "Rcpp")</div></font></div><div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif">
<font face="arial, helvetica, sans-serif">##</font></div><div style="background-color:transparent"><font face="arial, helvetica, sans-serif"><div style="background-color:transparent"># file5686a0c71e8.cpp: In function 'SEXPREC* fun2(SEXP, SEXP)':</div>
<div style="background-color:transparent"># file5686a0c71e8.cpp:52:33: error: invalid cast from type 'double' to type 'SEXP'</div><div style="background-color:transparent"># file5686a0c71e8.cpp:55:20: error: invalid cast from type
'Rcpp::traits::storage_type<14>::type {aka double}' to type 'SEXP'</div><div style="background-color:transparent"><br></div><div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif">
Is there any way to make it work?</div><div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif"><br></div><div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif">
Thanks,</div><div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif">Rubem</div></font></div><div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif">
<font face="arial,
helvetica, sans-serif"><br></font></div><div style="font-style:normal;font-size:13px;background-color:transparent;font-family:arial,helvetica,sans-serif"><font face="arial, helvetica, sans-serif"><br></font></div></div>
</div></div></div><br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br>