Entries Tagged as 'Collage'

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

ْْX vs !X

X vs !X

Tes7a aw maTes7ash …. Tenzel el kollia fatra owla aw Tanya aw talta msh far2a … no23od shahreen msh 3arfeen el manhag ray7 feen wgay mneen msh far2a … t7el fl mid-term aw mat7ellsh … bardo msh far2a :?

Howa da el 7al fl el term el awal … twahan fe twahan … tanfeed taaam w2hmal 3la el 25er . mnaheg ta3bana ,mo3eedeen zoraf gedan, da 3′eer el 2ltzam el fazee3 bl gadwal bta3na :D

3wamel kteer wsbab aktar el mohem n el term fl a5er baz mny … mafo2tsh feeh 5als atla3 mn course a5osh fProject … da 3′eer el wa2t el fazee3 elle kan bydee3 fel Site .kol da fl a5er 5lany arfa3 sh3ar ‘X’ .

X fl digital mean “Don’t Care” whowa da el 7al … la sections walla reports walla mid-term walla ay 7aga f7aga … kollo tat sh3ar ‘X:D

Lkn el 7war kda msh naf3 5als … ana msh hynfa3 a3ady 3la el 7rokrok kda … wbsra7a ana msh met3awd 3la 7aga zy keda … el sho3′el el technical wel interests 3moman she2 gameel awy … bas lazem balance 3shan may7salsh dorp lakadar AllaH .

Da ba2a el !X ! = ‘Not‘ fl programming …. El mohem n ana naweet a3′yar el wad3 da .. ISA ykoon feh 2htmam akbar bel drasa zy el IT …kman ana shayf kza 7aga hatsa3dny fl kessa de :

- feh kaza madda gamdeen awy el term da (Operating Systems – C# Programming – Web applications ) fl 7aya hatkoon exciting ISA :D

- mafeeeeeeeeeeeesh Math … bsara7a el ryada ba2t 3′abia awy fel a5er … ana mn el nas elle bet7eb el math gedan .. bas elle bna5do da akeed msh math … 27na bna5od wghat nazr sha5sia ll3olma2 fe 7al msa2el malhash lazma :? kollo dash fe dash :?

- No more responsibilities :D wde 7aga gamda gedan .

ISA rabna ywafa2ny wkol el dof3a wkoon term kways wActive gedan ISA .

Ah nseeet … el kalam da kollo lw 3adeet fl term el awal :? Rabena yostor wallahy :)

salam