Skip to content

Commit 8c17760

Browse files
committed
new sugar articles: all, any, diff
1 parent 70a1b80 commit 8c17760

6 files changed

+526
-0
lines changed

_posts/2012-12-23-sugar-all.md

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
title: Using Sugar Function all()
3+
author: Ross Bennett
4+
license: GPL (>= 2)
5+
tags: sugar
6+
summary: Illustrates the use of sugar function all()
7+
layout: post
8+
src: 2012-12-23-sugar-all.cpp
9+
---
10+
The sugar function all() answers the question, "Are all of the values ... ?".
11+
12+
13+
14+
The all_sug() function takes a LogicalVector as an argument and allows one to
15+
enter an expression for the argument as shown in the R examples. In this example,
16+
it is simply wrapper around the sugar function all() and includes is_true to
17+
return a boolean.
18+
Note that when comparing two vectors, it is an element-wise comparison.
19+
(i.e. `x[0] > y[0]`, ..., `x[n] > y[n]`)
20+
21+
{% highlight cpp %}
22+
#include <Rcpp.h>
23+
using namespace Rcpp;
24+
25+
// [[Rcpp::export]]
26+
bool all_sug(LogicalVector x) {
27+
// Note the use of is_true to return a bool type.
28+
return is_true(all(x == TRUE));
29+
}
30+
{% endhighlight %}
31+
32+
33+
While the above function may seem trivial, it can be easy to forget is_true() when
34+
using all() and will result in a compile error. The check_equal() function below
35+
is an example of a simple utility function to check two vectors for equality
36+
using the all_sug() function defined above.
37+
38+
{% highlight cpp %}
39+
// [[Rcpp::export]]
40+
void check_equal(NumericVector x, NumericVector y) {
41+
if(all_sug(x == y)) {
42+
Rcout << "Success! The input vectors are equal" << std::endl;
43+
// do something
44+
} else {
45+
Rcout << "Fail! The input vectors are not equal" << std::endl;
46+
// do something else
47+
}
48+
}
49+
{% endhighlight %}
50+
51+
52+
{% highlight r %}
53+
x <- c(3, 9, 0, 2, 7, 5, 6)
54+
y <- c(0, 0, 0, 0, 0, 0, 0)
55+
all_sug(x < 10)
56+
{% endhighlight %}
57+
58+
59+
60+
<pre class="output">
61+
[1] TRUE
62+
</pre>
63+
64+
65+
66+
{% highlight r %}
67+
all_sug(x != 3)
68+
{% endhighlight %}
69+
70+
71+
72+
<pre class="output">
73+
[1] FALSE
74+
</pre>
75+
76+
77+
78+
{% highlight r %}
79+
all_sug(x >= y)
80+
{% endhighlight %}
81+
82+
83+
84+
<pre class="output">
85+
[1] TRUE
86+
</pre>
87+
88+
89+
90+
{% highlight r %}
91+
all_sug(y == 0)
92+
{% endhighlight %}
93+
94+
95+
96+
<pre class="output">
97+
[1] TRUE
98+
</pre>
99+
100+
101+
102+
{% highlight r %}
103+
check_equal(x, y)
104+
{% endhighlight %}
105+
106+
107+
108+
<pre class="output">
109+
Fail! The input vectors are not equal
110+
</pre>
111+
112+
113+
114+
<pre class="output">
115+
NULL
116+
</pre>
117+
118+
119+
120+
{% highlight r %}
121+
check_equal(x, c(3, 9, 0, 2, 7, 5, 6))
122+
{% endhighlight %}
123+
124+
125+
126+
<pre class="output">
127+
Success! The input vectors are equal
128+
</pre>
129+
130+
131+
132+
<pre class="output">
133+
NULL
134+
</pre>
135+

