2017-04-11 39 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()'實現(後者仍然是可選的),您需要更明確地指出您選擇哪個。 「 –