Rcpp犰狳:RStudio 说“exp"模棱两可 [英] Rcpp Armadillo: RStudio says "exp" is ambiguous

查看:122
本文介绍了Rcpp犰狳:RStudio 说“exp"模棱两可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying out Rcpp / RcppArmadillo in RStudio with the following code:

#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).

RStudio says the calls to exp are ambiguous. I've tried calling std::exp in the code instead of using std::exp but have no success. The code compiles without warnings through Rcpp::sourceCpp('filename.cpp'). If I cast (float)x(ii) in the code the warning disappears, but not if I cast (double)x(ii).

感谢任何见解,我对 C++ 和 RStudio 都缺乏经验.

Any insight appreciated, I'm pretty inexperienced with both C++ and RStudio.

正在发生的事情的图片

推荐答案

对于初学者来说,不要做

For starters, don't do

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

如有疑问,请明确说明.你的代码就变成了

If in doubt, be explicit. Your code then becomes

#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).

and compiled without a hitch (after I also changed int to size_t for the loop) -- and without an issue in the RStudio IDE (using a fairly recent daily, 1.0.116).

  • std::exp() 在标准库中,使用double
  • Rcpp::exp() 来自 Rcpp Sugar,使用我们的向量
  • arma::exp() 来自 Armadillo 使用其向量
  • std::exp() in the standard library, using double
  • Rcpp::exp() from Rcpp Sugar, using our vectors
  • arma::exp() from Armadillo using its vectors

而且我总是发现明确表达最容易.

and I always found it easiest to be explicit.

我错过了log1p.以 std:: 为前缀也需要 C++11.进行了两项更改.

I had missed log1p. Prefixing it with std:: also requires C++11. Two changes made.

这篇关于Rcpp犰狳:RStudio 说“exp"模棱两可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