Article

# Complex math library for C# and VB.NET

, 15 Dec 2002
 Rate this:
Complex math library for C# and VB.NET.

## Introduction

The .NET platform doesn't have complex numbers built in. If you do scientific calculations such as groundwater modeling, complex numbers are essential. This article describes a full implementation of complex numbers for .NET, and how to use it with VB or C#.

Complex numbers have a real and imaginary part. Math operations are performed on complex numbers using special rules to keep track of the real and imaginary parts. Fortran and C++ have complex numbers built in.

## C# Example (cs_complex.cs)

using System;
using KarlsTools;

class TestComplex{
static void Main(string[] args)
{
Complex c1 = new Complex(3.0, 4.0);
double d = Complex.Abs(c1);
Console.WriteLine("Test Complex,  d = "+ d);
}
}

Compile and run the above code with the following commands:

c:\>csc cs_complex.cs /r:complex.dll
C:\>cs_complex
Test Complex, d = 5

## VB .NET Example (vb_complex.vb)

Imports System
Imports KarlsTools

Module Module1

Sub Main()
Dim c1 As Complex = New Complex(3.0, 4.0)
dim d as Double = Complex.Abs(c1)
Console.WriteLine("Test Complex,  d = "& d.ToString())
End Sub
End Module

Compile and run the above code with the following commands:

c:\>vbc vb_complex.vb /r:complex.dll /r:System.dll
c:\>vb_complex.exe
Test Complex, d = 5

## Implementation

This class is implemented using Managed C++. It duplicates the capabilities of the Fortran complex*16 type, and is a value type class with static (shared) math functions. This is how the .NET Math class is designed. Non-trivial methods are wrappers around the Standard Template Library (STL) <complex> class. The library has been tested with C# against all C++ STL <complex> sample output on Microsoft's web site.

Use either Visual Studio or the make file to compile the library (complex.dll). If you have Visual C++, open complex.vcproj and build. An alternate way to compile is by typing 'nmake' from a command prompt. A make file is included with the download.

## Issues when wrapping a C++ STL class for use with VB and C#

An easy way to provide complete functionality is to wrap the C++ STL <complex> class using Managed C++. This works well but wrapped methods run twice as slow as a method written from scratch. This is because the MSIL code generated by the C++ compiler has calls to the System.Runtime.CompilerServices to access the STL. I compromised by writing trivial methods from scratch and relied on the STL implementation otherwise.

This class duplicates the FORTRAN complex*16 type. Eight bytes for the real part, and 8 bytes for the imaginary part, by using std::complex<double>. This is not as flexible as the C++ STL class which allows double, float, or int to be used.

Extra code was added to provide VB functionality. The C++/C# operators !=, ==, +, -, * and / didn't directly work in VB. I added methods: NotEqual, Equals, Add, Subtract, Multiply and Divide to provide complete functionality in VB. I do not understand why I needed to do this - please comment.

Other
United States
Karl is a Water Resources Engineer and Programmer. He holds a Masters degree in Civil Engineering, and is a Microsoft Certified Solution Developer.

 View All Threads First Prev Next
 Cool Hard Coder 15-Feb-10 9:01
 Last Visit: 31-Dec-99 18:00     Last Update: 1-Aug-14 17:37 Refresh 1