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

Christian Gunning xian at unm.edu
Thu Dec 9 06:34:06 CET 2010


Attached is a patch relative to pkg/Rcpp/inst/doc/Rcpp-quickref that
supercedes the previous patch.  Advice on using stats functions is
provided, and examples include doubles for distribution-specific
parameters.

best,
xian

On Tue, Dec 7, 2010 at 3:15 AM, Christian Gunning <xian at unm.edu> wrote:
> 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
>



-- 
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
-------------- next part --------------
Index: Rcpp-quickref.Rnw
===================================================================
--- Rcpp-quickref.Rnw	(revision 2748)
+++ 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,60 @@
 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;
+
+// For details see Section 6.7.1--Distribution functions of the `Writing R Extensions' manual. In some cases (e.g. rnorm), distribution-specific arguments can be omitted; when in doubt, specify all dist-specific arguments. The use of doubles rather than integers for dist-specific arguments is recommended. Unless explicitly specified, log=FALSE. 
+
+// Equivalent to R calls
+NumericVector xx = runif(20);
+NumericVector xx1 = rnorm(20);
+NumericVector xx1 = rnorm(20, 0);
+NumericVector xx1 = rnorm(20, 0, 1);
+
+// Example vector of quantiles
+NumericVector quants(5);
+for (int i = 0; i < 5; i++) {
+    quants[i] = (i-2);
+}
+
+// in R, dnorm(-2:2)
+NumericVector yy = dnorm(quants) ;
+NumericVector yy = dnorm(quants, 0.0, 1.0) ;
+
+// in R, dnorm(-2:2, mean=2, log=TRUE)
+NumericVector yy = dnorm(quants, 2.0, true) ;
+
+// Note - cannot specify sd without mean
+// in R, dnorm(-2:2, mean=0, sd=2, log=TRUE)
+NumericVector yy = dnorm(quants, 0.0, 2.0, true) ;
+
+// To get original R api, use Rf_*
+double zz = Rf_rnorm(0, 2);
+@
+
+
+
 \end{document}


More information about the Rcpp-devel mailing list