kaneplusplus / bigmemory

126 stars 24 forks source link

diag function #88

Open cdeterman opened 6 years ago

cdeterman commented 6 years ago

In reference to the SO question here perhaps it would be a good idea to implement the diag function in bigmemory? Given that it is just a way of accessing the elements within the matrix I think it is most appropriate for it to be within bigmemory. The question would just come down to do we want to simply provide a wrapper using the special matrix accessor, as provided in @privefl answer, or would it be better to implement in C++?

Thoughts?

privefl commented 6 years ago

Implementing it in C++ would require a dispatcher. It is just two lines of (efficient) R code:

seq_d <- seq_len(min(dim(X)))
X[cbind(seq_d, seq_d)]

So, I think it would not be worth it..

cdeterman commented 6 years ago

@privefl That is perfectly fine with me. The only thing I am suggesting is wrapping those lines in a diag method so a user can simply call

diag(big_mat)

It is simply more convenient and expected with matrix operations. Either as an S4 or S3 method I don't think matters too much in this case. At least that is my two cents.