[Rcpp-devel] NumericVector Double mismatch when indexing an array

Dirk Eddelbuettel edd at debian.org
Tue Sep 25 22:41:28 CEST 2012


On 25 September 2012 at 13:53, Rodney Sparapani wrote:
| You just need an explicit type conversion like so:
| 
| funk2 <- cxxfunction(body='RNGScope scope;
|            NumericVector rn(5);
|            for (int i=0; i < 5; i++) rn[i] = as<double>(rnorm(1,0,1));
|            return wrap(rn);',
|            includes="using namespace Rcpp;\n", plugin="Rcpp")
| 
| funk2()

In the interest of constructive criticism, my version (and I must posted
essentially the same maybe ten times already on this very list):

R> f3 <- cxxfunction(signature(), plugin="Rcpp", body='
+    RNGScope scope;
+    NumericVector rn = Rcpp::rnorm(5,0,1);
+    return wrap(rn);
+ ')
R> f3()
[1] -0.837983 -0.706765  0.551781  0.662024  0.495562
R> 

Changes:

  -- no 'using namespace Rcpp;' as inline already does it; I still don't use
     it as I prefer to be explicit about provenance of functions and objects

  -- no loop (as Doug said) --- rnorm() is a sugar functions and vectorised

  -- no wrap, we already have an Rcpp vector for which wrap is implicit

Dirk

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


More information about the Rcpp-devel mailing list