我正在编写一个包含一些函数的包,这个函数调用RcppArmadillo ::来自RcppArmadillo的样本。 但是在编译时遇到了以下错误。
Citrus.cpp中包含的文件:2: ./R/x86_64-unknown-linux-gnu-library/3.0/RcppArmadillo/include/RcppArmadilloExtensions/sample.h:在函数'T Rcpp :: RcppArmadillo :: sample(const T&,int,bool,Rcpp :: NumericVector) )[与T = arma :: subview_col]': Citrus.cpp:241:从这里实例化 ./R/x86_64-unknown-linux-gnu-library/3.0/RcppArmadillo/include/RcppArmadilloExtensions/sample.h:45:错误:'const struct arma :: subview_col'没有名为'size'的成员 ./R/x86_64-unknown-linux-gnu-library/3.0/RcppArmadillo/include/RcppArmadilloExtensions/sample.h:48:错误:没有匹配函数来调用'arma :: subview_col :: subview_col(const int&)' ./R/x86_64-unknown-linux-gnu-library/3.0/RcppArmadillo/include/armadillo_bits/subview_bones.hpp:236:注意:候选人是:arma :: subview_col :: subview_col()[with eT = double] ./R/x86_64-unknown-linux-gnu-library/3.0/RcppArmadillo/include/armadillo_bits/subview_meat.hpp:2608:注意:arma :: subview_col :: subview_col(const arma :: Mat&,arma :: uword, arma :: uword,arma :: uword)[with eT = double] ./R/x86_64-unknown-linux-gnu-library/3.0/RcppArmadillo/include/armadillo_bits/subview_meat.hpp:2597:注意:arma :: subview_col :: subview_col(const arma :: Mat&,arma :: uword) [eT = double] ./R/x86_64-unknown-linux-gnu library / 3.0 / RcppArmadillo / include / armadillo_bits / forward_bones.hpp:29:注意:arma :: subview_col :: subview_col(const arma :: subview_col&) make:*** [Citrus.o]错误1
我使用的RcppArmadillo是0.7.700.0.0。
linux和OSX上都出现了同样的错误。使用Rstudio编译时,错误消息如下:
no member named 'size' in 'arma::subview_col<double>'.
no matching constructor for initialization of 'arma::subview_col<double>'
我在以前的作品中使用了RcppArmadillo :: sample。它突然不起作用。我感谢任何帮助。
答案 0 :(得分:1)
此功能适用于arma::vec
或NumericVector
中的预子集数据始终具有且始终如一。不要将此与从子集操作获得的中间向量(例如.col()
,.cols()
或.submat()
)一起使用。
您遇到的问题是您决定在调用样本中对数据进行子集化。 (你已经省略了诊断这部分的代码,所以我在这里推测。)由于sample()
需要同时使用Rcpp和Armadillo数据类型,因此从未调用过Armadillo特定大小的成员函数。相反,库选择调用.size()
容器的STL
成员函数,armadillo supported,因为它是在两个对象之间共享的。但是,犰狳将成员函数的实现限制在“活动”数据结构而不是临时数据结构中。因此,.size()
未实现subview_col
成员函数。因此,我们最终得到错误:
错误:'const struct arma :: subview_col'没有名为'size'的成员
要解决此限制并保存内存,请使用将重用内存的advanced vec
ctor,从而避免创建中间人arma::subview_col
。
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::export]]
void adv_rnd(int nrow, int ncol, bool replace = true){
// Create a matrix of given dimensions
arma::mat X(nrow, ncol);
X.randn();
// Show state before randomization
Rcpp::Rcout << "Before Randomization:" << std::endl << X << std::endl;
// Randomize each column
for(int i = 0; i < ncol; ++i){
arma::vec Y(X.colptr(i), nrow, false, true);
X.col(i) = Rcpp::RcppArmadillo::sample(Y, nrow, replace);
}
// Show state after randomization
Rcpp::Rcout << "After Randomization:" << std::endl << X << std::endl;
}
示例输出:
> adv_rnd(3,3)
Before Randomization:
-0.7197 1.2590 -0.5898
0.0253 0.1493 -0.0685
-0.6074 1.3843 0.0400
After Randomization:
-0.7197 1.2590 0.0400
-0.6074 1.2590 -0.5898
-0.6074 0.1493 -0.0685