4

I need to implement some pricing functions which involve complex numbers. The equations involve various expressions such as $Re$ and $Img$ (i.e the real and imaginary part of the complex number), and I need to do some Fast Fourier Transforms and other things. Obviously the end result is always real, but the intermediate calculations are complex.

I have never used complex numbers before when I have programmed, so is there something particular I should be aware of? I want to use either R or C++: which would be more suitable for handling complex numbers? Are there any computational difficulties when dealing with complex numbers?

1 Answers1

5

Why choose? C++ is perfectly integrated into R via the excellent Rcpp package (on CRAN). And you can use complex numbers there too:

library(Rcpp)
cppFunction("ComplexVector doubleMe(ComplexVector x) { return x+x; }")
doubleMe(1+1i)
## [1] 2+2i

doubleMe(c(1+1i, 2+2i))
## [1] 2+2i 4+4i

I would suggest starting out with R and if you run into performance problems, use C++ via Rcpp.

Just for reference, the same as above in Base R:

doubleMeR <- function(x) x+x 
doubleMeR(1+1i)
## [1] 2+2i

doubleMeR(c(1+1i, 2+2i))
## [1] 2+2i 4+4i
vonjd
  • 27,437
  • 11
  • 102
  • 165