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

Søren Højsgaard sorenh at math.aau.dk
Sat Jan 19 09:45:23 CET 2013


Dear Dirk

Thanks for your reply. Seems to me that there is an attr() method for for setting and retrieving attributes - but this is not available for sparse matrices. For sparse matrices there seems to be a slot() method that will retrieve but not set slots?

Regards
Søren

-----Original Message-----
From: Dirk Eddelbuettel [mailto:edd at debian.org] 
Sent: 19. januar 2013 00:18
To: Søren Højsgaard
Cc: rcpp-devel at lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] RcppEigen: Getting and setting dimnames of sparse matrix


Hi Søren,

On 18 January 2013 at 23:07, Søren Højsgaard wrote:
| 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. 

"It should".  

I have not worked all that much with sparse matrices, but dimnames etc are merely attributes at the R level.  And we read and write these just as easily at the C++ level. 

Have a look at few of the recent posts at   http://gallery.rcpp.org  
in particular 

  http://gallery.rcpp.org/articles/getting-attributes-for-xts-example

and

  http://gallery.rcpp.org/articles/creating-xts-from-c%2B%2B

where I, respectively, "get" and "set" the attributes that make an xts
object: dimnames, an index (itself with class and time zone attributes) and timezone and class info.

For sparse matrices the mechanics may be slighty different due to the S4-ness of things but the rest should work.  I put a feeble attempt at working with sparse matrices here:

  http://gallery.rcpp.org/articles/armadillo-sparse-matrix/

Maybe we can do something similar with richer 'matrix factory' functions for Eigen?

Dirk

| 
| 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
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-deve
| l

--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list