Resources » Code Snippets » C# Syntax

Examples On Complex() Class C#.4.0-New Feature in Framework 4.0


Posted Date: 02-Mar-2010  Last Updated:   Category: C# Syntax    
Author: Member Level: Gold    Points: 12


Complex() Class,VS2010,ASP.NET 4.0,C#



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

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

2.Used In Vector Calculus as well as Graphs

3.All most all Electric and Electronic Engineers Work with Complex Numbers.

4.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 Code will give you the brief idea regarding complex Numbers manipulation by using new BCL in .net framework 4.0.


Did you like this resource? Share it with your friends and show your love!

Responses to "Examples On Complex() Class C#.4.0-New Feature in Framework 4.0"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India