[Rcpp-devel] Rccp code with vector and matrix inputs and matrix output

Petre Caraiani petre.caraiani at gmail.com
Fri Mar 28 09:30:25 CET 2014


Hello,
The following code works well in R:
attach(dataqtr)

dataqtr <- data.table(dataqtr)
setkeyv(dataqtr,c("gvkey","qtr"))

vec_growth <- data.frame(0,0,0,0,0,0,0,0,0,0,0,0,0)
vec_eps    <- data.frame(0,0,0,0,0,0,0,0,0,0,0,0,0)

T <- 15

for (i in 1:nrow(dataqtr)) {
  vec_growth[i,] <- ( dataqtr[i,LTG] * exp(1:(T-2)/(T-1)*log(
dataqtr[i,meanLTG] / dataqtr[i,LTG] )))

}


However I am not able to reproduce it using the following Rccp code:

a<- dataqtr[,LTG]
b<- dataqtr[,meanLTG]

src <-'
 Rcpp::NumericVector a(aa);
 Rcpp::NumericVector b(bb);
 Rcpp::NumericMatrix Am (A);
 int n = a.size();
 int m = b.size();
 int T=15;

 int nrows = Am.nrow();
 int ncol  = Am.ncol();

     for (int ii = 1; ii < nrows; ii++) {
        for (int jj = 1; jj<ncol; jj++)  {
        Am[ii,jj] =  a[jj] * exp(jj/(T-1)*log( b[jj] / a[jj] ));
        }}
return Am;
'
fun <- cxxfunction(signature(aa="numeric", bb="numeric",A="numeric"), body
= src, plugin="Rcpp")

A<-matrix(data=NA,nrow=100,ncol=13)
fun(a,b,A)

I don't understand the error.

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140328/e69cbccb/attachment.html>


More information about the Rcpp-devel mailing list