Rcpp中的复杂伽玛函数

时间:2017-08-05 17:02:41

标签: c++ r boost rcpp

据我了解from the doc,可以评估tgamma C ++库的boost函数的复数。

我试图在Rcpp中使用它。这是我的代码:

// [[Rcpp::depends(BH)]]
#include <Rcpp.h>
#include <boost/math/special_functions/gamma.hpp>

// [[Rcpp::export]]
std::complex<double> gamma_boost(std::complex<double> z) { 
  std::complex<double> result = tgamma(z);
  return result;
}

此代码无法编译。我收到错误:

cannot convert 'std::complex<double>' to 'double' for argument '1' to 'double tgamma(double)'

2 个答案:

答案 0 :(得分:2)

您在这里可能有错tgamma(),请尝试boost::math::tgamma(...)。和/或您可能需要在std::complex上模板。

我通常的做法是先在命令行上运行一些东西,然后通过Rcpp将这些代码附加到R.

答案 1 :(得分:2)

基本上,你正在调用错误的函数。

您没有指定命名空间,因此由于z,ADL会从标准库中找到std::tgamma

std::tgamma不会将std::complex作为参数,因此会出现编译错误。您想要boost::math::tgamma

Boost的tgamma也不支持std::complex类型,因此您需要使用其他库或自行实施。

相关问题