cpp-imaginary-numbers


A fully SL compliant complex numbers C++ class

The C++ standard library contains a class and a set of functions to handle complex numbers. The std::complex<> class allows to do computation in many fields of science but can be improved in two ways.

The first improvement is the addition of an imaginary number class. In many applications, multiplications of complex numbers with pure imaginary numbers (i.e. a multiple of the imaginary unit sqrt(-1)) occur. Using only the SL class, one has to define these imaginary numbers as a complex number with a 0-valued real pars. The complex multiplication requires 4 real multiplications and 2 real additions whereas the knowledge that the real part of one of the numbers is 0 could reduce this to 2 real multiplications and one negation. The same is true for division and for many other operations. The purpose of this proposal is to implement pure imaginary numbers.

THe second improvement concerns the complex<> class itself. According to the C++ standard, the effect of instanciating the complex template class for any other type than float, double and long double is unspecified. In practice, instances of std::complex<OtherType> will be fonctional in cases where no mathematical functions involving OtherType are used. This is because the usual implementations of std::complex<> use the SL math functions which are only defined for float, long and double.

The addition of an imaginary<> class implies that the complex<> class has to be rewritten and it has been done in such a way that the class can be used with any type providing the mathematical functions such as sin(), cos(), exp(), log() and abs().

The classes presented here implements all operators and mathematical functions present in the std::complex<> header plus a few more. Most of them get improved both when dealing with imaginary numbers in terms of speed and precision due to the knowledge that the real part of these pure imaginary numbers is identically 0.

This project is made publicly available here in order for the boost community to review it.

Project Information

The project was created on Nov 15, 2011.

Labels:
CPlusPlus Library Numerics