RcppCore / RcppEigen

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

Support of Eigen::Ref? #109

Closed dazhwu closed 2 years ago

dazhwu commented 2 years ago

It seems that RcppEigen doesn't directly support Ref. It does support Map. But Ref is more flexible.

yixuan commented 2 years ago

I suppose you mean the support for Rcpp::as. It seems most of the operations are shared by Map and Ref, and typically you can directly assign a Map object to a Ref. At least for me the interface to Map can solve most of the problems.

dazhwu commented 2 years ago

Thank you for your response. I wrote a C++ extension using Eigen. Using these C++ code files as backbone, I first made a python package with pybind11 and it worked without any problem. I then tried to develop an R package using RcppEigen, but couldn't pass. I guess it is because of my use of Eigen::Ref. For example, the following works:

#include <RcppEigen.h>
// [[Rcpp::depends(RcppEigen)]]

using Eigen::Map;                       // 'maps' rather than copies
using Eigen::Ref;                       // 'maps' rather than copies
using Eigen::MatrixXd;                  // variable size matrix, double precision

// [[Rcpp::export]]
Eigen::MatrixXd doubleMe( Eigen::Map<Eigen::MatrixXd> M) {

  return M*2;
}

But if I change "Map" to "Ref", I got error " matching function for call to ‘Eigen::Ref<Eigen::Matrix<double, -1, -1> >::Ref(SEXPREC*&)".

On the other hand, I can compile the following to a .so without problem:

#include <eigen3/Eigen/Dense>

using Eigen::MatrixXd;

Eigen::MatrixXd doubleMe( Eigen::Ref<Eigen::MatrixXd> M) {

    return M*2;
}
eddelbuettel commented 2 years ago

What @yixuan tried to tell you is that Map is supported (hence your example 1 works) but that nobody has written converters code (which the compiler would inject on the interface) for Ref. So if you want Ref you would have to add that. And the bigger is ... that the difference may not be big.

In short, just because pybind11 has an interface for it does not imply RcppEigen does. And, I suspect, vice versa.

Hope this clarifies.

dazhwu commented 2 years ago

Thank you for your clarification.