犰狳媒介类的RCPP犰狳样本
RcppArmadillo sample on armadillo vector classes
我们一直在使用 sample
函数从 RcppArmadillo
随机采样NumericVector
对象。但是,我们注意到不可能在犰狳类型(vec
或uvec
(上使用相同的功能。我们已经查看了sample.h
文件中的函数定义,它看起来像一个应该能够使用这些类型的模板化函数,但是我们无法弄清楚如何使其与犰狳类一起工作,而无需从Rcpp
库中的NumericVector
或IntegerVector
类型进行大量转换。
例如,我们将此函数写入名为 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]
任何这方面的帮助将不胜感激:)
如果将来有人遇到此问题,该问题似乎与正在使用的命名空间中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;
}
相关文章:
- 如何使用Rcpp将R函数转换为C++函数
- rcpp函数中的清理时间很长
- 将 C++ 类与 Rcpp 一起使用,从 C 或 R 修改它
- 从 Rcpp Catch 测试中获取有关"inst/extdata"的数据
- 犰狳的 print() 方法和 cout 在从 Rcpp 调用时顺序不一致
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- std::mt19937 in Rcpp
- Rcpp并行无匹配函数,用于调用"转换"
- 使用源读取器从网络摄像头源获取 H264 样本
- 通过 RCPP 中的索引求和
- 混合 Rcpp 模块和 Rcpp::export
- 有没有办法在RCPP程序无法正常工作时阻止RGui崩溃?
- 用于子集字符串的 Rcpp 函数
- 通过 Rcpp 和 bit64 R 包将最大的int64_t变量值从 C++ 传递到 R
- 在模板化的 Rcpp 函数中调用另一个 cpp 函数
- Rcpp 函数在打包时使 RStudio 崩溃
- 在数据帧上应用 Rcpp
- 在 Rcpp 中的字符串类型之间转换时出错
- 犰狳媒介类的RCPP犰狳样本
- 在RCPP中生成整数样本