Dirk Eddelbuettel — Jan 14, 2013 | source

Boost is, to quote the quote by Sutter and Alexandrescu
which adornes the Boost website, *…one of the most highly
regarded and expertly designed C++ library projects in the world*.

The impact of Boost on C++ cannot be overstated. Boost is, at its core, a collection of thoroughly designed and peer-reviewed libraries. Some of these have been included into the new C++11 standard (see our intro post on C++11) as for example lambda functions which we illustrated in another post on C++11.

Boost is mostly implemented using templates. That means headers files only, and compile-time – but not linking. Which is perfect for example posts like these.

Many, many Boost libraries are useful, and we could fill a series of posts. Here, as an introduction, we going to use two simple functions from the Boost.Math library to compute greatest common denominator and least common multiple.

I should note that I write this post on a machine with Boost headers
in a standard system location. *So stuff just works.* If you have to install Boost from source,
and into a non-standard location, you may need to add a `-I`

flag, not unlike how added
the C++11 flag in this post .

```
#include <Rcpp.h>
#include <boost/math/common_factor.hpp> // one file, automatically found here
using namespace Rcpp;
// [[Rcpp::export]]
int computeGCD(int a, int b) {
return boost::math::gcd(a, b);
}
// [[Rcpp::export]]
int computeLCM(int a, int b) {
return boost::math::lcm(a, b);
}
```

We can test these:

```
a <- 6
b <- 15
cat( c(computeGCD(a,b), computeLCM(a,b)), "\n")
```

3 30

```
a <- 96
b <- 484
cat( c(computeGCD(a,b), computeLCM(a,b)), "\n")
```

4 11616

And as kindly suggested and submitted by Kohske Takahashi, we can also benchmark this against an R solution using the numbers package:

```
library(rbenchmark)
library(numbers)
a <- 962
b <- 4842
res <- benchmark(r1 = c(computeGCD(a,b), computeLCM(a,b)),
r2 = c(GCD(a,b), LCM(a,b)),
replications = 5000)
print(res[,1:4])
```

test replications elapsed relative 1 r1 5000 0.054 1.000 2 r2 5000 0.421 7.796

This shows a nice performance gain.

Tweet- Call Python from R through Rcpp — Wush Wu
- Using iterators for sparse vectors and matrices — Soren Hojsgaard and Doug Bates
- A simple array class with specialized linear algebra routines — Fabian Scheipl