[Rcpp-devel] RcppEigen: Getting and setting dimnames of sparse matrix

Søren Højsgaard sorenh at math.aau.dk
Sat Jan 19 00:07:02 CET 2013


Dear all,
I put a sparse "dgCMatrix" into RcppEigen code as a sparse matrix. In Rcpp I create a new matrix of same dimensions as the input matrix. I would like to copy the dimnames from the input matrix to the output matrix. Can this be done? (It is essential that I can stick to working with sparse matrices). I know that I can set the dimensions on the R-side, but that involves AFAIK copying the object which takes time. 

Example:

src<-'
using namespace Rcpp;
typedef Eigen::SparseMatrix<double> SpMat;
SpMat   X(as<SpMat>(XX_));
return(wrap(X));
'

names_ <- cxxfunction(signature(XX_="matrix"), plugin="RcppEigen",
	body=src)

ugM	<- new("dgCMatrix"
    , i = c(1L, 0L, 2L, 1L)
    , p = c(0L, 1L, 3L, 4L)
    , Dim = c(3L, 3L)
    , Dimnames = list(c("a", "b", "c"), c("a", "b", "c"))
    , x = c(1, 1, 1, 1)
    , factors = list())

> ugM
3 x 3 sparse Matrix of class "dgCMatrix"
  a b c
a . 1 .
b 1 . 1
c . 1 .
> 
> names_(ugM)
3 x 3 sparse Matrix of class "dgCMatrix"
          
[1,] . 1 .
[2,] 1 . 1
[3,] . 1 .

Any advice? Thanks in advance
Søren


More information about the Rcpp-devel mailing list