 # Boost :

Subject: [boost] Determining interest: Pure imaginary number library
From: Matthieu Schaller (matthieu.schaller_at_[hidden])
Date: 2011-11-15 18:40:04

Dear all,

I have recently been playing a lot on numerical analysis problems
containing complex numbers and functions. I have been using the SL
std::complex<> library and could observe that some of the algorithms
weren't optimized in cases where complex numbers have to be multiplied by
pure imaginary numbers (i.e. x*sqrt(-1)). I have implemented a class to
represent these numbers and overloaded all relevant operators in a more
optimized fashion thanks to the knowledge that the real part of these
complex numbers is 0. I could observe an important speed-up in some
calculations. As an example, this piece of code (1D Schroedinger equation
using finite differences):

for(size_t i(0); i<nbPoints; ++i)
{
Psi_new[i] = Psi_old[i]
+ ii * hbar * deltaT / (12.*m*deltaX*deltaX) *
(-Psi_cur[i-2] + 16.*Psi_cur[i-1] - 30.*Psi_cur[i] + 16.*Psi_cur[i+1] -
Psi_cur[i+2])
- 2. * ii * V(x[i]) / hbar * deltaT * Psi_cur[i];

}

is more than 2 times faster (g++ 4.6.1 core i7 2820QM) when the imaginary
unit (constant) variable ii is defined to be

Imaginary<double> ii(1.);

than when it is defined as

std::complex<double> ii(0.,1.);

Some other expressions also benefit from this improvement and can run
faster. Expressions including square roots or exponentials of pure
imaginary numbers are improved a lot.

The range of application of such a class is quite narrow but it may be
helpful to speed-up some simple numerical problems. The source code as well
as a working example is available here: