.Net Framework 4.0: Complex numbers

.Net Framework 4.0 Beta 2 introduces new class in System.Numerics namespace: Complex. Complex represents complex numbers and enables different arithmetic operations with complex numbers. In this posting I will show you how to use complex numbers in .Net Framework 4.0 applications.

Complex class has two constructors – one of them has no arguments and the other takes real and complex parts of complex number. Complex numbers have also properties for phase and magnitude. Let’s see the following code.

static void Main(string[] args)
{

var z1 = new Complex(); // this creates complex zero (0, 0)
var z2 = new
Complex(2, 4);

var z3 = new
Complex(3, 5);

Console.WriteLine("Complex zero: "
+ z1);

Console.WriteLine(z2 + " + " + z3 + " = "
+ (z2 + z3));

Console.WriteLine("|z2| = "
+ z2.Magnitude);

Console.WriteLine("Phase of z2 = "
+ z2.Phase);

}

The output of this code is as follows.

Complex zero: (0, 0)
(2, 4) + (3, 5) = (5, 9)
|z2| = 4,47213595499958
Phase of z2 = 1,10714871779409

As you can see from output there are some cool things:

• ToString() method of complex number formats complex number like in calculus courses in University (it is called Cartesian form).
• You can use complex numbers like any other numbers and use usual operators on them.

If you feel that this is not good for some reason then you can also use static methods of Complex like Add(), Divide() etc. Following example illustrates how to use methods for calculations.

static void Main(string[] args)
{

var z2 = new
Complex(2, 4);

var z3 = new
Complex(3, 5);

var
z4 = Complex.Add(z2, z3);

var
z5 = Complex.Subtract(z2, z3);

var
z6 = Complex.Multiply(z2, z3);

var
z7 = Complex.Divide(z2, z3);

Console.WriteLine("z2 + z3 = "
+ z4);

Console.WriteLine("z2 - z3 = "
+ z5);

Console.WriteLine("z2 * z3 = "
+ z6);

Console.WriteLine("z2 / z3 = "
+ z7);

}

This example produces the following output.

z2 + z3 = (5, 9)
z2 - z3 = (-1, -1) z2 * z3 = (-14, 22) z2 / z3 = (0,764705882352941, 0,0588235294117647)

There are also other mathematical operations defined for complex numbers like trigonometric ones and logarithms, so you can do basically everything you like with complex numbers. I found one thing missing – Parse() and TryParse() methods. Let’s hope these methods are available in stable version of .Net Framework 4.0.

Liked this post? Empower your friends by sharing it!

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET. 9 thoughts on “.Net Framework 4.0: Complex numbers”

• October 23, 2009 at 12:11 pm

Interesting… but I don’t find this sort of “functionality” worth of being added as part of the framework core… unless it accesses native CPU instructions specially designed for working with complex numbers, of course.

• October 23, 2009 at 2:05 pm

@Héctor: I disagree. By that argument, *nothing* is worth adding to the framework core unless you use it.

Without the new System.Numerics classes, anyone who needs to manipulate complex numbers or arbitrarily large integers has to roll their own version of the code. Two assemblies using different implementations of a complex number can’t work together.

By adding these classes to the CLR, everyone can use the same implementation, without having to re-invent the wheel.

• October 24, 2009 at 12:34 pm

Thanks for comments guys! :)

I want to add one more point to RichardD’s ones. There are a lot of applications that use complex numbers and it is very good if framework also supports some more advanced calculus stuff.

• October 25, 2009 at 8:00 am

Woo.

I agree that adding this type of stuff to the Framework is very useful. It is something that’s not so specialized that only a few developers would find use for it. It also helps give consistency for applications working with complex numbers in this case.

• October 25, 2009 at 8:12 pm

Yeah… I’m not even sure what the heck this is or what it does but it sounds good :-)

• October 26, 2009 at 3:31 pm

I think the API for complex numbers should be the same as for regular numbers. I want to write code like the following:

Complex x = new Complex(2,3);
Complex y = new Complex(4,5);

Complex a = x + y;
Complex b = x – y;
Complex c = x * y;
Complex d = x / y;

Seems more natural then.

–Bob

• November 3, 2009 at 3:44 pm

@Bob: I think you missed this line in the post:
“You can use complex numbers like any other numbers and use usual operators on them.”

• December 17, 2009 at 4:40 am
• 