Stéphane Laurent
Stéphane Laurent

Reputation: 84529

Complex gamma function in Rcpp

As I understand from the doc, the tgamma function of the boost C++ library can be evaluated for complex numbers.

I'm trying to use it in Rcpp. This is my code:

// [[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;
}

This code doesn't compile. I get the error:

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

Upvotes: 0

Views: 909

Answers (2)

Rakete1111
Rakete1111

Reputation: 48958

Basically, you are calling the wrong function.

You didn't specify a namespace, so ADL finds std::tgamma from the Standard Library due to z.

std::tgamma doesn't take std::complex as parameters, so you get a compiler error. You want boost::math::tgamma instead.

But Boost's tgamma also doesn't support std::complex types, so you'll need to use another library or implement it yourself.

Upvotes: 2

Dirk is no longer here
Dirk is no longer here

Reputation: 368261

You may have the wrong tgamma() here, try boost::math::tgamma(...). And/or you may need to template on std::complex.

My usual approach is to get something to work first on the command-line, and then attach such code to R via Rcpp.

Upvotes: 2

Related Questions