_posts/2012-12-23-sugar-any.md

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
title: Using Sugar Function any()
3+
author: Ross Bennett
4+
license: GPL (>= 2)
5+
tags: sugar
6+
summary: Illustrates the use of sugar function any()
7+
layout: post
8+
src: 2012-12-23-sugar-any.cpp
9+
---
10+
The sugar function any() answers the question, "Are any of the values ... ?".
11+
12+
13+
14+
15+
The any_sug() function takes a LogicalVector as an argument and allows one to
16+
enter an expression for the argument as shown in the R examples. In this
17+
example, it is simply wrapper around the sugar function any() and includes
18+
is_true to return a boolean.
19+
20+
Note that when comparing two vectors, it is an element-wise comparison.
21+
(i.e. `x[0] > y[0]`, ..., `x[n] > y[n]`)
22+
23+
{% highlight cpp %}
24+
#include <Rcpp.h>
25+
using namespace Rcpp;
26+
27+
// [[Rcpp::export]]
28+
bool any_sug(LogicalVector x){
29+
// Note the use of is_true to return a bool type
30+
return is_true(any(x == TRUE));
31+
}
32+
{% endhighlight %}
33+
34+
35+
While the above function may seem trivial, it can be easy to forget is_true()
36+
when using any() and will result in a compile error. The check_negative()
37+
function below is an example of a simple utility function to check if a
38+
vector contains negative values using the any_sug() function defined above.
39+
40+
{% highlight cpp %}
41+
// [[Rcpp::export]]
42+
void check_negative(NumericVector x) {
43+
if(any_sug(x < 0)) {
44+
Rcout << "The vector contains negative numbers" << std::endl;
45+
// do something
46+
} else {
47+
Rcout << "The vector does not contain negative numbers" << std::endl;
48+
// do something else
49+
}
50+
}
51+
{% endhighlight %}
52+
53+
54+
{% highlight r %}
55+
x <- c(3, 9, 0, 2, 7, -1, 6)
56+
y <- c(8, 3, 2, 6, 1, 5, 0)
57+
any_sug(x < 10)
58+
{% endhighlight %}
59+
60+
61+
62+
<pre class="output">
63+
[1] TRUE
64+
</pre>
65+
66+
67+
68+
{% highlight r %}
69+
any_sug(x != 3)
70+
{% endhighlight %}
71+
72+
73+
74+
<pre class="output">
75+
[1] TRUE
76+
</pre>
77+
78+
79+
80+
{% highlight r %}
81+
any_sug(x >= y)
82+
{% endhighlight %}
83+
84+
85+
86+
<pre class="output">
87+
[1] TRUE
88+
</pre>
89+
90+
91+
92+
{% highlight r %}
93+
any_sug(y == 0)
94+
{% endhighlight %}
95+
96+
97+
98+
<pre class="output">
99+
[1] TRUE
100+
</pre>
101+
102+
103+
104+
{% highlight r %}
105+
check_negative(x)
106+
{% endhighlight %}
107+
108+
109+
110+
<pre class="output">
111+
The vector contains negative numbers
112+
</pre>
113+
114+
115+
116+
<pre class="output">
117+
NULL
118+
</pre>
119+
120+
121+
122+
{% highlight r %}
123+
check_negative(y)
124+
{% endhighlight %}
125+
126+
127+
128+
<pre class="output">
129+
The vector does not contain negative numbers
130+
</pre>
131+
132+
133+
134+
<pre class="output">
135+
NULL
136+
</pre>
137+

_posts/2012-12-23-sugar-diff.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
title: Using Sugar Function diff()
3+
author: Ross Bennett
4+
license: GPL (>= 2)
5+
tags: sugar
6+
summary: Illustrates the use of sugar function diff()
7+
layout: post
8+
src: 2012-12-23-sugar-diff.cpp
9+
---
10+
11+
12+
13+
The sugar function diff() computes the difference of consecutive elements
14+
(i.e. lag = 1) of the input vector. Note that the size of the vector returned
15+
is one less than the input vector. The sugar function diff() works the same
16+
way as the diff() function in base R, except the lag is not specified.
17+
18+
{% highlight cpp %}
19+
#include <Rcpp.h>
20+
using namespace Rcpp;
21+
22+
// [[Rcpp::export]]
23+
NumericVector diff_sug(NumericVector x){
24+
return diff(x);
25+
}
26+
{% endhighlight %}
27+
28+
29+
One can use the diff() function to compute one period simple returns of stock
30+
prices.
31+
32+
{% highlight cpp %}
33+
// [[Rcpp::export]]
34+
NumericVector ret_simple(NumericVector x) {
35+
NumericVector vec_diff = diff(x);
36+
NumericVector res(x.size());
37+
// pad the front with an NA
38+
res[0] = NA_REAL;
39+
for(int i = 1; i < res.size(); i++) {
40+
res[i] = vec_diff[i-1] / x[i-1];
41+
}
42+
return res;
43+
}
44+
{% endhighlight %}
45+
46+
47+
{% highlight r %}
48+
x <- rnorm(10)
49+
# Close prices of S&P 500
50+
y <- c(1418.55, 1427.84, 1428.48, 1419.45, 1413.58,
51+
1430.36, 1446.79, 1435.81, 1443.69, 1430.15)
52+
diff_sug(x)
53+
{% endhighlight %}
54+
55+
56+
57+
<pre class="output">
58+
[1] 1.78889 -1.48820 0.05878 1.58578 -1.25415 -1.72598 0.57821 0.24119
59+
[9] 1.66974
60+
</pre>
61+
62+
63+
64+
{% highlight r %}
65+
# base R function
66+
diff(x)
67+
{% endhighlight %}
68+
69+
70+
71+
<pre class="output">
72+
[1] 1.78889 -1.48820 0.05878 1.58578 -1.25415 -1.72598 0.57821 0.24119
73+
[9] 1.66974
74+
</pre>
75+
76+
77+
78+
{% highlight r %}
79+
ret_simple(y)
80+
{% endhighlight %}
81+
82+
83+
84+
<pre class="output">
85+
[1] NA 0.0065489 0.0004482 -0.0063214 -0.0041354 0.0118706
86+
[7] 0.0114866 -0.0075892 0.0054882 -0.0093787
87+
</pre>
88+

0 commit comments

Comments
 (0)