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.

 First Prev Next
 S....L....O....W.... mattsoundworld 14-Oct-11 3:34
 Updates and extensions bitzblitz 14-Sep-10 3:54
 Cool Hard Coder 15-Feb-10 9:01
 hi Jay200032 29-Mar-09 12:18
 Norm of 3 and 4 won`t be 25. vitamine1983 1-Nov-08 20:51
 Re: Norm of 3 and 4 won`t be 25. bitzblitz 14-Sep-10 4:09
 Convert Double Precision Trupti Mehta 7-Oct-08 20:45
 Help Vucina 7-Aug-08 12:30
 Re: Help Karl Tarbet 9-Aug-08 8:23
 Thank you davoodrajabi 2-Nov-07 20:35
 A Simple Pure C# Complex Class bossin 8-Jun-06 16:29
 Re: A Simple Pure C# Complex Class boaza 14-Nov-07 21:17
 Re: A Simple Pure C# Complex Class leppie 12-Aug-08 0:19
 Re: A Simple Pure C# Complex Class thefellow3j 18-Oct-10 5:17
 Windows Forms support? Bill2005 31-Dec-05 20:24
 Re: Windows Forms support? Karl Tarbet 2-Jan-06 4:17
 Bill,   I recompiled with VS 2005, and it fixes the problems.   The version on codeproject was compiled with the old VS 2002.   You might want to try the Nmath library. It is pure managd code and can be downloaded from nmath.sourceforge.net It has a good complex math library, see the file src\Complex.cs (MathNet.Numerics-v0.3)       -- modified at 10:18 Monday 2nd January, 2006
 Visual Studio 2005 support? Bill2005 31-Dec-05 20:18
 Re: Visual Studio 2005 support? Karl Tarbet 1-Jan-06 4:46
 Re: Visual Studio 2005 support? Bill2005 1-Jan-06 14:42
 I must say it's very good piece of work piotr.kolodziej 11-Dec-05 8:45
 You don't have to Doker 20-Oct-05 21:51
 american history indian Anonymous 7-Mar-05 13:31
 <complex> and managed code PDHB 12-Oct-03 15:43
 This seems, well, too complex Marc Clifton 16-Dec-02 3:37
 Re: This seems, well, too complex Karl Tarbet 16-Dec-02 13:24
 Re: This seems, well, too complex Marc Clifton 17-Dec-02 1:18
 Last Visit: 31-Dec-99 18:00     Last Update: 12-Jul-14 20:48 Refresh 1