4
$\begingroup$

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?

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

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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.