Search code examples
c++rcppstdvectorarmadillorcpparmadillo

Automatically adapting to arma::mat or arma::sp_mat in an Rcpp::List?


I would like to store and operate an array of matrices (or pointers to them), which could be either arma::mat or arma::sp_mat of varying dimensions (passed from R via Rcpp). What would be the easiest way to achieve this? For example, given the extent of shared operations between the two types, perhaps there is already a parent class that arma::mat & arma::sp_mat derive from? That would allow me to derive pointers to the different matrices and populate my array with. Thank you!

Edit: I had thought storing the input list of matrices in an Rcpp::List would suffice. But for any downstream calculations, I would still need to instantiate an appropriate arma::mat or arma::sp_mat for a given index in the list, so it seems to me that simply storing the input matrices in a List won't be sufficient.


Solution

  • It appears the RcppGallery already has the answer, thanks to the author: https://gallery.rcpp.org/articles/dynamic-dispatch-for-sparse-matrices/