Complex number class

image_pdfimage_print
   
 
//http://extensionlibrary.codeplex.com/
//The MIT License (MIT)

using System;
using System.Collections.Generic;
using System.Text;

namespace ExtensionLibrary.Mathematics
{
    /// <summary>
    ///
    /// </summary>
    public class Complex
    {
        #region Fields

        private double imag;

        private double real;

        #endregion

        #region Properties

        public double Imag
        {
            get { return imag; }
            set { imag = value; }
        }

        public double Real
        {
            get { return real; }
            set { real = value; }
        }

        #endregion

        #region Constructors

        public Complex()
        {
            imag = 0.0;
            real = 0.0;
        }

        public Complex(double imag, double real)
        {
            this.imag = imag;
            this.real = real;
        }

        #endregion

        #region Methods

        public static Complex Add(Complex c1, Complex c2)
        {
            return new Complex(c1.imag + c2.imag, c1.real + c2.real);
        }

        public static Complex Substract(Complex c1, Complex c2)
        {
            return new Complex(c1.imag - c2.imag, c1.real - c2.real);
        }

        public static Complex Multiple(Complex c1, Complex c2)
        {
            throw new NotImplementedException();            
        }

        public static Complex Divide(Complex c1, Complex c2)
        {
            throw new NotImplementedException();
        }

        public static Complex Negative(Complex c)
        {
            return new Complex(-c.imag, -c.real);
        }

        public static Complex Sin(Complex c)
        {
            throw new NotImplementedException();
        }

        public static Complex Cos(Complex c)
        {
            throw new NotImplementedException();
        }

        public static Complex Tan(Complex c)
        {
            throw new NotImplementedException();
        }

        public static Complex Sinh(Complex c)
        {
            throw new NotImplementedException();
        }

        public static Complex Cosh(Complex c)
        {
            throw new NotImplementedException();
        }

        public static Complex Tanh(Complex c)
        {
            throw new NotImplementedException();
        }

        public static Complex Sqrt(Complex c)
        {
            throw new NotImplementedException();
        }

        public static Complex Exp(Complex c)
        {
            throw new NotImplementedException();            
        }

        public static Complex Pow(Complex x, Complex y)
        {
            throw new NotImplementedException();
        }

        #endregion

        public override string ToString()
        {
            if (double.IsNaN(real) || double.IsNaN(imag))
            {
                return double.NaN.ToString();
            }

            if (double.IsNegativeInfinity(real) || double.IsNegativeInfinity(imag))
            {
                return double.NegativeInfinity.ToString();
            }

            if (double.IsPositiveInfinity(real) || double.IsPositiveInfinity(imag))
            {
                return double.NegativeInfinity.ToString();
            }

            if (imag == 0.0)
            {
                return real.ToString();
            }
            
            if (real == 0.0)
            {
                return imag == 0.0 ? "0" : string.Format("{0}i", imag);
            }

            return string.Format("{0} + {1}i", real, imag);
        }
    }
}

   
     


A Complex Number Class

image_pdfimage_print

   
 
