[Rcpp-devel] Using complex numbers and functions in Rcpp

Martin S Ridout M.S.Ridout at kent.ac.uk
Tue Feb 21 16:46:35 CET 2017

Apologies if this is a naive  question - I'm a beginner with Rcpp.  I did see some earlier discussion on similar topic, but I don't think it answered my questions.

I am trying (on Windows) to speed up a function currently written in R. This works very well for real-valued arguments of the function. But it needs to work for complex arguments as well. Here is a stripped down (but still non-working) example of what I am trying to do

  ComplexVector test(ComplexVector s, ComplexVector lambda) {

    int n = s.size();
    int m = lambda.size();
    ComplexVector out(n);

    for (int i=0; i < n; ++i) {
      for (int j=0; j < m; ++j) {
         out[i] = out[i] - log(s[i] + 1/lambda[j]) / 2;
    return out;

I have (at least) 2 problems

i) Are the logarithm and exponential functions implemented for complex arguments? If not is there a work-around?

ii) What is the best way to specify constants like 1 and 2  as complex constants? This must be easy, but everything I tried so far gives an error (except passing them into the function via an extra ComplexVector argument, but there must be a better way).

Many thanks for any help,
Martin Ridout

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20170221/5dc6210b/attachment.html>

More information about the Rcpp-devel mailing list