Skip to content

[Rcpp-devel] List of matrices mimicking 3d array?

4 messages · Hadley Wickham, Dirk Eddelbuettel, Edward Wallace

#
Am I reading the Vector documentation correctly
(http://dirk.eddelbuettel.com/code/rcpp/html/classVector.html) in
interpreting that while you can create vectors with dimensions, that
there is no syntactic sugar for  multidimensional indexing?  I'm not
quite sure how to interpret method signatures like operator() (const
size_t &i, const size_t &j) const. I can guess that means you can
index by two integers - but I don't know what those two integers mean.

Hadley
#
On 4 January 2012 at 19:11, Hadley Wickham wrote:
| > Our arrays in Rcpp can be multidimensional just like they can in R. Here is a
| > line from one of the unit tests:
| >
| > ? return IntegerVector( Dimension( 2, 3, 4) ) ;
| >
| > which sets the dim attribute of a given vector to c(2,3,4) just like you
| > would in R. ?That should all work too but may have seen less exposure than
| > other parts of our code.
| 
| Am I reading the Vector documentation correctly
| (http://dirk.eddelbuettel.com/code/rcpp/html/classVector.html) in
| interpreting that while you can create vectors with dimensions, that
| there is no syntactic sugar for  multidimensional indexing?  I'm not

I think that is correct. We do have Range objects, and that is about it.
This is not yet a level comparable to R.

The unitTests/ directories, as always, has the largest collections of working
idioms to copy from.  runit.Vector.r is probably what you want to look at.

| quite sure how to interpret method signatures like operator() (const
| size_t &i, const size_t &j) const. I can guess that means you can
| index by two integers - but I don't know what those two integers mean.

Well if I had to guess I'd say row and column ...

Dirk

| 
| Hadley
| 
| -- 
| Assistant Professor / Dobelman Family Junior Chair
| Department of Statistics / Rice University
| http://had.co.nz/
#
In the end I wrote an obvious miniature sugary hack to convert 3d array
indices into numeric vector indices

int threeDIndex(int j, int k, int l, int J, int K, int L) {
    return j*K*L + k*L + l;
}

so that where one wants to call A[j,k,l], instead one
calls A[ threeDIndex(j,k,l,J,K,L) ], where dim(A) = (J,K,L). Presumably a
programmer with more experience than me could write a cpp method to do this
for NumericVectors with dimension attribute?

Edward
On Wednesday, January 4, 2012, Dirk Eddelbuettel wrote:

            

  
    
#
On 5 January 2012 at 10:00, Edward Wallace wrote:
| In the end I wrote an obvious miniature sugary hack to convert 3d array indices
| into numeric vector indices
| 
| int threeDIndex(int j, int k, int l, int J, int K, int L) {?
| ? ? return j*K*L + k*L + l;
| }
| 
| so that where one wants to call A[j,k,l], instead one calls?A[?threeDIndex

Just to restate something many of us tripped over at one point or other:
A[j,k,l] is NOT a valid C or C++ statement: the comma is an operator, so you
get something like A[l] here.

Multidimensional indexing _must_ use round parens:  A(j,k,l).

And I do think Rcpp support multi-dim arrays.  Whenever I do "real math work"
I tend to use RcppArmadillo though which has vectors, matrices, cubes
(3-dim), as well as fields (similar to lists or matlab cells).

Dirk

| (j,k,l,J,K,L) ], where dim(A) = (J,K,L). Presumably a programmer with more
| experience than me could write a cpp method to do this for NumericVectors with
| dimension attribute?
| 
| Edward
|
| On Wednesday, January 4, 2012, Dirk Eddelbuettel wrote:
| 
|
| On 4 January 2012 at 19:11, Hadley Wickham wrote:
|     | > Our arrays in Rcpp can be multidimensional just like they can in R.
|     Here is a
|     | > line from one of the unit tests:
|     | >
|     | > ? return IntegerVector( Dimension( 2, 3, 4) ) ;
|     | >
|     | > which sets the dim attribute of a given vector to c(2,3,4) just like
|     you
|     | > would in R. ?That should all work too but may have seen less exposure
|     than
|     | > other parts of our code.
|     |
|     | Am I reading the Vector documentation correctly
|     | (http://dirk.eddelbuettel.com/code/rcpp/html/classVector.html) in
|     | interpreting that while you can create vectors with dimensions, that
|     | there is no syntactic sugar for ?multidimensional indexing? ?I'm not
| 
|     I think that is correct. We do have Range objects, and that is about it.
|     This is not yet a level comparable to R.
| 
|     The unitTests/ directories, as always, has the largest collections of
|     working
|     idioms to copy from. ?runit.Vector.r is probably what you want to look at.
| 
|     | quite sure how to interpret method signatures like operator() (const
|     | size_t &i, const size_t &j) const. I can guess that means you can
|     | index by two integers - but I don't know what those two integers mean.
| 
|     Well if I had to guess I'd say row and column ...
| 
|     Dirk
| 
|     |
|     | Hadley
|     |
|     | --
|     | Assistant Professor / Dobelman Family Junior Chair
|     | Department of Statistics / Rice University
|     | http://had.co.nz/
| 
|     --
|     "Outside of a dog, a book is a man's best friend. Inside of a dog, it is
|     too
|     dark to read." -- Groucho Marx
| 
| 
| 
| --
| Edward Wallace, PhD
| Harvard FAS center for Systems Biology
| +1-773-517-4009