How do I convert R data types into an Eigen::Matrix<double,dim,1> in this situation?

狂风中的少年 提交于 2019-12-24 07:38:18

问题


Why is this reporting error: no matching function for call to ‘as<Eigen::Matrix<double, 400, 1, 0, 400, 1> >(int)’ and cannot convert ‘3’ (type ‘int’) to type ‘SEXP {aka SEXPREC*}’? There appears to be a similar problem here.

In a previous question, I asked about registering plugins so that I could use a c++ template library from within R. Continuing on with that example, I still cannot get this small example to work. The plugin issue has been figured out, but now Rcpp::as won't allow me to convert numeric vectors and matrices from R into the appropriate c++ type.

library(Rcpp)
inc <- 
'template <size_t dim>
class SillyWrapper  
{
public:
  Eigen::Matrix<double,dim,1> m_vec;
  SillyWrapper(const Eigen::Matrix<int,dim,1>& vec) : m_vec(vec) {};
  void printData() { Rcpp::Rcout << m_vec(0); };
};'
src <- '
void foo(){
  const int dim = 400;
  SillyWrapper<dim> myThing(Rcpp::as<Eigen::Map<Eigen::Matrix<double,dim,1>>>(3));
  myThing.printData();
}'
f <- function(x){
  plug <- Rcpp.plugin.maker(include.before = "#include <Eigen/Dense>")
  settings <- plug()
  settings$env$PKG_CPPFLAGS = "-I/usr/include/eigen3"
  settings
}
Rcpp::registerPlugin(name = "Eigen3", plugin = f)
fun <- cppFunction(code = src, 
                   plugins = "Eigen3",
                   includes = inc)

A few other things:

  • I am registering my own plugin instead of using RcppEigen for practice (perhaps it will help later when I need to use other header only libraries). I plan on relying on plugins because I have a lot of header files I need to include, and I don't want to paste them all together and assign the massive string to inc above.
  • I cannot use Eigen::MatrixXd I need to size the matrices with the compiler.
  • @DirkEddelbuettel mentioned something about configure logic. I tried searching that, but no luck.

来源:https://stackoverflow.com/questions/57791272/how-do-i-convert-r-data-types-into-an-eigenmatrixdouble-dim-1-in-this-situat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!