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

Baptiste Auguie baptiste.auguie at gmail.com
Tue Feb 21 22:36:07 CET 2017


Hi,

I think you can/should use std::complex, see for example the types used in

https://github.com/baptiste/rcppfaddeeva/blob/master/src/callFaddeeva.cpp

In general I prefer to work with RcppArmadillo for numerical (including
complex) computations, its syntax closely follows R and Matlab. See for
instance

https://github.com/baptiste/cda/blob/master/src/cda.cpp
https://github.com/baptiste/planar/blob/master/src/fresnel.cpp

for some examples.

HTH,

baptiste



On 22 February 2017 at 04:46, Martin S Ridout <M.S.Ridout at kent.ac.uk> wrote:

> 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
>
> cppFunction('
>   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
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20170222/04fa514b/attachment-0001.html>


More information about the Rcpp-devel mailing list