[Rcpp-devel] Array into C++ with cppFunction

Romain Francois romain at r-enthusiasts.com
Wed Feb 12 15:52:38 CET 2014


FYI, there is a Array template in Rcpp11.
https://github.com/romainfrancois/Rcpp11/blob/master/inst/include/Rcpp/Array.h

With which you could use code like this: 

#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp::export]]
double NewFunc( NumericArray<3> x){
  return x(1,2,3) + x(2,3,4) ; 
}

Romain

Le 12 févr. 2014 à 15:23, Nick Menzies <nick.menzies at gmail.com> a écrit :

> Hi Dirk, thanks for pointing me in the right direction. 
> 
> My solution below for a 3D array (documenting for rubes like myself).   N
> 
> library(Rcpp)
> library(RcppArmadillo)
> 
>     cppFunction('
>     double  NewFunc( NumericVector ArrayR ) {
>       IntegerVector DimsA = ArrayR.attr("dim");
>       arma::cube    ArrayC(ArrayR.begin(),DimsA[0],DimsA[1],DimsA[2],false);
>       double Output = ArrayC(1,2,3) + ArrayC(2,3,4);
>       return Output;      
>     }', depends="RcppArmadillo")
> 
>     NewFunc(array(1,dim=c(5,5,5))) # Test, works
> 
> 
> 
> On Tue, Feb 11, 2014 at 3:34 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
> 
> (Resending, this time with CC to list. Sorry for the extra first post. --Dirk)
> 
> On 11 February 2014 at 11:49, Nick Menzies wrote:
> | Is there a way to pass an array into c++ when using cppFunction?  I realize
> | arrays have been discussed a number of times, but the solutions I have found
> | (e.g. http://markovjumps.blogspot.com/2011/12/
> | r-array-to-rcpparmadillo-cube.html, or https://www.mail-archive.com/
> | rcpp-devel at lists.r-forge.r-project.org/msg05897.html) are described in the
> | context of cxxfunction, and I haven't been able to work how to implement this
> | with cppFunction.
> 
> An array is just a vector with a dimension attribute. Treat it the same at
> the C++ level.
> 
> There are several posts at the Rcpp Gallery dealing with reading / setting
> attributes, and you should know how to pass a single vector through.
> 
> Dirk
> 
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
> 
> 
> 
> -- 
> 
> Nick Menzies
> nick.menzies at gmail.com
> 404 217 1076
> _______________________________________________
> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140212/50c30352/attachment.html>


More information about the Rcpp-devel mailing list