# R or Cpp for some finance work involved complex numbers?

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?

• A very warm welcome to Quant.SE! Thank you for question, please see my answer below. – vonjd Oct 5 '19 at 14:51
• ...for "your" question, of course ;-) – vonjd Oct 5 '19 at 16:24
• If my answer helped you it is good practice here to upvote and accept it - Thank you – vonjd Oct 7 '19 at 11:46

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)
##  2+2i

doubleMe(c(1+1i, 2+2i))
##  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)
##  2+2i

doubleMeR(c(1+1i, 2+2i))
##  2+2i 4+4i