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.Numerics namespace. 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.
Download PDF
I
packed total article in PDF format.Click following link to download
total article
URL:http://www.4shared.com/file/229423533/6aa5b432/CollectionClassFramework40_24_.html
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();
}
}
}
Output:Complex
Numbers Addition :(4, 6)
Complex Numbers Substraction:(-2, -2)
Complex
Numbers Division:(-5, 10)
Complex Numbers Division :( 0.44, 0.08)
Example-3:
Magnitude Property
namespace ComplexNumbers
{
class Program
{
static void Main(string[] args)
{
var c1 = new Complex(1,2);
var c2 = new Complex(3, 4);
//Magnitude of c1=sqrt(1^2 + 2^2)
var magnitude = c1.Magnitude;
Console.WriteLine(magnitude);
Console.ReadLine();
}
}
}
Output:2.23606797749979
Example-4:
Real Stuff with Trigonometric Functions
In
this example I am going to put my hands on Complex Numbers with
Exponentials and Trigonometric hyperbolic functions. Some of the
Formulae were depicted below for better understanding the Concept.
- Exponential of exp(x+iy) = ex[cos(y)+isin(y)] = ex cis(y)
- Exponential of cosh(x+iy)= exp(x+iy)+exp(?x?iy) / 2
- Exponential
of sinh(x+iy)= exp(x+iy)?exp(?x?iy) / 2
The above expression
seems to be very much complicated. But my .net framework 4.0 solves this
kind of problems on a fly. That is the power of my
System.Numerics.Complex() Class under BCL.
namespace ComplexNumbers
{
class Program
{
static void Main(string[] args)
{
Var c1 = new Complex(1, 2);
//exp(x+iy) = ex[cos(y)+isin(y)] = ex cis(y)
var exponent = Complex.Exp(c1);
Console.WriteLine("Exponent="+exponent);
//cosh(x+iy)= exp(x+iy)+exp(-x-iy) / 2
var cosine = Complex.Cosh(c1);
Console.WriteLine("Cosine Exponent" + cosine);
//sinh(x+iy)= exp(x+iy)-exp(-x-iy) / 2
var sine = Complex.Sinh(c1);
Console.WriteLine("SineExponent"+sine);
Console.ReadLine();
}
}
}
Output:Exponent=
(-1.13120438375681, 2.47172667200482)
CosineExponent
(-0.64214812471552, 1.06860742138278)
SineExponent(-0.489056259041294,
1.40311925062204)
Advantages:
- 1.Electrical
Engineers deal with power Systems using complex numbers. They
Calculates Resistance(R) and Reactance(X) to calculate the impedance Z.
- Used
In Vector Calculus as well as Graphs
- All most all Electric and
Electronic Engineers Work with Complex Numbers.
- In Our Real
Time Development Scenario –we should easily Come out with Energy or
Scientific Projects by using all the Functions in Complex () Class
Conclusion:
I
hope this article will give you the brief idea regarding complex
Numbers manipulation by using new BCL in .net framework 4.0.