Skip to content

Commit e4b992a

Browse files
committed
added 'pi by Simulation' example
1 parent bdb7299 commit e4b992a

4 files changed

Lines changed: 59 additions & 8 deletions

File tree

ChangeLog

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2012-11-21 Dirk Eddelbuettel <edd@debian.org>
2+
3+
* inst/examples/Misc/piBySimulation.r: New simple example using Rcpp
4+
attributes in a 'compute pi by simulation in R and C++' exercise
5+
* inst/examples/Misc/piSugar.cpp: Corresponding C++ variant
6+
17
2012-11-21 Romain Francois <romain@r-enthusiasts.com>
28

39
* include/Rcpp/iostream/Rostream.h: Fix warning given by -Wreorder
@@ -10,12 +16,12 @@
1016

1117
2012-11-20 Romain Francois <romain@r-enthusiasts.com>
1218

13-
* include/Rcpp/iostream/Rostream.h: change order of initiaization in
14-
ctor. was making solaris compiler unhappy
19+
* include/Rcpp/iostream/Rostream.h: change order of initiaization in
20+
ctor. was making solaris compiler unhappy
1521
* include/Rcpp/stats/random/rnorm.h: not using function pointer
16-
generators, as this generates (anachronisms) warning on solaris. Also,
17-
this version is more efficient since there is no need to dereference
18-
the function pointer
22+
generators, as this generates (anachronisms) warning on
23+
solaris. Also, this version is more efficient since there is no need
24+
to dereference the function pointer
1925

2026
2012-11-19 JJ Allaire <jj@rstudio.org>
2127

@@ -30,8 +36,8 @@
3036

3137
2012-11-18 JJ Allaire <jj@rstudio.org>
3238

33-
* R/Attributes.R: sourceCpp embedded R code; print warning if
34-
no export attributes are found in source file
39+
* R/Attributes.R: sourceCpp embedded R code; print warning if no
40+
export attributes are found in source file
3541
* src/AttributesParser.h: sourceCpp embedded R code
3642
* src/AttributesParser.cpp: sourceCpp embedded R code; new scheme
3743
for mixing user and generated C++ headers

inst/NEWS.Rd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
\itemize{
77
\item Changes in Rcpp sugar:
88
\itemize{
9-
\item New functions: \code{setdiff}, \code{union_}, \code{intersect}
9+
\item New functions: \code{setdiff}, \code{union_}, \code{intersect}
1010
\code{setequal}, \code{in}, \code{min}, \code{max}, \code{range},
1111
\code{match}
1212
}
@@ -31,6 +31,10 @@
3131
\itemize{
3232
\item New function \code{areMacrosDefined}
3333
}
34+
\item Miscellaneous changes:
35+
\itemize{
36+
\item New example 'pi simulation' using R and C++ via Rcpp attributes
37+
}
3438
}
3539
}
3640
\section{Changes in Rcpp version 0.10.0 (2012-11-13)}{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/r
2+
3+
library(Rcpp)
4+
library(rbenchmark)
5+
6+
piR <- function(N) {
7+
x <- runif(N)
8+
y <- runif(N)
9+
d <- sqrt(x^2 + y^2)
10+
return(4 * sum(d < 1.0) / N)
11+
}
12+
13+
sourceCpp("piSugar.cpp")
14+
15+
N <- 1e6
16+
17+
set.seed(42)
18+
resR <- piR(N)
19+
20+
set.seed(42)
21+
resCpp <- piSugar(N)
22+
23+
## important: check results are identical with RNG seeded
24+
stopifnot(identical(resR, resCpp))
25+
26+
res <- benchmark(piR(N), piSugar(N), order="relative")
27+
28+
print(res[,1:4])

inst/examples/Misc/piSugar.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#include <Rcpp.h>
3+
4+
using namespace Rcpp;
5+
6+
// [[Rcpp::export]]
7+
double piSugar(const int N) {
8+
RNGScope scope; // ensure RNG gets set/reset
9+
NumericVector x = runif(N);
10+
NumericVector y = runif(N);
11+
NumericVector d = sqrt(x*x + y*y);
12+
return 4.0 * sum(d < 1.0) / N;
13+
}

0 commit comments

Comments
 (0)