#############################################################################
# EXAMPLE 1: Rcpp code logistic distribution
#############################################################################
library(Rcpp)
library(inline)
# define Rcpp file
code1 <- "
// input array A
Rcpp::NumericMatrix AA(A);
// Rcpp::IntegerVector dimAA(dimA);
int nrows = AA.nrow();
int ncolumns = AA.ncol();
Rcpp::NumericMatrix Alogis(nrows,ncolumns) ;
// compute logistic distribution
for (int ii=0; ii<nrows; ii++){
Rcpp::NumericVector h1=AA.row(ii) ;
Rcpp::NumericVector res = plogis( h1 ) ;
for (int jj=0;jj<ncolumns;jj++){
Alogis(ii,jj) = res[jj] ;
}
}
return( wrap(Alogis) );
"
# compile Rcpp code
fct_rcpp <- inline::cxxfunction( signature( A= "matrix"), code1,
plugin = "Rcpp", verbose=TRUE )
# copy function and save it as object 'calclogis'
name <- "calclogis" # name of the function
cxxfunction.copy( cppfct= fct_rcpp, name=name )
# function is available as object named as name
Reval( paste0( name , " <- fct_rcpp " ) )
# test function
m1 <- outer( seq( -2 , 2 , len=10 ) , seq(-1.5,1.5 ,len=4) )
calclogis(m1)
Run the code above in your browser using DataLab