Skip to content

Commit c419bd3

Browse files
committed
Merge branch 'gh-pages' of github.com:jjallaire/rcpp-gallery into gh-pages
2 parents 47fd1ee + 8ada33d commit c419bd3

8 files changed

Lines changed: 242 additions & 315 deletions

_posts/2012-12-20-simulating-pi.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ scaling up to the full circle, we have an estimate of pi.
2727

2828

2929

30-
3130
{% highlight r %}
3231
piR <- function(N) {
3332
x <- runif(N)
@@ -46,7 +45,6 @@ c(piR(1000), piR(10000), piR(100000), piR(1000000))
4645
[1] 3.156 3.155 3.139 3.141
4746
</pre>
4847

49-
5048
The neat thing about Rcpp sugar enables us to write C++ code that
5149
looks almost as compact.
5250

@@ -56,19 +54,19 @@ using namespace Rcpp;
5654

5755
// [[Rcpp::export]]
5856
double piSugar(const int N) {
59-
RNGScope scope; // ensure RNG gets set/reset
60-
NumericVector x = runif(N);
61-
NumericVector y = runif(N);
62-
NumericVector d = sqrt(x*x + y*y);
63-
return 4.0 * sum(d < 1.0) / N;
57+
NumericVector x = runif(N);
58+
NumericVector y = runif(N);
59+
NumericVector d = sqrt(x*x + y*y);
60+
return 4.0 * sum(d < 1.0) / N;
6461
}
6562
{% endhighlight %}
6663

67-
6864
Apart from using types (hey, this is C++) and assuring the RNG gets
6965
set and reset, the code is essentially identical.
7066

71-
And by using the same RNG, so are the results.
67+
And by using the same RNG, so are the results. Rcpp ensures that
68+
the RNG state is set and reset properly by instantiating an object
69+
of class `RNGScope`.
7270

7371
{% highlight r %}
7472
set.seed(5)
@@ -80,4 +78,3 @@ c(piSugar(1000), piSugar(10000), piSugar(100000), piSugar(1000000))
8078
<pre class="output">
8179
[1] 3.156 3.155 3.139 3.141
8280
</pre>
83-

_posts/2012-12-22-vector-cumulative-sum.md

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ src: 2012-12-22-vector-cumulative-sum.cpp
1010
---
1111

1212

13-
1413
The traditional way to compute the cumulative sum of a vector is with a
1514
for loop. This is demonstrated with the function cumsum1().
1615

@@ -35,7 +34,6 @@ NumericVector cumsum1(NumericVector x){
3534
}
3635
{% endhighlight %}
3736

38-
3937
The C++ standard template library (STL) has the partial_sum() function
4038
that computes the cumulative sum of a vector. This is demonstrated with
4139
the function cumsum2().
@@ -50,7 +48,6 @@ NumericVector cumsum2(NumericVector x){
5048
}
5149
{% endhighlight %}
5250

53-
5451
With Rcpp sugar, there is a cumsum() function which makes writing
5552
this function in C++ very similar to using the cumsum function in R.
5653

@@ -61,52 +58,15 @@ NumericVector cumsum_sug(NumericVector x){
6158
}
6259
{% endhighlight %}
6360

61+
And we can of course compare the versions discussed here with the base R variant.
6462

6563
{% highlight r %}
66-
x <- 1:10
67-
cumsum1(x)
68-
{% endhighlight %}
69-
70-
71-
72-
<pre class="output">
73-
[1] 1 3 6 10 15 21 28 36 45 55
74-
</pre>
75-
76-
77-
78-
{% highlight r %}
79-
cumsum2(x)
80-
{% endhighlight %}
81-
82-
83-
84-
<pre class="output">
85-
[1] 1 3 6 10 15 21 28 36 45 55
86-
</pre>
87-
88-
89-
90-
{% highlight r %}
91-
cumsum_sug(x)
64+
x <- 1:10
65+
all.equal(cumsum1(x), cumsum2(x), cumsum_sug(x), cumsum(x))
9266
{% endhighlight %}
9367

9468

9569

9670
<pre class="output">
97-
[1] 1 3 6 10 15 21 28 36 45 55
71+
[1] TRUE
9872
</pre>
99-
100-
101-
102-
{% highlight r %}
103-
# cumsum function in base R
104-
cumsum(x)
105-
{% endhighlight %}
106-
107-
108-
109-
<pre class="output">
110-
[1] 1 3 6 10 15 21 28 36 45 55
111-
</pre>
112-

0 commit comments

Comments
 (0)