<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/rcppalgorithm/"></div></div><div><a data-url="http://www.r-bloggers.com/rcppalgorithm/" data-counturl="http://www.r-bloggers.com/rcppalgorithm/" data-count="horizontal" data-text="Rcpp::algorithm" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://gallery.rcpp.org//articles/rcpp-algorithm/"> Rcpp Gallery</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Introduction</h2>
<p>A while back I saw a post on StackOverflow where the user was trying to use <code>Rcpp::sugar::sum()</code> on an <code>RcppParallel::RVector</code>.<br>
Obviously this doesn’t work and it raised the question “Why doesn’t something more generic exist to provide functions with R<br>
semantics that can be used on arbitrary data structures?” As a result, I set out to create a set of such functions following<br>
the pattern of <code>std::algorithm</code> in <code>Rcpp::algorithm</code>.</p>
<h2>Rcpp::algorithm</h2>
<p>Currently <code>Rcpp::algorithm</code> contains only a few simple functions, but if they are found to be useful, more will be added.<br>
Examples of using the currently implemented iterator-based functions are below.</p>
<h3>sum, sum_nona, prod, and prod_nona</h3>
<figure>
<pre><code data-lang="cpp"><span>#include &lt;Rcpp.h>
</span>
<span>using</span> <span>namespace</span> <span>Rcpp</span><span>;</span>

<span>// [[Rcpp::export]]
</span><span>double</span> <span>sum_of_matrix_row</span><span>(</span><span>NumericMatrix</span> <span>m</span><span>,</span> <span>int</span> <span>row</span><span>)</span> <span>{</span>
    <span>NumericMatrix</span><span>::</span><span>Row</span> <span>r</span> <span>=</span> <span>m</span><span>.</span><span>row</span><span>(</span><span>row</span><span>);</span>

    <span>return</span> <span>algorithm</span><span>::</span><span>sum</span><span>(</span><span>r</span><span>.</span><span>begin</span><span>(),</span> <span>r</span><span>.</span><span>end</span><span>());</span>
<span>}</span></code></pre>
</figure>
<h3>min, max, and mean</h3>
<figure>
<pre><code data-lang="cpp"><span>#include &lt;Rcpp.h>
</span>
<span>using</span> <span>namespace</span> <span>Rcpp</span><span>;</span>

<span>// [[Rcpp::export]]
</span><span>double</span> <span>mean_of_matrix_row</span><span>(</span><span>NumericMatrix</span> <span>m</span><span>,</span> <span>int</span> <span>row</span><span>)</span> <span>{</span>
    <span>NumericMatrix</span><span>::</span><span>Row</span> <span>r</span> <span>=</span> <span>m</span><span>.</span><span>row</span><span>(</span><span>row</span><span>);</span>

    <span>return</span> <span>algorithm</span><span>::</span><span>mean</span><span>(</span><span>r</span><span>.</span><span>begin</span><span>(),</span> <span>r</span><span>.</span><span>end</span><span>());</span>
<span>}</span></code></pre>
</figure>
<h3>log, exp, and sqrt</h3>
<figure>
<pre><code data-lang="cpp"><span>#include &lt;Rcpp.h>
</span>
<span>using</span> <span>namespace</span> <span>Rcpp</span><span>;</span>

<span>// [[Rcpp::export]]
</span><span>NumericVector</span> <span>log_of_matrix_row</span><span>(</span><span>NumericMatrix</span> <span>m</span><span>,</span> <span>int</span> <span>row</span><span>)</span> <span>{</span>
    <span>NumericMatrix</span><span>::</span><span>Row</span> <span>r</span> <span>=</span> <span>m</span><span>.</span><span>row</span><span>(</span><span>row</span><span>);</span>

    <span>NumericVector</span> <span>retval</span><span>(</span><span>m</span><span>.</span><span>cols</span><span>());</span>
    <span>algorithm</span><span>::</span><span>log</span><span>(</span><span>r</span><span>.</span><span>begin</span><span>(),</span> <span>r</span><span>.</span><span>end</span><span>(),</span> <span>retval</span><span>.</span><span>begin</span><span>());</span>

    <span>return</span> <span>retval</span><span>;</span>
<span>}</span></code></pre>
</figure>
<h2>Additional Benefits</h2>
<p>Through the coding of these simple “algorithms”, a few needs arose.</p>
<p>First, the ability to deduce the appropriate <code>C</code> numeric type<br>
given an <code>Rcpp</code> iterator was necessary. This gave birth to the <code>Rcpp::algorithm::helpers::decays_to_ctype</code> and<br>
<code>Rcpp::algorithm::helpers::ctype</code> type traits. Given a type, these allow you to determine whether it can be cast to a <code>C</code> numeric<br>
type and which type that would be.</p>
<p>Second, the need arose for more information about <code>R</code> types. This gave birth to the <code>Rcpp::algorithm::helpers::rtype</code> traits. These<br>
are defined as follows:</p>
<figure>
<pre><code data-lang="cpp"><span>template</span><span>&lt;</span> <span>typename</span> <span>T</span> <span>></span>
<span>struct</span> <span>rtype_helper</span> <span>{};</span>

