首页 文章

RcppArmadillo sample.h编译错误

提问于
浏览
0

我正在编写一个包含一些函数的包,调用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)[with 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()[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:note:arma :: subview_col :: subview_col(const arma :: Mat&,arma :: uword)[with eT = double] ./R/x86_64-unknown-linux-gnu library / 3.0 / RcppArmadillo / include / armadillo_bits / forward_bones.hpp:29:note :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 . 它突然不起作用 . 我感谢任何帮助 .

1 回答

  • 1

    此功能适用于 arma::vecNumericVector 中的预子集数据 . 始终具有且始终为 . 不要将此与从子集操作(例如 .col().cols().submat() )获得的中间向量一起使用 .


    您遇到的问题是've decided to subset the data within the call to sample. (You'已省略了诊断此部分的代码,因此我在此推测 . )由于 sample() 需要同时使用Rcpp和Armadillo数据类型,因此从未调用过Armadillo特定大小的成员函数 . 相反,库选择调用 STL 容器的 .size() 成员函数,armadillo supported,因为它在两个对象之间共享 . 但是,犰狳限制了成员函数在"active"数据结构中的实现位置,而不是临时数据 . 因此, .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
    

相关问题