【问题标题】:RcppArmadillo sample.h compiling errorRcppArmadillo sample.h 编译错误
【发布时间】:2017-08-27 19:06:49
【问题描述】:

我正在编写一个包,其中包含一些从 RcppArmadillo 调用 RcppArmadillo::sample 的函数。 但是我在编译时遇到了以下错误。

在 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 = 双] ./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。它突然不起作用。我很感激任何帮助。

【问题讨论】:

  • 如果没有上下文来说明您要完成的工作,这并没有真正的帮助。 RcppArmadilloExtensions/sample.h 有很多测试,所以我在这里有点怀疑。
  • 您的编译器可能太旧了。您正在通过 R 3.0.0(大约三年前/已过时)进行安装有点说明问题。

标签: r rcpp


【解决方案1】:

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


您遇到的问题是您决定在调用采样中对数据进行子集化。 (您省略了诊断这部分的代码,所以我在这里推测。)由于sample() 需要同时使用 Rcpp 和 Armadillo 数据类型,因此从未调用过特定大小的 Armadillo 成员函数。相反,库选择调用STL 容器的.size() 成员函数,它是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

【讨论】:

  • 非常感谢您的帮助。这正是我所需要的。我确实将 col 的一个子集传递给了示例函数。现在我知道如何解决它了。
猜你喜欢
  • 2017-03-07
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
相关资源
最近更新 更多