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

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


On Sat, Jan 19, 2013 at 10:38 AM, Douglas Bates <bates at stat.wisc.edu> wrote:

>
On thinking about this a bit more, the dimnames should be cloned before
being assigned, otherwise you end up with two references to the same
storage.  And once you try to clone you get into the SlotProxy area where
you need to know what you are cloning.  One way of doing this is shown in
the enclosed.  An alternative is

Xout.slot("Dimnames") = clone(as<SEXP>((Xin.slot("Dimnames"))));
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130119/9f822897/attachment-0001.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") = clone(List(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.392   0.496  10.918 


More information about the Rcpp-devel mailing list