Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

.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 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.

Posted: Oct 23 2009, 11:20 AM by DigiMortal | with 24 comment(s)
Filed under: ,

Twitter Trackbacks for .Net Framework 4.0: Complex numbers - Gunnar Peipman's ASP.NET blog [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 .Net Framework 4.0: Complex numbers - Gunnar Peipman's ASP.NET blog         [asp.net]        on Topsy.com

# October 23, 2009 4:50 AM

DotNetBurner - .net Framework said:

DotNetBurner - burning hot .NET content

# October 23, 2009 5:01 AM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# October 23, 2009 5:10 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# October 23, 2009 5:18 AM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# October 23, 2009 5:26 AM

9eFish said:

# October 23, 2009 5:33 AM

Gunnar Peipman's ASP.NET blog said:

Here are my postings about Visual Studio 2010 and .Net Framework 4.0 that may be interesting to my readers

# October 23, 2009 5:58 AM

.Net Framework 4.0: Complex numbers - Gunnar Peipman's ASP.NET blog said:

Pingback from  .Net Framework 4.0: Complex numbers - Gunnar Peipman&#39;s ASP.NET blog

# October 23, 2009 7:59 AM

Héctor said:

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 8:11 AM

RichardD said:

@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 23, 2009 10:05 AM

Владимир Юнев said:

# October 23, 2009 12:29 PM

Владимир Юнев said:

Перевод What's New in the BCL in .NET 4 Beta 2 [Justin Van Patten] Visual Stu

# October 23, 2009 12:35 PM

Servefault.com said:

Thank you for submitting this cool story - Trackback from Servefault.com

# October 23, 2009 11:24 PM

DigiMortal said:

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 24, 2009 8:34 AM

Visual C# Kicks said:

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 4:00 AM

Héctor said:

I've had to made some charting and graphing applications in the past, and my current project works around different maps, so Complex is a class that I'd use.

When wanting some consistency one would either export a custom class to an external library, and/or just use the Point/PointF structures.

My main concern with adding this sort of classes to the core can be seen in this blog post:

blogs.lessthandot.com/.../do-we-need-to-know-basic-math-as-program

I think this encourages some people not to worry about learning some basic concepts, the same goes for the new Enum.HasFlag method. Just my humble opinion anyway, and as I've previously said, if this uses some native CPU instructions to perform better then I see a more robut reason to use it.

# October 25, 2009 8:02 AM

Matt Watson said:

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

# October 25, 2009 4:12 PM

cool stuff :)

# October 25, 2009 4:36 PM

Bob Cravens said:

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

# October 26, 2009 11:31 AM

RichardD said:

@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."

# November 3, 2009 10:44 AM

Power group said:

hi

i have been trying to use this class, but i get error "Numerics is not a member of system". I am not a programer but an engineer. can any body help me resolve this problem.

thanks

# December 16, 2009 11:40 PM

DigiMortal said:

You need Visual Studio 2010 to use System.Numerics namespace.Older versions of Visual Studio doesn't have it.

# December 17, 2009 12:34 PM

.NET 4.0: ???? ???????????? ?? ?????????????? ???????????? (BCL)? ?????????????????? ?????????? | ???????? ?????????? ?????????? said:

Pingback from  .NET 4.0: ???? ???????????? ?? ?????????????? ???????????? (BCL)? ?????????????????? ?????????? | ???????? ?????????? ??????????

# January 22, 2010 6:04 PM

Level Up said:

.NET 4.0 New Feature - Complex

# November 1, 2010 7:57 AM