Rcpp Armadillo:RStudio 说 "exp" 不明确
Rcpp Armadillo: RStudio says "exp" is ambiguous
我正在使用以下代码在 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 都没有经验。
现场图
首先,不要这样做
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::exp()
来自 Rcpp Sugar,使用我们的载体
arma::exp()
来自 Armadillo 使用它的向量
而且我一直觉得直白最容易。
编辑: 我错过了 log1p
。用 std::
作为前缀也需要 C++11。进行了两项更改。
我正在使用以下代码在 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 都没有经验。
现场图
首先,不要这样做
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::exp()
来自 Rcpp Sugar,使用我们的载体arma::exp()
来自 Armadillo 使用它的向量
而且我一直觉得直白最容易。
编辑: 我错过了 log1p
。用 std::
作为前缀也需要 C++11。进行了两项更改。