英文:
Mapping Rcpp NumericVector to *static* or *fixed-size* Eigen Vectors?
问题
这个帖子提供了一个将Rcpp::NumericVector转换为Eigen::VectorXd的示例:
Rcpp::cppFunction("bool conv(NumericVector X) { \
      Eigen::Map<Eigen::VectorXd> \
      XS(Rcpp::as<Eigen::Map<Eigen::VectorXd>>(X)); return true; } ", 
            depends="RcppEigen")
conv(1:4)
但我无法将其调整为创建具有静态大小或固定大小的Eigen容器:
Rcpp::cppFunction("bool conv(NumericVector X) { \
      Eigen::Map<Eigen::Matrix<double,4,1>> \
      XS(Rcpp::as<Eigen::Map<Eigen::Matrix<double,4,1>>>(X)); return true; } ", 
                  depends="RcppEigen")
conv(1:4)
如何实现这个目标?
英文:
This thread provides an example of converting an Rcpp::NumericVector to an Eigen::VectorXd:
Rcpp::cppFunction("bool conv(NumericVector X) { \
      Eigen::Map<Eigen::VectorXd> \
      XS(Rcpp::as<Eigen::Map<Eigen::VectorXd> >(X)); return true; } ", 
            depends="RcppEigen")
conv(1:4)
but I can't manage to adapt this to creating statically-sized or fixed-size Eigen containers
Rcpp::cppFunction("bool conv(NumericVector X) { \
      Eigen::Map<Eigen::Matrix<double,4,1>> \
      XS(Rcpp::as<Eigen::Map<Eigen::Matrix<double,4,1>> >(X)); return true; } ", 
                  depends="RcppEigen")
conv(1:4)
How can this be done?
答案1
得分: 2
以下是翻译好的部分:
这是一种最小化的蛮力方法,用于实例化一个固定大小的4x1矩阵...然后使用memcpy()来复制数据负载。正如我之前的评论中提到的,我们在RcppEigen中没有代码可以从SEXP R对象转换到Eigen类型,因此我们不能做更多的事情。
代码
#include <RcppEigen.h>
// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::export]]
bool conv1(Rcpp::NumericVector X) {
    Eigen::Map<Eigen::VectorXd> XS(Rcpp::as<Eigen::Map<Eigen::VectorXd>>(X));
    return true;
}
// [[Rcpp::export]]
bool conv2(Rcpp::NumericVector X) {
    const int n = 4;
    if (X.size() != n) Rcpp::stop("Wrong size");
    Eigen::Matrix<double,4,1> m;
    std::memcpy(&m(0,0), &X[0], n*sizeof(double));
    Rcpp::Rcout << m << std::endl;
    return true;
}
输出
> Rcpp::sourceCpp("~/git/stackoverflow/76706728/answer.cpp")
> x <- as.numeric(1:4)
> conv1(x)
[1] TRUE
> conv2(x)
1
2
3
4
[1] TRUE
英文:
Here is a minimal brute-force approach of instantiating a fixed-size 4x1 matrix ... and then using memcpy() to copy the payload over.  As my comment earlier mentioned we have no code in RcppEigen that transfer from a SEXP R object to such an Eigen type so there is not a lot more we can do.
Code
#include <RcppEigen.h>
// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::export]]
bool conv1(Rcpp::NumericVector X) {
    Eigen::Map<Eigen::VectorXd> XS(Rcpp::as<Eigen::Map<Eigen::VectorXd>>(X));
    return true;
}
// [[Rcpp::export]]
bool conv2(Rcpp::NumericVector X) {
    const int n = 4;
    if (X.size() != n) Rcpp::stop("Wrong size");
    Eigen::Matrix<double,4,1> m;
    std::memcpy(&m[0,0], &X[0], n*sizeof(double));
    Rcpp::Rcout << m << std::endl;
    return true;
}
/*** R
x <- as.numeric(1:4)
conv1(x)
conv2(x)
*/
Output
> Rcpp::sourceCpp("~/git/stackoverflow/76706728/answer.cpp")
> x <- as.numeric(1:4)
> conv1(x)
[1] TRUE
> conv2(x)
1
2
3
4
[1] TRUE
> 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论