Rcpp Armadillo:RStudio说" exp"很暧昧

时间:2016-12-06 14:29:09

标签: c++ r rstudio rcpp

我正在使用以下代码在RStudio中尝试Rcpp / RcppArmadillo:

#include <RcppArmadillo.h>

//[[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;
using std::exp;
using std::log1p;

// [[Rcpp::export]]
arma::vec log1pexp(arma::vec x) {
  for(int ii = 0; ii < x.n_elem; ++ii){
    if(x(ii) < 18.0){
      x(ii) = log1p(exp(x(ii)));
    } else{
      x(ii) = x(ii) + exp(-x(ii));
    }
  }
  return x;
}

RStudio表示对exp的调用含糊不清。我尝试在代码中调用std::exp而不是using std::exp,但没有成功。代码通过Rcpp::sourceCpp('filename.cpp')编译而没有警告。如果我在代码中转换(float)x(ii)警告消失,但不是  我投了(double)x(ii)

任何洞察力都赞赏,我对C ++和RStudio都缺乏经验。

图片

enter image description here

1 个答案:

答案 0 :(得分:2)

首先,不要做

using namespace Rcpp;
using std::exp;
using std::log1p;

如果有疑问,请明确。然后你的代码变成

#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::plugins(cpp11)]]

// [[Rcpp::export]]
arma::vec log1pexp(arma::vec x) {
    for(size_t ii = 0; ii < x.n_elem; ++ii){
        if(x(ii) < 18.0){
            x(ii) = std::log1p(std::exp(x(ii)));
        } else{
            x(ii) = x(ii) + std::exp(-x(ii));
        }
    }
    return x;
}

并且顺利编译(在我将int更改为size_t循环之后) - 并且在RStudio IDE中没有问题(使用相当近的日期,1.0.116)。

    使用std::exp() 在标准库中
  • double 来自Rcpp Sugar的
  • Rcpp::exp(),使用我们的矢量
  • 来自犰狳的
  • arma::exp()使用其矢量

我总是觉得最容易明白。

编辑:我错过了log1p。用std::作为前缀也需要C ++ 11。做了两处改动。