[Rcpp-devel] Accessing/copying rows/colums of matrices

Michael Braun braunm at MIT.EDU
Thu Apr 7 06:31:53 CEST 2011


Hello, again.

A couple of weeks ago, I posted a problem in compiling and running one of the examples from the Rcpp-quickref pdf.  There were two suggestions:  one was to upgrade my compiler, and the other was to find Linux on "another account somewhere at my large-enough university."  I have done both (now using g++ 4.4 on Red Hat Enterprise Linux), and have the same problem. (The version of Rcpp is 0.9.3)

Consider the following code, executed using the inline package:

library(Rcpp)
library(inline)

body <- '

   Rcpp::NumericMatrix xx(3,2);
   int i;
   int xsize = xx.nrow() * xx.ncol();
  
   for (i = 0; i < xsize; i++) {
     xx[i] = pow(i,2);
   }

     Rcpp::NumericMatrix::Column z1 = xx(_,0);
     Rcpp::NumericVector z2 = xx(_,1);

   return (Rcpp::wrap(xx));
'

func <- cxxfunction(body=body,
                    plugin="Rcpp",
                    verbose=TRUE
                    )

Dirk ran this on his Debian system, and I can confirm that this works fine on my Red Hat system.  No problems so far.


Now, what if I want to take this code into production, and don't want to use the inline package?  The file t2.cpp is:

#include <Rcpp.h>

 RcppExport SEXP test() {
  
BEGIN_RCPP

   Rcpp::NumericMatrix xx(3,2);
   int i;
   int xsize = xx.nrow() * xx.ncol();
  
   for (i = 0; i < xsize; i++) {
     xx[i] = pow(i,2);
   }

     Rcpp::NumericMatrix::Column z1 = xx(_,0);
     Rcpp::NumericVector z2 = xx(_,1);

   return (Rcpp::wrap(xx));

END_RCPP
  
 } 

When I try to compile this, I get

equity> g++44 -m64 -O0 -fPIC   -Wall -I/usr/local/include -I/usr/include/R -I/usr/lib64/R/library/Rcpp/include -c t2.cpp -o t2.o

t2.cpp: In function ‘SEXPREC* test()’:
t2.cpp:17: error: ‘_’ was not declared in this scope
t2.cpp:17: warning: unused variable ‘z1’
t2.cpp:18: warning: unused variable ‘z2’

I get this kind of error on both Mac and Linux, using g++ or the Intel compiler.  So I hope we can agree that this is not a compiler issue and it is not a Mac issue.  There's something about this underscore that is messing things up.  (Incidentally, this fails on the Mac when using inline as well, but that appears to be yet another issue).

I tried to make this example as small and replicable as possible.  If you need me to present this problem another way, please let me know.

Thanks,

Michael







-------------------------------------------
Michael Braun
Homer A. Burnell (1928) Career Development Professor, 
	and Assistant Professor of Management Science (Marketing Group)
MIT Sloan School of Management
100 Main St.., E62-535
Cambridge, MA 02139
braunm at mit.edu
617-253-3436



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110407/6e2b4737/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1844 bytes
Desc: not available
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110407/6e2b4737/attachment-0001.bin>


More information about the Rcpp-devel mailing list