Skip to content

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

4 messages · Michael Braun, Dirk Eddelbuettel

#
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>
#
On 7 April 2011 at 00:31, Michael Braun wrote:
| 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

(Actually Ubuntu, for what it's worth.)

| my Red Hat system.  No problems so far.

Thank you for confirming what I suspected weeks ago: your Mac setup was at
fault, possibly by having too old a compiler.  No issue here with Rcpp.
 
| 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).

Sorry, but that is your error. The _ is only defined in the Rcpp namespace,
and inline uses a global 'using namespace Rcpp'. 

It so happens that I do not recommend examples or usage with global 'using
namespace ...'  but I have an extremely prolific coauthor who really likes
skipping the Rcpp:: prefix. ;-)  

That is what killed you here.  Either add

   using namespace Rcpp;

at the top of your file (and, as an aside, verbose=TRUE clearly shows you
that in the code built via inline -- we don't hide anything or use magic
obfuscation pixie dust), or equally switch to

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

and your 'production code' will compile:

edd at max:/tmp$ grep _ michael4.cpp
   BEGIN_RCPP
   Rcpp::NumericMatrix::Column z1 = xx(Rcpp::_,0);
   Rcpp::NumericVector z2 = xx(Rcpp::_,1);
   END_RCPP
edd at max:/tmp$ g++-4.5 -I/usr/share/R/include   -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O3 -Wall -pipe -pedantic -Wno-variadic-macros  -c michael4.cpp -o michael4.o
edd at max:/tmp$ ls -l michael4.*
-rw-r--r-- 1 edd edd    365 2011-04-06 23:45 michael4.cpp
-rw-r--r-- 1 edd edd 169584 2011-04-06 23:54 michael4.o
edd at max:/tmp$

| 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.

No, that was helpful enough -- thank you.

C++ can be messy, and the error messages are not always helpful. We all have
been tripped by things like this.  It is with experience that you start to
get a better feel for this.  You are on the right track -- keep going!

Cheers, Dirk


| 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
| 
| 
| 
| xapplication/pkcs7-signatu [Click mouse-2 to save to a file]
| 
| ----------------------------------------------------------------------
| _______________________________________________
| 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-devel
#
Yes!  Adding the using namespace Rcpp line did the trick.  Thank you.

Might I suggest adding this trick to the documentation?  Many (most? just me?) R users are simply looking for ways to do statistics faster, and just don't have the same skills or experience as professional software developers.  Certainly we are all appreciative of your contribution to R, and how it makes our lives easier.  By placing tricks like this in accessible and clearly written documentation, it would lower the barrier to even more researchers benefitting from your hard work.

Thanks again, and I did learn a lot from the process.
On Apr 7, 2011, at 12:59 AM, Dirk Eddelbuettel wrote:

            
-------------- 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/beb88012/attachment.bin>
#
On 7 April 2011 at 01:25, Michael Braun wrote:
| Yes!  Adding the using namespace Rcpp line did the trick.  Thank you.

Cool!

| Might I suggest adding this trick to the documentation?  Many (most? just me?) R users are simply looking for ways to do statistics faster, and just don't have the same skills or experience as professional software developers.  Certainly we are all appreciative of your contribution to R, and how it makes our lives easier.  By placing tricks like this in accessible and clearly written documentation, it would lower the barrier to even more researchers benefitting from your hard work.

Patch welcome. "The documentation" is several vignettes, lots of help files,
some webpages.  Where were you looking, ie where would you like to see it?
 
| Thanks again, and I did learn a lot from the process.

Pleasure!

Dirk