[Rcpp-devel] For a Matrix A, is A(i, j) or A[i, j] preferred as an accessor for an element

Romain Francois romain at r-enthusiasts.com
Mon Dec 10 17:50:14 CET 2012


Le 10/12/12 17:45, Douglas Bates a écrit :
> On Mon, Dec 10, 2012 at 10:38 AM, Romain Francois
> <romain at r-enthusiasts.com <mailto:romain at r-enthusiasts.com>> wrote:
>
>     Le 10/12/12 17:29, Douglas Bates a écrit :
>
>         or does it matter?
>
>
>     A[i,j] is wrong, not valid C or C++ code. so there is only one choice.
>
>
> Indeed.  Thanks.  Somehow I managed to convince myself that I had used
> A[i,j] at some point and the compiler accepted it.  Apparently my memory
> isn't what it used to be.

That is even worse than that. At least here, the compiler accepts it, 
and does something wrong with it:

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
double fun(NumericMatrix m){
   return m[5,0] ;
}

/*** R
   fun( diag(4) )
***/


doug.cpp: In function ‘double fun(Rcpp::NumericMatrix)’:
doug.cpp:7: warning: left-hand operand of comma has no effect
 > fun(diag(4))
[1] 1

So m[5,0] gets me m[0] ...

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com

blog:            http://romainfrancois.blog.free.fr
|- http://bit.ly/RE6sYH : OOP with Rcpp modules
`- http://bit.ly/Thw7IK : Rcpp modules more flexible



More information about the Rcpp-devel mailing list