## Absolute value of complex numbers

Tuesday, March 8th, 2011Max Hadley from Schlumberger in Southampton, UK came to us with an interesting bug report regarding the `MaxAbsValue()` and `MaxAbsIndex()` functions as applied to complex vectors in the `NMathFunctions` class. Most of the time these methods worked as expected, but they would intermittently fail to correctly identify the maximum element in large vectors with similar elements.

In researching the MKL documentation we found that this was in fact not a problem from MKL’s perspective. MKL uses the L1-norm, or Manhattan distance from 0, as a metric to compute the absolute value of a complex number. This simply means that it adds together the absolute values of the real and imaginary components:

We had expected the absolute value to be computed via the L2-norm, or Euclidean distance from zero, which is referred to in places as the magnitude metric. Interestingly, MKL uses the L1-norm because that is the norm defined by the underlying BLAS standard, and apparently the original designers of BLAS choose that norm for computational efficiency. This means that all BLAS-based linear algebra packages compute the norm of a complex vector in this way – and it’s probably not what most people expect.

This was a tricky bug to find for two reasons. First, substituting one norm for the other did not elicit incorrect behavior often because the real component generally dominates the magnitude. Second, the actual calculation of the absolute value of a complex number (rather than the maximum absolute value of a complex vector) has always been calculated using the L2-norm.

Now that we found the problem, we faced the unenviable task of trying to make our API consistent while interfacing with MKL and how it deals with finding the maximum absolute value element in a vector of complex numbers. We started by suffixing all complex versions of min and max abs methods that use MKL and therefore use the L1-norm to compute the absolute value of complex numbers with a ’1′:

public static int MaxAbs1Index( FloatComplexVector v ) public static int MaxAbs1Value( FloatComplexVector v ) public static int MinAbs1Index( FloatComplexVector v ) public static int MinAbs1Value( FloatComplexVector v ) public static int MaxAbs1Index( DoubleComplexVector v ) public static int MaxAbs1Value( DoubleComplexVector v ) public static int MinAbs1Index( DoubleComplexVector v ) public static int MinAbs1Value( DoubleComplexVector v ) |

And we have subsequently written new methods that compute the maximum and minimum absolute values of a complex vector according to the L2-norm, or Euclidean distance, of its elements. Users should be aware that these methods do not use MKL:

public static int MaxAbsIndex( FloatComplexVector v ) public static int MaxAbsValue( FloatComplexVector v ) public static int MinAbsIndex( FloatComplexVector v ) public static int MinAbsValue( FloatComplexVector v ) public static int MaxAbsIndex( DoubleComplexVector v ) public static int MaxAbsValue( DoubleComplexVector v ) public static int MinAbsIndex( DoubleComplexVector v ) public static int MinAbsValue( DoubleComplexVector v ) |

We hope the change is intuitive and useful.

Darren