Handling Complex Numbers in R and C++ for Pricing Work
Summary
The document addresses how to implement finance calculations that use complex intermediates, including real and imaginary components and Fourier transforms, when the final pricing result is real. It compares R and C++ and notes that both support complex-valued calculations. R can be used directly for basic operations, while C++ complex types can be accessed from R through the Rcpp interface.
The practical recommendation is to begin in R and move performance-sensitive work to C++ through Rcpp if profiling reveals a need. The examples demonstrate basic complex addition in both environments and show how C++ exposes a complex number’s real and imaginary components. The material is introductory: it does not discuss numerical stability, FFT implementation details, precision tradeoffs, or benchmark results, so those issues must be evaluated for the specific pricing method and workload.
Key ideas
- R and C++ both support complex-valued calculations.
- Rcpp lets R code call C++ functions that accept and return complex vectors.
- Starting in R provides a simple path, with C++ available when performance becomes an issue.
- C++ complex types provide access to a value’s real and imaginary components.
- The examples do not evaluate numerical stability or compare execution speeds.
Tags
Full text
# R or Cpp for some finance work involved complex numbers?
# 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?
## Answer by vonjd (score 5)
https://quant.stackexchange.com/a/49070
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
```
## Answer by Ian (score 0)
https://quant.stackexchange.com/a/79724
You can try the following codes to get a feeling:
```
#include <complex>
std::complex<double> z = (1,1); // 1+1i
z.real(); // the real part
z.imag(); // the imaginal part
```
Please refer to https://en.cppreference.com/w/cpp/numeric/complex for the details.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.