Update: Things have changed since this article was written in 2002. (Thanks artcodingSB for the note) .NET now has complex types built in. If you are using an older version of .NET, this might still be helpful.
The .NET platform doesn't have complex numbers built in. If you do scientific calculations such as groundwater modeling, complex numbers are essential. This tip 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.
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
Complex Number Class for .NET
List of Functionality
C# Example
using System; using KarlsTools;
Complex(double real, double imag)
Complex c1 = new Complex(3,4);
String ToString()
string s = "c1 = "+c1.ToString();
Static double Abs(Complex c)
double d = Complex.Abs(c1);
Static double Arg(Complex c)
double d2 = Complex.Arg(c1);
Static Complex Conj(Complex c)
Complex c2 = Complex.Conj(c1);
Static double Imag(Complex c)
double imag =Complex.Imag(c2);
Static double Real(Complex c)
double real =Complex.Real(c1);
Double Imag()
double imag2 = c1.Imag();
Double Real()
double real2 = c1.Real();
Static Complex Polar(double r, double theta)
Complex p = Complex.Polar(5,Math.PI/180);
Static Complex Cos(Complex c)
Complex c3 = Complex.Cos(p);
Static Complex Cosh(Complex c)
Complex c4 = Complex.Cosh(p);
Static Complex Exp(Complex c)
Complex c5 = Complex.Exp(p);
Static Complex Log(Complex c)
Complex c6 = Complex.Log(p);
Static Complex Log10(Complex c)
Complex c7 = Complex.Log10(p);
Static double Norm(Complex c)
double n = Complex.Norm(p);
Static Complex Pow(Complex base, double power)
Complex c9 = Complex.Pow(p,4);
Static Complex Pow(Complex base, Complex power)
Complex c10 = Complex.Pow(p,p);
Static Complex Pow(double base, Complex power)
Complex c11 = Complex.Pow(2,p);
Static Complex Sin(Complex c)
Complex c12 = Complex.Sin(p);
Static Complex Sinh(Complex c)
Complex c13 = Complex.Sinh(p);
Static Complex Sqrt(Complex c)
Complex c14 = Complex.Sqrt(p);
Static Complex Tan(Complex c)
Complex c15 = Complex.Tan(p);
Static Complex Tanh(Complex c)
Complex c15a = Complex.Tanh(p);

Complex c16 = c15;
+
Complex c17 = +c16;
==
bool eq = (c16 == c15);
bool eq2 = ( new Complex(2,0) == 2);
bool eq3 = ( 2 == new Complex(2,0));
!=
bool ne = (c16 != c16);
bool ne2 = ( new Complex(2,0) != 2);
bool ne3 = ( 2 != new Complex(2,0));
*
Complex c18 = c1*c1;
Complex c19 = c1*double.PositiveInfinity;
Complex c20 = 12*c1;
/
Complex c21 = c1/c1;
Complex c22 = c1/double.PositiveInfinity;
Complex c23 = 1/c1;
Complex c24 = c1+c1;
Complex c25 = c1+double.PositiveInfinity;
Complex c26 = 1+c1;
Complex c27 = c1c1;
Complex c28 = c1double.PositiveInfinity;
Complex c29 = 1c1;
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
c:\>vbc vb_complex.vb /r:complex.dll /r:System.dll c:\>vb_complex.exe Test Complex, d = 5
Imports KarlsTools
Dim c1 As Complex = New Complex(3, 4)
Dim s As String = "c1 = " + c1.ToString()
shared double Abs(Complex c)
Dim d As Double = Complex.Abs(c1)
shared double Arg(Complex c)
Dim d2 As Double = Complex.Arg(c1)
shared Complex Conj(Complex c)
Dim c2 As Complex = Complex.Conj(c1)
shared double Imag(Complex c)
Dim imag As Double = Complex.Imag(c2)
shared double Real(Complex c)
Dim real As Double = Complex.Real(c1)
double Imag()
Dim imag2 As Double = c1.Imag()
double Real()
Dim real2 As Double = c1.Real()
shared Complex Polar(double r, double theta)
Dim p As Complex = Complex.Polar(5, Math.PI / 180)
shared Complex Cos(Complex c)
Dim c3 As Complex = Complex.Cos(p)
shared Complex Cosh(Complex c)
Dim c4 As Complex = Complex.Cosh(p)
shared Complex Exp(Complex c)
Dim c5 As Complex = Complex.Exp(p)
shared Complex Log(Complex c)
Dim c6 As Complex = Complex.Log(p)
shared Complex Log10(Complex c)
Dim c7 As Complex = Complex.Log10(p)
shared double Norm(Complex c)
Dim n As Double = Complex.Norm(p)
shared Complex Pow(Complex base, double power)
Dim c9 As Complex = Complex.Pow(p, 4)
shared Complex Pow(Complex base, Complex power)
Dim c10 As Complex = Complex.Pow(p, p)
shared Complex Pow(double base, Complex power)
Dim c11 As Complex = Complex.Pow(2, p)
shared Complex Sin(Complex c)
Dim c12 As Complex = Complex.Sin(p)
shared Complex Sinh(Complex c)
Dim c13 As Complex = Complex.Sinh(p)
shared Complex Sqrt(Complex c)
Dim c14 As Complex = Complex.Sqrt(p)
shared Complex Tan(Complex c)
Dim c15 As Complex = Complex.Tan(p)
shared Complex Tanh(Complex c)
Dim c15a As Complex = Complex.Tanh(p)
Dim c16 As Complex = Complex.Negative(c15)
Dim c17 As Complex = Complex.Plus(c16)
Dim eq As Boolean = c16.Equals(c17)
Dim eq2 As Boolean = (New Complex(2, 0)).Equals(2)
Dim eq3 As Boolean = Complex.Equals(2, New Complex(2, 0))
Dim ne As Boolean = Complex.NotEqual(c16, c16)
Dim ne2 As Boolean = Complex.NotEqual(New Complex(2, 0), 2)
Dim ne3 As Boolean = Complex.NotEqual(2, New Complex(2, 0))
Dim c18 As Complex = Complex.Multiply(c1, c1)
Dim c19 As Complex = Complex.Multiply(c1, Double.PositiveInfinity)
Dim c20 As Complex = Complex.Multiply(12, c1)
Dim c21 As Complex = Complex.Divide(c1, c1)
Dim c22 As Complex = Complex.Divide(c1, Double.PositiveInfinity)
Dim c23 As Complex = Complex.Divide(1, c1)
Dim c24 As Complex = Complex.Add(c1, c1)
Dim c25 As Complex = Complex.Add(c1, Double.PositiveInfinity)
Dim c26 As Complex = Complex.Add(1, c1)
Dim c27 As Complex = Complex.Subtract(c1, c1)
Dim c28 As Complex = Complex.Subtract(c1, Double.PositiveInfinity)
Dim c29 As Complex = Complex.Subtract(1, c1)
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. Nontrivial 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.
static
Math
<complex>
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.
nmake
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.
System.Runtime.CompilerServices
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.
std::complex<double>
double
float
int
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.
NotEqual
Equals
Add
Subtract
Multiply
Divide
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.