【问题标题】:RcppArmadillo's sample() is ambiguous after updating RRcppArmadillo 的 sample() 在更新 R 后不明确
【发布时间】:2023-03-24 01:10:01
【问题描述】:

我通常使用一个简短的 Rcpp 函数,该函数将一个矩阵作为输入,其中每一行包含 K 个总和为 1 的概率。然后,该函数为每一行随机采样一个 1 到 K 之间的整数,对应于所提供的概率。这是函数:

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadilloExtensions/sample.h>

using namespace Rcpp;

// [[Rcpp::export]]
IntegerVector sample_matrix(NumericMatrix x, IntegerVector choice_set) {
  int n = x.nrow();
  IntegerVector result(n);
  for ( int i = 0; i < n; ++i ) {
    result[i] = RcppArmadillo::sample(choice_set, 1, false, x(i, _))[0];
  }
  return result;
}

我最近更新了 R 和所有软件包。现在我不能再编译这个函数了。我不清楚原因。跑步

library(Rcpp)
library(RcppArmadillo)
Rcpp::sourceCpp("sample_matrix.cpp")

抛出以下错误:

error: call of overloaded 'sample(Rcpp::IntegerVector&, int, bool, Rcpp::Matrix<14>::Row)' is ambiguous

这基本上告诉我,我对RcppArmadillo::sample() 的调用是模棱两可的。谁能告诉我为什么会这样?

【问题讨论】:

    标签: r rcpp rcpparmadillo


    【解决方案1】:

    这里发生了两件事,你的问题和答案有两部分。

    第一个是“元”:为什么是现在?好吧,我们在sample() 代码/设置中发现了一个错误,Christian 为最新的 RcppArmadillo 版本修复了该错误(并且都记录在那里)。简而言之,此处给您带来麻烦的概率参数的界面已更改因为重复使用/重复使用不安全。就是现在。

    第二,错误信息。你没有说你使用的是什么编译器或版本,但我的(目前g++-9.3)实际上对错误很有帮助。它仍然是 C++,所以需要一些解释性的舞蹈,但本质上它清楚地表明您使用 Rcpp::Matrix&lt;14&gt;::Row 调用并且没有为该类型提供接口。哪个是对的。 sample() 提供了一些接口,但没有用于Row 对象。因此,修复再次简单。通过将行设为NumericVector 添加一行来帮助编译器,一切都很好。

    固定代码

    #include <RcppArmadillo.h>
    #include <RcppArmadilloExtensions/sample.h>
    
    // [[Rcpp::depends(RcppArmadillo)]]
    
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    IntegerVector sample_matrix(NumericMatrix x, IntegerVector choice_set) {
      int n = x.nrow();
      IntegerVector result(n);
      for ( int i = 0; i < n; ++i ) {
        Rcpp::NumericVector z(x(i, _));
        result[i] = RcppArmadillo::sample(choice_set, 1, false, z)[0];
      }
      return result;
    }
    

    示例

    R> Rcpp::sourceCpp("answer.cpp")        # no need for library(Rcpp)   
    R> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多