[Rcpp-devel] returning array from C
Krzysztof Sakrejda
krzysztof.sakrejda at gmail.com
Wed Jun 12 03:20:54 CEST 2013
On Tue, Jun 11, 2013 at 4:10 PM, Steve Jaffe <sjaffe at riskspan.com> wrote:
> On Tuesday, June 11, 2013 3:02 PM, Romain Francois wrote:
>> Maintaining the idea that this external pointer acts as a numeric vector without being one.
>>
>> What happens then you want its mean, its quantile or whatever. You essentially have to reimplement everything.
>>
>> So yes : can be done, but lots of work.
Agreed.
>
> I don't yet know enough about R internals to say, but in principle it seems that if you have the subscript operator you'd be able to do practically any statistical calculation. Without re-implementing anything.
>
> (One might also need elementwise addition, multiplication by scalars, etc -- though in principle these can all be defined in terms of subscripting, I assume that R has specialized versions of these for atomic vectors due to performance. But in any case a relatively small number of fundamental operations ought to be enough to make something "act like" a vector. And this is exactly what Krzysztof provided the basics of.)
>
> When I say 'in principle' I'm thinking along the lines of C++ generics -- if I have an object that overloads the subscript operator, then any >algorithm that just uses subscripting will simply work. Whether that is true in R I'm not certain, but what I think I know of the idea of >generics and class-specific dispatch seems consistent with that picture.
In case Dirk's reply wasn't clear on this point: almost all the basic
functions which make R so pleasant for doing statistical analysis
(mean/quantile/etc) use the vector at the C level and completely
bypass the '[' function defined in R. In principle there's no reason
you couldn't re-implement the relevant functions relying just on '['
and a few other basic functions but, as Romain points out, that's a
lot of work and it would be slow.
I think it's more useful to think of whether a limited subset of these
operations will work for your application and whether you can convey
the limits of the class to the user---I'd hate to convince a user that
something _is_ a numeric vector and have them waste hours wondering
why some piece of code is not working.
Krzysztof
More information about the Rcpp-devel
mailing list