Complex Numbers Class in .Net
Salam 3lykom
As my friends (Daif and MadCore
) told me that we have to report a programs deals with the complex number – to simulate some equation in Electric Circuits – I found it a great chance to Create a new TYPE from scratch
.
Complex Numbers
There are no class in .Net that deal with complex numbers and its operation , So you must implement it yourself … I don’t think it is hard mission ,but it’s great thing to create new types.
CompleXLib features:
1- Define the Complex number by Real & Imaginary Form (Re + Imi)
2- Define the Complex number by Polar Form ( Magnitude , Angle )
3- Handle the equal (==) and not equal (!=) operators
4- Override ToString() Method to get the Complex Numbers From ( “Re+Imi”)
5- Implementation of the basic Arithmetic Operations (Sum ,Subtract , Multiplication ,Division)
//CompleXLib still under developing and I’m trying to solve polar constructor issue
using System;
using System.Collections.Generic;
using System.Text;
namespace CompleXLib
{
public class Complex
{
//Internal Data Members
double re, im , magnitude ;
float angle;
//Empty Constructor ==> 0+0i
/// <summary>
/// No parameters to get zero complex number == (0+0i)
/// </summary>
public Complex()
{
this.re = 0;
this.im = 0;
this.magnitude = 0;
this.angle = 0;
}
//Overloading Constructor for Real & Imaginary
/// <summary>
/// Complex number with initial values (Real & Imaginary Format)
/// </summary>
/// <param name=”re”>The Real term of the Complex number </param>
/// <param name=”im”>The Imaginary term of the Complex number</param>
public Complex(double re, double im)
{
this.re = re;
this.im = im;
this.angle = this.GetAngle();
this.magnitude = this.Absolute();
}
//Overloading Constructor for Polar
/// <summary>
/// Complex number with initial values (Polar Format)
/// </summary>
/// <param name=”mag”>The Magnitude of the Complex number</param>
/// <param name=”angle”>The Angle of the Complex number</param>
/// <param name=”PolarFlag”>Just a flag to get the Polar format … NO USE</param>
public Complex(double mag, float angle ,bool PolarFlag)
{
this.angle = angle;
this.magnitude = mag;
this.im = GetImaginary();
this.re = GetReal();
}
#region Properties :
/// <summary>
/// Get or Set the Angle of the Complex number
/// </summary>
public float Angle
{
get { return this.angle; }
set
{
this.angle = value;
this.re = GetReal();
this.im = GetImaginary();
}
}
/// <summary>
/// Get or Set the Magnitude of the Complex number
/// </summary>
public double Magnitude
{
get { return this.magnitude; }
set
{
this.magnitude = value;
this.re = GetReal();
this.im = GetImaginary();
}
}
/// <summary>
/// Get or Set the Real term of the Complex number
/// </summary>
public double Re
{
get { return this.re; }
set
{
this.re = value;
this.angle = GetAngle();
this.magnitude = Absolute();
}
}
/// <summary>
/// Get or Set the Imaginary term of the Complex number
/// </summary>
public double Im
{
get { return this.im; }
set
{
this.im = value;
this.angle = GetAngle();
this.magnitude = Absolute();
}
}
#endregion
#region Internal Methods
//This method to get the phase Q … as angle = (@+Q)
int GetPhase()
{
int phase = 0;
if ((Math.Sign(this.Re) == -1) && (Math.Sign(this.Im) == 1))
phase = 180;
else if ((Math.Sign(this.Re) == -1) && (Math.Sign(this.Im) == -1))
phase = 180;
else if ((Math.Sign(this.Re) == 1) && (Math.Sign(this.Im) == -1))
phase = 360;
return phase;
}
//Get the Exact angle of the Complex number … depend on Re and Im
float GetAngle()
{
int phase = this.GetPhase();
double angle = 0;
if (this.re != 0)
angle = phase + ((180 / Math.PI) * (Math.Atan(this.im / this.re)));
return (float)angle;
}
//Get the Real term of the Complex number … depend on Magnitude and Angle
double GetReal()
{
double re = (this.magnitude * Math.Cos((Math.PI /180)* this.angle));
return re;
}
//Get the Imaginary term of the Complex number … depend on Magnitude and Angle
double GetImaginary()
{
double im = (this.magnitude * Math.Sin((Math.PI/180)* this.angle));
return im;
}
#endregion
//Overriding ToString() Method to get Complex numbers format
/// <summary>
/// Get the Complex numbers Format ( Re+Imi)
/// </summary>
/// <returns>String</returns>
public override string ToString()
{
char sign;
if (this.Im >= 0)
{ sign = ‘+’; }
else { sign = ‘-’; }
return (String.Format(“{0}{1}{2}i”, this.Re, sign, Math.Abs(this.Im)));
}
//Overloading == operator
public static bool operator==(Complex c1, Complex c2)
{
if ((c1.Re == c2.Re) && (c1.Im == c2.Im))
return true;
else return false;
}
//Overloading != operator
public static bool operator !=(Complex c1, Complex c2)
{
return (!(c1 == c2));
}
//Static versions of the methods
#region Static Methods
/// <summary>
/// Get the Conjugate of the Complex number
/// </summary>
/// <param name=”c”>Complex number</param>
/// <returns>Complex number</returns>
public static Complex Conjugate(Complex c)
{
return new Complex(c.Re, (-1 * c.Im));
}
/// <summary>
/// Get the Absolute value of the Complex number
/// </summary>
/// <param name=”c”>Complex number</param>
public static double Absolute(Complex c)
{
double abs = (c.re * c.re + c.im * c.im);
return Math.Sqrt(abs);
}
#endregion
#region Object Methods
/// <summary>
/// Get the Absolute value of the Complex number
/// </summary>
public double Absolute()
{
return Complex.Absolute(this);
}
/// <summary>
/// Get the Conjugate of the Complex number
/// </summary>
public Complex Conjugate()
{
return Complex.Conjugate(this);
}
#endregion
#region Operators
//Defines Operations and Operators
public static Complex operator +(Complex c1, Complex c2)
{
return (new Complex(c1.Re + c2.Re, c1.Im + c2.Im));
}
public static Complex operator -(Complex c1, Complex c2)
{
return (new Complex(c1.Re - c2.Re, c1.Im - c2.Im));
}
public static Complex operator *(Complex c1, Complex c2)
{
return (new Complex((c1.Re * c2.Re - c1.Im * c2.Im), (c1.Re * c2.Im + c2.Re * c1.Im)));
}
public static Complex operator /(Complex c1, Complex c2)
{
if ((c2.Re == 0) && (c2.Im == 0))
throw new DivideByZeroException(“Division by Zero is not allowed”);
double re = (c1.Re * c2.Re + c1.Im * c2.Im) /
(c2.Re * c2.Re + c2.Im * c2.Im);
double im = (c2.Re * c1.Im - c1.Re * c2.Im) /
(c2.Re * c2.Re + c2.Im * c2.Im);
return (new Complex(re, im));
}
#endregion
}
}
//End of Code
Download CompleXLib with Test project:
http://www.4shared.com/file/40075838/7bab2ae4/TestComplex.html
W8ing feedbacks & Gug-Reports
Salam