犰狳向量类的 RcppArmadillo 样本

Jac*_*mos 2 c++ r armadillo rcpp

我们一直在使用samplefrom 函数RcppArmadilloNumericVector对象进行随机采样。然而,我们注意到不可能在犰狳类型(vecuvec)上使用相同的函数。我们已经查看了文件中的函数定义sample.h,它看起来像一个模板化函数,应该能够使用这些类型,但是我们无法弄清楚如何使其与犰狳类一起使用,而不需要做很多工作与库之间的转换NumericVector或类型。IntegerVectorRcpp

例如,我们将此函数编写在名为try.cpp.

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

using namespace arma;
using namespace Rcpp;

// [[Rcpp::export]]
arma::uvec sample_index(const int &size){
    arma::uvec sequence = linspace<uvec>(0, size-1, size);
    arma::uvec out = sample(sequence, size, false);
    return out;
}
Run Code Online (Sandbox Code Playgroud)

运行上面的代码会产生以下错误:

src/try.cpp|11 col 22 error| no matching function for call to 'sample' [cpp/gcc]      

~/Library/R/3.3/library/Rcpp/include/Rcpp/sugar/functions/sample.h|401 col 1 error| note: candidate function not viable: no known conversion from 'arma::uvec' (aka 'Col<unsigned int>') to 'int' for 1st argument [cpp/gcc]

~/Library/R/3.3/library/Rcpp/include/Rcpp/sugar/functions/sample.h|437 col 1 error| note: candidate template ignored: could not match 'Vector' against 'Col' [cpp/gcc]
Run Code Online (Sandbox Code Playgroud)

对此的任何帮助将不胜感激:)

Jac*_*mos 5

sample如果将来有人遇到这个问题,该问题似乎与正在使用的命名空间中函数的多个定义有关。具体地输入定义所需函数的名称空间可以解决问题。特别是,该sample函数需要从 调用Rcpp::RcppArmadillo

以下代码可按预期工作。

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

// [[Rcpp::export]]
arma::uvec sample_index(const int &size){
    arma::uvec sequence = arma::linspace<arma::uvec>(0, size-1, size);
    arma::uvec out = Rcpp::RcppArmadillo::sample(sequence, size, false);
    return out;
}
Run Code Online (Sandbox Code Playgroud)