[Rcpp-devel] Help with SubMatrix test case

Dirk Eddelbuettel edd at debian.org
Sat Dec 4 05:16:04 CET 2010


On 3 December 2010 at 21:56, Dirk Eddelbuettel wrote:
| 
| On 3 December 2010 at 19:45, Christian Gunning wrote:
| | In this test case, I expected yy to be a proper subset of xx.  I
| | confess that I don't understand the results at all, but I'm assuming
| | that the Row/Column exchange is involved. Am I missing anything?
| | 
| | thanks,
| | christian
| | 
| | src <- '
| |      NumericMatrix xx(4, 5);
| |      xx(0,0) = 3;
| |      xx(0,1) = 4;
| |      xx(0,2) = 5;
| |        Rf_PrintValue(xx);
| |      xx(1,_) = xx(0,_);
| |        Rf_PrintValue(xx);
| |      xx(_,3) = xx(_,2);
| |        Rf_PrintValue(xx);
| |      SubMatrix<REALSXP> yy = xx( Range(0,2), Range(0,3) ) ;
| |        Rf_PrintValue(wrap(yy));
| |      return(wrap(yy));
| |  '
| | 
| | myfun <- cxxfunction( , src, plugin='Rcpp')
| | 
| | aa = myfun()
| 
| That is pretty new code.  Let's give it a while.  I get
| 
| R> library(Rcpp)
| R> library(inline)
| R> src <- '
|      NumericMatrix xx(4, 5);
| + +      xx(0,0) = 3;
| +      xx(0,1) = 4;
| +      xx(0,2) = 5;
| +        Rf_PrintValue(xx);
| +      xx(1,_) = xx(0,_);
| +        Rf_PrintValue(xx);
| +      xx(_,3) = xx(_,2);
| +        Rf_PrintValue(xx);
| +      SubMatrix<REALSXP> yy = xx( Range(0,2), Range(0,3) ) ;
| +        Rf_PrintValue(wrap(yy));
| +      return(wrap(yy));
| +  '
| R> myfun <- cxxfunction( , src, plugin='Rcpp')
| R> aa = myfun()
|      [,1] [,2] [,3] [,4] [,5]
| [1,]    3    4    5    0    0
| [2,]    0    0    0    0    0
| [3,]    0    0    0    0    0
| [4,]    0    0    0    0    0
|      [,1] [,2] [,3] [,4] [,5]
| [1,]    3    4    5    0    0
| [2,]    3    4    5    0    0
| [3,]    0    0    0    0    0
| [4,]    0    0    0    0    0
|      [,1] [,2] [,3] [,4] [,5]
| [1,]    3    4    5    5    0
| [2,]    3    4    5    5    0
| [3,]    0    0    0    0    0
| [4,]    0    0    0    0    0
|      [,1] [,2] [,3] [,4]
| [1,]    3    4    0    0
| [2,]    3    0    0    0
| [3,]    0    0    5    0
| R> 
| 
| The first three look correct to me:
| i)   assign 3, 4, 5
| ii)  copy row 0 as 1
| iii) copy col 3 to 4
| 
| but I am also a little puzzled by the last submatrix.  
| 
| I'm sure Romain will weigh in in a few hours.

In the mean time, we can of course use the trusted Armadillo library and the
RcppArmadillo converters:

R> src2 <- '
+      NumericMatrix xx(4, 5);
+      xx(0,0) = 3;
+      xx(0,1) = 4;
+      xx(0,2) = 5;
+      xx(1,_) = xx(0,_);
+      xx(_,3) = xx(_,2);
+      arma::mat mm( xx.begin(), 4, 5, false );
+      arma::mat sm = mm.submat(0,0,2,3);
+      return(wrap(sm));
+  '
R> myfun2 <- cxxfunction( , src2, plugin='RcppArmadillo')
R> aa2 <- myfun2()
R> aa2
     [,1] [,2] [,3] [,4]
[1,]    3    4    5    5
[2,]    3    4    5    5
[3,]    0    0    0    0
R> 

That looks more like it.

Dirk

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


More information about the Rcpp-devel mailing list