将Rcpp NumericVector映射到*静态*或*固定大小*的Eigen向量?

huangapple go评论45阅读模式
英文:

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(&quot;bool conv(NumericVector X) { \
      Eigen::Map&lt;Eigen::VectorXd&gt; \
      XS(Rcpp::as&lt;Eigen::Map&lt;Eigen::VectorXd&gt; &gt;(X)); return true; } &quot;, 
            depends=&quot;RcppEigen&quot;)
conv(1:4)

but I can't manage to adapt this to creating statically-sized or fixed-size Eigen containers

Rcpp::cppFunction(&quot;bool conv(NumericVector X) { \
      Eigen::Map&lt;Eigen::Matrix&lt;double,4,1&gt;&gt; \
      XS(Rcpp::as&lt;Eigen::Map&lt;Eigen::Matrix&lt;double,4,1&gt;&gt; &gt;(X)); return true; } &quot;, 
                  depends=&quot;RcppEigen&quot;)
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 &lt;RcppEigen.h&gt;

// [[Rcpp::depends(RcppEigen)]]

// [[Rcpp::export]]
bool conv1(Rcpp::NumericVector X) {
    Eigen::Map&lt;Eigen::VectorXd&gt; XS(Rcpp::as&lt;Eigen::Map&lt;Eigen::VectorXd&gt;&gt;(X));
    return true;
}

// [[Rcpp::export]]
bool conv2(Rcpp::NumericVector X) {
    const int n = 4;
    if (X.size() != n) Rcpp::stop(&quot;Wrong size&quot;);
    Eigen::Matrix&lt;double,4,1&gt; m;
    std::memcpy(&amp;m[0,0], &amp;X[0], n*sizeof(double));
    Rcpp::Rcout &lt;&lt; m &lt;&lt; std::endl;
    return true;
}

/*** R
x &lt;- as.numeric(1:4)
conv1(x)
conv2(x)
*/

Output

&gt; Rcpp::sourceCpp(&quot;~/git/stackoverflow/76706728/answer.cpp&quot;)

&gt; x &lt;- as.numeric(1:4)

&gt; conv1(x)
[1] TRUE

&gt; conv2(x)
1
2
3
4
[1] TRUE
&gt; 

huangapple
  • 本文由 发表于 2023年7月18日 01:15:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定