[Rcpp-devel] Begginer Problems...

Dirk Eddelbuettel edd at debian.org
Mon Mar 21 21:38:56 CET 2011


On 21 March 2011 at 16:56, Antonio Hermes M. da S. Junior - Depto. Estatistica wrote:
| Hello,
| 
|   I spent last weekend trying to run this piece of code (and a lot of
| variations too):
| 
| suppressPackageStartupMessages(require(inline))
| suppressPackageStartupMessages(require(Rcpp))
| 
| vero.empi.cpp <- '
|   Rcpp::NumericVector xa(vpf) ;
|   Rcpp::NumericVector xb(vpe) ;
|   Rcpp::NumericVector xc(tempo) ;
|   Rcpp::NumericVector xd(evento) ;
|   int n_xa = xa.size(), n_xb = xb.size(), n_xd = xd.size() ;
|   Rcpp::NumericMatrix vero(n_xa,n_xb)
|   Rcpp::Function d(fun1) ;
|   Rcpp::Function S(fun2) ;
|   float aux ;
|   for (int i = 0; i < n_xa; i++){
|     for (int j = 0; j < n_xb; j++){
|       aux = 0 ;
|       for(int k = 0; k < n_xd; k++){
|       aux = aux*(d(xc[k], xa[i], xb[j])^xd[k])*(S(xc[k], xa[i], xb[j])^(1-
| xd[k])) ;
|       }
|       vero[i , j] = aux ;
|     }
|   }
|   return vero;
| '
| 
| vero.empi <- cxxfunction(
|   signature(vpf="numeric", vpe="numeric", tempo="numeric", evento="numeric",
| fun1 = "function", fun2 = "function"),
|   vero.empi.cpp, plugin="Rcpp", include = "using namespace Rcpp; ")
| 
| The purpose here is to generate a matrix of empirical values of a likelihood.
| For a first trial, I would like to use the R functions dweibull() and pweibull
| () in fun1 and fun2, respectively. But it didn't work for some reason I cannot
| see. Is this possible?

You have access to dweibull and pweibull etc at the C++ level, even two ways. 

One is via Rcpp sugar, see the vignette and previous discussions on this list.

The other is via the standard R math functions where you would have to prefix
via Rf_ (or even ::Rf_) to avoid confusion with the sugar functions. In other
words call ::Rf_pweibull.  That is basically as documented in 'Writing R
Exentions' with the added qualifier of needing to use the Rf_ prefix.

Hope this helps,  Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list