/*
A Programmer&#039;s Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/
// 25 - Operator OverloadingA Complex Number Class
// copyright 2000 Eric Gunnerson
using System;

struct Complex
{
    float real;
    float imaginary;
    
    public Complex(float real, float imaginary)
    {
        this.real = real;
        this.imaginary = imaginary;
    }
    
    public float Real
    {
        get
        {
            return(real);
        }
        set
        {
            real = value;
        }
    }
    
    public float Imaginary
    {
        get
        {
            return(imaginary);
        }
        set
        {
            imaginary = value;
        }
    }
    
    public override string ToString()
    {
        return(String.Format("({0}, {1}i)", real, imaginary));
    }
    
    public static bool operator==(Complex c1, Complex c2)
    {
        if ((c1.real == c2.real) &amp;&amp;
        (c1.imaginary == c2.imaginary))
        return(true);
        else
        return(false);
    }
    
    public static bool operator!=(Complex c1, Complex c2)
    {
        return(!(c1 == c2));
    }
    
    public override bool Equals(object o2)
    {
        Complex c2 = (Complex) o2;
        
        return(this == c2);
    }
    
    public override int GetHashCode()
    {
        return(real.GetHashCode() ^ imaginary.GetHashCode());
    }
    
    public static Complex operator+(Complex c1, Complex c2)
    {
        return(new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary));
    }
    
    public static Complex operator-(Complex c1, Complex c2)
    {
        return(new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary));
    }
    
    // product of two complex numbers
    public static Complex operator*(Complex c1, Complex c2)
    {
        return(new Complex(c1.real * c2.real - c1.imaginary * c2.imaginary,
        c1.real * c2.imaginary + c2.real * c1.imaginary));
    }
    
    // quotient of two complex numbers
    public static Complex operator/(Complex c1, Complex c2)
    {
        if ((c2.real == 0.0f) &amp;&amp;
        (c2.imaginary == 0.0f))
        throw new DivideByZeroException("Can&#039;t divide by zero Complex number");
        
        float newReal = 
        (c1.real * c2.real + c1.imaginary * c2.imaginary) /
        (c2.real * c2.real + c2.imaginary * c2.imaginary);
        float newImaginary = 
        (c2.real * c1.imaginary - c1.real * c2.imaginary) /
        (c2.real * c2.real + c2.imaginary * c2.imaginary);
        
        return(new Complex(newReal, newImaginary));
    }
    
    // non-operator versions for other languages
    public static Complex Add(Complex c1, Complex c2)
    {
        return(c1 + c2);
    }
    
    public static Complex Subtract(Complex c1, Complex c2)
    {
        return(c1 - c2);
    }
    
    public static Complex Multiply(Complex c1, Complex c2)
    {
        return(c1 * c2);
    }
    
    public static Complex Divide(Complex c1, Complex c2)
    {
        return(c1 / c2);
    }
}

public class AComplexNumberClass
{
    public static void Main()
    {
        Complex c1 = new Complex(3, 1);
        Complex c2 = new Complex(1, 2);
        
        Console.WriteLine("c1 == c2: {0}", c1 == c2);
        Console.WriteLine("c1 != c2: {0}", c1 != c2);
        Console.WriteLine("c1 + c2 = {0}", c1 + c2);
        Console.WriteLine("c1 - c2 = {0}", c1 - c2);
        Console.WriteLine("c1 * c2 = {0}", c1 * c2);
        Console.WriteLine("c1 / c2 = {0}", c1 / c2);
    }
}

           
         
     


Demonstates using checked keyword to detect an overflow 2

image_pdfimage_print

   

/*
C# Programming Tips &amp; Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
//  OvrFlow1.cs -- Demonstates using checked keyword to detect an overflow.
//
//          Compile this program with the following command line:
//              C:>csc OvrFlow1.cs
//
namespace nsOverflow
{
    using System;
    
    public class OvrFlow1
    {
        static public void Main ()
        {
            int large = 2147483647;
            int larger = large;
            try
            {
                larger = checked (++larger);
            }
            catch (OverflowException e)
            {
                Console.WriteLine ("The operation caused an overflow");
                Console.WriteLine (e.Message);
            }
            Console.WriteLine ("large = " + large);
            Console.WriteLine ("larger = " + larger);
        }
    }
}


           
          


Checking for overflows.

image_pdfimage_print
   
 


using System;
public class MainClass {
    public int CheckedAddition(short s1, short s2) {
        int z = 0;
        try {
            z = checked((short)(s1 + s2));
        } catch (OverflowException) {
            Console.WriteLine("Overflow Exception, Returning 0");
        }
        return z;
    }
    public int UncheckedAddition(short s1, short s2) {
        int z = ((short)(s1 + s2));
        return z;
    }

    public static void Main() {
        MainClass app = new MainClass();
        short s1 = 32767;
        short s2 = 32767;

        Console.WriteLine("Checked Addition is: {0}",app.CheckedAddition(s1, s2));
        Console.WriteLine("Unchecked Addition is: {0}",app.UncheckedAddition(s1, s2));
    }
}