<span>template</span><span>&lt;></span>
<span>struct</span> <span>rtype_helper</span><span>&lt;</span> <span>double</span> <span>></span> <span>{</span>
    <span>typedef</span> <span>double</span> <span>type</span><span>;</span>
    <span>static</span> <span>RCPP_CONSTEXPR</span> <span>int</span> <span>RTYPE</span> <span>=</span> <span>REALSXP</span><span>;</span>
    <span>static</span> <span>inline</span> <span>double</span> <span>NA</span><span>()</span> <span>{</span> <span>return</span> <span>NA_REAL</span><span>;</span> <span>}</span>
    <span>static</span> <span>inline</span> <span>RCPP_CONSTEXPR</span> <span>double</span> <span>ZERO</span><span>()</span> <span>{</span> <span>return</span> <span>0.0</span><span>;</span> <span>}</span>
    <span>static</span> <span>inline</span> <span>RCPP_CONSTEXPR</span> <span>double</span> <span>ONE</span><span>()</span> <span>{</span> <span>return</span> <span>1.0</span><span>;</span> <span>}</span>
<span>};</span>

<span>template</span><span>&lt;></span>
<span>struct</span> <span>rtype_helper</span><span>&lt;</span> <span>int</span> <span>></span> <span>{</span>
    <span>typedef</span> <span>int</span> <span>type</span><span>;</span>
    <span>static</span> <span>RCPP_CONSTEXPR</span> <span>int</span> <span>RTYPE</span> <span>=</span> <span>INTSXP</span><span>;</span>
    <span>static</span> <span>inline</span> <span>int</span> <span>NA</span><span>()</span> <span>{</span> <span>return</span> <span>NA_INTEGER</span><span>;</span> <span>}</span>
    <span>static</span> <span>inline</span> <span>RCPP_CONSTEXPR</span> <span>int</span> <span>ZERO</span><span>()</span> <span>{</span> <span>return</span> <span>0</span><span>;</span> <span>}</span>
    <span>static</span> <span>inline</span> <span>RCPP_CONSTEXPR</span> <span>int</span> <span>ONE</span><span>()</span> <span>{</span> <span>return</span> <span>1</span><span>;</span> <span>}</span>
<span>};</span>

<span>template</span><span>&lt;</span> <span>typename</span> <span>T</span> <span>></span>
<span>struct</span> <span>rtype</span> <span>{</span>
    <span>typedef</span> <span>typename</span> <span>rtype_helper</span><span>&lt;</span> <span>typename</span> <span>ctype</span><span>&lt;</span> <span>T</span> <span>>::</span><span>type</span> <span>>::</span><span>type</span> <span>type</span><span>;</span>
    <span>typedef</span> <span>rtype_helper</span><span>&lt;</span> <span>typename</span> <span>ctype</span><span>&lt;</span> <span>T</span> <span>>::</span><span>type</span> <span>></span> <span>helper_type</span><span>;</span>
    <span>static</span> <span>RCPP_CONSTEXPR</span> <span>int</span> <span>RTYPE</span> <span>=</span> <span>helper_type</span><span>::</span><span>RTYPE</span><span>;</span>
    <span>static</span> <span>inline</span> <span>T</span> <span>NA</span><span>()</span> <span>{</span> <span>return</span> <span>helper_type</span><span>::</span><span>NA</span><span>();</span> <span>}</span>
    <span>static</span> <span>inline</span> <span>RCPP_CONSTEXPR</span> <span>T</span> <span>ZERO</span><span>()</span> <span>{</span> <span>return</span> <span>helper_type</span><span>::</span><span>ZERO</span><span>();</span> <span>}</span>
    <span>static</span> <span>inline</span> <span>RCPP_CONSTEXPR</span> <span>T</span> <span>ONE</span><span>()</span> <span>{</span> <span>return</span> <span>helper_type</span><span>::</span><span>ONE</span><span>();</span> <span>}</span>
<span>};</span></code></pre>
</figure>
<p>These additional benefits may actually prove more useful than the algorithms themselves. Only time will tell.</p>
<h2>Wrapping Up</h2>
<p>There are now some simple iterator-based algorithms that can be used with any structure that supports iterators. They apply the same semantics<br>
as the analogous <code>Rcpp::sugar</code> functions but give us more flexibility in their usage. If you find these to be useful, feel free to request more.</p>
<p><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/rcpp-gallery/~4/pau5P6c8RvY"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/rcppalgorithm/"></div></div><div><a data-url="http://www.r-bloggers.com/rcppalgorithm/" data-counturl="http://www.r-bloggers.com/rcppalgorithm/" data-count="vertical" data-text="Rcpp::algorithm" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://gallery.rcpp.org//articles/rcpp-algorithm/"> Rcpp Gallery</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/8o2x3GS5UXA"></a></div>