[Rcpp-commits] r4006 - in pkg/Rcpp: . inst inst/examples/Misc
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Nov 21 13:46:45 CET 2012
Author: edd
Date: 2012-11-21 13:46:44 +0100 (Wed, 21 Nov 2012)
New Revision: 4006
Added:
pkg/Rcpp/inst/examples/Misc/piBySimulation.r
pkg/Rcpp/inst/examples/Misc/piSugar.cpp
Modified:
pkg/Rcpp/ChangeLog
pkg/Rcpp/inst/NEWS.Rd
Log:
added 'pi by Simulation' example
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2012-11-21 10:35:38 UTC (rev 4005)
+++ pkg/Rcpp/ChangeLog 2012-11-21 12:46:44 UTC (rev 4006)
@@ -1,3 +1,9 @@
+2012-11-21 Dirk Eddelbuettel <edd at debian.org>
+
+ * inst/examples/Misc/piBySimulation.r: New simple example using Rcpp
+ attributes in a 'compute pi by simulation in R and C++' exercise
+ * inst/examples/Misc/piSugar.cpp: Corresponding C++ variant
+
2012-11-21 Romain Francois <romain at r-enthusiasts.com>
* include/Rcpp/iostream/Rostream.h: Fix warning given by -Wreorder
@@ -10,12 +16,12 @@
2012-11-20 Romain Francois <romain at r-enthusiasts.com>
- * include/Rcpp/iostream/Rostream.h: change order of initiaization in
- ctor. was making solaris compiler unhappy
+ * include/Rcpp/iostream/Rostream.h: change order of initiaization in
+ ctor. was making solaris compiler unhappy
* include/Rcpp/stats/random/rnorm.h: not using function pointer
- generators, as this generates (anachronisms) warning on solaris. Also,
- this version is more efficient since there is no need to dereference
- the function pointer
+ generators, as this generates (anachronisms) warning on
+ solaris. Also, this version is more efficient since there is no need
+ to dereference the function pointer
2012-11-19 JJ Allaire <jj at rstudio.org>
@@ -30,8 +36,8 @@
2012-11-18 JJ Allaire <jj at rstudio.org>
- * R/Attributes.R: sourceCpp embedded R code; print warning if
- no export attributes are found in source file
+ * R/Attributes.R: sourceCpp embedded R code; print warning if no
+ export attributes are found in source file
* src/AttributesParser.h: sourceCpp embedded R code
* src/AttributesParser.cpp: sourceCpp embedded R code; new scheme
for mixing user and generated C++ headers
Modified: pkg/Rcpp/inst/NEWS.Rd
===================================================================
--- pkg/Rcpp/inst/NEWS.Rd 2012-11-21 10:35:38 UTC (rev 4005)
+++ pkg/Rcpp/inst/NEWS.Rd 2012-11-21 12:46:44 UTC (rev 4006)
@@ -6,7 +6,7 @@
\itemize{
\item Changes in Rcpp sugar:
\itemize{
- \item New functions: \code{setdiff}, \code{union_}, \code{intersect}
+ \item New functions: \code{setdiff}, \code{union_}, \code{intersect}
\code{setequal}, \code{in}, \code{min}, \code{max}, \code{range},
\code{match}
}
@@ -31,6 +31,10 @@
\itemize{
\item New function \code{areMacrosDefined}
}
+ \item Miscellaneous changes:
+ \itemize{
+ \item New example 'pi simulation' using R and C++ via Rcpp attributes
+ }
}
}
\section{Changes in Rcpp version 0.10.0 (2012-11-13)}{
Added: pkg/Rcpp/inst/examples/Misc/piBySimulation.r
===================================================================
--- pkg/Rcpp/inst/examples/Misc/piBySimulation.r (rev 0)
+++ pkg/Rcpp/inst/examples/Misc/piBySimulation.r 2012-11-21 12:46:44 UTC (rev 4006)
@@ -0,0 +1,28 @@
+#!/usr/bin/r
+
+library(Rcpp)
+library(rbenchmark)
+
+piR <- function(N) {
+ x <- runif(N)
+ y <- runif(N)
+ d <- sqrt(x^2 + y^2)
+ return(4 * sum(d < 1.0) / N)
+}
+
+sourceCpp("piSugar.cpp")
+
+N <- 1e6
+
+set.seed(42)
+resR <- piR(N)
+
+set.seed(42)
+resCpp <- piSugar(N)
+
+## important: check results are identical with RNG seeded
+stopifnot(identical(resR, resCpp))
+
+res <- benchmark(piR(N), piSugar(N), order="relative")
+
+print(res[,1:4])
Property changes on: pkg/Rcpp/inst/examples/Misc/piBySimulation.r
___________________________________________________________________
Added: svn:executable
+ *
Added: pkg/Rcpp/inst/examples/Misc/piSugar.cpp
===================================================================
--- pkg/Rcpp/inst/examples/Misc/piSugar.cpp (rev 0)
+++ pkg/Rcpp/inst/examples/Misc/piSugar.cpp 2012-11-21 12:46:44 UTC (rev 4006)
@@ -0,0 +1,13 @@
+
+#include <Rcpp.h>
+
+using namespace Rcpp;
+
+// [[Rcpp::export]]
+double piSugar(const int N) {
+ RNGScope scope; // ensure RNG gets set/reset
+ NumericVector x = runif(N);
+ NumericVector y = runif(N);
+ NumericVector d = sqrt(x*x + y*y);
+ return 4.0 * sum(d < 1.0) / N;
+}
More information about the Rcpp-commits
mailing list