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

Douglas Bates bates at stat.wisc.edu
Sat Jan 19 16:38:19 CET 2013


The Eigen sparse matrix only contains the numeric information, it doesn't
contain information like Dimnames that are specific to the R structure.  If
you want to access the slots or set their values it is easiest to do that
with the Rcpp::S4 class object.

Of course it helps to use R's str function first to ensure that you know
what the structure actually is.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130119/c9de6d35/attachment.html>
-------------- next part --------------

R version 2.15.2 (2012-10-26) -- "Trick or Treat"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(Matrix)
Loading required package: lattice
> library(RcppEigen)
Loading required package: Rcpp
> library(inline)
> 
> 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 .
> str(ugM)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:4] 1 0 2 1
  ..@ p       : int [1:4] 0 1 3 4
  ..@ Dim     : int [1:2] 3 3
  ..@ Dimnames:List of 2
  .. ..$ : chr [1:3] "a" "b" "c"
  .. ..$ : chr [1:3] "a" "b" "c"
  ..@ x       : num [1:4] 1 1 1 1
  ..@ factors : list()
> 
> src <- '
+ typedef Eigen::SparseMatrix<double> SpMat;
+ S4    Xin(XX_);
+ SpMat X(as<SpMat>(XX_));
+ S4    Xout(wrap(X));
+ Xout.slot("Dimnames") = Xin.slot("Dimnames");
+ return(Xout);
+ '
> 
> names_ <- cxxfunction(signature(XX_="dsCMatrix"), plugin="RcppEigen",
+         body=src)
> 
> names_(ugM)
3 x 3 sparse Matrix of class "dgCMatrix"
  a b c
a . 1 .
b 1 . 1
c . 1 .
> 
> 
> proc.time()
   user  system elapsed 
 10.356   0.436  10.859 


More information about the Rcpp-devel mailing list