[Rcpp-devel] new Rcpp API in mvabund

Romain Francois romain at r-enthusiasts.com
Tue Apr 6 08:17:00 CEST 2010


Le 06/04/10 03:06, Yi (Alice) Wang a écrit :
> On Mon, Apr 5, 2010 at 10:06 PM, Romain Francois
> <romain at r-enthusiasts.com <mailto:romain at r-enthusiasts.com>> wrote:
>
>     Alice,
>
>     Although I know I started this as private emails, I think it would
>     be beneficial for present and future users of Rcpp to continue on
>     the mailing list, if you don't mind.
>
>
> Sure.
>
>     Le 05/04/10 04:53, Yi Wang a écrit :
>
>         Hi, Romain and Dirk,
>
>             with a gsl_vector, you can do someething like:
>
>             gsl_vector * y = ..... ;
>             Rcpp::NumericVector x = Rcpp::wrap( y->data, y->data +
>         y->size );
>
>
>         Yeah, it works.
>
>             I have been toying this morning with gsl, so there is a
>         chance you
>             will get something like this possible soon:
>
>             Rcpp::NumericVector x = *y ;
>
>
>         It would be even better if it can be done for the other way, say
>         something like y->data = x or *y=x ...
>
>
>     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).
>
> Great! Thanks. :)

Won't be ready right now though ....

>     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()
>       ) ;
>
>         The new API is certainly more economic with the wrapper functions
>         (saving tedious data copying). But by doing so, I think I also
>         need to
>         be careful about realeasing the memories and modifiy
>         smry.releaseSummary() and anova.releaseTest() so that the
>         results will
>         not be wipped out, right?
>
>
>     I have not looked at this in detail, but you are responsible for the
>     gsl memory. Rcpp data structures can take care of themselves.
>
>   If I use std::copy, I can destroy data afterwards coz its elements are
> copied to x. But if I use Rcpp::wrap, I should not destroy y because
> only the pointer is passed to x, right?

The data is copied (hend the name std::copy) so you have to deal with 
the gsl memory yourself.

wrap copies the data too because it creates an R data structure for 
which R needs to be responsible for the memory.

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