2017-04-11 166 views
0

我们一直在使用sample函数从RcppArmadillo随机抽样一个NumericVector对象。但是,我们注意到在犰狳类型上不可能使用相同的功能(vecuvec)。我们已经看了sample.h文件中的函数定义,它看起来像一个模板化的函数,应该能够处理这些类型,但是我们还没有能够弄清楚如何使它与Armadillo类一起工作而没有做很多来自Rcpp库的NumericVectorIntegerVector类型的转换。RcppArmadillo样本上犰狳矢量类

例如,我们将此函数写入一个名为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; 
} 

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

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] 

任何帮助,将不胜感激:)

+0

您能否快速确认RcppArmadillo的版本是最新的? 'sessionInfo()'> = 7.6 – coatless

+0

'sessionInfo()'的输出:'RcppArmadillo_0.7.700.0.0' –

+2

作为第一步(防御),撤销'using namespace ...',因为我们现在有_two_'sample'函数。 –

回答

1

在任何情况下,运行在将来这个问题,这个问题似乎与正在使用的名称空间中的sample函数的多个定义有关。特别输入定义所需函数的名称空间可以解决问题。特别是,需要从Rcpp::RcppArmadillo调用sample函数。

以下代码根据需要工作。

// [[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; 
} 
+0

请删除文件顶部的'namespace'声明。你没有使用它们。这可能会让复制和粘贴代码的其他人受到影响。 – coatless

+0

我会将其改写为:“鉴于现在在Rcpp和RcppArmadillo命名空间中都有两个'sample()'实现(后者仍然是可选的),您需要更明确地指出您选择哪个。 “ –