In this article I will explain you about, how to manipulate Complex numbers by using pretty much cool feature introduced in .net framework 4.0 with System.Numericsnamespace.Under this namespace there is a predefined Complex class with different parameters, properties and methods.This is one of the key enhancements in BCL(Base Class Library).
Before going to deep drive towards this feature. I will come out with general analogy regarding mathematical Complex numbers manual implementation and their basic formulas for better understanding the technical analogy for the beginners.
Complex Numbers:
Complex number is a number consists of a real number and an imaginary number. It can Written in the form a+ib, where a and b are real numbers and i is the standard imaginary unit with the property i^2=-1.
Notations:
- (a+ib) =(a,b) e.g. (2+i3)=(2,3) -Representation
- (a+ib)+(c+id)=(a+c)+i(b+d) -Addition
- (a+bi)-(c+di)=(a-c)+i(b-d)-Substraction
- (a+bi)(c+di)=(ac-bd)+i(ad+bc)-Multiplication
- (a+bi)/(c+di)=((ac+bd)+i(bc-ad))/(c^2+d^2)-Division
- |a+ib| read as magnitude of a+ib having the formula sqrt (a^2+b^2).
Note:
All the above mentioned notations are origin for Complex numbers....for better understanding purpose I illustrated here...if you are aware jump this phase.There is no need to remember all these notations ...and I am not mentioning trigonometric and logarithmic expressions, it's just like a kids play with Framework 4.0 base class library. I depicted below how to handle programmatically in C#.
Technical Focus on Complex Class:
This Feature was introduced in .net framework 4.0.Before Going to do the application we first add System.Numerics namespace as reference to the project in Visual Studio 2010 Beta 1 or Beta 2 or RC.I worked out this examples in VS2010 RC.
- Complex() Class Constructors
- Complex()-> no overloads represents (0,0) complex number
- Complex(double real, double imaginary)->having two overloads to manipulate complex numbers taking as double type.
- Static Methods: Abs(), Add(), Asin(), Atan(), Conjugate(), Cos(), Cosh(), Divide(), Equals(), Exp(), FromPolarCoordinates(), Log(), Log10(), Multiply(), Negate(), Pow(), Reciprocal(), Sin(), Sinh(), Sqrt(), Tan(), Tanh(). These are all the static functions in Complex Class.
-
Properties:
Magnitude: Calculates the sqrt (a^2+b^2).
My Hands on Experiment
The following Example demonstrates the Basic skeleton of Complex Numbers Manipulation
Example-1: Traditional approach
namespace ComplexNumbers
{
class Program
{
static void Main(string[]
args)
{
var c1
= new Complex(1, 2);
var c2
= new Complex(3, 4);
var add = c1 + c2;
Console.WriteLine("Complex
Numbers Addition:"+add);
var sub = c1 - c2;
Console.WriteLine("Complex
Numbers Substraction:"+sub);
var mul = c1 * c2;
Console.WriteLine("Complex
Numbers Multiplication:"+mul);
var div = c1 / c2;
Console.WriteLine("Complex
Numbers Division:"+div);
Console.ReadLine();
}
}
}
Output:
Complex Numbers Addition :(4, 6)
Complex Numbers Substraction:(-2, -2)
Complex Numbers Division:(-5, 10)
Complex Numbers Division :( 0.44, 0.08)
Example-2: Using Static Methods
namespace
ComplexNumbers
{
class Program
{
static void Main(string[]
args)
{
var c1
= new Complex(1,2);
var c2
= new Complex(3, 4);
var add
= Complex.Add(c1,c2);
Console.WriteLine("Complex
Numbers Addition:"+add);
var sub
= Complex.Subtract(c1, c2);
Console.WriteLine("Complex
Numbers Division:"+sub);
var mul
= Complex.Multiply(c1, c2);
Console.WriteLine("Complex
Numbers Division:"+mul);
var div
= Complex.Divide(c1, c2);
Console.WriteLine("Complex
Numbers Division:"+div);
Console.ReadLine();
}

Join the conversation! Your thoughts help the community grow.