RcppCore / RcppEigen

Rcpp integration for the Eigen templated linear algebra library
Other
110 stars 40 forks source link

Tensor and wrap #83

Closed rsparapa closed 4 years ago

rsparapa commented 4 years ago

Hi Gang:

I see that the new tensores are included with RcppEigen which is great. However, I don't see a wrap function for tensors in RcppEigenWrap.h so that a tensor could be returned as an array object to R. I have never written a wrap function. But, if some nice person is willing to give me some hints, then I could take a crack at it.

Thanks,

Rodney

eddelbuettel commented 4 years ago

Some starters:

Plus a vignette from Rcpp that is also a chapter in the Rcpp book:

It is still fairly daunting material. Maybe start small and try a simple case working to not get carried away.

rsparapa commented 4 years ago

Hi Gang:

As I was thinking more about this, a question occurred to me. Is it the case that there is no multi-dimensional RcppArray in Rcpp itself? If so, then I think that might be an avenue or alternative to what I proposed above. If not (something like RcppArray does exist), then maybe I could look at those details and that would assist me here. I hope that this questions makes sense ;o)

Thanks

eddelbuettel commented 4 years ago

Well, as I recall, arrays are really just standard vectors with a dim attribute:

R> a <- array(1:8, dim=c(2,2,2))
R> a
, , 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8

R> class(a)
[1] "array"
R> str(a)
 int [1:2, 1:2, 1:2] 1 2 3 4 5 6 7 8
R> 

The above can be done easily (see Rcpp Gallery on setting dim easily). And there is more as e.g. https://gallery.rcpp.org/articles/simple-array-class/ plus some add-on packages on CRAN.

So if you specify the problem you want to solve more clearly, maybe we can help you. Here it looks like you just redefined one question into another, loosely related one. But maybe I am missing something...

rsparapa commented 4 years ago

You are right that doesn't really help here. Just have to find some time to work on this issue.

rsparapa commented 4 years ago

Ok, I see where I was going with my previous question above. The simple array class is ok, but that uses RcppArmadillo. I hesitate to add yet another dependency when I already have an RcppEigen dependency. But, instead of RcppEiegen tensors, I tried a different attack. I'm creating Rcpp 3D arrays on the C++ side which works seemlessly as you suggest. However, how do you access the memory locations? For example, I tried something like...

Rcpp::NumericVector impute_draw(Rcpp::Dimension(nkeeptrain, n, K));
impute_draw(0, 0, 0)=0.;

The first line is fine, but the second generates an error. So, this is what I am trying to do. I realize that this is not an RcppEigen tensor issue per se. Although that could be an avenue taken, I think just figuring this out for Rcpp core would mean that tensors are not really needed (unless of course you have already invested in an Eigen tensor code base which I have not). Am I making more sense now? Thanks

eddelbuettel commented 4 years ago

I guess the (...,) accessor is simply not implemented for three dimensions for a NumericVector type.

Here you create a vector --- a 1-D object. You can play games with N dimensions but you need to map those down to the 1-D case that that class understand.

As for multiple dependencies: up to you. A number of packages on CRAN do just that and depends on RcppArmadillo and RcppEigen. There is no rule against that.

rsparapa commented 4 years ago

True. But, I'm thinking that if I want this, then many others want it too and the best place is Rcpp itself. I have played around with the 1-D idea, i.e., impute_draw[i]. But, something is wrong with my math. Not sure what I am doing wrong. The simple array class should help here, but I'm not exacly following it. If I can figure this out, then it shows us how to create the (...,...,...) accessor for Rcpp, right? Or is that trouble because it is variadic?

eddelbuettel commented 4 years ago

I do not think this belongs in Rcpp proper as this can be handled just fine in a user-written package such as yours.

Can we close this here too as there is actually no demonstrated issue against RcppEigen...

rsparapa commented 4 years ago

Sure, it still doesn't work. But, I will post on Rcpp Gallery when I figure it out.