Dirk Eddelbuettel and Binxiang Ni — written Aug 5, 2013 — updated Oct 18, 2018 — source

An earlier article discussed
sparse matrix conversion
but stopped short of showing how to create custom `as<>()`

and `wrap()`

methods
or functions. This post started to close this gap.

We will again look at sparse matrices from the
Matrix package for R, as well as
the `SpMat`

class from Armadillo.
At least for now we will limit outselves to the
case of `double`

element types. These uses the `sp_mat`

typedef which will be
our basic type for sparse matrices at the C++ level.

*Nota bene: At the time of the update of the post, very similar
code (by Romain) has just been added to the SVN repo for
RcppArmadillo; it should appear in the next regular CRAN
release. Because we cannot redefine method with the same signature,
we renamed the code presented here as_<>() and wrap_(). Of
course, for conversion not already present in the package, names
without underscores should be used instead. My thanks to Romain for
improving over the initial versions I wrote in the first version of
this post.*

*Nota bene 2: As of RcppArmadillo release 0.3.920.1 (based on Armadillo 3.920.1)
there is also a new constructor taking vectors rowind, colptr and values.*

*Nota bene 3:
Since RcppArmadillo release 0.7.960.1.1, a fulle eleven types of sparse matrices on
the R side are supported. You can pass these directly to RcppArmadillo, and each will be
converted to an arma::sp_mat by a corresponding function. More details are in
the sparse Matrix vignette.*

First, we look at the `as`

method.

If you are interested in the as<>() for more types of sparse matrix, you might want to take a look at the source code

Next, we look at the corresponding `wrap()`

method.
Here the `sp_mat`

Armadillo object is wrapped into a `dgCMatrix`

.

We can now illustrate this with a simple example. *Note that the
compiler will use the methods as<>() and wrap() from the package
rather than the ones depicted here. However, the ones shown here compile as
well and are functionally identical.*

First, we create a sparse matrix. We then the function we just showed to
to a minimal (and boring) transformation: we double the values of the matrix.
The key really in the seamless passage of matrix `A`

from R down to the C++
code where it is accessed as `m`

, and the return of the new matrix `n`

which
becomes `B`

at the R level.

8 x 10 sparse Matrix of class "dgCMatrix" [1,] . 7 . . . . . . . . [2,] . . . . . . . . . . [3,] . . . . . . . . 14 . [4,] . . . . . 21 . . . . [5,] . . . . . . 28 . . . [6,] . . . . . . . 35 . . [7,] . . . . . . . . 42 . [8,] . . . . . . . . . 49

[matrix size: 8x10; n_nonzero: 7; density: 8.75%] (0, 1) 7.0000 (3, 5) 21.0000 (4, 6) 28.0000 (5, 7) 35.0000 (2, 8) 14.0000 (6, 8) 42.0000 (7, 9) 49.0000

8 x 10 sparse Matrix of class "dgCMatrix" [1,] . 14 . . . . . . . . [2,] . . . . . . . . . . [3,] . . . . . . . . 28 . [4,] . . . . . 42 . . . . [5,] . . . . . . 56 . . . [6,] . . . . . . . 70 . . [7,] . . . . . . . . 84 . [8,] . . . . . . . . . 98

[1] TRUETweet

- Benchmarking Rcpp code with RcppClock — Zach DeBruine
- Simulation Smoother using RcppArmadillo — Tomasz Woźniak
- Constructing a Sparse Matrix Class in Rcpp — Zach DeBruine and Dirk Eddelbuettel