Blog Archives

Call Python from R through Rcpp

April 5, 2014
By
Call Python from R through Rcpp

Introduction This post provides a brief introduction to calling Python from R through Rcpp. The official Python documentation explains how to embed python into C/C++ applications. Moreover, the Boost.Python library provides seamless interoperability between C++ and the Python programming language. Similarlly, Rcpp provides interoperability between C++ and R. Therefore, it is not hard to call Python from R through Rcpp and Boost.Python. Although there...

Read more »

Using iterators for sparse vectors and matrices

March 31, 2014
By
Using iterators for sparse vectors and matrices

Iterating over a sparse vector Consider the following vector: idx1 <- c(2L, 0L, 4L, 0L, 7L) A sparse representation of this vector will tell that at entries 1,3,5 (or at entries 0,2,4 if we are 0-based) we will find the values 2,4,7. Using Eigen via RcppEigen we can obtain the coercion with .sparseView(). We can iterate over all elements (including the zeros) in a sparse vector...

Read more »

A simple array class with specialized linear algebra routines

March 20, 2014
By
A simple array class with specialized linear algebra routines

Currie, Durban and Eilers write: Data with an array structure are common in statistics, and the design or regression matrix for analysis of such data can often be written as a Kronecker product. Factorial designs, contingency tables and smoothing of data on multidimensional grids are three such general classes of data and models. In such a setting, we develop an arithmetic...

Read more »

Dynamic dispatch for sparse matrices

March 19, 2014
By
Dynamic dispatch for sparse matrices

We want to do matrix multiplication for 3 cases: dense times dense sparse times dense for sparse matrices of class dgCMatrix sparse times dense for sparse matrices of class indMatrix, using R’s Matrix package for sparse matrices in R and RcppArmadillo for C++ linear algebra: // ] #include <RcppArmadillo.h> using namespace Rcpp ; arma::mat matmult_sp(const arma::sp_mat X, const arma::mat Y){ ...

Read more »

Vector Subsetting in Rcpp

March 15, 2014
By
Vector Subsetting in Rcpp

Rcpp 0.11.1 has introduced flexible subsetting for Rcpp vectors. Subsetting is implemented for the Rcpp vector types through the [ operator, and intends to mimic R’s [ operator for most cases. We diverge from R’s subsetting semantics in a few important ways: For integer and numeric vectors, 0-based indexing is performed, rather than 1-based indexing, for subsets. ...

Read more »

Condorcet Voting with Rcpp

March 13, 2014
By
Condorcet Voting with Rcpp

There is a lot of literature and debate on how to rank candidates under preferential voting systems. Two of the methods used to determine winners are those based on some form of Borda count and those based on some form of Condorcet method. Many students of politics and voting systems prefer Condorcet methods to Borda ones for its stronger theoretical qualities. However, while...

Read more »

Convex Hull of Polygon using Boost.Geometry

February 23, 2014
By
Convex Hull of Polygon using Boost.Geometry

Rcpp can be used to convert basic R data types to and from Boost.Geometry models. In this example we take a matrix of 2d-points and convert it into a Boost.Geometry polygon. We then compute the convex hull of this polygon using a Boost.Geometry function boost::geometry::convex_hull. The convex hull is then converted back to an R matrix. The conversions to and...

Read more »

Detecting a Time Series Change Point

January 4, 2014
By
Detecting a Time Series Change Point

In this example we will detect the change point in a time series of counts using Bayesian methodology. A natural solution to this problem utilizes a Gibbs sampler. We’ll first implement the sampler in R naively, then create a vectorized R implementation, and lastly create an implementation of the sampler using Rcpp and RcppArmadillo. We will compare these implementations...

Read more »

Performance of the divide-and-conquer SVD algorithm

December 9, 2013
By
Performance of the divide-and-conquer SVD algorithm

The ubiquitous LAPACK library provides several implementations for the singular-value decomposition (SVD). We will illustrate possible speed gains from using the divide-and-conquer method by comparing it to the base case. #include <RcppArmadillo.h> // ] // ] arma::vec baseSVD(const arma::mat & X) { arma::mat U, V; arma::vec S; arma::svd(U, S, V, X, "standard"); ...

Read more »

Converting C code to C++ code: An example from plyr

December 2, 2013
By
Converting C code to C++ code: An example from plyr

The plyr package uses a couple of small C functions to optimise a number of particularly bad bottlenecks. Recently, two functions were converted to C++. This was mostly stimulated by a segmentation fault caused by some large inputs to the split_indices() function: rather than figuring out exactly what was going wrong with the complicated C code, it was...

Read more »