Skip to content

[Rcpp-devel] NumericVector

6 messages · James Li, Dirk Eddelbuettel, Romain Francois

#
Dear Dirk and Rcpp-devel members,

I am currently passing a multidimensional (N > 2) array (i.e.
array(NA, dim = rep(3,5)) ) from R via Rcpp using

"in C++:"

//[[Rcpp::export]]
Rcpp::List check_arrayC (Rcpp::NumericVector x, Rcpp::IntegerVector modes){
//do stuff to x
    return Rcpp::List::create(Rcpp::_["data"] = x, Rcpp::_["modes"] = modes);
}


"in R:"

a <- array(1:32, dim=rep(2,5))
b <- check_arrayC(a, dim(a))

While I know that a multidimensional array is stored as a contiguous
array internally, is there currently a more natural/efficient way to
pass it back and forth within Rcpp?

Also from Dirk's book, it seems that an instance of
Rcpp::NumericVector can be instantiated into a multidimensional array
via

Rcpp::NumericVector vec3 =Rcpp::NumericVector( Rcpp::Dimension(4, 5, 6));

In this case, how do we access element vec3[1,2,3]?

Some background about what I am trying to do: I would like to create a
multidimensional array wrapper class around the base R multi-way array
class. I would also like to be able to pass this multidimensional
array via Rcpp to do all the heavy-lifting in c++. Ideally, I could
also convert the mda into a Boost::multi_array.

Thanks in advance for any help.

-James
#
Hi James,
On 16 August 2013 at 11:59, James Li wrote:
| Dear Dirk and Rcpp-devel members,
| 
| I am currently passing a multidimensional (N > 2) array (i.e.
| array(NA, dim = rep(3,5)) ) from R via Rcpp using


How big is 'N' going to be?

| "in C++:"
| 
| //[[Rcpp::export]]
| Rcpp::List check_arrayC (Rcpp::NumericVector x, Rcpp::IntegerVector modes){
| //do stuff to x
|     return Rcpp::List::create(Rcpp::_["data"] = x, Rcpp::_["modes"] = modes);
| }
| 
| 
| "in R:"
| 
| a <- array(1:32, dim=rep(2,5))
| b <- check_arrayC(a, dim(a))
| 
| While I know that a multidimensional array is stored as a contiguous
| array internally, is there currently a more natural/efficient way to
| pass it back and forth within Rcpp?
| 
| Also from Dirk's book, it seems that an instance of
| Rcpp::NumericVector can be instantiated into a multidimensional array
| via
| 
| Rcpp::NumericVector vec3 =Rcpp::NumericVector( Rcpp::Dimension(4, 5, 6));
| 
| In this case, how do we access element vec3[1,2,3]?
| 
| Some background about what I am trying to do: I would like to create a
| multidimensional array wrapper class around the base R multi-way array
| class. I would also like to be able to pass this multidimensional
| array via Rcpp to do all the heavy-lifting in c++. Ideally, I could
| also convert the mda into a Boost::multi_array.

For a moderately-sized project (at work, not open source) I had a very good
experience using Armadillo 'cubes' (3-d matrices) which I occassionally
stored in 'fields' (which I though of as lists of such cubes).  I think in
most (all?) cases I reduces data to 2-d matrices before returning that
R. That worked great.

Beyond that ... you are on your own as there is very little C++ support
already useable by Rcpp.  You'd have to write custom as<>() and wrap()
methods (which is not hard and may well be worth it).

Cheers, Dirk
 
| Thanks in advance for any help.
| 
| -James
| 
| -- 
| James Li | Ph.D. Candidate | http://jamesyili.com/
| Dept. of Statistical Science | Cornell University
| _______________________________________________
| 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
#
Le 16/08/13 17:59, James Li a ?crit :
Please be aware that Rcpp is a joint project and has been so for several 
years now.
We don't have support for this in Rcpp. But is is easy to do it 
yourself, with only a small code addition, e.g. :

#include <Rcpp.h>
using namespace Rcpp ;

class Offset {
private:
     int nrows, ncols, nmats ;

public:
     Offset( int nrows_, int ncols_, int nmats_) : nrows(nrows_), 
ncols(ncols_), nmats(nmats_){}

     int operator()( int i, int j, int k){
         return i + j * nrows + k * ( nrows * ncols ) ;
     }

} ;

// [[Rcpp::export]]
NumericVector foo(){
     NumericVector vec3 = NumericVector( Dimension(4, 5, 6) );
     Offset offset( 4, 5, 6 ) ;

     vec3[ offset(0,0,0) ] = 1.0 ;
     vec3[ offset(1,1,1) ] = 2.0 ;
     return vec3 ;
}

/*** R
     foo()
*/


You could go further and encapsulate both in a dedicated class, e.g. 
Array3 as in this: I have included the needed bits so that you can :
- pass an Array3 from R
- return one to R
- extract or set data using the operator()

It is not full featured, lacks testing, etc ... with it works:

#include <Rcpp.h>
using namespace Rcpp ;

class Offset{
private:
     int nrows, ncols, nmats ;

public:
     Offset( IntegerVector dim ) : nrows(dim[0]), ncols(dim[1]), 
nmats(dim[2]){}

     Offset( int nrows_, int ncols_, int nmats_) : nrows(nrows_), 
ncols(ncols_), nmats(nmats_){}

