or does it matter? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121210/7315e444/attachment.html>
[Rcpp-devel] For a Matrix A, is A(i, j) or A[i, j] preferred as an accessor for an element
5 messages · Romain Francois, Douglas Bates, Dirk Eddelbuettel
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. Romain
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
On Mon, Dec 10, 2012 at 10:38 AM, Romain Francois
<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. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121210/c65901d7/attachment.html>
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
On 10 December 2012 at 10:45, Douglas Bates wrote:
| On Mon, Dec 10, 2012 at 10:38 AM, Romain Francois <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.
No, it's worse, It is valid:
R> cppFunction("double val(Rcpp::NumericMatrix X) { return X[1,2]; }")
R> val(matrix(1:9,3,3))
[1] 3
R>
| 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.
.. but it is possibly the worst gotcha we can have in C++ and matrices as the
comma _is_ in fact a valid operator coming from C --- here though A[i,j] simply
collapses to A[j] with i being ignored as comma sequences the statements
around it.
Dirk
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com