Skip to content

Sparse matrix automatic conversion in RcppArmadillo

abhinavb05 edited this page Mar 25, 2017 · 18 revisions

Background

The Matrix type is one of the most important data types in the R language. In the package Matrix, there are several sparse matrix types such as like dgCMatrix, ngCMatrix, nsCMatrix and ntCMatrix. When passing a sparse matrix from the R side to the C++ side, currently only dgCMatrix is supported in RcppArmadillo package (see related issues #17 and #114 for more details).

The purpose for this project is to develop automatic conversion in RcppArmadillo for more sparse matrix types.

Details of your coding project

  • First, evaluating which sparse matrix types can be handled by the Armadillo library.
  • Second, developing different conversion functions for sparse matrix.
  • Third, providing warning message when automatic conversion is not possible.

Expected impact

The successful completion of this project will provide a seamless integration between R matrix and Armadillo matrix library.

Mentors

Qiang Kou, Dirk Eddelbuettel and other Rcpp team members

Tests

The sparsematrix in the Matrix package was defined as a S4 class. We can print out its contents like below:

> library(Matrix)
> x = rsparsematrix(10, 10, .05)
> str(x)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:5] 4 6 7 1 1
  ..@ p       : int [1:11] 0 1 2 2 2 2 3 3 4 5 ...
  ..@ Dim     : int [1:2] 10 10
  ..@ Dimnames:List of 2
  .. ..$ : NULL
  .. ..$ : NULL
  ..@ x       : num [1:5] -1.3 -1.1 0.37 1.9 -1.1
  ..@ factors : list()

Please write a C++ function to print out the content. The expected result should be similar to

> Rcpp::sourceCpp("test.cpp")
> print_matrix_cpp(x)
Type:	dgCMatrix
Dim:	10 10
length of i:	5
value of i:	4 6 7 1 1 
length of p:	11
value of p:	0 1 2 2 2 2 3 3 4 5 5 
length of x:	5
value of x:	-1.3 -1.1 0.37 1.9 -1.1 

Solutions of tests

Students, please post a link to your test results here.

Clone this wiki locally