     int operator()( int i, int j, int k){
         return i + j * nrows + k * ( nrows * ncols ) ;
     }

} ;

class Array3 : public NumericVector {
private:
     Offset offset ;

public:
     Array3( SEXP x) : NumericVector(x), offset( 
(IntegerVector)((RObject)x).attr("dim") ) { }
     Array3( Dimension dim ): NumericVector( dim ), offset( dim[0], 
dim[1], dim[2] ) {}
     Array3( int nrows_, int ncols_, int nmats_ ): NumericVector( 
Dimension(nrows_, ncols_, nmats_ ) ), offset( nrows_, ncols_, nmats_ ) {}

     inline double& operator()( int i, int j, int k){
         return ( (NumericVector*)(this) )->operator[]( offset( i, j, k) 
) ;
     }

} ;
// [[Rcpp::export]]
Array3 foo(Array3 arr){
     arr(0,0,0) = 1.0 ;
     arr(1,1,1) = 2.0 ;
     return arr ;
}

/*** R
     a <- array( 0, dim = c(4,5,6 ) )
     foo(a)
*/
#
Hi Dirk,

N could be anywhere between 3-10.

Thanks! I will definitely look into how to do those.

Also, if

Rcpp::NumericVector vec3 =Rcpp::NumericVector( Rcpp::Dimension(4, 5, 6));

In this case, how do we access element vec3[1,2,3]?

Thanks again,
James
On Friday, August 16, 2013, Dirk Eddelbuettel wrote:

            

  
    
#
Hi James,
On 16 August 2013 at 12:57, James Li wrote:
| Hi Dirk,
| 
| N could be anywhere between 3-10.?

Eek. 10 is a lot.

| Thanks! I will definitely look into how to do those.
| 
| Also, if?
| 
| Rcpp::NumericVector vec3 =Rcpp::NumericVector( Rcpp::Dimension(4, 5, 6));
| 
| In this case, how do we access element vec3[1,2,3]?

Well a) you cannot use [] to index, only () as the [] only allows a single
index (and , is a special operator for C/C++).  Romain already sent you a
first cut at something homegrown.  

Alternatively, if you find a matrix library dealing with N up to 10 ... you
may want to consider writing glue code to access it from R via Rcpp.

Cheers, Dirk
 
| Thanks again,
| James
|
| On Friday, August 16, 2013, Dirk Eddelbuettel wrote:
| 
| 
|     Hi James,
|
| On 16 August 2013 at 11:59, James Li wrote:
|     | Dear Dirk and Rcpp-devel members,
|     |
|     | I am currently passing a multidimensional (N > 2) array (i.e.
|     | array(NA, dim = rep(3,5)) ) from R via Rcpp using
| 
| 
|     How big is 'N' going to be?
| 
|     | "in C++:"
|     |
|     | //[[Rcpp::export]]
|     | Rcpp::List check_arrayC (Rcpp::NumericVector x, Rcpp::IntegerVector
|     modes){
|     | //do stuff to x
|     | ? ? return Rcpp::List::create(Rcpp::_["data"] = x, Rcpp::_["modes"] =
|     modes);
|     | }
|     |
|     |
|     | "in R:"
|     |
|     | a <- array(1:32, dim=rep(2,5))
|     | b <- check_arrayC(a, dim(a))
|     |
|     | While I know that a multidimensional array is stored as a contiguous
|     | array internally, is there currently a more natural/efficient way to
|     | pass it back and forth within Rcpp?
|     |
|     | Also from Dirk's book, it seems that an instance of
|     | Rcpp::NumericVector can be instantiated into a multidimensional array
|     | via
|     |
|     | Rcpp::NumericVector vec3 =Rcpp::NumericVector( Rcpp::Dimension(4, 5, 6));
|     |
|     | In this case, how do we access element vec3[1,2,3]?
|     |
|     | Some background about what I am trying to do: I would like to create a
|     | multidimensional array wrapper class around the base R multi-way array
|     | class. I would also like to be able to pass this multidimensional
|     | array via Rcpp to do all the heavy-lifting in c++. Ideally, I could
|     | also convert the mda into a Boost::multi_array.
| 
|     For a moderately-sized project (at work, not open source) I had a very good
|     experience using Armadillo 'cubes' (3-d matrices) which I occassionally
|     stored in 'fields' (which I though of as lists of such cubes). ?I think in
|     most (all?) cases I reduces data to 2-d matrices before returning that
|     R. That worked great.
| 
|     Beyond that ... you are on your own as there is very little C++ support
|     already useable by Rcpp. ?You'd have to write custom as<>() and wrap()
|     methods (which is not hard and may well be worth it).
| 
|     Cheers, Dirk
| 
|     | Thanks in advance for any help.
|     |
|     | -James
|     |
|     | --
|     | James Li | Ph.D. Candidate | http://jamesyili.com/
|     | Dept. of Statistical Science | Cornell University
|     | _______________________________________________
|     | 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
| 
|     --
|     Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
| 
| 
| 
| --
| James Li |?Ph.D. Candidate | http://jamesyili.com/
| Dept. of Statistical Science | Cornell University
#
Hi Romain and Dirk,

Sorry I did not see Romain's earlier response. That is very helpful!

Thank you both for your prompt assistance and for developing such a useful
tool.

-James
On Friday, August 16, 2013, Dirk Eddelbuettel wrote: