[Rcpp-devel] Rcpp-quickref patch - Inline, Random

Christian Gunning xian at unm.edu
Tue Dec 7 11:15:47 CET 2010


Attached is a patch relative to pkg/Rcpp/inst/doc/Rcpp-quickref.

I added Douglas' suggestion to the Matrix section.  In addition are
reference examples for inline and the random functions.

There was a recent list question about dnorm, and I ran across similar
confusion.  Is the default argument cascade the same for all of the d*
family?  For dnorm, assuming numvec contains {-1, 0, 1}, we have:

//Working:
NumericVector works1, works2;
 works1 = dnorm(numvec, 0, 1, false);
 works2 = dnorm(numvec, 0, false);

As far as I could tell, all other combinations of missing arguments
fails at compile time.  Since e.g. rnorm(10) works, this seems a bit
counter-intuitive.  Personally, this isn't a priority for me - just
trying to figure out the system.

best,
xian
-------------- next part --------------
Index: Rcpp-quickref.Rnw
===================================================================
--- Rcpp-quickref.Rnw	(revision 2732)
+++ Rcpp-quickref.Rnw	(working copy)
@@ -90,6 +90,23 @@
 int n = xx.size();
 @
 
+\paragraph{Inline}~
+  \newline
+  \newline
+<<lang=cpp>>=
+## Note - this is R code. inline allows rapid testing.
+require(inline)
+testfun = cxxfunction(
+            signature(x="numeric", i="integer"),
+            body = '
+                NumericVector xx(x);
+                int ii = as<int>(i);
+                xx = xx * ii;
+                return( xx );
+            ', plugin="Rcpp")
+testfun(1:5, 3)
+@
+
 \paragraph{Function}~
   \newline
   \newline
@@ -99,8 +116,7 @@
 @
 
 \paragraph{Environment}~
-  \newline
-  \newline
+    \newline
 <<lang=cpp>>=
 Environment stats("package:stats");
 Environment env( 2 ); // by position
@@ -173,7 +189,6 @@
 
 \paragraph{Using matrices}~
   \newline
-  \newline
 <<lang=cpp>>=
 // Initializing from SEXP, 
 // dimensions handled automatically
@@ -188,13 +203,51 @@
 for (int i = 0; i < xsize; i++) {
     xx[i] = 7;
 }
+// Same as above, using STL fill
+std::fill(xx.begin(), xx.end(), 8);
 
 // Assign this value to single element 
 // (1st row, 2nd col)
 xx(0,1) = 4;
 
-// Extract the second column
-NumericVector zz = xx( _, 1);
+// Reference the second column
+// Changes propagate to xx (same applies for Row)
+NumericMatrix::Column zzcol = xx( _, 1);
+zzcol = zzcol * 2;
+
+// Copy the second column into new object
+NumericVector zz1 = xx( _, 1);
+// Copy the submatrix (top left 3x3) into new object
+NumericMatrix zz2 = xx( Range(0,2), Range(0,2));
 @
 
+
+\paragraph{Random functions}~
+  \newline
+<<lang=cpp>>=
+// Set seed
+RNGScope scope;
+
+// Equivalent to R calls
+NumericVector xx = runif(20);
+NumericVector xx1 = rnorm(20);
+
+// Example vector of quantiles
+NumericVector quants(5);
+for (int i = 0; i < 5; i++) {
+    quants[i] = (i-2);
+}
+// in R, dnorm(-2:2, mean=0, sd=2, log=FALSE)
+NumericVector yy = dnorm(quants, 0, 2, false) ;
+
+// Here, must specify at least x, mean, and log
+// in R, dnorm(-2:2, mean=0, log=FALSE)
+NumericVector yy = dnorm(quants, 0, false) ;
+
+// To get original R api, use Rf_*
+double zz = Rf_rnorm(0, 2);
+@
+
+
+
 \end{document}


More information about the Rcpp-devel mailing list