Complex numbers in .NET with NMath

A set of classes for working with complex numbers is not including in the .NET framework. These classes are frequently hand rolled by programmers to fill an immediate need, but this forces the developer into an on-going task developing compatible numeric algorithms with these custom classes. CenterSpace’s NMath libraries solve this issue by providing a framework with an extensive set of numeric classes that work natively with complex numbers.

Complex Number Classes

The CenterSpace NMath framework contains two classes to represent complex numbers at two different levels of precision. Additionally, classes are included for supporting vectors and matrices of complex numbers which are integrated into NMath’s extensive linear algebra classes.

 
FloatComplex
DoubleComplex
FloatComplexVector
DoubleComplexVector
FloatComplexMatrix
DoubleComplexMatrix

Each class supports a natural set of overloaded operations that include testing for equality, and inequality, along with addition, subtraction, multiplication, and division where appropriate. Here’s a simple example are some basic uses cases with these classes.

DoubleComplex dc1 = new DoubleComplex(1, 1);
DoubleComplex dc2 = new DoubleComplex(2, 2);
 
DoubleComplex dc_add = dc1 - dc2;
DoubleComplex dc_div = dc1 / dc2;
DoubleComplex dc_sin = NMathFunctions.Sin(dc1);
 
DoubleComplexVector dcv1 = new DoubleComplexVector([(1,3.3) (2,3) (3,5)];
DoubleComplexVector dcv2 = new DoubleComplexVector([(1,1) (-2,3) (3,-5.7)];
 
DoubleComplexVector dcv = dcv1 + dcv2;
DoubleComplex dcv_dot = NMathFunctions.Dot(dcv1, dcv2);

Basic Supporting Functions

The NMath numerical framework contains a static class called NMathFunctions that provides fast implementations of dozens of common functions for all of the basic NMath types, including all of the complex types. Frequently, new NMath user’s often wish they had discovered this class sooner than they did.

Below is a brief summary of the functions available in the NMathFunction class that operate on complex types. These functions span the range from basic operations such as Abs, to a variety to vector helper functions such as the Dot product, to more advanced functions such as computing the determinate of a matrix, or solving a set of linear equations. Using these functions will help you create correct, efficient, and more compact code.

  • Abs, AbsSum, Sum
  • Exp, Expm, Log, Pow, Sqrt, Arg
  • Imag, Real, Conj
  • ConjDot, OuterProduct, Dot
  • Transpose, ConjTranspose, TransposeProduct, ConjTransposeProduct
  • Cos, Cosh, Sin, Sinh, Tan, Tanh
  • SumOfSquares, Variance
  • MaxAbsIndex, MinAbsIndex, Median, Mean
  • ConditionNumber, Determinant, Inverse, Solve

Summary

The CenterSpace framework for computation on the .NET platform seamless integrates complex types. While most of our customers are leveraging more sophisticated classes in NMath, a complete set of complex types with a surrounding framework is a key component to getting the job done quickly and accurately.

-Paul

Leave a Reply

Your email address will not be published. Required fields are marked *

Top