[Rcpp-devel] Cast for output of Rcpp Sugar runif()

Dirk Eddelbuettel edd at debian.org
Wed Apr 13 13:43:47 CEST 2011


On 13 April 2011 at 12:19, Cedric Ginestet wrote:
| Hi guys,
| 
| I want to produce discrete uniform variates within C++ from the runif()
| function available through Rcpp Sugar. However, I don't seem to be able to find
| the appropriate cast in order to return an IntegerVector as opposed to a
| numeric one. I have naively used int(), but this is clearly not sufficient.
| 
| Any help would be greatly appreciated,
| Ced
| 
| ##########################################
| #### Discrete uniform variate.
| src <- '
|   using namespace Rcpp ;
|   RNGScope scope;
|   int n = as<int>(xn);
|   int a = as<int>(xa);
|   int b = as<int>(xb);
|   IntegerVector vec(n);
|   for(int i=0; i<n; ++i) vec[i] = (int)floor(runif(i,a,b));
| return vec;'
| cxxfun <- cxxfunction(sig=signature(xn="numeric",xa="numeric",xb=
| "numeric"),body=src,plugin="Rcpp",verbose=TRUE)
| cxxfun(10,0,10);
| 
| ##########################
| Error in compileCode(f, code, language = language, verbose = verbose) :
|   Compilation ERROR, function(s)/method(s) not created! file52d217df.cpp: In
| function ?SEXPREC* file52d217df(SEXPREC*, SEXPREC*, SEXPREC*)?:
| file52d217df.cpp:37: error: invalid cast from type ?Rcpp::sugar::SugarMath_1
| <true, double, double, Rcpp::Vector<14>, double (*)(double)>? to type ?int?
| make: *** [file52d217df.o] Error 1
| ##################################################

Well as a start maybe try this as your loop:

   for (int i=0; i<n; ++i) {
       double tmp = runif(1,a,b));  	// why where you using i here?
       vec[i] = static_cast<int>(tmp);  // C++ static cast 
   }

But you do of course understand that 

    a) Rcpp sugar can draw an entire vector at once and

    b) Rcpp sugar also has a vectorised floor() function

right?   So with that try

R> src <- '
+    RNGScope scope;
+    int n = as<int>(xn);
+    int a = as<int>(xa);
+    int b = as<int>(xb);
+    IntegerVector vec(n);
+    vec = floor(runif(n,a,b));
+    return vec;'
R> cxxfun <- cxxfunction(sig=signature(xn="numeric",xa="numeric",xb="numeric"),
+                       body=src,plugin="Rcpp")
R> cxxfun(10,0,10);
 [1] 8 5 1 7 0 0 6 9 8 1
R> 


Hth, Dirk
 
| --
| Cedric Ginestet
| Centre for Neuroimaging Sciences (L3.04)
| NIHR Biomedical Research Centre
| Department of Neuroimaging
| Institute of Psychiatry, Box P089
| King's College London
| De Crespigny Park
| London
| SE5 8AF
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

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


More information about the Rcpp-devel mailing list