Complex Numbers in .NET 4

I have recently been fortunate enough to venture back into the world of math, specifically dealing with the idea of complex numbers. I am not sure how many developers are really into math so I will recap some concepts of complex numbers.

One of the main properties of a real number is that it square is nonnegative. For example, there is no real number x for which the following equation is true:

x² = -1

To help solve this kind of problem, a number called the imaginary unit was introduced (i).  The imaginary unit is the number whose square is –1, that is

i² = -1

The system that results from introducing the number i is called the complex number system. Complex numbers are numbers of the form a + bi, where a and b are real numbers . The real number a is referred to as the real part of the number a + bi, while the real number b is called the imaginary part of a + bi. For example the complex number -2 + 3i has the real part of -2 and the imaginary part of 3.

Now there are some basic rules for adding, subtracting and multiplying complex numbers as follows:

  • (a + bi) + (c + di) = (a + c) + (c + d)i
  • (a + bi) -  (c + di) = (a - c) + (c - d)i
  • (a + bi) * (c + di) = (ac - db) + (ad - bc)i

In .NET 4.0 we saw the introduction of a new struct designed to represent the complex number called Complex. With this new type the complex number addition, and multiplication patterns become really easy.

using System.Numerics;
Complex cpx1 = new Complex(2, 3);
Complex cpx2 = new Complex(4, 6);
Complex cpx3 = new Complex(1, 5);
Complex cpxsum = cpx1 + cpx2 - cpx3; // produces 5 + 4i
Complex cpxsum2 = cpx1 * cpx3; // produces -13 + 13i

Related Links:

Comment Section

Comments are closed.