[Rcpp-devel] new Rcpp API in mvabund

Romain Francois romain at r-enthusiasts.com
Tue Apr 20 08:32:30 CEST 2010


Le 20/04/10 07:54, Yi (Alice) Wang a écrit :
> Hi Romain,
>
> On Tue, Apr 6, 2010 at 11:06 AM, Yi (Alice) Wang <yi.wang at unsw.edu.au
> <mailto:yi.wang at unsw.edu.au>> wrote:
>
>
>
>         I suppose you can use std::copy for this. Something like this:
>
>         std::copy ( x.begin(), x.end(), y->data ) ;
>
>         couple of things to worry about :
>         - make sure the data is big enough
>         - make sure the data type matches, e.g. NumericVector and
>         gsl_vector both hold double so that's fine.
>
>
>                 gsl_matrix hosts its data as a flat array :
>
>                 typedef struct
>                 {
>                   size_t size1;
>                   size_t size2;
>                   size_t tda;
>                   double * data;
>                   gsl_block * block;
>                   int owner;
>                 } gsl_matrix;
>
>                 This should be possible to handle this
>
>
>             Okay. So, with struc like gsl_matrix or
>             struct my_matrix{
>                     double** data ;
>                     int ncol ;
>                     int nrow ;
>             } ;
>             how can I handle it with Rcpp::wrap?
>
>
>         It is something we need to document. I prefer documenting it
>         first and then point you to the documentation when I have tested
>         this works.
>
>         BTW, we have started a new package called RcppGsl (absolutely
>         not ready yet) that aims at closing this gap (more on this later).
>
>         For now I suppose you could do it manually like this:
>
>         Rcpp::NumericMatrix x( data->size1, data->size2 ) ;
>         std::copy(
>                 data->data,
>                 data->data + data->size1 * data->size2,
>                 x.begin()
>           ) ;
>
>
> I tried std::copy for matrix as above. But it seems that the column
> elements are copied to rows. For example, to copy Yr to Y I do
>
>     IntegerMatrix Yr(Ysexp);
>     int nRows = Yr.nrow();
>     int nVars = Yr.ncol();
>
>     gsl_matrix *Y = gsl_matrix_alloc(nRows, nVars);
>     std::copy( Yr.begin(), Yr.end(), Y->data );
>
> and get Y =
>
> 1 2
> 3 4
> 5 6
>
> though Yr =
> 1 4
> 2 5
> 3 6
>
> which is not right. So I get back to copy the matrix via loops. Is there
> a better way to do it?
>
> Thanks,
> Alice

I did not realize gsl used row major ordering before, sorry about that.

There will be a better way once we release the RcppGSL package, but this 
has to wait for the next version of Rcpp.

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/9aKDM9 : embed images in Rd documents
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7



More information about the Rcpp-devel mailing list